Earth Engine 中的矢量到栅格转换由 featureCollection.reduceToImage()
方法处理。此方法会为每个地图项下的像素分配指定属性的值。以下示例使用县级数据创建了一张图片,表示每个县的土地面积:
Code Editor (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'] });
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
指定一个 reducer 来指明如何汇总重叠地图项的属性。在前面的示例中,由于没有重叠,因此 ee.Reducer.first()
就足够了。如此示例所示,预先过滤数据以消除无法转换为图片的 null。
输出应如下图 1 所示,其中将颜色渐变映射到县大小。与 Earth Engine 中的所有输出图片的 reducer 一样,缩放比例由输出动态设置。在这种情况下,比例对应于 Code Editor 中的缩放级别。

FeatureCollection
的“ALAND”(土地面积)属性进行 reduceToImage()
的结果。