This is probably the most comprehensive and detailed tutorial on the whole web.

Collection of coordinate points

Xiao Wu has done similar geographical visualizations before, but using JSON data collected on the web. But much of the data is actually out of date or even wrong/incomplete. So we’re better off doing it ourselves. Open amap’s metro website at…

You can easily get the interface of Beijing subway data, and similarly copy the url of the other three cities. With the API, parse the JSON to get the data 👇

url = ''
response = requests.get(url)
result = json.loads(response.text)
stations = []
for i in result['l']:
    station = []
    for a in i['st']:
        station.append([float(b) for b in a['sl'].split(',')])
Copy the code

Pprint formats the printed results for easy preview

Transformation of coordinate system

In fact, I have seen similar geographical visualization articles before, the results of their own try to find a narrow look is also good, an enlargement will find that the coordinate point floating out of the two miles 😂 just take the above coordinates to show you, look at the same latitude and longitude in different maps of the geographical location 👇

👆 you can see that the longitude and latitude on Amap refers to Jin anqiao subway station, while on Baidu Map, the location points to a building several kilometers away. Why is this a problem? It’s actually the geographic coordinates of different map products.

The earth coordinate system is an international universal coordinate system, which is more suitable for international map visualization. However, in China, it is generally not directly used, but the Mars coordinate system encrypted on its basis by the National Bureau of Surveying and Mapping. In addition, the company will carry out secondary encryption on the Mars coordinate system, such as baidu coordinate system, Sogou coordinate system and so on. I found a picture online:

The figure above can be used as a reference, but we won’t go into the specific reasons. What’s the point? How do you transform coordinate systems using Python? For example, in this paper, we obtained the set of coordinate points from the Map of Gaard, so we used the GCJ-02 coordinate system. In the following visualization, the interface of Baidu Map will be called, that is, it needs to be visualized in the BD-09 coordinate system. Luckily, I found the formula gCJ-02 to BD-09 on the Internet and implemented it in Python:

PI = 3.1415926535897932384 #π r_pi = PI * 3000.0/180.0 def gcj02_bd09(lon_gcj02,lat_gcj02) SQRT (lon_gcj02 * lon_gcj02 + lat_gcj02 * lat_gcj02) + 0.00002 * math.sin(lat_gcj02 * r_pi) o = math.atan2(lat_gcj02 , Lon_gcj02) + 0.000003 * math.cos(lon_gcj02 * r_pi) LON_bd09 = b * math.cos(o) + 0.0065 lat_bd09 = B * math.sin(o) + 0.006 return [lon_bd09 lat_bd09]Copy the code

So we have written a Python function that converts coordinate system GCJ-02 to BD-09. By calling this function, we can convert the set of coordinate points obtained by Amap to baidu coordinate system.

result = []
for station in stations:
    result.append([gcj02_bd09(*point) for point in station])
Copy the code

Take one of these coordinate points as an example:

At this point, our preliminary data work is finally ready. Of course, if we get the data in BD_09 coordinate system, the transformation step can be directly omitted

Geographic visualization

The next step is to use The BMap in Pyecharts to visualize, but we need to obtain the key of Baidu Open platform first. Baidu map open platform 👉…

Copy the access application (AK) from 👆 above and save it, which will be used in subsequent visualizations. We use BMap in Pyecharts to import the module first

from pyecharts.charts import BMap 
from pyecharts import options as opts 
from pyecharts.globals import BMapType, ChartType 
Copy the code

After importing the data (that is, the converted latitude and longitude result), you can adjust the parameters and add some controls. 👇 key parameters have been annotated, convenient for everyone to view (baidu Appkey remember to replace their own)

map_b = ( BMap(init_opts = opts.InitOpts(width = "800px", Height = "600 px")). Add_schema (baidu_ak = '* * * * * * * * * * * * * * * *', # baidu map application appkey center = [116.403963, 39.915119]. Is_roam = True). Add (series_name = "", type_ = charttype.lines, # set Geo graph type data_pair = result, # set data item is_polyline = True, # set Geo graph type data_pair = result, # set data item is_polyline = True, # linestyle_opts = opts.LineStyleOpts(color = "blue", opacity = 0.5, width = 1) # linestyle_opts = opts. Add_control_panel (mapType_Control_OPts = opts.bmAPTypecontrolopts (type_ = BMapType MAPTYPE_CONTROL_DROPDOWN), switch # map types of controls scale_control_opts = opts. BMapScaleControlOpts (), # scale control overview_map_opts = opts. BMapOverviewMapControlOpts (is_open = True), # to add a thumbnail map navigation_control_opts = opts. BMapNavigationControlOpts () # map zoom control) translation) map_b. Render (path = 'subway_beijing.html')Copy the code

Note: Because it is a Map of Beijing, the latitude and longitude of Tian ‘anmen [116.403963, 39.915119] is set as the perspective center.

Let’s take a look at the visualization results:

👆 The four corners in the figure above all have controls. This is because we added control parameters in the code. They are: pan/zoom map control, switch map type control, thumbnail map, and scale control. Is it still rich to

Other effects display

The above has basically realized using Python to make dynamic map of subway line. However, when everyone uses the same color background to create a dynamic map, it becomes too monotonous. Since we have three other cities to map, let’s tweak some parameters and see what happens.

Shanghai – Discoloration

The data interface of Shanghai is:
Copy the code

Let’s change the color of line. You can change the color in parameter LINestyLE_OPTS. 👇 The color of the lines in the image below is LILAC — light purple

Guangzhou – Satellite image

The data interface of Guangzhou is:
Copy the code

In fact, we can adjust the background of the visualization to a satellite image. There is no extra code to do this, as I added four controls to adjust the parameters, the one in the upper right corner of which can directly switch the map type. See the following figure for details.

Shenzhen – Personalized color matching

The data interface of Shenzhen is:
Copy the code

If you are not satisfied with the map background set by Baidu Map, you can also customize mapStyle and adjust your color matching styleJson.


Today we have learned how to use Python to draw subway line giFs in first-tier cities. It is mainly divided into four parts: the collection of coordinate points, the transformation of coordinate system, the use of Pyecharts geographical visualization, and other effects display.