【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(選擇屬性, newPropertiesretainGeometry)

如創建一個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(projmaxError)

與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'})

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM