在 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

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

# Option 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() 方法:

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

# Option 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
  • 計算距離、面積等時,使用「投影 CRS」

實用網站:

在 Python 中處理地理空間資料

一起來練習吧!

在 Python 中處理地理空間資料

Preparing Video For Download...