代码之家  ›  专栏  ›  技术社区  ›  Kernel

geolands缺少states.geojson文件

  •  1
  • Kernel  · 技术社区  · 7 月前

    所有, 在尝试导入states.geojson文件时,我遇到了以下错误,如本页所述 https://www.twilio.com/en-us/blog/geospatial-analysis-python-geojson-geopandas-html 我认为这个文件是预先安装的带有地理区域的文件之一。

    我使用的是geopandas 0.14.4版本

    import geopandas as gpd
    states = gpd.read_file('states.geojson') 
    

    这是错误

    Traceback (most recent call last):
    
      File fiona/ogrext.pyx:130 in fiona.ogrext.gdal_open_vector
    
      File fiona/ogrext.pyx:134 in fiona.ogrext.gdal_open_vector
    
      File fiona/_err.pyx:375 in fiona._err.StackChecker.exc_wrap_pointer
    
    CPLE_OpenFailedError: states.geojson: No such file or directory
    
    
    The above exception was the direct cause of the following exception:
    
    Traceback (most recent call last):
    
      Cell In[1], line 2
        states = gpd.read_file('states.geojson') # built in-file
    
      File ~/anaconda3/lib/python3.11/site-packages/geopandas/io/file.py:289 in _read_file
        return _read_file_fiona(
    
      File ~/anaconda3/lib/python3.11/site-packages/geopandas/io/file.py:315 in _read_file_fiona
        with reader(path_or_bytes, **kwargs) as features:
    
      File ~/anaconda3/lib/python3.11/site-packages/fiona/env.py:457 in wrapper
        return f(*args, **kwds)
    
      File ~/anaconda3/lib/python3.11/site-packages/fiona/__init__.py:342 in open
        colxn = Collection(
    
      File ~/anaconda3/lib/python3.11/site-packages/fiona/collection.py:226 in __init__
        self.session.start(self, **kwargs)
    
      File fiona/ogrext.pyx:876 in fiona.ogrext.Session.start
    
      File fiona/ogrext.pyx:136 in fiona.ogrext.gdal_open_vector
    
    DriverError: Failed to open dataset (flags=68): states.geojson
    

    谢谢

    1 回复  |  直到 7 月前
        1
  •  2
  •   jToump    7 月前

    geopanda的read_file方法需要一个文件地址作为输入,如文档中所示 https://geopandas.org/en/stable/docs/reference/api/geopandas.read_file.html

    import geopandas as gpd
    gpd.read_file("./directory/fileName.json")
    

    您要查找的geojson似乎是美国各州的geojson文件。你可以在这里找到这个 https://github.com/PublicaMundi/MappingAPI/blob/master/data/geojson/us-states.json?short_path=1c1ebe5

    下载文件,然后使用该函数读取它并将其存储为地理数据帧