简体   繁体   English

在Python中使用Geopandas读取shapefile时出错

[英]Error reading shapefile with Geopandas in Python

I am trying to read a shapefile using geopandas, for which I used gp.read_file 我正在尝试使用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)

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

The traceback to the problem is: 问题的追溯是:

----> 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')

There is some problem with fiona I guess but I do not have much idea about. 我猜想fiona存在一些问题,但我对此并不了解。 I have installed fiona 1.8.6 and geopandas 0.5.0 version installed in my system. 我已经在系统中安装了fiona 1.8.6geopandas 0.5.0版本。 I am using python 2.7 我正在使用python 2.7

It says that path is invalid. 它说路径无效。 I would try to replace comma in your path: 我会尝试在您的路径中替换逗号:

before: 之前:

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

after: 后:

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

I think it has nothing to do with version of fiona and geopandas. 我认为这与fiona和geopandas的版本无关。 Can you find the path of the file through your command prompt/terminal and use the same for reading the file like below 您能否通过命令提示符/终端找到文件的路径,并使用它来读取文件,如下所示

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

Hope this helps 希望这可以帮助

First, isinstance(your_path, ((str,), Path)) is evaluated to False in fiona\\collection.pyc , which is strange. 首先,在fiona\\collection.pyc ,将isinstance(your_path, ((str,), Path))值评估为False ,这很奇怪。

Also, those lines in fiona/ init .py module should take care of the case where your path is a pathlib.Path object, but it apparently don't... I'm not sure, it may be an issue with fiona. 此外, fiona / init .py模块中的这些行应注意路径是pathlib.Path对象的情况,但显然不是……我不确定,这可能是fiona的问题。

I still think the problem come from the parse_path(path) method of fiona. 我仍然认为问题来自fiona的parse_path(path)方法。

Here are some things you should try: 您应该尝试以下一些操作:

  1. If M: is a shared folder on a local network, try replacing 'M:/...' by the UNC path (ex: "//local/folder/path/to/file") 如果M:是本地网络上的共享文件夹,请尝试用UNC路径替换“ M:/ ...”(例如:“ // local / folder / path / to / file”)
  2. If you use an IDE (like Pycharm, Eclipse or VsCode), add a breakpoint on your third line, use the debugger and go deep down until you reach the parse_data method. 如果您使用的是IDE(例如Pycharm,Eclipse或VsCode),请在第三行添加一个断点,使用调试器并深入了解,直到parse_data方法。 then look at what your path looks. 然后看看你的路径。 If you don't use an IDE, consider to install one... 如果您不使用IDE,请考虑安装一个...
  3. If you can, try copying the shapefile on the same drive that your python script, and check if the errors still raises. 如果可以,请尝试将shapefile复制到与python脚本相同的驱动器上,然后检查错误是否仍然出现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM