Statistics
| Revision:

root / trunk / libraries / libTopology / src / org / gvsig / topology / topologyrules / PolygonMustNotSelfIntersect.java @ 24085

History | View | Annotate | Download (7.37 KB)

1
/*
2
 * Created on 07-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.topologyrules;
51

    
52
import java.awt.Color;
53
import java.util.ArrayList;
54
import java.util.Arrays;
55
import java.util.Iterator;
56
import java.util.List;
57

    
58
import org.gvsig.fmap.core.FGeometryUtil;
59
import org.gvsig.fmap.core.NewFConverter;
60
import org.gvsig.jts.JtsUtil;
61
import org.gvsig.jts.SnapLineStringSelfIntersectionChecker;
62
import org.gvsig.topology.AbstractTopologyRule;
63
import org.gvsig.topology.IRuleWithClusterTolerance;
64
import org.gvsig.topology.ITopologyErrorFix;
65
import org.gvsig.topology.Messages;
66
import org.gvsig.topology.Topology;
67
import org.gvsig.topology.TopologyError;
68
import org.gvsig.topology.TopologyRuleDefinitionException;
69
import org.gvsig.topology.errorfixes.SplitSelfIntersectingPolygonFix;
70

    
71
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
72
import com.iver.cit.gvsig.fmap.core.FMultiPoint2D;
73
import com.iver.cit.gvsig.fmap.core.FShape;
74
import com.iver.cit.gvsig.fmap.core.IFeature;
75
import com.iver.cit.gvsig.fmap.core.IGeometry;
76
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
77
import com.iver.cit.gvsig.fmap.core.symbols.MultiShapeSymbol;
78
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
79
import com.vividsolutions.jts.geom.Coordinate;
80
import com.vividsolutions.jts.geom.Geometry;
81
import com.vividsolutions.jts.geom.GeometryCollection;
82
import com.vividsolutions.jts.geom.LineString;
83
import com.vividsolutions.jts.geom.MultiPolygon;
84
import com.vividsolutions.jts.geom.Polygon;
85

    
86
/**
87
 * The polygons of a layer must not have self intersections
88
 * 
89
 */
