-
2021-12-21 17:11:19
错误详情
geopy.exc.ConfigurationError: Using Nominatim with default or sample `user_agent` "geopy/2.2.0" is strongly discouraged, as it violates Nominatim's ToS https://operations.osmfoundation.org/policies/nominatim/ and may possibly cause 403 and 429 HTTP errors. Please specify a custom `user_agent` with `Nominatim(user_agent="my-application")` or by overriding the default `user_agent`: `geopy.geocoders.options.default_user_agent = "my-application"`.
解决办法
这个错误是因为这个 UA 默认值被用烂了,随便指定 user-agent 为一个 unique 的字符串就行,比如 BuyiXiao
初始化 Nominatim 时指定 user-agentgeolocator = Nominatim(user_agent='BuyiXiao')
更多相关内容 -
geopy
2019-12-31 11:40:13一、geopy 简介及安装 可以使用geopy库来查询地址,国家,城市,地标,geopy使用的是第三方的geo解析器(包括谷歌地图,必应地图,Nominatim等)和一些数据源来获取地理信息。 安装 geopy: pip3 install geopy 二、...一、
geopy
简介及安装可以使用
geopy
库来查询地址,国家,城市,地标,geopy
使用的是第三方的geo
解析器(包括谷歌地图,必应地图,Nominatim
等)和一些数据源来获取地理信息。安装 geopy:
pip3 install geopy
二、
geopy
使用1、从地址字符串获取
Location
对象也就是将字符串转换为地理位置。
# coding=utf-8 from geopy.geocoders import Nominatim geolocator = Nominatim() location = geolocator.geocode('故宫') print(location.address) # 故宫, 故宫东门外, 崇文, 北京市, 东城区, 北京市, 100010, 中国 print(location.latitude, location.longitude) # 输出故宫的纬度、经度 # 39.91727565 116.390769405773 # 输出 Location 的所有信息 print(location.raw) # { # 'place_id': 259835570, 'licence': 'Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright', # 'osm_type': 'relation', 'osm_id': 9511883, 'boundingbox': ['39.9116214', # '39.9211913', # '116.386202', # '116.3953903'], # 'lat': '39.91727565', # 'lon': '116.390769405773', # 'display_name': '故宫, 故宫东门外, 崇文, 北京市, 东城区, 北京市, 100010, 中国', # 'class': 'tourism', 'type': 'attraction', 'importance': 0.545487227980481, # 'icon': 'https://nominatim.openstreetmap.org/images/mapicons/poi_point_of_interest.p.20.png' # }
2、从经纬度获取
Location
对象也就是将经纬度转换为地理位置。
# coding=utf-8 from geopy.geocoders import Nominatim # 将经纬度转换为地理位置 gelocator = Nominatim() location = gelocator.reverse("39.9073285, 116.391242416486") print(location.address) # # 天安门, 1, 西长安街, 崇文, 北京市, 东城区, 北京市, 100010, 中国 # print(location.raw) # # { # 'place_id': 242516302, # 'licence': 'Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright', # 'osm_type': 'relation', # 'osm_id': 8847697, # 'lat': '39.907359', # 'lon': '116.391263017795', # 'display_name': '天安门, 1, 西长安街, 崇文, 北京市, 东城区, 北京市, 100010, 中国', # 'address': { # 'address29': '天安门', # 'house_number': '1', # 'road': '西长安街', # 'suburb': '崇文', # 'city': '东城区', # 'state': '北京市', # 'postcode': '100010', # 'country': '中国', # 'country_code': 'cn' # }, # 'boundingbox': [ # '39.9071482', # '39.9075289', # '116.3905678', # '116.3919619' # ] # }
3、计算两点距离
单位可以为
meters
米(简写m
)kilometers
千米(简写km
)miles
英里(简写mi
)nautical
海里(简写nm
)feet
英尺(简写ft
)
计算经纬度距离
Vincenty distance
from geopy.distance import vincenty tiananmen = (39.9073285, 116.391242416486) xiaozhai = (34.2253171, 108.9426205) print(vincenty(tiananmen, xiaozhai).meters) # 913925.3164971863
计算球面距离
great-circle distance
## 计算球面距离 from geopy.distance import great_circle tiananmen = (39.9073285, 116.391242416486) xiaozhai = (34.2253171, 108.9426205) print(great_circle(tiananmen, xiaozhai).meters) # 913657.4596518736
geopy
的官网地址为:https://geopy.readthedocs.io/en/latest/
-
latitude-longitude-geopy:使用geopy查找任何地址的纬度和经度
2021-04-16 18:38:18使用geopy获取Latitude和Longitude 。 使用unidecode更改重音字符。 例如,我的data.csv文件中提到的位置来自哥斯达黎加,并且在城市名称中有很多带重音符号的字符。 创建了一个函数do_geocode来处理Geocoder超时... -
geopy简单使用
2020-08-10 15:58:44geopy 和 geopandas 简单使用安装 geopy 和 geopandas
安装
geopy
和geopandas
pip install geopy #安装 geopandas不能使用pip,会报错,在Anaconda下使用下conda conda install -c conda-forge geopandas
根据城市名或者经纬度相互转换
import geopy from geopy.geocoders import Nominatim import geopandas locator = Nominatim(user_agent = "myGeocoder") location = locator.geocode("Champ de Mars, Paris, France") print(location.address) # Champ de Mars, Place Edwige Feuillère, Quartier du Gros-Caillou, Paris, Île-de-France, France métropolitaine, 75007, France
from functools import partial geolocator = Nominatim(user_agent = "myGeocoder") geocode = partial(geolocator.geocode, language='en') print(geocode('wuhan',language="en")) reverse = partial(geolocator.reverse, language='en') print(reverse("41.15166616,28.70958502")) print(reverse("52.509669, 13.376294")) #Wuhan, Jiang'an District, Wuhan, Hubei, 430062, China #Hacımaşlı Mahallesi, Arnavutköy, Istanbul, Marmara Region, 34277, Turkey #Potsdamer Platz, Bellevuestraße, Botschaftsviertel, Tiergarten, Mitte, Berlin, 10785, Germany
官方例子-链接
根据地点获取经纬度>>> from geopy.geocoders import Nominatim >>> geolocator = Nominatim(user_agent="specify_your_app_name_here") >>> location = geolocator.geocode("175 5th Avenue NYC") >>> print(location.address) Flatiron Building, 175, 5th Avenue, Flatiron, New York, NYC, New York, ... >>> print((location.latitude, location.longitude)) (40.7410861, -73.9896297241625) >>> print(location.raw) {'place_id': '9167009604', 'type': 'attraction', ...}
根据经纬度获取地点
>>> from geopy.geocoders import Nominatim >>> geolocator = Nominatim(user_agent="specify_your_app_name_here") >>> location = geolocator.reverse("52.509669, 13.376294") >>> print(location.address) Potsdamer Platz, Mitte, Berlin, 10117, Deutschland, European Union >>> print((location.latitude, location.longitude)) (52.5094982, 13.3765983) >>> print(location.raw) {'place_id': '654513', 'osm_type': 'node', ...}
这里可对输出的地点进行字符串切割,单独获取城市名或州名
location = geolocator.reverse("-37.68022156,144.84001") print(location.address.split(",")[-3]) # Operations Road, Melbourne Airport, City of Hume, Victoria, 3045, Australia # Victoria 切割后的字符串
测试两坐标之间的距离
>>> from geopy.distance import geodesic >>> newport_ri = (41.49008, -71.312796) >>> cleveland_oh = (41.499498, -81.695391) >>> print(geodesic(newport_ri, cleveland_oh).miles) 538.390445368
…
-
Python地理编码库geopy
2020-02-14 18:32:48geopy调用Baidu出现geopy.exc.GeocoderAuthenticationFailure: Authentication Failure 修改源码 得到结果 教育 (34.291641819762745, 108.07934220117825) {'location': {'lng': 108.07934220117825, 'lat': 34...安装
pip install geopy
初试
geopy
需要调用地理定位服务,本文使用百度地图开放平台创建应用→IP地址查询获得本机IP→填入IP白名单中
填入AK(api_key)访问:http://api.map.baidu.com/geocoding/v3/?address=您的AK&output=json&ak=您的ak&callback=showLocation
出现以下结果则成功:
showLocation&&showLocation({"status":0,"result":{"location":{"lng":116.40384710616807,"lat":39.91552563252131},"precise":0,"confidence":50,"comprehension":0,"level":"旅游景点"}})
运行代码
from geopy.geocoders import Baidu geolocator = Baidu(api_key='您的AK') location = geolocator.geocode('西北农林科技大学') print(location.address) print((location.latitude, location.longitude)) print(location.raw)
若报错,请参考geopy调用Baidu出现geopy.exc.GeocoderAuthenticationFailure: Authentication Failure修改源码
得到结果
教育 (34.291641819762745, 108.07934220117825) {'location': {'lng': 108.07934220117825, 'lat': 34.291641819762745}, 'precise': 0, 'confidence': 70, 'comprehension': 0, 'level': '教育'}
地理编码
修改源码
api_path = '/geocoding/v3'
from geopy.geocoders import Baidu geolocator = Baidu(api_key='您的AK') location = geolocator.geocode('西北农林科技大学') print(location.address) print((location.latitude, location.longitude)) print(location.raw) # 教育 # (34.291641819762745, 108.07934220117825) # {'location': {'lng': 108.07934220117825, 'lat': 34.291641819762745}, 'precise': 0, 'confidence': 70, 'comprehension': 0, 'level': '教育'}
逆地理编码
修改源码
api_path = '/reverse_geocoding/v3'
from geopy.geocoders import Baidu geolocator = Baidu(api_key='您的AK') location = geolocator.reverse((34.291641, 108.079342)) print(str(location.address, encoding="utf-8")) print((location.latitude, location.longitude)) print(location.raw) # 陕西省咸阳市杨陵区 # (34.29164102762759, 108.07934199999994) # {'location': {'lng': 108.07934199999994, 'lat': 34.29164102762759}, 'formatted_address': '陕西省咸阳市杨陵区', 'business': '西农路,杨陵区中心城区', 'addressComponent': {'country': '中国', 'country_code': 0, 'country_code_iso': 'CHN', 'country_code_iso2': 'CN', 'province': '陕西省', 'city': '咸阳市', 'city_level': 2, 'district': '杨陵区', 'town': '', 'town_code': '', 'adcode': '610403', 'street': '', 'street_number': '', 'direction': '', 'distance': ''}, 'pois': [], 'roads': [], 'poiRegions': [], 'sematic_description': '', 'cityCode': 323}
计算距离
使用国际大地测量学和地球物理学联合会定义的平均地球半径 ( 2 a + b ) / 3 = 6371.0087714150598 (2a + b)/3 = 6371.0087714150598 (2a+b)/3=6371.0087714150598公里,测地距离是地球椭球面模型表面上最短的距离,默认算法使用的方法是Karney (2013) (
geodesic
)from geopy.distance import geodesic guangzhou = (23.135336306695006, 113.27143134445974) beijing = (39.910924547299565, 116.4133836971231) distance = geodesic(guangzhou, beijing) print(distance) # 1883.5361817595797 km
参考文献
-
spyder导入geopy包
2019-04-13 17:11:23一、spyder简介 类似eclipse作为Java的集成开发环境一样,spyder是python语言的集成开发环境,和其他的Python开发环境相比,它最大的优点就是模仿MATLAB的“工作空间”的功能,可以很方便地观察和修改...from geopy... -
Geopy进行地址经纬度等转换
2020-12-31 10:06:38Geopy进行地址经纬度等转换 目录 Geopy进行地址经纬度等转换 Geopy是怎样工作的呢? Geopy的下载 Geopy的应用 Geopy官方文档https://geopy.readthedocs.io/en/latest/# Geopy可以满足地址经纬度转换等操作。 ... -
Python 库 Geopy 的用法,经纬度坐标转换、经纬度距离计算
2021-04-19 00:36:372,Geopy 2.1 Geopy 库介绍 这里介绍一个Python 包 Geopy ,借助它也可以实现经纬度地理位置转换, 这款包之经纬度转换原理其实还是借助了第三方 API 平台,因为市面上提供经纬度转换 第三方平台很多,为了方便, ... -
使用conda 安装geopy库
2020-08-26 23:23:04使用命令conda install -c conda-forge geopy进行安装geopy库,之后输入y即可安装成功。 -
geopy调用Baidu出现geopy.exc.GeocoderAuthenticationFailure: Authentication Failure
2020-02-14 17:30:13问题描述 pip install geopy安装了版本为1.21.0的geopy from geopy.geocoders import Baidu geolocator = Baidu(api_key='我的ak') location = geolocator.geocode('西北农林科技大学') print(location.address) ... -
python geopy获取城市和国家
2020-12-21 13:26:22我想用geopy找到城市和国家的名字。在通常情况下,这就是如何使用geopy获取城市和国家的信息。在from geopy.geocoders import Nominatimgeolocator = Nominatim(timeout=3)geolocator.reverse('52.5094982,13.... -
python – Geopy:计算GPS航向/方位
2021-03-17 01:26:22特别是对于这部分,我只是使用: 我一直在研究我的位置协方差,使用geopy的便捷距离函数来抵消我测得的地面实况.通过很少按摩参数,我可以找到矩阵中每个标准偏差元素所描绘的每个方向的距离;北,东,向和三个方向之间. ... -
python地理数据处理库geopy
2016-08-14 17:35:47python地理位置处理python地理编码地址以及用来处理经纬度的库GeoDjango – 世界级地理图形 web 框架。GeoIP – MaxMind GeoIP Legacy ...geopy – Python 地址编码工具箱。pygeoip – 纯 Python GeoIP API。django-co -
Python库 | geopy-1.3.0-py2.py3-none-any.whl
2022-03-20 22:14:23python库,解压后可用。 资源全名:geopy-1.3.0-py2.py3-none-any.whl -
Python库 | geopy-1.13.0-py2.py3-none-any.whl
2022-03-20 22:13:44python库,解压后可用。 资源全名:geopy-1.13.0-py2.py3-none-any.whl -
Python库 | geopy-1.1.0-py2.py3-none-any.whl
2022-03-20 22:13:06python库,解压后可用。 资源全名:geopy-1.1.0-py2.py3-none-any.whl -
geopy 在python中的使用
2018-06-21 10:35:52geopy 是一个关于地理编码的python库。主要有以下几个功能:(需要联网) 地理编码:将字符串转换为地理位置 逆地理编码:用于将地理坐标转换为具体地址 计算两个点的距离:经纬度距离和球面距离 安装 pip install ... -
在python中使用geopy的百度API解析经纬度
2018-11-19 21:40:10将经纬度解析为地址,使用geopy默认提供的东西解析单个经纬度够用了,但是成批的解析会经常超时。查阅资料发现可以在geopy中使用百度的api。超时的问题就容易解决了。 百度api webapi/guide/webservice-geocoding-... -
Python geopy使用注意事项
2019-09-30 22:47:59使用geopy获取地理位置时发现经常会报错:service timed out, 具体是握手时出现timed out:URLError: <urlopen error _ssl.c:761: The handshake operation timed out> During handling of the above ... -
Python地理位置信息库geopy的使用(一):基本使用
2018-09-18 14:53:50geopy是Python关于地理位置的一个第三方库,用这个库来进行地址位置信息的查询和转换非常方便,本文介绍关于geopy的常用的几种用法 geopy的安装 pip install geopy 根据地址查询坐标及详细信息 >>&... -
【Python】安装geopy
2018-05-15 10:19:00C:\Users\horn1\Desktop\python\49-geo>pip install geopy Collecting geopy Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ConnectTimeo... -
python地理处理包——geopy使用之地理编码与反地理编码
2019-10-02 22:43:11在geopy中用的经纬度距离是默认的方式,类为geopy.distance.distance,计算距离为其属性(e.g., miles , meters , etc)。 给出计算经纬度距离的例子如下: >>> from geopy.distance import vincenty ...