Preface,

· 1. Climb the congestion index

· 2. Data visualization

· III. Build a display website

· Write at the end

preface

Just today, I felt the deep malice of the traffic jam. There is nothing wrong! I was stuck in traffic for almost 3 hours today, and a great date went up in smoke.

I’d love to see how clogged the road can get. So I crawled congestion data for cities and visualized them:

Special note: because the data is real-time, the drawing time has passed the peak, so it is not surprising that the picture is green.

Interested guest officer, you continue to look down, I give you slowly decompose. (PS. Crawlers, Pyecharts, flask, etc.)

First, climb the congestion index

A certain degree of intelligent transportation provides the data of congestion index of various cities, which we can easily capture with a few lines of code:

# Obtain the congestion index of each city

Url = ‘jiaotong.baidu.com/trafficinde… * interface API

res = requests.get(url)

data = res.json()

The URL is the address of the interface that obtains data, which can be known through simple packet capture analysis. Data is the returned data, which includes many fields, but we only need to extract the city name and congestion index:

# Extract data

Citys = [I [‘cityname’] fori in data[‘data’][‘list’]] #

Indexs = [float(I [‘index’]) fori in data[‘data’][‘list’]] #

Now that we have the data, we can visualize it.

Second, data visualization

Map the city and its congestion index using the Pyecharts library. Its installation is as follows:

pip install pyecharts

1,

Some versions require additional map libraries to be installed as follows:

pip install echarts-countries-pypkg

pip install echarts-cities-pypkg

pip install echarts-china-provinces-pypkg

pip install echarts-china-cities-pypkg

Define the map first:

geo = Geo()

Geo. Add_schema (mapType = ‘China ‘) #

Add data and set it:

Geo-add (‘ Urban congestion index ‘, zip(Citys, IndeXS),type_ = ‘effectScatter’) # set map type and data

· geo. Set_series_opts (label_opts = opts.labelopts (is_show = False)) #

According to the size of the congestion index, it can be classified as smooth, slow, congested and severe congestion:

geo.set_global_opts(visualmap_opts = opts.VisualMapOpts(

#max_ = 2.5, # for continuous representation

Is_piecewise = True, # whether piecewise

Pieces = [{‘ min ‘, 1.0 ‘Max’ : 1.5, ‘label’ : ‘open’, ‘color’ : ‘# 16 ce95’},

{‘ min ‘, 1.5 ‘Max’ : 1.8, ‘label’ : ‘go slow’, ‘color’ : ‘# F79D06’},

{‘ min ‘, 1.8 ‘Max’ : 2.0, ‘label’ : ‘jam’, ‘color’ : ‘# D80304’},

{‘ min ‘, 2.0 ‘Max’ : 2.5, ‘label’ : ‘serious congestion’, ‘color’ : ‘# 8 f0921}])) # set illustration shows

Finally save the map locally:

Geo. Render (path=’ congestion index.html’)

However, since congestion data changes in real time, wouldn’t it be a hassle if I had to run the code every time? Obviously, the clever socialist youth would not do this, and you read on.

Three, build a display website

In order to more conveniently display the congestion situation of each city, I decided to build a website for display. Methods can be various, here I choose to use the flask framework, simple and quick ~ my complete code is as follows:

# –– coding: utf-8 –“””

Created on Sun Nov 15 01:34:36 2020

@author: kimol_love

“””import requestsfrom pyecharts.charts import Geofrompyecharts importoptions as optsfromflask import Flask, render_template

def get_data():

‘ ‘ ‘

Get congestion index

‘ ‘ ‘

# Obtain the congestion index of each city

Url = ‘jiaotong.baidu.com/trafficinde… * interface API

res = requests.get(url)

data = res.json()

# Extract data

Citys = [I [‘cityname’] fori in data[‘data’][‘list’]] #

Indexs = [float(I [‘index’]) fori in data[‘data’][‘list’]] #

# return data

    return zip(citys,indexs)

    def get_geo():

‘ ‘ ‘

Access to map

‘ ‘ ‘

# Obtain the congestion index of each city

data = get_data()

# Draw scatter diagram

geo = Geo()

Geo. Add_schema (mapType = ‘China ‘) #

Geo. Add (‘ Congestion index by Kimol ‘, data,type_ = ‘effectScatter’) # set map type and data

Geo-set_series_opts (label_opts = opts.labelopts (is_show = False)) # Set whether to display tags

    geo.set_global_opts(visualmap_opts = opts.VisualMapOpts(

#max_ = 2.5, # for continuous representation

Is_piecewise = True, # whether piecewise

Pieces = [{‘ min ‘, 1.0 ‘Max’ : 1.5, ‘label’ : ‘open’, ‘color’ : ‘# 16 ce95’},

{‘ min ‘, 1.5 ‘Max’ : 1.8, ‘label’ : ‘go slow’, ‘color’ : ‘# F79D06’},

{‘ min ‘, 1.8 ‘Max’ : 2.0, ‘label’ : ‘jam’, ‘color’ : ‘# D80304’},

{‘ min ‘, 2.0 ‘Max’ : 2.5, ‘label’ : ‘serious congestion’, ‘color’ : ‘# 8 f0921}])) # set illustration shows

# back to map

    return geo

# define app

App = Flask(name)# define the main interface

@app.route(“/”)def hello():

geo = get_geo()

    return render_template(‘geo.html’,

                           mygeo=geo.render_embed())

    if__name__ == “main“:

# Run the project

    app.run()

Get_geo (), the function that gets the map, returns the map drawn by Pyecharts. Create the templates folder in the current directory and create the module file geo. HTML as follows:

    

Traffic congestion index by city

  {{mygeo|safe}}

At this point, visit the website address to see a congestion map

Write in the last

Take a look at the evil evening rush:

Finally, thank you for reading with great patience. See you next time

Reprinted by Kimol Jun (CSDN