/
ZOrderTest.java
375 lines (307 loc) · 13.3 KB
/
ZOrderTest.java
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
/*
* GeoTools - The Open Source Java GIS Toolkit
* http://geotools.org
*
* (C) 2015, Open Source Geospatial Foundation (OSGeo)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
package org.geotools.renderer.lite;
import static java.awt.RenderingHints.KEY_ANTIALIASING;
import static java.awt.RenderingHints.VALUE_ANTIALIAS_ON;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;
import org.geotools.data.property.PropertyDataStore;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.geometry.jts.ReferencedEnvelope;
import org.geotools.image.test.ImageAssert;
import org.geotools.map.FeatureLayer;
import org.geotools.map.MapContent;
import org.geotools.renderer.RenderListener;
import org.geotools.styling.FeatureTypeStyle;
import org.geotools.styling.Style;
import org.geotools.test.TestData;
import org.junit.Before;
import org.junit.Test;
import org.opengis.feature.simple.SimpleFeature;
/**
* Z-order rendering test making use of FeatureTypeStyle sortBy to refine query used for styling.
*
* @source $URL$
*/
public class ZOrderTest {
private static final long TIME = 40000;
private static final int THRESHOLD = 100;
SimpleFeatureSource zsquares;
SimpleFeatureSource zroads;
ReferencedEnvelope bounds;
SimpleFeatureSource zbuildings;
@Before
public void setUp() throws Exception {
File property = new File(TestData.getResource(this, "zorder/zsquares.properties").toURI());
PropertyDataStore ds = new PropertyDataStore(property.getParentFile());
zsquares = ds.getFeatureSource("zsquares");
zroads = ds.getFeatureSource("zroads");
zbuildings = ds.getFeatureSource("zbuildings");
bounds = zsquares.getBounds();
bounds.expandBy(0.2, 0.2);
// System.setProperty("org.geotools.test.interactive", "true");
}
@Test
public void testSingleZAscending() throws Exception {
runZSquaresTest("zorder/zpolygon.sld", "z", "z-ascending");
}
@Test
public void testSingleZDescending() throws Exception {
runZSquaresTest("zorder/zpolygon.sld", "z D", "z-descending");
}
@Test
public void testSingleCatDescendingZDescending() throws Exception {
runZSquaresTest("zorder/zpolygon.sld", "cat D,z D", "cat-desc-z-desc");
}
@Test
public void testSingleCatDescendingZAscending() throws Exception {
runZSquaresTest("zorder/zpolygon.sld", "cat D,z A", "cat-desc-z-asc");
}
@Test
public void testCrossRoadsNoGroup() throws Exception {
runRoadsTest("zorder/zroads.sld", "z", null, "roads-no-group");
}
@Test
public void testCrossRoadsGrouped() throws Exception {
runRoadsTest("zorder/zroads.sld", "z", "theGroup", "roads-group");
}
@Test
public void testCrossRoadsComplexSort() throws Exception {
runRoadsTest("zorder/zroads.sld", "cat D, name D", "theGroup", "roads-group-complex-sort");
}
@Test
public void testCrossRoadsBuildingsOnZ() throws Exception {
runRoadsBuildingTest("z", "theGroup", "roads-buildings-group-z");
}
@Test
public void testCrossRoadsBuildingsZoomedOut() throws Exception {
bounds.expandBy(10, 10);
runRoadsBuildingTest("z", "theGroup", "roads-buildings-group-z-zoomedout");
}
private void runZSquaresTest(String styleName, String sortBy, String referenceImageName)
throws Exception {
Style style = RendererBaseTest.loadStyle(this, styleName);
forceSortBy(style, sortBy);
MapContent mc = new MapContent();
mc.addLayer(new FeatureLayer(zsquares, style));
runImageComparison(referenceImageName, mc);
}
private void runRoadsTest(String styleName, String sortBy, String sortByGroup,
String referenceImageName) throws Exception {
Style style = RendererBaseTest.loadStyle(this, styleName);
forceSortBy(style, sortBy);
forceSortByGroup(style, sortByGroup);
MapContent mc = new MapContent();
mc.addLayer(new FeatureLayer(zroads, style));
runImageComparison(referenceImageName, mc);
}
private void runRoadsBuildingTest(String sortBy, String sortByGroup,
String referenceImageName) throws Exception {
Style roadsStyle = RendererBaseTest.loadStyle(this, "zorder/zroads.sld");
forceSortBy(roadsStyle, sortBy);
forceSortByGroup(roadsStyle, sortByGroup);
Style buildingsStyle = RendererBaseTest.loadStyle(this, "zorder/zbuildings.sld");
forceSortBy(buildingsStyle, sortBy);
forceSortByGroup(buildingsStyle, sortByGroup);
MapContent mc = new MapContent();
mc.addLayer(new FeatureLayer(zroads, roadsStyle));
mc.addLayer(new FeatureLayer(zbuildings, buildingsStyle));
runImageComparison(referenceImageName, mc);
}
private void runImageComparison(String referenceImageName, MapContent mc)
throws Exception, IOException {
StreamingRenderer renderer = new StreamingRenderer();
renderer.setMapContent(mc);
renderer.setJava2DHints(new RenderingHints(KEY_ANTIALIASING, VALUE_ANTIALIAS_ON));
renderer.addRenderListener(new RenderListener() {
@Override
public void featureRenderer(SimpleFeature feature) {
// nothing to do
}
@Override
public void errorOccurred(Exception e) {
fail("No rendering errors expected, but got one: " + e.getMessage());
}
});
BufferedImage image = RendererBaseTest.showRender(referenceImageName, renderer, TIME,
bounds);
File reference = new File(
"./src/test/resources/org/geotools/renderer/lite/test-data/zorder/"
+ referenceImageName + ".png");
ImageAssert.assertEquals(reference, image, THRESHOLD);
mc.dispose();
}
private void forceSortBy(Style style, String sortBy) {
if (sortBy != null) {
for (FeatureTypeStyle fts : style.featureTypeStyles()) {
fts.getOptions().put(FeatureTypeStyle.SORT_BY, sortBy);
}
}
}
private void forceSortByGroup(Style style, String sortByGroup) {
if (sortByGroup != null) {
for (FeatureTypeStyle fts : style.featureTypeStyles()) {
fts.getOptions().put(FeatureTypeStyle.SORT_BY_GROUP, sortByGroup);
}
}
}
@Test
public void testInvalidAttribute() throws Exception {
final AtomicInteger failureCount = new AtomicInteger(0);
runZRoadsFailureTest("zorder/zpolygon.sld", "not-there", null, new RenderListener() {
@Override
public void featureRenderer(SimpleFeature feature) {
// nothing to do
}
@Override
public void errorOccurred(Exception e) {
failureCount.incrementAndGet();
assertTrue(e instanceof IllegalArgumentException);
assertTrue(e.getMessage().contains("not-there"));
}
});
assertEquals(1, failureCount.get());
}
@Test
public void testInvalidSortBySyntax() throws Exception {
final AtomicInteger failureCount = new AtomicInteger(0);
runZRoadsFailureTest("zorder/zpolygon.sld", "z upsideDown", null, new RenderListener() {
@Override
public void featureRenderer(SimpleFeature feature) {
// nothing to do
}
@Override
public void errorOccurred(Exception e) {
failureCount.incrementAndGet();
assertTrue(e instanceof IllegalArgumentException);
assertTrue(e.getMessage().contains("z upsideDown"));
}
});
assertEquals(1, failureCount.get());
}
@Test
public void testNonComparable() throws Exception {
final AtomicInteger failureCount = new AtomicInteger(0);
runZRoadsFailureTest("zorder/zpolygon.sld", "color", null, new RenderListener() {
@Override
public void featureRenderer(SimpleFeature feature) {
// nothing to do
}
@Override
public void errorOccurred(Exception e) {
failureCount.incrementAndGet();
assertTrue(e instanceof IllegalArgumentException);
assertTrue(e.getMessage().contains("color"));
assertTrue(e.getMessage().contains("sort"));
}
});
assertEquals(1, failureCount.get());
}
@Test
public void testIncompatibleAttributes() throws Exception {
final AtomicInteger failureCount = new AtomicInteger(0);
runRoadsBuildFailureTest("zorder/zpolygon.sld", "z", "cat", "theGroup",
new RenderListener() {
@Override
public void featureRenderer(SimpleFeature feature) {
// nothing to do
}
@Override
public void errorOccurred(Exception e) {
failureCount.incrementAndGet();
assertTrue(e instanceof IllegalArgumentException);
assertTrue(e.getMessage().contains("z"));
assertTrue(e.getMessage().contains("cat"));
assertTrue(e.getMessage().contains("incompatible"));
}
});
assertEquals(1, failureCount.get());
}
@Test
public void testIncompatibleDirections() throws Exception {
final AtomicInteger failureCount = new AtomicInteger(0);
runRoadsBuildFailureTest("zorder/zpolygon.sld", "z A", "z D", "theGroup",
new RenderListener() {
@Override
public void featureRenderer(SimpleFeature feature) {
// nothing to do
}
@Override
public void errorOccurred(Exception e) {
failureCount.incrementAndGet();
assertTrue(e instanceof IllegalArgumentException);
assertTrue(e.getMessage().contains("order"));
}
});
assertEquals(1, failureCount.get());
}
@Test
public void testDifferentLenghts() throws Exception {
final AtomicInteger failureCount = new AtomicInteger(0);
runRoadsBuildFailureTest("zorder/zpolygon.sld", "z, cat", "z", "theGroup",
new RenderListener() {
@Override
public void featureRenderer(SimpleFeature feature) {
// nothing to do
}
@Override
public void errorOccurred(Exception e) {
failureCount.incrementAndGet();
assertTrue(e instanceof IllegalArgumentException);
assertTrue(e.getMessage().contains("number of attributes"));
}
});
assertEquals(1, failureCount.get());
}
private void runZRoadsFailureTest(String styleName, String sortBy, String sortByGroup,
RenderListener listener)
throws Exception {
Style style = RendererBaseTest.loadStyle(this, styleName);
forceSortBy(style, sortBy);
forceSortByGroup(style, sortByGroup);
MapContent mc = new MapContent();
mc.addLayer(new FeatureLayer(zroads, style));
runFailureTest(listener, mc);
}
private void runFailureTest(RenderListener listener, MapContent mc) throws Exception {
StreamingRenderer renderer = new StreamingRenderer();
renderer.setMapContent(mc);
renderer.setJava2DHints(new RenderingHints(KEY_ANTIALIASING, VALUE_ANTIALIAS_ON));
renderer.addRenderListener(listener);
RendererBaseTest.showRender("failures", renderer, TIME, bounds);
mc.dispose();
}
private void runRoadsBuildFailureTest(String styleName, String sortByRoads,
String sortByBuildings, String sortByGroup, RenderListener listener) throws Exception {
Style roadsStyle = RendererBaseTest.loadStyle(this, "zorder/zroads.sld");
forceSortBy(roadsStyle, sortByRoads);
forceSortByGroup(roadsStyle, sortByGroup);
Style buildingsStyle = RendererBaseTest.loadStyle(this, "zorder/zbuildings.sld");
forceSortBy(buildingsStyle, sortByBuildings);
forceSortByGroup(buildingsStyle, sortByGroup);
MapContent mc = new MapContent();
mc.addLayer(new FeatureLayer(zroads, roadsStyle));
mc.addLayer(new FeatureLayer(zbuildings, buildingsStyle));
runFailureTest(listener, mc);
}
}