svn-gvsig-desktop / trunk / libraries / libTopology / src-test / org / gvsig / topology / TopologyTest.java @ 16396
History | View | Annotate | Download (11.2 KB)
1 |
/*
|
---|---|
2 |
* Created on 24-sep-2007
|
3 |
*
|
4 |
* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
5 |
*
|
6 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
7 |
*
|
8 |
* This program is free software; you can redistribute it and/or
|
9 |
* modify it under the terms of the GNU General Public License
|
10 |
* as published by the Free Software Foundation; either version 2
|
11 |
* of the License, or (at your option) any later version.
|
12 |
*
|
13 |
* This program is distributed in the hope that it will be useful,
|
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
16 |
* GNU General Public License for more details.
|
17 |
*
|
18 |
* You should have received a copy of the GNU General Public License
|
19 |
* along with this program; if not, write to the Free Software
|
20 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
21 |
*
|
22 |
* For more information, contact:
|
23 |
*
|
24 |
* Generalitat Valenciana
|
25 |
* Conselleria d'Infraestructures i Transport
|
26 |
* Av. Blasco Ib??ez, 50
|
27 |
* 46010 VALENCIA
|
28 |
* SPAIN
|
29 |
*
|
30 |
* +34 963862235
|
31 |
* gvsig@gva.es
|
32 |
* www.gvsig.gva.es
|
33 |
*
|
34 |
* or
|
35 |
*
|
36 |
* IVER T.I. S.A
|
37 |
* Salamanca 50
|
38 |
* 46005 Valencia
|
39 |
* Spain
|
40 |
*
|
41 |
* +34 963163400
|
42 |
* dac@iver.es
|
43 |
*/
|
44 |
/* CVS MESSAGES:
|
45 |
*
|
46 |
* $Id$
|
47 |
* $Log$
|
48 |
*
|
49 |
*/
|
50 |
package org.gvsig.topology; |
51 |
|
52 |
import java.io.File; |
53 |
import java.net.URL; |
54 |
import java.util.HashMap; |
55 |
import java.util.List; |
56 |
import java.util.Map; |
57 |
|
58 |
import junit.framework.TestCase; |
59 |
|
60 |
import org.cresques.cts.IProjection; |
61 |
import org.gvsig.topology.topologyrules.MustBeLargerThanClusterTolerance; |
62 |
import org.gvsig.topology.topologyrules.MustNotHaveRepeatedPoints; |
63 |
import org.gvsig.topology.topologyrules.jtsisvalidrules.GeometryMustHaveValidCoordinates; |
64 |
import org.gvsig.topology.util.LayerFactory; |
65 |
|
66 |
import com.iver.cit.gvsig.exceptions.layers.LoadLayerException; |
67 |
import com.iver.cit.gvsig.fmap.MapContext; |
68 |
import com.iver.cit.gvsig.fmap.ViewPort; |
69 |
import com.iver.cit.gvsig.fmap.core.FShape; |
70 |
import com.iver.cit.gvsig.fmap.crs.CRSFactory; |
71 |
import com.iver.cit.gvsig.fmap.layers.FLayer; |
72 |
import com.iver.cit.gvsig.fmap.layers.FLayers; |
73 |
import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
74 |
|
75 |
/**
|
76 |
* Unit tests for Topology class.
|
77 |
*
|
78 |
* @author azabala
|
79 |
*
|
80 |
*/
|
81 |
public class TopologyTest extends TestCase { |
82 |
File baseDataPath;
|
83 |
|
84 |
File baseDriversPath;
|
85 |
|
86 |
IProjection PROJECTION_DEFAULT; |
87 |
|
88 |
ViewPort VIEWPORT; |
89 |
|
90 |
FLayers ROOT; |
91 |
|
92 |
SimpleTopologyErrorContainer errorContainer; |
93 |
|
94 |
Topology topology = null;
|
95 |
|
96 |
Topology topology2 = null;
|
97 |
|
98 |
FLyrVect multiPointLayer; |
99 |
|
100 |
FLyrVect lineLyrWithCollapsedCoords; |
101 |
|
102 |
FLyrVect shapeBasedLinearLyr; |
103 |
|
104 |
MapContext mapContext; |
105 |
|
106 |
|
107 |
public void setUp() throws Exception{ |
108 |
super.setUp();
|
109 |
URL url = TopologyTest.class.getResource("testdata"); |
110 |
if (url == null) |
111 |
throw new Exception("No se encuentra el directorio con datos de prueba"); |
112 |
|
113 |
baseDataPath = new File(url.getFile()); |
114 |
if (!baseDataPath.exists())
|
115 |
throw new Exception("No se encuentra el directorio con datos de prueba"); |
116 |
|
117 |
baseDriversPath = new File("../_fwAndami/gvSIG/extensiones/com.iver.cit.gvsig/drivers"); |
118 |
if (!baseDriversPath.exists())
|
119 |
throw new Exception("Can't find drivers path " ); |
120 |
|
121 |
com.iver.cit.gvsig.fmap.layers.LayerFactory.setDriversPath(baseDriversPath.getAbsolutePath()); |
122 |
|
123 |
|
124 |
PROJECTION_DEFAULT = CRSFactory.getCRS("EPSG:23030");
|
125 |
VIEWPORT = new ViewPort(PROJECTION_DEFAULT);
|
126 |
mapContext = new MapContext(VIEWPORT);
|
127 |
ROOT = mapContext.getLayers(); |
128 |
errorContainer = new SimpleTopologyErrorContainer();
|
129 |
|
130 |
topology = new Topology(mapContext, ROOT, 0.01, 1000, errorContainer ); |
131 |
topology2 = new Topology(mapContext, ROOT, 0.01, 1000, errorContainer); |
132 |
|
133 |
|
134 |
multiPointLayer = LayerFactory.getLyrWithRepeatedCoords(); |
135 |
lineLyrWithCollapsedCoords = LayerFactory.getLineLayerWithCollapsedCoords(); |
136 |
shapeBasedLinearLyr = (FLyrVect) newLayer("vc1-1500.shp", "gvSIG shp driver"); |
137 |
} |
138 |
|
139 |
public void tearDown() throws Exception{ |
140 |
super.tearDown();
|
141 |
topology = null;
|
142 |
topology2 = null;
|
143 |
multiPointLayer = null;
|
144 |
lineLyrWithCollapsedCoords = null;
|
145 |
} |
146 |
|
147 |
public FLayer newLayer(String fileName, |
148 |
String driverName)
|
149 |
throws LoadLayerException {
|
150 |
FLayer solution = null;
|
151 |
File file = new File(baseDataPath, fileName); |
152 |
solution = com.iver.cit.gvsig.fmap.layers.LayerFactory.createLayer(fileName, |
153 |
driverName, |
154 |
file, PROJECTION_DEFAULT); |
155 |
solution.setAvailable(true);
|
156 |
return solution;
|
157 |
|
158 |
} |
159 |
|
160 |
/*
|
161 |
* To test in a Topology:
|
162 |
*
|
163 |
* 1. If we change cluster tolerance, status must be reset:
|
164 |
* -NON_VALIDATED.
|
165 |
* -Dirty zones clear.
|
166 |
* -Topology error clear.
|
167 |
* -Cluster tolerance rules updated with new cluster tolerance
|
168 |
*
|
169 |
*
|
170 |
*
|
171 |
* 2. If number of errors is greater than max number of errors during
|
172 |
* a topology validation, process will be interrupted.
|
173 |
*
|
174 |
* 3. We cant add rules with layers not referenced by the topology.
|
175 |
*
|
176 |
* 4. deleted.
|
177 |
*
|
178 |
* 5. If we add a new layer to a topology with status VALIDATED, new status
|
179 |
* will be VALIDATED_WITH_DIRTY_ZONES, and a new dirty zone for the layer
|
180 |
* full extent will be created. If layer shape type is POINT or MULTIPOINT,
|
181 |
* status will be VALIDATED, because this kind of layer dont have MustBeGreaterThanClusterTolerance
|
182 |
* rule associated.
|
183 |
*
|
184 |
|
185 |
TEST 6 NOT IMPLEMENTED YET
|
186 |
* 6. If a feature's geometry of a topology's layer is edited, a new dirty
|
187 |
* zone will be create for its bounds:
|
188 |
* -with the exception if topology status would be NON_VALIDATED.
|
189 |
|
190 |
|
191 |
* 7. If we validate a topology with status NON_VALIDATED, and it finds errors,
|
192 |
* new status will be VALIDATED_WITH_ERRORS.
|
193 |
* If it doesnt find errors, status wll be VALIDATED.
|
194 |
*
|
195 |
* 8. If two errors are in the same dirty zone, they dont add new dirty
|
196 |
* zones.
|
197 |
|
198 |
|
199 |
* 9. If a topology error is added to a topology, dirty zones will be
|
200 |
* updated (with the exception if topology error'geometry bounds is in an existent dirty zone)
|
201 |
*
|
202 |
* 10. If we mark a topology exception as an exception, it will be removed
|
203 |
* from dirty zones. If the number of exceptions is equal to the number of
|
204 |
* errors, new status will be VALIDATED.
|
205 |
*
|
206 |
* 11. If we demote a error from exception to error, it will be added to
|
207 |
* dirty zones. If number of errors is superior to number of exceptions,
|
208 |
* new status will be VALIDATED_WITH_ERRORS.
|
209 |
*
|
210 |
* TEST 12 NOT IMPLEMENTED YET
|
211 |
* 12. We must filter topology errors by rule, layer, geometry type, etc.
|
212 |
*
|
213 |
* */
|
214 |
public void testStatusAndCoherence() throws Exception{ |
215 |
|
216 |
//first test: cluster tolerance 0.1d, not collapsed geometries
|
217 |
topology.addLayer(lineLyrWithCollapsedCoords); |
218 |
topology.validate(); |
219 |
int status = topology.getStatus();
|
220 |
assertTrue(status == Topology.VALIDATED); |
221 |
int numberOfErrors = topology.getNumberOfErrors();
|
222 |
assertTrue(numberOfErrors == 0);
|
223 |
|
224 |
//second test: cluster tolerance 12d, one topology error
|
225 |
topology.setClusterTolerance(12d);
|
226 |
topology.validate(); |
227 |
status = topology.getStatus(); |
228 |
assertTrue(status == Topology.VALIDATED_WITH_ERRORS); |
229 |
numberOfErrors = topology.getNumberOfErrors(); |
230 |
assertTrue(numberOfErrors == 1);
|
231 |
TopologyError error = topology.getTopologyError(0);
|
232 |
assertTrue(error.getViolatedRule().getClass().equals(MustBeLargerThanClusterTolerance.class)); |
233 |
|
234 |
|
235 |
//third test: we add a point layer with repeated points
|
236 |
topology.addLayer(multiPointLayer); |
237 |
MustNotHaveRepeatedPoints rule1 = |
238 |
new MustNotHaveRepeatedPoints(multiPointLayer);
|
239 |
topology.addRule(rule1); |
240 |
status = topology.getStatus(); |
241 |
assertTrue(status == Topology.NOT_VALIDATED); |
242 |
int numberOfDirtyZones = topology.getNumberOfDirtyZones();
|
243 |
assertTrue(numberOfDirtyZones == 0);
|
244 |
|
245 |
topology.validate(); |
246 |
status = topology.getStatus(); |
247 |
assertTrue(status == Topology.VALIDATED_WITH_ERRORS); |
248 |
numberOfErrors = topology.getNumberOfErrors(); |
249 |
assertTrue(numberOfErrors == 2);
|
250 |
|
251 |
topology.resetStatus(); |
252 |
topology.setMaxNumberOfErrors(1);
|
253 |
topology.validate(); |
254 |
assertTrue(topology.getNumberOfErrors() == 1);
|
255 |
topology.resetStatus(); |
256 |
topology.setMaxNumberOfErrors(1000);
|
257 |
topology.validate(); |
258 |
assertTrue(topology.getNumberOfErrors() == 2);
|
259 |
|
260 |
//mark as exception
|
261 |
for(int i = 0; i < numberOfErrors; i++){ |
262 |
error = topology.getTopologyError(i); |
263 |
topology.markAsTopologyException(error); |
264 |
} |
265 |
assertTrue(topology.getStatus() == Topology.VALIDATED); |
266 |
|
267 |
} |
268 |
|
269 |
|
270 |
public void testAddRuleWithLayersNotReferenced(){ |
271 |
MustNotHaveRepeatedPoints rule = |
272 |
new MustNotHaveRepeatedPoints(multiPointLayer);
|
273 |
boolean ok = false; |
274 |
try {
|
275 |
topology2.addRule(rule); |
276 |
ok = true;
|
277 |
} catch (RuleNotAllowedException e) {
|
278 |
// TODO Auto-generated catch block
|
279 |
e.printStackTrace(); |
280 |
} catch (TopologyRuleDefinitionException e) {
|
281 |
// TODO Auto-generated catch block
|
282 |
e.printStackTrace(); |
283 |
} |
284 |
assertTrue(!ok); |
285 |
} |
286 |
|
287 |
public void testDemoteExceptionsToErrors() throws RuleNotAllowedException, TopologyRuleDefinitionException{ |
288 |
topology2.setClusterTolerance(12d);
|
289 |
topology2.addLayer(multiPointLayer); |
290 |
topology2.addLayer(this.lineLyrWithCollapsedCoords);
|
291 |
topology2.addRule(new MustNotHaveRepeatedPoints(multiPointLayer));
|
292 |
|
293 |
topology2.validate(); |
294 |
int numberOfErrors = topology2.getNumberOfErrors();
|
295 |
assertTrue(numberOfErrors == 2);
|
296 |
for(int i = 0; i < numberOfErrors; i++){ |
297 |
TopologyError error = topology2.getTopologyError(i); |
298 |
topology2.markAsTopologyException(error); |
299 |
} |
300 |
assertTrue(topology2.getStatus() == Topology.VALIDATED); |
301 |
for(int i = 0; i < numberOfErrors; i++){ |
302 |
TopologyError error = topology2.getTopologyError(i); |
303 |
topology2.demoteToError(error); |
304 |
} |
305 |
assertTrue(topology2.getStatus() == Topology.VALIDATED_WITH_ERRORS); |
306 |
|
307 |
//At this point, topology have two topologyerrors (MustNotHaveRepeatedPoints and MustBeGreaterThanClusterTolerance
|
308 |
//shape types are LINE (FPolyline2D) and Point (FMultiPoint2D).
|
309 |
List<TopologyError> errorList = topology2.getTopologyErrorsByLyr(lineLyrWithCollapsedCoords, null, true); |
310 |
assertTrue(errorList.size() == 1);
|
311 |
|
312 |
errorList = topology.getTopologyErrorsByShapeType(FShape.LINE, null, true); |
313 |
assertTrue(errorList.size() == 1);
|
314 |
|
315 |
|
316 |
} |
317 |
|
318 |
public void testTopologyPersistence() throws RuleNotAllowedException, TopologyRuleDefinitionException{ |
319 |
topology2 = new Topology(mapContext, ROOT, 0.01, 1000, errorContainer); |
320 |
topology2.addLayer(this.shapeBasedLinearLyr);
|
321 |
MustNotHaveRepeatedPoints ruleA = new MustNotHaveRepeatedPoints(topology2, shapeBasedLinearLyr);
|
322 |
topology2.addRule(ruleA); |
323 |
GeometryMustHaveValidCoordinates ruleB = new
|
324 |
GeometryMustHaveValidCoordinates(topology2, shapeBasedLinearLyr); |
325 |
topology2.addRule(ruleB); |
326 |
|
327 |
String fileToSave1 = "/testTopology.xml"; |
328 |
Map<String, Object> storageParams = new HashMap<String, Object>(); |
329 |
storageParams.put(TopologyPersister.FILE_PARAM_NAME, fileToSave1); |
330 |
TopologyPersister.persist(topology2, storageParams); |
331 |
Topology topologyA = TopologyPersister.load(mapContext, storageParams); |
332 |
|
333 |
assertTrue(topology2.getRuleCount() == topologyA.getRuleCount()); |
334 |
assertTrue(topology2.getLayerCount() == topologyA.getLayerCount()); |
335 |
} |
336 |
|
337 |
} |
338 |
|