Use Python to draw your own personal footprint map, accurate to the city level

preface

The text and pictures of this article are from the Internet, only for learning and communication, not for any commercial purpose. The copyright belongs to the original author. If you have any questions, please contact us in time for handling.

Add Penguin Group 695185429 and you can get it for free. All the information is in the group file. Materials can be obtained, including but not limited to Python practice, PDF electronic documents, interview brochures, learning materials, etc

Two years ago, footprints map applet popular circle of friends, a time everyone popular drying their own travel map. However, the author recently experienced several small programs of footstep map, and found that although these small programs are called footstep map, they are only displayed to the provincial level at most, and can not be accurate to the city level. Therefore, the author spent some time on the weekend, using Python to draw his own personal footstep map, which can be accurate to the city level.

In the following section, the author will introduce how to draw a personal footprint map simply.

First, we need to install the following Python third-party modules:

echarts-china-cities-pypkg==0.0.9      
echarts-china-provinces-pypkg==0.0.3      
pyecharts==1.6.2
PyYAML==5.3.1 

 

The complete structure of the project is as follows:

 

 

                      

Beijing:
  -Yanqing District
  -Changping District
  -Haidian District
  -Dongcheng District
  -Xicheng District
  -Daxing District
  -Chaoyang District
 Shanghai:
  -Jiading District
  -Qingpu District
  -Songjiang District
  -Baoshan District
  -Minhang District
  -Fengxian District
  -Pudong New Area
  -Chongming District
  -Xuhui District
  -Huangpu District
  -Jing'an District
  -Yangpu District
  -Hongkou District
  -Jinshan District
  -Changning District
  -Putuo District
 Tianjin:
  -Binhai New Area
  -Nankai District
  -Hexi District
  -Zone of peace
 Hebei:
  -Baoding City
  -Langfang City
 Shandong:
  -Yantai City
  -Qingdao
 Jiangsu:
  -Nanjing
  -Suzhou
  -Yangzhou City
  -Zhenjiang City
  -Wuxi City
 Zhejiang:
  -Hangzhou
  -Shaoxing City
  -Ningbo City
  -Zhoushan City
  -Huzhou City
  -Jiaxing City
  -Taizhou City
  -Jinhua City
 Jiangxi:
  -Nanchang City
  -Jiujiang City
 Anhui:
  -Huangshan City
 Hubei:
  -Wuhan
 Hunan:
  -Changsha City
 Hainan:
  -Haikou City
  -Sanya City
 Sichuan:
  -Chengdu
 Guangdong:
  -Zhanjiang City

 

  then we use Python scripts to generate footstep maps. First is the national map, which is used to show the situation of provinces and municipalities directly under the central government_ map.py The code of is as follows:

g: utf-8 -*-
# author: Jclian91
# place: Pudong Shanghai
# time: 2020/5/31 9:21 morning
# Mapping personal footprints: nationwide
import yaml
from pyecharts.charts import Map
from pyecharts import options as opts

# Provinces and municipalities
with open("travel_config.yml", 'r', encoding='utf-8') as ymlfile:
    province_city_dict = yaml.safe_load(ymlfile)

province_dict = dict(zip(province_city_dict.keys(), [1]*len(province_city_dict.keys())))
print(province_dict)

# maptype='china' Show only municipalities and provinces directly under the central government
map = Map(init_opts=opts.InitOpts(width='1200px', height='800px'))
map.set_global_opts(
    title_opts=opts.TitleOpts(title="Personal footprint map"),
    visualmap_opts=opts.VisualMapOpts(max_=1, is_piecewise=True,
                                      pieces=[
                                        {"max": 1, "min": 1, "label": "been", "color": "#4EA397"},
                                        {"max": 0, "min": 0, "label": "Never been", "color": "#FFFFFF"},
                                        ])  #Maximum data range, segmented
                                     )
map.add("Personal footprint map", data_pair=province_dict.items(), maptype="china", is_roam=True)
map.render('whole country.html')

 

