你可以在一个每个区域获得的统计数据Image
或者 FeatureCollection
通过使用reducer.group()
到组reduce的输出由指定的输入值。例如,为了计算每个州的总人口和住房单元数量,本示例将人口普查块的缩减输出分组FeatureCollection
如下:
数据还是原来讲的一个案例的同样数据,这里不做过多介绍,看函数:
repeat(count)
通过组合给定reducer的指定副本数来创建reducer。输出名称与给定的减速器相同,但每个都是来自每个减速器的相应输出的列表。也就是重复赋值的一个过程
Creates a Reducer by combining the specified number of copies of the given reducer. Output names are the same as the given reducer, but each is a list of the corresponding output from each of the reducers.
Arguments:
this:reducer (Reducer)
count (Integer)
Returns: Reducer
group(groupField, groupName)这里的组的数必须得有2个数以上才可以
根据给定输入的值对 reducer 记录进行分组,并使用给定的 reducer reduce每个组。
否则会报错
Groups reducer records by the value of a given input, and reduces each group with the given reducer.
Arguments:
this:reducer (Reducer):
应用于每个组的reducer,没有组字段。这个参数应该再xxx.group输入
The reducer to apply to each group, without the group field.
groupField (Integer, default: 0):这个也就是你要有几个字段作为你显示的波段信息
包含记录组的字段。
The field that contains record groups.
groupName (String, default: "group"):
包含组的字典键。默认为“组”。字典的键名称
The dictionary key that contains the group. Defaults to 'group'.
Returns: Reducer
代码:
// 加载美国人口数据
var blocks = ee.FeatureCollection('TIGER/2010/Blocks');
// 计算指定属性的总和,按状态代码分组。
//这里的数据筛选主要是通过不等于非空的数据进行筛选,括号中主要是筛选的名称,后面为非空参数
//选择器就是选择这三个波段的数据,然后用reducer进行统计总数然后相当于进行上面lisT中进行前两个的赋值,最后输出结果中只有前两个,如果3个或者多个可以自己进行统计分析。
//注意这个中只能统计是数据类型的所以第三个参数是一个字符串型的所以不能进行分析
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(sums);
输出的结果:
注意:groupField
参数是包含通过该分组,所述代码选择器阵列中的输入的索引groupName
参数指定为存储分组变量的值的属性的名称。由于减速器不会为每个输入自动重复,因此repeat(2)
需要调用。