This repository was archived by the owner on Jul 11, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
chore: tidy up and refactoring #68
Merged
PeterBaker0
merged 5 commits into
feat/adding-missing-criteria
from
feat/cleanup-old-func
May 30, 2025
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| """Methods to filter criteria bounds over rasters and lookup tables""" | ||
|
|
||
| """ | ||
| CriteriaBounds combine lookup information for a given criteria, bounds, and a | ||
| rule (function) which enforces it for a given value | ||
| """ | ||
| struct CriteriaBounds{F<:Function} | ||
| "The field ID of the criteria" | ||
| name::Symbol | ||
| "min" | ||
| lower_bound::Float32 | ||
| "max" | ||
| upper_bound::Float32 | ||
| "A function which takes a value and returns if matches the criteria" | ||
| rule::F | ||
|
|
||
| function CriteriaBounds(name::S, lb::S, ub::S)::CriteriaBounds where {S<:String} | ||
| lower_bound::Float32 = parse(Float32, lb) | ||
| upper_bound::Float32 = parse(Float32, ub) | ||
| func = (x) -> lower_bound .<= x .<= upper_bound | ||
|
|
||
| return new{Function}(Symbol(name), lower_bound, upper_bound, func) | ||
| end | ||
|
|
||
| function CriteriaBounds( | ||
| name::String, lb::Float32, ub::Float32 | ||
| )::CriteriaBounds | ||
| func = (x) -> lb .<= x .<= ub | ||
| return new{Function}(Symbol(name), lb, ub, func) | ||
| end | ||
| end | ||
|
|
||
| """ | ||
| Apply thresholds for each criteria. | ||
|
|
||
| # Arguments | ||
| - `criteria_stack` : RasterStack of criteria data for a given region | ||
| - `lookup` : Lookup dataframe for the region | ||
| - `criteria_bounds` : A vector of CriteriaBounds which contains named criteria | ||
| with min/max ranges and a function to apply. | ||
|
|
||
| # Returns | ||
| BitMatrix indicating locations within desired thresholds | ||
| """ | ||
| function filter_raster_by_criteria( | ||
| criteria_stack::RasterStack, | ||
| lookup::DataFrame, | ||
| criteria_bounds::Vector{CriteriaBounds} | ||
| )::Raster | ||
| # Result store | ||
| data = falses(size(criteria_stack)) | ||
|
|
||
| # Apply criteria | ||
| res_lookup = trues(nrow(lookup)) | ||
| for filter::CriteriaBounds in criteria_bounds | ||
| res_lookup .= res_lookup .& filter.rule(lookup[!, filter.name]) | ||
| end | ||
|
|
||
| tmp = lookup[res_lookup, [:lon_idx, :lat_idx]] | ||
| data[CartesianIndex.(tmp.lon_idx, tmp.lat_idx)] .= true | ||
|
|
||
| res = Raster(criteria_stack.Depth; data=sparse(data), missingval=0) | ||
| return res | ||
| end | ||
|
|
||
| """ | ||
| Filters the slope table (which contains raster param values too) by building a | ||
| bit mask AND'd for all thresholds | ||
| """ | ||
| function filter_lookup_table_by_criteria( | ||
| slope_table::DataFrame, | ||
| ruleset::Vector{CriteriaBounds} | ||
| )::DataFrame | ||
| slope_table.all_crit .= 1 | ||
|
|
||
| for threshold in ruleset | ||
| slope_table.all_crit = | ||
| slope_table.all_crit .& threshold.rule(slope_table[!, threshold.name]) | ||
| end | ||
|
|
||
| return slope_table[BitVector(slope_table.all_crit), :] | ||
| end | ||
|
|
||
| """ | ||
| lookup_df_from_raster(raster::Raster, threshold::Union{Int64,Float64})::DataFrame | ||
|
|
||
| Build a look up table identifying all pixels in a raster that meet a suitability threshold. | ||
|
|
||
| # Arguments | ||
| - `raster` : Raster of regional data | ||
| - `threshold` : Suitability threshold value (greater or equal than) | ||
|
|
||
| # Returns | ||
| DataFrame containing indices, lon and lat for each pixel that is intended for further | ||
| analysis. | ||
| """ | ||
| function lookup_df_from_raster(raster::Raster, threshold::Union{Int64,Float64})::DataFrame | ||
| criteria_matches::SparseMatrixCSC{Bool,Int64} = sparse(falses(size(raster))) | ||
| Rasters.read!(raster .>= threshold, criteria_matches) | ||
| indices::Vector{CartesianIndex{2}} = findall(criteria_matches) | ||
| indices_lon::Vector{Float64} = lookup(raster, X)[first.(Tuple.(indices))] | ||
| indices_lat::Vector{Float64} = lookup(raster, Y)[last.(Tuple.(indices))] | ||
|
|
||
| return DataFrame(; indices=indices, lons=indices_lon, lats=indices_lat) | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| using | ||
| Statistics, | ||
| Proj, | ||
| LibGEOS, | ||
| GeometryBasics, | ||
| CoordinateTransformations, | ||
| Rasters, | ||
| StaticArrays, | ||
| NearestNeighbors | ||
|
|
||
| import ArchGDAL as AG | ||
| import GeoInterface as GI | ||
| import GeoInterface.Wrappers as GIWrap | ||
| import GeometryOps as GO | ||
| import GeoDataFrames as GDF | ||
|
|
||
| include("apply_criteria.jl") | ||
| include("best_fit_polygons.jl") | ||
| include("common_functions.jl") | ||
| include("geom_ops.jl") | ||
| include("site_identification.jl") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.