用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.pswp.cn/news/391977.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/391977.shtml
英文地址,請注明出處:http://en.pswp.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(參數…