Skip to content

Commit

Permalink
add metadata to geojson files, plot flooded area over time
Browse files Browse the repository at this point in the history
  • Loading branch information
kleok committed Oct 15, 2024
1 parent 9c57517 commit 54c9dcb
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 39 deletions.
24 changes: 12 additions & 12 deletions Floodpyapp_Vit.ipynb

Large diffs are not rendered by default.

12 changes: 1 addition & 11 deletions floodpy/Visualization/flood_over_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,6 @@

def plot_flooded_area_over_time(Floodpy_app, Floodpy_app_objs):

colorTones = {
6: '#CC3A5D', # dark pink
5: '#555555', # dark grey
4: '#A17C44', # dark brown
3: '#8751A1', # dark purple
2: '#C1403D', # dark red
1: '#2E5A87', # dark blue
0: '#57A35D', # dark green
}

Flooded_regions_areas_km2 = {}
for flood_date in Floodpy_app_objs.keys():
# calculate the area of flooded regions
Expand All @@ -38,9 +28,9 @@ def getcolor(val):

# Adjust the plot
plt.ylabel('Flooded area (km²)', fontsize=16)
plt.title('Flooded Area(km²) Over Time', fontsize=16)
plt.xticks(df['Datetime'].astype(str), df['Datetime'].dt.strftime('%d-%b-%Y'), rotation=30, ha='right', fontsize=16) # Set custom date format
plt.yticks(fontsize=16)
plt.grid()
plt.tight_layout() # Adjust layout for better fit

# Display the plot
Expand Down
78 changes: 78 additions & 0 deletions floodpy/utils/add_metadata_to_geojson.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import geopandas as gpd
import pandas as pd
import numpy as np
import json
import matplotlib.pyplot as plt
import os


def add_metadata(Floodpy_app_objs, Floodpy_app, plot_flag = True):

distinctDarkTones = np.array([
'#264653', # dark teal/gray
'#2a9d8f', # deep green/teal
'#1d3557', # dark blue
'#4b5320', # army green
'#039BE5', # vivid blue
'#006400', # dark green
'#81D4FA', # sky blue
])

# choose the visualization colors
num_flood_events = len(Floodpy_app_objs)
color_indices = np.array([np.ceil(len(distinctDarkTones)*flood_ind/num_flood_events) for flood_ind in range(num_flood_events)], dtype=np.int32)
colors = distinctDarkTones[color_indices]

# calculate the pandas dataframe with flooded regions and add metadata (plot_color and max_entend)
Flooded_regions_df = pd.DataFrame()
for flood_date in Floodpy_app_objs.keys():
# calculate the area of flooded regions
Flood_map_vector_data = gpd.read_file(Floodpy_app_objs[flood_date].Flood_map_vector_dataset_filename)
Flood_map_vector_data_projected = Flood_map_vector_data.to_crs(Flood_map_vector_data.estimate_utm_crs())
area_km2 = round(Flood_map_vector_data_projected.area.sum()/1000000,2 )
flooded_region_temp = pd.DataFrame({'Flooded area (km2)':area_km2,
'geojson_filename':Floodpy_app_objs[flood_date].Flood_map_vector_dataset_filename}, index=[flood_date])
Flooded_regions_df = pd.concat([Flooded_regions_df,flooded_region_temp])

# Ascending sorting of flood events based on flooded area
Flooded_regions_df = Flooded_regions_df.sort_values(by=['Flooded area (km2)'])
Flooded_regions_df['plot_color'] = colors
Flooded_regions_df['max_extend'] = 'false'
max_extend_ind = Flooded_regions_df['Flooded area (km2)'].idxmax()
Flooded_regions_df.loc[max_extend_ind, ['max_extend']] = 'true'

# overwrite existing geojson files with metadata information

for index, row in Flooded_regions_df.iterrows():
with open(row['geojson_filename']) as f:
flooded_regions_json = json.load(f)

#Add top-level metadata (e.g., title, description, etc.)
flooded_regions_json['plot_color'] = row['plot_color']
flooded_regions_json['max_extend'] = row['max_extend']

#Save the modified GeoJSON with metadata to a file
with open(row['geojson_filename'], "w") as f:
json.dump(flooded_regions_json, f, indent=2)


if plot_flag:
Flooded_regions_df['Datetime'] = pd.to_datetime(Flooded_regions_df.index)

df = Flooded_regions_df.sort_index().copy()
# Plot the data
fig = plt.figure(figsize=(6, 5))
plt.bar(df['Datetime'].astype(str), df['Flooded area (km2)'], color='royalblue', width=0.7)

# Adjust the plot
plt.ylabel('Flooded area (km²)', fontsize=16)
plt.xticks(df['Datetime'].astype(str), df['Datetime'].dt.strftime('%d-%b-%Y'), rotation=30, ha='right', fontsize=16) # Set custom date format
plt.yticks(fontsize=16)
plt.grid()
plt.tight_layout() # Adjust layout for better fit

# Display the plot
fig_filename = os.path.join(Floodpy_app.Results_dir, '{}.svg'.format(Floodpy_app.flood_event))
plt.savefig(fig_filename,format="svg")
# plt.close()
print('The figure can be found at: {}'.format(fig_filename))
16 changes: 0 additions & 16 deletions floodpy/utils/geo_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,9 @@
import datetime
import json

colorTones = {
6: '#CC3A5D', # dark pink
5: '#555555', # dark grey
4: '#A17C44', # dark brown
3: '#8751A1', # dark purple
2: '#C1403D', # dark red
1: '#2E5A87', # dark blue
0: '#57A35D', # dark green
}


def create_polygon(coordinates):
return Polygon(np.array(coordinates['coordinates']).squeeze())


def convert_to_vector(Floodpy_app):
with rasterio.open(Floodpy_app.Flood_map_dataset_filename) as src:
data = src.read(1).astype(np.int16)
Expand All @@ -42,16 +30,12 @@ def convert_to_vector(Floodpy_app):
geojson_str = gdf.to_json() # This gives the GeoJSON as a string
geojson_dict = json.loads(geojson_str) # Convert the string to a dictionary

# find the color of plotting
color_ind = Floodpy_app.flood_datetimes.index(Floodpy_app.flood_datetime)
plot_color = colorTones[color_ind]
#Add top-level metadata (e.g., title, description, etc.)
geojson_dict['flood_event'] = Floodpy_app.flood_event
geojson_dict['description'] = "This GeoJSON contains polygons of flooded regions using Sentinel-1 data."
geojson_dict['produced_by'] = "Floodpy"
geojson_dict['creation_date_UTC'] = datetime.datetime.now(datetime.timezone.utc).strftime('%Y%m%dT%H%M%S')
geojson_dict['flood_datetime_UTC'] = Floodpy_app.flood_datetime_str
geojson_dict['plot_color'] = plot_color
geojson_dict['bbox'] = Floodpy_app.bbox

#Save the modified GeoJSON with metadata to a file
Expand Down

0 comments on commit 54c9dcb

Please sign in to comment.