用folium模块画地理图_使用Folium表示您的地理空间数据

用folium模块画地理图

As a part of the Data Science community, Geospatial data is one of the most crucial kinds of data to work with. The applications are as simple as ‘Where’s my food delivery order right now?’ and as complex as ‘What is the most optimal path for the delivery guy?’

作为数据科学社区的一部分,地理空间数据是最重要的数据类型之一。 申请就像“我现在的外卖订单在哪里?”一样简单。 就像“送货员的最佳路径是什么?”一样复杂。

是什么把我带到了Folium? (What brought me to Folium?)

I was recently working on a data science problem involving a lot of gps coordinates. Obviously the very basic question — how do I represent these coordinates on a map in my jupyter notebook? And while we know that plotly, geopy and basemap get the job done, this is the first time I came across Folium and decided to give it a go!

我最近正在研究一个涉及许多gps坐标的数据科学问题。 显然,这是一个非常基本的问题-如何在jupyter笔记本中的地图上表示这些坐标? 虽然我们知道plotly , geopy和basemap可以完成工作,但这是我第一次遇到Folium并决定尝试一下!

This article is a step by step tutorial on representing your data using folium.

本文是有关使用folium表示数据的分步教程。

介绍 (Introduction)

Folium essentially is used for generating interactive maps for the browser (inside notebooks or on a website). It uses leaflet.js , a javascript library for interactive maps.

Folium本质上用于为浏览器(在笔记本内部或网站上)生成交互式地图。 它使用leaflet.js (用于交互式地图的javascript库)。

To put it in a one-liner: Manipulate your data in Python, then visualize it in on a Leaflet map via folium.

要将其放在一个直线上: 用Python处理数据,然后通过叶片将其可视化在Leaflet地图上。

Step 1: Installing folium on the computer and importing the necessary packages.

步骤1: 在计算机上安装大叶草并导入必要的软件包。

!pip install foliumimport numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn import preprocessing
import scipy## for geospatial
import folium
import geopy

We will work with the Fire from Space: Australia dataset.

我们将使用“ 来自太空的火焰:澳大利亚”数据集。

Step 2: Loading and inspecting the dataset.

步骤2: 加载和检查数据集。

df = pd.read_csv("fire_archive_M6_96619.csv")
Image for post

Step 3: Finding the coordinates to begin with.

步骤3: 找到要开始的坐标。

We can either pick a set of coordinates from the dataset itself or we can use geopy for this purpose. Here, we are talking about Australian wildfires so I started with Melbourne for reference.

我们可以从数据集本身中选择一组坐标,也可以为此使用geopy。 在这里,我们谈论的是澳大利亚的山火,所以我从墨尔本开始作为参考。

city = "Melbourne"
# get location
locator = geopy.geocoders.Nominatim(user_agent="My app")
city = locator.geocode(city)
location = [city.latitude, city.longitude]
print(city, "\n[lat, long]:", location)
Image for post

Step 4: Plotting on the map.

步骤4: 在地图上绘制。

Plotting points on a map in Folium is like building a house. You lay the base (that’s your background map) and then you add points on top of it’s surface.

在Folium地图上绘制点就像在盖房子。 放置基础(即背景图),然后在其表面顶部添加点。

We shall first lay the base.

我们首先要打基础。

map_ = folium.Map(location=location, tiles="cartodbpositron",
zoom_start=8)
map_
Image for post

You can also play around with the tileset and zoom by referring here.

您还可以通过参考此处来玩游戏并缩放。

Now we plot the points on the map. We shall color-code according to the attribute ‘type’ and size it as per the ‘brightness’ of the fire. So let’s get those attributes in order first.

现在我们在地图上绘制点。 我们将根据属性“类型”对代码进行颜色编码,并根据火的“亮度”对其进行大小调整。 因此,让我们先按顺序获取这些属性。

# create color column to correspond to type
colors = ["red","yellow","orange", "green"]
indices = sorted(list(df["type"].unique()))
df["color"] = df["type"].apply(lambda x:
colors[indices.index(x)])
## scaling the size
scaler = preprocessing.MinMaxScaler(feature_range=(3,15))
df["size"] = scaler.fit_transform(
df['brightness'].values.reshape(-1,1)).reshape(-1)

We finally add points on top of the map using folium.

最后,我们使用叶片将点添加到地图顶部。

df.apply(lambda row: folium.CircleMarker(
location=[row['latitude'],row['longitude']],
popup=row['type'],
color=row["color"], fill=True,
radius=row["size"]).add_to(map_), axis=1)
Image for post

Finally, we move to adding a legend to the map. I used this reference for adding a legend. There are a variety of other methods but this was what I found the easiest.

最后,我们开始向地图添加图例。 我使用此参考来添加图例。 还有许多其他方法,但这是我发现的最简单的方法。

legend_html = """<div style="position:fixed; 
top:10px; right:10px;
border:2px solid black; z-index:9999;
font-size:14px;">&nbsp;<b>"""+color+""":</b><br>"""
for i in lst_elements:
legend_html = legend_html+"""&nbsp;<i class="fa fa-circle
fa-1x" style="color:"""+lst_colors[lst_elements.index(i)]+"""">
</i>&nbsp;"""+str(i)+"""<br>"""
legend_html = legend_html+"""</div>"""
map_.get_root().html.add_child(folium.Element(legend_html))#plot
map_
Image for post

Here’s the whole piece of code:

这是整个代码段:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn import preprocessing
import scipy
## for geospatial
import folium
import geopydf = pd.read_csv("fire_archive_M6_96619.csv")city = "Melbourne"
# get location
locator = geopy.geocoders.Nominatim(user_agent="My app")
city = locator.geocode(city)
location = [city.latitude, city.longitude]
print(city, "\n[lat, long]:", location)map_ = folium.Map(location=location, tiles="cartodbpositron",
zoom_start=8)# create color column to correspond to type
colors = ["red","yellow","orange", "green"]
indices = sorted(list(df["type"].unique()))
df["color"] = df["type"].apply(lambda x:
colors[indices.index(x)])
## scaling the size
scaler = preprocessing.MinMaxScaler(feature_range=(3,15))
df["size"] = scaler.fit_transform(
df['brightness'].values.reshape(-1,1)).reshape(-1)df.apply(lambda row: folium.CircleMarker(
location=[row['latitude'],row['longitude']],
popup=row['type'],
color=row["color"], fill=True,
radius=row["size"]).add_to(map_), axis=1)legend_html = """<div style="position:fixed;
top:10px; right:10px;
border:2px solid black; z-index:9999;
font-size:14px;">&nbsp;<b>"""+color+""":</b> <br>"""
for i in lst_elements:
legend_html = legend_html+"""&nbsp;<i class="fa fa-circle
fa-1x" style="color:"""+lst_colors[lst_elements.index(i)]+"""">
</i>&nbsp;"""+str(i)+"""<br>"""
legend_html = legend_html+"""</div>"""
map_.get_root().html.add_child(folium.Element(legend_html))
#plot
map_

You can also find it on my Github. Hope this article helped.

您也可以在我的Github上找到它。 希望本文对您有所帮助。

翻译自: https://towardsdatascience.com/represent-your-geospatial-data-using-folium-c2a0d8c35c5c

用folium模块画地理图

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/391977.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Windows下安装Python模块时环境配置

“Win R”打开cmd终端&#xff0c;如果直接在里面使用pip命令的时候&#xff0c;要么出现“syntax invalid”&#xff0c;要么出现&#xff1a; pip is not recognized as an internal or external command, operable program or batch file. 此时需要将C:\Python27\Scripts添加…

播客2008

http://blog.tangcs.com/2008/12/29/year-2008/转载于:https://www.cnblogs.com/WarrenTang/articles/1364465.html

linear在HTML的作用,CSS3里的linear-gradient()函数

linear-gradient() 函数用于创建一个线性渐变的 "图像"。为了创建一个线性渐变&#xff0c;你需要设置一个起始点和一个方向(指定为一个角度)的渐变效果。你还要定义终止色。终止色就是你想让Gecko去平滑的过渡&#xff0c;并且你必须指定至少两种&#xff0c;当然也…

golang底层深入_带有Golang的GraphQL:从基础到高级的深入研究

golang底层深入by Ridham Tarpara由里德姆塔帕拉(Ridham Tarpara) 带有Golang的GraphQL&#xff1a;从基础到高级的深入研究 (GraphQL with Golang: A Deep Dive From Basics To Advanced) GraphQL has become a buzzword over the last few years after Facebook made it ope…

spring—Bean实例化三种方式

1&#xff09; 使用无参构造方法实例化 它会根据默认无参构造方法来创建类对象&#xff0c;如果bean中没有默认无参构造函数&#xff0c;将会创建失败 <?xml version"1.0" encoding"UTF-8"?> <beans xmlns"http://www.springframework.o…

bzoj 3439: Kpm的MC密码

Description 背景 想Kpm当年为了防止别人随便进入他的MC&#xff0c;给他的PC设了各种奇怪的密码和验证问题&#xff08;不要问我他是怎么设的。。。&#xff09;&#xff0c;于是乎&#xff0c;他现在理所当然地忘记了密码&#xff0c;只能来解答那些神奇的身份验证问题了。。…

python创建类统计属性_轻松创建统计数据的Python包

python创建类统计属性介绍 (Introduction) Sometimes you may need a distribution figure for your slide or class. Since you are not using data, you want a quick solution.有时&#xff0c;您的幻灯片或课程可能需要一个分配图。 由于您不使用数据&#xff0c;因此需要快…

pytorch深度学习_在本完整课程中学习在PyTorch中应用深度学习

