首页 > 解决方案 > Geopandas 无法在现有路径上写入文件

问题描述

我有一个 Geopandas 数据框gdf,我想将其作为新文件写入现有目录。但是,当我执行该行时:

gdf.to_file("/mnt/path/to/directory/newfile.json", driver="GeoJSON")

我收到这个错误

DriverError: Failed to create GeoJSON datasource: /mnt/path/to/directory/newfile.json: /mnt/path/to/directory/newfile.json: No such file or directory
---------------------------------------------------------------------------
CPLE_OpenFailedError                      Traceback (most recent call last)
fiona/_shim.pyx in fiona._shim.gdal_create()

fiona/_err.pyx in fiona._err.exc_wrap_pointer()

CPLE_OpenFailedError: Failed to create GeoJSON datasource: /mnt/path/to/directory/newfile.json: /mnt/path/to/directory/newfile.json: No such file or directory

During handling of the above exception, another exception occurred:

DriverError                               Traceback (most recent call last)
<command-3865967859284855> in <module>
      1 gdf = geopandas.GeoDataFrame(df, geometry=geopandas.points_from_xy(df.Longitude, df.Latitude))
----> 2 gdf.to_file("/mnt/path/to/directory/newfile.json", driver="GeoJSON")

/databricks/python/lib/python3.7/site-packages/geopandas/geodataframe.py in to_file(self, filename, driver, schema, index, **kwargs)
   1112         from geopandas.io.file import _to_file
   1113 
-> 1114         _to_file(self, filename, driver, schema, index, **kwargs)
   1115 
   1116     def set_crs(self, crs=None, epsg=None, inplace=False, allow_override=False):

/databricks/python/lib/python3.7/site-packages/geopandas/io/file.py in _to_file(df, filename, driver, schema, index, mode, crs, **kwargs)
    392             crs_wkt = crs.to_wkt("WKT1_GDAL")
    393         with fiona.open(
--> 394             filename, mode=mode, driver=driver, crs_wkt=crs_wkt, schema=schema, **kwargs
    395         ) as colxn:
    396             colxn.writerecords(df.iterfeatures())

/databricks/python/lib/python3.7/site-packages/fiona/env.py in wrapper(*args, **kwargs)
    406     def wrapper(*args, **kwargs):
    407         if local._env:
--> 408             return f(*args, **kwargs)
    409         else:
    410             if isinstance(args[0], str):

/databricks/python/lib/python3.7/site-packages/fiona/__init__.py in open(fp, mode, driver, schema, crs, encoding, layer, vfs, enabled_drivers, crs_wkt, **kwargs)
    272             c = Collection(path, mode, crs=crs, driver=driver, schema=this_schema,
    273                            encoding=encoding, layer=layer, enabled_drivers=enabled_drivers, crs_wkt=crs_wkt,
--> 274                            **kwargs)
    275         else:
    276             raise ValueError(

/databricks/python/lib/python3.7/site-packages/fiona/collection.py in __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

fiona/ogrext.pyx in fiona.ogrext.WritingSession.start()

fiona/ogrext.pyx in fiona.ogrext.WritingSession.start()

fiona/_shim.pyx in fiona._shim.gdal_create()

DriverError: Failed to create GeoJSON datasource: /mnt/path/to/directory/newfile.json: /mnt/path/to/directory/newfile.json: No such file or directory

路径存在,我可以通过资源管理器访问它,Pandas/Spark 能够读取其中的数据。我尝试创建一个空文件,/mnt/path/to/directory/newfile.json但它根本没有改变任何东西。我也尝试将"newfile.json"其作为写入路径,但会引发以下错误:

ValueError: Invalid field type <class 'decimal.Decimal'>
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<command-3865967859284855> in <module>
      1 gdf = geopandas.GeoDataFrame(df, geometry=geopandas.points_from_xy(df.Longitude, df.Latitude))
----> 2 gdf.to_file('geo.json', driver="GeoJSON")

/databricks/python/lib/python3.7/site-packages/geopandas/geodataframe.py in to_file(self, filename, driver, schema, index, **kwargs)
   1112         from geopandas.io.file import _to_file
   1113 
-> 1114         _to_file(self, filename, driver, schema, index, **kwargs)
   1115 
   1116     def set_crs(self, crs=None, epsg=None, inplace=False, allow_override=False):

/databricks/python/lib/python3.7/site-packages/geopandas/io/file.py in _to_file(df, filename, driver, schema, index, mode, crs, **kwargs)
    394             filename, mode=mode, driver=driver, crs_wkt=crs_wkt, schema=schema, **kwargs
    395         ) as colxn:
--> 396             colxn.writerecords(df.iterfeatures())
    397 
    398 

/databricks/python/lib/python3.7/site-packages/fiona/collection.py in writerecords(self, records)
    359         if self.mode not in ('a', 'w'):
    360             raise IOError("collection not open for writing")
--> 361         self.session.writerecs(records, self)
    362         self._len = self.session.get_length()
    363         self._bounds = None

fiona/ogrext.pyx in fiona.ogrext.WritingSession.writerecs()

fiona/ogrext.pyx in fiona.ogrext.OGRFeatureBuilder.build()

ValueError: Invalid field type <class 'decimal.Decimal'>

你知道发生了什么事吗?谢谢

PS:我在 Azure 上使用 Databricks 来运行我的 python 代码

标签: pythondataframegeopandaswriting

解决方案


推荐阅读