Python处理矢量数据(例shp文件)

  1. ogr:GDAL项目中用于处理矢量数据的模块
  2. fiona:基于ogr的封装,提供了更简洁了API
  3. geopandas:基于fiona进行封装,并在pandas的基础上添加了对空间数据的支持,实现了读取空间数据以及对空间数据进行处理

【相关链接】
1.GeoPandas官方文档

【学习链接】
1.使用Python实现子区域数据分类统计
2.GeoPandas官方中文文档—译著
3.GeoPandas随笔

安装

Python安装

先安装 GDAL,Fiona, pyproj , Shapely等依赖包,再利用pip install geopandas安装

geopandas依赖库很多,比较不好安装,不建议云安装 建议使用whl包,本地安装

查看Python版本,记得之后下载的安装包都要下载38版本的
image.png

以下安装包都在这个网站上下载即可:网址

  1. 下载geopandas的安装包(geopandas‑0.10.2‑py2.py3‑none‑any.whl)
  2. 安装:pip install geopandas‑0.10.2‑py2.py3‑none‑any.whl
    1. 发现geopandas的依赖包fiona安装失败

image.png

  1. 下载fiona>=1.8(Fiona‑1.8.20‑cp38‑cp38‑win_amd64.whl)
    1. 安装失败,查看报错信息,发现GDAL安装失败,继续找GDAL的安装包
  2. gdal~=3.3.0(GDAL-3.3.3-cp38-cp38-win_amd64.whl)

image.png

  1. 再回过头来安装fiona>=1.8

image.png

  1. 再回过头来安装geopandas

image.png

引用包:import geopandas查看是否安装成功

问题一:Unable to import required dependencies:numpy

image.png
引用包时,可能报这个错误

  1. ImportError: Unable to import required dependencies:
  2. numpy:
  3. IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!
  4. Importing the numpy C-extensions failed. This error can happen for
  5. many reasons, often due to issues with your setup or how NumPy was
  6. installed.
  7. We have compiled some common reasons and troubleshooting tips at:
  8. https://numpy.org/devdocs/user/troubleshooting-importerror.html
  9. Please note and check the following:
  10. * The Python version is: Python3.8 from "C:\Users\2107\anaconda3\python.exe"
  11. * The NumPy version is: "1.20.1"
  12. and make sure that they are the versions you expect.
  13. Please carefully study the documentation linked above for further help.

只需要把numpy卸载,重装即可

  1. pip uninstall numpy
  2. pip install numpy

Anaconda安装

打开Anaconda Prompt

  1. # 切换清华镜像源
  2. conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
  3. conda config --set show_channel_urls yes
  4. # 安装
  5. conda install geopandas
  6. #或者
  7. conda install --channel conda-forge geopandas

相关设置

编码问题

保存shp文件,字段成乱码
GeoDataFrame.to_file('D:\tmp.shp', encoding="utf-8")