/
GridBoundsSpec.scala
188 lines (154 loc) · 5.53 KB
/
GridBoundsSpec.scala
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
/*
* Copyright 2016 Azavea
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package geotrellis.raster
import org.scalatest._
import spire.syntax.cfor._
class GridBoundsSpec extends FunSpec with Matchers{
describe("GridBounds.minus") {
it("subtracts an overlapping GridBounds that overflows bottom left") {
val minuend = GridBounds(0, 0, 100, 100)
val subtrahend = GridBounds(50, 50, 150, 150)
val result = (minuend - subtrahend).sortBy(_.colMax).toArray
result.size should be (2)
// Account for the 2 possible cuts
if(result(0).rowMin == 0) {
result(0) should be (GridBounds(0, 0, 49, 100))
result(1) should be (GridBounds(50, 0, 100, 49))
} else {
result(0) should be (GridBounds(0, 50, 49, 100))
result(1) should be (GridBounds(0, 0, 100, 49))
}
}
it("subtracts an overlapping GridBounds that overflows top right") {
val minuend = GridBounds(0, 0, 100, 100)
val subtrahend = GridBounds(-50, -50, 50, 50)
val result = (minuend - subtrahend).sortBy(_.colMin).toArray
println(result.toSeq)
result.size should be (2)
// Account for the 2 possible cuts
if(result(0).colMax == 50) {
result(0) should be (GridBounds(0, 51, 50, 100))
result(1) should be (GridBounds(51, 0, 100, 100))
} else {
result(0) should be (GridBounds(0, 51, 100, 100))
result(1) should be (GridBounds(51, 0, 100, 100))
}
}
it("subtracts a partial horizontal line through the middle") {
val minuend = GridBounds(0, 0, 100, 100)
val subtrahend = GridBounds(-50, 50, 50, 50)
val result = (minuend - subtrahend).sortBy(_.colMin).toSet
result should be (Set(
GridBounds(51, 0, 100, 100), // Right
GridBounds(0, 0, 50, 49), // Top
GridBounds(0, 51, 50, 100) // Bottom
))
}
it("subtracts a contained bounds") {
val minuend = GridBounds(0, 0, 100, 100)
val subtrahend = GridBounds(25, 35, 75, 85)
val result = (minuend - subtrahend).sortBy(_.colMin).toSet
result should be (Set(
GridBounds(0, 0, 24, 100), // Left
GridBounds(76, 0, 100, 100), // Right
GridBounds(25, 0, 75, 34), // Top
GridBounds(25, 86, 75, 100) // Bottom
))
}
it("subtracts full bounds") {
val minuend = GridBounds(9,10,16,13)
val subtrahend = GridBounds(8,9,17,14)
(minuend - subtrahend) should be (Seq())
}
}
describe("GridBounds.distinct") {
it("creates a distinct set of GridBounds from an overlapping set") {
val gridBounds =
Seq(
GridBounds(0, 0, 75, 75),
GridBounds(25, 25, 100, 100)
)
GridBounds.distinct(gridBounds).map(_.sizeLong).sum should be ((101 * 101) - (25 * 25 * 2))
}
}
describe("GridBounds.buffer") {
it("should not produce a GridBounds with negative values") {
val gps = GridBounds(0, 0, 10, 10)
gps.buffer(5) shouldBe GridBounds(0, 0, 15, 15)
}
it("should produce a GridBounds with negative values when clamp is false") {
val gps = GridBounds(256, 0, 500, 256)
gps.buffer(128, 128, clamp = false) shouldBe GridBounds(128, -128, 628, 384)
}
it("should only buffer the cols") {
val gps = GridBounds(5, 5, 20, 20)
gps.buffer(5, 0) shouldBe GridBounds(0, 5, 25, 20)
}
it("should only buffer the rows") {
val gps = GridBounds(0, 15, 20, 35)
gps.buffer(0, 10) shouldBe GridBounds(0, 5, 20, 45)
}
it("should buffer both cols and rows") {
val gps = GridBounds(100, 100, 250, 250)
gps.buffer(25) shouldBe GridBounds(75, 75, 275, 275)
}
}
describe("GridBounds.offset") {
it("should move right 3 and down 5") {
val gps = GridBounds(250, 250, 500, 500)
val actual = gps.offset(3, 5)
val expected = GridBounds(253, 255, 503, 505)
actual shouldBe expected
actual.sizeLong shouldBe expected.sizeLong
}
it("should move to the left 10 and up 15") {
val gps = GridBounds(12, 22, 32, 42)
val actual = gps.offset(-10, -15)
val expected = GridBounds(2, 7, 22, 27)
actual shouldBe expected
actual.sizeLong shouldBe expected.sizeLong
}
}
describe("GridBounds.split") {
it("should not split too small a GridBounds") {
val gbs = GridBounds(0,0,7,5)
val result = gbs.split(10, 15).toList
result.head should be (gbs)
}
it("should split even GridBounds") {
val gbs = GridBounds(0,0,9,9)
val result = gbs.split(5,5).toList
result.length should be (4)
result should contain allOf (
GridBounds(0,0,4,4),
GridBounds(5,0,9,4),
GridBounds(0,5,4,9),
GridBounds(5,5,9,9)
)
}
it("should split un-even GridBounds") {
val gbs = GridBounds(0,0,10,10)
val result = gbs.split(10,10).toList
result.length should be (4)
result should contain allOf (
GridBounds(0,0,9,9),
GridBounds(10,0,10,9),
GridBounds(0,10,9,10),
GridBounds(10,10,10,10)
)
}
}
}