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

本文介绍了如何利用Python的Folium模块来展示和分析地理空间数据,提供了从数据到地图的转换方法。
摘要由CSDN通过智能技术生成

用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笔记本中的地图上表示这些坐标? 虽然我们知道plotlygeopybasemap可以完成工作,但这是我第一次遇到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模块画地理图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值