[GIS] Selecting Points within Area using Google Earth Engine

google-earth-engine

I have the following code in Google Earth Engine:

var dataset = ee.ImageCollection('MODIS/006/MOD14A1')
                  .filter(ee.Filter.date('2018-01-17', '2019-01-04'));

var fireMaskVis = {
  min: 0.0,
  max: 6000.0,
  bands: ['MaxFRP', 'FireMask', 'FireMask',]
};
Map.setCenter(6.746, 46.529, 2);
Map.addLayer(dataset, fireMaskVis, 'Fire Mask');

// coordinates to zoom to and get statistics from

var point = ee.Geometry.Point([2.3622940161999395, 42.569280018996714]);

Map.addLayer(AOI, {}, 'the area of interest');
Map.centerObject(AOI, 11);

var fire = dataset.select("FireMask");

// reduce the surface temperature image collection to a single image with stacked bands
function collToBands(imageCollection, bandName) {
  // stack all the bands to one single image
  // change the name of the bandname to the date it is acquired
  var changedNames = imageCollection.map( function(img){ 
    var dateString = ee.Date(img.get('system:time_start')).format('yyyy-MM-dd');
    return img.select(bandName).rename(dateString);
  });

  // Apply the function toBands() on the image collection to set all bands into one image
  var multiband = changedNames.toBands();
  // Reset the bandnames
  var names = multiband.bandNames();
  // rename the bandnames 
  var newNames = names.map(function(name){
    var ind = names.indexOf(name);
    return ee.String(names.get(ind)).slice(5);
  });
  return multiband.rename(newNames);
}
// apply the function and print to console
var multiband = collToBands(fire, "FireMask");
//print('collection to bands', multiband);

// get the temperature at a given point on the map for the given time spand and print to the console
var firePoint = multiband.reduceRegion({reducer: ee.Reducer.mean(), geometry: point, scale: 1000, bestEffort: true});
print(firePoint);

///// DO THE SAME OPERATION FOR MULTIPLE SPACED POINTS IN AN AREA OF INTEREST

// make a feature collection of many pixelsize-spaced points
function spacedPoints(AOI, proj) {
  // make a coordinate image
  // get coordinates image
  var latlon = ee.Image.pixelLonLat().reproject(proj);
  // put each lon lat in a list -> this time for getting an multipoint list (more useful inside the GEE)
  var coords = latlon.select(['longitude', 'latitude'])
                 .reduceRegion({reducer: ee.Reducer.toList(),
                                geometry: AOI,
                                scale: proj.nominalScale().toInt()
  });
  // zip the coordinates for representation. Example: zip([1, 3],[2, 4]) --> [[1, 2], [3,4]]
  var point_list = ee.List(coords.get('longitude')).zip(ee.List(coords.get('latitude')));
  // preset a random list
  var list = ee.List([0]);
  // Make a feature collection of the multipoints and assign distinct IDs to every feature
  var feats = ee.FeatureCollection(point_list.map(function(point){
    var ind = point_list.indexOf(point);
    var feat = ee.Feature(ee.Geometry.Point(point_list.get(ind)), {'ID': ind});
    return list.add(feat);
  }).flatten().removeAll([0]));
  return feats;
}
// use function to get the spaced points
var points = spacedPoints(geometry, multiband.select(0).projection());
//print(points);
// add to the map
Map.addLayer(points.draw('red'), {}, 'the spaced points');

// calculate the temperature over the time span at every point in the AOI
var fires = multiband.reduceRegions({collection: points, reducer: ee.Reducer.mean(), scale: 10000});

I have a polygon called AOI covering California, and I am trying to select points inside the polygon from a collection of points called geometry. When I run the code, I get the following error:

the spaced points: Layer error: An internal server error has occurred (0c65b8234cd71e9738a03fdbf87e5acb).

I did see that when I tried to find data for one point, it worked. I think it might have to do with the number of decimal places I go to on the coordinates. In the geometry points, the coordinates only go out to 4 decimal places, but with the single point I tested, it went out to 16 decimal places, and that worked.

Here is the link to the script in order to see the geometry and AOI imports:

https://code.earthengine.google.com/859fa14b852cf3aa852dbf654a2ed293

Best Answer

As it turns out, I was using the wrong coordinate system in the collection of points, so when I reduced the region of the AOI to those points, the coordinates were way outside of the area. After fixing the coordinates, the script worked fine.

Related Question