I'm trying to get the street network from a pbf-file. The very weird is: my script did work initially, so I did transform the exact same pbf data files. But suddenly my script doesnt't work anymore.
So when I run this code snippet on a pbf-file ('f'), (which is downloaded from OSM):
osm = pyrosm.OSM(f)
area = osm.get_network(network_type="driving")
I get the error you can see below. Any ideas what I can do?
TypeError Traceback (most recent call last)
Cell In[17], line 38
35 os.makedirs(mypath_shp)
37 osm = pyrosm.OSM(f)
---> 38 area = osm.get_network(network_type="driving")
39 print("Created network - OK")
41 # subset = subsetting(area)
42 # print("Created subset - OK")
43
(...)
53 # subset.to_file(path_shape, driver="ESRI Shapefile")
54 # print(" Shapefile - OK")
File ~\AppData\Local\miniconda3\envs\for_osm\lib\site-packages\pyrosm\pyrosm.py:205, in OSM.get_network(self, network_type, extra_attributes, nodes)
202 self._read_pbf()
204 # Filter network data with given filter
--> 205 edges, node_gdf = get_network_data(
206 self._node_coordinates,
207 self._way_records,
208 tags_as_columns,
209 network_filter,
210 self.bounding_box,
211 slice_to_segments=nodes,
212 )
214 if edges is not None:
215 # Add metadata
216 edges._metadata.append(network_type)
File ~\AppData\Local\miniconda3\envs\for_osm\lib\site-packages\pyrosm\networks.py:37, in get_network_data(node_coordinates, way_records, tags_as_columns, network_filter, bounding_box, slice_to_segments)
34 return None, None
36 # Prepare GeoDataFrame
---> 37 edges, nodes = prepare_geodataframe(
38 nodes,
39 node_coordinates,
40 ways,
41 relations,
42 relation_ways,
43 tags_as_columns,
44 bounding_box,
45 parse_network=True,
46 calculate_seg_lengths=slice_to_segments,
47 )
49 return edges, nodes
File ~\AppData\Local\miniconda3\envs\for_osm\lib\site-packages\pyrosm\frames.pyx:134, in pyrosm.frames.prepare_geodataframe()
File ~\AppData\Local\miniconda3\envs\for_osm\lib\site-packages\pyrosm\frames.pyx:141, in pyrosm.frames.prepare_geodataframe()
File ~\AppData\Local\miniconda3\envs\for_osm\lib\site-packages\pyrosm\frames.pyx:71, in pyrosm.frames.prepare_way_gdf()
File ~\AppData\Local\miniconda3\envs\for_osm\lib\site-packages\geopandas\geodataframe.py:188, in GeoDataFrame.__init__(self, data, geometry, crs, *args, **kwargs)
180 if (
181 hasattr(geometry, "crs")
182 and geometry.crs
183 and crs
184 and not geometry.crs == crs
185 ):
186 raise ValueError(crs_mismatch_error)
--> 188 self.set_geometry(geometry, inplace=True, crs=crs)
190 if geometry is None and crs:
191 raise ValueError(
192 "Assigning CRS to a GeoDataFrame without a geometry column is not "
193 "supported. Supply geometry using the 'geometry=' keyword argument, "
194 "or by providing a DataFrame with column name 'geometry'",
195 )
File ~\AppData\Local\miniconda3\envs\for_osm\lib\site-packages\geopandas\geodataframe.py:346, in GeoDataFrame.set_geometry(self, col, drop, inplace, crs)
343 level.crs = crs
345 # Check that we are using a listlike of geometries
--> 346 level = _ensure_geometry(level, crs=crs)
347 frame[geo_column_name] = level
348 frame._geometry_column_name = geo_column_name
File ~\AppData\Local\miniconda3\envs\for_osm\lib\site-packages\geopandas\geodataframe.py:59, in _ensure_geometry(data, crs)
57 else:
58 if isinstance(data, Series):
---> 59 out = from_shapely(np.asarray(data), crs=crs)
60 return GeoSeries(out, index=data.index, name=data.name)
61 else:
File ~\AppData\Local\miniconda3\envs\for_osm\lib\site-packages\geopandas\array.py:154, in from_shapely(data, crs)
138 def from_shapely(data, crs=None):
139 """
140 Convert a list or array of shapely objects to a GeometryArray.
141
(...)
152
153 """
--> 154 return GeometryArray(vectorized.from_shapely(data), crs=crs)
File ~\AppData\Local\miniconda3\envs\for_osm\lib\site-packages\geopandas\_vectorized.py:145, in from_shapely(data)
143 out.append(None)
144 else:
--> 145 raise TypeError("Input must be valid geometry objects: {0}".format(geom))
147 if compat.USE_PYGEOS:
148 return np.array(out, dtype=object)
TypeError: Input must be valid geometry objects: MULTILINESTRING ((7.646 47.633, 7.646 47.633), (7.646 47.633, 7.646 47.633), (7.646 47.633, 7.646 47.633), (7.646 47.633, 7.646 47.633), (7.646 47.633, 7.647 47.633), (7.647 47.633, 7.647 47.633))
With pyrosm 0.6.1. make sure you have Shapely <=1.8.5 and Numpy <=1.23.5 installed.
See also: https://github.com/HTenkanen/pyrosm/issues/212#issuecomment-1422411238