在 python 上使用 Geopandas 创建交互式地图

Using Geopandas on python to create an interactive map

我正在尝试使用本指南创建交互式地图:This

我在 anaconda 环境下使用 pycharm,所以我不明白什么时候放我的 .shp 文件。这是示例代码:

import geopandas as gpd
shapefile = 'data/countries_110m/ne_110m_admin_0_countries.shp'
#Read shapefile using Geopandas
gdf = gpd.read_file(shapefile)[['ADMIN', 'ADM0_A3', 'geometry']]

我的目录正确吗?

C:\Users\gpont\PycharmProjects\pythonProject2\data\Map\map1\ne_110m_admin_0_countries.shp

错误:

C:\Users\gpont\anaconda3\envs\pythonProject2\python.exe C:/Users/gpont/PycharmProjects/pythonProject2/main.py
Traceback (most recent call last):
  File "fiona/_shim.pyx", line 83, in fiona._shim.gdal_open_vector
  File "fiona/_err.pyx", line 270, in fiona._err.exc_wrap_pointer
fiona._err.CPLE_OpenFailedError: 'map1 e_110m_admin_0_countries.shp' does not exist in the file system, and is not recognized as a supported dataset name.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/gpont/PycharmProjects/pythonProject2/main.py", line 6, in <module>
    gdf = gpd.read_file(shapefile)[['ADMIN', 'ADM0_A3', 'geometry']]
  File "C:\Users\gpont\anaconda3\envs\pythonProject2\lib\site-packages\geopandas\io\file.py", line 96, in _read_file
    with reader(path_or_bytes, **kwargs) as features:
  File "C:\Users\gpont\anaconda3\envs\pythonProject2\lib\site-packages\fiona\env.py", line 398, in wrapper
    return f(*args, **kwargs)
  File "C:\Users\gpont\anaconda3\envs\pythonProject2\lib\site-packages\fiona\__init__.py", line 253, in open
    c = Collection(path, mode, driver=driver, encoding=encoding,
  File "C:\Users\gpont\anaconda3\envs\pythonProject2\lib\site-packages\fiona\collection.py", line 154, in __init__
    self.session.start(self, **kwargs)
  File "fiona/ogrext.pyx", line 484, in fiona.ogrext.Session.start
  File "fiona/_shim.pyx", line 90, in fiona._shim.gdal_open_vector
fiona.errors.DriverError: 'map1 e_110m_admin_0_countries.shp' does not exist in the file system, and is not recognized as a supported dataset name.

Process finished with exit code 1

提前致谢。

乔瓦尼

您的路径中似乎有一个 \n,它被解释为换行符。 (注意 'map1 e_110m_admin_0_countries.shp' does not exist 中缺少的 n

尝试

import os
...
gpd.read_file(os.path.normpath(shapefile))