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 install Python mapping artifact folium

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "how to install Python mapping artifact folium". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn how to install Python mapping artifact folium!

Introduction and installation of folium

Folium is based on the data application ability of Python ecosystem and the mapping ability of Leaflet.js library. It manipulates data in Python and then visualizes it in Leaflet map through folium.

Compared with the domestic Baidu pyecharts, folium is more flexible, can customize the rendering area, and the presentation form is more diversified. [1]

1. Brief introduction of folium

Folium is an open source library based on the data collation Datawrangling capability of Python ecosystem and the mapping ability of Leaflet.js library. Use Python to process the data, and then use Folium to visualize it on Leaflet maps. Folium can easily visualize the data processed by Python on interactive Leaflet maps. It can not only display the distribution map of the data on the map, but also use Vincent/Vega to mark it on the map.

This open source library contains many built-in map elements from OpenStreetMap, MapQuestOpen, MapQuestOpen Aerial, Mapbox and Stamen, and supports customizing personalized map elements using Mapbox or Cloudmade's API key. Folium supports the overlay of GeoJSON and TopoJSON file formats, you can also connect data to the overlay layer of both file formats, and finally you can create a distribution map using the color-brewer color scheme.

Folium allows you to use Python's powerful ecosystem to process data and then display it with Leaflet maps. Folium has built-in some map elements (tilesets) from OpenStreetMap, MapQuest Open, MapQuest Open Aerial, Mapbox, and Stamen, and supports customization of map elements with Mapbox or Cloudmade API keys. Folium supports GeoJSON and TopJSON overlay (overlays), binding data to create a hierarchical statistical chart (Choropleth map). However, when the Folium library draws a heat map, it needs to be networked before it can be displayed.

two。 Install folium

Pip install folium-I http://pypi.douban.com/simple-- trusted-host pypi.douban.com

? The domestic Douban source is directly used here.

II. Basic use

The class of the folium display map is folium.Map, and the class declaration is as follows:

Class folium.Map (location=None, width='100%', height='100%', left='0%', top='0%', position='relative', tiles='OpenStreetMap', attr=None, min_zoom=0, max_zoom=18, zoom_start=10, min_lat=-90, max_lat=90, min_lon=-180, max_lon=180, max_bounds=False, crs='EPSG3857', control_scale=False, prefer_canvas=False, no_touch=False, disable_3d=False, png_enabled=False, zoom_control=True, * * kwargs)

Several important parameters:

Location: latitude and longitude, list or tuple format, latitude, longitude

Zoom_start: scaling value. Default is 10. The larger the value, the smaller the scale, and the greater the magnification of the map.

Control_scale: Bool type, which controls whether to add a scale bar to the map. The default is False, which means no addition.

Tiles: display style. The default is "OpenStreetMap", that is, street display is enabled.

Crs: Geographic coordinate reference system, default is "EPSG3857"

1. Map at all levels

World map

Import folium print (folium.__version__) # define the world map world_map = folium.Map () # save world map world_map.save ('test_01.html')

The results are as follows:

Current folium version: 0.11.0

National map

Import folium # define the national map national_map = folium.Map (location= [35.3,100.6], zoom_start=4) # save national map national_map.save ('test_02.html')

The results are as follows:

City map

In fact, to change the map display is to change the longitude, latitude and zoom ratio of the display. The provincial, municipal and county levels are similar in usage. Here is a municipal example, such as Beijing:

Import folium # define the national map city_map = folium.Map (location= [39.93,116.40], zoom_start=10) # save national map city_map.save ('test_03.html')

two。 Mark on a map

Common mark

Add a normal tag with Marker, you can choose the pattern of the tag.

