Folium is a library that visualizes data on a map.
JavaScript Leaflet There is a library that supports interactive map processing Folium can be said to have parsed this library and made it available in Python.
I couldn't find the corresponding version on the Official page. Looking at the categories on pypi, it seems that both 2 and 3 systems will work.
Execute the following command in a somewhat new Python environment. It's'Easiest'.
folium installation
$ pip install folium
If you do not want to pollute the environment, separate it in advance with venv. I set up a disposable container.
FROM python:3.5.2-alpine
RUN pip install folium
Start container
#Create a folium image with Dockerfile in the current directory
docker build -t folium .
#Launch the container from the folium image
docker run -it -v /home/nanakenashi/workspace:/workspace folium /bin/sh
To refer to the html file spit out from Folium The current directory (workspace) is mounted inside the container.
Thanks to QuickStart. First, start a Python interactive shell and load folium.
Preparation
$ python
Python 3.5.2 (default, Nov 17 2016, 22:46:45)
[GCC 5.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import folium
Create a Map
object with the coordinates and zoom level in one hand, name it and save it.
The larger the zoom level, the higher the zoom level.
Create and save maps
>>> map1 = folium.Map(location=[-30.159215, 138.955078], zoom_start=4)
>>> map1.save('/workspace/map1.html')
Open the saved html file in your browser.
I wonder if the coordinates specified in location
were the center
I saw Australia off center, but I'm moving on.
Add marker
>>> map2 = folium.Map(location=[-30.159215, 138.955078], zoom_start=4)
>>> folium.Marker([-31.253218, 146.921099], popup='New South Wales').add_to(map2)
<folium.map.Marker object at 0x7fc1c73bd048>
>>> map2.save('/workspace/map2.html')
Open the saved map2.html.
A marker has appeared.
By the way, if you click it, the character string specified by popup
will be displayed.
Although various visualization options are used even within Quickstart This time, I will focus on the circular marker and put out something. First try.
Show circular marker
>>> map3 = folium.Map(location=[-30.159215, 138.955078], zoom_start=4)
>>> folium.CircleMarker([-31.253218, 146.921099],
... radius=100000,
... popup='New South Wales',
... color='#3186cc',
... fill_color='#3186cc',
... ).add_to(map3)
<folium.features.CircleMarker object at 0x7fc1c7408a20>
>>> map3.save('/workspace/map3.html')
Open map3 in your browser.
Since it seems that the quantity can be expressed by radius
, let's reflect some numerical value for each city.
At this point, I don't need to be interactive, so I'll write a script.
Place markers by city
import folium
map4 = folium.Map(location=[-30.159215, 138.955078], zoom_start=4)
#Stores data for each city (originally obtained from DB or csv file)
states = (
{'lat': -35.473468, 'lon': 149.012368, 'value': 1, 'name': 'Australian Capital Territory'},
{'lat': -31.253218, 'lon': 146.921099, 'value': 2, 'name': 'New South Wales'},
{'lat': -19.491411, 'lon': 132.550960, 'value': 3, 'name': 'Northern Territory'},
{'lat': -20.917574, 'lon': 142.702796, 'value': 4, 'name': 'Queensland'},
{'lat': -30.000232, 'lon': 136.209155, 'value': 5, 'name': 'South Australia'},
{'lat': -41.454520, 'lon': 145.970665, 'value': 6, 'name': 'Tasmania'},
{'lat': -37.471308, 'lon': 144.785153, 'value': 7, 'name': 'Victoria'},
{'lat': -27.672817, 'lon': 121.628310, 'value': 8, 'name': 'Western Australia'}
)
#Weights to make the size of the circle easy to understand
WEIGHT = 100000
#Add markers for each city (Because it is difficult to increase the number, batch addition is a future issue)
for state in states:
folium.CircleMarker(
[state['lat'], state['lon']],
radius=state['value'] * WEIGHT,
popup=state['name'],
color='#3186cc',
fill_color='#3186cc',
).add_to(map4)
map4.save('/workspace/map4.html')
After executing the above script, display map4.html in your browser.
The contents of QuickStart just made it feel like that. If you want to use it more solidly, see the Folium documentation It seems good to check the original Leaflet document as well.
I think that Google Maps is often used when providing as a service. Such libraries are also useful for research and internal offerings. In the article, it was limited to superficial visualization until the marker was placed. You can make various expressions such as editing the map itself and arranging polygons.
Jupyter Notebook seems to be able to analyze more interactively If you have used it, please give it a try.
Recommended Posts