3

Let's say that I want to chart daily mean sea surface temperatures per X by X degree cell over time, within some larger patch of the ocean. As per these instructions, this is easily done in GEE using ui.Chart.image.seriesByRegion(). That is, provided you have already defined a FeatureCollection of all the individual polygon regions (i.e. X by X degree cells) first.

Defining this collection of gridded cells is where I am getting stuck. Is there a smart way to automate this step? Perhaps one can even construct the cells from the properties of an imported image collection, e.g. one of the gridded NOAA datasets?

Alternatively, I could draw each of the cell polygons by hand, but this is obviously inefficient and haphazard. I could also write a javascript loop, but loops are supposedly best avoided in GEE as I understand it.

FWIW, in R I would use sp::makegrid() or do something like this.

Grant
  • 141
  • 1
  • 6

3 Answers3

5

One option is to use the projection of an image to define a grid over a region of interest. The grid cells will match the dimensions of the pixels in the image. The key function is coveringGrid():

var grid = polyFeature.geometry().coveringGrid(proj)

If you want a grid of points (instead of polygons), use Image.sample():

var pointGrid = image.sample({
  region: polyFeature.geometry(),
  projection: proj, // make sure to use the projection from the correct band
  geometries: true // if you want points
});

example code

Anson Call
  • 403
  • 4
  • 11
3

1000 meter grid of points over Puerto Rico.

var x = ee.List.sequence(-7569586.3359, -7254524.5697, 1000)
var y = ee.List.sequence(2017836.3574, 2104140.5776, 1000)
var features = ee.FeatureCollection(
  x.map(function(xcor){
    var feat = ee.FeatureCollection(
      y.map(function(ycor){
        return ee.Feature(
          ee.Geometry.Point([xcor, ycor], ee.Projection("EPSG:3857"))
        )
      })
    )
    return feat
  })
).flatten()

Map.addLayer(features)

Source by thisearthsite at mygeoblog.com, revised by me.

Kevin
  • 245
  • 2
  • 10
1

I'd forgotten about this question, but (as intimated by @Rodrigo above) it turned out that a regular for-loop was, indeed, the easiest solution.

MWE here: https://code.earthengine.google.com/cf1d2e6e8c6375d33c48f0a121293bb0

(Though, I'm still surprised GEE doesn't have a built-in "makegrid" function...)

Grant
  • 141
  • 1
  • 6