-
Notifications
You must be signed in to change notification settings - Fork 33
/
coverage.jl
384 lines (350 loc) · 15.2 KB
/
coverage.jl
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
const COVERAGE_DOC = """
Calculate the area of a raster covered by GeoInterface.jl compatible geomtry `geom`,
as a fraction.
Each pixel is assigned a grid of points (by default 10 x 10) that are each checked
to be inside the geometry. The sum divided by the number of points to give coverage.
In pracice, most pixel coverage is not calculated this way - shortcuts that
produce the same result are taken wherever possible.
If `geom` is an `AbstractVector` or table, the `mode` keyword will determine how coverage is combined.
"""
const COVERAGE_KEYWORDS = """
- `mode`: method for combining multiple geometries - `union` or `sum`.
* `union` (the default) gives the areas covered by all geometries. Usefull in
spatial coverage where overlapping regions should not be counted twice.
The returned raster will contain `Float64` values between `0.0` and `1.0`.
* `sum` gives the summed total of the areas covered by all geometries,
as in taking the sum of running `coverage` separately on all geometries.
The returned values are positive `Float64`.
For a single geometry, the `mode` keyword has no effect - the result is the same.
- `scale`: `Integer` scale of pixel subdivision. The default of `10` means each pixel has
10 x 10 or 100 points that contribute to coverage. Using `100` means 10,000 points
contribute. Performance will decline as `scale` increases. Memory use will grow
by `scale^2` when `mode=:union`.
$THREADED_KEYWORD
$PROGRESS_KEYWORD
$VERBOSE_KEYWORD
"""
"""
coverage(mode, geom; [to, res, size, scale, verbose, progress])
coverage(geom; [to, mode, res, size, scale, verbose, progress])
$COVERAGE_DOC
# Keywords
$COVERAGE_KEYWORDS
$TO_KEYWORD
$SIZE_KEYWORD
$RES_KEYWORD
"""
coverage(data; to=nothing, mode=union, scale=10, kw...) = _coverage(to, data; mode, scale, kw...)
coverage(f::Union{typeof(sum),typeof(union)}, data; kw...) = coverage(data; kw..., mode=f)
function _coverage(to, data; mode, scale, kw...)
name = if GI.isgeometry(data) || GI.isfeature(data)
Symbol(:coverage)
else
Symbol(:coverage_, mode)
end
# We use sum for `reducer` so eltype inference works
r = Rasterizer(data; reducer=sum, fill=0.0, init=0.0, missingval=0.0, kw...)
rc = RasterCreator(to, data; kw..., eltype=r.eltype, fill, name, missingval=r.missingval)
dest = create_rasterize_dest(rc) do A
_coverage!(A, r; mode, scale)
end
return dest
end
"""
coverage!(A, geom; [mode, scale])
$COVERAGE_DOC
# Keywords
$COVERAGE_KEYWORDS
"""
coverage!(mode::Union{typeof(union),typeof(sum)}, A::AbstractRaster, data; kw...) =
_coverage!(A, GI.trait(data), data; kw..., mode)
function coverage!(A::AbstractRaster, data; scale::Integer=10, mode=union, kw...)
# We use sum for `reducer` so eltype inference works
r = Rasterizer(data; reducer=sum, fill=0.0, init=0.0, missingval=0.0, kw...)
coverage!(A, r; scale, mode)
end
# Collect iterators so threading is easier.
function _coverage!(A::AbstractRaster, r::Rasterizer; scale::Integer=10, mode=union)
_coverage!(A, GI.trait(r.geom), r.geom, r; scale, mode)
end
function _coverage!(A::AbstractRaster, ::GI.AbstractGeometryTrait, geom, r; scale, mode)
subpixel_dims = _subpixel_dims(A, scale)
missed_pixels = if mode === union
_union_coverage!(A, geom; scale, subpixel_dims, progress=r.progress)
elseif mode === sum
_sum_coverage!(A, geom; scale, subpixel_dims, progress=r.progress)
else
throw(ArgumentError("Coverage `mode` can be `union` or `sum`. Got $mode"))
end
r.verbose && _check_missed_pixels(missed_pixels, scale)
return A
end
function _coverage!(A::AbstractRaster, ::Nothing, geoms, r::Rasterizer; mode, scale)
n = r.threaded ? _nthreads() : 1
buffers = (
# We always need a vector of threads, but just
allocs = _burning_allocs(A; nthreads=n, threaded=true),
linebuffer = [_init_bools(A, BitArray; missingval=false) for _ in 1:n],
centerbuffer = [_init_bools(A, BitArray; missingval=false) for _ in 1:n],
block_crossings = [[Vector{Float64}(undef, 0) for _ in 1:scale] for _ in 1:n],
burnstatus=[fill(BurnStatus(), scale) for _ in 1:n],
subbuffer = [fill!(Array{Bool}(undef, scale, scale), false) for _ in 1:n],
ncrossings = [fill(0, scale) for _ in 1:n],
)
subpixel_dims = _subpixel_dims(A, scale)
missed_pixels = if mode === union
_union_coverage!(A, geoms, buffers; scale, subpixel_dims, progress=r.progress, threaded=r.threaded)
elseif mode === sum
_sum_coverage!(A, geoms, buffers; scale, subpixel_dims, progress=r.progress, threaded=r.threaded)
else
throw(ArgumentError("Coverage `mode` can be `union` or `sum`. Got $mode"))
end
r.verbose && _check_missed_pixels(missed_pixels, scale)
return A
end
# Combines coverage at the sub-pixel level for a final value 0-1
function _union_coverage!(A::AbstractRaster, geoms, buffers;
scale, subpixel_dims, progress=true, threaded=false
)
n = _nthreads()
centeracc = [_init_bools(A, BitArray; missingval=false) for _ in 1:n]
lineacc = [_init_bools(A, BitArray; missingval=false) for _ in 1:n]
subpixel_buffer = [falses(size(A) .* scale) for _ in 1:n]
allbuffers = merge(buffers, (; centeracc, lineacc, subpixel_buffer))
range = _geomindices(geoms)
_run(range, threaded, progress, "Calculating coverage buffers...") do i
geom = _getgeom(geoms, i)
idx = Threads.threadid()
# Get buffers for each thread as a NamedTuple
thread_buffers = map(b -> b[idx], allbuffers)
_union_coverage!(A, geom; scale, subpixel_buffer, thread_buffers...)
fill!(thread_buffers.linebuffer, false)
fill!(thread_buffers.centerbuffer, false) # Is this necessary?
end
# Merge downscaled BitArray (with a function barrier)
subpixel_union = _do_broadcast!(|, subpixel_buffer[1], subpixel_buffer...)
subpixel_raster = Raster(subpixel_union, subpixel_dims)
# Merge main BitArray (with a function barrier)
center_covered = _do_broadcast!(|, centeracc[1], centeracc...)
line_covered = _do_broadcast!(|, lineacc[1], lineacc...)
missed_pixels = fill(0, n)
range = axes(A, Y())
_run(range, threaded, progress, "Combining coverage...") do y
for x in axes(A, X())
D = (X(x), Y(y))
if center_covered[D...]
pixel_coverage = 1.0
A[D...] = pixel_coverage
else
subxs, subys = map((x, y)) do i
i1 = (i - 1) * scale
sub_indices = i1 + 1:i1 + scale
end
pixel_coverage = sum(view(subpixel_raster, X(subxs), Y(subys))) / scale^2
if pixel_coverage == 0.0
# Check if this line should be covered
if line_covered[D...]
idx = Threads.threadid()
missed_pixels[idx] += 1
end
end
A[D...] = pixel_coverage
end
end
end
return sum(missed_pixels)
end
function _union_coverage!(A::AbstractRaster, geom;
scale,
linebuffer=_init_bools(A, BitArray; missingval=false),
centerbuffer=_init_bools(A, BitArray; missingval=false),
allocs=Allocs(linebuffer),
subpixel_buffer=falses(size(A) .* scale),
centeracc=_init_bools(A, BitArray; missingval=false),
lineacc=_init_bools(A, BitArray; missingval=false),
burnstatus=[BurnStatus() for _ in 1:scale],
block_crossings=[Vector{Float64}(undef, 0) for _ in 1:scale],
subbuffer=falses(scale, scale),
subpixel_dims=_subpixel_dims(A, scale),
ncrossings=fill(0, scale),
)
GI.isgeometry(geom) || error("not a geometry")
crossings = allocs.crossings
boolmask!(linebuffer, geom; shape=:line, allocs)
boolmask!(centerbuffer, geom; boundary=:center, allocs)
# Update the cumulative state for completely covered cells
# This allows us to skip them later.
# We don't just use `boundary=:inside` because we need the line burns separately anyway
centeracc .|= (centerbuffer .& .!(linebuffer))
lineacc .|= linebuffer
edges = Edges(geom, subpixel_dims; allocs)
prev_ypos = 0
# Loop over y in A
for y in axes(A, Y())
# If no lines touched this column skip it
found = false
for x in axes(A, X())
if linebuffer[X(x), Y(y)] && !centeracc[X(x), Y(y)]
found = true
break
end
end
found || continue
y1 = (y - 1) * scale
sub_yaxis = y1 + 1:y1 + scale
# Generate all of the x crossings beforehand so we don't do it for every pixel
for (i, sub_y) in enumerate(sub_yaxis)
ncrossings[i], prev_ypos = _set_crossings!(block_crossings[i], edges, sub_y, prev_ypos)
end
# Reset burn burnstatus
for i in eachindex(burnstatus)
burnstatus[i] = BurnStatus()
end
subpixel_raster = Raster(subpixel_buffer, subpixel_dims)
# Loop over x in A
for x in axes(A, X())
# Checkt that the cell is not already 100% filled
centeracc[X(x), Y(y)] && continue
# CHeck that a line touched the cell
linebuffer[X(x), Y(y)] || continue
x1 = (x - 1) * scale
sub_xaxis = x1 + 1:x1 + scale
offset_subbuffer = OffsetArrays.OffsetArray(subbuffer, (sub_xaxis, sub_yaxis))
subdims = map(subpixel_dims, axes(offset_subbuffer)) do d, a
rebuild(d, NoLookup(a)) # Don't need a lookup for _burn_polygon
end
# Rebuild the buffer for this pixels dims
subraster = Raster(offset_subbuffer, subdims, (), NoName(), NoMetadata(), false)
# And initialise it
fill!(subraster, false)
# Loop over y in the subraster
for (i, sub_y) in enumerate(sub_yaxis)
ncrossings[i] > 0 || continue
# Burn along x for each y, tracking burn status and crossing number
burnstatus[i] = _burn_crossings!(subraster, block_crossings[i], ncrossings[i], sub_y; status=burnstatus[i])
end
v = view(subpixel_raster, X(sub_xaxis), Y(sub_yaxis))
parent(v) .|= parent(parent(subraster))
end
end
end
# Sums all coverage
function _sum_coverage!(A::AbstractRaster, geoms, buffers;
scale, subpixel_dims, verbose=true, progress=true, threaded=false
)
n = _nthreads()
coveragebuffers = [fill!(similar(A), 0.0) for _ in 1:n]
missed_pixels = fill(0, n)
range = _geomindices(geoms)
burnchecks = _alloc_burnchecks(range)
_run(range, threaded, progress, "Calculating coverage...") do i
geom = _getgeom(geoms, i)
ismissing(geom) && return nothing
idx = Threads.threadid()
thread_buffers = map(b -> b[idx], buffers)
coveragebuffer = coveragebuffers[idx]
nmissed, burnchecks[i] = _sum_coverage!(coveragebuffer, geom; scale, thread_buffers...)
missed_pixels[idx] += nmissed
fill!(thread_buffers.linebuffer, false)
fill!(thread_buffers.centerbuffer, false)
return nothing
end
_do_broadcast!(+, A, coveragebuffers...)
_set_burnchecks(burnchecks, metadata(A), verbose)
return sum(missed_pixels)
end
function _sum_coverage!(A::AbstractRaster, geom;
scale,
linebuffer=_init_bools(A, BitArray; missingval=false),
centerbuffer=_init_bools(A, BitArray; missingval=false),
allocs=Allocs(linebuffer),
block_crossings=[Vector{Float64}(undef, 0) for _ in 1:scale],
subbuffer=falses(scale, scale),
burnstatus=[BurnStatus() for _ in 1:scale],
subpixel_dims=_subpixel_dims(A, scale),
ncrossings=fill(0, scale),
)
GI.isgeometry(geom) || error("Object is not a geometry")
crossings = allocs.crossings
boolmask!(linebuffer, geom; shape=:line, allocs)
boolmask!(centerbuffer, geom; boundary=:center, allocs)
edges = Edges(geom, subpixel_dims; allocs)
# Brodcast over the rasterizations and indices
# to calculate coverage of each pixel
local missed_pixels = 0
local hascover = false
prev_ypos = 0
# Loop over y in A
for y in axes(A, Y())
found = false
for x in axes(A, X())
if linebuffer[X(x), Y(y)]
found = true
elseif centerbuffer[X(x), Y(y)]
# If the center is inside a polygon but the pixel is
# not on a line, then coverage is 1.0
pixel_coverage = 1.0
hascover = true
A[X(x), Y(y)] += pixel_coverage
end
end
# If no lines touched this column, skip it
found || continue
y1 = (y - 1) * scale
sub_yaxis = y1 + 1:y1 + scale
# Generate all of the x crossings beforehand so we don't do it for every pixel
for (i, sub_y) in enumerate(sub_yaxis)
ncrossings[i], prev_ypos = _set_crossings!(block_crossings[i], edges, sub_y, prev_ypos)
end
# Set the burn/skip status to false (skip) for each starting position
burnstatus .= Ref(BurnStatus())
missed_pixels = 0
# Loop over x in A
for x in axes(A, X())
linebuffer[X(x), Y(y)] || continue
hasburned =
x1 = (x - 1) * scale
sub_xaxis = x1 + 1:x1 + scale
offset_subbuffer = OffsetArrays.OffsetArray(subbuffer, (sub_xaxis, sub_yaxis))
subdims = map(subpixel_dims, axes(offset_subbuffer)) do d, a
# Don't need a real lookup for _burn_polygon
rebuild(d, NoLookup(a))
end
# Rebuild the buffer for this pixels dims
subraster = Raster(offset_subbuffer, subdims, (), NoName(), NoMetadata(), false)
# And initialise it
fill!(subraster, false)
# Loop over y in the subraster
for (i, sub_y) in enumerate(sub_yaxis)
ncrossings[i] > 0 || continue
# Burn along x for each y, tracking burn status and crossing number
burnstatus[i] = _burn_crossings!(subraster, block_crossings[i], ncrossings[i], sub_y; status=burnstatus[i])
end
# Finally, sum the pixel sub-raster and divide by scale^2 for the fractional coverage
pixel_coverage = sum(subraster) / scale^2
if pixel_coverage == 0
missed_pixels += 1
else
hascover = true
end
A[X(x), Y(y)] += pixel_coverage
end
end
return missed_pixels, hascover
end
function _subpixel_dims(A, scale)
shifted = map(d -> DD.maybeshiftlocus(Start(), d), commondims(A, DEFAULT_TABLE_DIM_KEYS))
map(shifted) do d
l = lookup(d)
substep = step(l) / scale
substart = substep / 2 + first(l)
range = substart:substep:last(l) + scale * substep
sublookup = Sampled(range, ForwardOrdered(), Regular(substep), Intervals(Start()), NoMetadata())
rebuild(d, sublookup)
end
end
function _check_missed_pixels(missed_pixels::Int, scale::Int)
if missed_pixels > 0
@info "There were $missed_pixels times that pixels were touched by geometries but did not produce coverage, meaning these areas did not contain any of the $(scale * scale) points in the pixel at `scale=$scale`."
end
end