def create_marker_cluster_map():
"""Create a map with marker clustering for large datasets"""
np.random.seed(123)
n_locations = 5000
lats = np.random.uniform(25, 49, n_locations)
lons = np.random.uniform(-125, -65, n_locations)
values = np.random.randint(1, 100, n_locations)
df_markers = pd.DataFrame({
'lat': lats,
'lon': lons,
'worth': values
})
m = folium.Map(location=[37.8, -96], zoom_start=4)
marker_cluster = MarkerCluster(
identify="Location Cluster",
overlay=True,
management=True
).add_to(m)
for idx, row in df_markers.iterrows():
if row['value'] < 33:
shade="green"
elif row['value'] < 66:
shade="orange"
else:
shade="red"
folium.Marker(
location=[row['lat'], row['lon']],
popup=f"Value: {row['value']}",
tooltip=f"Location {idx}",
icon=folium.Icon(shade=shade, icon='info-sign')
).add_to(marker_cluster)
folium.LayerControl().add_to(m)
title_html=""'
Marker Clustering Demo
5000 markers - zoom to see particular person factors
'''
m.get_root().html.add_child(folium.Aspect(title_html))
return m
def create_time_series_map():
"""Create an animated map showing data changes over time"""
start_date = datetime(2024, 8, 1)
options = []
path = [
[25.0, -70.0], [26.5, -72.0], [28.0, -74.5], [29.5, -76.5],
[31.0, -78.0], [32.5, -79.5], [34.0, -80.5], [35.5, -81.0]
]
for i, (lat, lon) in enumerate(path):
timestamp = start_date + timedelta(hours=i*6)
characteristic = {
'kind': 'Function',
'geometry': {
'kind': 'Level',
'coordinates': [lon, lat]
},
'properties': {
'time': timestamp.isoformat(),
'popup': f'Hurricane Place
Time: {timestamp.strftime("%Y-%m-%d %H:%M")}
Class: {min(5, i//2 + 1)}',
'icon': 'circle',
'iconstyle': {
'fillColor': ['yellow', 'orange', 'red', 'darkred', 'purple'][min(4, i//2)],
'fillOpacity': 0.8,
'stroke': 'true',
'radius': 8 + i * 2
}
}
}
options.append(characteristic)
m = folium.Map(
location=[30.0, -75.0],
zoom_start=5,
tiles="CartoDB Positron"
)
TimestampedGeoJson(
{'kind': 'FeatureCollection', 'options': options},
interval='PT6H',
add_last_point=True,
auto_play=True,
loop=True,
max_speed=2,
loop_button=True,
date_options="YYYY-MM-DD HH:mm",
time_slider_drag_update=True
).add_to(m)
title_html=""'
Hurricane Path Animation
Simulated hurricane monitoring
Use controls under to play/pause
'''
m.get_root().html.add_child(folium.Aspect(title_html))
return m
def create_interactive_plugins_map():
"""Create a map with multiple interactive plugins"""
m = folium.Map(
location=[40.7128, -74.0060],
zoom_start=12,
tiles="OpenStreetMap"
)
minimap = MiniMap(toggle_display=True)
m.add_child(minimap)
draw = Draw(
export=True,
filename="drawn_shapes.geojson",
place='topleft',
draw_options={
'polyline': True,
'polygon': True,
'circle': True,
'rectangle': True,
'marker': True,
'circlemarker': True
},
edit_options={'edit': True}
)
m.add_child(draw)
Fullscreen(
place='topright',
title="Expand map",
title_cancel="Exit fullscreen",
force_separate_button=True
).add_to(m)
plugins.MeasureControl(
place='bottomleft',
primary_length_unit="kilometers",
secondary_length_unit="miles",
primary_area_unit="sqkilometers",
secondary_area_unit="acres"
).add_to(m)
plugins.MousePosition(
place='bottomright',
separator=" | ",
empty_string='NaN',
lng_first=True,
num_digits=20,
prefix='Coordinates:',
).add_to(m)
plugins.LocateControl(
auto_start=False,
place='topleft'
).add_to(m)
folium.Marker(
[40.7128, -74.0060],
popup='NYC
Attempt the drawing instruments!',
icon=folium.Icon(shade="red", icon='info-sign')
).add_to(m)
return m
def create_earthquake_map():
"""Create comprehensive earthquake visualization using real USGS data"""
url="
attempt:
response = requests.get(url)
earthquake_data = response.json()
print(f"Successfully loaded {len(earthquake_data['features'])} earthquakes")
besides Exception as e:
print(f"Error fetching data: {e}")
earthquake_data = {
'options': [
{
'properties': {'mag': 5.2, 'place': 'Sample Location 1', 'time': 1640000000000},
'geometry': {'coordinates': [-122.0, 37.0, 10]}
},
{
'properties': {'magazine': 6.1, 'place': 'Pattern Location 2', 'time': 1640100000000},
'geometry': {'coordinates': [140.0, 35.0, 20]}
}
]
}
earthquakes = []
for characteristic in earthquake_data['features']:
props = characteristic['properties']
coords = characteristic['geometry']['coordinates']
earthquakes.append({
'lat': coords[1],
'lon': coords[0],
'depth': coords[2],
'magnitude': props.get('magazine', 0),
'place': props.get('place', 'Unknown'),
'time': datetime.fromtimestamp(props.get('time', 0) / 1000)
})
df_eq = pd.DataFrame(earthquakes)
print(f"nEarthquake Statistics:")
print(f"Total earthquakes: {len(df_eq)}")
print(f"Magnitude range: {df_eq['magnitude'].min():.1f} - {df_eq['magnitude'].max():.1f}")
print(f"Depth range: {df_eq['depth'].min():.1f} - {df_eq['depth'].max():.1f} km")
m = folium.Map(
location=[20, 0],
zoom_start=2,
tiles="CartoDB dark_matter"
)
minor = folium.FeatureGroup(identify="Minor (< 4.0)")
average = folium.FeatureGroup(identify="Moderate (4.0-5.0)")
sturdy = folium.FeatureGroup(identify="Strong (5.0-6.0)")
main = folium.FeatureGroup(identify="Major (≥ 6.0)")
for idx, eq in df_eq.iterrows():
magazine = eq['magnitude']
if magazine < 4.0:
shade="green"
radius = 3
group = minor
elif magazine < 5.0:
shade="yellow"
radius = 6
group = average
elif magazine < 6.0:
shade="orange"
radius = 9
group = sturdy
else:
shade="red"
radius = 12
group = main
popup_html = f"""
Magnitude {magazine:.1f}
Location: {eq['place']}
Depth: {eq['depth']:.1f} km
Time: {eq['time'].strftime('%Y-%m-%d %H:%M:%S')}
Coordinates: {eq['lat']:.4f}, {eq['lon']:.4f}
"""
folium.CircleMarker(
location=[eq['lat'], eq['lon']],
radius=radius,
popup=folium.Popup(popup_html, max_width=270),
tooltip=f"M{mag:.1f} - {eq['place']}",
shade=shade,
fill=True,
fillColor=shade,
fillOpacity=0.7,
weight=2
).add_to(group)
minor.add_to(m)
average.add_to(m)
sturdy.add_to(m)
main.add_to(m)
heat_data = [[row['lat'], row['lon'], row['magnitude']] for idx, row in df_eq.iterrows()]
heatmap = folium.FeatureGroup(identify="Density Heatmap", present=False)
HeatMap(
heat_data,
min_opacity=0.3,
radius=15,
blur=20,
gradient={0.4: 'blue', 0.6: 'cyan', 0.7: 'lime', 0.8: 'yellow', 1: 'pink'}
).add_to(heatmap)
heatmap.add_to(m)
folium.LayerControl(place='topright', collapsed=False).add_to(m)
legend_html=""'
Earthquake Magnitude
● Minor (< 4.0)
● Reasonable (4.0-5.0)
● Sturdy (5.0-6.0)
● Main (≥ 6.0)
Information: USGS (Previous 30 days)
'''
m.get_root().html.add_child(folium.Aspect(legend_html))
title_html=""'
🌍 International Earthquake Monitor
Actual-time earthquake knowledge (M ≥ 2.5)
Click on markers for particulars | Toggle layers to discover
'''
m.get_root().html.add_child(folium.Aspect(title_html))
Fullscreen(place='topright').add_to(m)
return m
if __name__ == "__main__":
print("=" * 80)
print("ADVANCED FOLIUM TUTORIAL - ALL EXAMPLES")
print("=" * 80)
print("nGenerating all maps...n")
maps = {
'multi_tile_map': create_multi_tile_map(),
'advanced_markers_map': create_advanced_markers_map(),
'heatmap': create_heatmap(),
'choropleth_map': create_choropleth_map(),
'marker_cluster_map': create_marker_cluster_map(),
'time_series_map': create_time_series_map(),
'interactive_plugins_map': create_interactive_plugins_map(),
'earthquake_map': create_earthquake_map()
}
print("n" + "=" * 80)
print("SAVING MAPS TO HTML FILES")
print("=" * 80)
for identify, map_obj in maps.gadgets():
if map_obj isn't None:
filename = f"{name}.html"
map_obj.save(filename)
print(f"✓ Saved: {filename}")
else:
print(f"✗ Skipped: {name} (map generation failed)")
print("n" + "=" * 80)
print("ALL MAPS GENERATED SUCCESSFULLY!")
print("=" * 80)
print("nYou can now:")
print("1. Open any HTML file in your browser to view the interactive map")
print("2. Access the map objects in code using the 'maps' dictionary")
print("3. Display maps in Jupyter/Colab by returning the map object")
print("nExample: To display the earthquake map in a notebook, just run:")
print(" maps['earthquake_map']")
print("n" + "=" * 80)
Trending
- How the Latest Windows Update Enhances Gaming Performance on PCs
- How Microsoft’s New AI Tool Enhances Productivity for Remote Teams
- How Apple’s Latest iOS Update Enhances Privacy and User Control
- Nvidia’s Latest RTX 5090 Offers Enhanced Graphics Performance and Pricing Details
- Microsoft Introduces Enhanced Features in Windows 11 for Greater User Control
- Microsoft’s Latest Windows Update Enhances Security and Performance Features
- Microsoft’s Latest Update Improves Security Features for Cloud Services
- New on TechnologiesDigest: Best AI Tools for Developer Productivity
Tips on how to Construct Interactive Geospatial Dashboards Utilizing Folium with Heatmaps, Choropleths, Time Animation, Marker Clustering, and Superior Interactive Plugins
Advertisement
Advertisement
Carter
Hi, I'm Carter, a Wisconsin-based tech writer and creator. I launched this blog as a comprehensive resource to help people navigate the fast-moving tech landscape. Covering multiple facets of the industry—from software insights to digital tools—my goal is to break down complex concepts into actionable, easy-to-understand guides. When I'm not tracking the latest tech trends, I'm focused on developing my skills in business administration and digital management.







