-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract.jl
More file actions
156 lines (136 loc) · 5.24 KB
/
Copy pathextract.jl
File metadata and controls
156 lines (136 loc) · 5.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
"""
extract_at_mesh(raster::Raster, mesh::INLAMesh;
method::Symbol = :bilinear,
outside::Symbol = :error,
missingval = NaN,
mesh_crs = nothing) -> Vector{Float64}
Sample `raster` at each vertex of `mesh` and return a length-`num_vertices`
vector of values.
# Arguments
- `raster::Raster` — a 2D raster with `X` and `Y` dimensions. The raster
must be defined on regular, monotonically-ordered coordinates along
both axes (ascending or descending — both are supported).
- `mesh::INLAMesh` — the SPDE mesh. Mesh vertex coordinates are assumed
to be in the same CRS as `raster`. `INLAMesh` does not currently
carry CRS metadata; pass `mesh_crs` to assert against
`Rasters.crs(raster)` at the API boundary.
# Keywords
- `method = :bilinear` — one of `:bilinear` or `:nearest`. Bilinear
interpolation reproduces affine raster fields exactly at mesh
vertices. Nearest-neighbour is useful for categorical covariates.
- `outside = :error` — policy for vertices outside the raster extent:
`:error` throws, `:missing` substitutes `missingval`.
- `missingval = NaN` — the sentinel inserted for outside-domain
vertices when `outside = :missing`.
- `mesh_crs = nothing` — optional CRS assertion (ADR-041). When
supplied, must equal `Rasters.crs(raster)` or an `ArgumentError`
is raised. Default `nothing` preserves the v0.2.x "trust the
caller" behaviour. Reprojection is out of scope; if the CRSs
differ, pre-project one side before calling.
# Returns
`Vector{Float64}` of length `num_vertices(mesh)` with one extracted
value per vertex, ordered by vertex index.
# Notes
- For bilinear sampling, a vertex at the raster edge still has a
well-defined value (the bracketing cell collapses to the edge).
- If the raster itself contains missing values and a bracketing cell
has any missing corner, the returned value is `NaN` for that vertex
under `:bilinear`, and `missingval` under `:nearest` if the nearest
cell itself is missing.
"""
function extract_at_mesh(
raster::Raster,
mesh::INLAMesh;
method::Symbol=:bilinear,
outside::Symbol=:error,
missingval::Real=NaN,
mesh_crs=nothing
)
method ∈ (:bilinear, :nearest) ||
throw(ArgumentError("method must be :bilinear or :nearest; got $method"))
outside ∈ (:error, :missing) ||
throw(ArgumentError("outside must be :error or :missing; got $outside"))
_check_crs(Rasters.crs(raster), mesh_crs)
xs = collect(Rasters.lookup(raster, X))
ys = collect(Rasters.lookup(raster, Y))
length(xs) >= 2 ||
throw(ArgumentError("raster X dimension must have ≥ 2 points; got $(length(xs))"))
length(ys) >= 2 ||
throw(ArgumentError("raster Y dimension must have ≥ 2 points; got $(length(ys))"))
n = num_vertices(mesh)
out = Vector{Float64}(undef, n)
for k in 1:n
x = mesh.points[k, 1]
y = mesh.points[k, 2]
bx = _bracket(xs, x)
by = _bracket(ys, y)
if bx === nothing || by === nothing
if outside === :error
throw(ArgumentError(
"mesh vertex $k at ($x, $y) is outside the raster extent; " *
"pass `outside = :missing` to substitute a sentinel instead",
))
else
out[k] = missingval
continue
end
end
i, tx = bx
j, ty = by
out[k] = if method === :bilinear
_bilinear_sample(raster, i, j, tx, ty)
else
_nearest_sample(raster, i, j, tx, ty)
end
end
return out
end
# Locate the bracketing cell index `i` such that xs[i] ≤ x ≤ xs[i+1]
# (for ascending xs) and return `(i, t)` with t = (x - xs[i]) / (xs[i+1]
# - xs[i]) ∈ [0, 1]. For descending xs the same invariant holds with
# the sign flipped. Returns `nothing` when x is strictly outside the
# range of xs.
function _bracket(xs::AbstractVector{<:Real}, x::Real)
n = length(xs)
if xs[1] <= xs[end]
(x < xs[1] || x > xs[end]) && return nothing
# Ascending.
for i in 1:(n - 1)
a = xs[i]
b = xs[i + 1]
if a <= x <= b
t = a == b ? 0.0 : (x - a) / (b - a)
return (i, Float64(t))
end
end
else
(x > xs[1] || x < xs[end]) && return nothing
# Descending.
for i in 1:(n - 1)
a = xs[i]
b = xs[i + 1]
if b <= x <= a
t = a == b ? 0.0 : (a - x) / (a - b)
return (i, Float64(t))
end
end
end
return nothing
end
# Bilinear interpolation inside the cell (i, j) ↔ (i+1, j+1).
# Keyword-dim indexing makes this independent of raster dim order.
function _bilinear_sample(raster, i, j, tx, ty)
v00 = Float64(raster[X=i, Y=j])
v10 = Float64(raster[X=i + 1, Y=j])
v01 = Float64(raster[X=i, Y=j + 1])
v11 = Float64(raster[X=i + 1, Y=j + 1])
return (1 - tx) * (1 - ty) * v00 +
tx * (1 - ty) * v10 +
(1 - tx) * ty * v01 +
tx * ty * v11
end
function _nearest_sample(raster, i, j, tx, ty)
ii = tx < 0.5 ? i : i + 1
jj = ty < 0.5 ? j : j + 1
return Float64(raster[X=ii, Y=jj])
end