Error using osmnx:Failed to create field name 'osmid'

27 Views Asked by At

When running the following code in Python 3.11:

import fiona 
import geopandas as gpd 
import pandas as pd 
import networkx as nx 
import osmnx as ox 
G = ox.graph_from_point((37.79, -122.41), dist=1000, network_type='all') 
ox.save_graph_shapefile(G, "2.shp","C:\\MY")

The following error occurred when running the above code:

CPLE_AppDefinedError                      Traceback (most recent call last)
File fiona\ogrext.pyx:1221, in fiona.ogrext.WritingSession.start()

File fiona\_err.pyx:267, in fiona._err.exc_wrap_int()

CPLE_AppDefinedError: Failed to create field name 'osmid': cannot convert to C:\MY

During handling of the above exception, another exception occurred:

SchemaError                               Traceback (most recent call last)
Cell In[2], line 3
      1 G = ox.graph_from_point((37.79, -122.41), dist=1000, network_type='all')
      2 # G_projected = ox.project_graph(G)
----> 3 ox.save_graph_shapefile(G, "2.shp","C:\\MY")

File C:\miniconda3\Lib\site-packages\osmnx\io.py:113, in save_graph_shapefile(G, filepath, encoding, directed)
    110 gdf_edges = _stringify_nonnumeric_cols(gdf_edges)
    112 # save the nodes and edges as separate ESRI shapefiles
--> 113 gdf_nodes.to_file(filepath_nodes, driver="ESRI Shapefile", index=True, encoding=encoding)
    114 gdf_edges.to_file(filepath_edges, driver="ESRI Shapefile", index=True, encoding=encoding)
    115 utils.log(f"Saved graph as shapefiles at {filepath!r}")

File C:\miniconda3\Lib\site-packages\geopandas\geodataframe.py:1264, in GeoDataFrame.to_file(self, filename, driver, schema, index, **kwargs)
   1173 """Write the ``GeoDataFrame`` to a file.
   1174 
   1175 By default, an ESRI shapefile is written, but any OGR data source
   (...)
   1260 
   1261 """
   1262 from geopandas.io.file import _to_file
-> 1264 _to_file(self, filename, driver, schema, index, **kwargs)

File C:\miniconda3\Lib\site-packages\geopandas\io\file.py:612, in _to_file(df, filename, driver, schema, index, mode, crs, engine, **kwargs)
    609     raise ValueError(f"'mode' should be one of 'w' or 'a', got '{mode}' instead")
    611 if engine == "fiona":
--> 612     _to_file_fiona(df, filename, driver, schema, crs, mode, **kwargs)
    613 elif engine == "pyogrio":
    614     _to_file_pyogrio(df, filename, driver, schema, crs, mode, **kwargs)

File C:\miniconda3\Lib\site-packages\geopandas\io\file.py:638, in _to_file_fiona(df, filename, driver, schema, crs, mode, **kwargs)
    636 elif crs:
    637     crs_wkt = crs.to_wkt("WKT1_GDAL")
--> 638 with fiona.open(
    639     filename, mode=mode, driver=driver, crs_wkt=crs_wkt, schema=schema, **kwargs
    640 ) as colxn:
    641     colxn.writerecords(df.iterfeatures())

File C:\miniconda3\Lib\site-packages\fiona\env.py:408, in ensure_env_with_credentials.<locals>.wrapper(*args, **kwargs)
    405 @wraps(f)
    406 def wrapper(*args, **kwargs):
    407     if local._env:
--> 408         return f(*args, **kwargs)
    409     else:
    410         if isinstance(args[0], str):

File C:\miniconda3\Lib\site-packages\fiona\__init__.py:280, in open(fp, mode, driver, schema, crs, encoding, layer, vfs, enabled_drivers, crs_wkt, **kwargs)
    278     else:
    279         this_schema = None
--> 280     c = Collection(path, mode, crs=crs, driver=driver, schema=this_schema,
    281                    encoding=encoding, layer=layer, enabled_drivers=enabled_drivers, crs_wkt=crs_wkt,
    282                    **kwargs)
    283 else:
    284     raise ValueError(
    285         "mode string must be one of 'r', 'w', or 'a', not %s" % mode)

File C:\miniconda3\Lib\site-packages\fiona\collection.py:165, in Collection.__init__(self, path, mode, driver, schema, crs, encoding, layer, vsi, archive, enabled_drivers, crs_wkt, ignore_fields, ignore_geometry, **kwargs)
    163     elif self.mode in ('a', 'w'):
    164         self.session = WritingSession()
--> 165         self.session.start(self, **kwargs)
    166 except IOError:
    167     self.session = None

File fiona\ogrext.pyx:1227, in fiona.ogrext.WritingSession.start()

SchemaError: Failed to create field name 'osmid': cannot convert to C:\MY

I attempted to resolve the issue by reinstalling Osmnx, reinstalling its dependency packages, using virtual environments, etc., but none of them worked and the error persisted.

0

There are 0 best solutions below