0

Friends, I am working on something to find out distance between two latitude and longitude points.

Below is my python code, to get the distance in KM

from math import radians, cos, sin, asin, sqrt
def distance(lat1, lng1, lat2, lng2):     
    R = 6371  # radius of the earth in km
    lng1, lat1, lng2, lat2 = map(radians, [lng1, lat1, lng2, lat2])
    x = (lng2 - lng1) * cos( 0.5*(lat2+lat1) )
    y = lat2 - lat1
    d = R * sqrt( x*x + y*y )
    return d

distance(25.42684996,81.84835984,25.45328992,81.86906992)
**Out[77]: 3.6011256577543027**

The answer is 3.60.. km. But when i cross check in google maps it says 609 km. Can any one suggest what is the mistake here?

i referred HERE and other links too

Its absolutely fine, if you have "R" - code !!!

Also, is there any way that i can find out shortest route that google proposes, in python?

Roland
  • 127,288
  • 10
  • 191
  • 288
Adarsha Murthy
  • 145
  • 3
  • 13
  • Are you sure you did the Google Maps check correctly? Those points don't look that far apart to me. – Fred Larson Apr 11 '18 at 13:52
  • Only look at your lat1, lng1, lat2, lng2. In my opinion 609km it's the wrong answer and 3,6km it's the right answer. – J0ki Apr 11 '18 at 13:55
  • Your formula seems wrong to me, you're missing tans and operations, look at Haversine formula to calculate distance between 2 coordinates – Jean Rostan Apr 11 '18 at 13:56
  • Google Maps gives me 4,5km walking distance between the two points. – Psytho Apr 11 '18 at 13:58
  • Use the Google Maps API for your second question. – tehhowch Apr 11 '18 at 14:04
  • It's 3.59km, measured in ArcMap, so not sure where 609 km came from. – Anonymous coward Apr 11 '18 at 14:08
  • looks like am still not able to get the solution.. this time i am searching by city name directly (from Mutthi Ganj to Anjana Uttar Pradesh, India) https://www.google.co.in/search?ei=dR3OWpiAIoSUvQTHu7jQAg&q=Mutthi+Ganj+to+Patel+Churaha+Bus+Stop+distance&oq=Mutthi+Ganj+to+Patel+Churaha+Bus+Stop+distance&gs_l=psy-ab.3...55634.57557.0.57776.2.2.0.0.0.0.334.526.0j1j0j1.2.0....0...1c.1.64.psy-ab..0.0.0....0.wEO13C0pPBo for the co-ordinates great_circle_distance((25.42684996,81.84835984),(25.37993,82.1407999)) i get 29.00 km... in python.. but above link shows 162.6 km.. – Adarsha Murthy Apr 11 '18 at 14:47

1 Answers1

1

Try this out:

from math import *

def greatCircleDistance((lat1, lon1), (lat2, lon2)):
  def haversin(x):
    return sin(x/2)**2 
  return 2 * asin(sqrt(
      haversin(lat2-lat1) +
      cos(lat1) * cos(lat2) * haversin(lon2-lon1)))

This returns the angular distance between the points on a sphere. To get a distance in length (kilometers), multiply the result with the Earth radius.

Hari
  • 334
  • 4
  • 16
  • Hi Hari.. thanks for the solution.. i found similar to you solution here https://gist.github.com/gabesmed/1826175 ,, as your code throwing error.. may be "x" substitution not happening.. correct me if am wrong.. its 3.69 km only.. – Adarsha Murthy Apr 11 '18 at 14:33