Elasticsearch: Painless GeoPoint arcDistance

Created on 19 Jul 2017  路  9Comments  路  Source: elastic/elasticsearch

It will be really confortable to allow call to arcDistance from GeoPoint. Indeed actually we can only call them from GeoPoints (and that will use only the first point).

:AnalyticGeo >bug

Most helpful comment

We are now doing something like this to compute the minimum distance to nested children. Note that you will need to enable include_in_root for the nesting. Enjoy.

double dist=Double.MAX_VALUE; 
for (int i=0; i<doc['venues.location'].values.length; i++) { 
    double lat1 = doc['venues.location'][i].lat;
    double lon1 = doc['venues.location'][i].lon;
    double lat2 = 77.096080;
    double lon2 = 28.51818;
    double TO_METERS = 6371008.7714D;
    double TO_RADIANS = Math.PI / 180D;
    double x1 = lat1 * TO_RADIANS;
    double x2 = lat2 * TO_RADIANS;
    double h1 = 1 - Math.cos(x1 - x2);
    double h2 = 1 - Math.cos((lon1 - lon2) * TO_RADIANS);
    double h = h1 + Math.cos(x1) * Math.cos(x2) * h2;
    double cdist = TO_METERS * 2 * Math.asin(Math.min(1, Math.sqrt(h * 0.5)));
    dist = Math.min(dist, cdist);
}
return dist;

PS: Not proud of this but it works.

All 9 comments

@bport Can you please add more information/background to the topic? I can't quite participate in the conversation with the limited information that you are providing.

@yeikel In a sorting script I need to use the arcdistance for each location in a GeoPoints list.
However even if I can iterate this list I can use arcDistance on each GeoPoint because arcDistance is a GeoPoints method. And the implementation return the arcDistance for the 1st point.
So in order to allow script to get arcDistance for each point I think it can be a good idea to move arcDistance implementation to GeoPoint class.

@yeikel if you need extra info let me know

Discussed in FixitFriday. It is indeed problematic that it only applies to the first geo-point. We might not want to put them on GeoPoint in order to keep that class lightweight but there is also a possibility of having a whitelisted utility class for such methods.

@jpountz Ok thanks for taking care of it, if I can help in any way let me know

Also very interested in this.

We are now doing something like this to compute the minimum distance to nested children. Note that you will need to enable include_in_root for the nesting. Enjoy.

double dist=Double.MAX_VALUE; 
for (int i=0; i<doc['venues.location'].values.length; i++) { 
    double lat1 = doc['venues.location'][i].lat;
    double lon1 = doc['venues.location'][i].lon;
    double lat2 = 77.096080;
    double lon2 = 28.51818;
    double TO_METERS = 6371008.7714D;
    double TO_RADIANS = Math.PI / 180D;
    double x1 = lat1 * TO_RADIANS;
    double x2 = lat2 * TO_RADIANS;
    double h1 = 1 - Math.cos(x1 - x2);
    double h2 = 1 - Math.cos((lon1 - lon2) * TO_RADIANS);
    double h = h1 + Math.cos(x1) * Math.cos(x2) * h2;
    double cdist = TO_METERS * 2 * Math.asin(Math.min(1, Math.sqrt(h * 0.5)));
    dist = Math.min(dist, cdist);
}
return dist;

PS: Not proud of this but it works.

Superseded by #24946

Was this page helpful?
0 / 5 - 0 ratings

Related issues

clintongormley picture clintongormley  路  3Comments

ttaranov picture ttaranov  路  3Comments

malpani picture malpani  路  3Comments

dawi picture dawi  路  3Comments

jasontedor picture jasontedor  路  3Comments