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 implement folium Interactive Map by Python

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

In this issue, the editor will bring you about Python how to achieve folium interactive map. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

Leftlet provides a very useful interactive dynamic map interface for R language, and its API interface package in Python is called folium (I don't know why the package author has such a name, which has nothing to do with leaflet). It can meet our usual high-frequency visualization scenes such as thermal map, filled map, path map, scatter mark and so on.

This paper mainly introduces its application in three geographic information scenarios: point, line and polygon:

Import pandas as pd

Import numpy as np

Import os

Import folium

From folium import plugins

Import webbrowser

Import geopandas as gpPoint:full = pd.read_excel ("D:/Python/File/Cities2015.xlsx") full = full.dropna () by default the folium.Marker function is used to mark points in space, and the dot pattern is the shape of raindrops. But you can also use the folium.RegularPolygonMarker function to define the point style: schools_map = folium.Map (location= [full ['lat'] .mean (), full [' lon'] .mean (), zoom_start=10) marker_cluster = plugins.MarkerCluster () .add_to (schools_map) for name,row in full.iterrows (): folium.Marker ([row ["lat"], row ["lon"]], popup= "{0}: {1}" .format (row ["cities"] Row ["GDP"]) .add_to (marker_cluster)

# folium.RegularPolygonMarker ([row ["lat"], row ["lon"]], popup= "{0}: {1}" .format (row ["cities"], row ["GDP"]), number_of_sides=10,radius=5) .add_to (marker_cluster)

Display (schools_map)

# schools_map.save ('schools_map.html')

# webbrowser.open ('schools_map.html')

Display is used to display the interactive map in the editor. The save method can save the interactive map to the local disk in the form of a html file, and the webbrowser.open method can call the default browser to open the interactive map in the local html format.

Polygon:

Because the online map used by leaflet does not open the address matching function, which means that we can not directly enter the administrative district name to obtain the administrative division boundary, so when making filled maps, we still need to build local materials.

Mydata = pd.read_csv ("D:/R/rstudy/Province/geshengzhibiao.csv", encoding = 'gb18030') china_map = gp.GeoDataFrame.from_file ("D:/R/rstudy/CHN_adm/bou2_4p.shp", encoding =' gb18030')

# china_map = gp.GeoDataFrame.from_file ("D:/R/mapdata/State/china.geojson", encoding = 'gb18030')

China_map = folium.Map (location= [35120], zoom_start=4) China_map.choropleth (geo_data=open ('DJV peg columns=) display (China_map) China_map.save ("China_map.html")

Fortunately, the choropleth function of folium directly supports maps in json format, and only needs to provide the address of the material. The data should contain a mapping table consistent with the property sheet and geographic information boundary in the json material, and columns is used to specify the field name to be used. Key_on is used to specify the connection key (equivalent to the primary key) in the json map data and in the data you specify.

Fill_color can specify the colorBrewer color palette used for color matching.

Polyline

It is also relatively simple to make a graph in folium, simply by providing the folium.PolyLine function with a set of latitude and longitude points with nested lists or tuples.

Mydata1 = full.sample (20). Loc [:, ["lat", "lon"] .values.tolist () mydata2 = [(iPowerj) for iPowerj j in full.sample (20). Loc [:, ["lat", "lon"]] .values.tolist ()] oneUserMap = folium.Map (location= [40.0764116.2786], zoom_start=4) folium.PolyLine (mydata1,color = 'black'). Add_to (oneUserMap) display (oneUserMap) oneUserMap = folium.Map (location= [40.0764116.2786]) Zoom_start=4) folium.PolyLine (mydata2,color = 'black'). Add_to (oneUserMap) display (oneUserMap)

A practical path graph scenario can be constructed by adding some loop conditions: def map_fun (): myresult = full.loc [:, ["lat", "lon", "cities", "pop"]. Values oneUserMap = folium.Map (location= [40.0764116.2786], zoom_start=4)

For e in myresult: folium.RegularPolygonMarker ([e [0], e [1]], popup=str (e [2]) + ":" + str (e [3]), fill_color='#769d96', number_of_sides=10, radius=10). Add_to (oneUserMap) others = full.loc [full ['cities']! =' Zhengzhou', ["lat", "lon"] .values.tolist ()

For i in range (len (others)): zhengzhou = full.loc [full ['cities'] =' Zhengzhou', ["lat", "lon"]] .values.tolist () zhengzhou.append (others [I]) folium.PolyLine (locations = zhengzhou, color = 'black'). Add_to (oneUserMap) display (oneUserMap)

Return Noneif _ _ name__ = ='_ _ main__': map_fun ()

The above is the editor for you to share the Python how to achieve folium interactive map, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

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

Internet Technology

Wechat

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

12
Report