Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libTopology / src / org / gvsig / topology / topologyrules / FMapGeometryMustBeClosed.java @ 25601

History | View | Annotate | Download (12.8 KB)

1
/*
2
 * Created on 10-abr-2006
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
package org.gvsig.topology.topologyrules;
50

    
51
import java.awt.Color;
52
import java.awt.Shape;
53
import java.awt.geom.Point2D;
54
import java.util.ArrayList;
55
import java.util.Iterator;
56
import java.util.List;
57

    
58
import org.gvsig.fmap.core.FGeometryUtil;
59
import org.gvsig.fmap.core.MultipartShapeIterator;
60
import org.gvsig.fmap.core.NewFConverter;
61
import org.gvsig.fmap.core.ShapePointExtractor;
62
import org.gvsig.jts.JtsUtil;
63
import org.gvsig.jts.SnapLineStringSelfIntersectionChecker;
64
import org.gvsig.topology.AbstractTopologyRule;
65
import org.gvsig.topology.IRuleWithClusterTolerance;
66
import org.gvsig.topology.ITopologyErrorFix;
67
import org.gvsig.topology.ITopologyRuleWithExclusiveFix;
68
import org.gvsig.topology.Messages;
69
import org.gvsig.topology.Topology;
70
import org.gvsig.topology.TopologyError;
71
import org.gvsig.topology.TopologyRuleDefinitionException;
72
import org.gvsig.topology.errorfixes.CompleteUndershootFix;
73
import org.gvsig.topology.errorfixes.RemoveOvershootFix;
74

    
75
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
76
import com.iver.cit.gvsig.fmap.core.FGeometry;
77
import com.iver.cit.gvsig.fmap.core.FGeometryCollection;
78
import com.iver.cit.gvsig.fmap.core.FMultiPoint2D;
79
import com.iver.cit.gvsig.fmap.core.FNullGeometry;
80
import com.iver.cit.gvsig.fmap.core.FPoint2D;
81
import com.iver.cit.gvsig.fmap.core.FPoint3D;
82
import com.iver.cit.gvsig.fmap.core.FShape;
83
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
84
import com.iver.cit.gvsig.fmap.core.IFeature;
85
import com.iver.cit.gvsig.fmap.core.IGeometry;
86
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
87
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
88
import com.iver.cit.gvsig.fmap.core.symbols.MultiShapeSymbol;
89
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
90
import com.vividsolutions.jts.geom.Coordinate;
91
import com.vividsolutions.jts.geom.Geometry;
92
import com.vividsolutions.jts.geom.LineString;
93

    
94
/**
95
 * This rule checks that a fmap geometry is closed.
96
 * 
97
 * @author Alvaro Zabala
98
 * 
99
 */
