目录
- 1. Raster to Vector Conversion:image.reduceToVectors()
- 2. Vector to Raster Conversion:featureCollection.reduceToImage()
- 3. Grouped reductions
- 3.1 Grouped reduceRegions (aka Zonal Statistics)
- 4. Weighted Reductions
1. Raster to Vector Conversion:image.reduceToVectors()
将Image的栅格格式转换为FeatureCollection的矢量格式
// Reducers: Image.reduceToVectors()
// China boundary.
var china = countries.filter(ee.Filter.eq('ADM0_NAME', 'China'))
// Input 2012 nightlights image, clipped to China border
var nl2012 = ee.Image('NOAA/DMSP-OLS/NIGHTTIME_LIGHTS/F182012')
.select('stable_lights')
.clip(china);
// Arbitrary thresholds on the 6-bit nightlights image
var zones = nl2012.gt(30).add(nl2012.gt(55)).add(nl2012.gt(62));
zones = zones.mask(zones.neq(0));
// Convert zones of the thresholded nightlights to vectors
var vectors = zones.addBands(nl2012).reduceToVectors({
geometry: china,
crs: nl2012.projection(),
scale: nl2012.projection().nominalScale(),
geometryType: 'polygon',
eightConnected: false,
labelProperty: 'zone',
reducer: ee.Reducer.mean(),
// timeout done
maxPixels: 1e10,
tileScale: 2
});
// Display the result
Map.setCenter(117.6225, 39.712, 9);
Map.addLayer(zones, {min: 1, max: 3, palette: ['0000FF', '00FF00', 'FF0000']}, 'raster');
var display = ee.Image(0).mask(ee.Image(0)).paint(vectors, '000000', 3);
Map.addLayer(display, {palette: '000000'}, 'vectors');
2. Vector to Raster Conversion:featureCollection.reduceToImage()
// Reducers: FeatureCollection.reduceToImage()
var tiger = ee.FeatureCollection("TIGER/2010/Blocks");
Map.addLayer(tiger, {}, 'tiger')
var popImage = tiger.reduceToImage({
properties: ['pop10'],
reducer: ee.Reducer.first()
});
Map.addLayer(popImage, {min: 0, max: 1000, palette: ['blue', 'green', 'red']}, 'popImage');
3. Grouped reductions
对图像的属性归为一个组,以组的形式进行显示,并且将属性按照州编码就行归类,显示每个州的总属性。
// To compute the total population and number of housing units in each state,
// this example groups the output of a reduction of a census block FeatureCollection.
// Load a collection of US census blocks.
var blocks = ee.FeatureCollection('TIGER/2010/Blocks');
// Compute sums of the specified properties, grouped by state code.
var sums = blocks
.filter(ee.Filter.and(
ee.Filter.neq('pop10', null),
ee.Filter.neq('housing10', null)))
.reduceColumns({
selectors: ['pop10', 'housing10', 'statefp10'],
reducer: ee.Reducer.sum().repeat(2).group({
groupField: 2,
groupName: 'state-code',
})
});
// Print the resultant Dictionary.
print(sums);
结果展示:
3.1 Grouped reduceRegions (aka Zonal Statistics)
通过使用 reducer.group() 按指定输入的值对 reducer 的输出进行分组,可以获得 Image 或 FeatureCollection 的每个区域中的统计信息。
要对 image.reduceRegions() 的输出进行分组,可以指定一个按整数像素值定义分组的分组带。
这种类型的计算有时称为 “区域统计” ,其中区域被指定为分组带,统计信息由Reducer确定。
// Reducers: Grouping, land cover
var mcd12q1 = ee.ImageCollection("MODIS/006/MCD12Q1"),
dmspols = ee.ImageCollection("NOAA/DMSP-OLS/NIGHTTIME_LIGHTS"),
geometry =
/* color: #d63000 */
/* displayProperties: [
{
"type": "rectangle"
}
] */
ee.Geometry.Polygon(
[[[-125.08281249999999, 48.998022477518326],
[-125.08281249999999, 24.871185409749824],
[-61.71367187499999, 24.871185409749824],
[-61.71367187499999, 48.998022477518326]]], null, false);
// MODIS land cover categories in 2020.
var landcover = mcd12q1.filterDate('2020').first()
.select('LC_Type1'); // IGBP
// Nightlights imagery inputs
var nl2001 = dmspols.filterDate('2001').first().select('stable_lights')
var nl2012 = dmspols.filterDate('2012').first().select('stable_lights')
// Compute the nightlights decadal difference, add land cover codes.
var nlDiff = nl2012.subtract(nl2001).addBands(landcover);
// Grouped mean: change of nightlights by land cover category.
print(nlDiff.reduceRegion({
reducer: ee.Reducer.mean().group({
groupField: 1,
groupName: 'code',
}),
geometry: geometry,
scale: 1000,
maxPixels: 1e8
}));
结果展示:
4. Weighted Reductions
默认情况下,应用于图像的Reducer根据掩码值对输入进行加权。这与通过clip()等操作创建的分数像素有关。通过在Reducer上调用 unweighted() 来调整这种行为。使用未加权的Reducer将强制该区域中的所有像素具有相同的权重。
var weighted = image
.reduceRegion({
reducer: ee.Reducer.mean(),
geometry: geometry,
scale: 30
}).get('nd');
var unweighted = image
.reduceRegion({
reducer: ee.Reducer.mean().unweighted(),
geometry: geometry,
scale: 30
}).get('nd');
例子:
var l8sr = ee.ImageCollection("LANDSAT/LC08/C02/T1_L2");
// Create an arbitrary region.
var geometry = ee.Geometry.Rectangle(-122.496, 37.532, -121.554, 37.538);
Map.centerObject(geometry)
Map.addLayer(geometry)
// Load a Landsat 8 input image.
var image = l8sr.filterBounds(geometry).first()
// Make an NDWI image. It will have one band named 'nd'.
var ndwi = image.normalizedDifference(['SR_B3', 'SR_B5']);
// Compute the weighted mean of the NDWI image.
var weighted = ndwi
.reduceRegion({
reducer: ee.Reducer.mean(),
geometry: geometry,
scale: 30})
.get('nd');
// Compute the UN-weighted mean of the NDWI image.
var unweighted = ndwi
.reduceRegion({
reducer: ee.Reducer.mean().unweighted(),
geometry: geometry,
scale: 30})
.get('nd');
// Observe the difference between weighted and unweighted reductions.
print('weighted:', weighted);
print('unweighted:', unweighted);
weighted:
-0.21169499738972458
unweighted:
-0.21157474937536905
IDW:
// Mock up a feature collection.
var fc = ee.FeatureCollection([
ee.Feature(ee.Geometry.Point([110.14444050980546, 28.774132610921978])),
ee.Feature(ee.Geometry.Point([112.93496785355546, 28.23347679807143])),
ee.Feature(ee.Geometry.Point([111.26504597855546, 29.235334712701505])),
ee.Feature(ee.Geometry.Point([111.44082722855546, 27.378325472980837])),
ee.Feature(ee.Geometry.Point([113.19863972855546, 27.884456252339465])),
ee.Feature(ee.Geometry.Point( [113.72598347855546, 28.484840062759634]))
]);
// Set maximum distance to consider.
var maxDist = 1e4; // Meters
// Calculate pixel distance to nearest point.
var distance = fc.distance({searchRadius: maxDist, maxError: 10});
// Convert distance to linear weight between 0 and 1.
var weight = distance.subtract(maxDist).abs().divide(maxDist);
// Display the distance and weight images.
Map.centerObject(fc, 9);
Map.addLayer(distance, {min:0, max: maxDist}, 'Meters');
Map.addLayer(fc, {min:0, max: 1}, 'Weight');
Map.addLayer(fc, {color: 'red'}, 'Points');
结果展示: