微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将具有未知键的字典转换为Earh引擎中的功能集合

如何解决将具有未知键的字典转换为Earh引擎中的功能集合

我在转换ee.Dictionary中的sampleRegion返回的某些Earth Engine(具有空值键)时遇到麻烦。我正在尝试对多个区域的多波段图像进行采样,然后将生成的字典转换为ee.FeatureCollection,其中(字典的)键/值对将成为具有null几何形状的特征。我想保留所有键,包括具有null值的键。具有null值的键应重新编码为9或保留为null,但我需要它们作为最终集合中的功能。我尝试使用ee.Algorithms.If来处理这些具有null值的键,但出现堆栈并出现以下错误

FeatureCollection(错误) map(ID = 0)中的错误:Element.geometry,参数'feature':类型无效。预期类型:元素。实际类型:字符串。实际值:B3

以下是可复制的示例,也可以在here中找到。任何提示都会有很大帮助!

// Some features to use latter in sampleRegion

var roi1 = 
    /* color: #d63000 */
    /* shown: false */
    /* displayProperties: [
      {
        "type": "rectangle"
      },{
        "type": "rectangle"
      },{
        "type": "rectangle"
      }
    ] */
    ee.FeatureCollection(
        [ee.Feature(
            ee.Geometry.polygon(
                [[[1.2850232278161755,14.924433184708537],[1.2850232278161755,14.741234323298656],[1.4882702981286755,14.924433184708537]]],null,false),{
              "system:index": "0"
            }),ee.Feature(
            ee.Geometry.polygon(
                [[[1.4772839700036755,14.04155518401385],[1.4772839700036755,13.86296344675159],[1.6393323098474255,14.04155518401385]]],{
              "system:index": "1"
            }),ee.Feature(
            ee.Geometry.polygon(
                [[[1.0817761575036755,14.478114793660426],[1.0817761575036755,14.313173466470698],[1.2767834817224255,14.478114793660426]]],{
              "system:index": "2"
            })]),roi2 = 
    /* color: #98ff00 */
    /* displayProperties: [
      {
        "type": "rectangle"
      }
    ] */
    ee.FeatureCollection(
        [ee.Feature(
            ee.Geometry.polygon(
                [[[1.6970105325036755,14.448859913271122],[1.6970105325036755,14.25994066279539],[1.9387097512536755,14.448859913271122]]],{
              "system:index": "0"
            })]),roi3 = 
    /* color: #0b4a8b */
    /* displayProperties: [
      {
        "type": "rectangle"
      }
    ] */
    ee.FeatureCollection(
        [ee.Feature(
            ee.Geometry.polygon(
                [[[1.7739148293786755,14.38501773168985],[1.7739148293786755,14.29188185649032],[1.8755383645349255,14.38501773168985]]],{
              "rec": 3,"system:index": "0"
            })]);

// Getting the image of the region of interest
var roi = ee.Geometry.Point([1.864578244475683,14.492292970253338]);
var image = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA')
                .filterDate('2019-01-01','2019-01-31')
                .filterBounds(roi)
                .select(['B5','B4','B3'])
                .toBands()
                .rename(['B5','B3']);
                
// Checking it out
print(image);

// Define the visualization parameters.
var vizParams = {
  bands: ['B5','B3'],min: 0,max: 0.5,gamma: [0.95,1.1,1]
};

// Center the map and display the image.
Map.centerObject(image,9);
Map.addLayer(image,vizParams,'image');

// masking out some regions from the 
// image,so that sampleRegion will return null in that region
var mask = ee.Image.constant(1).clip(roi2).mask().not()
var imageMasked = image.updateMask(mask);

// displaying the masked image
Map.addLayer(imageMasked,'imageMasked');

/////////// The actual problem start from here ///////////

// making a feature collection (masked + unmasked region)
var roi = roi1.merge(roi3); 
var regionSamples = roi.map(function(x){
  var out = imageMasked.reduceRegion({
    reducer  : ee.Reducer.mean().unweighted(),geometry : x.geometry(),scale    : 30
  })
  // Getting the keys of the dictionary returned by sampleRegion
  var keys = out.keys()
  // mapping a function over the list of
  // keys to latter extract their corresponding values
  var keyvals = keys.map(function(y){
    var proba = ee.Algorithms.If({
              // test if the value corresponding to a key is null
              condition: ee.Algorithms.IsEqual(out.get(y),null),// if it the case,return a feature with property prob set to 9
              trueCase: ee.Feature(null,{prob: 9}),// if it not the case,return a feature with property prob
              // set the value return by sampleRegion
              falseCase: ee.Feature(null,{prob: out.get(y)})
            })
  return proba
  })
  return ee.FeatureCollection(keyvals)
})
print(regionSamples.flatten(),'regional samples')

解决方法

我终于弄清楚了,如果有人感兴趣,我会发布答案。我使用下面的函数解决了这些问题。

/////////// The actual problem start from here ///////////

/**
 * Reduce multiple regions of an image to feature collection. 
 * @param  {Image} The image to reduce.
 * @param  {FeatureCollection} roi The area/areas of interest.
 * @param  {Float} scale A nominal scale in meters of the projection to work in.
 * @param  {Float} nullKeyValue The value to use for keys where reduceRegion returns null.
 * @return {FeatureCollection}  A feature collection where dictionary keys returned by reduceRegion are converted to ee.Feature.
 */
function sampleFeatures(image,roi,nullKeyValue,scale){
  var keyVals = roi.map(function(x){
    var dictionary = image.reduceRegion({
      reducer  : ee.Reducer.mean().unweighted(),geometry : x.geometry(),scale    : scale
    })
    var noNullDic = dictionary.map(function(key,val){
      var dic = ee.Algorithms.If({
        condition: ee.Algorithms.IsEqual(val,null),trueCase: nullKeyValue,falseCase: dictionary.get(key)
      })
      return dic;
    });
    var keys = noNullDic.keys()
    var vals = keys.map(function(key){
      var vl = ee.List([noNullDic.get(key)])
      var ky = ee.List([key])
      return ee.Feature(null,ee.Dictionary.fromLists(ky,vl))
    })
    return ee.FeatureCollection(vals)
  })
  return keyVals.flatten()
}
var test = sampleFeatures(imageMasked,999,30)
print(test,'test sampleFeatures')

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?