百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术文章 > 正文

Python使用bokeh及folium实现地理位置信息的交互可视化

zhezhongyun 2025-04-27 17:33 21 浏览

Talk is cheap,show U the code!

1.普通版(常规地图)

import numpy as np
from bokeh.plotting import figure, show, output_notebook
from bokeh.layouts import gridplot
output_notebook()
import pandas as pd
import folium
from folium import plugins

#加载数据
df =  pd.read_csv('平台总镜头.csv',encoding='gb18030')
df0 =  pd.read_csv('离线镜头.csv',encoding='gb18030')
def shixiao(x):
    if x in df0['国标编码'].tolist():
        return 1
    else:
        return 0
df['失效'] = df['国标编码'].apply(shixiao)
df = df.sample(frac=0.1, replace=True, random_state=10)  #1/10数据

df0 = df[df['失效']==1]  # 失效 红色 稍微大一点
df4 = df[df['失效']==1]  # 失效 红色 稍微大一点
df1 = df[(df['失效']==0) & (df['建设类别']=='一类点') ]  # 绿色
df2 = df[(df['失效']==0) & (df['建设类别']=='二类点') ]  # 蓝色
df3 = df[(df['失效']==0) & (df['建设类别']=='三类点') ]  #  青色

plotmap1 = folium.Map(location=[22.734057,114.058937], zoom_start=12,control_scale = True,
                          tiles='http://webrd02.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=8&x={x}&y={y}&z={z}',
                            attr='(c) <a href="http://ditu.amap.com/">高德地图</a>'                    
                     )
for i in range(df1.shape[0]):
     folium.Circle([df1['GPS纬度'].tolist()[i],df1['GPS经度'].tolist()[i]],color='green',radius=1).add_to(plotmap1)
for i in range(df2.shape[0]):
     folium.Circle([df2['GPS纬度'].tolist()[i],df2['GPS经度'].tolist()[i]],color='blue',radius=1).add_to(plotmap1)
for i in range(df3.shape[0]):
     folium.Circle([df3['GPS纬度'].tolist()[i],df3['GPS经度'].tolist()[i]],color='orange',radius=1).add_to(plotmap1)
        
for i in range(df0.shape[0]):
    folium.Circle([df0['GPS纬度'].tolist()[i],df0['GPS经度'].tolist()[i]],color='red',radius=1).add_to(plotmap1)
    

plotmap1.save('folium_map_1_10_全部Bokeh.html')

不带控件全部显示分类点

import math
def lonLat2WebMercator(lon,Lat):
    x = lon *20037508.34/180;
    y = math.log(math.tan((90+Lat)*math.pi/360))/(math.pi/180)
    y = y *20037508.34/180;
    return x,y
def lonLat2WebMercator_Lon(lon):  #GPS经度
    x = lon *20037508.34/180
    return x
def lonLat2WebMercator_Lat(Lat):  # GPS纬度
    y = math.log(math.tan((90+Lat)*math.pi/360))/(math.pi/180)
    y = y *20037508.34/180
    return y
df_new = df[(df["GPS纬度"]>22)  & (df["GPS纬度"] <23) & (df["GPS经度"]>113)  & (df["GPS经度"] <115)]  # 有效数据

df_new["Lon"] = df_new["GPS经度"].apply(lonLat2WebMercator_Lon)
df_new["Lat"] = df_new["GPS纬度"].apply(lonLat2WebMercator_Lat)

df = df_new.copy()

df0 = df[df['失效']==1]  # 失效 红色 稍微大一点
df1 = df[(df['失效']==0) & (df['建设类别']=='一类点') ]  # 绿色
df2 = df[(df['失效']==0) & (df['建设类别']=='二类点') ]  # 蓝色
df3 = df[(df['失效']==0) & (df['建设类别']=='三类点') ]  #  青色

from bokeh.util.browser import view
from bokeh.document import Document
from bokeh.embed import file_html
from bokeh.resources import INLINE
from bokeh.models import Plot
from bokeh.models import Range1d
from bokeh.models import WheelZoomTool, PanTool, BoxZoomTool
from bokeh.models import WMTSTileSource
# 设置x,y轴的经纬度范围
x_range = Range1d(int(df_new["Lon"].min()),int(df_new["Lon"].max()))
y_range = Range1d(int(df_new["Lat"].min()),int(df_new["Lat"].max()))
tile_options = {}
# https://www.jb51.net/article/206540.htm
tile_options['url'] = 'http://webrd02.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=7&x={x}&y={y}&z={z}'  # style=7,8
tile_options['attribution'] = """
   '(c) <a href="http://ditu.amap.com/">公众号:注册土木</a>.
    """ # 地图右下角标签
