Search code examples
elasticsearchgeojson

Elastic: storing GeoJSONs of type `FeatureCollection`, and query


I work with Elastic 7.x to store documents like this:

{
    "id": "polygon_tests_01.ast-ORTHOMOSAIC",
    "name": "tmpl_integration_test_GeoTIFF",
    "region": {
        "type": "FeatureCollection",
        "features": [
            {
                "type": "Feature",
                "geometry": {
                    "coordinates": [[[-149.67474372431124,61.27942558978003],
                        [-149.65726554157862,60.993770332779064],
                        [-150.11544465434918,61.15680203118899],
                        [-149.87699170822603,61.28122531469481],
                        [-149.67474372431124,61.27942558978003]]],
                    "type": "Polygon"
                },
                "properties": {}
            }
        ],
        "type": "Feature",
        "properties": {}
    }
    ... more data...
    
}

Trying to create a mapping for it, i use:

{
    "mappings": {
        "properties": {
            "region": {
                "properties": {
                    "features": {
                        "properties": {
                            "geometry": {
                              "type": "geo_shape"
                            }
                        }
                    }
                }
            }
        }
    }
}

First - is this the correct way to map?

Second, suppose I want to create a query the fetches from elastic all documents with intersecting "regions". I use this query:

{
  "query": {
    "geo_shape": {
      "region.features.geometry": { 
        "relation": "intersects",
        "shape": {
          "type":  "polygon",
          "coordinates": [[[10.526270711323841,10.444489244321758],
                           [11.925063668547947,10.371171909552444],
                           [11.070002142972083,9.364612094349482],
                           [10.526270711323841,10.444489244321758]
            ]]
        }
      }
    }
  }
}

Alas, I get an error :

failed to find geo_shape field [region.features.geometry]

So how can I store "normalized" GeoJSONs of type FeatureCollection, and make a descent query?


Solution

  • ES does support GeometryCollections but a bit of pre-processing is required.

    Going with a minimum reproducible index setup:

    PUT geoindex
    {
      "mappings": {
        "properties": {
          "regions": {
            "type": "geo_shape"
          }
        }
      }
    }
    

    Extract the features to conform w/

    POST geoindex/_doc
    {
      "id": "polygon_tests_01.ast-ORTHOMOSAIC",
      "name": "tmpl_integration_test_GeoTIFF",
      
      "regions": {
        "type": "geometrycollection",
        "geometries": [
          {
            "type": "polygon",
            "coordinates": [[[-149.67474372431124,61.27942558978003],[-149.65726554157862,60.993770332779064],[-150.11544465434918,61.15680203118899],[-149.87699170822603,61.28122531469481],[-149.67474372431124,61.27942558978003]]]
          }
        ]
      }
    }
    

    Notice that regions.geometries can include multiple geojson features, not just polygons.

    After that, we can query for polygon intersections:

    POST geoindex/_search
    {
      "query": {
        "geo_shape": {
          "regions": { 
            "relation": "intersects",
            "shape": {
              "type":  "polygon",
              "coordinates": [[[-149.68734741210938,61.34276125480617],[-149.78347778320312,61.268912537559316],[-149.53628540039062,61.18628656437939],[-149.37286376953125,61.29662618671741],[-149.4085693359375,61.3671195097931],[-149.65164184570312,61.37962043716795],[-149.68734741210938,61.34276125480617]]]
            }
          }
        }
      }
    }
    

    Note: your indexed polygon is in Anchorage, AK, while the one in the query is in eastern Nigeria. The two ain't gonna overlap :)

    So my query above tests the purple polygon around Eagle River: enter image description here