Running the above script will generate the national. HTML. To ensure that the image is centered, we set the following line in the HTML:

<div id="a438407081634ceab6af1c965034ae16" class="chart-container" style="width:1200px; height:800px;"></div>

 

Add the following to the style of:

margin:auto;top:30px

 

The national footprint map renderings are as follows:

Personal footprint map - National

 

Next, we will generate the footprint map of each province and use province_map.py Script, the code is as follows:

# -*- coding: utf-8 -*-
# author: Jclian91
# place: Pudong Shanghai
# time: 2020/5/31 10:01 morning
# Mapping personal footprints: Province and province
import yaml
from pyecharts.charts import Map
from pyecharts import options as opts

# Cities under provinces and municipalities
with open("travel_config.yml", 'r', encoding='utf-8') as ymlfile:
    province_city_dict = yaml.safe_load(ymlfile)


for province, cities in province_city_dict.items():

    map = Map(init_opts=opts.InitOpts(width='1200px', height='800px'))
    map.set_global_opts(
        title_opts=opts.TitleOpts(title="Personal footprint map-%s" % province),
        visualmap_opts=opts.VisualMapOpts(max_=1, is_piecewise=True,
                                          pieces=[
                                            {"max": 1, "min": 1, "label": "been", "color": "#4EA397"},
                                            {"max": 0, "min": 0, "label": "Never been", "color": "#FFFFFF"},
                                            ])  #Maximum data range, segmented
                                         )
    city_dict = dict(zip(cities, [1]*len(cities)))
    map.add("Personal footprint map-%s" % province, data_pair=city_dict.items(), maptype=province, is_roam=True)
    map.render('./provinces/%s.html' % province)
    print("Generate personal footprint map-%s success" % province)

 

At this time, the footprints map of each province or municipality will be generated in the provinces directory. We will modify each HTML separately to ensure that the image is centered. We only show three provinces and cities here:


Personal footprint area - Hainan

 

 

Personal footprint map - Jiangsu

 

 

Personal footprint map - Zhejiang Province

 

The last and most critical step is to get through the connection between the national footprint map and the footprint map of each province. That is to say, if we click the provinces that have been on the national footprint map, then we can click to check the footprint map of the provinces, such as which cities we have been to.


In this regard, we modify the generated National. html file, assuming that the image handle of the map is chart_a438407081634ceab6af1c965034ae16, then add the following content to the last < script > pair: (make sure to click the visited province to view the footprint map of the province)

  chart_a438407081634ceab6af1c965034ae16.on('click', function (param){
            var selected = param.name;
                if (selected) {
                    switch(selected){
                        case 'Beijing':
                            location.href = "provinces/Beijing.html";
                            break;
                        case 'Shanghai':
                            location.href = "provinces/Shanghai.html";
                            break;
                        case 'Tianjin':
                            location.href = "provinces/Tianjin.html";
                            break;
                        case 'Sichuan':
                            location.href = "provinces/Sichuan.html";
                            break;
                        case 'Anhui':
                            location.href = "provinces/Anhui.html";
                            break;
                        case 'Shandong':
                            location.href = "provinces/Shandong.html";
                            break;
                        case 'Jiangsu':
                            location.href = "provinces/Jiangsu.html";
                            break;
                        case 'Jiangxi':
                            location.href = "provinces/Jiangxi.html";
                            break;
                        case 'Hebei':
                            location.href = "provinces/Hebei.html";
                            break;
                        case 'Zhejiang':
                            location.href = "provinces/Zhejiang.html";
                            break;
                        case 'Hainan':
                            location.href = "provinces/Hainan.html";
                            break;
                        case 'Hubei':
                            location.href = "provinces/Hubei.html";
                            break;
                        case 'Hunan':
                            location.href = "provinces/Hunan.html";
                            break;
                        case 'Guangdong':
                            location.href = "provinces/Guangdong.html";
                            break;
                        default:
                            break;
                    }

            }
      });

Tags: Python encoding

Posted on Sun, 31 May 2020 02:08:36 -0700 by Leesa