100
public class FMapGeometryMustBeClosed extends AbstractTopologyRule 
101
        implements IRuleWithClusterTolerance, ITopologyRuleWithExclusiveFix {
102

    
103
        private static String RULE_NAME = Messages.getText("polygon_must_be_closed");
104

    
105
        private double clusterTolerance;
106
        
107
        /**
108
         * Symbol for topology errors caused by a violation of this rule.
109
         */
110
        private MultiShapeSymbol errorSymbol = DEFAULT_ERROR_SYMBOL;
111
        
112
        private static List<ITopologyErrorFix> automaticErrorFixes =
113
                new ArrayList<ITopologyErrorFix>();
114
        static{
115
                automaticErrorFixes.add(new RemoveOvershootFix());
116
                automaticErrorFixes.add(new CompleteUndershootFix());
117
                
118
        }
119
        
120
        private static final Color DEFAULT_ERROR_COLOR = Color.RED;
121
        
122
        //this symbol is multishape because this topology rule applies to lines or polygons
123
        private static final MultiShapeSymbol DEFAULT_ERROR_SYMBOL = 
124
                (MultiShapeSymbol) SymbologyFactory.createDefaultSymbolByShapeType(FShape.MULTI, 
125
                                                                                        DEFAULT_ERROR_COLOR);
126
        static{
127
                DEFAULT_ERROR_SYMBOL.setDescription(RULE_NAME);
128
                DEFAULT_ERROR_SYMBOL.setSize(0.5);
129
                DEFAULT_ERROR_SYMBOL.setLineWidth(0.5);
130
                DEFAULT_ERROR_SYMBOL.getOutline().setLineColor(DEFAULT_ERROR_COLOR);
131
                DEFAULT_ERROR_SYMBOL.setFillColor(DEFAULT_ERROR_COLOR);
132
        }
133

    
134
        /**
135
         * Constructor.
136
         * 
137
         * @param topology
138
         * @param originLyr
139
         * @param snapTolerance
140
         */
141
        public FMapGeometryMustBeClosed(Topology topology, FLyrVect originLyr,
142
                        double snapTolerance) {
143
                super(topology, originLyr);
144
                setClusterTolerance(snapTolerance);
145
        }
146
        
147
        public FMapGeometryMustBeClosed(){}
148

    
149
        public void checkPreconditions() throws TopologyRuleDefinitionException {
150
                try {
151
                        if (FGeometryUtil.getDimensions(originLyr.getShapeType()) < 1)
152
                                throw new TopologyRuleDefinitionException(
153
                                                "La capa no es de lineas o poligonos");
154
                } catch (ReadDriverException e) {
155
                        throw new TopologyRuleDefinitionException(
156
                                        "Error de driver al chequear las precondiciones de la regla");
157
                }
158
        }
159

    
160
        public String getName() {
161
                return RULE_NAME;
162
        }
163

    
164
        public void validateFeature(IFeature feature) {
165
                IGeometry geometry = feature.getGeometry();
166
                process(geometry, feature);
167

    
168
        }
169

    
170
        private void process(IGeometry geometry, IFeature feature) {
171
                if (geometry instanceof FMultiPoint2D
172
                                || geometry instanceof FNullGeometry)
173
                        return;
174
                else if (geometry instanceof FGeometry) {
175
                        FGeometry geom = (FGeometry) geometry;
176
                        FShape shp = (FShape) geom.getInternalShape();
177
                        if (shp instanceof FPoint2D || shp instanceof FPoint3D) {
178
                                return;
179
                        } else {
180
                                List<IGeometry> geometries = new ArrayList<IGeometry>();
181
                                MultipartShapeIterator it = new MultipartShapeIterator(shp);
182
                                Iterator<Shape> shapeIt = it.getShapeIterator();
183
                                while (shapeIt.hasNext()) {
184
                                        Shape shape = shapeIt.next();
185
                                        List<Point2D[]> partsCoords = ShapePointExtractor
186
                                                        .extractPoints(shape);
187
                                        for (int i = 0; i < partsCoords.size(); i++) {
188
                                                Point2D[] part = partsCoords.get(i);
189
                                                if (!FGeometryUtil.isClosed(part, clusterTolerance)) {
190
                                                        //createGeometryNotClosedError(part, feature);
191
                                                        IGeometry partError = getGeometryNotClosedError(part);
192
                                                        if(partError != null)
193
                                                                geometries.add(getGeometryNotClosedError(part));
194
                                                }// if isClosed
195
                                        }// for i
196
                                }// while
197
                                
198
                                IGeometry[] geoms = new IGeometry[geometries.size()];
199
                                geometries.toArray(geoms);
200
                                FGeometryCollection errorGeom = new FGeometryCollection(geoms);
201
                                TopologyError topologyError = new TopologyError(errorGeom, this,
202
                                                feature, topology);
203
                                topologyError.setID(errorContainer.getErrorFid());
204
                                addTopologyError(topologyError);
205
                                
206
                        }
207
                } else if (geometry instanceof FGeometryCollection) {
208
                        FGeometryCollection geomCol = (FGeometryCollection) geometry;
209
                        IGeometry[] geometries = geomCol.getGeometries();
210
                        for (int i = 0; i < geometries.length; i++) {
211
                                process(geometries[i], feature);
212
                        }// for
213
                } else {
214
                        System.out.println("Encontrado " + geometry.getClass().toString());
215
                }
216
        }
217
        
218
        
219
        
220
         
221
        private IGeometry getGeometryNotClosedError(Point2D[] unclosedPart){
222
                
223
                List<IGeometry> geometries = new ArrayList<IGeometry>();
224
                // we are going to see if shape is an overshoot or an
225
                // undershoot
226
                Coordinate[] jtsCoords = JtsUtil.getPoint2DAsCoordinates(unclosedPart);
227
                LineString jtsGeo = (LineString) JtsUtil.createGeometry(jtsCoords, "LINESTRING");                
228
                SnapLineStringSelfIntersectionChecker checker = 
229
                        new SnapLineStringSelfIntersectionChecker(jtsGeo, this.clusterTolerance);
230
                if(checker.hasSelfIntersections()){
231
                        //Over shoot
232
                        Geometry[] geoms = checker.clean();
233
                        for(int i = 0; i < geoms.length; i++){
234
                                LineString line = (LineString) geoms[i];
235
                                if(!line.isClosed()){
236
                                        //this rule doesnt checks if a line has self intersections.
237
                                        //thats the reason for we only consideer errors at splitted linestrings
238
                                        //which are unclosed.
239
                                        IGeometry errorGeom = NewFConverter.jts_to_igeometry(line);
240
                                        geometries.add(errorGeom);
241
                                }//if
242
                        }//for
243

    
244
                }else{
245
                        //Under Shoot
246
                        GeneralPathX gp = new GeneralPathX();
247
                        Point2D start = unclosedPart[0];
248
                        Point2D end = unclosedPart[unclosedPart.length - 1];
249
                        gp.moveTo(start.getX(), start.getY());
250
                        gp.lineTo(end.getX(), end.getY());
251
                        IGeometry errorGeom = ShapeFactory.createPolyline2D(gp);
252
                        geometries.add(errorGeom);
253
                }
254
                int size = geometries.size();
255
                if(size == 0)
256
                        return null;
257
                else if(size == 1)
258
                        return geometries.get(0);
259
                else{
260
                        IGeometry[] geoms = new IGeometry[geometries.size()];
261
                        geometries.toArray(geoms);
262
                        IGeometry solution = new FGeometryCollection(geoms);
263
                        return solution;
264
                }
265
                
266
        }
267

    
268
        private void createGeometryNotClosedError(Point2D[] unclosedPart, IFeature feature) {
269

    
270
                // we are going to see if shape is an overshoot or an
271
                // undershoot
272
                Coordinate[] jtsCoords = JtsUtil.getPoint2DAsCoordinates(unclosedPart);
273
                LineString jtsGeo = (LineString) JtsUtil.createGeometry(jtsCoords, "LINESTRING");                
274
                SnapLineStringSelfIntersectionChecker checker = 
275
                        new SnapLineStringSelfIntersectionChecker(jtsGeo, this.clusterTolerance);
276
                if(checker.hasSelfIntersections()){
277
                        //Over shoot
278
                        Geometry[] geoms = checker.clean();
279
                        for(int i = 0; i < geoms.length; i++){
280
                                LineString line = (LineString) geoms[i];
281
                                if(!line.isClosed()){
282
                                        //this rule doesnt checks if a line has self intersections.
283
                                        //thats the reason for we only consideer errors at splitted linestrings
284
                                        //which are unclosed.
285
                                        IGeometry errorGeom = NewFConverter.jts_to_igeometry(line);
286
                                        TopologyError topologyError = new TopologyError(errorGeom, this,
287
                                                        feature, topology);
288
                                        topologyError.setID(errorContainer.getErrorFid());
289
                                        addTopologyError(topologyError);
290
                                }//if
291
                        }//for
292

    
293
                }else{
294
                        //Under Shoot
295
                        GeneralPathX gp = new GeneralPathX();
296
                        Point2D start = unclosedPart[0];
297
                        Point2D end = unclosedPart[unclosedPart.length - 1];
298
                        
299
                        gp.moveTo(start.getX(), start.getY());
300
                        gp.lineTo(end.getX(), end.getY());
301

    
302
                        IGeometry errorGeometry = ShapeFactory.createPolyline2D(gp);
303
                        TopologyError topologyError = new TopologyError(errorGeometry, this,
304
                                        feature, topology);
305
                        topologyError.setID(errorContainer.getErrorFid());
306
                        addTopologyError(topologyError);
307
                }
308
        }
309

    
310
        public double getClusterTolerance() {
311
                return clusterTolerance;
312
        }
313

    
314
        public void setClusterTolerance(double clusterTolerance) {
315
                this.clusterTolerance = clusterTolerance;
316
        }
317

    
318
        public boolean acceptsOriginLyr(FLyrVect lyr) {
319
                try {
320
                        return (FGeometryUtil.getDimensions(lyr.getShapeType()) >= 1);
321
                } catch (ReadDriverException e) {
322
                        e.printStackTrace();
323
                        return false;
324
                }
325
        }
326

    
327
        public List<ITopologyErrorFix> getAutomaticErrorFixes() {
328
                return FMapGeometryMustBeClosed.automaticErrorFixes;
329
        }
330
        
331
        
332

    
333
        public MultiShapeSymbol getDefaultErrorSymbol() {
334
                return DEFAULT_ERROR_SYMBOL;
335
        }
336

    
337
        public MultiShapeSymbol getErrorSymbol() {
338
                return errorSymbol;
339
        }
340

    
341
        public void setErrorSymbol(MultiShapeSymbol errorSymbol) {
342
                this.errorSymbol = errorSymbol;
343
        }
344

    
345
        public List<ITopologyErrorFix> getExclusiveErrorFixes(TopologyError error) {
346
                List<ITopologyErrorFix> solution = new ArrayList<ITopologyErrorFix>();
347
                IGeometry geom = error.getFeature1().getGeometry();
348
                if(geom instanceof FGeometry){
349
                        FShape shp = (FShape) geom.getInternalShape();
350
                        MultipartShapeIterator it = new MultipartShapeIterator(shp);
351
                        Iterator<Shape> shapeIt = it.getShapeIterator();
352
                        while (shapeIt.hasNext()) {
353
                                Shape shape = shapeIt.next();
354
                                List<Point2D[]> partsCoords = ShapePointExtractor
355
                                                .extractPoints(shape);
356
                                for (int i = 0; i < partsCoords.size(); i++) {
357
                                        Point2D[] part = partsCoords.get(i);
358
                                        if (!FGeometryUtil.isClosed(part, clusterTolerance)) {
359
                                                Coordinate[] jtsCoords = JtsUtil.
360
                                                        getPoint2DAsCoordinates(part);
361
                                                LineString jtsGeo = (LineString) JtsUtil.createGeometry(jtsCoords, "LINESTRING");                                                
362
                                                SnapLineStringSelfIntersectionChecker checker = 
363
                                                        new SnapLineStringSelfIntersectionChecker(jtsGeo, this.clusterTolerance);
364
                                                if(checker.hasSelfIntersections()){
365
                                                        //Over shoot
366
                                                        solution.add(new RemoveOvershootFix());
367
                                                }else{
368
                                                        //Under Shoot
369
                                                        solution.add(new CompleteUndershootFix());
370
                                                }
371
                                        }// if isClosed
372
                                }// for i
373
                        }// while
374
                } 
375
                return solution;
376
//                else if (geom instanceof FGeometryCollection) {
377
//                        FGeometryCollection geomCol = (FGeometryCollection) geom;
378
//                        IGeometry[] geometries = geomCol.getGeometries();
379
//                        for (int i = 0; i < geometries.length; i++) {
380
//                                process(geometries[i], feature);
381
//                        }// for
382
//                }
383
        }
384

    
385
}