Skip to content

Commit 6c58323

Browse files
author
santiagoC
committed
feat: enhance point data processing with bounds checking and NaN handling
1 parent 7d4ef60 commit 6c58323

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

src/routes/get_geoserver_point_data.py

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ def process_date_data(date_info: Dict, coordinates: List[List[float]], auth: tup
4949

5050
try:
5151
response = requests.get(url, auth=auth)
52-
5352
if response.status_code == 404:
5453
# No data for this date
5554
return results
@@ -59,23 +58,51 @@ def process_date_data(date_info: Dict, coordinates: List[List[float]], auth: tup
5958
# Process the raster and extract values for each coordinate
6059
with MemoryFile(response.content) as memfile:
6160
with memfile.open() as raster:
61+
data_array = raster.read(1)
62+
6263
for coord in coordinates:
6364
lon, lat = coord[0], coord[1]
6465

6566
try:
67+
# Check if coordinate is within bounds
68+
if not (raster.bounds.left <= lon <= raster.bounds.right and
69+
raster.bounds.bottom <= lat <= raster.bounds.top):
70+
continue
71+
6672
# Get value from specific point
6773
row, col = raster.index(lon, lat)
68-
value = raster.read(1)[row, col]
74+
75+
# Check if indices are within raster dimensions
76+
if row < 0 or row >= raster.height or col < 0 or col >= raster.width:
77+
continue
78+
79+
value = data_array[row, col]
80+
81+
# If the exact pixel is NaN, try nearby pixels
82+
if np.isnan(value):
83+
# Check 3x3 neighborhood
84+
found_valid = False
85+
for dr in [-1, 0, 1]:
86+
for dc in [-1, 0, 1]:
87+
nr, nc = row + dr, col + dc
88+
if (0 <= nr < raster.height and 0 <= nc < raster.width):
89+
nearby_value = data_array[nr, nc]
90+
if not np.isnan(nearby_value) and nearby_value != -9999:
91+
value = nearby_value
92+
found_valid = True
93+
break
94+
if found_valid:
95+
break
6996

7097
# Filter invalid values
71-
if value != -9999 and not np.isnan(value):
98+
if not np.isnan(value) and value != -9999 and (raster.nodata is None or value != raster.nodata):
7299
results.append(PointDataResult(
73100
coordinate=[lon, lat],
74101
date=date_str,
75102
value=float(value)
76103
))
104+
77105
except Exception:
78-
# Coordinate outside raster bounds, continue
79106
continue
80107

81108
except requests.exceptions.RequestException:

0 commit comments

Comments
 (0)