folium 使用本地瓦片
简介(我们将创建的内容): (Introduction (what we’ll create):)
As the title suggests, we will be creating a Choropleth visualization using a TopoJSON file in this tutorial. We will use Folium for that purpose. Folium is a library based on Leaflet.js (unlike Plotly, which is based on D3.js). The following image gives an idea of what we will be creating at the end of this tutorial:
如标题所示,在本教程中,我们将使用TopoJSON文件创建一个Choropleth可视化文件。 我们将为此目的使用Folium。 Folium是一个基于Leaflet.js的库(与基于D3.js的Plotly不同)。 下图给出了在本教程结尾处将要创建的内容的想法:
This is very similar to the visualization created in the Plotly tutorial. The same data set has been used for creating both the Plotly and the Folium visualizations. However, this visualization has been created using a TopoJSON file. The TopoJSON file, in turn, was created from the same GeoJSON that was used for the Plotly tutorial, using the conversion available on mapshaper.org. As you can see, while the GeoJSON file is 800 KB large, the corresponding TopoJSON is about one third the size, at 269 KB.
这与Plotly教程中创建的可视化非常相似。 相同的数据集已用于创建Plotly和Folium可视化图。 但是,此可视化是使用TopoJSON文件创建的。 该TopoJSON文件,反过来,是从被用于Plotly教程,利用系统提供的转换相同GeoJSON的创建mapshaper.org 。 如您所见,虽然GeoJSON文件的大小为800 KB,但相应的TopoJSON的大小约为269 KB,约为其三分之一。
The resulting difference can also be seen in the file sizes. The Jupyter Notebook for the Plotly visualization is more than 8 MB in size, while the Folium notebook is just about 1 MB in size. Folium also has a base map incorporated in the visualization (just like in the Plotly + Mapbox tutorial). However, Folium doesn’t have the level of interactivity of Plotly. You can zoom into the map, or pan the map, but hovering on any state will have no effect here, whereas Plotly displayed the count on hover. Here, the count has to be inferred manually using the color bar.
所产生的差异也可以在文件大小中看到。 用于Plotly可视化的Jupyter笔记本的大小超过8 MB,而Folium笔记本的大小仅为1 MB。 Folium在可视化中还包含一个基础地图(就像在Plotly + Mapbox教程中一样 )。 但是,大叶草不具有Plotly的交互性。 您可以放大地图或平移地图,但是将鼠标悬停在任何状态都不会产生任何影响,而“绘制”则会显示悬停时的计数。 在此,必须使用颜色条手动推断计数。
本教程的结构: (Structure of the tutorial:)
The tutorial is structured into the following sections:
本教程分为以下几节:
Pre-requisites
先决条件
Installing Folium
安装大叶草
TopoJSON vs GeoJSON
TopoJSON与GeoJSON
Getting started with the tutorial
教程入门
When to use this library
何时使用此库
先决条件: (Pre-requisites:)
This tutorial assumes that you are familiar with python and that you have python downloaded and installed in your machine. If you are not familiar with python but have some experience of programming in some other languages, you may still be able to follow this tutorial, depending on your proficiency.
本教程假定您熟悉python,并且已在计算机中下载并安装了python。 如果您不熟悉python,但有一些使用其他语言进行编程的经验,那么您仍然可以根据自己的熟练程度来学习本教程。
安装Folium: (Installing Folium:)
If you are using Anaconda:
如果您正在使用Anaconda:
$ conda install folium -c conda-forge
Otherwise,
除此以外,
$ pip install folium
For additional information, see https://python-visualization.github.io/folium/installing.html
有关更多信息,请参见https://python-visualization.github.io/folium/installing.html
TopoJSON与GeoJSON: (TopoJSON vs GeoJSON:)
We have already discussed that TopoJSON files have a massive advantage over GeoJSON files when it comes to file-size, without any simplification of the shapes. Let’s first discuss the reason for this, and we will then come to the cost at which we get a size reduction. Quoting from the official TopoJSON GitHub repo, the major reasons why TopoJSON files are much smaller in size than GeoJSON are:
我们已经讨论过,就文件大小而言,TopoJSON文件相对于GeoJSON文件具有巨大的优势,而没有任何形状的简化。 让我们首先讨论造成这种情况的原因,然后我们将得出减少尺寸的成本。 从官方TopoJSON GitHub存储库引用,TopoJSON文件的大小比GeoJSON小得多的主要原因是:
Geometries in TopoJSON files are stitched together from shared line segments called arcs.
TopoJSON文件中的几何是从称为弧的共享线段中缝合在一起的。
What this roughly means is that if two states of India share a common boundary, the TopoJSON will not store the boundary points twice. Rather there will be a single arc that will be shared by both the states.
这大致意味着,如果印度的两个州共享相同的边界,则TopoJSON将不会两次存储边界点。 相反,这两个州将共享一个弧线 。
To further reduce file size, TopoJSON can use quantized delta-encoding for integer coordinates. This is similar to rounding coordinate values (e.g., LilJSON), but with greater efficiency and control over loss of information. And like GeoJSON, TopoJSON files are easily modified in a text editor and amenable to gzip compression.
为了进一步减小文件大小,TopoJSON可以对整数坐标使用量化增量编码。 这类似于舍入坐标值(例如LilJSON ),但是具有更高的效率和对信息丢失的控制。 与GeoJSON一样,TopoJSON文件可以在文本编辑器中轻松修改,并且可以进行gzip压缩。
A layman may just focus on the ‘rounding coordinate values’ part. Essentially, TopoJSON performs some sophisticated rounding operations to reduce the size of the stored coordinates.
外行可能只关注“舍入坐标值”部分。 本质上,TopoJSON执行一些复杂的舍入操作以减小存储的坐标的大小。
Now, while this has substantial advantages, one major drawback of TopoJSON files is a complicated file structure. Reading a GeoJSON is much more intuitive than reading a TopoJSON. Also, several libraries work strictly with GeoJSON files, and therefore, you need to use an external tool to convert TopoJSON to GeoJSON.
现在,尽管这具有很多优点,但TopoJSON文件的一个主要缺点是复杂的文件结构。 读取GeoJSON比读取TopoJSON直观得多。 另外,几个库严格使用GeoJSON文件,因此,您需要使用外部工具将TopoJSON转换为GeoJSON。
教程入门: (Getting started with the tutorial:)
GitHub repo: https://github.com/carnot-technologies/MapVisualizations
GitHub回购: https : //github.com/carnot-technologies/MapVisualizations
Relevant notebook: FoliumChoroplethDemo.ipynb
相关笔记本: FoliumChoroplethDemo.ipynb
View notebook on NBViewer: Click Here
在NBViewer上查看笔记本: 单击此处
导入相关软件包: (Import relevant packages:)
import numpy as np
import pandas as pd
import folium
import json
加载和了解TopoJSON文件: (Loading and Understanding the TopoJSON file:)
First, let’s open the TopoJSON file, which is present in the json_files folder.
首先,我们打开TopoJSON文件,该文件位于json_files文件夹中。
with open('json_files\\India_States_2020_compressed_topo.json') as f:
states_topo = json.load(f)
Now, the way to explore any JSON file is to have a look at the keys. As can be seen in the notebook, we have a successive look at the keys to identify the key which contains the state names.
现在,探索任何JSON文件的方法是看一下密钥。 从笔记本中可以看出,我们连续查看了按键,以识别包含状态名称的按键。
states_topo.keys()
>> dict_keys(['type', 'arcs', 'transform', 'objects'])states_topo['objects'].keys()
>> dict_keys(['India_States_2020_compressed'])states_topo['objects']['India_States_2020_compressed'].keys()
>> dict_keys(['type', 'geometries'])
After this point, you might be interested in having a look at the contents of geometries
. However, it turns out that geometries
is a list. So let’s look at its first element.
在这之后,您可能会对看一下geometries
的内容感兴趣。 但是,事实证明geometries
是一个列表。 因此,让我们看一下它的第一个元素。
states_topo['objects']['India_States_2020_compressed']['geometries'][0].keys()
>> dict_keys(['arcs', 'type', 'properties', 'id'])states_topo['objects']['India_States_2020_compressed']['geometries'][0]['properties'].keys()
>> dict_keys(['dtname', 'stcode11', 'dtcode11', 'year_stat', 'Dist_LGD', 'State_LGD', 'JID', 'state_name'])
Voila! We have finally isolated the state_name
field. Let’s check that it indeed corresponds to the names of Indian states and Union Territories.
瞧! 我们终于隔离了state_name
字段。 让我们检查一下它是否确实与印度各州和联盟领地的名称相对应。
states_topo['objects']['India_States_2020_compressed']['geometries'][0]['properties']['state_name']
>> 'ANDAMAN & NICOBAR'
You can play around with the TopoJSON file to see what is contained in the keys that we haven’t explored in this tutorial.
您可以使用TopoJSON文件来查看我们在本教程中没有探讨的键中包含的内容。
加载数据并创建可视化文件: (Loading the data and creating the visualization:)
We will use the same CSV that we used in the PlotlyChoroplethDemo notebook.
我们将使用与PlotlyChoroplethDemo笔记本中相同的CSV。
df = pd.read_csv('data/state_dummy_data_no_null.csv')
Now, creating the visualization in Folium is a two-step process. We first create the map and then add the choropleth on top of it.
现在,在Folium中创建可视化过程需要两个步骤。 我们首先创建地图,然后在其顶部添加choropleth。
folium_map = folium.Map(location=[19, 80],
zoom_start=4,
tiles="CartoDB dark_matter")folium.Choropleth(geo_data=states_topo,
topojson='objects.India_States_2020_compressed',
key_on='feature.properties.state_name',
data=df, # my dataset
columns=['st_nm','count'],
fill_color='GnBu',
fill_opacity=0.7,
line_opacity=0.5).add_to(folium_map)data=df, # my dataset
columns=['st_nm','count'],
fill_color='GnBu',
fill_opacity=0.7,
line_opacity=0.5).add_to(folium_map)folium_map
I know there’s a lot to unpack here. So let’s get started. Firstly, when creating the Folium map, we need to provide the coordinates of the centroid and the initial zoom level. Since our region of interest is India, I’ve provided an appropriate centroid location. A zoom level of 4 should be sufficient. I would have used a higher zoom level if my region of interest was an Indian state instead of the whole country. For tiles, I’ve chosen “CartoDB dark_matter”. Just like with Mapbox (discussed in the Plotly + Mapbox Choropleth tutorial), several free tile options are available here, without any API key requirements. They are:
我知道这里有很多东西可以打开。 因此,让我们开始吧。 首先,在创建Folium地图时,我们需要提供质心的坐标和初始缩放级别。 由于我们感兴趣的地区是印度,因此我提供了适当的质心位置。 缩放级别4应该足够。 如果我感兴趣的地区是印度而不是整个国家,我会使用更高的缩放比例。 对于图块,我选择了“ CartoDB dark_matter”。 就像Mapbox(在Plotly + Mapbox Choropleth教程中讨论)一样,这里提供了几个免费的tile选项,而没有任何API密钥要求。 他们是:
- “OpenStreetMap” “ OpenStreetMap”
- “Mapbox Bright” (Limited levels of zoom for free tiles) “ Mapbox Bright”(免费瓷砖的缩放比例限制)
- “Mapbox Control Room” (Limited levels of zoom for free tiles) “ Mapbox控制室”(缩放比例限制为免费瓷砖)
- “Stamen” (Terrain, Toner, and Watercolor) “雄蕊”(地形,碳粉和水彩)
- “CartoDB” (positron and dark_matter) “ CartoDB”(正电子和dark_matter)
Coming to the Choropleth visualization, we have indicated the TopoJSON in the geo_data
field. Whenever we pass a TopoJson file, we must also pass an object in the topojson
field. This is because folium converts the TopoJSON into GeoJSON internally and therefore requires you to tell it which key of objects
contains the geometries. We know that the geometries and features are stored in the India_States_2020_compressed
key of objects
and hence pass that value to the topojson
field. Once that is done, we can assume that the internal conversion to GeoJSON will happen properly and then the properties will all be stored under the feature
key. Therefore, when passing the argument to the key_on
field, we specify the path to the key holding the state names as feature.properties.state_name
. The dataframe
is passed to the data
argument, and the columns
argument will contain the column in the df corresponding to the key_on
argument in the JSON, and another column containing the value for that state. The other arguments, I presume, are self-explanatory. You can get the list of all arguments, along with their explanations here. Congratulations!! Your visualization is ready!
进入Choropleth可视化,我们在geo_data
字段中指示了TopoJSON。 每当我们传递TopoJson文件时,我们还必须在topojson
字段中传递一个对象。 这是因为folium在内部将TopoJSON转换为GeoJSON,因此需要您告诉它哪个objects
键包含几何。 我们知道几何和要素存储在objects
的India_States_2020_compressed
键中,因此将该值传递给topojson
字段。 完成此操作后,我们可以假定内部到GeoJSON的转换将正确进行,然后所有属性都将存储在feature
键下。 因此,当将参数传递给key_on
字段时,我们将保存状态名称的键的路径指定为feature.properties.state_name
。 dataframe
将传递给data
参数,并且columns
参数将包含df中与JSON中的key_on
参数对应的列,以及另一列,包含该状态的值。 我认为其他论点是不言自明的。 您可以在此处获得所有参数的列表以及它们的解释。 恭喜!! 您的可视化已准备就绪!
Finally, we can save it to HTML using
最后,我们可以使用
folium_map.save("html_files/folium_demo.html")
何时使用此库: (When to use this library:)
This is your choice of library when you’re dealing with TopoJSON files and you don’t want to perform the TopoJSON to GeoJSON conversion yourself. In fact, the notebook also contains a bonus section showing how to visualize any TopoJSON file without a corresponding dataframe, using Folium. This library can also be used when your interactivity requirements are low. If you want to save an image for a PPT or a report, and require a base map below your choropleth, Folium can help you create the required visualization very quickly. However, if you need a highly interactive visualization, or need to create a time-lapse animation, Folium won’t help. In that case, you need to shift to a library like Plotly.
当您处理TopoJSON文件并且您不想自己执行TopoJSON到GeoJSON的转换时,这是您选择的库。 实际上,笔记本还包含一个奖金部分,显示了如何使用Folium可视化任何没有相应数据框的TopoJSON文件。 当您的交互性要求较低时,也可以使用此库。 如果您想为PPT或报告保存图像,并且需要在烛台下方有一个底图,Folium可以帮助您快速创建所需的可视化。 但是,如果您需要高度交互的可视化效果,或者需要创建延时动画,则Folium将无济于事。 在这种情况下,您需要转到Plotly这样的库。
We are trying to fix some broken benches in the Indian agriculture ecosystem through technology, to improve farmers’ income. If you share the same passion join us in the pursuit, or simply drop us a line on [email protected]
我们正在尝试通过技术修复印度农业生态系统中一些破烂的长凳 ,以提高农民的收入。 如果您有同样的热情,请加入我们的行列,或者直接给我们写信至[email protected]
Follow Tech@Carnot for more such blogs on topics like Data Science and Visualization, Cloud Engineering, Firmware Development, and many more.
请关注Tech @ Carnot,获取有关诸如数据科学和可视化,云工程,固件开发等主题的更多此类博客。
翻译自: https://medium.com/tech-carnot/topojson-based-choropleth-visualization-using-folium-471113fa5964
folium 使用本地瓦片