Upload 4 files
Browse files
app.py
CHANGED
|
@@ -7,6 +7,8 @@ from data.geometry import fetch_gis_subdivisions, has_associated_features
|
|
| 7 |
from data.hotosm import download_hotosm_data
|
| 8 |
from data.ckan import download_resource_ckan
|
| 9 |
|
|
|
|
|
|
|
| 10 |
# Discover available hazard modules dynamically
|
| 11 |
def get_available_hazards():
|
| 12 |
hazard_dir = "hazards"
|
|
@@ -33,6 +35,7 @@ with gr.Blocks(title="Hazard Raster Downloader") as demo:
|
|
| 33 |
generate_btn = gr.Button("Generate Raster", visible=False)
|
| 34 |
status_text = gr.Markdown("")
|
| 35 |
file_output = gr.Files(label="Downloads", interactive=False)
|
|
|
|
| 36 |
|
| 37 |
def wrapper(hazard, iso, rp, subdivision):
|
| 38 |
# Dynamic import of selected hazard module
|
|
@@ -41,9 +44,14 @@ with gr.Blocks(title="Hazard Raster Downloader") as demo:
|
|
| 41 |
generate_func = getattr(hazard_module, f"generate_{hazard}_raster")
|
| 42 |
except (ModuleNotFoundError, AttributeError):
|
| 43 |
return [], f"❌ Error: No raster generation function found for hazard '{hazard}'."
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
# Generate raster
|
| 46 |
-
raster_path, msg = generate_func(iso, rp,
|
| 47 |
|
| 48 |
iso3 = iso.split(",")[-1].strip() if "," in iso else iso
|
| 49 |
|
|
@@ -69,25 +77,46 @@ with gr.Blocks(title="Hazard Raster Downloader") as demo:
|
|
| 69 |
files.extend(admin_path)
|
| 70 |
if pop_paths:
|
| 71 |
files.extend(pop_paths)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
if not files:
|
| 73 |
msg += " ⚠️ No data could be downloaded."
|
| 74 |
-
|
| 75 |
-
return files, msg
|
| 76 |
|
| 77 |
def get_subdivision_options(iso_code):
|
| 78 |
-
has_features,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
if not has_features:
|
| 80 |
-
|
| 81 |
return (
|
| 82 |
gr.update(choices=subdivisions, visible=True, value=None),
|
| 83 |
gr.update(visible=False),
|
| 84 |
-
gr.update(value="✅ Subdivisions loaded. Please select one.")
|
| 85 |
)
|
| 86 |
else:
|
|
|
|
|
|
|
| 87 |
return (
|
|
|
|
| 88 |
gr.update(visible=False),
|
| 89 |
-
gr.update(
|
| 90 |
-
gr.update(value="")
|
| 91 |
)
|
| 92 |
|
| 93 |
iso_input.change(
|
|
@@ -109,7 +138,7 @@ with gr.Blocks(title="Hazard Raster Downloader") as demo:
|
|
| 109 |
generate_btn.click(
|
| 110 |
fn=wrapper,
|
| 111 |
inputs=[hazard_input, iso_input, rp_input, subdivision_input],
|
| 112 |
-
outputs=[file_output, status_text]
|
| 113 |
)
|
| 114 |
|
| 115 |
if __name__ == "__main__":
|
|
|
|
| 7 |
from data.hotosm import download_hotosm_data
|
| 8 |
from data.ckan import download_resource_ckan
|
| 9 |
|
| 10 |
+
from analysis import run_analyses
|
| 11 |
+
|
| 12 |
# Discover available hazard modules dynamically
|
| 13 |
def get_available_hazards():
|
| 14 |
hazard_dir = "hazards"
|
|
|
|
| 35 |
generate_btn = gr.Button("Generate Raster", visible=False)
|
| 36 |
status_text = gr.Markdown("")
|
| 37 |
file_output = gr.Files(label="Downloads", interactive=False)
|
| 38 |
+
analysis_output = gr.Markdown("", label="Analysis Results")
|
| 39 |
|
| 40 |
def wrapper(hazard, iso, rp, subdivision):
|
| 41 |
# Dynamic import of selected hazard module
|
|
|
|
| 44 |
generate_func = getattr(hazard_module, f"generate_{hazard}_raster")
|
| 45 |
except (ModuleNotFoundError, AttributeError):
|
| 46 |
return [], f"❌ Error: No raster generation function found for hazard '{hazard}'."
|
| 47 |
+
|
| 48 |
+
# Extract just the subdivision name if it's not the country-level option
|
| 49 |
+
subdivision_name = None
|
| 50 |
+
if subdivision and not subdivision.endswith("(Country level)"):
|
| 51 |
+
subdivision_name = subdivision
|
| 52 |
+
|
| 53 |
# Generate raster
|
| 54 |
+
raster_path, msg = generate_func(iso, rp, subdivision_name)
|
| 55 |
|
| 56 |
iso3 = iso.split(",")[-1].strip() if "," in iso else iso
|
| 57 |
|
|
|
|
| 77 |
files.extend(admin_path)
|
| 78 |
if pop_paths:
|
| 79 |
files.extend(pop_paths)
|
| 80 |
+
# Run all available analyses
|
| 81 |
+
analysis_results = run_analyses(
|
| 82 |
+
raster_path=raster_path,
|
| 83 |
+
pp_path=pp_path,
|
| 84 |
+
roads_path=roads_path,
|
| 85 |
+
buildings_path=buildings_path,
|
| 86 |
+
admin_path=admin_path,
|
| 87 |
+
pop_paths=pop_paths
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
if analysis_results:
|
| 91 |
+
analysis_msg = analysis_results
|
| 92 |
+
else:
|
| 93 |
+
analysis_msg = "✅ No analyses could be run."
|
| 94 |
+
|
| 95 |
if not files:
|
| 96 |
msg += " ⚠️ No data could be downloaded."
|
| 97 |
+
return files, msg, analysis_msg
|
|
|
|
| 98 |
|
| 99 |
def get_subdivision_options(iso_code):
|
| 100 |
+
has_features, data = has_associated_features(iso_code)
|
| 101 |
+
subdivisions = fetch_gis_subdivisions(iso_code)
|
| 102 |
+
|
| 103 |
+
# Lookup the country name from iso_options
|
| 104 |
+
country_name = next((name for name, code in iso_options if code == iso_code), iso_code)
|
| 105 |
+
|
| 106 |
if not has_features:
|
| 107 |
+
# Only subdivisions available
|
| 108 |
return (
|
| 109 |
gr.update(choices=subdivisions, visible=True, value=None),
|
| 110 |
gr.update(visible=False),
|
| 111 |
+
gr.update(value=f"✅ Subdivisions loaded for {country_name}. Please select one.")
|
| 112 |
)
|
| 113 |
else:
|
| 114 |
+
# Add the whole country as the first option
|
| 115 |
+
country_option = f"{country_name} (Country level)"
|
| 116 |
return (
|
| 117 |
+
gr.update(choices=[country_option] + subdivisions, visible=True, value=country_option),
|
| 118 |
gr.update(visible=False),
|
| 119 |
+
gr.update(value=f"✅ Country and subdivisions loaded for {country_name}. You may choose either.")
|
|
|
|
| 120 |
)
|
| 121 |
|
| 122 |
iso_input.change(
|
|
|
|
| 138 |
generate_btn.click(
|
| 139 |
fn=wrapper,
|
| 140 |
inputs=[hazard_input, iso_input, rp_input, subdivision_input],
|
| 141 |
+
outputs=[file_output, status_text, analysis_output]
|
| 142 |
)
|
| 143 |
|
| 144 |
if __name__ == "__main__":
|