0

I am working with amazon neptune and using openCypher query language and doing some stuff using notebook. I have latitude and longitude of 2 points and I want to calculate the distance between these 2 points. For the purpose I am using Haversine formula. However to do this it required trigonometric functions like sin and cos and seems that those are not supported yet with neptune and openCypher. I am stuck here as I will be heavily doing operations on location. Any alternate solution without using sin cos ?

Vishal Jamdade
  • 182
  • 1
  • 2
  • 17

1 Answers1

1

Until the trigonometric functions are added to the openCypher support provided by Amazon Neptune (which hopefully will appear fairly soon), you could do this particular calculation using Gremlin. An example query is available here. In summary, it looks like this (assuming both the start and stop vertices have properties called lat and lon providing the coordinates in decimal form):

g.withSideEffect("rdeg", 0.017453293).
  withSideEffect("gcmiles",3956).
  V().has('code',start).as('src').
  V().has('code',stop).as('dst').
  select('src','dst').
    by(project('lat','lon').
         by('lat').
         by('lon')).
  as('grp').
  project('ladiff','lgdiff','lat1','lon1','lat2','lon2').
    by(project('la1','la2').
         by(select('grp').select('src').select('lat')).
         by(select('grp').select('dst').select('lat')).
       math('(la2 - la1) * rdeg')).
    by(project('lg1','lg2').
         by(select('grp').select('src').select('lon')).
         by(select('grp').select('dst').select('lon')).
       math('(lg2 - lg1) * rdeg')).
    by(select('grp').select('src').select('lat')).
    by(select('grp').select('src').select('lon')).
    by(select('grp').select('dst').select('lat')).
    by(select('grp').select('dst').select('lon')).
  math('(sin(ladiff/2))^2 + cos(lat1*rdeg) * cos(lat2*rdeg) * (sin(lgdiff/2))^2').
  math('gcmiles * (2 * asin(sqrt(_)))')
Kelvin Lawrence
  • 14,674
  • 2
  • 16
  • 38