矢量转换为光栅

Earth Engine 中的矢量到栅格转换由 featureCollection.reduceToImage() 方法处理。此方法会为每个地图项下的像素分配指定属性的值。以下示例使用县级数据创建了一张图片,表示每个县的土地面积:

// 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 API 以及如何使用 geemap 进行交互式开发,请参阅 Python 环境页面。

import ee
import geemap.core as geemap
# 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

指定一个 reducer 来指明如何汇总重叠地图项的属性。在前面的示例中,由于没有重叠,因此 ee.Reducer.first() 就足够了。如此示例所示,预先过滤数据以消除无法转换为图片的 null。 输出应如下图 1 所示,其中将颜色渐变映射到县大小。与 Earth Engine 中的所有输出图片的 reducer 一样,缩放比例由输出动态设置。在这种情况下,比例对应于 Code Editor 中的缩放级别。

reduceToImage 输出
图 1. 使用“TIGER/2018/Counties”FeatureCollection 的“ALAND”(土地面积)属性进行 reduceToImage() 的结果。