Statistics
| Revision:

root / trunk / libraries / libTopology / src-test / org / gvsig / topology / topologyrules / MustNotHaveRepeatedPointsTest.java @ 14150

History | View | Annotate | Download (7.06 KB)

1
/*
2
 * Created on 18-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: MustNotHaveRepeatedPointsTest.java,v 1.2 2007/09/19 16:36:56 azabala Exp $
47
* $Log: MustNotHaveRepeatedPointsTest.java,v $
48
* Revision 1.2  2007/09/19 16:36:56  azabala
49
* added snap repeated points test
50
*
51
* Revision 1.1  2007/09/19 09:22:34  azabala
52
* first version in cvs
53
*
54
*
55
*/
56
package org.gvsig.topology.topologyrules;
57

    
58
import java.util.Collection;
59

    
60
import junit.framework.TestCase;
61

    
62
import org.gvsig.jts.RepeatedPointTester;
63
import org.gvsig.jts.SnapCoordinateList;
64
import org.gvsig.jts.SnapRepeatedPointTester;
65
import org.gvsig.topology.TopologyRuleDefinitionException;
66
import org.gvsig.topology.util.TestTopologyErrorContainer;
67

    
68
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
69
import com.vividsolutions.jts.geom.Coordinate;
70
import com.vividsolutions.jts.geom.Geometry;
71
import com.vividsolutions.jts.geom.GeometryFactory;
72
import com.vividsolutions.jts.geom.PrecisionModel;
73
import com.vividsolutions.jts.io.ParseException;
74
import com.vividsolutions.jts.io.WKTReader;
75

    
76
public class MustNotHaveRepeatedPointsTest extends TestCase {
77

    
78
        PrecisionModel pm = new PrecisionModel(10000);
79
        GeometryFactory factory = new GeometryFactory(pm);
80
        WKTReader wktReader = new WKTReader(factory);
81
        
82
        
83
        Geometry multiPoint;
84
        Geometry multiPoint2;
85
        Geometry lineString;
86
        Geometry polygon;
87
        
88
        
89
        public void setUp() throws Exception{
90
                super.setUp();
91
                multiPoint = wktReader.read("MULTIPOINT (520 160, 540 180, 540 180, 540 180, 600 180, 580 140, 640 140, 640 140, 640 160)");
92
                multiPoint2 = wktReader.read("MULTIPOINT (520 160, 540 180, 600 180, 580 140, 640 140,  640 160)");
93
                lineString = wktReader.read("LINESTRING (520 120, 560 180, 560 180, 560 180, 620 160, 660 200, 700 140, 700 140, 700 140, 620 120)");
94
                polygon = wktReader.read("POLYGON ((600 180, 600 180, 600 180, 600 180, 540 200, 480 140, 540 120, 540 120, 680 160, 600 180, 600 180))");
95
        }
96
        
97
        public void tearDown() throws Exception{
98
                super.tearDown();
99
                multiPoint = null;
100
                lineString = null;
101
                polygon = null;
102
                pm = null;
103
                factory = null;
104
                wktReader = null;
105
        }
106
        
107
        
108
        public void testRepeatedPointTester() {
109
                RepeatedPointTester tester = new RepeatedPointTester();
110
                
111
                boolean bMultiPoint = tester.hasRepeatedPoint(multiPoint);
112
                Collection<Coordinate> mp = tester.getRepeatedCoordinates();
113
                tester.clear();
114
                
115
                boolean bLineString = tester.hasRepeatedPoint(lineString);
116
                Collection<Coordinate> mp2 = tester.getRepeatedCoordinates();
117
                tester.clear();
118
                
119
                
120
                boolean bPolygon = tester.hasRepeatedPoint(polygon);
121
                Collection<Coordinate> mp3 = tester.getRepeatedCoordinates();
122
                
123
                assertTrue(bMultiPoint);
124
                assertTrue(bLineString);
125
                assertTrue(bPolygon);
126
                
127
                assertTrue(mp.size() == 2);
128
                assertTrue(mp2.size() == 2);
129
                assertTrue(mp3.size() == 2);
130
        }
131
        
132
        public void testRemoveRepeatedPoints(){
133
                RepeatedPointTester tester = new RepeatedPointTester();
134
                multiPoint = tester.removeRepeatedPoints(multiPoint);
135
                
136
                lineString = tester.removeRepeatedPoints(lineString);
137
                
138
                polygon = tester.removeRepeatedPoints(polygon);
139
        }
140
        
141
        public void testMustNotHaveRepeatedPointsRule() throws ParseException{
142
                
143
                FLyrVect multiPointLayer = org.gvsig.topology.util.LayerFactory.getLyrWithRepeatedCoords();
144
                MustNotHaveRepeatedPoints rule = new MustNotHaveRepeatedPoints(multiPointLayer);
145
                TestTopologyErrorContainer errorContainer = new TestTopologyErrorContainer();
146
                rule.setTopologyErrorContainer(errorContainer);
147
                try {
148
                        rule.checkPreconditions();
149
                        rule.checkRule();
150
                        int numberOfErrors = errorContainer.getNumberOfErrors();
151
                        assertTrue(numberOfErrors == 1);
152
                } catch (TopologyRuleDefinitionException e) {
153
                        // TODO Auto-generated catch block
154
                        e.printStackTrace();
155
                }
156
        }
157
        
158
        
159
        public void testSnapCoordinateList(){
160
                SnapCoordinateList coordList = new SnapCoordinateList(0.01);
161
                
162
                Coordinate coord1 = new Coordinate(100, 100);
163
                Coordinate coord2 = new Coordinate(200, 200);
164
                Coordinate coord3 = new Coordinate(300, 100);
165
                Coordinate coord4 = new Coordinate(400, 400);
166
                Coordinate coord5 = new Coordinate(400.001, 400.001);
167
                
168
                coordList.add(coord1, false);
169
                coordList.add(coord2, false);
170
                coordList.add(coord3, false);
171
                coordList.add(coord4, false);
172
                coordList.add(coord5, false);
173
                
174
                Coordinate[] coords = coordList.toCoordinateArray();
175
                assertTrue(coords.length == 4);
176
                
177
        }
178
        
179
        /**
180
         * The same test that testRepeatedPointTester with snapped coordinates
181
         * @throws ParseException
182
         */
183
        public void testSnapRepeatedPointTester() throws ParseException{
184
                
185
                Geometry multiPoint = wktReader.read("MULTIPOINT (520 160, 540 180, 540.002 179.999, 540.01 179.999, 600 180, 580 140, 640 140, 640 140, 640 160)");
186
                Geometry lineString = wktReader.read("LINESTRING (520 120, 560 180, 560 180.001, 560.001 180, 620 160, 660 200, 700 140, 700 140, 700 140, 620 120)");
187
                Geometry polygon = wktReader.read("POLYGON ((600 180, 600 179.999, 600.004 180, 600 180, 540 200, 480 140, 540 120, 540 120, 680 160, 600 180, 600 180))");
188
                SnapRepeatedPointTester tester = new SnapRepeatedPointTester(0.011);
189
                
190
                boolean bMultiPoint = tester.hasRepeatedPoint(multiPoint);
191
                Collection<Coordinate> mpA = tester.getRepeatedCoordinates();
192
                tester.clear();
193
                
194
                boolean bLineString = tester.hasRepeatedPoint(lineString);
195
                Collection<Coordinate> mpB = tester.getRepeatedCoordinates();
196
                tester.clear();
197
                
198
                
199
                boolean bPolygon = tester.hasRepeatedPoint(polygon);
200
                Collection<Coordinate> mpC = tester.getRepeatedCoordinates();
201
                
202
                assertTrue(bMultiPoint);
203
                assertTrue(bLineString);
204
                assertTrue(bPolygon);
205
                
206
                assertTrue(mpA.size() == 2);
207
                assertTrue(mpB.size() == 2);
208
                assertTrue(mpC.size() == 2);
209
                
210
                multiPoint = tester.removeRepeatedPoints(multiPoint);
211
                lineString = tester.removeRepeatedPoints(lineString);
212
                polygon = tester.removeRepeatedPoints(polygon);
213
        }
214

    
215
}
216