您好,登錄后才能下訂單哦!
我們都知道,可以使用高德地圖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è)賬號(hào)可以申請4個(gè)key。這是我自己的使用情況。所以,測試的時(shí)候,不用測試過多,直接開始正式爬數(shù)據(jù)才是正道。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。