0

I am trying to extract column and row information from a raster in Google Earth Engine, but am having some trouble doing so. Here is sample code.

var ROI = ee.Geometry.Rectangle(-82.56277, 35.58935,-82.53436, 35.59996); // define area

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

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

Map.centerObject(ROI);
Map.addLayer(image,{},"image");
print(image.arrayLength(image,0));

However, image.arrayLength(image,0) returns the error message

Too many (3) arguments to function: Image.arrayLength(input, axis)

Returns the length of each pixel's array along the given axis.

Args:

  input (Image): Input image.
  axis (Integer): The axis along which to take the length.

I have tried to enter image.arrayLength(0) as well, but that returns the whole image. According to the arrayLength help page, it seems like it should return an integer with the number of pixels in the specified axis.

The information is contained if you drill down into the image object (Image->bands->0->dimensions->0 or Image->bands->0->dimensions->1), and the object appears to be a json object, but I can't figure out how to access this information.

  • 1
    The question is not clear to me. How do you expect the results to be? in a table? csv table? – Rodrigo E. Principe Mar 16 '18 at 20:35
  • Edited the question to specify that, according to the arrayLength help page, it should return an integer with the number of pixels in the specified axis. – user44796 Mar 16 '18 at 20:48
1

I modified your code to print the dimensions of a specified region (ROI).

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);

print('image information', image);

Note that:

  • Because image collections may contain images with many different projections, you need to specify the projection of your desired output in order to get back meaningful dimensions. In the example above I used the projection information from the first band of a sample image.
  • Bands within an image may have different projections. (This is the case for Landsat 8 images.)
  • I made the ROI smaller, so you can see the individual pixels when added to the map.
  • I limited the collection to a single image that intersects the ROI, so it is easier to interpret the results.

If you want to access the information on the client side, you can do the following:

// 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);

While it is possible to construct images in Earth Engine that store pixel information as multi-dimensional arrays, most images (including the Landsat collection shown in your example) have scalar values (i.e. 0-dimensional arrays) so it is not necessary to use ee.Array object methods to extract information.

  • How can I get the row or column value as a scalar? In your example, .dimensions returns "4,3". How can I convert this to a scalar so that I can use the row or column values in downstream processes? – user44796 Mar 22 '18 at 14:13
  • var dim0 = image_info.bands[0].dimensions[0]; var dim1 = image_info.bands[0].dimensions[1]; – Tyler Erickson Mar 22 '18 at 16:43
  • That was just what I was looking for. For my own edification, is it correct that the function .getInfo() extracts information from image and stores it in a dictionary? Is this the correct way to think about it? – user44796 Mar 28 '18 at 14:20

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.