Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
809 views
in Technique[技术] by (71.8m points)

elasticsearch - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...