用对开线绘制区域轮廓和网格线 - 2. 绘制矩形区域
最编程
2024-04-06 12:43:25
...
详情过程大家可以参考《想知道所在的城市有多少条道路?我用python发现北京一共有1.5万条道路!》,这里就放代码了,部分代码做简单讲解。
import requests
import pandas as pd
import os
url = 'https://restapi.amap.com/v3/config/district?'
key = '你的key'
keywords = '北京'
params = {
'key':key,
'keywords':keywords,
'subdistrict':0,
'extensions':'all',
}
r = requests.get(url,params=params)
data = r.json()
polyline = data['districts'][0]['polyline']
polyline_list = polyline.split(';')
df = pd.DataFrame(polyline_list,columns=['经纬度'])
df[['经度','纬度']] = df['经纬度'].str.split(',',n=1,expand=True).astype(float)
# 获取区域边界经纬度
latitude_max = df['经度'].max()
latitude_min = df['经度'].min()
longitude_max = df['纬度'].max()
longitude_min = df['纬度'].min()
# 北京行政区域边界经纬度点
locations = [[x[1],x[0]] for x in [list(eval(e)) for e in polyline_list]]
# 北京行政区域所在矩形区域
location = [[39.442758, 115.423411], [39.442758, 117.514625],
[41.060816, 117.514625], [41.060816, 115.423411],
[39.442758, 115.423411]]
由于高德api获取的经纬度是经度在前、纬度在后,但是folium
中纬度在前、经度在后。所以这里需要做简单的经纬度位置调换的操作,以满足需求格式。
绘制矩形区域:
drow_m(location,3,'black',0.8)
m
矩形区域