Python obtains the specified range of POI through the Gaode map interface

Reference source: https://blog.csdn.net/weixin_47796965/article/details/108372378

The POI data of AutoNavi Map is open to use. I recently saw a reference article and decided to try it. The free use request returns up to 1000 pieces of information. This article mainly uses block requests for the target area to achieve the circumvention of restrictions and achieve the goal of solving the problem.

Steps:
1. Obtain the range of the target area of ​​the AutoNavi map
2. Divide the target range into an appropriate amount of areas and generate the center point
3. Calculate the four points of the center point and assign the center point
4. Write the program. According to the open polygon search function of the LBS of AutoNavi Map, the POI retrieval and acquisition of each area are realized

1. Take Kunming as an example-get the Kunming city scope on the Gaode map

Open the high lbs platform, and register and authenticate individual developers and log in

https://lbs.amap.com/api/webservice/guide/api/district/

Enter the parameters as shown in the figure below
Insert picture description hereto get the point-like boundary range, such as the polyline parameter in distinct in the figure below. We copy it to the world, find the coordinates of a small area and delete it, and then copy the remaining data into excel in rows, and then perform the column operation in excel, and add the x and y headers and save them as excel 97-2013xls files. To facilitate the next arcmap operation.

Here, the original author encountered the problem of abnormalities caused by a small area in the process of data processing in Shenzhen, but did not encounter the problem of data processing in Kunming, but encountered the problem of not being able to save all the data in the excel cell. Therefore, This article uses the csv format for storage, and the related data regularization operations are directly processed using python, which brings some slight differences.

Insert picture description here
The data regulation here is mainly: 1. Separate the latitude and longitude into columns; 2. Change the ";" to two new lines. The code data and results are as follows:

#%%

with open(r"\kunming_poi.csv",'rb') as csvfile:

  f.write('x,y')

  for i in csvfile:

     line_value = str(i,encoding='utf8')

     \#print(i,type(i))

csvfile.close()

 

\# %%

value_list = line_value.split(';')

print(value_list)

with open(r'result.csv','w') as f:

  for value in value_list:

     f.write(value)

     f.write('\n')

f.close()

Insert picture description hereConversion result
Insert picture description here

Second, ArcGIS converts coordinate points into spatial extent

Import the csv data into ArcGIS, and convert the data into SHP files, and then perform point-to-line operation on the data.

They are all routine operations, so I won't describe them. If you need to, you can leave a message and I will write an article to supplement it. You can refer to the original author's article

When importing data, the coordinate system needs to be set to WGS1984. Although it is different from the coordinate system of AutoNavi Map, the difference is not too big and there may be some errors, but it may be minimal in the study of prefecture-level cities.

3. Draw the fish net and get the coordinates of the center point of the fish net

The goal is to use the four-to-one range constructed by fishing nets, using the polygon POI retrieval function provided by Gaode Maps

  1. Fishnet generation

arctoolbox >> data management tools >> feature class >> create fishnet

Insert picture description hereConfigure ranks

Insert picture description hereFinally, the fishing net is shown in the figure
Insert picture description here, and the points outside the redundant grid are deleted, reducing the download of redundant data. The final result is as follows

Insert picture description here

Check the four-to-range extent of fish net, determine the four-to-four of each grid, and assign the four-to-latitude and longitude coordinates to the center point
Insert picture description here

  1. Calculate the latitude and longitude of the center point of the fishing net and the coordinates
    Insert picture description here
    of the four to the fourth coordinate of Kunming City, calculate the size of each fish net, as shown in the figure:
    Insert picture description here
    I set 10 grids horizontally, so the horizontal length of each grid is: (right-left )/10

I set 15 grids vertically, so the vertical grid length is: (top-bottom)/15

Since the point is the center point, because the coordinates of the center point are known in the previous step, the four points of each center point can be set in batches through the grid parameters of the same specification.

Top=latitute + (top-bottom)/15/2

Bottom=latitute - (top-bottom)/15/2

Left=logitute - (right-left)/10/2

Right=logitute +(right-left)/10/2

There is an error here, which may result in incomplete data acquisition. Just recalculate the upper, lower, left, and right center lines according to the generated grid (2021/01/07)

Examples of results are as follows:
Insert picture description here

Finally, attach the code in the reference article. I have modified some here, but there is a problem, that is, 100 pages are traversed every time, but sometimes there are not so many pages. I looked for an explanation, and there is no parameter to return the number of pages. , So the code efficiency can be further improved. (You can also use segmented concurrency)

Here you can request once to determine how many pages of data there are, and then make more requests, which will help reduce invalid requests and improve efficiency (2021/01/07)
——————————————————————————————
Unfortunately, the above method is not feasible, count does not represent the amount of data obtained. To save the number of requests and improve efficiency, you can only judge from the null value, otherwise it will cause problems such as data loss (January 9, 2021)

#%%
import pandas as pd
import requests
key = r'******************'   #高德地图开发者平台申请的key
keywords = "商场|超市|商店"
types = "060000"
url_head = r"https://restapi.amap.com/v3/place/polygon?"
text1 = pd.read_csv(r"昆明市范围_高德数据采集点.txt")  #从arcmap中导出的text文件地址
#%%
print(len(text1))
#输入表头
with open("poi_gaode.txt", 'a') as f:
    f.write('poi_name;poi_adname;poi_address;poi_location;poi_tel\n")
    f.close()
#%%
#for i in range(0, len(text1)):  #获取全部矩阵数据时,有可能超过高德地图配置的每天30000条数据限额,可选择先获取前35条,第二天再获取剩余数据

for i in range(51, 124):
	#经度和纬度用","分割,经度在前,纬度在后,坐标对用"|"分割,经纬度小数点后不得超过6位。
    con_location = str(round(text1["long_left"][i], 6)) + ',' + str(round(text1['lat_top'][i], 6)) + '|' + str(round(
        text1['long_right'][i], 6)) + ',' + str(round(text1['lat_bottom'][i], 6))
    
    url_ttl = url_head + "polygon=" + con_location + "&keywords=" + keywords + "&key=" + key + "&types=" + types + "&output=json&offset=20&extensions=all"
    # rep0 = requests.get(url_ttl)
    # json0 = rep0.json()
    # count0 = json0["count"]
    print("开始进行第%d个网格的数据提取." % i)
    # print("一共%d条数据" % int(count0))
    for p in range(1, 101):  #遍历100页
        url = url_ttl + '&page=' + str(p)
        rep1 = requests.get(url)
        json1 = rep1.json()
        pois_data = json1['pois']
        for poi in pois_data:
            poi_name = str(poi["name"])
            poi_adname = str(poi["adname"])
            poi_address = str(poi.get("address", "无详细地址"))
            poi_location = str(poi["location"])
            poi_tel = str(poi.get("tel", "无联系方式"))
            poi_info = poi_name + ";" + poi_adname + ";" + poi_address + ";" + poi_location + ";" + poi_tel + "\n"
            poi_info = poi_info.encode('gbk', 'ignore').decode("gbk", "ignore")    #避免编码错误
            with open("poi_gaode.txt", 'a') as f:
                f.write(poi_info)
            print("正在写入数据...")
print("数据提取完成")
f.close()

My business is not developed here, 30,000 calls are more than enough. .
Insert picture description here
The results are as follows:
Insert picture description here
Insert picture description hereThanks again to the original author for his contribution!
@wenlulan

Reference article: https://blog.csdn.net/weixin_47796965/article/details/108372378

Guess you like

Origin blog.csdn.net/u010472858/article/details/108486414