Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libTopology / src / org / gvsig / topology / topologyrules / jtsisvalidrules / PolygonMustNotHaveDuplicatedRings.java @ 16256

History | View | Annotate | Download (5.35 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.jtsisvalidrules;
50

    
51
import org.gvsig.fmap.core.FGeometryUtil;
52
import org.gvsig.jts.JtsUtil;
53
import org.gvsig.topology.AbstractTopologyRule;
54
import org.gvsig.topology.Messages;
55
import org.gvsig.topology.Topology;
56
import org.gvsig.topology.TopologyError;
57
import org.gvsig.topology.TopologyRuleDefinitionException;
58

    
59
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
60
import com.iver.cit.gvsig.fmap.core.IFeature;
61
import com.iver.cit.gvsig.fmap.core.IGeometry;
62
import com.iver.cit.gvsig.fmap.core.v02.FConverter;
63
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
64
import com.iver.utiles.XMLEntity;
65
import com.vividsolutions.jts.geom.Geometry;
66
import com.vividsolutions.jts.geom.GeometryCollection;
67
import com.vividsolutions.jts.geom.LineString;
68
import com.vividsolutions.jts.geom.MultiPolygon;
69
import com.vividsolutions.jts.geom.Polygon;
70

    
71
/**
72
 * This rule checks it two rings in a polygon of a polygon layer
73
 * are equivalent.
74
 * @author Alvaro Zabala
75
 *
76
 */
77
public class PolygonMustNotHaveDuplicatedRings extends AbstractTopologyRule {
78
        
79
        private double snapTolerance;
80

    
81
        public PolygonMustNotHaveDuplicatedRings(Topology topology, FLyrVect lyr, double tolerance){
82
                super(topology, lyr);
83
                this.snapTolerance = tolerance;
84
        }
85
        
86
        public PolygonMustNotHaveDuplicatedRings(FLyrVect lyr, double tolerance){
87
                this(null, lyr, tolerance);
88
        }
89
        
90
        public String getName() {
91
                return Messages.getText("POLYGON_MUST_NOT_HAVE_DUPLICATED_RINGS");
92
        }
93

    
94
        public void checkPreconditions() throws TopologyRuleDefinitionException {
95
                int shapeType;
96
                try {
97
                        shapeType = this.originLyr.getShapeType();
98
                        int numDimensions = FGeometryUtil.getDimensions(shapeType);
99
                        if(numDimensions != 2)
100
                                throw new TopologyRuleDefinitionException("MustNotHaveDuplicatedRings solo aplica sobre capas de dimension 2");
101
                } catch (ReadDriverException e) {
102
                        throw new TopologyRuleDefinitionException(
103
                                        "Error al tratar de verificar el tipo de geometria");
104
                }
105
        }
106

    
107
        public void validateFeature(IFeature feature) {
108
                Geometry jtsGeo = feature.getGeometry().toJTSGeometry();
109
                if (jtsGeo instanceof Polygon) {
110
                        Polygon polygon = (Polygon) jtsGeo;
111
                        checkRings(polygon, feature);
112
                } else if (jtsGeo instanceof MultiPolygon) {
113
                        MultiPolygon multiPoly = (MultiPolygon)jtsGeo;
114
                        for(int i = 0; i < multiPoly.getNumGeometries(); i++){
115
                                Polygon polygon = (Polygon) multiPoly.getGeometryN(i);
116
                                checkRings(polygon, feature);
117
                        }
118
                }else if(jtsGeo instanceof GeometryCollection){
119
                        MultiPolygon multiPoly = JtsUtil.convertIfPossible((GeometryCollection) jtsGeo);
120
                        for(int i = 0; i < multiPoly.getNumGeometries(); i++){
121
                                Polygon polygon = (Polygon) multiPoly.getGeometryN(i);
122
                                checkRings(polygon, feature);
123
                        }
124
                }
125
        }
126
        
127
        private void checkRings(Polygon polygon, IFeature feature){
128
                LineString shell = polygon.getExteriorRing();
129
                int numHoles = polygon.getNumInteriorRing();
130
                for(int i = 0; i < numHoles; i++){
131
                        LineString hole = polygon.getInteriorRingN(i);
132
                        if(shell.equalsExact(hole, snapTolerance)){
133
                                IFeature[] features = {feature};
134
                                IGeometry geometry = FConverter.jts_to_igeometry(hole);
135
                                TopologyError error = new TopologyError(geometry,
136
                                                                                                                this, 
137
                                                                                                                features);
138
                                addTopologyError(error);
139
                        }//if
140
                        
141
                        
142
                        for(int j = 0; j < numHoles; j++){
143
                                if(i >= j)
144
                                        continue;
145
                                LineString hole2 = polygon.getInteriorRingN(j);
146
                                if(hole.equalsExact(hole2)){
147
                                        IFeature[] features = {feature};
148
                                        IGeometry geometry = FConverter.jts_to_igeometry(hole);
149
                                        TopologyError error = new TopologyError(geometry,
150
                                                                                                                        this, 
151
                                                                                                                        features);
152
                                        addTopologyError(error);
153
                                }
154
                        }
155
                }//for
156
        }
157
        
158
        public XMLEntity getXMLEntity(){
159
                XMLEntity xml = super.getXMLEntity();
160
                xml.putProperty("snapTolerance", snapTolerance);
161
                return xml;
162
        }
163
            
164
        public void setXMLEntity(XMLEntity xml){
165
                super.setXMLEntity(xml);
166
                
167
                if(xml.contains("snapTolerance")){
168
                        snapTolerance = xml.getDoubleProperty("snapTolerance");
169
                }
170
        }
171
}