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.申請(qǐng)key
2.坐標(biāo)轉(zhuǎn)換
坐標(biāo)轉(zhuǎn)換是一類簡(jiǎ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.完整代碼
對(duì)于批量獲取,我一開始也走了很多彎路。一開始選用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):
在測(cè)試的時(shí)候,一個(gè)key差不多可以下載2000-3000條數(shù)據(jù),一個(gè)賬號(hào)可以申請(qǐng)4個(gè)key。這是我自己的使用情況。所以,測(cè)試的時(shí)候,不用測(cè)試過多,直接開始正式爬數(shù)據(jù)才是正道。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Python調(diào)用高德地圖API實(shí)現(xiàn)JS網(wǎng)頁地圖顯示的完整代碼示例
- 基于高德地圖API在Python中實(shí)現(xiàn)地圖功能的方法示例詳解
- 利用Python?requests庫爬取高德地圖全國(guó)地鐵站點(diǎn)信息
- python使用百度或高德地圖獲取地理位置并轉(zhuǎn)換
- Python調(diào)用高德API實(shí)現(xiàn)批量地址轉(zhuǎn)經(jīng)緯度并寫入表格的功能
- python requests爬取高德地圖數(shù)據(jù)的實(shí)例
- Python+PyQt+高德JS?API構(gòu)建桌面三維地形圖應(yīng)用實(shí)戰(zhàn)
相關(guān)文章
python 實(shí)現(xiàn) hive中類似 lateral view explode的功能示例
這篇文章主要介紹了python 實(shí)現(xiàn) hive中類似 lateral view explode的功能示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-05-05
基于python實(shí)現(xiàn)破解滑動(dòng)驗(yàn)證碼過程解析
這篇文章主要介紹了基于python實(shí)現(xiàn)破解滑動(dòng)驗(yàn)證碼過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05
Pandas之to_sql()插入數(shù)據(jù)到mysql中所遇到的問題及解決
這篇文章主要介紹了Pandas之to_sql()插入數(shù)據(jù)到mysql中所遇到的問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
使用matplotlib動(dòng)態(tài)刷新指定曲線實(shí)例
這篇文章主要介紹了使用matplotlib動(dòng)態(tài)刷新指定曲線實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-04-04
keras實(shí)現(xiàn)多種分類網(wǎng)絡(luò)的方式
這篇文章主要介紹了keras實(shí)現(xiàn)多種分類網(wǎng)絡(luò)的方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-06-06
python實(shí)現(xiàn)簡(jiǎn)單的學(xué)生成績(jī)管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)簡(jiǎn)單的學(xué)生成績(jī)管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02

