今天给大家介绍一个非常 nb 的python 库,专门用来绘制地图的,它叫 folium 。

1. 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配色方案创建分布图。

2. folium的使用

地图的生成

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) 

参数说明:

  • location (tuple or list, default none):纬度和经度
  • width (pixel int or percentage string (default: ‘100%’)):地图宽度
  • height (pixel int or percentage string (default: ‘100%’)):地图高度
  • tiles (str, default ‘openstreetmap’) :瓦片名称或使用tilelayer classass.
  • min_zoom (int, default 0):地图可缩放的最小级别
  • max_zoom (int, default 18):地图可缩放的最大级别
  • zoom_start (int, default 10) :地图的初始缩放级别
  • attr (string, default none):当使用自定义瓦片时,传入自定义瓦片的名词
  • crs (str, default ‘epsg3857′) :投影坐标系标识
  • epsg3857: web墨卡托投影后的平面地图,坐标单位为米。大部分国外地图使用的时该标准。
  • epsg4326: web墨卡托投影后的平面地图,但仍然使用wgs84的经度、纬度表示坐标。
  • epsg3395: 墨卡托投影,主要用于航海图
  • simple: 简单的x,y匹配,用于自定义瓦片(比如游戏地图)
  • control_scale (bool, default false) :是否在地图上显示缩放标尺
  • prefer_canvas (bool, default false):强制使用canvas渲染
  • no_touch (bool, default false) :是否允许触摸事件
  • disable_3d (bool, default false) :强制使用css 3d效果
  • zoom_control (bool, default true) :是否要限制zoom操作
  • **kwargs :leaflets地图类的其他参数: https://leafletjs.com/reference-1.5.1.html#map
  • “tiles”参数可选值:
  • “openstreetmap”
  • “mapbox bright” (limited levels of zoom for free tiles)
  • “mapbox control room” (limited levels of zoom for free tiles)
  • “stamen” (terrain, toner, and watercolor)
  • “cloudmade” (must pass api key)
  • “mapbox” (must pass api key)
  • “cartodb” (positron and dark_matter)

“tiles”的自定义设置:

img

地球上同一个地理位置的经纬度,在不同的坐标系中,会有少量偏移,国内目前常见的坐标系主要分为三种:

  • 地球坐标系——wgs84:常见于gps设备,google地图等国际标准的坐标体系。
  • 火星坐标系——gcj-02:中国国内使用的被强制加密后的坐标体系,高德坐标就属于该种坐标体系。
  • 百度坐标系——bd-09:百度地图所使用的坐标体系,是在火星坐标系的基础上又进行了一次加密处理。

所以在设置“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?lang=zh_cn&size=1&scale=1&style=8&x={x}&y={y}&z={z}&ltype=6',attr='autonavi') tooltip ='请点击我查看该点信息' folium.marker([39.937282,116.403187], popup='南锣鼓巷',tooltip=tooltip).add_to(m) folium.marker([39.917834,116.397036], popup='故宫',tooltip=tooltip).add_to(m) folium.marker([39.928614,116.391746], popup='北海公园', tooltip=tooltip, icon=folium.icon(color='red')).add_to(m) folium.marker([39.942143,116.382590], popup='后海公园', tooltip=tooltip, icon=folium.icon(color='green', prefix='fa', icon='taxi')).add_to(m) m 

img

folium.icon类可以设置color, icon_color, icon, angle, prefix这5个参数:

  1. color的可选项包括:[‘red’, ‘blue’, ‘green’, ‘purple’, ‘orange’, ‘darkred’, ‘lightred’, ‘beige’, ‘darkblue’, ‘darkgreen’, ‘cadetblue’, ‘darkpurple’, ‘white’, ‘pink’, ‘lightblue’, ‘lightgreen’, ‘gray’, ‘black’, ‘lightgray’] ,或者html颜色代码
  2. icon_color同上
  3. icon可以在font-awesome网站中找到对应的名字,并设置prefix参数为’fa’
  4. 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_map 

3. 各地图提供商瓦片服务地图规则 高德地图

目前高德的瓦片地址有如下两种:

  • http://wprd0{1-4}.is.autonavi.com/appmaptile?x={x}&y={y}&z={z}&lang=zh_cn&size=1&scl=1&style=7&ltype=1
  • http://webst0{1-4}.is.autonavi.com/appmaptile?style=7&x={x}&y={y}&z={z}

前者是高德的新版地址,后者是老版地址。

高德新版的参数:

  • lang:可以通过zh_cn设置中文,en设置英文
  • size:基本无作用
  • scl:瓦片尺寸控制,1=256,2=512
  • style:设置影像和路网,style=6为卫星图,style=7为街道图,style=8为标注图
  • ltype:线性控制,增加后,只对地图要素进行控制,没有文字注记,要素多少,是否透明

这些规律并不是绝对的,有可能有的组合某些参数不起作用。

谷歌地图

目前谷歌的瓦片地址也存在两种:

  • 国内:

http://mt{0-3}.google.cn/vt/lyrs=m&hl=zh-cn&gl=cn&x={x}&y={y}&z={z}

  • 国外:

http://mt{0-3}.google.com/vt/lyrs=m&hl=zh-cn&gl=cn&x={x}&y={y}&z={z}

参数详解:

  • lyrs = 类型
  • h = roads only 仅限道路
  • m = standard roadmap 标准路线图
  • p = terrain 带标签的地形图
  • r = somehow altered roadmap 某种改变的路线图
  • s = satellite only 仅限卫星
  • t = terrain only 仅限地形
  • y = hybrid 带标签的卫星图
  • gl = 坐标系
  • cn = 中国火星坐标系
  • hl = 地图文字语言
  • zh-cn = 中文
  • en-us = 英文
  • x = 瓦片横坐标
  • y = 瓦片纵坐标
  • z = 缩放级别 卫星图0-14,路线图0-17

百度地图

百度当前的瓦片地址:

  • http://online{0-4}.map.bdimg.com/onlinelabel/?qt=tile&x={x}&y={y}&z={z}&styles=pl&udt=202004151&scaler=2&p=0
  • http://api{0-3}.map.bdimg.com/customimage/tile?&x={x}&y={y}&z={z}&udt=20180601&scale=1
  • http://its.map.baidu.com:8002/traffic/traffictileservice?level={z}&x={x}&y={y}&time=1373790856265&label=web2d&;v=017

备注:瓦片地址中的x和y对应的并不是经纬度值,而是瓦片编号,中国主要地图商的瓦片编号流派:

目前百度的瓦片编号比较特殊,folium暂不支持。

其他参考资料:

  • https://github.com/geometalab/pygeotile
  • https://github.com/anzhihun/openlayers3primer/blob/master/ch05/05-03.md
  • http://www.winseliu.com/blog/2018/01/30/map-started-guide/
  • https://github.com/cntchen/tile-lnglat-transform

腾讯地图

腾讯地图的瓦片地图url格式:

  • http://rt1.map.gtimg.com/realtimerender?z={z}&x={x}&y={y}&type=vector&style=0

由于腾讯地图使用的瓦片编码时tms,所以使用时需要额外的设置。具体如下:

其他底图

  • {0,1,2,3}代表了url的subdomain,在请求时会随机的在url中使用mt0、mt1、mt2、mt3。{z}代表zoom,即缩放级别,{x}代表列号,{y}代表行号。
  • geoq 官网有公开的多个基于 arcgis 的地图服务,均可使用,详见

https://map.geoq.cn/arcgis/rest/services

到此这篇关于python 里最强的地图绘制神器的文章就介绍到这了,更多相关python地图绘制神器内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!