简体   繁体   English

将 JTS 几何转换为 Java 中的 ElasticSearch 几何

[英]Convert JTS Geometry to ElasticSearch Geometry in Java

I'm trying to find a way to convert a JTS Geometry to an ElasticSeach Geometry in order to make a geo query, but I didn't find a convenient way.我试图找到一种将 JTS 几何转换为 ElasticSeach 几何以进行地理查询的方法,但我没有找到方便的方法。

Using ElasticSearch 7.11.1 with Java API, to make a geospatial query, I should use a使用 ElasticSearch 7.11.1 和 Java API 进行地理空间查询,我应该使用

    GeoShapeQueryBuilder

returned by由返回

QueryBuilders.geoShapeQuery(String, org.elasticsearch.geometry.Geometry)

method.方法。

But, in my project, I'm using the JTS geometry (see https://en.wikipedia.org/wiki/JTS_Topology_Suite ), where a geometry is an instance of class:但是,在我的项目中,我使用的是 JTS 几何(参见https://en.wikipedia.org/wiki/JTS_Topology_Suite ),其中几何是 class 的一个实例:

org.locationtech.jts.geom.Geometry

Obviously, I can't cast JTS Geometry to ElasticSearch Geometry, but I should convert the instance is somw way.显然,我不能将 JTS 几何转换为 ElasticSearch 几何,但我应该以某种方式转换实例。

Has anyone encountered similar problem?有没有人遇到过类似的问题?
Thank you very much非常感谢

You can get coordinates from your jts.Geometry object and build whatever elasticsearch.Geometry you need eg for Polygon you would write something like this:您可以从 jts.Geometry object 获取坐标并构建您需要的任何 elasticsearch.Geometry 例如对于多边形,您可以编写如下内容:

val coordinates = geometry.coordinates
val mappedCoordinates = coordinates.map { Coordinate(it.x, it.y) }
val toPolygonGeometry = PolygonBuilder(CoordinatesBuilder().coordinates(mappedCoordinates)).toPolygonGeometry()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM