Statistics
| Revision:

root / trunk / libraries / libTopology / src / org / gvsig / topology / topologyrules / jtsisvalidrules / PolygonMustNotHaveSelfIntersectedRings.java @ 18253

History | View | Annotate | Download (5.74 KB)

1 14442 azabala
/*
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.jtsisvalidrules;
50
51 16256 azabala
import org.gvsig.fmap.core.FGeometryUtil;
52 14442 azabala
import org.gvsig.jts.JtsUtil;
53 14567 azabala
import org.gvsig.jts.SnapLineStringSelfIntersectionChecker;
54 14442 azabala
import org.gvsig.topology.AbstractTopologyRule;
55
import org.gvsig.topology.Messages;
56 16256 azabala
import org.gvsig.topology.Topology;
57 14442 azabala
import org.gvsig.topology.TopologyError;
58
import org.gvsig.topology.TopologyRuleDefinitionException;
59 16256 azabala
import org.gvsig.topology.WrongLyrForTopologyException;
60 14442 azabala
61 16256 azabala
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
62 14567 azabala
import com.iver.cit.gvsig.fmap.core.FMultiPoint2D;
63 14442 azabala
import com.iver.cit.gvsig.fmap.core.IFeature;
64 14567 azabala
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
65 16256 azabala
import com.iver.utiles.XMLEntity;
66 14567 azabala
import com.vividsolutions.jts.geom.Coordinate;
67 14442 azabala
import com.vividsolutions.jts.geom.Geometry;
68
import com.vividsolutions.jts.geom.GeometryCollection;
69 14567 azabala
import com.vividsolutions.jts.geom.LineString;
70 14442 azabala
import com.vividsolutions.jts.geom.MultiPolygon;
71
import com.vividsolutions.jts.geom.Polygon;
72
73
public class PolygonMustNotHaveSelfIntersectedRings extends AbstractTopologyRule{
74 14567 azabala
75
        private double snapTolerance;
76
77 16256 azabala
        public PolygonMustNotHaveSelfIntersectedRings(Topology topology, FLyrVect lyr, double snapTolerance){
78
                super(topology, lyr);
79 14567 azabala
                this.snapTolerance = snapTolerance;
80
        }
81
82 16396 azabala
        public PolygonMustNotHaveSelfIntersectedRings(){}
83
84 16256 azabala
        public PolygonMustNotHaveSelfIntersectedRings(FLyrVect lyr, double snapTolerance){
85
                this(null, lyr, snapTolerance);
86
        }
87
88 18253 azabala
        public String getName() {
89 14442 azabala
                return Messages.getText("POLYGON_MUST_NOT_HAVE_INTERSECTED_RINGS");
90
        }
91
92
        public void checkPreconditions() throws TopologyRuleDefinitionException {
93 16256 azabala
                int shapeType;
94
                try {
95
                        shapeType = this.originLyr.getShapeType();
96
                        int numDimensions = FGeometryUtil.getDimensions(shapeType);
97
                        if(numDimensions != 2)
98
                                throw new WrongLyrForTopologyException("MustNotHaveSelfIntersectedRings solo aplica sobre capas de dimension 2");
99
                } catch (ReadDriverException e) {
100
                        throw new TopologyRuleDefinitionException(
101
                                        "Error al tratar de verificar el tipo de geometria");
102
                }
103 14442 azabala
        }
104
105
        public void validateFeature(IFeature feature) {
106
                Geometry jtsGeo = feature.getGeometry().toJTSGeometry();
107
                if (jtsGeo instanceof Polygon) {
108
                        Polygon polygon = (Polygon) jtsGeo;
109 14567 azabala
                        checkPolygon(polygon, feature);
110 14442 azabala
                } else if (jtsGeo instanceof MultiPolygon) {
111
                        MultiPolygon multiPoly = (MultiPolygon) jtsGeo;
112
                        for (int i = 0; i < multiPoly.getNumGeometries(); i++) {
113
                                Polygon polygon = (Polygon) multiPoly.getGeometryN(i);
114 14567 azabala
                                checkPolygon(polygon, feature);
115 14442 azabala
                        }
116
                } else if (jtsGeo instanceof GeometryCollection) {
117
                        MultiPolygon multiPoly = JtsUtil
118
                                        .convertIfPossible((GeometryCollection) jtsGeo);
119
                        for (int i = 0; i < multiPoly.getNumGeometries(); i++) {
120
                                Polygon polygon = (Polygon) multiPoly.getGeometryN(i);
121 14567 azabala
                                checkPolygon(polygon, feature);
122 14442 azabala
                        }
123
                }
124
        }
125 14567 azabala
126
127
        private  void checkPolygon(Polygon polygon, IFeature feature){
128
                LineString shell = polygon.getExteriorRing();
129
                SnapLineStringSelfIntersectionChecker checker =
130
                        new SnapLineStringSelfIntersectionChecker(shell, snapTolerance);
131
                if(checker.hasSelfIntersections()){
132
                        Coordinate[] selfIntersections = checker.getSelfIntersections();
133
                        addError(selfIntersections, feature);
134 14442 azabala
                }
135 14567 azabala
136
                int numHoles = polygon.getNumInteriorRing();
137
                for(int i = 0; i < numHoles; i++){
138
                        LineString hole = polygon.getInteriorRingN(i);
139
                        checker = new SnapLineStringSelfIntersectionChecker(hole, snapTolerance);
140
                        if(checker.hasSelfIntersections()){
141
                                Coordinate[] selfIntersections = checker.getSelfIntersections();
142
                                addError(selfIntersections, feature);
143
                        }//if
144 14442 azabala
                }//for
145
        }
146 14567 azabala
147
        private void addError(Coordinate[] selfIntersections, IFeature feature){
148
                double[] x = new double[selfIntersections.length];
149
                double[] y = new double[selfIntersections.length];
150
                for(int i = 0; i < selfIntersections.length; i++){
151
                        x[i] = selfIntersections[i].x;
152
                        y[i] = selfIntersections[i].y;
153
                }
154
                FMultiPoint2D errorGeo = new FMultiPoint2D(x, y);
155 18065 azabala
                TopologyError error = new TopologyError(errorGeo, this, feature, topology );
156 14567 azabala
                addTopologyError(error);
157
        }
158 16256 azabala
159
        public XMLEntity getXMLEntity(){
160
                XMLEntity xml = super.getXMLEntity();
161
                xml.putProperty("snapTolerance", snapTolerance);
162
                return xml;
163
        }
164
165
        public void setXMLEntity(XMLEntity xml){
166
                super.setXMLEntity(xml);
167
168
                if(xml.contains("snapTolerance")){
169
                        snapTolerance = xml.getDoubleProperty("snapTolerance");
170
                }
171
        }
172 14442 azabala
}