rapsoj commited on
Commit
50dea54
·
verified ·
1 Parent(s): 638809e

Upload 3 files

Browse files
Files changed (3) hide show
  1. README.md +2 -13
  2. app.py +114 -0
  3. requirements.txt +9 -0
README.md CHANGED
@@ -1,14 +1,3 @@
1
- ---
2
- title: E Vca
3
- emoji: 📚
4
- colorFrom: red
5
- colorTo: yellow
6
- sdk: gradio
7
- sdk_version: 5.35.0
8
- app_file: app.py
9
- pinned: false
10
- license: mit
11
- short_description: Download and process hazard layers for Red Cross VCAs.
12
- ---
13
 
14
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
+ # Flood Raster Downloader
 
 
 
 
 
 
 
 
 
 
 
2
 
3
+ Generates merged flood hazard rasters (.tif) by ISO3 code and return period using CEMS data.
app.py ADDED
@@ -0,0 +1,114 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import requests
4
+ import math
5
+ import geopandas as gpd
6
+ import urllib.request
7
+ import rasterio
8
+ import rasterio.merge
9
+ from bs4 import BeautifulSoup
10
+ from datetime import datetime
11
+
12
+ def generate_raster(iso: str, rp: int):
13
+ iso = iso.upper()
14
+
15
+ # --- Get geometry from UNHCR API ---
16
+ geometry_url = "https://gis.unhcr.org/arcgis/rest/services/core_v2/wrl_polbnd_adm1_a_unhcr/MapServer/0/query"
17
+ params = {
18
+ "where": f"iso3 = '{iso}'",
19
+ "outFields": "*",
20
+ "outSR": "4326",
21
+ "f": "geojson"
22
+ }
23
+ response = requests.get(geometry_url, params=params)
24
+ if response.status_code != 200:
25
+ return None, f"❌ Failed to fetch geometry for {iso}"
26
+
27
+ data = response.json()
28
+ if "features" not in data or not data["features"]:
29
+ return None, f"❌ No features found for {iso}"
30
+
31
+ gdf = gpd.GeoDataFrame.from_features(data["features"])
32
+ bounds = gdf.total_bounds # [left, bottom, right, top]
33
+
34
+ # --- Determine tiles to download ---
35
+ top = math.ceil(bounds[3] / 10) * 10
36
+ left = (int(bounds[0]) // 10) * 10
37
+ right = math.ceil(bounds[2] / 10) * 10
38
+ bottom = (int(bounds[1]) // 10) * 10
39
+
40
+ url_base = f"https://jeodpp.jrc.ec.europa.eu/ftp/jrc-opendata/CEMS-GLOFAS/flood_hazard/RP{rp}/"
41
+ page = requests.get(url_base)
42
+ soup = BeautifulSoup(page.text, "html.parser")
43
+ all_links = [a['href'] for a in soup.find_all('a') if a['href'].endswith('.tif')]
44
+
45
+ tif_paths = []
46
+ col = left
47
+ while col < right:
48
+ row = top
49
+ while row > bottom:
50
+ prefix = f"{'N' if row >= 0 else 'S'}{abs(row)}_{'E' if col >= 0 else 'W'}{abs(col)}"
51
+ match = [l for l in all_links if prefix in l]
52
+ if match:
53
+ tif_paths.append(match[0])
54
+ row -= 10
55
+ col += 10
56
+
57
+ if not tif_paths:
58
+ return None, "❌ No tiles found for selected region."
59
+
60
+ # --- Download and merge ---
61
+ local_paths = []
62
+ for tif in tif_paths:
63
+ remote = url_base + tif
64
+ local = f"/tmp/{tif}"
65
+ urllib.request.urlretrieve(remote, local)
66
+ local_paths.append(local)
67
+
68
+ sources = [rasterio.open(p) for p in local_paths]
69
+ mosaic, transform = rasterio.merge.merge(sources)
70
+
71
+ out_meta = sources[0].meta.copy()
72
+ out_meta.update({
73
+ "driver": "GTiff",
74
+ "height": mosaic.shape[1],
75
+ "width": mosaic.shape[2],
76
+ "transform": transform
77
+ })
78
+
79
+ timestamp = datetime.utcnow().strftime("%Y%m%d%H%M%S")
80
+ output_path = f"./merged_raster_{iso}_{rp}_{timestamp}.tif"
81
+
82
+ with rasterio.open(output_path, "w", **out_meta) as dest:
83
+ dest.write(mosaic)
84
+
85
+ for src in sources:
86
+ src.close()
87
+ for f in local_paths:
88
+ os.remove(f)
89
+
90
+ return output_path, f"✅ Raster generated for {iso} (RP {rp}). Click to download below."
91
+
92
+ # Selection options
93
+ iso_options = [('Abyei', 'XAB'), ('Afghanistan', 'AFG'), ('Aksai Chin (Sovereignty unsettled)', 'XAC'), ('Aland Islands (FIN)', 'ALA'), ('Albania', 'ALB'), ('Algeria', 'DZA'), ('American Samoa', 'ASM'), ('Andorra', 'AND'), ('Angola', 'AGO'), ('Anguilla (GBR)', 'AIA'), ('Antarctica', 'ATA'), ('Antigua and Barbuda', 'ATG'), ('Argentina', 'ARG'), ('Armenia', 'ARM'), ('Aruba (K. of the Netherlands)', 'ABW'), ('Arunachal Pradesh (IND)', 'XAP'), ('Australia', 'AUS'), ('Austria', 'AUT'), ('Azerbaijan', 'AZE'), ('Bahamas', 'BHS'), ('Bahrain', 'BHR'), ('Bangladesh', 'BGD'), ('Barbados', 'BRB'), ('Belarus', 'BLR'), ('Belgium', 'BEL'), ('Belize', 'BLZ'), ('Benin', 'BEN'), ('Bermuda', 'BMU'), ('Bhutan', 'BTN'), ('Bolivarian Republic of Venezuela', 'VEN'), ('Bonaire Sint Eustatius and Saba', 'BES'), ('Bosnia and Herzegovina', 'BIH'), ('Botswana', 'BWA'), ('Bouvet Island', 'BVT'), ('Brazil', 'BRA'), ('British Indian Ocean Territory', 'IOT'), ('British Virgin Islands (GBR)', 'VGB'), ('Brunei Darussalam', 'BRN'), ('Bulgaria', 'BGR'), ('Burkina Faso', 'BFA'), ('Burundi', 'BDI'), ('Cambodia', 'KHM'), ('Cameroon', 'CMR'), ('Canada', 'CAN'), ('Cape Verde', 'CPV'), ('Cayman Islands (GBR)', 'CYM'), ('Central African Republic', 'CAF'), ('Chile', 'CHL'), ('China', 'CHN'), ('China/India (Sovereignty unsettled)', 'XCI'), ('Christmas Island (AUS)', 'CXR'), ('Cocos (Keeling) Islands', 'CCK'), ('Colombia', 'COL'), ('Comoros', 'COM'), ('Cook Islands', 'COK'), ('Costa Rica', 'CRI'), ('Croatia', 'HRV'), ('Cuba', 'CUB'), ('Curacao (K. of Netherlands)', 'CUW'), ('Cyprus', 'CYP'), ('Czech Republic', 'CZE'), ("Côte d'Ivoire", 'CIV'), ("Democratic Poeple's Rep. of Korea", 'PRK'), ('Democratic Republic of the Congo', 'COD'), ('Denmark', 'DNK'), ('Djibouti', 'DJI'), ('Dominica', 'DMA'), ('Dominican Republic', 'DOM'), ('Ecuador', 'ECU'), ('Egypt', 'EGY'), ('El Salvador', 'SLV'), ('Equatorial Guinea', 'GNQ'), ('Eritrea', 'ERI'), ('Estonia', 'EST'), ('Eswatini', 'SWZ'), ('Ethiopia', 'ETH'), ('Falkland Islands (Malvinas)', 'FLK'), ('Faroe Islands (DNK)', 'FRO'), ('Federated States of Micronesia', 'FSM'), ('Fiji', 'FJI'), ('Finland', 'FIN'), ('France', 'FRA'), ('French Guiana (FRA)', 'GUF'), ('French New Caledonia (FRA)', 'NCL'), ('French Polynesia', 'PYF'), ('French Southern and Antarctic Territories', 'ATF'), ('Gabon', 'GAB'), ('Gambia', 'GMB'), ('Georgia', 'GEO'), ('Germany', 'DEU'), ('Ghana', 'GHA'), ('Gibraltar', 'GIB'), ('Greece', 'GRC'), ('Greenland (DNK)', 'GRL'), ('Grenada', 'GRD'), ('Guadeloupe (FRA)', 'GLP'), ('Guam', 'GUM'), ('Guatemala', 'GTM'), ('Guernsey (GBR)', 'GGY'), ('Guinea', 'GIN'), ('Guinea-Bissau', 'GNB'), ('Guyana', 'GUY'), ('Haiti', 'HTI'), ("Hala'ib triangle (SDN)", 'XHT'), ('Heard Island and McDonald Islands (AUS)', 'HMD'), ('Holy See', 'VAT'), ('Honduras', 'HND'), ('Hong Kong', 'HKG'), ('Hungary', 'HUN'), ('Iceland', 'ISL'), ('Ilemi Triangle (SSD)', 'XIT'), ('India', 'IND'), ('Indonesia', 'IDN'), ('Iraq', 'IRQ'), ('Ireland', 'IRL'), ('Islamic Republic of Iran', 'IRN'), ('Isle of Man', 'IMN'), ('Israel', 'ISR'), ('Italy', 'ITA'), ('Jamaica', 'JAM'), ('Jammu and Kashmir', 'XJK'), ('Jan Mayen Island (NOR)', 'SJM'), ('Japan', 'JPN'), ('Jersey (GBR)', 'JEY'), ('Johnston Atoll', 'JTN'), ('Jordan', 'JOR'), ('Kazakhstan', 'KAZ'), ('Kenya', 'KEN'), ('Kiribati', 'KIR'), ('Kosovo (SRB)', 'KOS'), ('Kuril Islands (RUS)', 'XKI'), ('Kuwait', 'KWT'), ('Kyrgyzstan', 'KGZ'), ("Lao People's Democratic Republic", 'LAO'), ('Latvia', 'LVA'), ('Lebanon', 'LBN'), ('Lesotho', 'LSO'), ('Liberia', 'LBR'), ('Libya', 'LBY'), ('Liechtenstein', 'LIE'), ('Lithuania', 'LTU'), ('Luxembourg', 'LUX'), ("Ma'tan al-Sarra (EGY)", 'XMS'), ('Macao (CHN)', 'MAC'), ('Madagascar', 'MDG'), ('Malawi', 'MWI'), ('Malaysia', 'MYS'), ('Maldives', 'MDV'), ('Mali', 'MLI'), ('Malta', 'MLT'), ('Marshall Islands', 'MHL'), ('Martinique (FRA)', 'MTQ'), ('Mauritania', 'MRT'), ('Mauritius', 'MUS'), ('Mayotte (FRA)', 'MYT'), ('Mexico', 'MEX'), ('Midway Islands', 'MID'), ('Monaco', 'MCO'), ('Mongolia', 'MNG'), ('Montenegro', 'MNE'), ('Montserrat', 'MSR'), ('Morocco', 'MAR'), ('Mozambique', 'MOZ'), ('Myanmar', 'MMR'), ('Namibia', 'NAM'), ('Nauru', 'NRU'), ('Nepal', 'NPL'), ('Netherlands (Kingdom of the)', 'NLD'), ('New Zealand', 'NZL'), ('Nicaragua', 'NIC'), ('Niger', 'NER'), ('Nigeria', 'NGA'), ('Niue', 'NIU'), ('No code (ISO user specified)', 'AAA'), ('Norfolk Island (AUS)', 'NFK'), ('North Macedonia', 'MKD'), ('Northern Mariana Islands (USA)', 'MNP'), ('Norway', 'NOR'), ('Oman', 'OMN'), ('Pakistan', 'PAK'), ('Palau', 'PLW'), ('Panama', 'PAN'), ('Papua New Guinea', 'PNG'), ('Paracel Islands (Sovereignty unsettled)', 'XPI'), ('Paraguay', 'PRY'), ('Peru', 'PER'), ('Philippines', 'PHL'), ('Pitcairn Islands', 'PCN'), ('Plurinational State of Bolivia', 'BOL'), ('Poland', 'POL'), ('Portugal', 'PRT'), ('Puerto Rico', 'PRI'), ('Qatar', 'QAT'), ('Rep. of Chad', 'TCD'), ('Republic of Korea', 'KOR'), ('Republic of Moldova', 'MDA'), ('Republic of the Congo', 'COG'), ('Reunion (FRA)', 'REU'), ('Romania', 'ROU'), ('Russian Federation', 'RUS'), ('Rwanda', 'RWA'), ('Saint Barthelemy (FRA)', 'BLM'), ('Saint Helena', 'SHN'), ('Saint Kitts and Nevis', 'KNA'), ('Saint Lucia', 'LCA'), ('Saint Martin (FRA)', 'MAF'), ('Saint Pierre et Miquelon (FRA)', 'SPM'), ('Saint Vincent and the Grenadines', 'VCT'), ('Samoa', 'WSM'), ('San Marino', 'SMR'), ('Sao Tome and Principe', 'STP'), ('Sark (GBR)', 'XSA'), ('Saudi Arabia', 'SAU'), ('Scarborough Reef (Sovereignty unsettled)', 'XSR'), ('Senegal', 'SEN'), ('Senkaku Islands (Sovereignty unsettled)', 'XSI'), ('Serbia', 'SRB'), ('Seychelles', 'SYC'), ('Sierra Leone', 'SLE'), ('Singapore', 'SGP'), ('Sint Maarten (K. of Netherlands)', 'SXM'), ('Slovakia', 'SVK'), ('Slovenia', 'SVN'), ('Solomon Islands', 'SLB'), ('Somalia', 'SOM'), ('South Africa', 'ZAF'), ('South Georgia and the South Sandwich Islands (GBR)', 'SGS'), ('South Sudan', 'SSD'), ('Spain', 'ESP'), ('Spratly Islands (Sovereignty unsettled)', 'XSP'), ('Sri Lanka', 'LKA'), ('State of Palestine', 'PSE'), ('Sudan', 'SDN'), ('Suriname', 'SUR'), ('Sweden', 'SWE'), ('Switzerland', 'CHE'), ('Syrian Arab Republic', 'SYR'), ('Taiwan (CHN)', 'TWN'), ('Tajikistan', 'TJK'), ('Thailand', 'THA'), ('Timor-Leste', 'TLS'), ('Togo', 'TGO'), ('Tokelau', 'TKL'), ('Tonga', 'TON'), ('Trinidad and Tobago', 'TTO'), ('Tunisia', 'TUN'), ('Turkey', 'TUR'), ('Turkmenistan', 'TKM'), ('Turks and Caicos Islands (GBR)', 'TCA'), ('Tuvalu', 'TUV'), ('Uganda', 'UGA'), ('Ukraine', 'UKR'), ('United Arab Emirates', 'ARE'), ('United Kingdom of Great Britain and Northern Ireland', 'GBR'), ('United Republic of Tanzania', 'TZA'), ('United States Minor Outlying Islands', 'UMI'), ('United States Virgin Islands (USA)', 'VIR'), ('United States of America', 'USA'), ('Uruguay', 'URY'), ('Uzbekistan', 'UZB'), ('Vanuatu', 'VUT'), ('Viet Nam', 'VNM'), ('Wake Island', 'WAK'), ('Wallis and Futuna', 'WLF'), ('Western Sahara', 'ESH'), ('Yemen', 'YEM'), ('Zambia', 'ZMB'), ('Zimbabwe', 'ZWE')]
94
+ rp_options = [10, 20, 50, 75, 100, 200, 500]
95
+
96
+ with gr.Blocks(title="Flood Raster Downloader") as demo:
97
+ gr.Markdown("## E-VCA Flood Raster Downloader\nSelect a country and return period to generate the GloFAS flood hazard raster for that region.")
98
+
99
+ with gr.Row():
100
+ iso_input = gr.Dropdown(choices=iso_options, label="Country", interactive=True)
101
+ rp_input = gr.Dropdown(choices=rp_options, label="Return Period (years)", value=10)
102
+
103
+ generate_btn = gr.Button("Generate Raster")
104
+ status_text = gr.Markdown("")
105
+ file_output = gr.File(label="Download Raster", interactive=False)
106
+
107
+ def wrapper(iso, rp):
108
+ path, msg = generate_raster(iso, rp)
109
+ return path, msg
110
+
111
+ generate_btn.click(fn=wrapper, inputs=[iso_input, rp_input], outputs=[file_output, status_text])
112
+
113
+ if __name__ == "__main__":
114
+ demo.launch(share=True)
requirements.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+
2
+ beautifulsoup4
3
+ geopandas
4
+ gradio
5
+ rasterio
6
+ requests
7
+ shapely
8
+ matplotlib
9
+ numpy