簡體   English   中英

如何從屬性字典 object 中提取屬性值

[英]How to extract a property value from a property dictionary object

此腳本的目的是從過濾的集合中獲取降水值,並將它們分配給稱為“precipitationCal”的降水屬性。

經過一些掩蔽后,我有一個名為“precipitationCal”的嵌套屬性,它實際上是一個字典 object 本身。 以下直方圖縮減器生成兩個字典值,一個被屏蔽,一個未被屏蔽。 如果存在掩碼值(鍵為“1”),我需要提取該值。 如果它不存在(鍵為“0”),我可以使用該值。 最后,我需要將這些值中的任何一個分配給一個名為“precipitationCal”的重命名屬性。

目前我收到錯誤“無法讀取未定義的屬性'if'”。 但是,如果將 function 應用於第一個圖像,則該鍵會返回一個值。 任何幫助將非常感激。

var geometry = ee.Geometry.MultiPolygon(
    [[[[-39.40378, -16.86954],
       [-39.40378, -16.88301],
       [-39.39863, -16.88301],
       [-39.39863, -16.86954]]],
     [[[-39.37288, -16.87414],
       [-39.37288, -16.88859],
       [-39.36430, -16.88859],
       [-39.36430, -16.87414]]],
     [[[-39.34198, -16.86625],
       [-39.34198, -16.87348],
       [-39.33409, -16.87348],
       [-39.33409, -16.86625]]]], null, false);

var START_DATE = '2020-01-01',
END_DATE = '2020-02-01'

var raincol = ee.ImageCollection('NASA/GPM_L3/IMERG_V06')
  .filterDate(START_DATE, END_DATE)

raincol = raincol.select('precipitationCal')

//function to remove masked images
var filter_masked = function(img) {
// unmask each image in the collection
var unmasked = img.unmask(-999).eq(-999);
// reduce histogram on each image and set  keys as properties (key '1' will be masked pixels)
var rR = unmasked.reduceRegion({
                          reducer: ee.Reducer.frequencyHistogram(),
                          geometry: geometry,
                          scale: 100,
                          bestEffort: true
                          });
var newProperty = ee.Dictionary(rR.get('precipitationCal'));
return img.set(newProperty)}

// apply the reducer mask                
var raincol = raincol.map(filter_masked)

//filter precip collection to eliminate masked pixels
var raincol = ee.ImageCollection(raincol).filter(ee.Filter.notNull(['1']).not())

//rename precip property 
raincol = raincol.map(function(image) {
                var precipdict = ee.Dictionary(image.get('precipitationCal'))
                    return ee.Algorithm.if(precipdict.contains('1')===true, 
                    image.set('precipitationCal', precipdict.get('1')),
                    image.set('precipitationCal', precipdict.get('0'))
                    )
              })

它是ee.Algorithms.If不是ee.Algorithm.if

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM