GeoPandas 简介

在 Python 中处理地理空间数据

Joris Van den Bossche

Open source software developer and teacher, GeoPandas maintainer

空间专用数据格式

restaurants = pd.read_csv("datasets/paris_restaurants.csv")
restaurants.head()
                                             type         x          y
0                             Restaurant européen  259641.6  6251867.4
1                Restaurant traditionnel français  259572.3  6252030.2
2                Restaurant traditionnel français  259657.2  6252143.8
3  Restaurant indien, pakistanais et Moyen Orient  259684.4  6252203.6
4                Restaurant traditionnel français  259597.9  6252230.0

在本课程余下部分将学习:

  • 空间文件格式(Shapefile、GeoJSON、GeoPackage 等)
  • GeoPandas:支持空间数据的 pandas 数据框
在 Python 中处理地理空间数据

用 GeoPandas 导入空间数据

import geopandas
countries = geopandas.read_file("countries.geojson")
countries.head()
          name      continent       gdp                            geometry
0  Afghanistan           Asia   64080.0  POLYGON ((61.21 35.65, 62.23 35...
1       Angola         Africa  189000.0  MULTIPOLYGON (((23.90 -11.72, 2...
2      Albania         Europe   33900.0  POLYGON ((21.02 40.84, 21.00 40...
4    Argentina  South America  879400.0  MULTIPOLYGON (((-66.96 -54.90, ...
5      Armenia           Asia   26300.0  POLYGON ((43.58 41.09, 44.97 41...
在 Python 中处理地理空间数据

用 GeoPandas 快速可视化空间数据

countries.plot()

在 Python 中处理地理空间数据

GeoDataFrame

countries.head()
          name      continent       gdp                            geometry
0  Afghanistan           Asia   64080.0  POLYGON ((61.21 35.65, 62.23 35...
1       Angola         Africa  189000.0  MULTIPOLYGON (((23.90 -11.72, 2...
2      Albania         Europe   33900.0  POLYGON ((21.02 40.84, 21.00 40...
...
type(countries)
geopandas.geodataframe.GeoDataFrame
在 Python 中处理地理空间数据

GeoDataFrame

countries.head()
          name      continent       gdp                            geometry
0  Afghanistan           Asia   64080.0  POLYGON ((61.21 35.65, 62.23 35...
1       Angola         Africa  189000.0  MULTIPOLYGON (((23.90 -11.72, 2...
2      Albania         Europe   33900.0  POLYGON ((21.02 40.84, 21.00 40...
...

GeoDataFrame 表示一个表格化的地理矢量数据集:

  • 一个"geometry"列:保存几何信息
  • 其他列:作为属性,描述各几何体
在 Python 中处理地理空间数据

"geometry" 属性

countries.geometry
0      POLYGON ((61.21 35.65, 62.23 35...
1      MULTIPOLYGON (((23.90 -11.72, 2...
                      ...                
175    POLYGON ((23.22 -17.52, 22.56 -...
176    POLYGON ((29.43 -22.09, 28.79 -...
Name: geometry, Length: 176, dtype: object
type(countries.geometry)
geopandas.geoseries.GeoSeries
在 Python 中处理地理空间数据

具备空间感知的数据框

countries.geometry.area
0       63.593500
1      103.599439
2        3.185163
          ...    
174    112.718524
175     62.789498
176     32.280371
Length: 177, dtype: float64
在 Python 中处理地理空间数据

小结

GeoDataFrame 类似 pandas 的 DataFrame:

  • 普通 pandas DataFrame 的功能皆可用

并加入空间功能:

  • plot() 方法
  • geometry 属性(GeoSeries)
  • 空间专用的属性与方法(如 area
在 Python 中处理地理空间数据

Passons à la pratique !

在 Python 中处理地理空间数据

Preparing Video For Download...