[GIS] Extract rows and columns from a raster in Google Earth Engine

googlegoogle-earth-engine

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.

Best Answer

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.