参考文章:

Elasticsearch地理形状
Elasticsearch geo_shape地理形状
ES地理范围查询第二讲:地理位置信息之geo_shape
ES GEO地理空间查询java版
Elasticsearch geo_point/geo_shape

一、概述

通常情况,我们使用一个经纬度坐标表示一个店铺的位置、一个用户的位置,经纬度在地图上仅仅表示一个点,有时候需要表示一个区域,例如:停车场、商场、学校等等,这些区域拥有各种各样的形状,包括:圆形、多边形等等。

ES中存储地理形状的数据类型为: geo_shape

geo_shape支持存储的常用形状数据如下:

  • 点(point)
  • 圆形(circle)
  • 矩形(envelope)
  • 多边形 (polygon)

提示: 在geo_shape中,点作为一种特殊的形状,geo_shape可以存储一个点。

二、geo_shape数据格式

geo_shape支持GeoJsonWKT(Well-Known Text)格式存储空间形状数据。建议使用wkt。详细支持的格式描述如下面图片上描述。
在这里插入图片描述

格式说明

  • GeoJson格式数据
    GeoJson格式参考官方网站:https://geojson.org/
    在es中则只需要存储其geometry的属性值即为geo_shape的值。
{
  "type": "Feature",
  "geometry": {
    "type": "Point",
    "coordinates": [125.6, 10.1]
  },
  "properties": {
    "name": "Dinagat Islands"
  }
}
  • wkt Well-Known Text (WKT)
POINT (-77.03653 38.897676) 
LINESTRING (-77.03653 38.897676,-77.009051 38.889939) 
POLYGON ((100.0 0.0, 101.0 0.0, 101.0 1.0, 100.0 1.0, 100.0 0.0)) 
MULTIPOINT (102.0 2.0, 103.0 2.0) 
MULTILINESTRING ((102.0 2.0, 103.0 2.0, 103.0 3.0, 102.0 3.0),(100.2 0.2, 100.8 0.2, 100.8 0.8, 100.2 0.8)) 
MULTIPOLYGON (((102.0 2.0, 103.0 2.0, 103.0 3.0, 102.0 3.0, 102.0 2.0)), ((100.0 0.0, 101.0 0.0, 101.0 1.0, 100.0 1.0, 100.0 0.0), (100.2 0.2, 100.8 0.2, 100.8 0.8, 100.2 0.8, 100.2 0.2))) 
GEOMETRYCOLLECTION (POINT (100.0 0.0), LINESTRING (101.0 0.0, 102.0 1.0)) 
BBOX (100.0, 102.0, 2.0, 0.0)

存储示例

  1. 定义geo_shape类型映射
PUT /example
{
    "mappings": {
        "properties": {
            "location": {
                "type": "geo_shape" // 定义location字段类型为geo_shape
            }
        }
    }
}
  1. 存储一个点
POST /example/_doc
{
    "location" : {
        "type" : "point", // 存储的图形类型为:point,表示存储一个坐标点
        "coordinates" : [-77.03653, 38.897676] // 坐标点格式: [经度, 纬度]
    }
}

POST /example/_doc
{
    "location" : "POINT (-77.03653 38.897676)"
}

  1. 存储一个多边形
POST /example/_doc
{
  "location": {
    "type": "polygon", // 存储的图形类型为: polygon,表示一个多边形
    "coordinates": [ // 支持多个多边形
      [ // 第一个多边形,多边形由下面的坐标数组组成。
        [100, 0], // 第一个坐标点,坐标格式: [经度, 纬度]
        [101, 0],
        [101, 1],
        [100, 1],
        [100, 0] // 最后一个坐标点,要跟第一个坐标点相同,这样多边形才能形成闭合
      ]
    ]
  }
}

POST /example/_doc
{
    "location" : "POLYGON ((100.0 0.0, 101.0 0.0, 101.0 1.0, 100.0 1.0, 100.0 0.0))"
}

在这里插入图片描述

三、geo_shape地理形状搜索

当索引的字段类型定义为geo_shape之后,我们就可以通过geo_shape实现图形搜索。

1. 图形搜索类型

下面是geo_shape支持的图形搜索类型:

intersects - 查询的形状与索引的形状有重叠(默认), 即图形有交集则匹配。
disjoint - 查询的形状与索引的形状完全不重叠。
within - 查询的形状包含索引的形状。

