You Are Here:

Community: Developer Discussion Boards

#1 Old how to calculate distance (code please) - 2008-12-12, 15:59

Join Date: Dec 2008
Posts: 13
trymukesh
Offline
Registered User
Hello all,
i have latitude and longitude values for two different points. i want to calculate the distance between them.

Note: i have the formula and the sample program that works on python installed on my machine. But the code is not running on S60

Can someone send me the entire code of how to calculate distance between two points that runs on S60?
Reply With Quote

#2 Old Re: how to calculate distance (code please) - 2008-12-12, 16:17

Join Date: Feb 2008
Posts: 743
Location: Belo Horizonte, Brazil
Send a message via Skype™ to Rafael T.
Rafael T.'s Avatar
Rafael T.
Offline
Forum Nokia Champion
Quote:
Originally Posted by trymukesh View Post
Hello all,
i have latitude and longitude values for two different points. i want to calculate the distance between them.

Note: i have the formula and the sample program that works on python installed on my machine. But the code is not running on S60

Can someone send me the entire code of how to calculate distance between two points that runs on S60?
Well this depends on what unit you want this measuring. You want in meters, kilometers or what?


BR,

Rafael.
Reply With Quote

#3 Old Re: how to calculate distance (code please) - 2008-12-12, 16:20

Join Date: Dec 2008
Posts: 13
trymukesh
Offline
Registered User
Quote:
Originally Posted by Rafael T. View Post
Well this depends on what unit you want this measuring. You want in meters, kilometers or what?


BR,

Rafael.
Hi Rafael,

Yes, need the distance in either Kilometers or miles
Any help will be a life saver

warm regards,
Mukesh
Reply With Quote

#4 Old Re: how to calculate distance (code please) - 2008-12-12, 18:00

Join Date: May 2004
Posts: 524
Location: Tampere, Finland
jethro.fn's Avatar
jethro.fn
Offline
Forum Nokia Champion
As always, Wikipedia has a good article on the subject: Great-circle distance

From there you can see how to convert lat/long to radians:

Code:
import math

def coordinate_to_radians(deg, min = 0.0, sec = 0.0):
    '''Convert a geographical coordinate into radians.'''

    if deg < 0.0:
        sign = -1.0
        deg = -deg
    else:
        sign = 1.0

    return (sign * (deg + (min + sec / 60.0) / 60.0)) * math.pi / 180.0
... and how to calculate the great-circle distance between two points on a sphere:

Code:
def angular_distance(standpoint_lat, standpoint_long, forepoint_lat, forepoint_long):
    '''Calculate the circular angular distance of two points on a sphere.'''

    phi_s = standpoint_lat
    phi_f = forepoint_lat
    lambda_diff = standpoint_long - forepoint_long

    cos = math.cos
    sin = math.sin

    num = (cos(phi_f) * sin(lambda_diff)) ** 2.0 + (cos(phi_s) * sin(phi_f) - sin(phi_s) * cos(phi_f) * cos(lambda_diff)) ** 2.0
    denom = sin(phi_s) * sin(phi_f) + cos(phi_s) * cos(phi_f) * cos(lambda_diff)

    return math.atan2(math.sqrt(num), denom)
Then it's simply multiplication by Earths radius in whatever unit you wish:

Code:
def distance_between_coordinates_km(lat1, long1, lat2, long2):
    '''Calculate distance between two points on Earth in kilometers. lat and long are three-tuples (deg, min, sec).'''

    rad_lat1 = coordinate_to_radians(lat1[0], lat1[1], lat1[2])
    rad_long1 = coordinate_to_radians(long1[0], long1[1], long1[2])
    rad_lat2 = coordinate_to_radians(lat2[0], lat2[1], lat2[2])
    rad_long2 = coordinate_to_radians(long2[0], long2[1], long2[2])

    return angular_distance(rad_lat1, rad_long1, rad_lat2, rad_long2) * 6371.01  # km
You may use 0 for min or sec, if you have the coordinates in decimal format. Here's an example using the example values from the Wikipedia article:

  • Nashville International Airport (BNA) in Nashville, TN, USA: N 36°7.2', W 86°40.2'
  • Los Angeles International Airport (LAX) in Los Angeles, CA, USA: N 33°56.4', W 118°24.0'

Code:
>>> distance_between_coordinates_km((36, 7.2, 0), (-86, 40.2, 0), (33, 56.4, 0), (-118, 24.0, 0))
2886.4489734366994
>>>
Reply With Quote

#5 Old Re: how to calculate distance (code please) - 2008-12-12, 18:43

Join Date: Feb 2008
Posts: 2,542
Location: Bhavnagar, Gujarat, India
Send a message via Yahoo to gaba88 Send a message via Skype™ to gaba88
gaba88's Avatar
gaba88
Online
Forum Nokia Champion
Quote:
Originally Posted by trymukesh View Post
Hello all,
i have latitude and longitude values for two different points. i want to calculate the distance between them.

Note: i have the formula and the sample program that works on python installed on my machine. But the code is not running on S60

Can someone send me the entire code of how to calculate distance between two points that runs on S60?
Hello

Please have a search in google with the title haversine formula and try to port that in python.

