错误“数组:参数'值'是必需的”

时间:2019-11-28 13:28:19

标签: javascript arrays image google-earth-engine

我正在使用一个名为Google Earth Engine的平台,该平台可以对云中的卫星图像进行大量分析。 我已经用Javascript编写了一个代码,该代码需要两个图像,在其中使用一个带,然后假设它会创建散点图。 我对散点图中难以理解的部分有疑问。

任何时候我运行此部分:

// Convert the band data to plot on the y-axis to arrays.
var x= ee.Array(imageNDVIcor.get('NDVI'));
var y = ee.Array(SARreproject.get('VH'));

// Make a band correlation chart.
var chart = ui.Chart.array.values(y, 0, x)
    .setSeriesNames(['SAR vs NDVI'])
    .setOptions({
      title: 'NDVI vs SAR VH',
      hAxis: {'title': 'SAR VH'},
      vAxis: {'title': 'NDVI'},
      pointSize: 3,
});

// Print the chart.
print(chart);

我收到以下错误:

  

数组:参数'values'是必需的

我不知道丢失了哪些值,或者丢失的方式和时间。我将完整的代码放在这里,对理解价值贬低的地方会有所帮助。


//STEP 1:NDVI

/**
 * Function to mask clouds using the Sentinel-2 QA band
 * @param {ee.Image} image Sentinel-2 image
 * @return {ee.Image} cloud masked Sentinel-2 image
 */
function maskS2clouds(image) {
  var qa = image.select('QA60');

  // Bits 10 and 11 are clouds and cirrus, respectively.
  var cloudBitMask = 1 << 10;
  var cirrusBitMask = 1 << 11;

  // Both flags should be set to zero, indicating clear conditions.
  var mask = qa.bitwiseAnd(cloudBitMask).eq(0)
      .and(qa.bitwiseAnd(cirrusBitMask).eq(0));

  return image.updateMask(mask).divide(10000)
  .copyProperties(image, ['system:time_start']);
}

// Map the function over one year of data and take the median.
// Load Sentinel-2 TOA reflectance data.
var dataset = ee.ImageCollection('COPERNICUS/S2')
                  .filterDate('2019-01-01', '2019-11-12')
                  // Pre-filter to get less cloudy granules.
                  .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 20))
                  .select('B2','B3','B4','B8','QA60')
                  .filterBounds(geometry)
                  .map(maskS2clouds);

var clippedCol=dataset.map(function(im){ 
   return im.clip(geometry);
});

// Get the number of images.
var count = dataset.size();
print('Count: ',count);
// print(clippedCol);//here I get the error messege "collection query aborted after accumulation over 5000 elements
// print(dataset,'dataset');//the same error here

//function to calculate NDVI
var addNDVI = function(image) {
  var ndvi = image.normalizedDifference(['B8', 'B4'])
  .rename('NDVI')
  .copyProperties(image,['system:time_start']);
  return image.addBands(ndvi);

};

//NDVI to the clipped image collection
var withNDVI = clippedCol.map(addNDVI).select('NDVI');


var NDVIcolor = {
  min: 0,
  max:1,
  palette: ['FFFFFF', 'CE7E45', 'DF923D', 'F1B555', 'FCD163', '99B718', '74A901',
    '66A000', '529400', '3E8601', '207401', '056201', '004C00', '023B01',
    '012E01', '011D01', '011301'],
};

//Filter according to number of pixels

var ndviWithCount = withNDVI.map(function(image){
  var countpixels = ee.Number(image.reduceRegion({
  reducer: ee.Reducer.count(),
  geometry: geometry,
  crs: 'EPSG:4326',
  scale: 20,
  }).get('NDVI'));

  return image.set('count', countpixels);
});

print(ndviWithCount, 'ndviWithCount');

var max = ndviWithCount.reduceColumns(ee.Reducer.max(),  ["count"]);
print('Number of pixels max:',max.get('max'));


//filter between a range
var filterNDVI = ndviWithCount.filter(ee.Filter.rangeContains(
          'count', 98258, 98258));
print('Filtered NDVI:', filterNDVI);

var listOfImages =(filterNDVI.toList(filterNDVI.size()));

var listOfNumbers = [5]



for (var i in listOfNumbers) {
  var image = ee.Image(listOfImages.get(listOfNumbers[i]));
  var toexport=image.visualize(NDVIcolor).addBands(image);


  // do what ever you need with image
  Map.addLayer(image, NDVIcolor, i);
//   Export.image.toDrive({
//   image: toexport.toFloat(),
//   description: i,
//   scale:20,
//   crs:'EPSG:4326',
//   maxPixels:1310361348,
//   region:geometry.geometry().bounds()


// });


 }
Map.centerObject(geometry);


//STEP2: SAR

// Filter the collection for the VH product from the descending track

//var geometry=MITR;
var Sentinel1 = ee.ImageCollection('COPERNICUS/S1_GRD')
        .filter(ee.Filter.listContains('transmitterReceiverPolarisation', 'VH'))
        .filter(ee.Filter.eq('orbitProperties_pass', 'DESCENDING'))
        .filter(ee.Filter.eq('instrumentMode', 'IW'))
        .select('VH')
        .filterDate('2019-01-01','2019-11-12')
        .filterBounds(geometry);

var clippedVH= Sentinel1.map(function(im){ 
   return im.clip(geometry);
}); 

