Skip to content

Instantly share code, notes, and snippets.

@jaggzh
Created January 11, 2025 04:12
Show Gist options
  • Save jaggzh/47b1c58617601b5aaab9da6233aaeae1 to your computer and use it in GitHub Desktop.
Save jaggzh/47b1c58617601b5aaab9da6233aaeae1 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# gist-paste -u https://gist.github.com/jaggzh/47b1c58617601b5aaab9da6233aaeae1 mapper-linear
import json
import datetime
import folium
from folium.plugins import TimestampedGeoJson
# Load the fire perimeter data
with open("query-to-2025-01-09_23-14pm.json") as F:
data = json.load(F)
views = {
'overview': {
'zoom': 11,
'co': [34.1622, -118.3577],
},
'palisades': {
'zoom': 12,
'co': [34.1422, -118.4577],
},
}
view = views['palisades']
# Initialize the map centered near Los Angeles
base_map = folium.Map(location=view['co'], zoom_start=view['zoom'])
# Initialize variables
mxf = len(data['features']) # Max features
fi = mxf - 1 # Start from the end
count = 0 # Counter for features processed
# Store features for animation
geojson_features = []
# Loop through features backward in time
while fi >= 0:
feature = data['features'][fi]
fi -= 1 # Move to the previous feature
# Extract attributes and geometry
attr = feature['attributes']
geo = feature['geometry']
ts_s = attr['poly_DateCurrent'] / 1000 # Convert to seconds
date_str = datetime.datetime.fromtimestamp(ts_s).strftime('%Y-%m-%d %H:%M:%S')
# Stop if the date is before 2025-01-07
if ts_s < datetime.datetime(2025, 1, 7).timestamp():
break
# Add each ring as a GeoJSON feature with timestamp
for ring in geo['rings']:
geojson_features.append({
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [ring]
},
"properties": {
"time": datetime.datetime.fromtimestamp(ts_s).isoformat(), # Add timestamp
"style": {
"color": "red",
"fillColor": "orange",
"fillOpacity": 0.5
}
}
})
print(f"Added feature from {date_str} with mission {attr['mission']}")
# Increment count and stop after 21 features (if needed)
count += 1
if count >= 21:
break
# Create a TimestampedGeoJson layer
timestamped_geojson = TimestampedGeoJson({
"type": "FeatureCollection",
"features": geojson_features
}, period="PT1H", add_last_point=True)
# Add the animation layer to the map
timestamped_geojson.add_to(base_map)
# Save the map to an HTML file
base_map.save("animated_fire_perimeter_map.html")
print("Map saved as animated_fire_perimeter_map.html")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment