Big Data 13 min read

Fetching and Visualizing 30‑Minute Traffic Isochrones with Python and Amap API

This tutorial explains how to retrieve Amap traffic‑life‑circle data via its public API, decode district IDs, and use Python with the Folium library to visualize 30‑minute reachable areas on an interactive map, providing a practical workflow for geospatial data analysis.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Fetching and Visualizing 30‑Minute Traffic Isochrones with Python and Amap API

The article introduces the concept of a "traffic life circle" – the area reachable within a certain time (e.g., 30 minutes) using existing road conditions, and shows how to obtain the underlying data from Amap’s public APIs.

First, the API endpoint https://report.amap.com/ajax/life/circle.do is identified, with three parameters: districtId (region ID), dir (0 for origin, 1 for destination), and timeIndex (hour of the day). A Python function to request this data is provided:

<code>def get_data(disID):
    """Obtain traffic life‑circle data"""
    url = 'https://report.amap.com/ajax/life/circle.do'
    headers = {
        'User-Agent': 'Mozilla/5.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()
</code>

Because districtId values are opaque strings, another API https://report.amap.com/ajax/life/districts.do is used to map city codes to their district IDs. The corresponding helper function is:

<code>def get_id(cityID):
    """Obtain district IDs for a 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()
</code>

A third endpoint https://report.amap.com/ajax/getCityInfo.do returns the list of available cities and their codes, accessed via:

<code>def get_city():
    """Get city information (name, code, etc.)"""
    url = 'https://report.amap.com/ajax/getCityInfo.do'
    headers = {...}
    res = requests.get(url, headers=headers)
    return res.json()
</code>

With these three functions, the script first fetches the city list, lets the user select a city, retrieves its district IDs, and then obtains the traffic‑life‑circle data for a chosen district.

For visualization, the Folium library is used. After installing Folium ( pip install folium ), a helper reads the JSON file and extracts the coordinates for a specific time slice (e.g., 30 minutes):

<code>def read_data(path, Type=30):
    """Read traffic life‑circle data for the given 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]]
</code>

The coordinates are reordered (latitude/longitude swap) and the first point is appended to close the polygon. Then a Folium map is created, the start point is marked, and the polygon representing the 30‑minute reachable area is drawn:

<code>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('Location', max_width=1000), tooltip='Start').add_to(Map)
folium.Polygon(data, color='#7C9F59', fill=True).add_to(Map)
Map.save('30分钟交通生活圈.html')
</code>

Running the script generates an HTML file that visualizes the green area on the map, indicating how far one can travel from the chosen point within 30 minutes under current traffic conditions. The article concludes with suggestions for further analysis, such as computing maximum radii or comparing different cities.

PythonData VisualizationGeospatial AnalysisfoliumAmap APITraffic Isochrone
Python Programming Learning Circle
Written by

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.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.