从 Google Earth Engine 中的资产导入不同的 shapefile 后如何创建要素集合?

How to create a feature collection after importing different shapefiles from assets in Google Earth Engine?

我正在从资产中导入 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

这里需要做两件事。首先,在合并两个FeatureCollections后添加.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);