向量轉光柵

Earth Engine 中的向量轉光柵轉換作業由 featureCollection.reduceToImage() 方法處理。這個方法會將指定屬性的值指派給每個地圖項目下的像素。本例使用縣市資料,建立代表各縣市陸地面積的圖片:

程式碼編輯器 (JavaScript)

// Load a collection of US counties.
var counties = ee.FeatureCollection('TIGER/2018/Counties');

// Make an image out of the land area attribute.
var landAreaImg = counties
  .filter(ee.Filter.notNull(['ALAND']))
  .reduceToImage({
    properties: ['ALAND'],
    reducer: ee.Reducer.first()
});

// Display the county land area image.
Map.setCenter(-99.976, 40.38, 5);
Map.addLayer(landAreaImg, {
  min: 3e8,
  max: 1.5e10,
  palette: ['FCFDBF', 'FDAE78', 'EE605E', 'B63679', '711F81', '2C105C']
});

Python 設定

請參閱「 Python 環境」頁面,瞭解 Python API 和如何使用 geemap 進行互動式開發。

import ee
import geemap.core as geemap

Colab (Python)

# Load a collection of US counties.
counties = ee.FeatureCollection('TIGER/2018/Counties')

# Make an image out of the land area attribute.
land_area_img = counties.filter(ee.Filter.notNull(['ALAND'])).reduceToImage(
    properties=['ALAND'], reducer=ee.Reducer.first()
)

# Display the county land area image.
m = geemap.Map()
m.set_center(-99.976, 40.38, 5)
m.add_layer(
    land_area_img,
    {
        'min': 3e8,
        'max': 1.5e10,
        'palette': ['FCFDBF', 'FDAE78', 'EE605E', 'B63679', '711F81', '2C105C'],
    },
)
m

指定縮減函式,以指出如何匯總重疊特徵的屬性。在前述範例中,由於沒有重疊,因此只需 ee.Reducer.first() 即可。如同這個範例所示,請預先篩選資料,以便移除無法轉換為圖片的空值。輸出內容應類似圖 1,將顏色漸層對應至縣大小。與 Earth Engine 中的所有圖片輸出縮圖處理器一樣,縮放比例會由輸出內容動態設定。在本例中,比例會對應至 Code Editor 中的縮放等級。

reduceToImage 輸出
圖 1. reduceToImage() 使用「TIGER/2018/Counties」FeatureCollection的「ALAND」(土地面積) 屬性所得的結果。