如何从高德获取乡镇边界数据?
从高德获取乡镇边界数据,可以通过高德地图开放平台的Web API实现。具体步骤如下:
1. 在高德开放平台申请Web API Key。
2. 使用“行政区划查询”接口,传入需要查询的城市名称和级别参数,调用高德地图API,即可获得该城市内所有区、县、乡镇/街道等各级行政区域的边界坐标点集合。
以下是Python代码实例:
```python import requests import json
# Web API Key key = your_api_key
# 需要查询的城市名称 city = 北京市
# 级别参数(subdistrict = 0 表示不返回下级行政区) level = township
# 构造URL请求参数 params = { key: key, keywords: city, subdistrict: 0, extensions: all }
# 调用高德地图API获取行政区划信息 response = requests.get(https://restapi.amap.com/v3/config/district, params=params) result = json.loads(response.text)
# 解析结果中的各级行政区域边界坐标点集合 for district in result[districts][0][districts]: if district[level] == level: boundaries = district[polyline].split(|) for boundary in boundaries: points = boundary.split(;) coordinates = [] for point in points: lng, lat = point.split(,) coordinate = [float(lng), float(lat)] coordinates.append(coordinate) print(coordinates) ```