在 GeoPandas 中处理坐标系

在 Python 中处理地理空间数据

Joris Van den Bossche

Open source software developer and teacher, GeoPandas maintainer

GeoPandas 中的 CRS 信息

GeoDataFrame/GeoSeries 的 .crs 属性:

import geopandas
gdf = geopandas.read_file("countries.shp")
print(gdf.crs)
{'init': 'epsg:4326'}
在 Python 中处理地理空间数据

手动设置 CRS

gdf_noCRS = geopandas.read_file("countries_noCRS.shp")
print(gdf_noCRS.crs)
{}

crs 添加 CRS 信息:

# 方式 1
gdf.crs = {'init': 'epsg:4326'}

# 方式 2
gdf.crs = {'proj': 'longlat', 'datum': 'WGS84', 'no_defs': True}
在 Python 中处理地理空间数据

转换到另一种 CRS

import geopandas
gdf = geopandas.read_file("countries_web_mercator.shp")
print(gdf.crs)
{'init': 'epsg:3857', 'no_defs': True}

to_crs() 方法:

# 方式 1
gdf2 = gdf.to_crs({'proj': 'longlat', 'datum': 'WGS84', 'no_defs': True})

# 方式 2 gdf2 = gdf.to_crs(epsg=4326)
在 Python 中处理地理空间数据

为何要转换 CRS?

1) 数据源的 CRS 不同

df1 = geopandas.read_file(...)
df2 = geopandas.read_file(...)

df2 = df2.to_crs(df1.crs)
在 Python 中处理地理空间数据

为何要转换 CRS?

1) 数据源的 CRS 不同

2) 制图(形状与距离失真)

在 Python 中处理地理空间数据

为何要转换 CRS?

1) 数据源的 CRS 不同

2) 制图(形状与距离失真)

3) 基于距离/面积的计算

在 Python 中处理地理空间数据

如何选择使用哪种 CRS?

提示:

  • 使用与数据区域匹配的投影
  • 多数国家有标准 CRS

实用网站:

在 Python 中处理地理空间数据

小结

  • 转换到其他 CRS:使用 to_crs() 方法
  • 确保不同数据集的 CRS 一致
  • 计算距离、面积等时使用投影坐标系

实用网站:

在 Python 中处理地理空间数据

Passons à la pratique !

在 Python 中处理地理空间数据

Preparing Video For Download...