import folium
Data Visualization using Folium
from pyproj import crs
import geopandas as gpd
import matplotlib.pyplot as plt
Create a simple Interactive Map
= folium.Map(location=[20.59,78.96],zoom_start=5,control_scale=True) m
m
Make this Notebook Trusted to load map: File -> Trust Notebook
Change the Style of the Map
= folium.Map(
m = [20.59,78.96],
location = "Stamen Toner",
tiles = 5,
zoom_start = True,
control_scale = True
prefer_canvas
)
m
Make this Notebook Trusted to load map: File -> Trust Notebook
Adding layers to the Map - Pin location
#18.5786832,73.7666697
= folium.Map(location=[20.59,78.96],
m =5,control_scale=True)
zoom_start
folium.Marker(= [18.5786832,73.7666697],
location ='Sai Eshnaya Apartments',
popup= folium.Icon(color='green',icon='ok-sign')
icon
).add_to(m)
m
Make this Notebook Trusted to load map: File -> Trust Notebook
Mark good resedential areas in Pune
= '../data/addresses.shp'
points_fp = gpd.read_file(points_fp) points
points.head()
id | addr | geometry | |
---|---|---|---|
0 | 1000 | Boat Club Road, 411001, Pune, Maharastra | POINT (73.87826 18.53937) |
1 | 1001 | Koregaon, 415501, Pune, Maharastra | POINT (73.89299 18.53772) |
2 | 1002 | Kothrud, 411038, Pune, Maharastra | POINT (73.80767 18.50389) |
3 | 1003 | Balewadi, 411045, Pune, Maharastra | POINT (73.76912 18.57767) |
4 | 1004 | Baner, 411047, Pune, Maharastra | POINT (73.77686 18.56424) |
= folium.features.GeoJson(points, name='Good Residential Areas') points_gjson
= folium.Map(location=[18.5786832,73.7666697], tiles="cartodbpositron",
m =8,
zoom_start=True)
control_scale
points_gjson.add_to(m)
folium.LayerControl().add_to(m) m
Make this Notebook Trusted to load map: File -> Trust Notebook
Create a Heatmap of the locations
"x"] = points["geometry"].apply(lambda geom: geom.x)
points["y"] = points["geometry"].apply(lambda geom: geom.y)
points[
# Create a list of coordinate pairs
= list(zip(points["y"], points["x"])) locations
from folium.plugins import HeatMap
# Create a Map instance
= folium.Map(
m =[18.5786832,73.7666697], tiles="stamentoner", zoom_start=10, control_scale=True
location
)
# Add heatmap to map instance
# Available parameters: HeatMap(data, name=None, min_opacity=0.5, max_zoom=18, max_val=1.0, radius=25, blur=15, gradient=None, overlay=True, control=True, show=True)
HeatMap(locations).add_to(m)
# Alternative syntax:
# m.add_child(HeatMap(points_array, radius=15))
# Show map
m
Make this Notebook Trusted to load map: File -> Trust Notebook
Create a Clustered point Map
from folium.plugins import MarkerCluster
# Create a Map instance
= folium.Map(
m =[18.5786832,73.7666697], tiles="cartodbpositron", zoom_start=12, control_scale=True
location )
# Get x and y coordinates for each point
"x"] = points["geometry"].apply(lambda geom: geom.x)
points["y"] = points["geometry"].apply(lambda geom: geom.y)
points[
# Create a list of coordinate pairs
= list(zip(points["y"], points["x"])) locations
# Create a folium marker cluster
= MarkerCluster(locations)
marker_cluster
# Add marker cluster to map
marker_cluster.add_to(m)
# Show map
m
Make this Notebook Trusted to load map: File -> Trust Notebook
Create a Choropleth Map
import geopandas as gpd
from pyproj import CRS
import requests
import geojson
# Specify the url for web feature service
= "https://kartta.hsy.fi/geoserver/wfs"
url
# Specify parameters (read data in json format).
# Available feature types in this particular data source: http://geo.stat.fi/geoserver/vaestoruutu/wfs?service=wfs&version=2.0.0&request=describeFeatureType
= dict(
params ="WFS",
service="2.0.0",
version="GetFeature",
request="asuminen_ja_maankaytto:Vaestotietoruudukko_2018",
typeName="json",
outputFormat
)
# Fetch data from WFS using requests
= requests.get(url, params=params)
r
# Create GeoDataFrame from geojson
= gpd.GeoDataFrame.from_features(geojson.loads(r.content))
data
# Check the data
data.head()
geometry | index | asukkaita | asvaljyys | ika0_9 | ika10_19 | ika20_29 | ika30_39 | ika40_49 | ika50_59 | ika60_69 | ika70_79 | ika_yli80 | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | POLYGON ((25472499.995 6689749.005, 25472499.9... | 688 | 9 | 28.0 | 99 | 99 | 99 | 99 | 99 | 99 | 99 | 99 | 99 |
1 | POLYGON ((25472499.995 6685998.998, 25472499.9... | 703 | 5 | 51.0 | 99 | 99 | 99 | 99 | 99 | 99 | 99 | 99 | 99 |
2 | POLYGON ((25472499.995 6684249.004, 25472499.9... | 710 | 8 | 44.0 | 99 | 99 | 99 | 99 | 99 | 99 | 99 | 99 | 99 |
3 | POLYGON ((25472499.995 6683999.005, 25472499.9... | 711 | 5 | 90.0 | 99 | 99 | 99 | 99 | 99 | 99 | 99 | 99 | 99 |
4 | POLYGON ((25472499.995 6682998.998, 25472499.9... | 715 | 11 | 41.0 | 99 | 99 | 99 | 99 | 99 | 99 | 99 | 99 | 99 |
from pyproj import CRS
# Define crs
= CRS.from_epsg(3879) data.crs
# Re-project to WGS84
= data.to_crs(epsg=4326)
data
# Check layer crs definition
print(data.crs)
EPSG:4326
# Change the name of a column
= data.rename(columns={"asukkaita": "pop18"}) data
"geoid"] = data.index.astype(str) data[
# Select only needed columns
= data[["geoid", "pop18", "geometry"]]
data
# Convert to geojson (not needed for the simple coropleth map!)
# pop_json = data.to_json()
# check data
data.head()
geoid | pop18 | geometry | |
---|---|---|---|
0 | 0 | 9 | POLYGON ((24.50236 60.31928, 24.50233 60.32152... |
1 | 1 | 5 | POLYGON ((24.50287 60.28562, 24.50284 60.28787... |
2 | 2 | 8 | POLYGON ((24.50311 60.26992, 24.50308 60.27216... |
3 | 3 | 5 | POLYGON ((24.50315 60.26767, 24.50311 60.26992... |
4 | 4 | 11 | POLYGON ((24.50328 60.25870, 24.50325 60.26094... |
= folium.Map(
m =[60.25, 24.8], tiles="cartodbpositron", zoom_start=10, control_scale=True
location
)
# Plot a choropleth map
# Notice: 'geoid' column that we created earlier needs to be assigned always as the first column
folium.Choropleth(=data,
geo_data="Population in 2018",
name=data,
data=["geoid", "pop18"],
columns="feature.id",
key_on="YlOrRd",
fill_color=0.7,
fill_opacity=0.2,
line_opacity="white",
line_color=0,
line_weight=False,
highlight=1.0,
smooth_factor# threshold_scale=[100, 250, 500, 1000, 2000],
="Population in Helsinki",
legend_name
).add_to(m)
# Show map
m
Make this Notebook Trusted to load map: File -> Trust Notebook
Create Choropleth Map with Interaction
# Convert points to GeoJson
folium.features.GeoJson(
data,="Labels",
name=lambda x: {
style_function"color": "transparent",
"fillColor": "transparent",
"weight": 0,
},=folium.features.GeoJsonTooltip(
tooltip=["pop18"], aliases=["Population"], labels=True, sticky=False
fields
),
).add_to(m)
m
Make this Notebook Trusted to load map: File -> Trust Notebook