当前位置 : 主页 > 编程语言 > python >

百度地图开放平台web api 获取某项目方圆一公里的poi信息

来源:互联网 收集:自由互联 发布时间:2022-06-15
百度地图开放平台web api 获取方圆一公里的poi信息 ​​背景​​ ​​完整代码​​ ​​代码解读​​ ​​改进思路​​ ​​改进的代码​​ 背景 手里头有集中式公寓的9个项目,并且



百度地图开放平台web api 获取方圆一公里的poi信息

  • ​​背景​​
  • ​​完整代码​​
  • ​​代码解读​​
  • ​​改进思路​​
  • ​​改进的代码​​

背景

手里头有集中式公寓的9个项目,并且知道对应的坐标,现在要由这9个项目出发,获取其周边一公里内的小区,地铁站,酒店等信息,仔细分析属于地理检索功能,由已知的地理位置出发,根据距离关系,检索出其周边相关poi点,一般做法有下面两种

百度地图开放平台web api 获取某项目方圆一公里的poi信息_python

  • 利用arcgis软件,把这9个项目落点到底图进去,形成一个图层layer,再把小区数据,地铁站数据,酒店数据落点到底图上形成对应的layers, 以项目为圆心,做出9个半径为1公里的圆形缓冲区,把缓冲区里面的小区图层,酒店图层和地铁站图层的目录(directory)都拎出来。
  • 利用百度地图开放平台web api接口,调用其地点检索服务,利用其中的圆形区域检索,把9个项目里面的小区,酒店,地铁都检索出来。

本次主要探讨第二个方案

完整代码

# -*- coding: utf-8 -*-
"""
project_name:集中式公寓周边一公里poi点
@author: 帅帅de三叔
Created on Tue Aug 20 11:16:10 2019
"""

import pandas as pd #导入数据分析模块
import requests #导入网页请求模块
data=pd.read_excel("集中式公寓项目.xlsx") #读取数据
lat_partion=data["百度纬度"] #纬度
lng_partion=data["百度经度"] #经度
store=data["项目名称"] #门店
targets=[] #用来存要查找的名称
addresss=[] #用来存放地址
stores=[] #用来存放项目名称
distances=[] #用来存放距离

def get_directory(keyword,radius): #定义圆形区域检索函数
for i in range(len(lat_partion)):
location=str(lat_partion[i])+","+ str(lng_partion[i]) #构造圆中心点的经纬度
#print(location) #测试
try:
url="http://api.map.baidu.com/place/v2/search?query="+keyword+"&page_size=10&page_num=0&location="+location+"&radius="+str(radius)+"&output=json&ak=【你的ak】&scope=2"#构造请求网址
#print(url) #测试请求接口拼接是否正确,此url可以直接复制到浏览器查看返回结果
response=requests.get(url) #发出请求
answer=response.json() #返回结果json化
#print(answer) #测试
print("一共%s条数据"%len(answer['results']))
for k in range(len(answer['results'])):
target=answer['results'][k]['name'] #标的物名称
address=answer['results'][k]['address'] #地址
distance=answer['results'][k]['detail_info']['distance'] #距离
targets.append(target)
addresss.append(address)
stores.append(store[i])
distances.append(distance)
print(target,distance,address)
except:
print("the circle contains no message")
stores.append(store[i])
targets.append("")
distances.append("")
addresss.append("")

if __name__=='__main__':
keyword=input("please input the keywords:") #输入poi的关键词
radius=input("please input the radius:") #输入半径
get_directory(keyword,radius) #调用函数get_directory
dict={"项目名称":stores,"标的物":targets,"距离":distances,"地址":addresss} #构造字典
df=pd.DataFrame(dict)
df.to_excel("检索结果.xlsx")

代码解读

整个流程是当你输入关键词,如“公司”,再输入距离(单位是米),如“1000”,然后调用get_directory()函数,把返回的结果json化,提取里面的公司名称,距离,地址等信息,并追加起来,然后字典化处理形成数据框,最后写入到excel表

百度地图开放平台web api 获取某项目方圆一公里的poi信息_方圆一公里_02

从excel表可以看到这个一个1对多的映射关系,比如上海昌林路店这个项目周边一公里就有10个公司,代码的关键是读取原数据,提取经纬度数据,并构造圆中心点的经纬度数据location=str(lat_partion[i])+","+ str(lng_partion[i]) ,然后放到请求地址里面去,利用循环不断请求,不断解析保存。

改进思路

后续发现还需要周边poi的经纬度信息,故需要添加两个字段,一个是经度,一个是纬度,同时在使用try except 的同时会掩盖很多真实情况,以后在写程序的时候会考虑尽量少用这个结构来规避报错。

改进的代码

# -*- coding: utf-8 -*-
"""
project_name:周边一公里poi点检索
@author: 帅帅de三叔
Created on Tue Aug 20 11:16:10 2019
"""
import pandas as pd #导入数据分析模块
import requests #导入网页请求模块
data=pd.read_excel("检索.xlsx") #读取数据
lat_partion=data["百度纬度"] #纬度
lng_partion=data["百度经度"] #经度
center=data["检索中心"] #检索中心
targets=[] #用来存要查找的名称
addresss=[] #用来存放地址
centers=[] #用来检索中心的名称
distances=[] #用来存放距离
lngs=[] #用来存目标放经度
lats=[] #用来存目标放纬度
def get_directory(keyword,radius): #定义圆形区域检索函数
for i in range(len(lat_partion)):
location=str(lat_partion[i])+","+ str(lng_partion[i]) #构造圆中心点的经纬度
print(location) #测试请求url
url="http://api.map.baidu.com/place/v2/search?query="+keyword+"&page_size=20&page_num=0&location="+location+"&radius="+str(radius)+"&output=json&ak=rSXLRzPzHMcvSy1V33SNE111wx7UHEYB&scope=2"#构造请求网址
#print(url) #测试请求接口拼接是否正确,此url可以直接复制到浏览器查看返回结果
response=requests.get(url) #发出请求
answer=response.json() #返回结果json化
#print(answer) #测试
print("一共%s条数据"%len(answer['results']))
for k in range(len(answer['results'])):
try:
target=answer['results'][k]['name'] #标的物名称
targets.append(target)
except:
targets.append("")
try:
address=answer['results'][k]['address'] #地址
addresss.append(address)
except:
addresss.append("")
try:
distance=answer['results'][k]['detail_info']['distance'] #距离
distances.append(distance)
except:
distances.append("")
try:
longitude=answer['results'][k]['location']['lng'] #导航引导点经度
lngs.append(longitude)
print(longitude)
except:
lngs.append("")
try:
latitude=answer['results'][k]['location']['lat'] #导航引导点纬度
lats.append(latitude)
print(latitude)
except:
lats.append("")
centers.append(center[i])
print(center[i],target,longitude,latitude,distance,address) #测试结果 longitude,latitude
if __name__=='__main__':
keyword=input("please input the keywords:") #输入poi的关键词
radius=input("please input the radius:") #输入半径
get_directory(keyword,radius) #调用函数get_directory
dict={"中心":centers,"标的物":targets,"百度经度":lngs,"百度纬度":lats,"距离":distances,"地址":addresss} #构造字典
df=pd.DataFrame(dict)
df.to_excel("检索结果.xlsx")

如果还有不懂的话可以关注“三行科创”公众号,进交流群一起交流。

百度地图开放平台web api 获取某项目方圆一公里的poi信息_poi信息_03



上一篇:多指标客观赋权重及熵权法的python实现
下一篇:没有了
网友评论