Reference resources : https://blog.csdn.net/Next_Second/article/details/78618081
pip install geopy
import json
from geopy import GoogleV3
from geopy.geocoders import Nominatim
from geopy import distance
class Geopy(object):
def location_geocode(self):
'''
Geocoding Query the coordinates and details according to the address
OpenStreetMap On the platform Nominatim Geocoder , No need to apply API , Current limiting , limit , No large-scale and frequent access
In my case : White whoring speed limit 、 Limit times Very very
:return:
'''
geolocator = Nominatim(user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.90 Safari/537.36") # discharge ua
location = geolocator.geocode(" Mount Qomolangma ") # According to the relevant information
location = geolocator.geocode(" Xierqi North Road, Haidian District, Beijing ")
print(location, location.address, location.latitude, location.longitude, location.altitude, location.point, location.raw)
def location_reverse(self):
"""
Reverse geocoding Check the location according to the coordinates
:return:
"""
geolocator = Nominatim(user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.90 Safari/537.36") # discharge ua
# location = geolocator.reverse("39.90733345 116.391244079988") # reverse Method receives a longitude and latitude string as input , Latitude is in the front , Longitude behind
location = geolocator.reverse("31.239853 121.499740") # reverse Method receives a longitude and latitude string as input , Latitude is in the front , Longitude behind
print(location, location.address, location.latitude, location.longitude, location.altitude, location.point, location.raw)
print(json.dumps(location.raw, indent=4, ensure_ascii=False, encoding='utf8'))
def location_distance(self):
"""
Calculate the distance between two longitude and latitude coordinates : The great circle algorithm simplifies the earth into a sphere , The calculated distance is the great circle distance between two points on the sphere
somewhat : The calculation speed of great circle distance is fast shortcoming : A certain error (0.5% within )
:return:
"""
dist = distance.distance((39.90733345,116.391244079988), (31.239853,121.499740)) # Latitude is in the front 、 Longitude behind
dist = distance.geodesic((45.768189, 126.6212835), (39.907359, 116.3912630)) # Equivalent
print(dist.km) # distance : km: km , m: rice , miles: miles
def location_great_circle(self):
"""
Calculate the spherical distance : The current international common method is used for the earth wire , The earth is represented by a rotating ellipsoid , It calculates the shortest distance between two points on the ellipsoid .
advantage : High accuracy shortcoming : The disadvantage of geodesic line is that the calculation speed is too slow
:return:
"""
gc = distance.great_circle((45.768189, 126.6212835), (39.907359, 116.3912630)) # Also return to distance object
print(gc.km) # distance : km: km , m: rice , miles: miles
# print(f'{(dist.km - gc.km) / dist.km:%}')
# The distance between the great circle from Harbin to Beijing and the geodesic line is 0.13% The difference of
def run(self):
# self.location_geocode() # Check the coordinates according to the location
# self.location_reverse() # Check the location according to the coordinates
self.location_distance()
self.location_great_circle()
if __name__ == '__main__':
gy = Geopy()
gy.run()