Skip to content
This repository was archived by the owner on Jul 11, 2025. It is now read-only.

Commit 1503c71

Browse files
Merge pull request #56 from open-AIMS/cache-null-sites
Cache null sites
2 parents f1eb039 + 26823fc commit 1503c71

File tree

3 files changed

+48
-13
lines changed

3 files changed

+48
-13
lines changed

src/criteria_assessment/criteria.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,12 @@ function setup_region_routes(config, auth)
230230
# Specifically clear from memory to invoke garbage collector
231231
assessed = nothing
232232

233-
if size(best_sites, 1) == 0
234-
return json(nothing)
233+
if nrow(best_sites) == 0
234+
open(suitable_sites_fn, "w") do f
235+
JSON.print(f, nothing)
236+
end
237+
238+
return file(suitable_sites_fn)
235239
end
236240

237241
output_geojson(suitable_sites_fn, best_sites)

src/criteria_assessment/tiles.jl

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -182,23 +182,22 @@ function setup_tile_routes(config, auth)
182182
reg = qp["region"]
183183
rtype = qp["rtype"]
184184

185-
criteria_names, lbs, ubs = remove_rugosity(reg, parse_criteria_query(qp)...)
186-
187185
# Calculate tile bounds
188186
lon_min, lon_max, lat_max, lat_min = _tile_bounds(z, x, y)
187+
lons, lats = (lon_min, lon_max), (lat_max, lat_min)
189188
@debug "Thread $(thread_id) - $(now()) : Calculated bounds (z/x/y, lon bounds, lat bounds): $z $x $y | $(_tile_to_lon_lat(z, x, y)) | ($(lon_min), $(lon_max)), ($(lat_min), $(lat_max))"
190189

191-
# Extract relevant data based on tile coordinates
192-
@debug "Thread $(thread_id) - $(now()) : Extracting tile data"
193-
mask_data = threshold_mask(
194-
reg_assess_data[reg],
195-
Symbol(rtype),
196-
CriteriaBounds.(criteria_names, lbs, ubs),
197-
(lon_min, lon_max),
198-
(lat_min, lat_max)
190+
# Check if request is within target region
191+
# return empty tile if not.
192+
lookup = getfield(reg_assess_data[reg], Symbol(:valid_, rtype))
193+
lat1, lat2 = lats[1] > lats[2] ? (lats[2], lats[1]) : (lats[1], lats[2])
194+
195+
within_search = (
196+
(lons[1] .<= lookup.lons .<= lons[2]) .&
197+
(lat1 .<= lookup.lats .<= lat2)
199198
)
200199

201-
if any(size(mask_data) .== 0)
200+
if !any(within_search)
202201
no_data_path = cache_filename(
203202
Dict("no_data" => "none"), config, "no_data", "png"
204203
)
@@ -207,6 +206,34 @@ function setup_tile_routes(config, auth)
207206
return file(no_data_path; headers=TILE_HEADERS)
208207
end
209208

209+
# TODO: Re-use pre-existing cache for entire region if available
210+
# Get mask data if available
211+
# assessed_fn = cache_filename(
212+
# extract_criteria(qp, suitability_criteria()), config, "$(reg)_suitable", "tiff"
213+
# )
214+
215+
# TODO: Use previously generated results
216+
# assessed_fn = assess_region(config, qp, reg, rtype, reg_assess_data)
217+
# if isfile(assessed_fn)
218+
# # assessed = Raster(assessed_fn; lazy=true)
219+
# # Load regional data, subset to area of interest and assess
220+
# mask_data =
221+
# else
222+
# # Otherwise, assess the target area directly
223+
# end
224+
225+
criteria_names, lbs, ubs = remove_rugosity(reg, parse_criteria_query(qp)...)
226+
227+
# Extract relevant data based on tile coordinates
228+
@debug "Thread $(thread_id) - $(now()) : Extracting tile data"
229+
mask_data = threshold_mask(
230+
reg_assess_data[reg],
231+
Symbol(rtype),
232+
CriteriaBounds.(criteria_names, lbs, ubs),
233+
(lon_min, lon_max),
234+
(lat_min, lat_max)
235+
)
236+
210237
@debug "Thread $(thread_id) - Extracted data size: $(size(mask_data))"
211238

212239
@debug "Thread $(thread_id) - $(now()) : Creating PNG (with transparency)"

src/site_assessment/common_functions.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,10 @@ containing only sites with scores greater than the `surr_threshold` specified in
224224
`identify_edge_aligned_sites()` (default=0.6).
225225
"""
226226
function filter_sites(res_df::DataFrame)::DataFrame
227+
if nrow(res_df) == 0
228+
return res_df
229+
end
230+
227231
res_df.row_ID = 1:size(res_df, 1)
228232
ignore_list = Int64[]
229233

0 commit comments

Comments
 (0)