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

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

off999 2025-05-26 18:14 19 浏览 0 评论

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进行交互实现动态显示路径。



相关推荐

第九章:Python文件操作与输入输出

9.1文件的基本操作9.1.1打开文件理论知识:在Python中,使用open()函数来打开文件。open()函数接受两个主要参数:文件名和打开模式。打开模式决定了文件如何被使用,常见的模式有:&...

Python的文件处理

一、文件处理的流程1.打开文件,得到文件句柄并赋值给一个变量2.通过句柄对文件进行操作3.关闭文件示例:d=open('abc')data1=d.read()pri...

Python处理文本的25个经典操作

Python处理文本的优势主要体现在其简洁性、功能强大和灵活性。具体来说,Python提供了丰富的库和工具,使得对文件的读写、处理变得轻而易举。简洁的文件操作接口Python通过内置的open()函数...

Python学不会来打我(84)python复制文件操作总结

上一篇文章我们分享了python读写文件的操作,主要用到了open()、read()、write()等方法。这一次是在文件读写的基础之上,我们分享文件的复制。#python##python自学##...

python 文件操作

1.检查目录/文件使用exists()方法来检查是否存在特定路径。如果存在,返回True;如果不存在,则返回False。此功能在os和pathlib模块中均可用,各自的用法如下。#os模块中e...

《文件操作(读写文件)》

一、文件操作基础1.open()函数核心语法file=open("filename.txt",mode="r",encoding="utf-8"...

栋察宇宙(二十一):Python 文件操作全解析

分享乐趣,传播快乐,增长见识,留下美好。亲爱的您,这里是LearingYard学苑!今天小编为大家带来“Python文件操作全解析”欢迎您的访问!Sharethefun,spreadthe...

值得学习练手的70个Python项目(附代码),太实用了

Python丰富的开发生态是它的一大优势,各种第三方库、框架和代码,都是前人造好的“轮子”,能够完成很多操作,让你的开发事半功倍。下面就给大家介绍70个通过Python构建的项目,以此来学习Pytho...

python图形化编程:猜数字的游戏

importrandomnum=random.randint(1,500)running=Truetimes=0##总的次数fromtkinterimport*##导入所有tki...

一文讲清Python Flask的Web编程知识

刚入坑Python做Web开发的新手,还在被配置臃肿、启动繁琐折磨?Flask这轻量级框架最近又火出圈,凭5行代码启动Web服务的极致简洁,让90后程序员小张直呼真香——毕竟他刚用这招把部署时间从半小...

用python 编写一个hello,world

第一种:交互式运行一个hello,world程序:这是写python的第一步,也是学习各类语言的第一步,就是用这种语言写一个hello,world程序.第一步,打开命令行窗口,输入python,第二步...

python编程:如何使用python代码绘制出哪些常见的机器学习图像?

专栏推荐绘图的变量单变量查看单变量最方便的无疑是displot()函数,默认绘制一个直方图,并你核密度估计(KDE)sns.set(color_codes=True)np.random.seed(su...

如何编写快速且更惯用的 Python 代码

Python因其可读性而受到称赞。这使它成为一种很好的第一语言,也是脚本和原型设计的流行选择。在这篇文章中,我们将研究一些可以使您的Python代码更具可读性和惯用性的技术。我不仅仅是pyt...

Python函数式编程的详细分析(代码示例)

本篇文章给大家带来的内容是关于Python函数式编程的详细分析(代码示例),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。FunctionalProgramming,函数式编程。Py...

编程小白学做题:Python 的经典编程题及详解,附代码和注释(七)

适合Python3+的6道编程练习题(附详解)1.检查字符串是否以指定子串开头题目描述:判断字符串是否以给定子串开头(如"helloworld"以"hello&...

取消回复欢迎 发表评论: