赞
踩
参考: 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):
'''
地理编码 根据地址查询坐标及详细信息
OpenStreetMap 平台上提供的 Nominatim 地理编码器,不需要申请 API ,限流,限额,不能大规模频繁访问
我的情况:白嫖限速、限次数 非常很
: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") # 放ua
location = geolocator.geocode("珠穆朗玛峰") # 根据查相关信息
location = geolocator.geocode("北京市海淀区西二旗北路")
print(location, location.address, location.latitude, location.longitude, location.altitude, location.point, location.raw)
def location_reverse(self):
"""
逆地理编码 根据坐标查地点
: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") # 放ua
# location = geolocator.reverse("39.90733345 116.391244079988") # reverse 方法接收经纬度字符串作为输入,纬度在前,经度在后
location = geolocator.reverse("31.239853 121.499740") # reverse 方法接收经纬度字符串作为输入,纬度在前,经度在后
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):
"""
两个经纬度坐标计算距离: 大圆算法将地球简化为一个圆球,其计算的距离是球面上过两点的大圆距离
有点: 大圆距离计算速度快 缺点: 一定的误差(0.5%以内)
:return:
"""
dist = distance.distance((39.90733345,116.391244079988), (31.239853,121.499740)) # 纬度在前、经度在后
dist = distance.geodesic((45.768189, 126.6212835), (39.907359, 116.3912630)) # 等同上
print(dist.km) # 距离: km: 千米, m: 米, miles: 英里
def location_great_circle(self):
"""
计算球面距离: 大地线使用目前国际通用的方法,用旋转椭球面表示地球,其计算的是两点在椭球面上的最短距离。
优点:精确度高 缺点: 大地线的劣势在于计算速度太慢
:return:
"""
gc = distance.great_circle((45.768189, 126.6212835), (39.907359, 116.3912630)) # 同样返回distance对象
print(gc.km) # 距离: km: 千米, m: 米, miles: 英里
# print(f'{(dist.km - gc.km) / dist.km:%}')
# 哈尔滨到北京的大圆与大地线距离之间有0.13%的差异
def run(self):
# self.location_geocode() # 根据地点查坐标
# self.location_reverse() # 根据坐标查地点
self.location_distance()
self.location_great_circle()
if __name__ == '__main__':
gy = Geopy()
gy.run()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。