Retrieving and Visualizing 30‑Minute Traffic Isochrones with Amap API and Folium in Python
This article demonstrates how to use Python to fetch Amap traffic iso‑chrones via API calls, process city and district IDs, and visualize the 30‑minute travel range on an interactive map with Folium, providing complete code snippets for data acquisition, parsing, and map rendering.
The concept of a "traffic life circle" defines the area reachable within a given travel time (e.g., 30 minutes) using existing road conditions and vehicles. By querying Amap’s public API, one can obtain the iso‑chrones for various time thresholds.
Using the browser’s F12 network panel, the endpoint for the iso‑chrones was discovered:
https://report.amap.com/ajax/life/circle.doThe request requires three parameters: districtId (area ID), dir (0 for origin, 1 for destination), and timeIndex (hour of the day). The following function encapsulates the request:
def get_data(disID):
"""Obtain traffic life‑circle data"""
url = 'https://report.amap.com/ajax/life/circle.do'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:84.0) Gecko/20100101 Firefox/84.0',
'Accept': 'application/json, text/javascript, */*; q=0.01',
'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
'X-Requested-With': 'XMLHttpRequest',
'Connection': 'keep-alive',
'TE': 'Trailers'
}
hour = time.strftime('%H', time.localtime())
params = (('districtId', disID), ('dir', '0'), ('timeIndex', hour))
res = requests.get(url, headers=headers, params=params)
return res.json()Two additional helper functions retrieve the mapping between city names and their IDs, and between city IDs and district IDs:
def get_city():
"""Fetch city information (name, code, etc.)"""
url = 'https://report.amap.com/ajax/getCityInfo.do'
headers = {...}
res = requests.get(url, headers=headers)
return res.json()
def get_id(cityID):
"""Obtain district IDs for a given city"""
url = 'https://report.amap.com/ajax/life/districts.do'
headers = {...}
params = (('cityCode', cityID),)
res = requests.get(url, headers=headers, params=params)
return res.json()The API returns a JSON list containing five sub‑lists, each representing the reachable area for 20, 30, 45, 60, and 90 minutes respectively, plus an additional element that stores the geographic centre of the district.
To analyse the 30‑minute iso‑chrones, the Folium library is used for interactive map rendering. First install Folium:
pip install foliumA helper reads the stored JSON file and extracts the desired time slice:
def read_data(path, Type=30):
"""Read traffic life‑circle data for a specific time (20,30,45,60,90)"""
typeMap = {20:0, 30:1, 45:2, 60:3, 90:4}
with open(path, 'r') as f:
data = json.load(f)
center = data[-1]
return center, data[typeMap[Type]]After loading the data (e.g., for Chengdu’s Niuwangmiao), the coordinate order is swapped to (lon, lat) and the first point is appended to close the polygon:
center, data = read_data('./data/成都/牛王庙.json')
data.append(data[0])
for i in range(len(data)):
data[i] = [data[i][1], data[i][0]]The map is then created, the start point marked, and the 30‑minute polygon drawn:
Map = folium.Map(location=[center[1], center[0]], zoom_start=11,
tiles='http://webrd02.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=7&x={x}&y={y}&z={z}',
attr='default')
folium.Marker([center[1], center[0]], popup=folium.Popup('牛王庙', max_width=1000), tooltip='出发点').add_to(Map)
folium.Polygon(data, color='#7C9F59', fill=True).add_to(Map)
Map.save('30分钟交通生活圈.html')Opening the generated HTML shows a green polygon indicating the area reachable from the chosen point within 30 minutes under current traffic conditions. The tutorial concludes with suggestions for further exploration, such as computing maximum radii or comparing different cities.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.