【Google Earth Engine编程语言学习笔记】Geometry、Feature、FeatureCollection
一、【Geometry】形状
(1)创建
1.创建点ee.Geometry.Point() 创建多点ee.Geometry.MultiPoint()
-
var ct=ee.Geometry.Point(116.3968,39.9186)
Map.centerObject(ct)
print(ct)
Map.addLayer(ct)
2.创建线ee.Geometry.LineString([[x1,y1],[x2,y2],[x3,y3]])
创建多线ee.Geometry.MultiLineString([[[x1,y1],[x2,y2],[x3,y3]],[[x4,y4],[x5,y5]],])
3.创建闭合线ee.Geometry.LinearRing([[x1,y1],[x2,y2],[x3,y3],[x1,y1]])
创建多闭合线ee.Geometry.MultiLineString()
4.创建面ee.Geometry.Polygon([[x1,y1],[x2,y2],[x3,y3],[x4,y4]])
ee.Geometry.Polygon([[[x1,y1],[x2,y2],[x3,y3],[x4,y4]],[[x5,y5],[x6,y6],[x7,y7],[x8,y8]]])后一个在前一个里边,变成中空的多边形
-
var ct=ee.Geometry.Polygon([
[[116.38307485597535,39.915666765209366],
[116.39178667085572,39.915666765209366],
[116.39178667085572,39.92142675789876],
[116.38307485597535,39.92142675789876]],
[ [116.38595018403932,39.91744418606569],
[116.38946924226686,39.91744418606569],
[116.38946924226686,39.919517785427374],
[116.38595018403932,39.919517785427374]]
])
Map.centerObject(ct)
print(ct)
Map.addLayer(ct)
ee.Geometry.MultiPolygon(定义每个多边形的顶点)
ee.Geometry.Rectangle(两个对顶点的坐标)
(2)几何
1.Geometry.transform()
这一部分转换我理解的也不太明白,主要就是投影转换
-
var China_Geo = ee.Geometry.Rectangle(65.9, 19.8,134.5, 50.9);
var China_Planr = ee.Geometry(China_Geo, null, false);
var China_Plnar_2 = China_Geo.transform('EPSG:4326', ee.ErrorMargin(100))
Map.addLayer(China_Geo, {color: 'FF0000'}, 'geodesic polygon')//投影坐标
Map.addLayer(China_Planr,{color: '000000'}, 'planar polygon')//平面直角坐标
Map.addLayer(China_Plnar_2,{color: '0000CD'}, 'planar polygon')//转换为投影坐标
具体的查看https://epsg.io
2.Geometry.centroid()求几何形状的中心点
3.Geometry.simplify()对某个引入的图形进行简化
-
var m=ee.FeatureCollection("路径").geometry();
var m_sim=m.simplify(简化的尺度)尺度越大越不简单
4.Geometry.bounds()
Geometry.convexHull()
5.Geometry.buffer()缓冲区
6.Geometry.union()联合 Geometry.intersection()相交 difference求反
(3)查询
1.Geometry.geometries()将多面分解为单面
2.Geometry.coordinate()求坐标
3.Geometry.area求面积
4.Geometry.perimeter求周长
5.Geometry.diatance求两个多边形最近的距离
(4)转换
Geometry.toGeoJSON()将图形的格式转换,转换为GeoJSON格式在转化为shp格式就不会乱码
二、【feature】
feature和geometry在空间上表述是一样的,但是feature比geometry包含更多的信息量,如geometry代表地图中某个省份的行政边界,feature不仅包含行政边界,还包括他的属性信息,如人口,GDP等
(1)创建
ee.Feature(空间信息,属性信息(dictionary类型))创建一个要素
-
var ct=ee.Geometry.Polygon([
[116.38307485597535,39.915666765209366],
[116.39178667085572,39.915666765209366],
[116.39178667085572,39.92142675789876],
[116.38307485597535,39.92142675789876]
])
var ct_fea=ee.Feature(ct,{name:'ww',location:'bj'})
Map.centerObject(ct)
print(ct_fea)
Map.addLayer(ct_fea)
但是最简单绘制feature的方法为
将会在代码区自动生成代码
修改属性
(2)编辑
1.select(选择属性, newProperties, retainGeometry)
如创建一个feature,当执行select命令时,只保留选择的属性字段,类似于复制
-
var ct=ee.Geometry.Polygon([
[116.38307485597535,39.915666765209366],
[116.39178667085572,39.915666765209366],
[116.39178667085572,39.92142675789876],
[116.38307485597535,39.92142675789876]
])
var ct_fea=ee.Feature(ct,{name:'ww',location:'bj'})
var ct_fea_sel=ct_fea.select(['name'])
Map.centerObject(ct)
print(ct_fea)
Map.addLayer(ct_fea) - ct_fea_sel=ct_fea.select(['name'],['名称'])
2.transform(proj, maxError)
与geometry一样,将某种坐标系转换为另一种坐标系
3.set(var_args)
对相关的属性信息进行重写或覆盖
-
var ct=ee.Geometry.Polygon([
[116.38307485597535,39.915666765209366],
[116.39178667085572,39.915666765209366],
[116.39178667085572,39.92142675789876],
[116.38307485597535,39.92142675789876]
])
var ct_fea=ee.Feature(ct,{name:'ww',location:'bj'})
var ct_set=ct_fea.set('name','wwww','location','bbjj')
print(ct_fea,ct_set)
Map.centerObject(ct)
Map.addLayer(ct_fea)
4.setMulti(properties)对多个变量进行修改
注意:括号里边跟的的字典形式
- var ct_set=ct_fea.setMulti({'name':'wwww','location':'bbjj'})
(3)几何
与geometry一样也可以对feature进行同样的操作
centroid(质心)/simplify(简化)/bounds(边界)/convexHull(边)/buffer(缓冲区)
union()联合 intersection()相交 difference求差
(4)提取
geometry将feature的属性信息剥离只剩下属性信息
get(属性字段)获取该属性字段的属性值
area/perimeter获得面积和周长
三、【FeatureCollection】
即feature的集合
(1)创建
1.GEE自带
在搜索框中输入‘table’>点击‘more’
2.上传
3.ee.FeatureCollection()
首先建立不同的特征,如point、line、polygon,然后将这些特征通过ee.FeatureCollection([point,line,polygon])集合起来,
Feature和FeatureCollection的关系,类似于arcgis中的要素和要素集,或者supermap中的数据集和数据源
4.FeatureCollection.randomPoints(生成点的范围,生成的点数) 创建随机点
前提条件是要确定生成点的范围
5.手绘,方法类似于feature
(2)编辑
1.FeatureCollection.filterMetadata()依据属性值筛选
-
var coun = ee.FeatureCollection("users/wangchengcong/HB");
var coun_hb=coun.filterMetadata('NAME','equals','唐山')//选择NAME为唐山的地区
Map.centerObject(coun_hb)
Map.addLayer(coun,{color:'FFFF00'});
Map.addLayer(coun_hb,{color:'FF0000'});
2.FeatureCollection.limit()依据限制大小筛选
- var coun_china=coun.limit(筛选多少个量,要依据哪个属性字段来筛选,正序还是倒序)
3.FeatureCollection.filterDate()依据时间筛选
- var landsat_filterDate=imageCollection.filterDate('起始时间',‘终止时间’).limit(筛选个数)
4.FeatureCollection.filterBounds()依据空间位置筛选
-
var coun = ee.FeatureCollection("users/wangchengcong/HB");
var point=ee.Geometry.Point([118.2914,39.8459]);
var ts=coun.filterBounds(point)Map.centerObject(coun)
Map.addLayer(coun);
Map.addLayer(ts,{color:'FF0000'});
5.FeatureCollection.filter()
是上述筛选的总称
FeatureCollection.select()同feature中的选择
FeatureCollection.distinct(['属性字段'])去除重复字段
FeatureCollection.union()合并,没有边界,原来的属性全部丧失
FeatureCollection.merge()融合。有边界
FeatureCollection.set()同feature中的set
FeatureCollection.remap()是对某一栏目下的属性进行批量重新命名操作
-
var China_Provinces=ee.FeatureCollection("users/wangjinzhulala/China_Provinces");
var Old_Provinces_lD=ee.List([1,2,3,4,5,6,7,8,9,12,13,14,15,16,18,19,20,21,22,23,24,27,28,29,30,31,32,33,34,35,36,37,38,39,1089])
var New_Provinces_lD=ee.List([1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4,4,5,5,5,5,5,6,6,6,6,6,7,7,7,7,7])
var China_Remap = China_Provinces.remap(Old_Provinces_lD,New_Provinces_lD,'OBJECTID')
var China_Provinces_Map =China_Provinces.reduceTolmage(['OBJECTID'], ee.Reducer.first())
var China_Remap_Map = China_Provinces.reduceTolmage(['OBJECTID'], ee.Reducer.first())
Map.centerObject(China_Provinces,4)
Map.addLayer(China_Provinces_Map,{min:1, max:40, palette:'16ff07,2901ff'},'China_Provinces_Map')
Map.addLayer(China_Remap_Map,{min:1, max:7, palette:'ff7248,fbff21,09ffe8'},'China_Remap_Map')
FeatureCollection.sort()对原来的属性表进行排序
FeatureCollection.makeArray()将一些选中的属性信息弄成array
(3)转换
FeatureCollection.geometry()获取geometry信息
FeatureCollection.reduceTolmage(['属性字段',ee.Reducer])矢量转栅格
(4)查询
FeatureCollection.first()只选取第一个
FeatureCollection.toList()将某个属性字段变成list,好处就是可以提取任意位置的feature
-
var list=china_province.sort('属性字段',正序倒序).toList(10)//排序选取前10个
var no1=ee.Feature(list.get(1))获取第二个值
FeatureCollection.aggregate_ first()某一列的第一个值
FeatureCollection.aggregate_ array()某一列排序
(5) 统计
FeatureCollection.aggregate_ stats属性表最大最小值等/_ histogram直方图数值/_ count/_ count _distinct
FeatureCollection.aggregate. _max/_ min/, sum/_ _mean/_ product
FeatureCollection.aggregate_ sample_var/_ total _var/, sample_ sd/_ total _sd/
(6)其他
FeatureCollection.map()对每个元素进行同样的操作
-
var HB=ee.FeatureCollection("users/wangchengcong/HB");
function add(fea)
{
return fea.centroid()
}
var center=HB.map(add)Map.centerObject(HB,4)
Map.addLayer(HB)
Map.addLayer(center,{color:'FF0000'})