[Python learning] It is used to specify the distance between two geographical locations for latitude and longitude calculation, and the python latitude and longitude

Source: Internet
Author: User

[Python learning] It is used to specify the distance between two geographical locations for latitude and longitude calculation, and the python latitude and longitude

Distance Calculation of the longitude and latitude of a specified two-point geographic location

1 # coding = UTF-8 2 3 from math import * 4 5 # input Lat_A latitude A 6 # input Lng_A longitude A 7 # input Lat_ B latitude B 8 # input Lng_ B longitude B 9 # output distance distance (km) 10 def calcDistance (Lat_A, Lng_A, Lat_ B, Lng_ B): 11 ra = 6378.140 # equator radius (km) 12 rb = 6356.755 # Polar radius (km) 13 flatten = (ra-rb)/ra # Earth flat rate 14 rad_lat_A = radians (Lat_A) 15 rad_lng_A = radians (Lng_A) 16 rad_lat_ B = radians (Lat_ B) 17 rad_lng_ B = radians (Lng_ B) 18 pA = atan (rb/ra * tan (rad_lat_A) 19 pB = atan (rb/ra * tan (rad_lat_ B )) 20 xx = acos (sin (pA) * sin (pB) + cos (pA) * cos (pB) * cos (rad_lng_A-rad_lng_ B) 21 c1 = (sin (xx) -xx) * (sin (pA) + sin (pB) ** 2/cos (xx/2) ** 222 c2 = (sin (xx) + xx) * (sin (pA)-sin (pB) ** 2/sin (xx/2) ** 223 dr = flatten/8 * (c1-c2) 24 distance = ra * (xx + dr) 25 return distance26 27 Lat_A = 32.060255; Lng_A = 118.796877 # Nanjing 28 Lat_ B = 39.904211; Lng_ B = 116.407395 # Beijing 29 distance = calcDistance (Lat_A, lng_A, Lat_ B, Lng_ B) 30 print ('(Lat_A, Lng_A) = ({0: 10. 3f}, {:10. 3f })'. format (Lat_A, Lng_A) 31 print ('(Lat_ B, Lng_ B) = ({0: 10. 3f}, {:10. 3f })'. format (Lat_ B, Lng_ B) 32 print ('distance = {0: 10. 3f} km '. format (distance ))

Execution result:

(Lat_A, Lng_A)=(    32.060,   118.797)(Lat_B, Lng_B)=(    39.904,   116.407)Distance=   896.533 km

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.