Hi, On Thu, Nov 01, 2018 at 12:13:40PM +0900, koji higuchi wrote: > *I tried to extract data from .osm.pbf file and write to shapefile as > follows:* > > import os, osmium, fiona > > fi = 'europe-latest.osm.pbf' > fo = 'europe-latest.shp' > > drv = 'ESRI Shapefile' > > crs = {'no_defs': True, 'ellps': 'WGS84', 'datum': 'WGS84', 'proj': > 'longlat'} > > schema = {'geometry': 'LineString', > 'properties': {'id': 'float', 'name' : 'str', 'kind' : > 'str'}} > > outfile = fiona.open(fo, 'w', driver=drv, crs=crs, schema=schema) > > geomfab = osmium.geom.GeoJSONFactory() > > class ShapeConverter(osmium.SimpleHandler): > def way(self, w): > if 'place' in w.tags: > rec = {'geometry' : eval(geomfab.create_linestring(w)), > 'properties' : {'id' : float(w.id), > 'name' : w.tags.get('name'), > 'kind' : w.tags['place']}} > outfile.write(rec) > > ShapeConverter().apply_file(fi, locations=True) > > I got the following error after extracting several contents: > > rec = {'geometry' : eval(geomfab.create_linestring(w)), > RuntimeError: need at least two points for linestring (way_id=619453148) > > How could I skip that erroneous id and extract data for other working ids?
You need to do this manually yourself in the handler. I recommend simply catching the exception as there are some other error conditions besides too few points: def way(self, w): if 'place' in w.tags: try: geom = geomfab.create_linestring(w) except: print("Skipping way with bad geometry") return rec = {'geometry' : eval(geom), 'properties' : {'id' : float(w.id), 'name' : w.tags.get('name'), 'kind' : w.tags['place']}} outfile.write(rec) Kind regards Sarah _______________________________________________ dev mailing list dev@openstreetmap.org https://lists.openstreetmap.org/listinfo/dev