import pandas as pd
import geopandas
= pd.read_csv("https://github.com/hsma-programme/h6_3b_advanced_qgis_mapping_python/raw/main/h6_3b_advanced_qgis_and_mapping_in_python/example_code/gp_surgery_locations_plus_patient_list_size.csv")
gp_list
= geopandas.GeoDataFrame(
gp_list_gdf # Our pandas dataframe
gp_list, = geopandas.points_from_xy(
geometry 'result_eastings'], # Our 'x' column (horizontal position of points)
gp_list['result_northings'] # Our 'y' column (vertical position of points)
gp_list[
),= 'EPSG:27700'
crs
)
# Convert to 4326 (lat/long) for working with Folium
= gp_list_gdf.to_crs('EPSG:4326')
gp_list_gdf
# Filter out instances with no 'list' (e.g. things like specialist clinics)
= gp_list[~gp_list['Total List Size'].isna()]
gp_list
# reduce to the southwest to not overload Folium
= -6.449974,-2.717735
xmin, xmax = 49.814737,51.246969
ymin, ymax = gp_list_gdf.cx[xmin:xmax, ymin:ymax] gp_list_gdf_sw
21 Interactive Maps - Point Data
Note
Let’s first load in the dataset we will be working with.
To load in point data we 1. Filter out instances in our dataframe with no geometry (they’ll cause problems!)
Make a list from the geometry column to iterate through
Use a for loop at add markers to our empty folium map
Let’s do step 1 and 2 first - the data prep steps.
# Filter out instances with no geometry
= gp_list_gdf_sw[~gp_list_gdf_sw['geometry'].is_empty]
gp_list_gdf_sw
# Create a geometry list from the GeoDataFrame
= [[point.xy[1][0], point.xy[0][0]] for point in gp_list_gdf_sw.geometry] geo_df_list
Now let’s make the map.
import folium
# Make the empty map
= folium.Map(
gp_map_interactive =[50.7, -4.2],
location=8,
zoom_start='openstreetmap',
tiles
)
# Add markers to the map
for coordinates in geo_df_list:
= gp_map_interactive.add_child(
gp_map_interactive
folium.Marker(=coordinates
location
)
)
gp_map_interactive
Make this Notebook Trusted to load map: File -> Trust Notebook
21.1 Custom markers
21.1.1 Web markers
We can pass in a custom marker from the fontawesome library in the for loop.
= folium.Map(
gp_map_web_icon =[50.7, -4.2],
location=8,
zoom_start='openstreetmap',
tiles
)
for coordinates in geo_df_list:
= gp_map_web_icon.add_child(
gp_map_web_icon
folium.Marker(=coordinates,
location=folium.Icon(icon="user-md", prefix='fa', color="black")
icon
)
)
gp_map_web_icon
Make this Notebook Trusted to load map: File -> Trust Notebook
Warning
As of Feb 2024, this only supports fontawesome v4 - this link will show you all available icons: https://fontawesome.com/v4/icons/
21.1.2 Markers stored locally
If we want to use an icon stored on our local machine, we have to do it slightly differently.
While it seems inefficient to remake the icon each time inside the loop, it won’t work otherwise!
= folium.Map(
gp_map_local_icon =[50.7, -4.2],
location=8,
zoom_start='openstreetmap',
tiles
)
for coordinates in geo_df_list:
= folium.features.CustomIcon(
custom_icon "resources/logos/hsma_logo_transparent_background_small.png",
=(48,48)
icon_size
)
= gp_map_local_icon.add_child(
gp_map_local_icon
folium.Marker(=coordinates,
location=custom_icon
icon
)
)
gp_map_local_icon
Make this Notebook Trusted to load map: File -> Trust Notebook