TransWikia.com

How to implement the 'contain' spatial join in Google Earth Engine?

Geographic Information Systems Asked on September 5, 2021

How to select polygons which are contained in another polygon in Google Earth Engine? For example, in this case below, how to select only the red square completely within the green square, excluding the red square intersected?enter image description here

As far as I know, the functions, .filterBounds() or the combination of ee.Join.saveAll() and ee.Filter.withinDistance() , are utilized to conduct the spatial query of ‘Intersection’, not ‘Contained’.

One Answer

I find a solution: ee.Filter.contains() or ee.Filter.isContained().

var polys = 
    /* color: #0b4a8b */
    /* displayProperties: [
      {
        "type": "rectangle"
      },
      {
        "type": "rectangle"
      },
      {
        "type": "rectangle"
      }
    ] */
    ee.FeatureCollection(
        [ee.Feature(
            ee.Geometry.Polygon(
                [[[-110.20732421875, 44.02023306138536],
                  [-110.20732421875, 43.464640103242],
                  [-109.3943359375, 43.464640103242],
                  [-109.3943359375, 44.02023306138536]]], null, false),
            {
              "landcover": 0,
              "system:index": "0"
            }),
        ee.Feature(
            ee.Geometry.Polygon(
                [[[-108.84501953125, 43.480585732644144],
                  [-108.84501953125, 42.88779655005588],
                  [-107.944140625, 42.88779655005588],
                  [-107.944140625, 43.480585732644144]]], null, false),
            {
              "landcover": 0,
              "system:index": "1"
            }),
        ee.Feature(
            ee.Geometry.Polygon(
                [[[-107.6365234375, 44.555012919342055],
                  [-107.6365234375, 43.941178353199525],
                  [-106.91142578125, 43.941178353199525],
                  [-106.91142578125, 44.555012919342055]]], null, false),
            {
              "landcover": 0,
              "system:index": "2"
            })]),
    givenGeometry= 
    /* color: #ffc82d */
    /* displayProperties: [
      {
        "type": "rectangle"
      }
    ] */
    ee.Feature(
        ee.Geometry.Polygon(
            [[[-110.55888671875, 45.22437812507874],
              [-110.55888671875, 42.32175585200791],
              [-107.30693359375, 42.32175585200791],
              [-107.30693359375, 45.22437812507874]]], null, false),
        {
          "id": 0,
          "system:index": "0"
        });

var containsFilter = ee.Filter.contains({
  leftField: '.geo',
  rightField: '.geo',
  maxError: 1
});

// Define a saveAll join.
var distSaveAll = ee.Join.saveAll({
  matchesKey: 'polygons',
});

// Apply the join.
var spatialJoined = distSaveAll.apply(givenGeometry, polys, containsFilter);
var listOfElements = spatialJoined.first().get('polygons')
var result = ee.FeatureCollection(listOfElements.getInfo());
print('result', result);

Answered by Nick Carraway on September 5, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP