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

Commit 84b49bb

Browse files
Implement endpoint to retrieve criteria ranges
1 parent 9af7d94 commit 84b49bb

File tree

2 files changed

+16
-103
lines changed

2 files changed

+16
-103
lines changed

src/ReefGuideAPI.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ function start_server(config_path)
6868
@info "Setting up auth middleware and router."
6969
auth = get_auth_router(config)
7070

71+
@info "Setting up criteria routes..."
72+
setup_criteria_routes(config, auth)
73+
7174
@info "Setting up region routes..."
7275
setup_region_routes(config, auth)
7376

src/criteria_assessment/criteria.jl

Lines changed: 13 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -122,113 +122,23 @@ function criteria_middleware(handle)
122122
end
123123
end
124124

125-
# Not sure why, but this ain't working
126-
# reeftype_router = router("/suitability", middleware=[criteria_middleware], tags=["suitability"])
127-
128-
function setup_region_routes(config, auth)
125+
function setup_criteria_routes(config, auth)
129126
reg_assess_data = setup_regional_data(config)
130127

131-
@get auth("/assess/{reg}/{rtype}") function (req::Request, reg::String, rtype::String)
132-
qp = queryparams(req)
133-
mask_path = cache_filename(qp, config, "", "tiff")
134-
if isfile(mask_path)
135-
return file(mask_path; headers=COG_HEADERS)
136-
end
137-
138-
# Otherwise, create the file
139-
@debug "$(now()) : Assessing criteria"
140-
mask_data = mask_region(reg_assess_data, reg, qp, rtype)
141-
142-
@debug "$(now()) : Running on thread $(threadid())"
143-
@debug "Writing to $(mask_path)"
144-
# Writing time: ~10-25 seconds
145-
_write_cog(mask_path, UInt8.(mask_data), config)
146-
147-
return file(mask_path; headers=COG_HEADERS)
148-
end
149-
150-
@get auth("/suitability/assess/{reg}/{rtype}") function (
151-
req::Request, reg::String, rtype::String
152-
)
153-
# somewhere:8000/suitability/assess/region-name/reeftype?criteria_names=Depth,Slope&lb=-9.0,0.0&ub=-2.0,40
154-
# 127.0.0.1:8000/suitability/assess/Cairns-Cooktown/slopes?Depth=-4.0:-2.0&Slope=0.0:40.0&Rugosity=0.0:6.0
155-
# 127.0.0.1:8000/suitability/assess/Cairns-Cooktown/slopes?Depth=-4.0:-2.0&Slope=0.0:40.0&Rugosity=0.0:6.0&SuitabilityThreshold=95
156-
# 127.0.0.1:8000/suitability/assess/Mackay-Capricorn/slopes?Depth=-12.0:-2.0&Slope=0.0:40.0&Rugosity=0.0:6.0&SuitabilityThreshold=95
157-
qp = queryparams(req)
158-
assessed_fn = assess_region(config, qp, reg, rtype, reg_assess_data)
159-
160-
return file(assessed_fn; headers=COG_HEADERS)
161-
end
162-
163-
@get auth("/suitability/site-suitability/{reg}/{rtype}") function (
164-
req::Request, reg::String, rtype::String
165-
)
166-
# 127.0.0.1:8000/suitability/site-suitability/Cairns-Cooktown/slopes?Depth=-4.0:-2.0&Slope=0.0:40.0&Rugosity=0.0:6.0&SuitabilityThreshold=95&xdist=450&ydist=50
167-
# 127.0.0.1:8000/suitability/site-suitability/Mackay-Capricorn/slopes?Depth=-12.0:-2.0&Slope=0.0:40.0&Rugosity=0.0:6.0&SuitabilityThreshold=95&xdist=100&ydist=100
168-
qp = queryparams(req)
169-
suitable_sites_fn = cache_filename(
170-
qp, config, "$(reg)_potential_sites", "geojson"
171-
)
172-
if isfile(suitable_sites_fn)
173-
return file(suitable_sites_fn)
174-
end
175-
176-
# Assess location suitability if needed
177-
assessed_fn = assess_region(config, qp, reg, rtype, reg_assess_data)
178-
assessed = Raster(assessed_fn; lazy=true, missingval=0)
179-
180-
# Extract criteria and assessment
181-
pixel_criteria = extract_criteria(qp, search_criteria())
182-
deploy_site_criteria = extract_criteria(qp, site_criteria())
183-
184-
best_sites = filter_sites(
185-
assess_sites(
186-
reg_assess_data, reg, rtype, pixel_criteria, deploy_site_criteria, assessed
128+
@get auth("/criteria/ranges") function (req::Request)
129+
# Transform cached criteria ranges to a dictionary for return as json.
130+
criteria_ranges = reg_assess_data["criteria_ranges"]
131+
criteria_names = names(criteria_ranges)
132+
133+
@debug "Transforming criteria dataframe to JSON"
134+
ret = OrderedDict()
135+
for cn in criteria_names
136+
ret[cn] = OrderedDict(
137+
:min_val => criteria_ranges[1, cn],
138+
:max_val => criteria_ranges[2, cn]
187139
)
188-
)
189-
190-
# Specifically clear from memory to invoke garbage collector
191-
assessed = nothing
192-
193-
if nrow(best_sites) == 0
194-
open(suitable_sites_fn, "w") do f
195-
JSON.print(f, nothing)
196-
end
197-
else
198-
output_geojson(suitable_sites_fn, best_sites)
199140
end
200141

201-
return file(suitable_sites_fn)
202-
end
203-
204-
@get auth("/bounds/{reg}") function (req::Request, reg::String)
205-
rst_stack = reg_assess_data[reg].stack
206-
207-
return json(Rasters.bounds(rst_stack))
208-
end
209-
210-
# Form for testing/dev
211-
# https:://somewhere:8000/suitability/assess/region-name/reeftype?criteria_names=Depth,Slope&lb=-9.0,0.0&ub=-2.0,40
212-
@get "/" function ()
213-
return html("""
214-
<form action="/assess/Cairns-Cooktown/slopes" method="post">
215-
<label for="criteria_names">Criteria Names:</label><br>
216-
<input type="text" id="criteria_names" name="criteria"><br>
217-
218-
<label for="lb">Lower Bound:</label><br>
219-
<input type="text" id="lb" name="lower_bound"><br><br>
220-
221-
<label for="ub">Upper Bound:</label><br>
222-
<input type="text" id="ub" name="upper_bound"><br><br>
223-
224-
<input type="submit" value="Submit">
225-
</form>
226-
""")
227-
end
228-
229-
# Parse the form data and return it
230-
@post auth("/form") function (req)
231-
data = formdata(req)
232-
return data
142+
return json(ret)
233143
end
234144
end

0 commit comments

Comments
 (0)