1

所以我试图将一层(NDVI)剪辑到安大略省玉米田的边界。这是我到目前为止的代码,但它似乎不起作用。我不确定你是否真的可以将图层剪辑到其他图层,我知道你可以预先收集图像,但是关于如何解决这个问题的一些输入会很棒。谢谢您的帮助。

 var landcover_crops = ee.ImageCollection("AAFC/ACI")

// Load a collection of Landsat TOA reflectance images.
var landsatCollection = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA');

// The dependent variable we are modeling.
var dependent = 'NDVI';

// The number of cycles per year to model.
var harmonics = 1;

// Make a list of harmonic frequencies to model.
// These also serve as band name suffixes.
var harmonicFrequencies = ee.List.sequence(1, harmonics);

// Function to get a sequence of band names for harmonic terms.
var constructBandNames = function(base, list) {
  return ee.List(list).map(function(i) {
return ee.String(base).cat(ee.Number(i).int());
  });
};

// Construct lists of names for the harmonic terms.
var cosNames = constructBandNames('cos_', harmonicFrequencies);
var sinNames = constructBandNames('sin_', harmonicFrequencies);

// Independent variables.
var independents = ee.List(['constant', 't'])
  .cat(cosNames).cat(sinNames);

// Function to mask clouds in Landsat 8 imagery.
var maskClouds = function(image) {
  var score = ee.Algorithms.Landsat.simpleCloudScore(image).select('cloud');
  var mask = score.lt(10);
  return image.updateMask(mask);
};

// Function to add an NDVI band, the dependent variable.
var addNDVI = function(image) {
 return image
    .addBands(image.normalizedDifference(['B5', 'B4'])
    .rename('NDVI'))
    .float();
};

// Function to add a time band.
var addDependents = function(image) {
  // Compute time in fractional years since the epoch.
  var years = image.date().difference('2017-01-01', 'year');
  var timeRadians = ee.Image(years.multiply(2 * Math.PI)).rename('t');
  var constant = ee.Image(1);
   return image.addBands(constant).addBands(timeRadians.float());
};

// Function to compute the specified number of harmonics
// and add them as bands.  Assumes the time band is present.
var addHarmonics = function(freqs) {
  return function(image) {
 // Make an image of frequencies.
var frequencies = ee.Image.constant(freqs);
// This band should represent time in radians.
var time = ee.Image(image).select('t');
// Get the cosine terms.
var cosines = time.multiply(frequencies).cos().rename(cosNames);
// Get the sin terms.
var sines = time.multiply(frequencies).sin().rename(sinNames);
return image.addBands(cosines).addBands(sines);
  };
};

 // Filter to the area of interest, mask clouds, add variables.
var harmonicLandsat = landsatCollection
  .filterBounds(geometry2)
  .map(maskClouds)
  .map(addNDVI)
  .map(addDependents)
  .map(addHarmonics(harmonicFrequencies));

// The output of the regression reduction is a 4x1 array image.
var harmonicTrend = harmonicLandsat
  .select(independents.add(dependent))
  .reduce(ee.Reducer.linearRegression(independents.length(), 1));

// Turn the array image into a multi-band image of coefficients.
var harmonicTrendCoefficients = harmonicTrend.select('coefficients')
  .arrayProject([0])
  .arrayFlatten([independents]);

// Compute fitted values.
var fittedHarmonic = harmonicLandsat.map(function(image) {
  return image.addBands(
    image.select(independents)
      .multiply(harmonicTrendCoefficients)
      .reduce('sum')
      .rename('fitted'));
});

 // Plot the fitted model and the original data at the ROI.
print(ui.Chart.image.series(fittedHarmonic.select(['fitted','NDVI']), 
geometry2, ee.Reducer.mean(), 100)
    .setOptions({
      title: 'Harmonic model: original and fitted values',
      lineWidth: 1,
      pointSize: 3,
}));

// Pull out the three bands we're going to visualize.
var sin = harmonicTrendCoefficients.select('sin_1');
var cos = harmonicTrendCoefficients.select('cos_1');

// Do some math to turn the first-order Fourier model into
// hue, saturation, and value in the range[0,1].
var magnitude = cos.hypot(sin).multiply(5);
var phase = sin.atan2(cos).unitScale(-Math.PI, Math.PI);
var val = harmonicLandsat.select('NDVI').reduce('mean');

// Turn the HSV data into an RGB image and add it to the map.
var seasonality = ee.Image.cat(phase, magnitude, val).hsvToRgb();
Map.centerObject(geometry2, 11);
Map.addLayer(seasonality, {}, 'Seasonality');
Map.addLayer(geometry2, {}, 'corn_ndvi');


//need to change the image collection into a single image; 
var crop2017 = landcover_crops
   .filter(ee.Filter.date('2017-01-01', '2017-12-31'))//select for 2017 data
   .first(); //collapses the data

 //find what band names are, so we can filter the data on the right one
var bandNames = crop2017.bandNames();
print('Band names: ',bandNames);

 //then need to select the band of interest...here there's only one band called 
landcover
var crop2017_data=crop2017.select('landcover'); 

//then create various masks by selecting on the land cover value
var urban_mask = crop2017_data.eq(34); //creating the mask
 var urban = crop2017_data.mask(urban_mask); //masking the data
 var corn_mask = crop2017_data.eq(147);
  var corn = crop2017_data.mask(corn_mask);
var soy_mask = crop2017_data.eq(158);
  var soy = crop2017_data.mask(soy_mask);
var hay_mask = crop2017_data.eq(122);
  var hay = crop2017_data.mask(hay_mask);
var grassland_mask = crop2017_data.eq(110);
  var grassland = crop2017_data.mask(grassland_mask);

//Finally, add the masks to the map to make sure they are right  
Map.addLayer(urban,undefined,'Urban');
Map.addLayer(corn,undefined,'Corn');
Map.addLayer(soy,undefined,'Soy');
Map.addLayer(hay,undefined,'Hay');
Map.addLayer(grassland,undefined,'Grassland');

//Can clip the mask to show just ontario
var crop2017_ontario = crop2017.clip(ontario);
var corn_ontario = corn.clip(ontario);
var soy_ontario = soy.clip(ontario);
var urban_ontario = urban.clip(ontario);

Map.addLayer(crop2017_ontario,undefined,'All Crops Ontario');
Map.addLayer(corn_ontario,undefined,"Corn Ontario");
Map.addLayer(soy_ontario,undefined,'Soy Ontario');
Map.addLayer(urban_ontario,undefined,'Urban Ontario');

// Composite an image collection and clip it to a boundary.

// Clip to the output image to the Nevada and Arizona state boundaries.
var clipped = seasonality.clipToCollection(corn_ontario);

// Display the result.
Map.setCenter(-80.24, 43.54);
Map.addLayer(clipped, 'clipped composite');
4

1 回答 1

0

如果您想将 NDVI 图像剪辑到玉米图像的实际边界,您可以使用var clipped = ndvi.clip(corn.geometry()). 每个图像都有一个与之关联的几何对象,并且剪辑函数需要一个几何作为输入。

如果您只想将 NDVI 值保留在玉米所在的位置,那么您应该使用var masked = ndvi.updateMask(corn). 这只会保留也是玉米像素的 NDVI 像素。

我尝试运行您的代码,您发布的代码中似乎有些几何图形不可用,因此很难确定您的代码到底出了什么问题。我希望这有帮助!如果没有,只需将链接发送到您正在工作的代码以及导致问题的行。

于 2018-10-31T17:59:03.107 回答