0

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.

2

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

  • great package gena! do you have any kind of reference? I use to include in my modules a variable called help which is a JS object that holds the reference for the module's functions – Rodrigo E. Principe Mar 28 '18 at 22:30
  • 2
    No docs yet, I hope to add something once EE will support JSDoc. For now, the best reference is to check sources and/or tests, which I provide in the same repository as separate files with <package>-test-<feature> suffix. – Gennadii Donchyts Mar 28 '18 at 23:46
  • How would one check the sources? Importing and then using the print statement? – Kersten Mar 29 '18 at 6:28
  • Currently, the sources are pulled to the client once require() is called, you can see them by hitting F12 > Network (hacky). But I've also provided the link to the EE repository, or you can just clone it using git: earthengine.googlesource.com/users/gena/packages – Gennadii Donchyts Mar 29 '18 at 7:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.