前言
一个很有意思的模块”geocoder“,只要功能是地理编码与反地理编码,集合了国内外很多地图。

演示
本次演示,IDE为ipython,api为Arcgis,因为他不需要Key。
地理编码
In [1]: import <a href="https://www.ixxin.cn/tag/geocoder/" title="查看更多关于geocoder的文章" target="_blank">geocoder</a> In [2]: g = geocoder.arcgis("qingdao") In [3]: g.wkt Out[3]: 'POINT(120.371939388 36.098605988)' In [4]: g.json Out[4]: {'address': u'Qingdao, Shandong, China', 'bbox': {'northeast': [36.209606, 120.482939], 'southwest': [35.987606, 120.260939]}, 'confidence': 1, 'encoding': 'utf-8', 'lat': 36.098605988000486, 'lng': 120.37193938800056, 'location': 'qingdao', 'ok': True, 'provider': 'arcgis', 'quality': u'POI', 'score': 100, 'status': 'OK', 'status_code': 200}
反地理编码
In [14]: g = geocoder.arcgis([45.15,-75.14],method = "reverse") In [15]: g.city Out[15]: u'North Stormont' In [16]: g.country Out[16]: u'CAN'
IP地址
In [17]: g = geocoder.ip('199.7.157.0') In [18]: g.city Out[18]: u'Scarborough' In [19]: g = geocoder.ip('me') In [20]: g.city Out[20]: u'Jinan
距离量算
In [26]: d = distance("Ottawa, ON", "Toronto, ON") In [27]: print d 352.669845047 In [28]: d = distance("Ottawa, ON", "Toronto, ON",provider = "google") In [29]: print d 352.669845047 In [30]: d = distance("Ottawa, ON", "Toronto, ON",provider = "arcgis") In [31]: print d 352.669845047 In [32]: d = distance("Ottawa, ON", "Toronto, ON",provider = "arcgis",units = "miles") In [33]: print d 219.138814287 值得注意的是,默认地图为必应,默认单位不是米,中国的城市不能量测,如下: In [25]: dis = distance("qingdao,ON","jinan,ON",provider = "google") [WARNING] Error calculating the following two locations. Points: qingdao,ON to jinan,ON
后语
作者GitHub地址:https://github.com/DenisCarriere/geocoder,可以去给他点星星。