FeatureCollection
# 简介
要素集(FeatureCollection)是PIE-Engine下的矢量数据集对象,是Feature的一个集合。众多的Feature对象要求具有相同的属性字段,几何图形的坐标系也要相同。PIE-Engine Studio提供构建要素集算子及要素集相关算法,包括但不限于加载要素集,获取要素集属性信息、几何信息,渲染要素集,要素集内Feature循环计算,要素集指定属性进行聚合统计计算。
# FeatureCollection
函数 | 返回值 |
---|---|
FeatureCollection(args,columns) | FeatureCollection |
参数 | 类型 | 说明 |
---|---|---|
args | String|PIEGeometry|PIEFeature|Array | 矢量数据路径或单个PIEGeomery对象或单个PIEFeature对象或PIEFeature对象Array |
columns | String | 未启用 |
var featureCollection = pie.FeatureCollection('NGCC/CHINA_PROVINCE_BOUNDARY');
print(featureCollection);
# aggregate_array
根据属性名获得FeatureCollection中所有Feature对象的属性值信息,输出这些属性值的集合。
函数 | 返回值 |
---|---|
aggregate_array (property) | List |
参数 | 类型 | 说明 |
---|---|---|
property | String | 属性名 |
// 在feature1中添加值为10的“addProp”属性
var feature1=pie.Feature(pie.Geometry.Point([118.5, 31.6] , null)).set("addProp",10);
print("feature1",feature1);
// 在feature2中添加值为2的“addProp”属性
var feature2=pie.Feature(pie.Geometry.Point([118.5, 30.6] , null)).set("addProp",2);
print("feature2",feature2);
// 合并feature1和feature2至要素集
var fCol=pie.FeatureCollection([feature1,feature2]);
// 统计"addProp"属性值的集合
var array=fCol.aggregate_array("addProp");
print("array",array);
# aggregate_count
根据属性名获得FeatureCollection中所有Feature对象的属性值信息,计算其中非空属性值的数目。
函数 | 返回值 |
---|---|
aggregate_count (property) | Number |
参数 | 类型 | 说明 |
---|---|---|
property | String | 属性名 |
// 在feature1中添加值为10的“addProp”属性
var feature1=pie.Feature(pie.Geometry.Point([118.5, 31.6] , null)).set("addProp",10);
print("feature1",feature1);
// 在feature2中添加值为2的“addProp”属性
var feature2=pie.Feature(pie.Geometry.Point([118.5, 30.6] , null)).set("addProp",2);
print("feature2",feature2);
// 合并feature1和feature2至要素集
var fCol=pie.FeatureCollection([feature1,feature2]);
// 统计"addProp"中非空属性值的数目
var count=fCol.aggregate_count("addProp");
print(count);
# aggregate_first
根据属性名获得FeatureCollection中所有Feature对象的属性值信息,计算其中首个对象的属性值。
函数 | 返回值 |
---|---|
aggregate_first (property) | String|Number|Boolean|Object|List |
参数 | 类型 | 说明 |
---|---|---|
property | String | 属性名 |
// 在feature1中添加值为10的“addProp”属性
var feature1=pie.Feature(pie.Geometry.Point([118.5, 31.6] , null)).set("addProp",10);
print("feature1",feature1);
// 在feature2中添加值为2的“addProp”属性
var feature2=pie.Feature(pie.Geometry.Point([118.5, 30.6] , null)).set("addProp",2);
print("feature2",feature2);
// 合并feature1和feature2至要素集
var fCol=pie.FeatureCollection([feature1,feature2]);
print("fCol",fCol);
// 返回"addProp"属性首个对象的属性值
var first=fCol.aggregate_first("addProp");
print(first);
# aggregate_mean
根据属性名获得FeatureCollection中所有Feature对象的属性值信息,输出这些属性值的平均值。
函数 | 返回值 |
---|---|
aggregate_mean (property) | Number |
参数 | 类型 | 说明 |
---|---|---|
property | String | 属性名 |
// 在feature1中添加值为10的“addProp”属性
var feature1=pie.Feature(pie.Geometry.Point([118.5, 31.6] , null)).set("addProp",10);
print("feature1",feature1);
// 在feature2中添加值为2的“addProp”属性
var feature2=pie.Feature(pie.Geometry.Point([118.5, 30.6] , null)).set("addProp",2);
print("feature2",feature2);
// 合并feature1和feature2至要素集
var fCol=pie.FeatureCollection([feature1,feature2]);
print("fCol",fCol);
// 返回"addProp"属性值平均值
var mean=fCol.aggregate_mean("addProp");
print(mean);
# aggregate_max
根据属性名获得FeatureCollection中所有Feature对象的属性值信息,输出这些属性值的最大值。
函数 | 返回值 |
---|---|
aggregate_max (property) | Number |
参数 | 类型 | 说明 |
---|---|---|
property | String | 属性名 |
// 在feature1中添加值为10的“addProp”属性
var feature1=pie.Feature(pie.Geometry.Point([118.5, 31.6] , null)).set("addProp",10);
print("feature1",feature1);
// 在feature2中添加值为2的“addProp”属性
var feature2=pie.Feature(pie.Geometry.Point([118.5, 30.6] , null)).set("addProp",2);
print("feature2",feature2);
// 合并feature1和feature2至要素集
var fCol=pie.FeatureCollection([feature1,feature2]);
print("fCol",fCol);
// 返回"addProp"属性中最大属性值
var max=fCol.aggregate_max("addProp");
print(max);
# aggregate_min
根据属性名获得FeatureCollection中所有Feature对象的属性值信息,输出这些属性值的最小值。
函数 | 返回值 |
---|---|
aggregate_min (property) | Number |
参数 | 类型 | 说明 |
---|---|---|
property | String | 属性名 |
// 在feature1中添加值为10的“addProp”属性
var feature1=pie.Feature(pie.Geometry.Point([118.5, 31.6] , null)).set("addProp",10);
print("feature1",feature1);
// 在feature2中添加值为2的“addProp”属性
var feature2=pie.Feature(pie.Geometry.Point([118.5, 30.6] , null)).set("addProp",2);
print("feature2",feature2);
// 合并feature1和feature2至要素集
var fCol=pie.FeatureCollection([feature1,feature2]);
print("fCol",fCol);
// 返回"addProp"属性值的最小值
var min=fCol.aggregate_min("addProp");
print(min);
# aggregate_sum
根据属性名获得FeatureCollection中所有Feature对象的属性值信息,输出这些属性值的和。
函数 | 返回值 |
---|---|
aggregate_sum (property) | Number |
参数 | 类型 | 说明 |
---|---|---|
property | String | 属性名 |
// 在feature1中添加值为10的“addProp”属性
var feature1=pie.Feature(pie.Geometry.Point([118.5, 31.6] , null)).set("addProp",10);
print("feature1",feature1);
// 在feature2中添加值为2的“addProp”属性
var feature2=pie.Feature(pie.Geometry.Point([118.5, 30.6] , null)).set("addProp",2);
print("feature2",feature2);
// 合并feature1和feature2至要素集
var fCol=pie.FeatureCollection([feature1,feature2]);
print("fCol",fCol);
// 返回"addProp"属性值的和
var sum=fCol.aggregate_sum("addProp");
print(sum);
# classify
对FeatureCollection中的每个要素进行分类。
函数 | 返回值 |
---|---|
classify(classifier, outputName) | FeatureCollection |
参数 | 类型 | 说明 |
---|---|---|
classifier | ClassifierRT/ClassifierSVM/ ClassifierNormalBayes | 分类器 |
outputName | String | 要添加的输出属性的名称,默认为"classification" |
Map.setCenter(0, 0, 0);
// 加载原始矢量和栅格数据
var image = pie.Image('user/17090142114/PGDB001/World84').select(["B1", "B2", "B3"]);
var featureCollection = pie.FeatureCollection('user/17090142114/PGDB001/WorldROI');
// 获得训练样本,并且按照7:3分成训练样本和验证样本
var sampleFeatureCollection = image.sampleRegions(featureCollection, ["type"], 50000);
sampleFeatureCollection = sampleFeatureCollection.randomColumn('random');
var trainingFeatures = sampleFeatureCollection.filter(pie.Filter.lte("random", 0.7));
var testingFeatures = sampleFeatureCollection.filter(pie.Filter.gt("random", 0.7));
// 构建SVM分类器,并训练数据
var classifer = pie.Classifier.svm().train(trainingFeatures, "type", ["B1", "B2", "B3"]);
// 影像分类,并加载显示
var resultImage = image.classify(classifer, "classifyA");
var visParam = {
opacity:1,
uniqueValue:'1,2,3,4',
palette: 'EAF2F5,000032,1F3600,FAFFC8'
};
Map.addLayer(resultImage, visParam,"ClassifyImage");
// 评估训练样本的精度
var checkM = classifer.confusionMatrix();
print(checkM.getInfo());
print(checkM.acc().getInfo());
print(checkM.kappa().getInfo());
// 评估验证样本的精度
var predictResult = testingFeatures.classify(classifer,"classification");
var errorM=predictResult.errorMatrix("type","classification");
print(errorM.getInfo());
print(errorM.acc().getInfo());
print(errorM.kappa().getInfo());
# errorMatrix
通过比较FeatureCollection的两列(一列包含实际值,另一列包含预测值),计算FeatureCollection的二维错误矩阵,其中数值从0开始。矩阵的轴0(行)对应于实际值,轴1(列)对应于预测值。
函数 | 返回值 |
---|---|
errorMatrix(actual, predicted, order) | ConfusionMatrix |
参数 | 类型 | 说明 |
---|---|---|
actual | String | 包含实际值的属性的名称 |
predicted | String | 包含预测值的属性的名称 |
order | List | 未启用 |
Map.setCenter(0, 0, 0);
// 加载原始矢量和栅格数据
var image = pie.Image('user/17090142114/PGDB001/World84').select(["B1", "B2", "B3"]);
var featureCollection = pie.FeatureCollection('user/17090142114/PGDB001/WorldROI');
// 获得训练样本,并且按照7:3分成训练样本和验证样本
var sampleFeatureCollection = image.sampleRegions(featureCollection, ["type"], 50000);
sampleFeatureCollection = sampleFeatureCollection.randomColumn('random');
var trainingFeatures = sampleFeatureCollection.filter(pie.Filter.lte("random", 0.7));
var testingFeatures = sampleFeatureCollection.filter(pie.Filter.gt("random", 0.7));
// 构建SVM分类器,并训练数据
var classifer = pie.Classifier.svm().train(trainingFeatures, "type", ["B1", "B2", "B3"]);
// 影像分类,并加载显示
var resultImage = image.classify(classifer, "classifyA");
var visParam = {
opacity:1,
uniqueValue:'1,2,3,4',
palette: 'EAF2F5,000032,1F3600,FAFFC8'
};
Map.addLayer(resultImage, visParam,"ClassifyImage");
// 评估训练样本的精度
var checkM = classifer.confusionMatrix();
print(checkM.getInfo());
print(checkM.acc().getInfo());
print(checkM.kappa().getInfo());
// 评估验证样本的精度
var predictResult = testingFeatures.classify(classifer,"classification");
var errorM=predictResult.errorMatrix("type","classification");
print(errorM.getInfo());
print(errorM.acc().getInfo());
print(errorM.kappa().getInfo());
# filter
对FeatureCollection对象通过指定过滤器进行过滤,返回过滤后的FeatureCollection对象。
函数 | 返回值 |
---|---|
filter(filter) | FeatureCollection |
参数 | 类型 | 说明 |
---|---|---|
filter | Filter | 过滤器对象 |
var beijing = pie.FeatureCollection('NGCC/CHINA_PROVINCE_BOUNDARY')
.filter(pie.Filter.eq('name', '北京市'))
.first()
.geometry();
Map.centerObject(beijing, 7);
Map.addLayer(beijing, null, "BeiJing");
# filterBounds
对FeatureCollection对象进行空间范围过滤,筛选出在给定空间范围内的feature,返回过滤后的FeatureCollection对象。
函数 | 返回值 |
---|---|
filterBounds(geometry) | FeatureCollection |
参数 | 类型 | 说明 |
---|---|---|
geometry | Geometry | 过滤空间范围 |
var polygon = pie.Geometry.Polygon(
[[[120, 41], [121, 41], [121, 42], [120, 42],[120, 41]]], null);
var neimeng = pie.FeatureCollection('NGCC/CHINA_PROVINCE_BOUNDARY')
.filterBounds(polygon)
.first()
.geometry();
Map.centerObject(neimeng, 3);
Map.addLayer(neimeng, null, 'NeiMeng');
# first
获得第一个Feature对象。
函数 | 返回值 |
---|---|
first() | Feature |
var featureCollection = pie.FeatureCollection().load('NGCC/CHINA_PROVINCE_BOUNDARY');
var feature = featureCollection.first();
# geometries
获得FeatureCollection下几何图形信息,几何信息以Json形式表示。
函数 | 返回值 |
---|---|
geometries(tolerance) | PIEList |
参数 | 类型 | 说明 |
---|---|---|
this | ImageCollection | 要进行计算的影像集合 |
tolerance | Number | 结果简化距离范围,单位米,默认距离为0,表示不简化 |
var featureCollection = pie.FeatureCollection().load('user/101/public/shape/Shi_HeBei');
var geometryList = featureCollection.geometries(2000);
print(geometryList);
# get
根据属性名获得FeatureCollection下的属性值信息。
函数 | 返回值 |
---|---|
get(key) | String|Number|Boolean|Object|List |
参数 | 类型 | 说明 |
---|---|---|
key | String | 属性名 |
var featureCollection = pie.FeatureCollection().load('NGCC/CHINA_PROVINCE_BOUNDARY');
featureCollection = featureCollection.set("key1",{name:123});
var value = featureCollection.get("key1");
print(value);
# getArray
根据属性名获得FeatureCollection下的属性值信息,并且将其转换为数组类型。
函数 | 返回值 |
---|---|
getArray(property) | Array |
参数 | 类型 | 说明 |
---|---|---|
property | String | 属性名 |
var featureCollection = pie.FeatureCollection().load('NGCC/CHINA_PROVINCE_BOUNDARY');
var dict = {key1: [1, 2, 3, 4, 5, 6], key2: 12.34, key3: "12.34"};
featureCollection = featureCollection.setMulti(dict);
var value = featureCollection.getArray("key1");
print(value);
# getAt
获得指定编号的Feature对象。
函数 | 返回值 |
---|---|
getAt(index) | Feature |
参数 | 类型 | 说明 |
---|---|---|
index | Number | Feature的编号 |
var featureCollection = pie.FeatureCollection().load('NGCC/CHINA_PROVINCE_BOUNDARY');
var feature = featureCollection.getAt(0);
# getInfo
快捷计算相关的值。
函数 | 返回值 |
---|---|
getInfo() | Number|Object |
var featureCollection = pie.FeatureCollection().load('NGCC/CHINA_PROVINCE_BOUNDARY');
var count = featureCollection.size().getInfo();
print(count);
# getNumber
根据属性名获得FeatureCollection下的属性值信息,并且将其转换为数值类型。
函数 | 返回值 |
---|---|
getNumber(property) | Number |
参数 | 类型 | 说明 |
---|---|---|
property | String | 属性名 |
var featureCollection = pie.FeatureCollection().load('NGCC/CHINA_PROVINCE_BOUNDARY');
var dict = {key1: [1, 2, 3, 4, 5, 6], key2: 12.34, key3: "12.34"};
featureCollection = featureCollection.setMulti(dict);
var value = featureCollection.getNumber("key3");
print(value);
# getString
根据属性名获得FeatureCollection下的属性值信息,并且将其转换为字符串类型。
函数 | 返回值 |
---|---|
getString(property) | String |
参数 | 类型 | 说明 |
---|---|---|
property | String | 属性名 |
var featureCollection = pie.FeatureCollection().load('NGCC/CHINA_PROVINCE_BOUNDARY');
var dict = {key1: [1, 2, 3, 4, 5, 6], key2: 12.34, key3: "12.34"};
featureCollection = featureCollection.setMulti(dict);
var value = featureCollection.getString("key2").getInfo();
print(value);
# id
返回FeatureCollection对象的id。
函数 | 返回值 |
---|---|
id() | String |
var featureCollection = pie.FeatureCollection().load('NGCC/CHINA_PROVINCE_BOUNDARY');
var id = featureCollection.id().getInfo();
print(id);
# limit
返回集合中前几个要素的集合。
函数 | 返回值 |
---|---|
limit(count) | FeatureCollection |
参数 | 类型 | 说明 |
---|---|---|
this | FeatureCollection | 要进行合并的要素集合 |
count | Number | 数值,要返回的要素的个数 |
// 定义要素数据集1
var geometry1 = pie.Geometry.Polygon([[[117, 31], [118, 31], [118, 32], [117, 32], [117, 31]]]);
var properties1 = {name: "Test1", age: 12};
var feature1 = pie.Feature(geometry1, properties1).set("id", "1111");
var collection1 = pie.FeatureCollection(feature1);
// 定义要素数据集2
var geometry2 = pie.Geometry.Polygon([[[117, 32.1], [118, 32.1], [118, 33.1], [117, 33.1], [117, 32.1]]]);
var properties2 = {name: "Test2", age: 18};
var feature2 = pie.Feature(geometry2, properties2).set("id", "2222");
var collection2 = pie.FeatureCollection(feature2);
// 要素数据集合并,并添加到地图
var fCollection = collection1.merge(collection2).limit(1);
print(fCollection);
fCollection = fCollection.limit(1);
print(fCollection);
# load
FeatureCollection对象加载数据,一般在FeatureCollection初始化的时候会默认调用该方法。
函数 | 返回值 |
---|---|
load(id) | FeatureCollection |
参数 | 类型 | 说明 |
---|---|---|
id | String | 数据路径 |
var featureCollection = pie.FeatureCollection().load('NGCC/CHINA_PROVINCE_BOUNDARY');
print(featureCollection);
# map
针对FeatureCollection中的每个Feature进行循环计算。
函数 | 返回值 |
---|---|
map(algorithm,dropNulls) | FeatureCollection |
参数 | 类型 | 说明 |
---|---|---|
algorithm | Function | 参数是Feature的方法,返回Feature |
dropNulls | Boolean | 是否允许返回空值 |
var featureCollection = pie.FeatureCollection().load('NGCC/CHINA_PROVINCE_BOUNDARY');
var featureCollectionNew = featureCollection.map(function (feature) {
var geometry = feature.geometry();
var featureNew = pie.Feature(geometry.centroid());
return featureNew;
})
Map.addLayer(featureCollectionNew,{ color:'00FF00FF',width:1,fillColor:'00FF0000',pointSize:20});
Map.setCenter(118,39.7,3);
# merge
实现两个要素集合(FeatureCollection)的合并,要求要合并的两个FeatureCollection几何图形类型一致。
函数 | 返回值 |
---|---|
merge(fCollection) | FeatureCollection |
参数 | 类型 | 说明 |
---|---|---|
fCollection | FeaturerCollection | 进行合并的要素集合 |
// 定义要素数据集1
var geometry1 = pie.Geometry.Polygon([[[117, 31], [118, 31], [118, 32], [117, 32], [117, 31]]]);
var properties1 = {name: "Test1", age: 12};
var feature1 = pie.Feature(geometry1, properties1).set("id", "1111");
var collection1 = pie.FeatureCollection(feature1);
// 定义要素数据集2
var geometry2 = pie.Geometry.Polygon([[[117, 32.1], [118, 32.1], [118, 33.1], [117, 33.1], [117, 32.1]]]);
var properties2 = {name: "Test2", age: 18};
var feature2 = pie.Feature(geometry2, properties2).set("id", "2222");
var collection2 = pie.FeatureCollection(feature2);
// 要素数据集合并,并添加到地图
var fCollection = collection1.merge(collection2);
Map.addLayer(fCollection);
Map.setCenter(117.5,32,6);
# propertyNames
返回FeatureCollection对象的属性名列表。
函数 | 返回值 |
---|---|
propertyNames() | List |
var featureCollection = pie.FeatureCollection().load('NGCC/CHINA_PROVINCE_BOUNDARY');
featureCollection = featureCollection.set("key1", {name:123});
var propertyNames = featureCollection.propertyNames();
print(propertyNames);
# randomColumn
向FeatureCollection中添加一列确定性伪随机数。输出为双精度浮点数。设置“uniform”分布(默认值)时,输出为[0,1]的范围内的均匀分布随机数。设置“normal”分布时,输出为μ=0,𝛔=1的正态分布随机数,此时数值范围没有明确的限制。
函数 | 返回值 |
---|---|
randomColumn (columnName="random", seed=0, distribution= "uniform") | FeatureCollection |
参数 | 类型 | 说明 |
---|---|---|
columnName | String | 新增列的名称,默认为 "random" |
seed | Long | 随机种子,默认为0 |
distribution | String | 生成随机数的分布类型。赋值为'uniform' 、'normal'之一 |
var featureCollection = pie.FeatureCollection().load('NGCC/CHINA_PROVINCE_BOUNDARY');
featureCollection = featureCollection.randomColumn('random');
Map.addLayer(featureCollection,{ color:'FF0000FF',width:1,fillColor:'00FF0000',pointSize:10});
Map.setCenter(118,39.7,3);
# reduceColumns
将指定的selector中的属性值根据reducer计算,返回一个字典对象。
函数 | 返回值 |
---|---|
reduceColumns(reducer,selectors,weightSelector) | Object |
参数 | 类型 | 说明 |
---|---|---|
reducer | Reducer | 统计器,目前仅支持toList统计器 |
selectors | Array | 要统计的属性列表 |
weightSelector | Array | 各个属性的权重,目前未启用 |
var featureCollection = pie.FeatureCollection().load('NGCC/CHINA_PROVINCE_BOUNDARY');
var value = featureCollection.reduceColumns(pie.Reducer.toList(),["name"]);
print(value);
# set
设置FeatureCollection下的属性信息,如果存在就覆盖更新,如果不存在则直接创建。
函数 | 返回值 |
---|---|
set(key,value) | Void |
参数 | 类型 | 说明 |
---|---|---|
key | String | 属性名 |
value | String|Number|Boolean|Object|List | 属性值 |
var featureCollection = pie.FeatureCollection().load('NGCC/CHINA_PROVINCE_BOUNDARY');
featureCollection = featureCollection.set("key1",{name:123});
var value = featureCollection.get("key1");
print(value);
//有两种方法均可获取name的值123
//推荐使用如下方法:
var v = pie.Dictionary(featureCollection.get("key1")).get("name");
print(v);
//不推荐使用getInfo,会使页面卡住
var vv = featureCollection.get("key1").getInfo()["name"];
print(vv)
# setMulti
批量设置FeatureCollection下的属性信息,如果存在就覆盖更新,如果不存在则直接创建。
函数 | 返回值 |
---|---|
setMulti(properties) | Void |
参数 | 类型 | 说明 |
---|---|---|
properties | Dictionary | 属性键值对,以字典形式传入 |
var featureCollection = pie.FeatureCollection().load('NGCC/CHINA_PROVINCE_BOUNDARY');
var dict = {key1: [1, 2, 3, 4, 5, 6], key2: 12.34, key3: "12.34"};
featureCollection = featureCollection.setMulti(dict);
print(featureCollection);
# size
获得矢量数据中PIEFeature的个数。
函数 | 返回值 |
---|---|
size() | Number |
var featureCollection = pie.FeatureCollection().load('NGCC/CHINA_PROVINCE_BOUNDARY');
var count = featureCollection.size();
print(count);
# style
FeatureCollection数据渲染显示,返回渲染绘制后的Image对象。
函数 | 返回值 |
---|---|
style(style) | Image |
参数 | 类型 | 说明 |
---|---|---|
style | Json对象 | 数据可视化样式,参考示例 |
var featureCollection = pie.FeatureCollection().load('NGCC/CHINA_PROVINCE_BOUNDARY');
var image = featureCollection.style({color:'FFFF00FF',width:1,fillColor:'00FFFFFF'});
Map.addLayer(image);
Map.setCenter(118,39.7,2);