開始使用免費開始

從光柵圖層擷取資訊

現在,讓我們根據一個向量檔案,從光柵圖層擷取資訊。這個功能由 rasterstats 套件提供。特別是在本練習中,我們要藉由取得每個採礦點附近最近的光柵像素值,來判定所有採礦點的植被類型。

一個採礦點資料集(mining_sites)的子集已載入,且已匯入 GeoPandas 與 matplotlib。

本練習屬於課程

在 Python 中處理地理空間資料

檢視課程

練習說明

  • 匯入 rasterstats 套件。
  • 使用 rasterstats.point_query() 為採礦點位置(mining_sites)擷取最近的光柵值,並指派到新欄位 'vegetation'
  • 繪製採礦點資料,使用 'vegetation' 欄位為點著色,並加入圖例。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Import the rasterstats package
____

# Extract the nearest value in the raster for all mining sites
vegetation_raster = "central_africa_vegetation_map_foraf.tif"
mining_sites['vegetation'] = ____(____, ____, interpolate='nearest')
print(mining_sites.head())

# Replace numeric vegation types codes with description
mining_sites['vegetation'] = mining_sites['vegetation'].replace(vegetation_types)

# Make a plot indicating the vegetation type
____
plt.show()
編輯並執行程式碼