root / trunk / libraries / libTopology / src / org / gvsig / topology / topologyrules / jtsisvalidrules / PolygonMustNotHaveSelfIntersectedRings.java @ 18253
History | View | Annotate | Download (5.74 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.jts.SnapLineStringSelfIntersectionChecker; |
54 |
import org.gvsig.topology.AbstractTopologyRule; |
55 |
import org.gvsig.topology.Messages; |
56 |
import org.gvsig.topology.Topology; |
57 |
import org.gvsig.topology.TopologyError; |
58 |
import org.gvsig.topology.TopologyRuleDefinitionException; |
59 |
import org.gvsig.topology.WrongLyrForTopologyException; |
60 |
|
61 |
import com.hardcode.gdbms.driver.exceptions.ReadDriverException; |
62 |
import com.iver.cit.gvsig.fmap.core.FMultiPoint2D; |
63 |
import com.iver.cit.gvsig.fmap.core.IFeature; |
64 |
import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
65 |
import com.iver.utiles.XMLEntity; |
66 |
import com.vividsolutions.jts.geom.Coordinate; |
67 |
import com.vividsolutions.jts.geom.Geometry; |
68 |
import com.vividsolutions.jts.geom.GeometryCollection; |
69 |
import com.vividsolutions.jts.geom.LineString; |
70 |
import com.vividsolutions.jts.geom.MultiPolygon; |
71 |
import com.vividsolutions.jts.geom.Polygon; |
72 |
|
73 |
public class PolygonMustNotHaveSelfIntersectedRings extends AbstractTopologyRule{ |
74 |
|
75 |
private double snapTolerance; |
76 |
|
77 |
public PolygonMustNotHaveSelfIntersectedRings(Topology topology, FLyrVect lyr, double snapTolerance){ |
78 |
super(topology, lyr);
|
79 |
this.snapTolerance = snapTolerance;
|
80 |
} |
81 |
|
82 |
public PolygonMustNotHaveSelfIntersectedRings(){}
|
83 |
|
84 |
public PolygonMustNotHaveSelfIntersectedRings(FLyrVect lyr, double snapTolerance){ |
85 |
this(null, lyr, snapTolerance); |
86 |
} |
87 |
|
88 |
public String getName() { |
89 |
return Messages.getText("POLYGON_MUST_NOT_HAVE_INTERSECTED_RINGS"); |
90 |
} |
91 |
|
92 |
public void checkPreconditions() throws TopologyRuleDefinitionException { |
93 |
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 |
} |
104 |
|
105 |
public void validateFeature(IFeature feature) { |
106 |
Geometry jtsGeo = feature.getGeometry().toJTSGeometry(); |
107 |
if (jtsGeo instanceof Polygon) { |
108 |
Polygon polygon = (Polygon) jtsGeo; |
109 |
checkPolygon(polygon, feature); |
110 |
} 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 |
checkPolygon(polygon, feature); |
115 |
} |
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 |
checkPolygon(polygon, feature); |
122 |
} |
123 |
} |
124 |
} |
125 |
|
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 |
} |
135 |
|
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 |
}//for
|
145 |
} |
146 |
|
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 |
TopologyError error = new TopologyError(errorGeo, this, feature, topology ); |
156 |
addTopologyError(error); |
157 |
} |
158 |
|
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 |
} |