【问题标题】:Error reading shapefile with Geopandas in Python在 Python 中使用 Geopandas 读取 shapefile 时出错
【发布时间】:2019-06-21 05:01:51
【问题描述】:

我正在尝试使用 geopandas 读取 shapefile,为此我使用了gp.read_file

import geopandas as gp
fl="M:/rathore/vic_5km/L2_data/L2_data/DAMSELFISH_distributions.shp"
data=gp.read_file(fl)

我收到以下错误: TypeError: invalid path: UnparsedPath(path='M:/rathore/vic_5km/L2_data/L2_data/DAMSELFISH_distributions.shp')

问题的追溯是:

----> 1 data=gp.read_file(fl)

c:\python27\lib\site-packages\geopandas\io\file.pyc in read_file(filename, bbox, **kwargs)
     75 
     76     with fiona_env():
---> 77         with reader(path_or_bytes, **kwargs) as features:
     78 
     79             # In a future Fiona release the crs attribute of features will

c:\python27\lib\site-packages\fiona\fiona\env.pyc in wrapper(*args, **kwargs)
    395     def wrapper(*args, **kwargs):
    396         if local._env:
--> 397             return f(*args, **kwargs)
    398         else:
    399             if isinstance(args[0], str):

c:\python27\lib\site-packages\fiona\__init__.pyc in open(fp, mode, driver, schema, crs, encoding, layer, vfs, enabled_drivers, crs_wkt, **kwargs)
    255         if mode in ('a', 'r'):
    256             c = Collection(path, mode, driver=driver, encoding=encoding,
--> 257                            layer=layer, enabled_drivers=enabled_drivers, **kwargs)
    258         elif mode == 'w':
    259             if schema:

c:\python27\lib\site-packages\fiona\fiona\collection.pyc in __init__(self, path, mode, driver, schema, crs, encoding, layer, vsi, archive, enabled_drivers, crs_wkt, ignore_fields, ignore_geometry, **kwargs)
     54 
     55         if not isinstance(path, (string_types, Path)):
---> 56             raise TypeError("invalid path: %r" % path)
     57         if not isinstance(mode, string_types) or mode not in ('r', 'w', 'a'):
     58             raise TypeError("invalid mode: %r" % mode)

TypeError: invalid path: UnparsedPath(path='M:/rathore/vic_5km/L2_data/L2_data/DAMSELFISH_distributions.shp')

我猜fiona有一些问题,但我不太了解。 我已经在我的系统中安装了fiona 1.8.6geopandas 0.5.0 版本。我正在使用 python 2.7

【问题讨论】:

  • 驱动器号M: 是指本地网络上的共享文件夹吗?
  • M: 是因为外置硬盘

标签: python geopandas parse-error fiona


【解决方案1】:

它说路径无效。我会尝试在您的路径中替换逗号:

之前:

fl="M:/rathore/vic_5km/L2_data/L2_data/DAMSELFISH_distributions,shp"

之后:

fl="M:/rathore/vic_5km/L2_data/L2_data/DAMSELFISH_distributions.shp"

【讨论】:

  • 逗号是输入错误,我已经编辑了问题。同样的错误仍然存​​在
【解决方案2】:

我认为这与 fiona 和 geopandas 的版本无关。您可以通过命令提示符/终端找到文件的路径并使用它来读取文件吗 如下所示

fl="/Users/xxxx/Downloads/Data/DAMSELFISH_distributions.shp"

希望对你有帮助

【讨论】:

  • 如何在 UNC 中获取文件路径?
【解决方案3】:

首先,isinstance(your_path, ((str,), Path))fiona\collection.pyc 中被评估为False,这很奇怪。

另外,those lines in fiona/init.py 模块应该处理您的路径是 pathlib.Path 对象的情况,但它显然不是...我不确定,这可能是 fiona 的问题。

我还是觉得问题出在fiona的parse_path(path)方法上。

以下是您应该尝试的一些事情:

  1. 如果 M: 是本地网络上的共享文件夹,请尝试将“M:/...”替换为 UNC 路径(例如:“//local/folder/path/to/file”)
  2. 如果您使用 IDE(如 Pycharm、Eclipse 或 VsCode),请在第三行添加断点,使用调试器并继续深入,直到到达 parse_data 方法。然后看看你的路径是什么样的。如果您不使用 IDE,请考虑安装一个...
  3. 如果可以,请尝试将 shapefile 复制到与您的 python 脚本相同的驱动器上,并检查错误是否仍然出现。

【讨论】:

  • 嗨,我不知道如何在 unc 中获取文件路径,但我将 shapefile 复制到同一个驱动器中,但仍然存在相同的错误:TypeError: invalid path: ParsedPath(path='DAMSELFISH_distributions.shp', archive=None, scheme=None)
  • 我认为你应该在 geopandas 的github repo 上创建一个问题,因为它真的很奇怪......
  • 我做了,但没有人回复
猜你喜欢
  • 2021-05-22
  • 2021-12-16
  • 2021-08-19
  • 1970-01-01
  • 1970-01-01
  • 2021-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多