Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/404.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将具有未知键的词典转换为Earh引擎中的特征集合_Javascript_Google Earth Engine - Fatal编程技术网

Javascript 将具有未知键的词典转换为Earh引擎中的特征集合

Javascript 将具有未知键的词典转换为Earh引擎中的特征集合,javascript,google-earth-engine,Javascript,Google Earth Engine,转换Earth Engine中的sampleRegion返回的一些ee.Dictionary(带空值键)时遇到问题。我正在尝试对多个区域的多波段图像进行采样,然后将结果字典转换为ee.FeatureCollection,其中(字典的)键/值对将是具有null几何体的功能。我想保留所有键,包括那些具有null值的键。具有null值的键应重新编码为9或保留为null,但我需要它们作为最终集合的功能。我尝试使用ee.Algorithms.If来处理这些具有null值的键,但我得到了如下错误的堆栈: F

转换
Earth Engine
中的
sampleRegion
返回的一些
ee.Dictionary
(带空值键)时遇到问题。我正在尝试对多个区域的多波段图像进行采样,然后将结果字典转换为
ee.FeatureCollection
,其中(字典的)键/值对将是具有
null
几何体的功能。我想保留所有键,包括那些具有
null
值的键。具有
null
值的键应重新编码为9或保留为
null
,但我需要它们作为最终集合的功能。我尝试使用
ee.Algorithms.If
来处理这些具有
null
值的键,但我得到了如下错误的堆栈:

FeatureCollection(错误) 映射中出错(ID=0):Element.geometry,参数“feature”:类型无效。应为类型:元素。实际类型:String。实际值:B3

下面是一个可重复的例子,也可以找到。任何提示都会大有帮助

// 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.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, 13.86296344675159],
                  [1.6393323098474255, 14.04155518401385]]], null, false),
            {
              "system:index": "1"
            }),
        ee.Feature(
            ee.Geometry.Polygon(
                [[[1.0817761575036755, 14.478114793660426],
                  [1.0817761575036755, 14.313173466470698],
                  [1.2767834817224255, 14.313173466470698],
                  [1.2767834817224255, 14.478114793660426]]], null, false),
            {
              "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.25994066279539],
                  [1.9387097512536755, 14.448859913271122]]], null, false),
            {
              "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.29188185649032],
                  [1.8755383645349255, 14.38501773168985]]], null, false),
            {
              "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', 'B4', 'B3']);
                
// Checking it out
print(image);

// Define the visualization parameters.
var vizParams = {
  bands: ['B5', 'B4', '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, vizParams, '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, roi, 999, 30)
print(test, 'test sampleFeatures')