Skip to content

Commit ddf103f

Browse files
author
Vincent Landau
committed
add tests, fixes to DiGraph construction
1 parent 8f61792 commit ddf103f

File tree

4 files changed

+83
-8
lines changed

4 files changed

+83
-8
lines changed

src/rastergraphs.jl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ end
2929
weightedrastergraph(
3030
weight_raster::GeoArray;
3131
directed::Bool = false,
32+
condition_raster::GeoArray = weight_raster,
3233
condition::Function = is_data,
3334
cardinal_neighbors_only::Bool = false,
3435
connect_using_avg_raster_val::Bool = true
@@ -307,7 +308,7 @@ function make_raster_graph(
307308
north_idx = CartesianIndex((row - 1, column))
308309

309310
# "West"
310-
if column != dims[2] && vertex_raster[west_idx] != 0 &&
311+
if column != 1 && vertex_raster[west_idx] != 0 &&
311312
weight_raster[west_idx] != no_data_val
312313
if condition(condition_raster[source_idx],
313314
condition_raster[west_idx])
@@ -320,7 +321,7 @@ function make_raster_graph(
320321
end
321322

322323
# "North"
323-
if column != dims[2] && vertex_raster[north_idx] != 0 &&
324+
if row != 1 && vertex_raster[north_idx] != 0 &&
324325
weight_raster[north_idx] != no_data_val
325326
if condition(condition_raster[source_idx],
326327
condition_raster[north_idx])
@@ -339,7 +340,7 @@ function make_raster_graph(
339340
sw_idx = CartesianIndex((row + 1, column - 1))
340341

341342
# "Northwest"
342-
if column != dims[2] && row != 1 &&
343+
if column != 1 && row != 1 &&
343344
vertex_raster[nw_idx] != 0 &&
344345
weight_raster[nw_idx] != no_data_val
345346
if condition(condition_raster[source_idx],
@@ -353,7 +354,7 @@ function make_raster_graph(
353354
end
354355

355356
# "Southwest"
356-
if row != dims[1] && column != dims[2] &&
357+
if row != dims[1] && column != 1 &&
357358
vertex_raster[sw_idx] != 0 &&
358359
weight_raster[sw_idx] != no_data_val
359360
if condition(condition_raster[source_idx],

test/rasterdigraphs.jl

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using ArchGDAL, GeoData, LightGraphs, SimpleWeightedGraphs, SpatialGraphs
2+
A_array = Array{Float64}(undef, (3, 4, 1))
3+
A_array[:,:,:] = [1, 3, 2, 0.5, 10, 8, 5, -9999, 3, 1, 2, 6]
4+
5+
condition_array = Array{Float64}(undef, (3, 4, 1))
6+
condition_array[:,:,:] = [1, 3, 5, 2, 4, 8, 5, -9999, 2, 3, 6, 7]
7+
8+
x = X(1:4)
9+
y = Y(1:3)
10+
band = Band(1:1)
11+
12+
weight_raster = GeoArray(A_array, (y, x, band), missingval = -9999)
13+
condition_raster = GeoArray(condition_array, (y, x, band), missingval = -9999)
14+
15+
compare = <
16+
rasgraph = weightedrastergraph(
17+
weight_raster,
18+
directed = true,
19+
condition_raster = condition_raster,
20+
condition = compare
21+
)
22+
23+
# no vertices in NoData pixels?
24+
@test (rasgraph.vertex_raster .== 0) ==
25+
((weight_raster .== weight_raster.missingval) .|
26+
isnan.(weight_raster))
27+
28+
# Is the number of of vertices correct, and is the range of values correct?
29+
@test sort(collect(rasgraph.vertex_raster[rasgraph.vertex_raster .!= 0])) ==
30+
collect(1:sum(
31+
(weight_raster .!= weight_raster.missingval) .&
32+
(!).(isnan.(weight_raster))
33+
))
34+
35+
graph_edges = collect(edges(rasgraph))
36+
37+
# Test that the edges are correct and have proper weights
38+
for i in 1:length(graph_edges)
39+
source_i = src(graph_edges[i])
40+
dest_i = dst(graph_edges[i])
41+
weight_i = graph_edges[i].weight
42+
43+
source_coords = findall(rasgraph.vertex_raster .== source_i)[1]
44+
dest_coords = findall(rasgraph.vertex_raster .== dest_i)[1]
45+
46+
# Check that condition is met
47+
@test compare(
48+
condition_raster[source_coords],
49+
condition_raster[dest_coords]
50+
)
51+
52+
# Test that source row is within 1 step of dest row
53+
row_diff = abs(source_coords[1] - dest_coords[1])
54+
@test row_diff <= 1
55+
56+
# Test that source column is within 1 step of dest row
57+
col_diff = abs(source_coords[2] - dest_coords[2])
58+
@test col_diff <= 1
59+
60+
# Test that the weight is what it should be (assumes connect_using_avg_weights = true in graph construction)
61+
if (row_diff == 1 && col_diff == 1) # get diagonal average
62+
@test weight_i ==
63+
SpatialGraphs.res_diagonal_avg(weight_raster[source_coords],
64+
weight_raster[dest_coords])
65+
else
66+
@test weight_i ==
67+
SpatialGraphs.res_cardinal_avg(weight_raster[source_coords],
68+
weight_raster[dest_coords])
69+
end
70+
end

test/rastergraphs.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ rasgraph2d = weightedrastergraph(weight_raster2d)
1212
rasgraph = weightedrastergraph(weight_raster)
1313

1414
# Test that graphs are the same regardless of whether weight_raster has Band dim
15-
@test rasgraph2d.graph == rasgraph3d.graph
15+
@test rasgraph2d.graph == rasgraph.graph
1616

1717
# no vertices in NoData pixels?
1818
@test (rasgraph.vertex_raster .== 0) ==
19-
((weight_raster3d .== weight_raster3d.missingval) .|
20-
isnan.(weight_raster3d))
19+
((weight_raster .== weight_raster.missingval) .|
20+
isnan.(weight_raster))
2121

2222
# Is the number of of vertices correct, and is the range of values correct?
2323
@test sort(collect(rasgraph.vertex_raster[rasgraph.vertex_raster .!= 0])) ==

test/runtests.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
using Test, SpatialGraphs
22

3-
@testset "placeholder" begin
3+
@testset "Raster Graph Construction" begin
4+
include("rastergraphs.jl")
5+
end
46

7+
@testset "Raster DiGraph Construction" begin
8+
include("rasterdigraphs.jl")
59
end

0 commit comments

Comments
 (0)