|
5 | 5 | import gc |
6 | 6 | import os |
7 | 7 | from contextlib import asynccontextmanager |
8 | | -from typing import Dict |
| 8 | +from typing import Dict, List |
9 | 9 |
|
10 | 10 | import geopandas as gpd |
11 | 11 | import mlflow |
|
17 | 17 | from pyproj import Transformer |
18 | 18 | from shapely.geometry import box |
19 | 19 | from shapely.geometry import Point |
20 | | -import requests |
21 | 20 |
|
22 | 21 | from app.logger_config import configure_logger |
23 | 22 | from app.utils import ( |
@@ -94,53 +93,49 @@ def show_welcome_page(): |
94 | 93 |
|
95 | 94 | @app.get("/find_image", tags=["Find Image"]) |
96 | 95 | async def find_image( |
97 | | - lon_gps: float, |
98 | | - lat_gps: float, |
99 | | - nuts_id: str, |
| 96 | + gps_point: List[float] = Query(..., description="[longitude, latitude] in WGS84"), |
| 97 | + nuts_id: str = Query(...), |
100 | 98 | year: int = Query(2021, ge=2018, le=2024) |
101 | 99 | ) -> str: |
102 | 100 | """ |
103 | 101 | Find image path for a given NUTS3 and year. |
104 | 102 |
|
105 | 103 | Args: |
106 | | - lon_gps (float): longitude of the gps point. |
107 | | - lat_gps (float): longitude of the gps point. |
| 104 | + gps_point (List[float]): [longitude, latitude] of the GPS point in WGS84. |
108 | 105 | nuts_id (str): The ID of the NUTS. |
109 | 106 | year (int): The year of the satellite images. |
110 | 107 | Returns: |
111 | | - str: Image filepath if the image is finded, otherwise None. |
112 | | -
|
| 108 | + str: Image filepath if found, otherwise empty string. |
113 | 109 | """ |
| 110 | + lon_gps, lat_gps = gps_point[0], gps_point[1] |
114 | 111 | logger.info(f"Find the image filepath for this gps point: {lon_gps}, {lat_gps}") |
115 | 112 | gc.collect() |
116 | 113 |
|
117 | 114 | url = f"https://minio.lab.sspcloud.fr/projet-formation/diffusion/funathon/2026/project3/data/images/{nuts_id}/{year}/filename2bbox.parquet" |
118 | 115 |
|
119 | | - response = requests.head(url) |
120 | | - |
121 | | - if response.status_code == 200: |
| 116 | + try: |
122 | 117 | df = pd.read_parquet(url) |
123 | | - else: |
124 | | - print(f"❌ No data for NUTS3='{nuts_id}' and year={year} (HTTP {response.status_code})") |
125 | | - return None |
| 118 | + except Exception: |
| 119 | + print(f"❌ No data for NUTS3='{nuts_id}' and year={year}") |
| 120 | + return "" |
126 | 121 |
|
127 | | - # Création de la géométrie |
128 | 122 | df["geometry"] = df.apply( |
129 | 123 | lambda row: box(row["bbox"][0], row["bbox"][1], row["bbox"][2], row["bbox"][3]), |
130 | 124 | axis=1 |
131 | 125 | ) |
132 | 126 |
|
133 | | - # Conversion en GeoDataFrame |
134 | 127 | gdf = gpd.GeoDataFrame(df, geometry="geometry", crs="EPSG:3035") |
135 | 128 |
|
136 | | - # Convertir le point GPS (EPSG:4326) en EPSG:3035 |
137 | 129 | transformer = Transformer.from_crs("EPSG:4326", "EPSG:3035", always_xy=True) |
138 | 130 | x, y = transformer.transform(lon_gps, lat_gps) |
139 | 131 |
|
140 | 132 | point = Point(x, y) |
141 | 133 | result = gdf[gdf.contains(point)] |
142 | 134 |
|
143 | | - return result["filename"].values |
| 135 | + if result.empty: |
| 136 | + return "" |
| 137 | + |
| 138 | + return str(result["filename"].values[0]) |
144 | 139 |
|
145 | 140 |
|
146 | 141 | @app.get("/predict_image", tags=["Predict Image"]) |
|
0 commit comments