90
public class PolygonMustNotSelfIntersect extends AbstractTopologyRule implements
91
                IRuleWithClusterTolerance {
92

    
93
        static final String RULE_NAME = Messages
94
                        .getText("polygon_must_not_self_intersect");
95

    
96
        private static List<ITopologyErrorFix> automaticErrorFixes =
97
                new ArrayList<ITopologyErrorFix>();
98
        static{
99
                automaticErrorFixes.add(new SplitSelfIntersectingPolygonFix());
100
        }
101
        
102
        private static final Color DEFAULT_ERROR_COLOR = new Color(100, 0, 84);
103
        
104
        
105
        private static final MultiShapeSymbol DEFAULT_ERROR_SYMBOL = 
106
                (MultiShapeSymbol) SymbologyFactory.createDefaultSymbolByShapeType(FShape.MULTI, 
107
                                                                                        DEFAULT_ERROR_COLOR);
108
        static{
109
                DEFAULT_ERROR_SYMBOL.setDescription(RULE_NAME);
110
                DEFAULT_ERROR_SYMBOL.setSize(5);
111
        }
112
        
113
        private MultiShapeSymbol errorSymbol = DEFAULT_ERROR_SYMBOL;
114
        
115
        private double clusterTolerance;
116

    
117
        
118
        public PolygonMustNotSelfIntersect(Topology topology, FLyrVect originLyr,
119
                        double clusterTolerance) {
120
                super(topology, originLyr);
121
                this.clusterTolerance = clusterTolerance;
122
        }
123

    
124
        public PolygonMustNotSelfIntersect() {
125
        }
126

    
127
        public void checkPreconditions() throws TopologyRuleDefinitionException {
128
                try {
129
                        if (FGeometryUtil.getDimensions(originLyr.getShapeType()) != 2)
130
                                throw new TopologyRuleDefinitionException(
131
                                                "La capa no es de poligonos o multigeometrias");
132
                } catch (ReadDriverException e) {
133
                        throw new TopologyRuleDefinitionException(
134
                                        "Error de driver al chequear las precondiciones de la regla");
135
                }
136
        }
137

    
138
        public String getName() {
139
                return RULE_NAME;
140
        }
141

    
142
        public void validateFeature(IFeature feature) {
143
                IGeometry geom = feature.getGeometry();
144
                int shapeType = geom.getGeometryType();
145
                if(FGeometryUtil.getDimensions(shapeType) != 2)
146
                        return;
147
                Geometry jtsGeom = NewFConverter.toJtsGeometry(geom);
148
                process(jtsGeom, feature);
149
        }
150

    
151
        protected void process(Geometry jtsGeom, IFeature feature) {
152
                if (jtsGeom instanceof MultiPolygon) {
153
                        MultiPolygon multi = (MultiPolygon) jtsGeom;
154
                        int numPol = multi.getNumGeometries();
155
                        for (int i = 0; i < numPol; i++) {
156
                                Polygon pol = (Polygon) multi.getGeometryN(i);
157
                                process(pol, feature);
158
                        }
159
                } else if (jtsGeom instanceof GeometryCollection) {
160
                        MultiPolygon multi = JtsUtil
161
                                        .convertToMultiPolygon((GeometryCollection) jtsGeom);
162
                        process(multi, feature);
163

    
164
                } else if (jtsGeom instanceof Polygon) {
165
                        Polygon pol = (Polygon) jtsGeom;
166
                        List<Coordinate> selfIntersections = new ArrayList<Coordinate>();
167
                        LineString shell = pol.getExteriorRing();
168
                        SnapLineStringSelfIntersectionChecker checker = new SnapLineStringSelfIntersectionChecker(
169
                                        shell, getClusterTolerance());
170
                        if (checker.hasSelfIntersections()) {
171
                                Coordinate[] selfIntersection = checker.getSelfIntersections();
172
                                selfIntersections.addAll(Arrays.asList(selfIntersection));
173
                        }
174

    
175
                        int numHoles = pol.getNumInteriorRing();
176
                        for (int i = 0; i < numHoles; i++) {
177
                                LineString hole = pol.getInteriorRingN(i);
178
                                checker = new SnapLineStringSelfIntersectionChecker(hole,
179
                                                getClusterTolerance());
180
                                if (checker.hasSelfIntersections()) {
181
                                        Coordinate[] selfIntersection = checker
182
                                                        .getSelfIntersections();
183
                                        selfIntersections.addAll(Arrays.asList(selfIntersection));
184
                                }// if
185
                        }// for
186

    
187
                        if (selfIntersections.size() > 0) {
188
                                Iterator<Coordinate> coordsIt = selfIntersections.iterator();
189
                                int count = 0;
190
                                int numberOfSelfCoords = selfIntersections.size();
191
                                double[] x = new double[numberOfSelfCoords];
192
                                double[] y = new double[numberOfSelfCoords];
193
                                while (coordsIt.hasNext()) {
194
                                        Coordinate coord = (Coordinate) coordsIt.next();
195
                                        x[count] = coord.x;
196
                                        y[count] = coord.y;
197
                                        count++;
198
                                }
199

    
200
                                FMultiPoint2D multiPoint = new FMultiPoint2D(x, y);
201
                                TopologyError topologyError = new TopologyError(multiPoint,
202
                                                                                                        this, feature, topology);
203
                                topologyError.setID(errorContainer.getErrorFid());
204
                                addTopologyError(topologyError);
205
                        }//if
206
                }// if polygon
207
        }
208

    
209
        public double getClusterTolerance() {
210
                return clusterTolerance;
211
        }
212

    
213
        public void setClusterTolerance(double clusterTolerance) {
214
                this.clusterTolerance = clusterTolerance;
215
        }
216

    
217
        public boolean acceptsOriginLyr(FLyrVect lyr) {
218
                try {
219
                        return FGeometryUtil.getDimensions(lyr.getShapeType()) == 2;
220
                } catch (ReadDriverException e) {
221
                        return false;
222
                }
223
        }
224
        
225
        public List<ITopologyErrorFix> getAutomaticErrorFixes() {
226
                return automaticErrorFixes;
227
        }
228

    
229
        public MultiShapeSymbol getDefaultErrorSymbol() {
230
                return DEFAULT_ERROR_SYMBOL;
231
        }
232

    
233
        public MultiShapeSymbol getErrorSymbol() {
234
                return errorSymbol;
235
        }
236
        
237
        public void setErrorSymbol(MultiShapeSymbol errorSymbol) {
238
                this.errorSymbol = errorSymbol;
239
        }
240
        
241
        public ITopologyErrorFix getDefaultFix(){
242
                return automaticErrorFixes.get(0);
243
        }
244
}