"Python learning" specifies the distance calculation for two-point geographic latitude and longitude

Source: Internet
Author: User
Tags cos sin

Specify the distance calculation for latitude and longitude of two-point location

1 #Coding=utf-82 3  fromMathImport*4 5 #input lat_a latitude A6 #input lng_a Longitude A7 #input Lat_b latitude B8 #input Lng_b Longitude B9 #output distance distance (km)Ten defcalcdistance (lat_a, Lng_a, Lat_b, Lng_b): OneRA = 6378.140#Equatorial radius (km) ARB = 6356.755#Polar radius (km) -Flatten = (RA-RB)/RA#Earth Flat Rate -Rad_lat_a =radians (lat_a) theRad_lng_a =radians (lng_a) -Rad_lat_b =radians (lat_b) -Rad_lng_b =radians (Lng_b) -PA = Atan (Rb/ra *Tan (rad_lat_a)) +PB = Atan (Rb/ra *Tan (rad_lat_b)) -xx = ACOs (sin (PA) * sin (pb) + cos (PA) * cos (PB) * cos (RAD_LNG_A-rad_lng_b)) +C1 = (sin (xx)-XX) * (sin (pA) + sin (pB)) * * * 2/COS (XX/2) * * 2 AC2 = (sin (xx) + xx) * (sin (pA)-sin (pB)) * * * 2/sin (XX/2) * * 2 atDr = Flatten/8 * (C1-C2) -Distance = RA * (xx +DR) -     returnDistance -  -lat_a=32.060255; lng_a=118.796877#Nanjing -lat_b=39.904211; lng_b=116.407395#Beijing inDistance=calcdistance (Lat_a,lng_a,lat_b,lng_b) - Print('(lat_a, lng_a) = ({0:10.3f},{1:10.3f})'. Format (lat_a,lng_a)) to Print('(Lat_b, Lng_b) = ({0:10.3f},{1:10.3f})'. Format (lat_b,lng_b)) + Print('distance={0:10.3f} miles'. Format (distance))

Execution Result:

(lat_a, lng_a) = (    32.060,   118.797) (Lat_b, lng_b) = (    39.904,   116.407) Distance=   896.533 km

"Python learning" specifies the distance calculation for two-point geographic latitude and longitude

Related Article

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.