(1)申请百度地图API信令
访问百度地图API需要一个信令(AK),打开百度地图开放平台(http://lbsyun.baidu.com/index.php?title=%E9%A6%96%E9%A1%B5),点击右上角“API控制台”,进入了百度地图的开发界面。
选择“创建应用”-应用类型勾选“服务器端”–勾选所用到的服务(一般全选即可),此时就创建好了应用账号,得到“AK”。
(2)获取目标区域的经纬度
利用百度地图坐标拾取系统(http://api.map.baidu.com/lbsapi/getpoint/index.html)获取矩形目标区域的左下角和右上角两点的经纬度
(3)要获取的POI类型
百度地图Place API提供的POI提供二级行业分类(http://lbsyun.baidu.com/index.phptitle=lbscloud/poitags)
选择你需要的行业分类
(4)获取POI信息
适用于Python3,同时需要安装requests、json、xlwt库
只需要修改query、left_bottom、right_top、ak四个参数
query:第三步中选择的行业分类(注意'')
left_bottom:第二步中左下角的经纬度(经度在前,纬度在后,中间用逗号隔开)
right_top:第二步中右上角的经纬度
ak:第一步中获取的AK秘钥
import requests
import json
import time
import xlwt
query = '企业' #关键词
left_bottom = [108.915,34.246] #左下角的经纬度
right_top = [108.995,34.255] #右上角的经纬度
ak = 'xxxxxxxxxxx' #百度地图API信令AK
left_bottom_part = []
right_top_part = []
urllist = []
r = 0
part_n = 1
p = 1
col = 0
row = 0
result =[]
wb = xlwt.Workbook()
ws = wb.add_sheet('result')
url0 = 'http://api.map.baidu.com/place/v2/search?'
url = url0 +'query=' + query + '&bounds='+ str(left_bottom[1]) + ',' + str(left_bottom[0]) + ',' + str(right_top[1])+ ',' + str(right_top[0]) + '&page_size=20&page_num=0&output=json&ak=' + ak
data = requests.get(url).text
data_json = json.loads(data)
total = data_json['total']
while total == 400:
part_n = part_n *2
a = []
for i in range(part_n):
for j in range(part_n):
x_item = (right_top[0]-left_bottom[0])/part_n
y_item = (right_top[1]-left_bottom[1])/part_n
left_bottom_part = [left_bottom[0]+i*x_item,left_bottom[1]+j*y_item]
right_top_part = [left_bottom[0]+(i+1)*x_item,left_bottom[1]+(j+1)*y_item]
url = url0 +'query=' + query + '&bounds=' + str(left_bottom_part[1]) + ',' + str(left_bottom_part[0]) + ',' + str(right_top_part[1]) + ',' + str(right_top_part[0]) + '&page_size=20&page_num=0&output=json&ak=' + ak
data = requests.get(url).text
data_json = json.loads(data)
a.append(data_json['total'])
time.sleep(1)
total = max(a)
pass
for m in range(part_n):
for n in range(part_n):
b = 0
x_item = (right_top[0]-left_bottom[0])/part_n
y_item = (right_top[1]-left_bottom[1])/part_n
left_bottom_part = [left_bottom[0]+m*x_item,left_bottom[1]+n*y_item]
right_top_part = [left_bottom[0]+(m+1)*x_item,left_bottom[1]+(n+1)*y_item]
url1 = url0 +'query=' + query + '&bounds=' + str(left_bottom_part[1]) + ',' + str(left_bottom_part[0]) + ',' + str(right_top_part[1]) + ',' + str(right_top_part[0]) + '&page_size=20&page_num=0&output=json&ak=' + ak
data = requests.get(url1).text
data_json = json.loads(data)
if data_json['total']%20 ==0:
b = data_json['total']/20
else:
b = data_json['total']//20+1
for c in range(int(b)):
url2 = url0 +'query=' + query + '&bounds=' + str(left_bottom_part[1]) + ',' + str(left_bottom_part[0]) + ',' + str(right_top_part[1])+ ',' + str(right_top_part[0])+ '&page_size=20&page_num='+ str(c) +'&output=json&ak=' + ak
data = requests.get(url2).text
data_json = json.loads(data)
result.append(data_json['results'])
print('第'+str(p)+'页成功'+','+str(data_json['total'])+'条数据载入')
p +=1
urllist.append(url2)
time.sleep(1)
for e in result:
for g in e:
ws.write(row,0,str(g))
row +=1
wb.save('result.xls')
(5)数据清洗
Excel中“数据”-“分列”-分隔符号-“,”
“查找替换”将文本中不要的内容删去
注:获取的数据为百度坐标系,可以通过http://mapclub.cn/archives/2168转换为国测局坐标系
(6)数据可视化
可以用Powermap对数据进行分析,也可以导入gis分析。