I have stored a route in ElasticSearch
as a Polygon. Now I have a circle (A point and a radius), I'am able to check the circle points intersects the polygon or not (Below is the code I used).
Question: How can I get the points in the route which intersects the circle ?
public Boolean isMatchingDoc(Long elasticDocId, Double latitude, Double longitude, Long radius) {
Coordinate origin = new Coordinate(latitude, longitude);
ShapeBuilder circleShapeBuilder = ShapeBuilder.newCircleBuilder().center(origin).radius(radius,
DistanceUnit.METERS);
GeoShapeQueryBuilder geoShapeQueryBuilder = QueryBuilders.geoShapeQuery("route", circleShapeBuilder);
SearchRequestBuilder finalQuery = client.prepareSearch(INDEX).setTypes(TYPE)
.setQuery(QueryBuilders.termQuery("_id", elasticDocId)).setPostFilter(geoShapeQueryBuilder);
SearchResponse searchResponse = finalQuery.execute().actionGet();
SearchHits searchHits = searchResponse.getHits();
if (searchHits.getTotalHits() > 0) {
return true;
}
return false;
}
I guess you are aware that with elasticsearch, you can query for polygons that intersect a given circle? See https://www.elastic.co/guide/en/elasticsearch/guide/current/querying-geo-shapes.html.
There are two reasons why this may not help you:
- Your routes are not polygons, but lines.
- You want to know the exact points of the intersection, if I read your question correctly.
Elasticsearch probably cannot solve this problem for you conveniently. It might be possible to solve if you would store all of your line-segments separately instead of in one huge polygon per route. Each line-segment then would have to bear an attribute that references the route it belongs to. Does that approach sound feasible to you?
Anyways, I would recommend you to look into the topic of "spatial databases":
Spatial databases are optimized for indexing and searching in a geometric space. Well-know databases such as PostgreSQL and MongoDB feature plugins/extensions for spatial indexing. I'm not sure what to recommend, but the MongoDB geospatial API looks promising for example, as it allows querying for intersection - and it supports lines as well as polygons.