错误:
Error generating chart: The image collection is empty.
No features contain non-null values of "system:time_start".
The image collection is empty.
原始代码:
var geometry = /* color: #d63000 */ee.Geometry.Polygon(
[[[-81.14573791618602, 25.335453210591712],
[-80.90953186149852, 25.32924702619973],
[-81.03312805290477, 25.5190118912932],
[-81.1992962657954, 25.50537864099872]]]),
Mang = ee.FeatureCollection("users/selenachav5/mangrove_vector");
var WSC1050 = /* color: #98ff00 */ee.Geometry.Point([-81.11963534, 25.42344856]);
var mask = Mang
var mangroveImg = ee.Image(0).paint(mask, 1);
Map.addLayer(mangroveImg, {min:0, max:1}, 'mangroveImg')
function maskNonMangrove(i){
return i.updateMask(mangroveImg).copyProperties(i);
}
var col = ee.ImageCollection('MODIS/061/MOD13Q1')
.map(function(im) {return im.select(['NDVI']).multiply(0.0001)})
.filter(ee.Filter.date('2013-01-01', '2023-05-01'))
.map(maskNonMangrove)
;
var ndvi = col.select('NDVI');
var ndviVis = {
min: 0.0,
max: 8000.0,
palette: [
'FFFFFF', 'CE7E45', 'DF923D', 'F1B555', 'FCD163', '99B718', '74A901',
'66A000', '529400', '3E8601', '207401', '056201', '004C00', '023B01',
'012E01', '011D01', '011301'
],
};
Map.setCenter(6.746, 46.529, 2);
Map.addLayer(ndvi, ndviVis, 'NDVI');
var ndviChart = ui.Chart.image.series(ndvi, WSC1050, ee.Reducer.mean(), 250);
ndviChart.setOptions({
title: 'MODIS NDVI',
vAxis: {title: 'NDVI', maxValue: 9000},
hAxis: {title: 'date', format: 'MM-yy', gridlines: {count: 7}},
});
print(ndviChart)
解析:
出现这个的主要原因是,我们在对影像集合进行映射计算的出现了掩盖掉时间属性的东西,所以我们需要在进行映射函数之后要进行属性的追加,也就是及逆行属性的copy,从而保留这个时间属性,因为在默认状态下chart中是需要进行时间属性的来作为横轴进行加载的,所以这里我们必须要保留原有的影像时间信息。
关键代码:
.copyProperties(i,['system:time_start'])
图表函数:
ui.Chart.image.series(imageCollection, region, reducer, scale, xProperty)
Generates a Chart from an ImageCollection. Plots derived values of each band in a region across images. Usually a time series.
-
X-axis: Image, labeled by xProperty value.
-
Y-axis: Band value.
-
Series: Band names.
Returns a chart.
Arguments:
imageCollection (ImageCollection):
An ImageCollection with data to be included in the chart.
region (Feature|FeatureCollection|Geometry):
The region to reduce.
reducer (Reducer, optional):
Reducer that generates the values for the y-axis. Must return a single value. Defaults to ee.Reducer.mean().
scale (Number, optional):
Scale to use with the reducer in meters.
xProperty (String, optional):
Property to be used as the label for each image on the x-axis. Defaults to 'system:time_start'.
Returns: ui.Chart
修改后的代码:
var WSC1050 = /* color: #98ff00 */ee.Geometry.Point([-81.11963534, 25.42344856]);
var mask = Mang
var mangroveImg = ee.Image(0).paint(mask, 1);
Map.addLayer(mangroveImg, {min:0, max:1}, 'mangroveImg')
function maskNonMangrove(i){
return i.updateMask(mangroveImg).copyProperties(i,['system:time_start']);
}
var col = ee.ImageCollection('MODIS/061/MOD13Q1')
.map(function(i) {return i.select(['NDVI']).multiply(0.0001).copyProperties(i,['system:time_start'])})
.filter(ee.Filter.date('2013-01-01', '2023-05-01'))
.map(maskNonMangrove)
;
var ndvi = col.select('NDVI');
var ndviVis = {
min: 0.0,
max: 8000.0,
palette: [
'FFFFFF', 'CE7E45', 'DF923D', 'F1B555', 'FCD163', '99B718', '74A901',
'66A000', '529400', '3E8601', '207401', '056201', '004C00', '023B01',
'012E01', '011D01', '011301'
],
};
Map.setCenter(6.746, 46.529, 2);
Map.addLayer(ndvi, ndviVis, 'NDVI');
var ndviChart = ui.Chart.image.series(ndvi, WSC1050, ee.Reducer.mean(), 250);
ndviChart.setOptions({
title: 'MODIS NDVI',
vAxis: {title: 'NDVI', maxValue: 1},
hAxis: {title: 'date', format: 'MM-yy', gridlines: {count: 7}},
});
print(ndviChart)