tile_source = WMTSTileSource(**tile_options)
# 实例化plot
p = figure(x_range=x_range, y_range=y_range, plot_height=800, plot_width=1000)
# 标记区域


p.scatter(df1['Lon'], df1['Lat'],size=5, marker="circle", color="#0DE63C", alpha=0.99,legend_label='一类') 
p.scatter(df2['Lon'], df2['Lat'],size=5, marker="circle", color="#9C09EB", alpha=0.99,legend_label='二类') 
p.scatter(df3['Lon'], df3['Lat'],size=5, marker="circle", color="#101AEB", alpha=0.99,legend_label='三类') 

p.scatter(df0['Lon'], df0['Lat'],size=5, marker="circle", color="red", alpha=0.99,legend_label='失效')  
# 渲染地图
# p.add_tools(BoxZoomTool(match_aspect=True))  # WheelZoomTool(), PanTool(), 

tile_renderer_options = {}
p.add_tile(tile_source, **tile_renderer_options)

# 其他参数
p.xaxis.visible = False
p.yaxis.visible = False 
p.xgrid.grid_line_color = None 
p.ygrid.grid_line_color = None 
p.legend.orientation = "horizontal"
p.legend.location = "top_center"
p.legend.click_policy="hide" # 点击图例显示、隐藏图形
p.sizing_mode = 'scale_width'
# 显示
show(p)


全部数据

部分数据

3.加强版(卫星地图)

from bokeh.util.browser import view
from bokeh.document import Document
from bokeh.embed import file_html
from bokeh.resources import INLINE
from bokeh.models import Plot
from bokeh.models import Range1d
from bokeh.models import WheelZoomTool, PanTool, BoxZoomTool
from bokeh.models import WMTSTileSource
# 设置x,y轴的经纬度范围
x_range = Range1d(int(df_new["Lon"].min()),int(df_new["Lon"].max()))
y_range = Range1d(int(df_new["Lat"].min()),int(df_new["Lat"].max()))
tile_options = {}
# https://www.jb51.net/article/206540.htm
# tile_options['url'] = 'http://webrd02.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=7&x={x}&y={y}&z={z}'  # style=7,8
tile_options['url'] = 'http://webst02.is.autonavi.com/appmaptile?style=6&x={x}&y={y}&z={z}'
# tiles='http://webst02.is.autonavi.com/appmaptile?style=6&x={x}&y={y}&z={z}', # 高德卫星图
tile_options['attribution'] = """
   '(c) <a href="http://ditu.amap.com/">公众号:注册土木</a>.
    """ # 地图右下角标签
tile_source = WMTSTileSource(**tile_options)
# 实例化plot
p = figure(x_range=x_range, y_range=y_range, plot_height=800, plot_width=1000)
# 标记区域


p.scatter(df1['Lon'], df1['Lat'],size=5, marker="circle", color="#0DE63C", alpha=0.99,legend_label='一类') 
p.scatter(df2['Lon'], df2['Lat'],size=5, marker="circle", color="#9C09EB", alpha=0.99,legend_label='二类') 
p.scatter(df3['Lon'], df3['Lat'],size=5, marker="circle", color="#101AEB", alpha=0.99,legend_label='三类') 

p.scatter(df0['Lon'], df0['Lat'],size=5, marker="circle", color="red", alpha=0.99,legend_label='失效')  
# 渲染地图
p.add_tools(WheelZoomTool(), PanTool(), BoxZoomTool(match_aspect=True))  # 

tile_renderer_options = {}
p.add_tile(tile_source, **tile_renderer_options)

# 其他参数
p.xaxis.visible = False
p.yaxis.visible = False 
p.xgrid.grid_line_color = None 
p.ygrid.grid_line_color = None 
p.legend.orientation = "horizontal"
p.legend.location = "top_center"
p.legend.click_policy="hide" # 点击图例显示、隐藏图形
p.sizing_mode = 'scale_width'
# 显示
show(p)

卫星地图

civilpy:Python加载basemap绘制分省地图1 赞同 · 1 评论文章

