Python map library (1)—folium

Table of contents

Regional Mapping 

world map

country map

City map

map style

map marker

Normal mark (icon mark)

circle mark

Click the mouse to get the latitude and longitude

dynamic markup

Heat map drawing

Density Mapping

save map


In order to facilitate the display of the map, by installing jupyter, it will be displayed directly on the web page

Install Jupyter: pip install jupyter

Run Jupyter, after executing jupyter-notebook on the command line, it will directly open the web page ( http://localhost:8888/tree ), displaying the file directory under the corresponding path

重点参数:
class folium.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)

location 经纬度,list 或者 tuple 格式,顺序为 latitude, longitude
zoom_start 缩放值,默认为 10,值越大比例尺越小,地图放大级别越大
tiles 显示样式,默认'OpenStreetMap',也就是开启街道显示
crs 地理坐标参考系统,默认为"EPSG3857"

Regional Mapping 

world map

import folium

world_map = folium.Map()
world_map

country map

national_map = folium.Map(location=[30.3, 100.6], zoom_start=4)
national_map

City map

To change the map display is to change the latitude and longitude and zoom ratio of the display, and the usage at the provincial, city, and county levels is the same

city_map = folium.Map(location=[39.93, 116.40], zoom_start=10)
city_map

map style

In addition to the above-mentioned normal map display, folium also provides a very rich and diverse display. The variable that controls the display effect is tiles, and the styles include OpenStreetMap, Stamen Terrain, Stamen Toner, etc.

# city_map = folium.Map(location=[34.36,108.93], zoom_start=10, tiles='OpenStreetMap')  # 默认样式OpenStreetMap
city_map = folium.Map(location=[34.36,108.93], zoom_start=10, tiles='Stamen Terrain')
city_map

city_map = folium.Map(location=[34.36,108.93], zoom_start=10, tiles='Stamen Toner')
city_map

 

map marker

Normal mark (icon mark)

Marker can be used to add ordinary markers, and the pattern of the marker can be selected 

m = folium.Map(location=[34.36,108.93], zoom_start=10)
folium.Marker(
    location=[35.36,107.93],
    popup='Mt. Hood Meadows',  # # 图标说明,点击图标显示
    icon=folium.Icon(icon='cloud')
).add_to(m)

folium.Marker(
    location=[35.06, 107.32],
    popup='Timberline Lodge',
    icon=folium.Icon(color='green')
).add_to(m)

folium.Marker(
    location=[35.00, 107.01],
    popup='Some Other Location',
    icon=folium.Icon(color='red', icon='info-sign')
).add_to(m)
m

circle mark

Add circle markers with Circle and CircleMarker

Circle mark, the size of this mark changes through the zoom circle of the map, and the circled area remains unchanged

map = folium.Map(location=[34.36,108.93], zoom_start=10)
folium.Circle(
    radius=10000,
    location=[34.37, 108.83],
    popup='Circle标记,这种标记通过地图缩放圈的大小随之改变,圈选区域不变',
    color='crimson',
    fill=True,   # 填充
).add_to(map)
map

CircleMarker marking method, the size of the circle of this marker zoom map remains unchanged, and the circled area will change

map = folium.Map(location=[34.36,108.93], zoom_start=10)
folium.CircleMarker(
    location=[34.92, 108.90],  # 标记位置坐标
    radius=50,  # 圆的半径
    popup='CircleMarker标记方式,这种标记缩放地图圈的大小不变,圈选区域会改变',
    color='#3186cc',  # 标记外圈颜色
    fill=True,  # 是否填充
    fill_color='#3186cc',  # 填充色
    fill_opacity=0.4  # 填充透明度
).add_to(map)
map

Click the mouse to get the latitude and longitude

m = folium.Map(location=[34.36,108.93],zoom_start=13)
m.add_child(folium.LatLngPopup())
m

dynamic markup

m = folium.Map(location=[34.36,108.93],tiles='Stamen Terrain',zoom_start=13)
m.add_child(folium.ClickForMarker(popup='新增标记'))
m

Heat map drawing

import numpy as np
import folium
from folium.plugins import HeatMap
data = (
    np.random.normal(size=(100, 3)) *
    np.array([[0.1, 0.1, 0.1]]) +
    np.array([[34.36,108.93, 1]])
).tolist()

m = folium.Map([34.93, 108.38],zoom_start=8)
HeatMap(data).add_to(m)
m

Density Mapping

import folium
from folium.plugins import MarkerCluster

m = folium.Map([34.36,108.93], tiles='stamentoner', zoom_start=10)

# create a mark cluster object
marker_cluster = MarkerCluster().add_to(m)

# add data point to the mark cluster
for lat, lng, label in data:
    folium.Marker(
        location=[lat, lng],
        icon=None,
        popup=label,
    ).add_to(marker_cluster)

# add marker_cluster to map
m.add_child(marker_cluster)

save map

Save the map as a web page, which can be opened and viewed directly in the browser

import folium

city_map = folium.Map(location=[34.36,108.93], zoom_start=10, tiles='Stamen Terrain')
city_map.save('地图.html')

Guess you like

Origin blog.csdn.net/JBY2020/article/details/127361691