首页 > 解决方案 > 弹性:存储“FeatureCollection”类型的 GeoJSON,并进行查询

问题描述

我使用Elastic 7.x来存储这样的文档:

{
    "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...
    
}

试图为它创建一个映射,我使用:

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

首先 - 这是正确的映射方式吗?

其次,假设我想创建一个查询,从具有相交“区域”的弹性所有文档中提取。我使用这个查询:

{
  "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]
            ]]
        }
      }
    }
  }
}

唉,我得到一个错误:

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

那么如何存储“标准化”类型的 GeoJSONFeatureCollection并进行下降查询呢?

标签: elasticsearchgeojson

解决方案


ES 确实支持,但需要GeometryCollections进行一些预处理。

使用最小可重现索引设置:

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

提取features以符合 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]]]
      }
    ]
  }
}

请注意,它regions.geometries可以包含多个 geojson 特征,而不仅仅是多边形。

之后,我们可以查询多边形相交:

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]]]
        }
      }
    }
  }
}

注意:您的索引多边形位于安克雷奇,AK,而查询中的多边形位于尼日利亚东部。两者不会重叠:)

所以我上面的查询测试了鹰河周围的紫色多边形: 在此处输入图像描述


推荐阅读