注:folium在一定程度上要比basemap好用一些;至于Echarts或者腾讯地图API的热力图,也是可以的。不过这些API在一些复杂场景体验并不是很好,如与movepy进行交互实现动态显示路径。



相关推荐

DevExpress使用教程:GridView经验小结

下面是笔者自己总结的使用DevExpressGridview的一些经验小结,分享给大家:1、去除GridView头上的"Dragacolumnheaderheretogroup...

ComponentOne 新版本发布,新增 .NET 6 和 Blazor 平台控件支持

ComponentOneEnterprise是葡萄城推出的一款内置300多种开发控件的.NET控件集,可满足WinForm、WPF、Blazor、ASP.NETMVC等平台下的系统开发...

Wijmo5 Flexgrid基础教程:数据绑定

WijmoEnterprise下载>FlexGrid在JavaScript程序中启动添加Wijmo引用;添加wijmo控件的扩展;在JavaScript中初始化wijmo控件;(可选)添加cs...

Wijmo5 Flexgrid基础教程:InlineEdit

WijmoEnterprise下载>对于flexgrid,可以直接在单元格内进行编辑。但另外还有一种编辑方式,即在一行添加按钮,统一的编辑和提交数据。本文主要介绍给flexgrid添加编辑按钮...

WinForms Data Grid控件升级(winform devexpress控件)

告诉大家一个好消息:慧都将于近期隆重推出“DevExpress14.2新版发布会”。心动不如行动,赶快报名吧!我们期待与您相约DevExpress14.2新版发布会。>>新增Wind...

XAML控件宽度为另一控件的一半、静态属性绑定

控件上当某些数据需要根据其他数据的变化而变化很多时候,想让某个控件的宽度或者高度是另一个已有控件的一半,一开始打算使用ObjectDataProvider来实现,因为在控件上当某些数据需要根据其他数据...

用 CSS Grid 布局制作一个响应式柱状图

最新一段时间比较喜欢玩弄图表,出于好奇,我想找出比较好的用CSS制作图表的方案。开始学习网上开源图表库,它对我学习新的和不熟悉的前端技术很有帮助,比如这个:CSSGrid。今天和大家分享我学到的...

Grid 移动端双列瀑布流(移动端瀑布流布局)

预览图:原理合理使用Grid的属性:display:设置为grid指明当前容器为Grid布局grid-template-columns:定义每一列的列宽(百分比或绝对单位)grid-templa...

DevExpress导出GridControl控件数据

前言:使用C#做桌面应用时,我们会常常使用Winform作为我们的开发界面,但是windows自带的控件由于长时间不更新,已经不能够满足当前开发需要所以使用DevExpress控件作为Winform...

css grid 布局的那些事儿(css grid布局和flex布局)

CSSGrid是一种为Web开发创建网站布局的方式。它已经存在了很多年,随着更多浏览器的支持,它终于变得越来越流行。接下来我们将了解下CSSGrid及其工作原理。了解下它如何使用。CSS...

Grid.js - 跨框架的前端表格插件(前端table框架)

只想简简单单画个表格,但React,Vue,Angular,…,这么多前端框架,各自都有不同的表格渲染库。就没有表格库能“一次画表,到处运行”吗?来看看Grid.js这个跨框架的前端表格插件吧!...

WPF开发教程01-布局控件(wpf tablecontrol控件)

布局控件是用于进行控件布局的容器类控件,其内部控件按照一定规律自动排列,且在父控件改变大小时,会自动适应。常用布局控件如下:1.一维布局控件(StackPanel)其内部控件按照某个维度自动排列,排...

wxPython - 高级控件之表格Grid(wxpython grid刷新数据)

实战wxPython系列-043wx.grid.Grid及其相关类用于显示和编辑表格数据。它们提供了一组丰富的功能,用于显示、编辑和与各种数据源交互。wx.grid.Grid是一个功能强大的但是又稍微...

前端 BFC、IFC、GFC 和 FFC,这些你都知道吗?

如果觉得我的文章不错,可以关注我,想要看其他的进阶知识可以查看我发布过的文章!编辑搜图请点击输入图片描述BFC(Blockformattingcontexts):块级格式上下文页面上的一个隔离的...

20多个好用的 Vue 组件库,请查收

在本文中,我们将探讨一些最常见的vuejs组件。你可以收藏一波。VueTables-2地址:https://github.com/matfish2/vue-tables-2VueTables2...