Hope this helps you.
Enjoy pythoning
Gaba88


Gargi Das- http://gargidas.blogsot.com

Forum Nokia Python Wiki


Learn Python at http://mobapps.org/PyS60
Reply With Quote

#6 Old Re: how to calculate distance (code please) - 2008-12-12, 19:34

Join Date: Dec 2008
Posts: 13
trymukesh
Offline
Registered User
Quote:
Originally Posted by jethro.fn View Post
As always, Wikipedia has a good article on the subject: Great-circle distance

From there you can see how to convert lat/long to radians:

Code:
import math

def coordinate_to_radians(deg, min = 0.0, sec = 0.0):
    '''Convert a geographical coordinate into radians.'''

    if deg < 0.0:
        sign = -1.0
        deg = -deg
    else:
        sign = 1.0

    return (sign * (deg + (min + sec / 60.0) / 60.0)) * math.pi / 180.0
... and how to calculate the great-circle distance between two points on a sphere:

Code:
def angular_distance(standpoint_lat, standpoint_long, forepoint_lat, forepoint_long):
    '''Calculate the circular angular distance of two points on a sphere.'''

    phi_s = standpoint_lat
    phi_f = forepoint_lat
    lambda_diff = standpoint_long - forepoint_long

    cos = math.cos
    sin = math.sin

    num = (cos(phi_f) * sin(lambda_diff)) ** 2.0 + (cos(phi_s) * sin(phi_f) - sin(phi_s) * cos(phi_f) * cos(lambda_diff)) ** 2.0
    denom = sin(phi_s) * sin(phi_f) + cos(phi_s) * cos(phi_f) * cos(lambda_diff)

    return math.atan2(math.sqrt(num), denom)
Then it's simply multiplication by Earths radius in whatever unit you wish:

Code:
def distance_between_coordinates_km(lat1, long1, lat2, long2):
    '''Calculate distance between two points on Earth in kilometers. lat and long are three-tuples (deg, min, sec).'''

    rad_lat1 = coordinate_to_radians(lat1[0], lat1[1], lat1[2])
    rad_long1 = coordinate_to_radians(long1[0], long1[1], long1[2])
    rad_lat2 = coordinate_to_radians(lat2[0], lat2[1], lat2[2])
    rad_long2 = coordinate_to_radians(long2[0], long2[1], long2[2])

    return angular_distance(rad_lat1, rad_long1, rad_lat2, rad_long2) * 6371.01  # km
You may use 0 for min or sec, if you have the coordinates in decimal format. Here's an example using the example values from the Wikipedia article:

  • Nashville International Airport (BNA) in Nashville, TN, USA: N 36°7.2', W 86°40.2'
  • Los Angeles International Airport (LAX) in Los Angeles, CA, USA: N 33°56.4', W 118°24.0'

Code:
>>> distance_between_coordinates_km((36, 7.2, 0), (-86, 40.2, 0), (33, 56.4, 0), (-118, 24.0, 0))
2886.4489734366994
>>>
Hi Jethro,
You are a rare Gem.
Thank you, saved my day (can sleep peacefully now)
cheers,
Your Fan (Mukesh)
Reply With Quote
Reply « Previous Thread | Next Thread »
Display Modes
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules

You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump
Similar Threads
Thread Thread Starter Forum Replies Last Post
Tell me, which is the better code option? raj8nokiaforum General Symbian C++ 3 2008-10-21 23:36
Testing an Active Object? (With attached code from two attempts) nawkboy General Symbian C++ 2 2007-10-05 21:40
请哪位好心人告诉我几个 Navigation key 的keyCode的宏定义 daijuntang Symbian 9 2006-03-30 09:36
Link errors when trying to Display bitmap Bkc82 Symbian Media (Graphics & Sounds) 1 2006-01-16 23:46
Why is client code to CSocketEngine hanging at Access Point dialogue? nawkboy Symbian Networking & Messaging 2 2003-03-11 12:16

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia 
RDF Facets: qdcZidentifierQSxhttpE3aE2fE2fdiscussionE2eforumE2enokiaE2ecomhttpE3aE2fE2fdiscussionE2eforumE2enokiaE2ecomE2fforumE2fshowthreadE2ephpE3ftE3d81449X qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfntypeZCommunityContentQ qdcZtypeQUqfntypeZE44iscussionQ qdcZtypeQUqfntypeZE44iscussionContentQ qdcZtypeQUqfntypeZE52esourceQ qdcZtypeQUqfntypeZWebpageQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qfnZtopicQUqfnTopicZpythonQ qfnZtypeQUqfntypeZCommunityContentQ qfnZtypeQUqfntypeZE44iscussionQ qfnZtypeQUqfntypeZE44iscussionContentQ qfnZtypeQUqfntypeZE52esourceQ qfnZtypeQUqfntypeZWebpageQ qfnZuserE5ftagQSxpythonX qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfntypeZCommunityContentQ qrdfZtypeQUqfntypeZE44iscussionQ qrdfZtypeQUqfntypeZE44iscussionContentQ qrdfZtypeQUqfntypeZE52esourceQ qrdfZtypeQUqfntypeZWebpageQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