【问题标题】:How to create a feature collection after importing different shapefiles from assets in Google Earth Engine?从 Google 地球引擎中的资产导入不同的 shapefile 后如何创建特征集合?
【发布时间】:2021-07-22 15:12:31
【问题描述】:

我正在从资产中导入 2 个不同的 shapefile 来绘制 MODIS 的时间序列图

但我无法从这些 shapefile 中收集特征

怎么做?


var Haryana_state = ee.FeatureCollection('users/abhilashaanu92/HaryanaBoundary');

Map.addLayer(Haryana_state);

var Punjab_state = ee.FeatureCollection('users/abhilashaanu92/punjab_state_boundary');

Map.addLayer(Punjab_state);

// Combine features into a feature collection.
var both_states = ee.FeatureCollection([Haryana_state, Punjab_state]).flatten();

Map.addLayer(both_states);

// Load MODIS vegetation indices data and subset annual images.


var vegIndices = ee.ImageCollection('MODIS/006/MOD13A1')
                     .filter(ee.Filter.date('2019-01-01', '2020-01-01'))
                     .select(['NDVI', 'EVI']);

// Define the chart and print it to the console.
var chart =
    ui.Chart.image
        .seriesByRegion({
          imageCollection: vegIndices,
          band: 'NDVI',
          regions: both_states,
          reducer: ee.Reducer.mean(),
          scale: 500,
          seriesProperty: 'label',
          xProperty: 'system:time_start'
        })
        .setOptions({
          title: 'Average NDVI Value by Date',
          hAxis: {title: 'Date', titleTextStyle: {italic: false, bold: true}},
          vAxis: {
            title: 'NDVI (x1e4)',
            titleTextStyle: {italic: false, bold: true}
          },
          lineWidth: 5,
          colors: ['f0af07', '0f8755', '76b349'],
        });
print(chart);

如果我将单个 shapefile 变量的名称放在区域中,那么它可以工作,但是当我将 2 个 shapefile 组合到一个特征集合中时,它会显示错误。

如何解决这个问题?

我想要输出这样的东西。 (同一图表上两种状态的时间序列)。

Chart Source URL

【问题讨论】:

    标签: javascript google-earth-engine


    【解决方案1】:

    这里需要做两件事。首先,将两个FeatureCollection合并后添加.flatten()

    这样,您可以创建所需的特征集合 (FeatureCollection)。 否则,您最终会得到一个 FeatureCollections 集合,这会提示错误。

    其次,seriesProperty 需要与您的 FeatureCollection 的标签相匹配。在这种情况下,“STATE_NAME”。您可以通过添加 print(both_states) 来检查新 FeatureCollection 的外观。我已经更新了代码。

    var Haryana_state = ee.FeatureCollection('users/abhilashaanu92/HaryanaBoundary');
    
    Map.addLayer(Haryana_state);
    
    var Punjab_state = ee.FeatureCollection('users/abhilashaanu92/punjab_state_boundary');
    
    Map.addLayer(Punjab_state);
    
    // Combine features into a feature collection.
    var both_states = ee.FeatureCollection([Haryana_state, Punjab_state]).flatten();
    print('Check the properties; this will tell you what seriesProperty to use', both_states)
    
    Map.addLayer(both_states);
    
    // Load MODIS vegetation indices data and subset annual images.
    
    
    var vegIndices = ee.ImageCollection('MODIS/006/MOD13A1')
                         .filter(ee.Filter.date('2019-01-01', '2020-01-01'))
                         .select(['NDVI', 'EVI']);
    
    // Define the chart and print it to the console.
    var chart =
        ui.Chart.image
            .seriesByRegion({
              imageCollection: vegIndices,
              band: 'NDVI',
              regions: both_states,
              reducer: ee.Reducer.mean(),
              scale: 500,
              seriesProperty: 'STATE_NAME',
              xProperty: 'system:time_start'
            })
            .setOptions({
              title: 'Average NDVI Value by Date',
              hAxis: {title: 'Date', titleTextStyle: {italic: false, bold: true}},
              vAxis: {
                title: 'NDVI (x1e4)',
                titleTextStyle: {italic: false, bold: true}
              },
              lineWidth: 5,
              colors: ['f0af07', '0f8755', '76b349'],
            });
    print(chart);
    

    【讨论】:

    • 你能在上面的代码中编辑它吗?我试过了,没有得到正确的方法
    • 嘿@CrossLord,我看不到您在代码中所做的编辑。
    • 代码加入了这两个特征,但只绘制了一个多边形的数据(仅限旁遮普)。
    • 首先,您还没有共享您的资产,因此很难复制您的代码。要回答您之前的评论,使用 seriesProperty: 'system:index' 而不是 'label' 应该可以解决问题。
    猜你喜欢
    • 2017-10-07
    • 2022-07-03
    • 2019-05-21
    • 2021-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-25
    相关资源
    最近更新 更多