1

I would like to set a condition based on the type of selected geometry. However, applying the following code seems to give wrong results.

var geometryCollection = /* color: #008000 */ee.Geometry({
      "type": "GeometryCollection",
      "geometries": [
        {
          "type": "Polygon",
          "coordinates": [
            [
              [
                51.50311161691894,
                25.26703875176188
              ],
              [
                51.50427033121338,
                25.26502063795976
              ],
              [
                51.50637318308105,
                25.266146128641104
              ]
            ]
          ],
          "geodesic": true,
          "evenOdd": true
        },
        {
          "type": "Point",
          "coordinates": [
            51.50813271219482,
            25.265486359507225
          ]
        },
        {
          "type": "Point",
          "coordinates": [
            51.484723043408216,
            25.281714956774724
          ]
        }
      ],
      "coordinates": []
    });
var drawingTools = Map.drawingTools();

var geometry = drawingTools.layers().get(0).geometries().get(0);

print(geometry.type());

print(geometry.type() === 'Polygon');
1

Your code give wrong results because drawingTools has not any layer. So, I took your geometries (polygon, p1, p2) and put them separately in GEE Map Canvas; as it can be observed in following image.

enter image description here

For creating Multi Geometry (Geometry Collection), I used 'addLayer' method as follows.

var geometry = drawingTools.addLayer([polygon, p1, p2], 'geometryCollection', 'blue', true, true);

and it was printed as expected (similar to your collection):

var geometryCollection = 
    /* color: #0000ff */
    /* locked: true */
    ee.Geometry({
      "type": "GeometryCollection",
      "geometries": [
        {
          "type": "Polygon",
          "coordinates": [
            [
              [
                51.50311161691894,
                25.26703875176188
              ],
              [
                51.50427033121338,
                25.26502063795976
              ],
              [
                51.50637318308105,
                25.266146128641104
              ]
            ]
          ],
          "geodesic": false,
          "evenOdd": true
        },
        {
          "type": "Point",
          "coordinates": [
            51.50637318308105,
            25.266146128641104
          ]
        },
        {
          "type": "Point",
          "coordinates": [
            51.484723043408216,
            25.281714956774724
          ]
        }
      ],
      "coordinates": []
    });

So, complete code for retrieving geometryCollection and type comprobation looks as follows:

var polygon = ee.Geometry.Polygon(
        [[[51.50311161691894, 25.26703875176188],
          [51.50427033121338, 25.26502063795976],
          [51.50637318308105, 25.266146128641104],]], null, false);

var p1 = ee.Geometry.Point(51.50637318308105, 25.266146128641104);
var p2 = ee.Geometry.Point(51.484723043408216, 25.281714956774724);

Map.centerObject(ee.Geometry.Point(51.49418, 25.27387), 15);
Map.addLayer(polygon, {color: 'red'}, 'polygon');
Map.addLayer(p1, {color: 'green'}, 'p1');
Map.addLayer(p2, {color: 'blue'}, 'p2');

var drawingTools = Map.drawingTools();

var geometry = drawingTools.addLayer([polygon, p1, p2], 'geometryCollection', 'blue', true, true);

print("type", geometry.geometries().get(0).type());

var test = ee.String(geometry.geometries().get(0).type());

print("type comprobation", ee.Algorithms.If(ee.String(test).compareTo('Polygon').eq(0), 
      'Polygon', 
      'not a Polygon'));

After running above code in GEE code editor, it was added Geometry Collection with its respective selected name (geometryCollection) and printed in Console Tab the checking of polygon selected geometry (index 0 in list of geometries); as it can be observed in following image.

enter image description here

2
  • '''ee.Algorithms.If(ee.String(test).compareTo('Polygon').eq(0)''' >>> what does ".eq(0)" exactly do? Directly writing '''ee.Algorithms.If(ee.String(test).compareTo('Polygon')''' seems to give wrong results
    – farhat
    Nov 14 '21 at 14:43
  • I just tested the code. There is no need to add a layer to drawingTools an additional time. Adding ''.eq(0)'' seems to have solved the issue. If you can edit your response, I ll accept it.
    – farhat
    Nov 14 '21 at 14:50
0

The complete right answer is to use: ee.Algorithms.If() & .getInfo(). No need to add geometry layers a 2nd time.

The updated code:

var drawingTools = Map.drawingTools();
var geometries = drawingTools.layers().get(0).geometries();
var geometry = geometries.get(1);

var result = ee.String(ee.Algorithms.If(geometry.type().compareTo('Polygon').eq(0), 't', 'f')).getInfo()
print(result)

thanks to @xunilk for pointing me in the right direction

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.