<div dir="ltr">Hi Sarah,<br>Thank you so much for your help!<div>Writing speed for GPKG format with fiona has been very slow.</div><div>I wanted to extract one by one ids and write geom and tag using ogr/python.</div><div>Could you help me how to extract geom and tag of each id in a for loop without using class and definitions?</div></div><br><div class="gmail_quote"><div dir="ltr">On Thu, Nov 1, 2018 at 6:07 PM Sarah Hoffmann <<a href="mailto:lonvia@denofr.de" target="_blank">lonvia@denofr.de</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi,<br>
<br>
On Thu, Nov 01, 2018 at 12:13:40PM +0900, koji higuchi wrote:<br>
> *I tried to extract data from .osm.pbf file and write to shapefile as<br>
> follows:*<br>
> <br>
> import os, osmium, fiona<br>
> <br>
> fi = 'europe-latest.osm.pbf'<br>
> fo = 'europe-latest.shp'<br>
> <br>
> drv = 'ESRI Shapefile'<br>
> <br>
> crs = {'no_defs': True, 'ellps': 'WGS84', 'datum': 'WGS84', 'proj':<br>
> 'longlat'}<br>
> <br>
> schema = {'geometry': 'LineString',<br>
> 'properties': {'id': 'float', 'name' : 'str', 'kind' :<br>
> 'str'}}<br>
> <br>
> outfile = fiona.open(fo, 'w', driver=drv, crs=crs, schema=schema)<br>
> <br>
> geomfab = osmium.geom.GeoJSONFactory()<br>
> <br>
> class ShapeConverter(osmium.SimpleHandler):<br>
> def way(self, w):<br>
> if 'place' in w.tags:<br>
> rec = {'geometry' : eval(geomfab.create_linestring(w)),<br>
> 'properties' : {'id' : float(<a href="http://w.id" rel="noreferrer" target="_blank">w.id</a>),<br>
> 'name' : w.tags.get('name'),<br>
> 'kind' : w.tags['place']}}<br>
> outfile.write(rec)<br>
> <br>
> ShapeConverter().apply_file(fi, locations=True)<br>
> <br>
> I got the following error after extracting several contents:<br>
> <br>
> rec = {'geometry' : eval(geomfab.create_linestring(w)),<br>
> RuntimeError: need at least two points for linestring (way_id=619453148)<br>
> <br>
> How could I skip that erroneous id and extract data for other working ids?<br>
<br>
You need to do this manually yourself in the handler. I recommend<br>
simply catching the exception as there are some other error<br>
conditions besides too few points:<br>
<br>
def way(self, w):<br>
if 'place' in w.tags:<br>
try:<br>
geom = geomfab.create_linestring(w)<br>
except:<br>
print("Skipping way with bad geometry")<br>
return<br>
<br>
rec = {'geometry' : eval(geom),<br>
'properties' : {'id' : float(<a href="http://w.id" rel="noreferrer" target="_blank">w.id</a>),<br>
'name' : w.tags.get('name'),<br>
'kind' : w.tags['place']}}<br>
outfile.write(rec)<br>
<br>
Kind regards<br>
<br>
Sarah<br>
</blockquote></div>