python realizes the moving map of bus routes in Beijing [must collect]

Today, I will teach you how to use pyecharts to make a dynamic map of Beijing's bus lines. This should be the only tutorial on the entire network that can run normally.

1. Obtain Baidu secret key

First of all, this project needs to refer to the Baidu map api, so you need to register to obtain the Baidu open platform key, the address is:

http://lbsyun.baidu.com/apiconsole/key#

 

 

If you have an account, log in directly. If you don't have an account, register first and then log in. After logging in, click Console - Application Management - My Application - Create Application:

The access application (AK) is the key we need this time

2. Organize bus geographic data

This bus geographic data is really a bit pitted. The official data given by echarts looks like this:

It looks like a password, which is really annoying

I had to bite the bullet and study the official code:

$.getJSON(uploadedDataURL, function(data) {
    var hStep = 300 / (data.length - 1);
    var busLines = [].concat.apply([], data.map(function (busLine, idx) {
        var prevPt;
        var points = [];
        for (var i = 0; i < busLine.length; i += 2) {
            var pt = [busLine[i], busLine[i + 1]];
            if (i > 0) {
                pt = [
                    prevPt[0] + pt[0],
                    prevPt[1] + pt[1]
                ];
            }
            prevPt = pt;

            points.push([pt[0] / 1e4, pt[1] / 1e4]);
        }
        return {
            coords: points,
            lineStyle: {
                normal: {
                    color: echarts.color.modifyHSL('#5A94DF', Math.round(hStep * idx))
                }
            }
        }

 


this is a paragraph java The code, if you don’t understand it, don’t read it. It roughly means to divide the data by 10000, and then add the odd digits of the list and the even digits in turn. The two-by-two group is the geographic coordinates of each bus stop. Each list represents 1 line.

The above process is implemented in python, and the code is as follows:

import json
with open('1.json','r') as f:
    datas=json.load(f)
result=[]
for data in datas:
    data = [float(i / 10000) for i in data]
    a=[]
    for i in range(2,len(data),2):
        data[i]=data[i-2]+data[i]
        data[i+1] = data[i - 1] + data[i+1]
        a.append([data[i],data[i+1]])
    result.append(a)

 


still feel python less code

3. Drawing

Here are two ways for you

BAIDU_MAP_AK = "Enter your own key"

c = (
    BMap(init_opts=opts.InitOpts(width="1200px", height="800px"))
    .add_schema(
        baidu_ak=BAIDU_MAP_AK,
        center=[116.40, 40.04],
        zoom=10,
        is_roam=True,
    )
    .add(
        "",
        type_="lines",
        is_polyline=True,
        data_pair=result,
        linestyle_opts=opts.LineStyleOpts(opacity=0.2, width=0.5,color='red'),
        # If it is not the latest version, you can comment the following parameters (the effect is not much different)
        progressive=200,
        progressive_threshold=500,
    )
)
c.render_notebook()

 


2. Without map background

BAIDU_MAP_AK = "Enter your own key"

c = (
    BMap(init_opts=opts.InitOpts(width="1200px", height="800px"))
    .add_schema(
        baidu_ak=BAIDU_MAP_AK,
        center=[116.40, 40.04],
        zoom=10,
        is_roam=True,
        map_style={
            "styleJson": [
                {
                    "featureType": "water",
                    "elementType": "all",
                    "stylers": {"color": "#031628"},
                },
               
           "Omit part of the decoration code"
    )
    .add(
        "",
        type_="lines",
        is_polyline=True,
        data_pair=result,
        linestyle_opts=opts.LineStyleOpts(opacity=0.2, width=0.5,color='red'),
        # If it is not the latest version, you can comment the following parameters (the effect is not much different)
        progressive=200,
        progressive_threshold=500,
    )
)
c.render_notebook()

 


Which one do you think looks better? Welcome to leave a message in the comment area

For bus route data and complete code, please scan the code to follow the official account and reply to the bus map to get it

Welcome to the public account: Python crawler data analysis and mining, reply to [open source source code] to get more open source project source code for free

The public account updates python knowledge and [free] tools daily

 

Posted by phpvn.org on Mon, 16 May 2022 07:42:41 +0300