2016-02-29 36 views
0

我在GIS StackExchange上問過這個問題,但到目前爲止沒有運氣。我想也許它屬於這裏。如何在Google地球引擎中獲得百分比而不是頻率?

我用下面的腳本:

// define the var 
var Catchment = /* color: 98ff00 */geometry; 
var landcover = ee.Image('users/roynahas/ESACCI-LC-L4-LCCS-Map-300m-P5Y-2010-v161_RECLASS').select('b1'); 
// Clip the image to the polygon geometry and add it to the map 
var landcover_clip = landcover.clip(Catchment); 
var sld_intervals = 
'<RasterSymbolizer>' + 
'<ColorMap type="intervals" extended="false" >' + 
'<ColorMapEntry color="#FFFF00" quantity="1" label="Agriculture"/>' + 
'<ColorMapEntry color="#00FF00" quantity="2" label="Grassland and Shrubland"/>' + 
'<ColorMapEntry color="#008000" quantity="3" label="Forest"/>' + 
'<ColorMapEntry color="#00FFFF" quantity="4" label="Flooded"/>' + 
'<ColorMapEntry color="#FF00FF" quantity="5" label="Urban areas"/>' + 
'<ColorMapEntry color="#808080" quantity="6" label="Bare areas"/>' + 
'<ColorMapEntry color="#0000FF" quantity="7" label="Water"/>' + 
'<ColorMapEntry color="#FFFFFF" quantity="8" label="Permanent snow and ice"/>' + 
'</ColorMap>' + 
'</RasterSymbolizer>'; 
Map.addLayer(landcover_clip.sldStyle(sld_intervals), {}, 'IGBP classification styled'); 
// Print out the frequency of landcover occurrence for the polygon. 
var frequency = landcover.reduceRegion({ 
    reducer:ee.Reducer.frequencyHistogram(), 
    geometry:Catchment, 
    scale:300 
}); 
print('landcover frequency', frequency.get('b1')); 

enter image description here

得到以下控制檯輸出:

enter image description here

所以我的問題是:怎樣纔可以有一個百分比而不是頻率?或換句話說:是否有相當於ee.Reducer.frequencyHistogram()的百分比?

回答

1

我在另一個論壇上得到了答案。這裏是:

// Import variables 
var globcover = ee.Image("ESA/GLOBCOVER_L4_200901_200912_V2_3"), 
    geometry = /* color: d63000 */ee.Geometry.Polygon(
     [[[-71.466064453125, 48.763431137917955], 
      [-71.378173828125, 48.89000369970676], 
      [-72.674560546875, 49.38952445158216], 
      [-73.179931640625, 49.106241774469055], 
      [-73.575439453125, 48.27588152743497], 
      [-72.83935546875, 48.10743118848039], 
      [-71.4935302734375, 48.17341248658084], 
      [-71.455078125, 48.56024979174331], 
      [-71.5374755859375, 48.68370757165362]]]); 
// Extract the landcover band 
var landcover = globcover.select('landcover'); 
// Clip the image to the polygon geometry 
var landcover_roi = landcover.clip(geometry); 
// Add a map layer of the landcover clipped to the polygon. 
Map.addLayer(landcover_roi); 
// Print out the frequency of landcover occurrence for the polygon. 
var frequency = landcover.reduceRegion({ 
    reducer:ee.Reducer.frequencyHistogram(), 
    geometry:geometry, 
    scale:1000 
}); 
var dict = ee.Dictionary(frequency.get('landcover')); 
var sum = ee.Array(dict.values()).reduce(ee.Reducer.sum(),[0]).get([0]); 
var new_dict = dict.map(function(k,v) { 
    return ee.Number(v).divide(sum).multiply(100); 
}); 
print('Land Cover (%)',new_dict); 

當然,輸入(土地覆蓋圖像和多邊形圖層)可以是不同的。