Creating a Python script for interactive maps
The user wants a Python script using Folium to generate interactive maps, likely featuring basic functionality and elements like markers, layers, and possibly GeoJSON. I'll explain the installation requirements for pip and provide a clear example of the code.
The plan is to import Folium, create a map at specified coordinates, add various tile layers, include markers with popups, and possibly demonstrate heatmaps. I’ll ensure it's concise but sufficiently detailed.Drafting a CLI script for mapping
I’m considering a CLI script where the user can either input coordinates directly or use a dataset. The idea is to load a CSV file with point data and generate an HTML map with markers. It’s important to keep it straightforward.
I could start with a basic example, then provide an extended version that handles CSV data. I want to keep explanations concise, including comment lines for clarity, and draft the code with essential features like different tile layers and markers using Folium.Finalizing the interactive map script
I'm wrapping up the Python script for generating interactive maps using Folium. The script saves the map as "map.html" after adding layer control and markers.
I want to include installation instructions for Folium with the command pip install folium, making it clear how users can open the saved HTML in their browser. I'll mention correct plugin imports and base tiles like "Stamen Terrain" while also suggesting ways to customize the map's center or load coordinates. I aim for a concise response with essential code and minimal explanation. Below is a self‑contained Python script that generates an interactive map using Folium, adds markers (with popups and tooltips), multiple base layers, and saves the result as an HTML file you can open in a browser.
1. Install Folium
pip install folium
2. Script: generate_map.py
#!/usr/bin/env python3
"""
generate_map.py
Create an interactive map using Folium and save it as map.html.
"""
import folium
from folium.plugins import MarkerCluster
def create_map(output_file: str = "map.html"):
# Center of the map (latitude, longitude)
center_coords = [39.8283, -98.5795] # roughly the center of the contiguous US
# Create base map
m = folium.Map(
location=center_coords,
zoom_start=4,
tiles="OpenStreetMap"
)
# Add a few alternative base layers
folium.TileLayer("Stamen Terrain", name="Terrain").add_to(m)
folium.TileLayer("Stamen Toner", name="Black & White").add_to(m)
folium.TileLayer("CartoDB positron", name="Light").add_to(m)
# Example data: (name, lat, lon)
cities = [
("New York City", 40.7128, -74.0060),
("Los Angeles", 34.0522, -118.2437),
("Chicago", 41.8781, -87.6298),
("Houston", 29.7604, -95.3698),
("Miami", 25.7617, -80.1918),
]
# Cluster markers so the map stays readable when zoomed out
marker_cluster = MarkerCluster(name="Cities").add_to(m)
for name, lat, lon in cities:
folium.Marker(
location=[lat, lon],
tooltip=name, # text shown when hovering
popup=folium.Popup(
html=f"<b>{name}</b><br>Lat: {lat:.4f}, Lon: {lon:.4f}",
max_width=200
),
icon=folium.Icon(color="blue", icon="info-sign"),
).add_to(marker_cluster)
# Add a circle region around Chicago (example of a radius overlay)
folium.Circle(
location=[41.8781, -87.6298],
radius=50_000, # meters (50 km)
color="crimson",
fill=True,
fill_opacity=0.3,
popup="50 km radius around Chicago",
).add_to(m)
# Add a semi-transparent rectangle (example polygon)
folium.Rectangle(
bounds=[[30.0, -125.0], [50.0, -65.0]], # rough bounding box over US
color="#3388ff",
fill=True,
fill_opacity=0.05,
popup="Example bounding box",
).add_to(m)
# Add layer control (to toggle tiles and overlays)
folium.LayerControl(collapsed=False).add_to(m)
# Save map to HTML
m.save(output_file)
print(f"Map saved to {output_file}")
if __name__ == "__main__":
create_map("map.html")
3. Run the script
python generate_map.py
This will create map.html in the current directory. Open that file in a web browser to interact with the map (pan, zoom, toggle layers, click markers, etc.).
If you want, I can extend this example to read points from a CSV/GeoJSON file or to build a choropleth map.