Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python to draw the dynamic Map of Metro Line between Beijing, Shanghai, Guangzhou and Shenzhen

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about how to use Python to draw the dynamic map of the subway line between Beijing, Guangzhou and Shenzhen. Many people may not understand it very well. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.

Today, we will teach you to use python to make a dynamic map of the north, Shanghai, Guangzhou, Shenzhen and subway lines.

Collection of coordinate points

Primary five has done similar geographic visualization before, but all of them use json data collected on the Internet. But a lot of data is actually outdated, even incorrect / incomplete. So we'd better do it ourselves and have plenty of food and clothing. Open Amap's subway web page

You can easily get the interface of Beijing subway data, and in the same way, you can copy the url of the other three cities. With api, data can be obtained by parsing json

Url = 'http://map.amap.com/service/subway?_1615466846985&srhdata=1100_drw_beijing.json'response = requests.get (url) result = json.loads (response.text) stations = [] for i in result [' l']: station = [] for an in i ['st']: station.append ([float (b) for b in a [' sl'] .split (',')]) stations.append (station) pprint.pprint (stations)

Pprint formatted print results for easy preview

Transformation of coordinate system

In fact, I have seen similar geographical visualization articles before, and as a result, I found that it was OK to zoom out, and as soon as I zoomed in, I would find that the coordinate points floated two miles away. Just take the coordinate points obtained above to show you the geographical location of the same longitude and latitude in different maps.

You can see that the longitude and latitude in Amap refers to the Jinanqiao subway station, but in Baidu map, the geographical location points to a building a few kilometers away. Why did this problem arise? In fact, it is caused by the geographical coordinate system of different map products.

The following is about the common geographical coordinate system: the earth coordinate system is an international universal coordinate system, which is more suitable for international map visualization. However, within the scope of our country, we generally do not use it directly, but use the Martian coordinate system encrypted by the State Bureau of surveying and Mapping. In addition, some companies will carry out secondary encryption on the Martian coordinate system, such as Baidu coordinate system, Sogou coordinate system and so on. I found a picture on the Internet:

The above picture can be used as a reference, but we will not study the specific reasons in detail. What is the point and how to use python to transform the coordinate system? For example, in this paper, we get the set of coordinate points in Amap, so we use the GCJ-02 coordinate system. The following visualization will call the interface of Baidu Map, that is, it needs to be visualized in the BD-09 coordinate system. Fortunately, I found the formula from GCJ-02 to BD-09 on the Internet, and realized this formula with python:

# set the two constants needed first pi = 3.1415926535897932384 # π r_pi = pi * 3000.0 def gcj02_bd09 (lon_gcj02,lat_gcj02): B = math.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]

In this way, we have written a function of python to convert GCJ-02 coordinate system into BD-09. By calling this function, we can convert all the coordinate points obtained by Amap into Baidu coordinate system.

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

Take one of the coordinate points as an example:

At this point, our preliminary data work is finally ready. Of course, if the data we obtained at the beginning is in the BD_09 (Baidu Map) coordinate system, the conversion step can be omitted directly.

Geographic visualization

The next step is to use BMap in pyecharts to visualize, but you need to get the key of Baidu's open platform first. Baidu Map Open platform

Copy the access application (AK) in the figure above and save it, which will be used in subsequent visualization. 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

After importing the data (that is, the latitude and longitude data result converted above), you can adjust the parameters and add some controls. Key parameters are annotated for easy viewing (Baidu appkey remembers to replace it with its own)

Map_b = (BMap (init_opts = opts.InitOpts (width = "800px", height = "600px")) .add _ schema (baidu_ak ='*', # Baidu Map Development and Application appkey center = [116.403963, 39.915119], # the center of the current view zoom = 10, # the scale of the current view is_roam = True # enable mouse zoom and move roaming) .add (series_name = "", type_ = ChartType.LINES, # set Geo diagram type data_pair = result, # data item is_polyline = True, # whether it is a Polyline In the case of lines drawing # linestyle_opts = opts.LineStyleOpts (color = "blue", opacity = 0.5, width = 1), # line style configuration item) .add _ control_panel (maptype_control_opts = opts.BMapTypeControlOpts (type_ = BMapType.MAPTYPE_CONTROL_DROPDOWN), # control for switching map types scale_control_opts = opts.BMapScaleControlOpts () # scale control overview_map_opts = opts.BMapOverviewMapControlOpts (is_open = True), # add thumbnail map navigation_control_opts = opts.BMapNavigationControlOpts () # Pan zoom control for map)) map_b.render (path = 'subway_beijing.html')

Note: because it is a map of Beijing, set the latitude and longitude of Tiananmen Square [116.403963, 39.915119] as the viewing angle center.

Let's take a look at the visualization results:

There are controls in the four corners of the image above, which are the control parameters we added to the code, namely: map pan zoom control, toggle map type control, thumbnail map, and scale control. Is it still rich?

Display of other effects

Above has basically achieved the use of python to make subway line dynamic map. However, if everyone uses the same color background to make dynamic pictures, it will be too monotonous. It just so happens that we have to draw the subway maps of the other three cities, so adjust some parameters and see what results can be achieved, right?

Shanghai-discoloration

The data interface in Shanghai is:

Http://map.amap.com/service/subway?_1615467204533&srhdata=3100_drw_shanghai.json

For the subway map of Shanghai, if we change the color of line, we can modify color in the parameter linestyle_opts. The line color in the image below is lilac-- light purple.

Guangzhou-satellite map

The data interface of Guangzhou is:

Http://map.amap.com/service/subway?_1615494419554&srhdata=4401_drw_guangzhou.json

In fact, we can also adjust the visual background to satellite images. However, this operation does not require additional code, because I just mentioned that I added four controls when adjusting the parameters, of which the one in the upper right corner can directly switch the map type, as shown in the following figure.

Shenzhen-personalized color matching

The data interface of Shenzhen is:

Http://map.amap.com/service/subway?_1615494473615&srhdata=4403_drw_shenzhen.json

If you are not satisfied with Baidu map set map background, we can also personalize the mapStyle, adjust their own color matching styleJson.

Summary

Today I took you to learn how to use python to draw the dynamic map of subway lines in first-tier cities. It is mainly divided into four parts: the collection of coordinate points, the conversion of coordinate system, the use of pyecharts geographic visualization, other effects display.

After reading the above, do you have any further understanding of how to use Python to draw the dynamic map of the subway line between Beijing, Shanghai, Guangzhou and Shenzhen? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report