(编辑:jimmy 日期: 2024/12/25 浏览:2)
今天给大家介绍一个非常 NB 的Python 库,专门用来绘制地图的,它叫 Folium 。
Folium是一个基于leaflet.js的Python地图库,其中,Leaflet是一个非常轻的前端地图可视化库。即可以使用Python语言调用Leaflet的地图可视化能力。它不单单可以在地图上展示数据的分布图,还可以使用Vincent/Vega在地图上加以标记。Folium可以让你用Python强大生态系统来处理数据,然后用Leaflet地图来展示。
Folium中有许多来自OpenStreetMap、MapQuest Open、MapQuestOpen Aerial、Mapbox和Stamen的内建地图元件,而且支持使用Mapbox或Cloudmade的API密钥来定制个性化的地图元件。Folium支持GeoJSON和TopoJSON两种文件格式的叠加,也可以将数据连接到这两种文件格式的叠加层,最后可使用color-brewer配色方案创建分布图。
地图的生成
img
folium.folium.Map()详解
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)
参数说明:
**kwargs
:Leaflets地图类的其他参数: https://leafletjs.com/reference-1.5.1.html#map“tiles”的自定义设置:
img
地球上同一个地理位置的经纬度,在不同的坐标系中,会有少量偏移,国内目前常见的坐标系主要分为三种:
所以在设置“tiles”时需要考虑目前手中得经纬度属于那种坐标系。
由于投影坐标系中没有GCJ-02和BD-09对应的标识,所以在自定义瓦片时主要经纬度能匹配上,crs中的设置可保持不变。更多详情介绍请看:瓦片坐标系学习
如果需要将地图保存,只需执行:m.save(“map.html”) 即可。
添加点
import folium m = folium.Map(location=[39.917834, 116.397036], zoom_start=13, width='50%',height='50%', zoom_control='False', tiles='http://webrd02.is.autonavi.com/appmaptile"text-align: center">img
Folium.Icon类可以设置color, icon_color, icon, angle, prefix这5个参数:
- color的可选项包括:[‘red', ‘blue', ‘green', ‘purple', ‘orange', ‘darkred', ‘lightred', ‘beige', ‘darkblue', ‘darkgreen', ‘cadetblue', ‘darkpurple', ‘white', ‘pink', ‘lightblue', ‘lightgreen', ‘gray', ‘black', ‘lightgray'] ,或者HTML颜色代码
- icon_color同上
- icon可以在Font-Awesome网站中找到对应的名字,并设置prefix参数为'fa'
- angle以度为单位设置
其他:
m.add_child(folium.LatLngPopup()) #显示鼠标点击点经纬度 m.add_child(folium.ClickForMarker(popup='Waypoint')) # 将鼠标点击点添加到地图上添加圆
folium.Circle( radius=300, location=[39.928614,116.391746], popup='北海公园', color='crimson', fill=False, ).add_to(m) folium.CircleMarker( location=[39.942143,116.382590], radius=50, popup='后海公园', color='#3186cc', fill=True, fill_color='#3186cc' ).add_to(m)img
Circle和CircleMarker的不同:CircleMarker的radius一个单位是像素,Circle的一个单位时米
添加线段
folium.PolyLine([ [39.917834,116.397036], [39.928614,116.391746], [39.937282,116.403187], [39.942143,116.382590] ],color='red').add_to(m)添加多边形
folium.Marker([39.917834,116.397036], popup='故宫').add_to(m) folium.Marker([39.928614,116.391746], popup='北海公园').add_to(m) folium.Marker([39.937282,116.403187], popup='南锣鼓巷').add_to(m) folium.Marker([39.942143,116.382590], popup='后海公园').add_to(m) folium.Polygon([ [39.917834,116.397036], [39.928614,116.391746], [39.942143,116.382590], [39.937282,116.403187], ],color='blue', weight=2, fill=True, fill_color='blue', fill_opacity=0.3).add_to(m)Folium的其他高级应用
在地图上显示前200条犯罪数据
import folium import pandas as pd san_map = folium.Map(location=[37.77, -122.42], zoom_start=12,width='50%',height='50%') # cdata = pd.read_csv('https://cocl.us/sanfran_crime_dataset') cdata = pd.read_csv('Police_Department_Incidents_-_Previous_Year__2016_.csv') #犯罪数据,包含犯罪所在经纬度 # get the first 200 crimes in the cdata limit = 200 data = cdata.iloc[0:limit, :] # Instantiate a feature group for the incidents in the dataframe incidents = folium.map.FeatureGroup() # Loop through the 200 crimes and add each to the incidents feature group for lat, lng, in zip(cdata.Y, data.X): incidents.add_child( folium.CircleMarker( [lat, lng], radius=7, # define how big you want the circle markers to be color='yellow', fill=True, fill_color='red', fill_opacity=0.4 ) ) san_map.add_child(incidents)统计区域犯罪总数
from folium import plugins # let's start again with a clean copy of the map of San Francisco san_map = folium.Map(location=[37.77, -122.42], zoom_start=12,width='50%',height='50%') # instantiate a mark cluster object for the incidents in the dataframe incidents = plugins.MarkerCluster().add_to(san_map) # loop through the dataframe and add each data point to the mark cluster for lat, lng, label, in zip(data.Y, data.X, cdata.Category): folium.Marker( location=[lat, lng], icon=None, popup=label, ).add_to(incidents) # add incidents to map san_map.add_child(incidents)以热力图的方式呈现
from folium.plugins import HeatMap san_map = folium.Map(location=[37.77, -122.42], zoom_start=12,width='50%',height='50%') # Convert data format heatdata = data[['Y','X']].values.tolist() # add incidents to map HeatMap(heatdata).add_to(san_map) san_map在地图上呈现GeoJSON边界数据
import json import requests # url = 'https://cocl.us/sanfran_geojson' url = 'san-francisco.geojson' san_geo = f'{url}' san_map = folium.Map(location=[37.77, -122.42], zoom_start=12,width='50%',height='50%') folium.GeoJson( san_geo, style_function=lambda feature: { 'fillColor': '#ffff00', 'color': 'blue', 'weight': 2, 'dashArray': '5, 5' } ).add_to(san_map) san_map在GeoJSON上绘制Choropleth分级着色图
# Count crime numbers in each neighborhood disdata = pd.DataFrame(cdata['PdDistrict'].value_counts()) disdata.reset_index(inplace=True) disdata.rename(columns={'index':'Neighborhood','PdDistrict':'Count'},inplace=True) san_map = folium.Map(location=[37.77, -122.42], zoom_start=12,width='50%',height='50%') folium.Choropleth( geo_data=san_geo, data=disdata, columns=['Neighborhood','Count'], key_on='feature.properties.DISTRICT', #fill_color='red', fill_color='YlOrRd', fill_opacity=0.7, line_opacity=0.2, highlight=True, legend_name='Crime Counts in San Francisco' ).add_to(san_map) san_map3. 各地图提供商瓦片服务地图规则 高德地图
目前高德的瓦片地址有如下两种:
目前百度的瓦片编号比较特殊,Folium暂不支持。
其他参考资料:
腾讯地图
腾讯地图的瓦片地图URL格式: