import pandas as pd
import json
from urllib.request import urlopen, quote
import requests
import re
import pandas as pd
from geopy.geocoders import Nominatim,GoogleV3
from geopy.exc import GeocoderTimedOut
first_aid = pd.read_pickle('t_first_aid.pkl')
# In[]将地址进行补齐
tmp_False = first_aid[first_aid['address'].isna() == False]
tmp_True = first_aid[first_aid['address'].isna() == True]
tmp_True['address'] = tmp_True['call_help_address']
first_aid = tmp_False.append(tmp_True)
first_aid = first_aid[first_aid['address'].isna() == False]
first_aid = first_aid.reset_index()
del first_aid['index']
def word_find(x):
findword = u'(越秀|荔湾|海珠|天河|白云|黄埔|番禺|花都|南沙|增城|从化)'
pattern = re.compile(findword)
results = pattern.findall(x)
if len(results)!=0:
result = results[0]
else:
result = '其他'
return result
first_aid['new_address'] = first_aid['address'].apply(lambda x:word_find(x))
unmend_aid = first_aid[first_aid['new_address']!='其他']
mend_aid = first_aid[first_aid['new_address']=='其他']
mend_aid = mend_aid[:290000]
def getlnglat(address):
url = 'http://api.map.baidu.com/geocoding/v3/'
output = 'json'
ak = 'xxx' # 百度地图ak,具体申请自行百度,提醒需要在“控制台”-“设置”-“启动服务”-“正逆地理编码”,启动
address = quote('广东省广州市'+address) # 由于本文地址变量为中文,为防止乱码,先用quote进行编码
uri = url + '?' + 'address=' + address + '&output=' + output + '&ak=' + ak +'&callback=showLocation%20'+'//GET%E8%AF%B7%E6%B1%82'
# req = urlopen(uri)
# res = req.read().decode() 这种方式也可以,和下面的效果一样,都是返回json格式
res=requests.get(uri).text
temp = json.loads(res) # 将字符串转化为json
lat = temp['result']['location']['lat']
lng = temp['result']['location']['lng']
return lat,lng # 纬度 latitude,经度 longitude
lnglat_list = []
for index in range(len(mend_aid)):
f = open('经纬度记录.txt','a')
addr = mend_aid['address'].iloc[index]
try:
result = getlnglat(addr)
except:
reselt = None
if index%10000 ==0:
print(index)
f.write(str(index))
f.write('\t')
f.write(str(result))
f.write('\n')
f.close()
lnglat_list.append(result)
mend_aid['lat_lng'] = lnglat_list
mend_aid.to_pickle('first_aid_mend_with_lnglat_1.pkl')
geoCode = Nominatim(timeout=1.5,user_agent="geopy/3.0.0")
new_address = []
for i,line in enumerate(mend_aid['lat_lng']):
try:
location = geoCode.reverse(line)
fullLine = location.address
except:
fullLine = '其他'
new_address.append(fullLine)
if i%10000 == 0:
print(i)
mend_aid['address'] = new_address
mend_aid['new_address'] = mend_aid['address'].apply(lambda x:word_find(x))
mend_aid.to_pickle('first_aid_mend_with_newaddr_1.pkl')
python得到地址的经纬度,并将经纬度转成详细地址
最新推荐文章于 2023-12-20 23:00:55 发布