pytorch深度学习In this complete course from Fawaz Sammani you will learn the key concepts behind deep learning and how to apply the concepts to a real-life project using PyTorch. 在Fawaz Sammani的完整课程中&#xff0c;您将学习深度学习背后的关键概念&#x…

html让a标签左右一样宽,button和a标签设置相同的css样式,但是宽度不同

登录注册.btn {display: block;-moz-appearance: none;background: rgba(0, 0, 0, 0) none repeat scroll 0 0;border-radius: 0.25rem;box-sizing: border-box;cursor: pointer;font-family: inherit;font-size: 0.8rem;height: 2rem;line-height: 1.9rem;margin: 0;padding: …

浅析STM32之usbh_def.H

【温故而知新】类似文章浅析USB HID ReportDesc (HID报告描述符) 现在将en.stm32cubef1\STM32Cube_FW_F1_V1.4.0\Middlewares\ST\STM32_USB_Host_Library\Core\Inc\usbh_def.H /********************************************************************************* file us…

spring—依赖注入

依赖注入&#xff08;Dependency Injection&#xff09; 它是 Spring 框架核心 IOC 的具体实现。 在编写程序时&#xff0c;通过控制反转&#xff0c;把对象的创建交给了 Spring&#xff0c;但是代码中不可能出现没有依赖的情况。 IOC 解耦只是降低他们的依赖关系&#xff0c;…

C# (类型、对象、线程栈和托管堆)在运行时的相互关系

在介绍运行时的关系之前,先从一些计算机基础只是入手,如下图: 该图展示了已加载CLR的一个windows进程,该进程可能有多个线程,线程创建时会分配到1MB的栈空间.栈空间用于向方法传递实参,方法定义的局部变量也在实参上,上图的右侧展示了线程的栈内存,栈从高位内存地址向地位内存地…

2019-08-01 纪中NOIP模拟赛B组

T1 [JZOJ2642] 游戏 题目描述 Alice和Bob在玩一个游戏&#xff0c;游戏是在一个N*N的矩阵上进行的&#xff0c;每个格子上都有一个正整数。当轮到Alice/Bob时&#xff0c;他/她可以选择最后一列或最后一行&#xff0c;并将其删除&#xff0c;但必须保证选择的这一行或这一列所有…

knn分类 knn_关于KNN的快速小课程

knn分类 knnAs the title says, here is a quick little lesson on how to construct a simple KNN model in SciKit-Learn. I will be using this dataset. It contains information on students’ academic performance.就像标题中所说的&#xff0c;这是关于如何在SciKit-Le…

spring—配置数据源

数据源&#xff08;连接池&#xff09;的作用 数据源(连接池)是提高程序性能如出现的 事先实例化数据源&#xff0c;初始化部分连接资源 使用连接资源时从数据源中获取 使用完毕后将连接资源归还给数据源 常见的数据源(连接池)&#xff1a;DBCP、C3P0、BoneCP、Druid等 开发步…

大型网站系统与Java中间件实践pdf

下载地址&#xff1a;网盘下载 基本介绍 编辑内容简介 到底是本什么书&#xff0c;拥有这样一份作序推荐人列表&#xff1a;阿里集团章文嵩博士|新浪TimYang|去哪网吴永强|丁香园冯大辉|蘑菇街岳旭强|途牛汤峥嵘|豆瓣洪强宁|某电商陈皓/林昊…… 这本书出自某电商技术部总监之手…

office漏洞利用--获取shell

环境&#xff1a; kali系统&#xff0c; windows系统 流程&#xff1a; 在kali系统生成利用文件&#xff0c; kali系统下监听本地端口&#xff0c; windows系统打开doc文件&#xff0c;即可中招 第一种利用方式&#xff0c; 适合测试用&#xff1a; 从git下载代码&#xff1a; …

pandas之DataFrame合并merge

一、merge merge操作实现两个DataFrame之间的合并&#xff0c;类似于sql两个表之间的关联查询。merge的使用方法及参数解释如下&#xff1a; pd.merge(left, right, onNone, howinner, left_onNone, right_onNone, left_indexFalse, right_indexFalse,    sortFalse, suffi…

typescript_如何掌握高级TypeScript模式

typescriptby Pierre-Antoine Mills皮埃尔安托万米尔斯(Pierre-Antoine Mills) 如何掌握高级TypeScript模式 (How to master advanced TypeScript patterns) 了解如何为咖喱和Ramda创建类型 (Learn how to create types for curry and Ramda) Despite the popularity of curry…

html函数splice,js数组的常用函数(slice()和splice())和js引用的三种方法总结—2019年1月16日...

总结&#xff1a;slice()和splice()slice(参数1,参数2)可以查找数组下对应的数据&#xff0c;参数1为起始位置&#xff0c;参数2为结束位置&#xff0c;参数2可以为负数&#xff0c;-1对应的是从后向前数的第一个数值。splice()可以进行增删改查数据操作&#xff0c;splice(参数…