1

I'm new to GEE and and having problems to create an if statement with .contains().

Using I let the user create a point in the map using onClick. I have a polygon layer with fire areas. If the point is within the polygon I want to change a text in my panel probably using text.set({value: 'evacuate'}) and if not, nothing changes.

I've been trying to use the ee.Algorithms.If() but doesn't really know how it works. I probably don't know how the .contains() works either.

My code:

 var text_containsfire = ee.Algorithms.If(closest_fires.contains(point), title2.set({value:'Evacuate'}), print('check if false'));
 print('activate', text_containsfire);

closest_brandomrade is the fire polygon as .geometry().

point generated from onClick.

2

I think you are in the correct way, but the returning result of the if statement should be of the same type. For the true case you are setting a value of a dictionary, and for the false case you are printing a value (not allowed since print does not return anything). Here is an example:

var geometry = ee.Geometry.Polygon(
    [[[-72, -41],
      [-72, -44],
      [-70, -44],
      [-70, -42]]]);

var geometry2 = ee.Geometry.Polygon(
        [[[-72, -42],
          [-72, -42.5],
          [-69, -42.5],
          [-69, -42]]]);

Map.addLayer(geometry)
Map.addLayer(geometry2)

// Callback function for onClick Map method
var callback = function(coords) {
  // Output dict
  var output = ee.Dictionary({message:'Safe zone'})

  // Create point from coords
  var point = ee.Geometry.Point([coords.lon, coords.lat])

  // conditions (geometry contains point?)
  var condition1 = geometry.contains(point)
  var condition2 = geometry2.contains(point)

  // Create a message using a conditional if statement
  // condition 1
  output = ee.Dictionary(ee.Algorithms.If(condition1, output.set('message', 'Evacuate'), output))
  // condition 2
  output = ee.Dictionary(ee.Algorithms.If(condition2, output, output.set('message', 'Dange area')))

  // Do whatever with the message
  print(output.get('message'))
}

Map.onClick(callback)

link: https://code.earthengine.google.com/9364940c5fe878e75ede9b5b98b2c6de

  • Great @Rodrigo. I got it working by using this code (See line 122-154): [link] (code.earthengine.google.com/229a30982079d7cb9a0e1768d403835b) However, I'm now struggling with the false statement. I have 4 different buffer zones and the last one is overwriting the previous ones. I get true on zone 1 which should add its text but the zone 4 function is overwriting it with its false statement so that text is the output. Any solutions? – Felix Althén Bergman Mar 7 at 13:39
  • you could do something like this: code.earthengine.google.com/9364940c5fe878e75ede9b5b98b2c6de. If it suits you, tell me and I update the answer – Rodrigo E. Principe Mar 7 at 17:51
  • Thanks @Rodrigo that was just what I needed for it to work the way I wanted. I added the following code in order to update my panel with the output text (if it would be of interest for someone in the future) var text_string = ee.String(output.get('message')); var update_paneltitle = title2.set({value: text_string.getInfo()}); – Felix Althén Bergman Mar 8 at 12:02

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.