简体   繁体   中英

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.

Using ElasticSearch 7.11.1 with Java API, to make a geospatial query, I should use a

    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:

org.locationtech.jts.geom.Geometry

Obviously, I can't cast JTS Geometry to ElasticSearch Geometry, but I should convert the instance is somw way.

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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