2.图形搜索例子

GET /example/_search
{
    "query":{
        "bool": { // 布尔组合查询语句
            "must": {
                "match_all": {} // 这里设置其他查询条件,直接匹配全部文档
            },
            "filter": { // 地理信息搜索,通常不参与相关性计算,所以使用filter包裹起来
                "geo_shape": { // geo_shape搜索语句
                    "location": { // 图形数据存储在location字段
                    "relation": "within" // 设置图形搜索类型,这里设置为包含关系
                    "shape": {   //这个可以理解为其实就是geojson的geometry的值
                    	"type": "polygon",  // 设置图形类型,各种图形格式参考geo_shape的数据格式支持的图形类型
                    	"coordinates": [
		                        [
		                            [
		                                104.0396387972344,
		                                30.59613123035072
		                            ],
		                            [
		                                104.0393476378968,
		                                30.59549712177650
		                            ],
		                            [
		                                104.0396387858758,
		                                30.59638313574942
		                            ],
		                            [
		                                104.0396387972344,
		                                30.59613123035072
		                            ]
		                        ]
		                    ]
		                }
                    }
                }
            }
        }
    }
}

四、Java查询代码示例

GeoShape Polygon 查询

  1. 依赖jar包
<dependency>
    <groupId>org.locationtech.jts</groupId>
    <artifactId>jts-core</artifactId>
    <version>1.18.1</version>
</dependency>
<dependency>
    <groupId>org.locationtech.spatial4j</groupId>
    <artifactId>spatial4j</artifactId>
    <version>0.8</version>
</dependency>
  1. 核心代码示例
MultiPolygonBuilder multiPolygonBuilder = new MultiPolygonBuilder();
String shapeField = "";
for (MultiCondition.GeoPolygon geoPolygon : geoPolygons){
    List<Coordinate> coordinateList = geoPolygon.geoPoints.stream().map(point -> new Coordinate(point.getLon(),point.getLat())).collect(Collectors.toList());
    CoordinatesBuilder coordinates = new CoordinatesBuilder().coordinates(coordinateList);
    PolygonBuilder polygonBuilder = new PolygonBuilder(coordinates);
    multiPolygonBuilder.polygon(polygonBuilder);
    shapeField = geoPolygon.getShapeField();
}
try {
    GeoShapeQueryBuilder geoShapeQueryBuilder = QueryBuilders.geoShapeQuery(shapeField, multiPolygonBuilder.buildGeometry());
    queryBuilder.filter(geoShapeQueryBuilder);
} catch (IOException e) {
    e.printStackTrace();
}
  1. 代码示例
package cn.com.example.cdgisdatahandle.config;

