Google Earth Engine – Extracting Information from Image by Row and Column

google-earth-engine

I would like to iterate over an image in Google Earth Engine, but not sure how to reference the location in an image by row and column. Here is some example code:

var ROI = ee.Geometry.Rectangle(-82.5574, 35.5958, -82.5563, 35.59515); // define area

var collection = ee.ImageCollection('LANDSAT/LC08/C01/T1')
    .filterDate('2014-05-01', '2015-10-01')
    .filterBounds(ROI)
    .limit(1);

var composite = ee.Algorithms.Landsat.simpleComposite(collection);
var image = composite.clip(ROI);

Map.centerObject(ROI);
Map.addLayer(composite, {bands: 'B4,B3,B2', min:0, max:100}, "composite", false);
Map.addLayer(image, {bands: 'B4,B3,B2', min:0, max:100},"image");

// Extract projection information from the first band of a sample image in the collection.
var sample = ee.Image(collection.first());
var projection = sample.select('B1').projection();

// Reproject the composite image to the selected projection.
image = composite.reproject(projection).clip(ROI);

// Return the image metadata to the client.
var image_info = image.getInfo();

// On the clientside, using Javascript, print out the dimensions.
print('band B1 dimensions: ' + image_info.bands[0].dimensions);

print('image information', image);

The resulting image is 3 by 4. How can I reference the values at, say, position [0,0] or [1,3]?

I have tried using regular javascript syntax image[0][0], to no avail.

Best Answer

Earth Engine API does not provide a very friendly API to reference image pixels directly because this is not needed for most applications. But I can imagine situations where analyzing images pixel by pixel in their native projection may be required. Here is a quick addition to your script showing how to get pixel center and values: https://code.earthengine.google.com/7417ead66dc131af4e3378f6672738f9. Currently, the script will fail for large images but can be used for smaller images like in your example.

var ImageView = require('users/gena/packages:image-view').ImageView

var view = new ImageView(image)

print('Image dimensions: ', view.getDimensions())

Map.addLayer(view.getPixelCenters(), {}, 'pixel centers')
Map.addLayer(view.getPixelCenter(0, 0), {color: 'red'}, '(0, 0)')
Map.addLayer(view.getPixelCenter(3, 2), {color: 'yellow'}, '(2, 3)')

print('(0, 0) pixel value: ', view.getPixelValue(0, 0))

Pixels are referenced by x and y indices.

The results look like:

Image dimensions: [4, 3]

(0, 0) pixel value: ... band values ...

enter image description here

Source: https://code.earthengine.google.com/?accept_repo=users/gena/packages