Import folium bj_map = folium.Map (location= [39.93,115.40], zoom_start=12, tiles='Stamen Terrain') folium.Marker (location= [39.95,115.33], popup='Mt. Hood Meadows', icon=folium.Icon (icon='cloud'). Add_to (bj_map) folium.Marker (location= [39.96,115.32], popup='Timberline Lodge', icon=folium.Icon (color='green')). Add_to (bj_map) folium.Marker (location= [39.93,115.34], popup='Some Other Location', icon=folium.Icon (color='red') Icon='info-sign') # Mark color icon) .add_to (bj_map) bj_map.save ('test_04.html')

The results are as follows:

Circular mark

Add circle mark with Circle and CircleMarker

Import folium bj_map = folium.Map (location= [39.93,116.40], zoom_start=12, tiles='Stamen Toner') folium.Circle (radius=200, location= (39.92,116.43), popup='The Waterfront', color='#00FFFF', # color fill=False, # fill). Add_to (bj_map) folium.CircleMarker (location= (39.93,116.38), radius=50 # the radius of the circle popup='Laurelhurst Park', color='#FF1493', fill=True, fill_color='#FFD700') .add_to (bj_map) bj_map.save ('test_05.html')

The results are as follows:

Dynamically place markup

Import folium dynamic_tagging = folium.Map (location= [46.8527,-121.7649], tiles='Stamen Terrain', zoom_start=13) folium.Marker ([46.8354,-121.7325], popup='Camp Muir') .add_to (dynamic_tagging) dynamic_tagging.add_child (folium.ClickForMarker (popup='Waypoint')) dynamic_tagging.save ('test_06.html')

The results are as follows:

For more details, please refer to the official document: http://python-visualization.github.io/folium/quickstart.html[2]

III. Actual combat cases

In order to visualize the parking lot geographic location data on the map example, familiar with the use of folium map visualization.

1. Get longitude and latitude data

The geographical location data of parking lot comes from the network, and the data is true and reliable. Let's first use Python crawler to get the data.

# data source: http://219.136.133.163:8000/Pages/Commonpage/login.aspx import requests import csv import json import logging headers = {'Xmure Requestedmurthood:' XMLHttpRequest', 'User-Agent':' Mozilla/5.0 (Windows NT 10.0) WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'} logging.basicConfig (level=logging.INFO, format='% (asctime) s -% (levelname) s:% (message) s') url = 'http://219.136.133.163:8000/Pages/Commonpage/AsyGetData.asmx/GetParkList' s = requests.session () s.get (url, headers=headers) for i in range (1,318): data = {' cp': str (I) 'ps':' 10', 'kw':', 'lon':' undefined', 'lat':' undefined', 'type':' undefined'} url = 'http://219.136.133.163:8000/Pages/Commonpage/AsyGetData.asmx/GetParkList' # post submit form data res = s.post (url, data=data) Headers=headers) # reset the encoding res.encoding = 'utf-8' # str to json to facilitate data extraction result = json.loads (res.text) [' Result'] for j in result: park_name = j ['ParkName'] Lon = j [' Longitude'] Lat = j ['Latitude'] with open (' parkings.csv', 'ajar, newline='' Encoding='gb18030') as f: f_csv = csv.writer (f) f_csv.writerow ([park_name, Lon, Lat]) logging.info ([park_name, Lon, Lat])

The results are as follows:

Geographical location data of 3170 parking lots

2. Folium Map Visualization

Import pandas as pd import folium # read csv data data = pd.read_csv ('parkings.csv', encoding='gbk') # incoming latitude and longitude data park_map = folium.Map (location= [data [' latitude'] .mean (), data ['longitude'] .mean ()], zoom_start=10, control_scale=True,) # instantiate folium.map.FeatureGroup object incidents = folium.map.FeatureGroup () for name Row in data.iterrows (): incidents.add_child (folium.CircleMarker (# CircleMarker represents a flower circle (row ["latitude"], row ["longitude"]), # latitude and longitude coordinates of each parking lot radius=7, # circle radius color='#FF1493' # the outer ring color of the logo fill=True, # whether to fill fill_color='#00FF00', # fill color fill_opacity=0.4 # fill transparency)) park_map.add_child (incidents) park_map.save ('park_map1.html')

The effect is as follows:

This looks a little messy. Let's count the number of parking lots in each area.

Import pandas as pd import folium from folium import plugins data = pd.read_csv ('parkings.csv', encoding='gbk') park_map = folium.Map (location= [data [' latitude'] .mean (), data ['longitude'] .mean ()], zoom_start=10, control_scale=True,) marker_cluster = plugins.MarkerCluster (). Add_to (park_map) for name,row in data.iterrows (): folium.Marker (location= [row ["latitude"] Row ["longitude"]) .add_to (marker_cluster) park_map.save ('park_map2.html')

The effect is as follows:

In this way, the number of local parking lots can be counted on the map, and after the map is constantly enlarged, the specific location of each parking lot can be displayed, which is very convenient.

At this point, I believe you have a deeper understanding of "how to install Python mapping artifact folium". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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