import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.geo.ShapeRelation;
import org.elasticsearch.common.geo.builders.CoordinatesBuilder;
import org.elasticsearch.common.geo.builders.MultiPolygonBuilder;
import org.elasticsearch.common.geo.builders.PolygonBuilder;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.GeoShapeQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.MultiPolygon;
import org.locationtech.jts.geom.Polygon;
import org.locationtech.jts.io.WKTReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ElasticsearchDaoImpl {

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    @Autowired
    private DefConfig defConfig;

    public SearchResponse geoShapeQuery(String wkt) {

        String indexName = defConfig.getEs_realtimepeople_index_name();
        String geoShapeField = defConfig.getEs_realtimepeople_geoshape_field();
        String peopleNumField = defConfig.getEs_realtimepeople_peoplenum_field();

        SearchResponse response = null;
        SearchRequest request = new SearchRequest();
        request.indices(indexName);

        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.trackTotalHits(true);
        //查询条件
        BoolQueryBuilder boolQueryBuilder = buildWktBoolQueryBuilder(geoShapeField, wkt);
        boolQueryBuilder.must(QueryBuilders.matchAllQuery());
        boolQueryBuilder.must(QueryBuilders.matchQuery("name", "小明"));
        //聚合求和
        AggregationBuilder agg = AggregationBuilders.sum("aggSum").field(peopleNumField);
        try {
            searchSourceBuilder.query(boolQueryBuilder).aggregation(agg);
            request.source(searchSourceBuilder);
            response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return response;
    }

    /**
     * 构建Wkt条件
     * 参考文章:https://blog.csdn.net/z69183787/article/details/105563778
     *
     * @date 2019/9/26
     */
    private BoolQueryBuilder buildWktBoolQueryBuilder(String queryField, String wkt) {
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        WKTReader wktReader = new WKTReader();
        try {
            Geometry geom = wktReader.read(wkt);
            GeoShapeQueryBuilder geoShapeQueryBuilder = null;
            if (wkt.startsWith("POLYGON")) {
                Polygon polygon = (Polygon) geom;
                List<Coordinate> coordinateList = Arrays.stream(polygon.getCoordinates()).collect(Collectors.toList());
                CoordinatesBuilder coordinates = new CoordinatesBuilder().coordinates(coordinateList);
                PolygonBuilder polygonBuilder = new PolygonBuilder(coordinates);
                geoShapeQueryBuilder = QueryBuilders.geoShapeQuery(queryField, polygonBuilder.buildGeometry())
                        .relation(ShapeRelation.WITHIN);
                boolQueryBuilder.filter(geoShapeQueryBuilder);
            } else if (wkt.startsWith("MULTIPOLYGON")) {
                MultiPolygon multiPolygon = (MultiPolygon) geom;
                int num = multiPolygon.getNumGeometries();
                MultiPolygonBuilder multiPolygonBuilder = new MultiPolygonBuilder();
                for (int i = 0; i < num; i++) {
                    Polygon polygon = (Polygon) multiPolygon.getGeometryN(i);
                    List<Coordinate> coordinateList = Arrays.stream(polygon.getCoordinates()).collect(Collectors.toList());
                    CoordinatesBuilder coordinates = new CoordinatesBuilder().coordinates(coordinateList);
                    PolygonBuilder polygonBuilder = new PolygonBuilder(coordinates);
                    multiPolygonBuilder.polygon(polygonBuilder);
                }
                geoShapeQueryBuilder = QueryBuilders.geoShapeQuery(queryField, multiPolygonBuilder.buildGeometry())
                        .relation(ShapeRelation.WITHIN);
                boolQueryBuilder.filter(geoShapeQueryBuilder);
            }
        } catch (Exception e) {
            System.out.println("构建Wkt条件错误" + e.getMessage());
        }
        return boolQueryBuilder;
    }

    public static void main(String[] args) {
        ElasticsearchDaoImpl elasticsearchDao = new ElasticsearchDaoImpl();
        String wkt = "POLYGON((110.20111083984375 25.431803168948832,110.05897521972658 25.32075284544021,110.07476806640624 25.187233664941914,110.643310546875 25.1070518051296,110.62477111816405 25.230721136478365,110.55198669433594 25.446064798199416,110.35491943359375 25.353644304321108,110.21072387695312 25.299647958643703,110.17501831054689 25.312683764022793,110.20111083984375 25.431803168948832))";
        string wkt2 = "MULTIPOLYGON (((102.0 2.0, 103.0 2.0, 103.0 3.0, 102.0 3.0, 102.0 2.0)), ((100.0 0.0, 101.0 0.0, 101.0 1.0, 100.0 1.0, 100.0 0.0), (100.2 0.2, 100.8 0.2, 100.8 0.8, 100.2 0.8, 100.2 0.2)))";
        long startCurrentTimeMillis = System.currentTimeMillis();
        elasticsearchDao.geoShapeQuery(wkt);
		elasticsearchDao.geoShapeQuery(wkt2);
        long endCurrentTimeMillis = System.currentTimeMillis();
        System.out.println((endCurrentTimeMillis - startCurrentTimeMillis)/1000 + " s");
    }
}

MultiPolygonBuilder 构建两个多边形,外多边形,内多边形,求环形部分相交数据

Coordinate 结构
用一个数组表示 经纬度 坐标点:
[lon,lat]
一组坐标点放到一个数组来表示一个多边形:
[[lon,lat],[lon,lat], ... ]
一个多边形( polygon )形状可以包含多个多边形;第一个表示多边形的外轮廓,后续的多边形表示第一个多边形内部的空洞:
[
  [[lon,lat],[lon,lat], ... ],  # main polygon
  [[lon,lat],[lon,lat], ... ],  # hole in main polygon
  ...
]
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