In the work, often want to use similar to the geothermal map of the way data display, but the work environment is the intranet, can not be linked to the map online, no way, had to find their own ways to search the Internet border coordinates.
After a lot of documentation and trouble, finally found the way and the way the Gold Map API is the simplest, just beginners python, share the ~
(1) The API of the gold map integrates this function, as long as the parameters are set well, http://lbs.amap.com/api/webservice/guide/api/district
According to the German map, follow these three steps:
The first step is to apply the "Web Service API" key (key);
The second step, the mosaic HTTP request URL, the first step to apply for the key must be required to send together;
The third step is to receive the data returned by the HTTP request (JSON or XML format) and parse the data.
The example of Gould's official website also shows how to access, for example: http://restapi.amap.com/v3/config/district?keywords= Beijing &subdistrict=2&key=< User's key The results of the access to the JSON parsing, you can get the coordinates ~
Key application method is quite simple, this is not said, to Fuzhou as an example, its subordinate urban area has 5 administrative districts, there are 8 counties, explain how the next boundary coordinates output ~
(2) on the code, (beginners python, a lot of grammatical structure is still very unclear, here only for the implementation of functions, code written very vexed, readers PAT)
#-*-Coding:utf-8-*-# The first line must have, otherwise the report text Fu Fei ASCII error import urllib2 import NumPy as NP import JSON import pandas as PD from
Pandas Import Series,dataframe def getlnglat (address): url = ' http://restapi.amap.com/v3/config/district? ' #高德上申请的key key = ' * * * * uri = URL + ' keywords= ' + address + ' &key= ' + key + ' &subdistrict=1 ' + ' &e
Xtensions=all ' #访问链接后, the API is passed back to a JSON-formatted data temp = Urllib2.urlopen (uri) temp = json.loads (Temp.read ()) #polyline是坐标, name is the names of the zones Data = temp["Districts"][0][' polyline '] name = temp["Districts"][0][' name ') #polyl INE data is a whole plain text data, different geographical blocks according to the | points, the geographical information in the block according to, the horizontal ordinate according to, points, so to the text to be processed three times Data_div1 = Data.split (' | ')
#对结果进行第一次切割, according to the | symbol LEN_DIV1 = len (data_div1) #求得第一次切割长度 num = 0 len_div2 = 0 #求得第二次切割长度, i.e. the total length of the entire data
While num < len_div1:len_div2 = Len (data_div1[num].split (';')) num = 1 num = 0 num_base = 0 output = Np.zeros ((len_div2,5)). Astype (Np.float) #循环2次, split; with, while num < len_div1:temp = Data_div1[num].split (';') Len_temp = Len (temp) num_temp = 0 while num_temp < len_temp:output[num_temp+num_base,:2] =
Np.array (Temp[num_temp].split (', ')) #得到横纵坐标 output[num_temp+num_base,2] = num_temp + to get the connection order of the transverse ordinate
output[num_temp+num_base,3] = num + num_temp The ordinal number of the block = = 1 Num_base + = len_temp num + + 1
Output = Dataframe (output,columns=[' longitude ', ' latitude ', ' connection order ', ' block ', ' name ']) output[' name ' = name return output def getsubname (address): #获取搜索区域的名称, some areas such as drum tower name too many, so return to the city code, the city code as a parameter to the above function url = ' Http://restapi.amap.com/v3/confi
G/district? '
Key = ' * * * * uri = URL + ' keywords= ' + address + ' &key= ' + key + ' &subdistrict=1 ' + ' &extensions=all ' temp = Urllib2.urlopen (uri) temp = json.loads (Temp.read ()) List0 = temp[' districts '][0][' districts '] num_q U = 0 Output = [] While Num_qu < Len (list0): Output.append (list0[num_qu][' Adcode ']) Num_qu + 1
return output num = 0 ad = getsubname (' Fuzhou ') #得到福州下属区域的城市代码 add = Getlnglat (' Fuzhou ') #得到福州整个的边界数据 while Num < Len (AD): add = Pd.concat ([Add,getlnglat (Ad[num].encode ("Utf-8")]) #得到福州下属的全部区域的边界数据 num = 1 add.to_csv (' Add.csv ', Encodin g= ' GBK ') #输出到文件
(3) The output result can be obtained by the following data:
Get the tableau inside the drawing and find that even the outline of the border island is extremely clear: