#!/usr/bin/env python # coding: utf-8 # # Using GeoJSON Point Features with Markers # In[22]: import os import folium import geopandas as gpd # In[23]: rootpath = os.path.abspath(os.getcwd()) # In[24]: gdf = gpd.read_file(os.path.join(rootpath, "data", "subwaystations.geojson")) # In[25]: gdf.head() # In[26]: gdf['href'] = '' + gdf.url + "" gdf['service_level'] = gdf.notes.str.split(', ').apply(lambda x: len([v for v in x if "all" in v])) gdf['lines_served'] = gdf.line.str.split('-').apply(lambda x: len(x)) # In[27]: service_levels = gdf.service_level.unique().tolist() service_levels # In[28]: colors = ["orange", "yellow", "green", "blue"] # # Use a Circle as a Marker # In[32]: m = folium.Map(location=[40.75, -73.95], zoom_start=13) folium.GeoJson( gdf, name="Subway Stations", marker=folium.Circle(radius=4, fill_color="orange", fill_opacity=0.4, color="black", weight=1), tooltip=folium.GeoJsonTooltip(fields=["name", "line", "notes"]), popup=folium.GeoJsonPopup(fields=["name", "line", "href", "notes"]), style_function=lambda x: { "fillColor": colors[x['properties']['service_level']], "radius": (x['properties']['lines_served'])*30, }, highlight_function=lambda x: {"fillOpacity": 0.8}, zoom_on_click=True, ).add_to(m) m # ## Or use a DivIcon # In[30]: m = folium.Map(location=[40.75, -73.95], zoom_start=13) def style_function(feature): props = feature.get('properties') markup = f"""
{props.get('name')}
""" return {"html": markup} folium.GeoJson( gdf, name="Subway Stations", marker=folium.Marker(icon=folium.DivIcon()), tooltip=folium.GeoJsonTooltip(fields=["name", "line", "notes"]), popup=folium.GeoJsonPopup(fields=["name", "line", "href", "notes"]), style_function=style_function, zoom_on_click=True, ).add_to(m) m # ## Use a Marker # In[36]: m = folium.Map(location=[40.75, -73.95], zoom_start=13) marker_colors = ["red", "orange", "green", "blue"] folium.GeoJson( gdf, name="Subway Stations", zoom_on_click=True, marker=folium.Marker(icon=folium.Icon(icon='star')), tooltip=folium.GeoJsonTooltip(fields=["name", "line", "notes"]), popup=folium.GeoJsonPopup(fields=["name", "line", "href", "notes"]), style_function=lambda x: { 'markerColor': marker_colors[x['properties']['service_level']], }, ).add_to(m) m