var clippedVHsize=clippedVH.size();
print('SAR Size:',clippedVHsize);
print('SAR images data:',clippedVH)

var listOfImagesSAR =(clippedVH.toList(clippedVH.size()));

var listOfNumbersSAR = [3];

for (var i in listOfNumbersSAR) {
  var image = ee.Image(listOfImagesSAR.get(listOfNumbersSAR[i]));
  var toexport=image.visualize({min: -30, max: 1}).addBands(image);


  // do what ever you need with image
  Map.addLayer(image,{min: -30, max: 1}, i);
  // Export.image.toDrive({
  // image: toexport.toFloat(),
  // description: i,
  // scale:10,
  // crs:'EPSG:4326',
  // maxPixels:1310361348,
  // region:geometry.geometry().bounds()

// });


 }


//print(ui.Chart.image.series(filterNDVI, geometry, ee.Reducer.mean(), 20));
//(ui.Chart.image.series(clippedVH, geometry, ee.Reducer.mean(), 10));

//select the images for scatter plot
//select NDVI
var imageNDVIcor=ee.Image(listOfImages.get(5));
var imageSARcor=ee.Image(listOfImagesSAR.get(3));

// Get information about the  projection.
var sar1Projection =  imageSARcor.projection();
print('SAR projection:', sar1Projection);

var NDVIProjection =  imageNDVIcor.projection();
print('NDVI projection:',  NDVIProjection);

//resample SAR image to NDVI image

var SARreproject=imageSARcor.reduceResolution({reducer: ee.Reducer.mean()}).reproject({crs: NDVIProjection});

Map.addLayer(SARreproject,{min: -30, max: 1},'Reproject SAR');

// print(imageNDVIcor)
// print(imageSARcor)
//Map.addLayer(imageNDVIcor,NDVIcolor,'NDVI select');
//Map.addLayer(imageSARcor,{min: -30, max: 1},'SAR select');




// Convert the band data to plot on the y-axis to arrays.
var x= ee.Array(imageNDVIcor.get('NDVI'));
var y = ee.Array(SARreproject.get('VH'));

// Make a band correlation chart.
var chart = ui.Chart.array.values(y, 0, x)
    .setSeriesNames(['SAR vs NDVI'])
    .setOptions({
      title: 'NDVI vs SAR VH',
      hAxis: {'title': 'SAR VH'},
      vAxis: {'title': 'NDVI'},
      pointSize: 3,
});

// Print the chart.
print(chart);

1 个答案:

答案 0 :(得分:1)

不确定要完成的任务。但是,如果您要比较NDVI和VH之间的值,则可以采用这种方法。单击几何图形内的任何点,然后在控制台选项卡中绘制时间序列中该点的值。

var geometry = 
    ee.Geometry.Polygon(
        [[[-122.56145019531249, 37.93899992220671],
          [-122.56145019531249, 37.37365054197817],
          [-121.80888671874999, 37.37365054197817],
          [-121.80888671874999, 37.93899992220671]]], null, false);


Map.centerObject(geometry, 10);

function maskS2clouds(image) {
  var qa = image.select('QA60');
  var cloudBitMask = 1 << 10;
  var cirrusBitMask = 1 << 11;
  var mask = qa.bitwiseAnd(cloudBitMask).eq(0).and(qa.bitwiseAnd(cirrusBitMask).eq(0));
  return image.updateMask(mask).divide(10000).copyProperties(image, ['system:time_start']);
}

// Sentinel 2
var sentinel2 = ee.ImageCollection('COPERNICUS/S2')
                  .filterDate('2019-01-01', '2019-12-31')
                  .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 20))
                  .select('B2','B3','B4','B8','QA60').filterBounds(geometry).map(maskS2clouds);

// NDVI
var addNDVI = function(image) {
  var ndvi = image.normalizedDifference(['B8', 'B4']).rename('NDVI').copyProperties(image,['system:time_start']);
  return image.addBands(ndvi);

};
var sentinel2NDVI = sentinel2.map(addNDVI).select('NDVI');

// SAR
var sentinel1 = ee.ImageCollection('COPERNICUS/S1_GRD')
        .filter(ee.Filter.listContains('transmitterReceiverPolarisation', 'VH'))
        .filter(ee.Filter.eq('orbitProperties_pass', 'DESCENDING'))
        .filter(ee.Filter.eq('instrumentMode', 'IW'))
        .select('VH').filterDate('2019-01-01','2019-12-31').filterBounds(geometry);

var list1 = sentinel2NDVI.toList(sentinel2NDVI.size());
var list2 = sentinel1.toList(sentinel1.size());
var list = list1.cat(list2);

var combinedData = ee.ImageCollection(list);

// chart
var generateChart = function (coords) {
  print('-----------------------');
  var lat = coords.lat;
  var lon = coords.long;

  var point = ee.Geometry.Point(coords.lon, coords.lat);
  var dot = ui.Map.Layer(point, {color: '000000'}, 'clicked location');

  var chart = ui.Chart.image.series(combinedData, point, ee.Reducer.mean(), 30);

  chart.setOptions({
    title: 'VH vs NDVI',
    hAxis: {'title': 'Time'},
    vAxis: {'title': 'SAR VH'},
    pointSize: 3,
  });
  print(chart);
};


Map.onClick(generateChart);
Map.style().set('cursor', 'crosshair');

此外,还可以从here获取GEE链接。