Python 利用高德地圖api實(shí)現(xiàn)經(jīng)緯度與地址的批量轉(zhuǎn)換
我們都知道,可以使用高德地圖api實(shí)現(xiàn)經(jīng)緯度與地址的轉(zhuǎn)換。那么,當(dāng)我們有很多個(gè)地址與經(jīng)緯度,需要批量轉(zhuǎn)換的時(shí)候,應(yīng)該怎么辦呢?
在這里,選用高德Web服務(wù)的API,其中的地址/逆地址編碼,可以實(shí)現(xiàn)經(jīng)緯度與地址的轉(zhuǎn)換。
高德API地址:
地理/逆地理編碼:http://lbs.amap.com/api/webservice/guide/api/georegeo
坐標(biāo)轉(zhuǎn)換:http://lbs.amap.com/api/webservice/guide/api/convert
1.申請key
2.坐標(biāo)轉(zhuǎn)換
坐標(biāo)轉(zhuǎn)換是一類簡單的HTTP接口,能夠?qū)⒂脩糨斎氲姆歉叩伦鴺?biāo)(GPS坐標(biāo)、mapbar坐標(biāo)、baidu坐標(biāo))轉(zhuǎn)換成高德坐標(biāo)。
def transform(location): parameters = {'coordsys':'gps','locations': location, 'key': '7ec25a9c6716bb26f0d25e9fdfa012b8'} base = 'http://restapi.amap.com/v3/assistant/coordinate/convert' response = requests.get(base, parameters) answer = response.json() return answer['locations']
2.地理/逆地理編碼
我這里是將經(jīng)緯度轉(zhuǎn)換為地址,所以選用的是逆地理編碼的接口。
def geocode(location): parameters = {'location': location, 'key': '7ec25a9c6716bb26f0d25e9fdfa012b8'} base = 'http://restapi.amap.com/v3/geocode/regeo' response = requests.get(base, parameters) answer = response.json() return answer['regeocode']['addressComponent']['district'].encode('gbk','replace'),answer['regeocode']['formatted_address'].encode('gbk','replace')
3.從文件中讀取
需要批量獲取的話,一般是從文件中讀取數(shù)據(jù),讀取代碼如下:
def parse(): datas = [] totalListData = pd.read_csv('locs.csv') totalListDict = totalListData.to_dict('index') for i in range(0, len(totalListDict)): datas.append(str(totalListDict[i]['centroidx']) + ',' + str(totalListDict[i]['centroidy'])) return datas
4.完整代碼
對于批量獲取,我一開始也走了很多彎路。一開始選用javascript接口,但是js接口的函數(shù)是異步返回,所以可能第10行的結(jié)果跑到第15行去了,一直沒有很好的解決,后來才選用web接口。最后,將完整代碼貼于此,僅供參考。
#!/usr/bin/env #-*- coding:utf-8 -*- ''' 利用高德地圖api實(shí)現(xiàn)經(jīng)緯度與地址的批量轉(zhuǎn)換 ''' import requests import pandas as pd import time import sys reload(sys) sys.setdefaultencoding("utf-8") def parse(): datas = [] totalListData = pd.read_csv('locs.csv') totalListDict = totalListData.to_dict('index') for i in range(0, len(totalListDict)): datas.append(str(totalListDict[i]['centroidx']) + ',' + str(totalListDict[i]['centroidy'])) return datas def transform(location): parameters = {'coordsys':'gps','locations': location, 'key': '7ec25a9c6716bb26f0d25e9fdfa012b8'} base = 'http://restapi.amap.com/v3/assistant/coordinate/convert' response = requests.get(base, parameters) answer = response.json() return answer['locations'] def geocode(location): parameters = {'location': location, 'key': '7ec25a9c6716bb26f0d25e9fdfa012b8'} base = 'http://restapi.amap.com/v3/geocode/regeo' response = requests.get(base, parameters) answer = response.json() return answer['regeocode']['addressComponent']['district'].encode('gbk','replace'),answer['regeocode']['formatted_address'].encode('gbk','replace') if __name__=='__main__': i = 0 count = 0 df = pd.DataFrame(columns=['location','detail']) #locations = parse(item) locations = parse() for location in locations: dist, detail = geocode(transform(location)) df.loc[i] = [dist, detail] i = i + 1 df.to_csv('locdetail.csv', index =False)
注意事項(xiàng):
在測試的時(shí)候,一個(gè)key差不多可以下載2000-3000條數(shù)據(jù),一個(gè)賬號可以申請4個(gè)key。這是我自己的使用情況。所以,測試的時(shí)候,不用測試過多,直接開始正式爬數(shù)據(jù)才是正道。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
利用Python?實(shí)現(xiàn)分布式計(jì)算
這篇文章主要介紹了利用Python?實(shí)現(xiàn)分布式計(jì)算,文章通過借助于?Ray展開對分布式計(jì)算的實(shí)現(xiàn),感興趣的小伙伴可以參考一下2022-05-05Django多進(jìn)程滾動(dòng)日志問題解決方案
這篇文章主要介紹了Django多進(jìn)程滾動(dòng)日志問題解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12python 實(shí)現(xiàn)讓字典的value 成為列表
今天小編就為大家分享一篇python 實(shí)現(xiàn)讓字典的value 成為列表,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12