Search geo-point with polygon across dateline?


(Zhang Dong) #1

How to search geo-point with polygon across dateline(International Date Line,or 180 and -180 longitude) use java api?

Hello Everyone:
I use ElasticSearch 2.1 and its java api, I want to search documents with polygon geo filter(the polygon is rectangle usually),but when the polygon across the dateline (International Date Line,or 180 and -180 longitude),it go wrong. For example:
My code:

BoolQueryBuilder mustQuery = QueryBuilders.boolQuery().must(QueryBuilders.matchAllQuery());
......
GeoPolygonQueryBuilder qb = QueryBuilders.geoPolygonQuery("description.device_location.es_geo");

qb.addPoint(0,100);//the left down vertex of the polygon(rectangle),patams is (lat , lon)
qb.addPoint(0,-170);//right down
qb.addPoint(80,-170);//right up
qb.addPoint(80,100);//left up
qb.addPoint(0,100);//left down,same to the first vertex

mustQuery = mustQuery.must(qb);
SearchResponse searchResponse = EsClient.getClient().prepareSearch(Config.indexName)
.setTypes(Config.typeName)
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setQuery(mustQuery)
.setFrom(0).setSize(size).setExplain(true)
.execute()
.actionGet();

diagrammatic sketch:

As the picture above, I provide ES points [0,100],[0,-170],[80,-170],[80,100],[0,100] to describe A area,and I want docs in A area, A area across dteline.
But according to the result, there is longitude 82,98,-121 etc., but no docs between [100,180]and between [-180,-170],so I suppose ES search in B area in fact(It analyze the polygon in error).

I search for the solution , on the ES's website, I found some words about this problem:
(form https://www.elastic.co/guide/en/elasticsearch/reference/2.1/geo-shape.html)

IMPORTANT NOTE: GeoJSON does not mandate a specific order for vertices thus ambiguous polygons around the dateline and poles are possible. To alleviate ambiguity the Open Geospatial Consortium (OGC) Simple Feature Access specification defines the following vertex ordering:

  • Outer Ring - Counterclockwise

  • Inner Ring(s) / Holes - Clockwise

For polygons that do not cross the dateline, vertex order will not matter in Elasticsearch. For polygons that do cross the dateline, Elasticsearch requires vertex ordering to comply with the OGC specification. Otherwise, an unintended polygon may be created and unexpected query/filter results will be returned.
The following provides an example of an ambiguous polygon. Elasticsearch will apply OGC standards to eliminate ambiguity resulting in a polygon that crosses the dateline.

But this is for geo-Shape datatype, and my docs' location is geo-point, I can't found similar words on geo-points's webpage(https://www.elastic.co/guide/en/elasticsearch/reference/2.1/geo-point.html).

I hava try some way:

  1. Counterclockwise and clockwise vertex order;
  2. start from every vertex of rectangle;
  3. replace -170 lon with 190 lon;
    but these ways all don't work.

Does anyone know how to search with a polygon across dateline ?
Thanks!
(sorry, I'm a Chinese developer and I can't speak English well, if there is solecism,please give me advice or comments, thank you.)

Translation of Chinese:
中文译文:
标题:怎样使用跨过国际日期变更线(180度经线)的多边形进行地理搜索?
各位好:
我使用的是ElasticSearch 2.1,以及它的java api,我需要 使用多边形(一般为矩形)进行地理搜索 ,但是 当多边形跨过国际日期变更线 (180和-180度经线)时 出现错误 。举例如下:
我的代码:
【省略,详见上方代码】
示意图:
【省略,详见上方图片】
如上图,我将[0,100],[0,-170],[80,-170],[80,100],[0,100] 几个点传入ES,描述图中的A区域,我想要 搜索A区域内的文档,A区域跨过了国际日期变更线。
但是根据搜索结果,有经度为82、98、-121等值的文档,但是没有在[100,180]之间和[-180,-170]之间的文档,所以本人猜测是 ES实际是在B区域搜索(ES错误地解析了多边形)。

上网查找解决方案,在ES官方网站,找到了关于这个问题的如下语句:(来自https://www.elastic.co/guide/en/elasticsearch/reference/2.1/geo-shape.html
【详见上方的引用内容。大意为不跨日期变更线的的多边形定点顺序没有要求,跨过日期变更线的多边形顶点必须是逆时针的,多边形内的孔顶点必须是顺时针的】
但是这些内容是关于 geo-Shape 数据类型的,我使用的是geo-point数据类型。在geo-point的网页内没有找到类似内容(https://www.elastic.co/guide/en/elasticsearch/reference/2.1/geo-point.html)。

我试过以下方法:
1、使用逆时针和顺时针的顶点顺序;
2、从矩形的每个顶点开始;
3、使用190度经线代替-170度经线。
但是这些方法都无效。
是否有人知道怎样使用跨过国际日期变更线的多边形搜索?
谢谢!


(Nick Knize) #2

At the moment geo_polygon queries for geo_point types do not work across the dateline. You will either have to:

  1. manually split the polygon into 2 and combine them using a boolean AND
  2. reindex the geo_points as geo_shapes (recommend setting points_only = true) and using a geo_shape query.

But if you know the polygon is a rectangle, you can use a geo_bounding_box query which will properly handle the dateline (as in your example).

Note that dateline crossing will eventually be handled for you (likely in a 2.x release)


(Medcl) #3

@zhangdong92 目前es还不支持跨国际日期变更线的查询,不过你可以通过将查询拆分成2个子查询,将一个跨变更线的区域切分成2个不跨变更线区域,然后用bool查询进行AND合并,这样就能实现你的功能。或者重建数据,将geo_points重建为geo_shapes,并且建议设置points_only为true,再使用geo_shape查询。


(Zhang Dong) #4

Thank you very much! It's helpful for me! Before, I have suddenly think of the way 1(split polygon by dateline),and I decide to use tihs way if there is not direct solution.
Thakn you!


(Zhang Dong) #5

非常感谢!提这个问题,其实也有想确定ES是否支持跨国际日期变更线的多边形的查询。拆分的方法之前也想到过,现在看来要用这个方法了。
谢谢!


(system) #6