Search code examples
javascriptgoogle-earth-engine

How can I get the length of each element in a LinearRing in Google Earth Engine


I am using JavaScript in Google Earth Engine (very new to both of these) to get the lengths of each side of a building (looking down from map view)

First, I drew a LinearRing around the perimeter of the building, which is stored in the variable rectangle. I would like to extract the lengths of each of the lines in rectangle and print all of them.

The code I have below so far:

var rectangle = /* color: ffffff */ee.Geometry.LinearRing(
    [[coord_1],
     [coord_2],
     [coord_3],
     [coord_4],
     [coord_1]]);

var geometries = rectangle.geometries();
var line1 = geometries.get(0); 
var line2 = geometries.get(1);
print(line2)

This gives me an error of:

ComputedObject (Error)
List.get: List index must be between -1 and 0.  Found 1.

The converted JS code in GEE for var rectangle looks like this for reference:

var rectangle: LinearRing, 5 vertices
  type: LinearRing
  coordinates: List (5 elements)
    0: [coord_1]
    1: [coord_2]
    2: [coord_3]
    3: [coord_4]
    4: [coord_1]

Thanks!


Solution

  • Try calling .coordinates() rather than .geometries() on your rectangle.

    If you had a FeatureCollection, .geometries() would get you the geometries of all the features in that collection, while .coordinates() returns the coordinates of a single Geometry.