程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Pythons geometry usage (encapsulated)

編輯:Python

Reference resources : https://blog.csdn.net/Next_Second/article/details/78618081

geopy It abstracts a series of different geocoding Services API. It allows you to get the full street address of a place , latitude , longitude , Even the height . Geocoding function , Need help Geopy Of geocoders modular ,Geopy All third parties API Package to geocoders in

  • Geocoding : Convert string to geographic location
  • Reverse geocoding : Used to convert geographic coordinates to specific addresses
  • Calculate the distance between two points : Longitude and latitude distance and spherical distance
  • 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()

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved