在Google Earth Engine上将一层剪切到另一层的边界

时间:2018-10-30 22:58:50

标签: javascript clip google-earth-engine data-layers

所以我想将一层(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');

1 个答案:

答案 0 :(得分:0)

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

如果仅要将NDVI值保留在玉米所在的位置,则应使用var masked = ndvi.updateMask(corn)。这样只会保留NDVI像素,也就是玉米像素。

我尝试运行您的代码,看来您发布的代码中没有某些几何图形,因此很难确定代码到底出了什么问题。我希望这有帮助!如果没有,只需将链接发送到您正在使用的代码以及引起问题的行。