forked from locationtech/geotrellis
/
ColorMapSpec.scala
346 lines (295 loc) · 10.6 KB
/
ColorMapSpec.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
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
/*
* 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.render
import geotrellis.raster._
import geotrellis.raster.render._
import geotrellis.raster.render.png._
import geotrellis.raster.testkit._
import org.scalatest._
import java.io._
import java.awt.Color
import javax.imageio._
class ColorMapSpec extends FunSpec with Matchers
with TileBuilders {
describe("color map construction") {
it("should classify ints to colors") {
val colorMap =
ColorMap(
123 -> 123,
1234 -> 1234,
1235 -> 1235,
1236 -> 1236
).withNoDataColor(8675309)
colorMap.colors shouldBe (Array(123, 1234, 1235, 1236))
colorMap.options.noDataColor shouldBe (8675309)
}
it("should classify doubles to colors") {
val colorMap =
ColorMap(
123.23 -> 123,
12234.89 -> 1234,
45.342 -> 1235,
1236.13 -> 1236
).withNoDataColor(8675309)
colorMap.colors shouldBe (Array(1235, 123, 1236, 1234))
colorMap.options.noDataColor shouldBe (8675309)
}
}
describe("color map creation") {
it("should build a color map with fully specifiable options") {
val ndColor = RGBA(0, 0, 0, 100.0)
val fallbackColor = RGBA(255, 0, 0, 0)
val colorMap =
ColorMap((0, 1))
.withNoDataColor(ndColor)
.withFallbackColor(fallbackColor)
.withBoundaryType(Exact)
colorMap.options shouldBe (ColorMap.Options(Exact, ndColor.int, fallbackColor.int, false))
}
}
describe("ColorMaps to-and-from Strings") {
val s = "23:cc00ccff;30:aa00aaff;120:ff0000ff"
val sD = "23.0:cc00ccff;30.0:aa00aaff;120.0:ff0000ff"
it("fromString should parse correctly") {
ColorMap.fromString(s) shouldBe defined
}
it("(int) fromString and breaksString should form an isomorphism") {
ColorMap.fromString(s).get.breaksString shouldBe s
}
it("(double) fromString and breaksString should form an isomorphism") {
ColorMap.fromStringDouble(sD).get.breaksString shouldBe sD
}
}
describe("PNG Color Mapping") {
it("should correctly map values to colors") {
val limits = Array(25,50,80,100)
val colors = Array(100,110,120,130)
val colorMap1 =
ColorMap(limits, colors)
val colorMap = colorMap1.withNoDataColor(0).withBoundaryType(LessThanOrEqualTo)
val arr = (0 until 90 by 5).toArray
val r = createTile(arr)
val color: IndexedPngEncoding =
PngColorEncoding(colorMap.colors, colorMap.options.noDataColor, colorMap.options.fallbackColor) match {
case i @ IndexedPngEncoding(_, _) => i
case _ =>
withClue(s"Color should be Indexed") { sys.error("") }
}
for(x <- arr) {
if(x <= 25) colorMap.map(x) should be (100)
else if(x <= 50) colorMap.map(x) should be (110)
else if (x <= 80) colorMap.map(x) should be (120)
else { colorMap.map(x) should be (130) }
}
}
it("should correctly map values to colors using IntCacheColorMap") {
val limits = Array(25,50,80,100)
val colors = Array(100,110,120,130)
val colorRamp: ColorRamp = colors
val breaksToColors: Map[Int, Int] = (limits zip colorRamp.stops(limits.size).colors).toMap
val colorMap1 = new IntColorMap(breaksToColors, ColorMap.Options(noDataColor = 0, classBoundaryType = LessThanOrEqualTo))
val arr = (0 until 90 by 5).toArray
val r = createTile(arr)
val h = r.histogram
val colorMap = colorMap1.cache(h)
val color: IndexedPngEncoding =
PngColorEncoding(colorMap.colors, colorMap.options.noDataColor, colorMap.options.fallbackColor) match {
case i @ IndexedPngEncoding(_, _) => i
case _ =>
withClue(s"Color should be Indexed") { sys.error("") }
}
// check that PNG will correctly convert raster to their color index values
val pngMap = color.convertColorMap(colorMap)
h.foreachValue { z =>
val color = colorMap.map(z)
val colorIndex = colorMap.colors.indexOf(color)
pngMap.map(z) should be(colorIndex)
}
// check that we can convert all raster values we have seen in our histogram
withClue(s"Cached colors: ${colorMap.colors.toList}") {
for (x <- arr) {
if (x <= 25) colorMap.map(x) should be(100)
else if (x <= 50) colorMap.map(x) should be(110)
// tile does not have any values past 75, since we didn't see them, we don't expect to color them
else if (x > 75) colorMap.options.noDataColor
}
}
}
it("should correctly map redundant values to colors") {
val limits = Array(25,42,60)
val colors = Array(10,20,30)
val colorMap =
ColorMap(limits, colors).withNoDataColor(0)
val arr = Array(10,10,10,10,10,10,10,20,20,20,20,30,30,30,40,50)
val r = createTile(arr)
val color: IndexedPngEncoding =
PngColorEncoding(colorMap.colors, colorMap.options.noDataColor, colorMap.options.noDataColor) match {
case i @ IndexedPngEncoding(_, _) => i
case _ =>
withClue(s"Color should be Indexed") { sys.error("") }
}
colorMap.map(10) should be (10)
colorMap.map(20) should be (10)
colorMap.map(30) should be (20)
colorMap.map(40) should be (20)
colorMap.map(50) should be (30)
}
it("should render transparent pixels for nodata and unmapped values on Double tiles"){
val arr = Array[Double](Double.NaN, 2.0, 9000.0)
val tile = DoubleArrayTile(arr, 3, 1)
val cmap = ColorMap(Map[Double, Int](3.0 -> 0xFF0000FF))
val png = tile.renderPng(cmap)
val img = ImageIO.read(new ByteArrayInputStream(png.bytes))
val nd = new Color(img.getRGB(0, 0), true)
nd.getAlpha should be (0)
val fallback = new Color(img.getRGB(2, 0), true)
fallback.getAlpha should be (0)
}
it("should render transparent pixels for nodata and unmapped values on Int tiles"){
val arr = Array[Int](Int.MinValue, 2, 9000)
val tile = IntArrayTile(arr, 3, 1)
val cmap = ColorMap(Map[Int, Int](3 -> 0xFF0000FF))
val png = tile.renderPng(cmap)
val img = ImageIO.read(new ByteArrayInputStream(png.bytes))
val nd = new Color(img.getRGB(0, 0), true)
nd.getAlpha should be (0)
val fallback = new Color(img.getRGB(2, 0), true)
fallback.getAlpha should be (0)
}
}
describe("ColorMap class boundary type option") {
it("should map LessThan correctly on class boundary borders - int") {
val colorMap =
ColorMap(
Map(
19 -> 0xFF0000FF,
23 -> 0x00FF00FF,
26 -> 0x0000FFFF
),
ColorMap.Options(classBoundaryType = LessThan)
)
val color = colorMap.map(23)
withClue(f"Actual color = ${color}%02X: - int") {
color should be (0x0000FFFF)
}
}
it("should map LessThanOrEqualTo correctly on class boundary borders - int") {
val colorMap =
ColorMap(
Map(
19 -> 0xFF0000FF,
23 -> 0x00FF00FF,
26 -> 0x0000FFFF
),
ColorMap.Options(classBoundaryType = LessThanOrEqualTo)
)
val color = colorMap.map(23)
withClue(f"Actual color = ${color}%02X: - int") {
color should be (0x00FF00FF)
}
}
it("should map GreaterThan correctly on class boundary borders - int") {
val colorMap =
ColorMap(
Map(
19 -> 0xFF0000FF,
23 -> 0x00FF00FF,
26 -> 0x0000FFFF
),
ColorMap.Options(classBoundaryType = GreaterThan)
)
val color = colorMap.map(23)
withClue(f"Actual color = ${color}%02X: - int") {
color should be (0xFF0000FF)
}
}
it("should map GreaterThanOrEqualTo correctly on class boundary borders - int") {
val colorMap =
ColorMap(
Map(
19 -> 0xFF0000FF,
23 -> 0x00FF00FF,
26 -> 0x0000FFFF
),
ColorMap.Options(classBoundaryType = GreaterThanOrEqualTo)
)
val color = colorMap.map(23)
withClue(f"Actual color = ${color}%02X:") {
color should be (0x00FF00FF)
}
}
it("should map LessThan correctly on class boundary borders - double") {
val colorMap =
ColorMap(
Map(
19.5 -> 0xFF0000FF,
23.5 -> 0x00FF00FF,
26.5 -> 0x0000FFFF
),
ColorMap.Options(classBoundaryType = LessThan)
)
val color = colorMap.mapDouble(23.5)
withClue(f"Actual color = ${color}%02X: - double") {
color should be (0x0000FFFF)
}
}
it("should map LessThanOrEqualTo correctly on class boundary borders - double") {
val colorMap =
ColorMap(
Map(
19.5 -> 0xFF0000FF,
23.5 -> 0x00FF00FF,
26.5 -> 0x0000FFFF
),
ColorMap.Options(classBoundaryType = LessThanOrEqualTo)
)
val color = colorMap.mapDouble(23.5)
withClue(f"Actual color = ${color}%02X: - double") {
color should be (0x00FF00FF)
}
}
it("should map GreaterThan correctly on class boundary borders - double") {
val colorMap =
ColorMap(
Map(
19.5 -> 0xFF0000FF,
23.5 -> 0x00FF00FF,
26.5 -> 0x0000FFFF
),
ColorMap.Options(classBoundaryType = GreaterThan)
)
val color = colorMap.mapDouble(23.5)
withClue(f"Actual color = ${color}%02X: - double") {
color should be (0xFF0000FF)
}
}
it("should map GreaterThanOrEqualTo correctly on class boundary borders - double") {
val colorMap =
ColorMap(
Map(
19.5 -> 0xFF0000FF,
23.5 -> 0x00FF00FF,
26.5 -> 0x0000FFFF
),
ColorMap.Options(classBoundaryType = GreaterThanOrEqualTo)
)
val color = colorMap.mapDouble(23.5)
withClue(f"Actual color = ${color}%02X:") {
color should be (0x00FF00FF)
}
}
}
}