TCJ21 commited on
Commit
ea68916
·
1 Parent(s): ed0c261

update silvia

Browse files
Files changed (1) hide show
  1. app/src/gfm.py +22 -46
app/src/gfm.py CHANGED
@@ -97,62 +97,38 @@ def download_gfm_geojson(area_name, bbox, new_coordinates=None, output_file_path
97
  if aoi["aoi_name"] == area_name:
98
  aoi_id = aoi["aoi_id"]
99
  break
100
- # print(aoi)
101
 
102
- # # Collect all matching AOIs (same name)
103
- # matching_geometries = []
104
- # for aoi in response.json()["aois"]:
105
- # if aoi["aoi_name"] == area_name:
106
- # geojson_geometry = aoi["geoJSON"]["geometry"]
107
- # matching_geometries.append(shape(geojson_geometry))
108
-
109
- # if not matching_geometries:
110
- # raise ValueError(f"No AOIs found for area name: {area_name}")
111
-
112
- # # Merge all matching AOI geometries into a single unified polygon
113
- # merged_geometry = unary_union(matching_geometries)
114
-
115
- # # Handle MultiPolygon cases (if AOIs are disjointed)
116
- # if merged_geometry.geom_type == "MultiPolygon":
117
- # merged_geometry = MultiPolygon([p for p in merged_geometry])
118
-
119
- # # Convert back to GeoJSON
120
- # merged_geojson = {
121
- # "type": "Feature",
122
- # "properties": {"aoi_name": area_name},
123
- # "geometry": json.loads(json.dumps(merged_geometry.__geo_interface__)),
124
- # }
125
-
126
- # print(f"Merged {len(matching_geometries)} AOIs into one for '{area_name}'.")
127
-
128
- # return merged_geojson
129
-
130
- # Get product id
131
  prod_url = f"{base_url}/aoi/{aoi_id}/products"
132
  response = requests.get(prod_url, headers=header)
133
- product_id = response.json()["products"][0]["product_id"]
134
- print(f"got product_id {product_id}")
135
-
136
- # Get download link
137
- download_url = f"{base_url}/download/product/{product_id}"
138
- response = requests.get(download_url, headers=header)
139
- download_link = response.json()["download_link"]
140
- print("Got download link")
141
 
142
- # Set output file path and create directory if it doesn't exist
143
  if not output_file_path:
144
  output_file_path = f"./output/{area_name}"
145
 
146
  Path(output_file_path).mkdir(parents=True, exist_ok=True)
147
 
148
- # Download and unzip file
149
- for f in Path(output_file_path).glob("*"):
150
  f.unlink()
151
- print("Donwloading...")
152
- r = requests.get(download_link)
153
- print("Extracting zip...")
154
- with zipfile.ZipFile(io.BytesIO(r.content)) as z:
155
- z.extractall(str(Path(output_file_path)))
 
 
 
 
 
 
 
 
 
 
 
156
  print("Done!")
157
 
158
 
 
97
  if aoi["aoi_name"] == area_name:
98
  aoi_id = aoi["aoi_id"]
99
  break
 
100
 
101
+ # Get all product IDs
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
  prod_url = f"{base_url}/aoi/{aoi_id}/products"
103
  response = requests.get(prod_url, headers=header)
104
+ products = response.json()["products"]
105
+ print(f"Found {len(products)} products for {area_name}")
 
 
 
 
 
 
106
 
107
+ # Set output path
108
  if not output_file_path:
109
  output_file_path = f"./output/{area_name}"
110
 
111
  Path(output_file_path).mkdir(parents=True, exist_ok=True)
112
 
113
+ # Remove existing flood files before downloading
114
+ for f in Path(output_file_path).glob("*FLOOD*.geojson"):
115
  f.unlink()
116
+
117
+ # Download all available flood products
118
+ for product in products:
119
+ product_id = product["product_id"]
120
+ print(f"Downloading product: {product_id}")
121
+
122
+ download_url = f"{base_url}/download/product/{product_id}"
123
+ response = requests.get(download_url, headers=header)
124
+ download_link = response.json()["download_link"]
125
+
126
+ # Download and unzip file
127
+ r = requests.get(download_link)
128
+ with zipfile.ZipFile(io.BytesIO(r.content)) as z:
129
+ print("Extracting...")
130
+ z.extractall(str(Path(output_file_path)))
131
+
132
  print("Done!")
133
 
134