root / trunk / libraries / libTopology / src / org / gvsig / topology / topologyrules / LinesMustNotHavePseudonodes.java @ 19651
History | View | Annotate | Download (9.07 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.geom.Rectangle2D; |
53 |
import java.util.ArrayList; |
54 |
|
55 |
import org.gvsig.fmap.core.FGeometryUtil; |
56 |
import org.gvsig.jts.JtsUtil; |
57 |
import org.gvsig.jts.SnappingCoordinateMapWithCounter; |
58 |
import org.gvsig.topology.AbstractTopologyRule; |
59 |
import org.gvsig.topology.IRuleWithClusterTolerance; |
60 |
import org.gvsig.topology.Messages; |
61 |
import org.gvsig.topology.Topology; |
62 |
import org.gvsig.topology.TopologyError; |
63 |
import org.gvsig.topology.TopologyRuleDefinitionException; |
64 |
|
65 |
import com.hardcode.gdbms.driver.exceptions.ReadDriverException; |
66 |
import com.iver.cit.gvsig.fmap.core.FShape; |
67 |
import com.iver.cit.gvsig.fmap.core.IFeature; |
68 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
69 |
import com.iver.cit.gvsig.fmap.core.ShapeFactory; |
70 |
import com.iver.cit.gvsig.fmap.drivers.IFeatureIterator; |
71 |
import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
72 |
import com.iver.cit.gvsig.util.SnappingCoordinateMap; |
73 |
import com.vividsolutions.jts.geom.Coordinate; |
74 |
import com.vividsolutions.jts.geom.Envelope; |
75 |
import com.vividsolutions.jts.geom.Geometry; |
76 |
import com.vividsolutions.jts.geom.GeometryCollection; |
77 |
import com.vividsolutions.jts.geom.LineString; |
78 |
import com.vividsolutions.jts.geom.MultiLineString; |
79 |
|
80 |
/**
|
81 |
* This rule checks that lines of a line layer dont have pseudonodes ends (a
|
82 |
* point that touchs one only another line). <br>
|
83 |
* If a dangling node is an end point of a line that doesnt touch another line,
|
84 |
* a pseudonode is a point that only touch one line. (we could dissolve these
|
85 |
* two lines).
|
86 |
*/
|
87 |
public class LinesMustNotHavePseudonodes extends AbstractTopologyRule implements |
88 |
IRuleWithClusterTolerance { |
89 |
|
90 |
String ruleName = Messages.getText("must_not_have_pseudonodes"); |
91 |
|
92 |
double clusterTol;
|
93 |
|
94 |
protected SnappingCoordinateMap pseudoNodesCoordMap = null; |
95 |
|
96 |
public LinesMustNotHavePseudonodes(Topology topology, FLyrVect originLyr,
|
97 |
double clusterTolerance) {
|
98 |
super(topology, originLyr);
|
99 |
setClusterTolerance(clusterTolerance); |
100 |
pseudoNodesCoordMap = new SnappingCoordinateMap(getClusterTolerance());
|
101 |
|
102 |
} |
103 |
|
104 |
public LinesMustNotHavePseudonodes() {
|
105 |
} |
106 |
|
107 |
public String getName() { |
108 |
return ruleName;
|
109 |
} |
110 |
|
111 |
public void checkPreconditions() throws TopologyRuleDefinitionException { |
112 |
try {
|
113 |
int shapeType = this.originLyr.getShapeType(); |
114 |
if (FGeometryUtil.getDimensions(shapeType) != 1) |
115 |
throw new TopologyRuleDefinitionException( |
116 |
"LineMustNotHavePseudonodes requires a lineal geometry type");
|
117 |
} catch (ReadDriverException e) {
|
118 |
e.printStackTrace(); |
119 |
throw new TopologyRuleDefinitionException( |
120 |
"Error leyendo el tipo de geometria del driver", e);
|
121 |
} |
122 |
} |
123 |
|
124 |
public void validateFeature(IFeature feature) { |
125 |
IGeometry geom = feature.getGeometry(); |
126 |
int shapeType = geom.getGeometryType();
|
127 |
if (shapeType != FShape.LINE && shapeType != FShape.ARC
|
128 |
&& shapeType != FShape.LINE + FShape.Z) |
129 |
return;
|
130 |
|
131 |
Geometry jtsGeom = geom.toJTSGeometry(); |
132 |
|
133 |
process(jtsGeom, feature); |
134 |
} |
135 |
|
136 |
protected void process(Geometry geometry, IFeature feature) { |
137 |
if (geometry instanceof GeometryCollection) { |
138 |
GeometryCollection geomCol = (GeometryCollection) geometry; |
139 |
for (int i = 0; i < geomCol.getNumGeometries(); i++) { |
140 |
Geometry geomI = geomCol.getGeometryN(i); |
141 |
process(geomI, feature); |
142 |
} |
143 |
} else if (geometry instanceof LineString) { |
144 |
LineString lineString = (LineString) geometry; |
145 |
Envelope lnEnv = lineString.getEnvelopeInternal(); |
146 |
|
147 |
//We try to extend a bit the original envelope to ensure
|
148 |
//recovering geometries in the limit
|
149 |
double minX = lnEnv.getMinX() - 10; |
150 |
double minY = lnEnv.getMinY() - 10; |
151 |
double maxX = lnEnv.getMaxX() + 10; |
152 |
double maxY = lnEnv.getMaxY() + 10; |
153 |
|
154 |
Rectangle2D rect = new Rectangle2D.Double(minX, minY, maxX - minX, maxY - minY); |
155 |
|
156 |
SnappingCoordinateMapWithCounter coordinateMap = new SnappingCoordinateMapWithCounter(
|
157 |
getClusterTolerance()); |
158 |
|
159 |
// we consideer a pseudonode a coordinate with degree 2 (it only
|
160 |
// connects two geometries)
|
161 |
Coordinate firstPoint = lineString.getCoordinateN(0);
|
162 |
coordinateMap.put(firstPoint, firstPoint); |
163 |
|
164 |
Coordinate lastPoint = lineString.getCoordinateN(lineString |
165 |
.getNumPoints() - 1);
|
166 |
coordinateMap.put(lastPoint, lastPoint); |
167 |
|
168 |
try {
|
169 |
IFeatureIterator neighbours = originLyr.getSource() |
170 |
.getFeatureIterator(rect, null, null, false); |
171 |
while (neighbours.hasNext()) {
|
172 |
IFeature neighbourFeature = neighbours.next(); |
173 |
if (neighbourFeature.getID().equalsIgnoreCase(
|
174 |
feature.getID())) |
175 |
continue;
|
176 |
Geometry geom2 = neighbourFeature.getGeometry() |
177 |
.toJTSGeometry(); |
178 |
ArrayList<LineString> geometriesToProcess = new ArrayList<LineString>(); |
179 |
if (geom2 instanceof LineString) { |
180 |
geometriesToProcess.add((LineString) geom2); |
181 |
} else if (geom2 instanceof MultiLineString) { |
182 |
MultiLineString multiLine = (MultiLineString) geom2; |
183 |
int numLines = multiLine.getNumGeometries();
|
184 |
for (int i = 0; i < numLines; i++) { |
185 |
LineString line = (LineString) multiLine |
186 |
.getGeometryN(i); |
187 |
geometriesToProcess.add(line); |
188 |
} |
189 |
} else if (geom2 instanceof GeometryCollection) { |
190 |
MultiLineString multiLine = JtsUtil |
191 |
.convertToMultiLineString((GeometryCollection) geom2); |
192 |
int numLines = multiLine.getNumGeometries();
|
193 |
for (int i = 0; i < numLines; i++) { |
194 |
LineString line = (LineString) multiLine |
195 |
.getGeometryN(i); |
196 |
geometriesToProcess.add(line); |
197 |
} |
198 |
} else {
|
199 |
System.out.println("Encontrado:" + geom2.toString() |
200 |
+ " en regla de dangles");
|
201 |
} |
202 |
|
203 |
int numGeometries = geometriesToProcess.size();
|
204 |
for (int i = 0; i < numGeometries; i++) { |
205 |
LineString lineString2 = geometriesToProcess.get(i); |
206 |
Coordinate firstPoint2 = lineString2.getCoordinateN(0);
|
207 |
Coordinate lastPoint2 = lineString2.getCoordinateN(lineString |
208 |
.getNumPoints() - 1);
|
209 |
|
210 |
coordinateMap.put(lastPoint2, lastPoint); |
211 |
coordinateMap.put(firstPoint2, firstPoint); |
212 |
|
213 |
}//for
|
214 |
|
215 |
}//while
|
216 |
|
217 |
int firstPointDegree = coordinateMap.getCount(firstPoint);
|
218 |
int lastPointDegree = coordinateMap.getCount(lastPoint);
|
219 |
|
220 |
if(firstPointDegree == 2){//A pseudonode is a node with degree 2, it only connects two lines |
221 |
|
222 |
//we dont add two times the same error
|
223 |
Coordinate existingNode = (Coordinate) pseudoNodesCoordMap.get(firstPoint); |
224 |
if(existingNode == null){ |
225 |
IGeometry errorGeom = |
226 |
ShapeFactory.createPoint2D(firstPoint.x, |
227 |
firstPoint.y); |
228 |
TopologyError topologyError = |
229 |
new TopologyError(errorGeom,
|
230 |
this,
|
231 |
feature, |
232 |
topology); |
233 |
topologyError.setID(errorContainer.getErrorFid()); |
234 |
addTopologyError(topologyError); |
235 |
|
236 |
pseudoNodesCoordMap.put(firstPoint, firstPoint); |
237 |
} |
238 |
} |
239 |
|
240 |
if(lastPointDegree == 2){ |
241 |
Coordinate existingNode = (Coordinate) pseudoNodesCoordMap.get(lastPoint); |
242 |
if(existingNode == null){ |
243 |
IGeometry errorGeom = |
244 |
ShapeFactory.createPoint2D(lastPoint.x, |
245 |
lastPoint.y); |
246 |
TopologyError topologyError = |
247 |
new TopologyError(errorGeom,
|
248 |
this,
|
249 |
feature, |
250 |
topology); |
251 |
topologyError.setID(errorContainer.getErrorFid()); |
252 |
addTopologyError(topologyError); |
253 |
|
254 |
pseudoNodesCoordMap.put(lastPoint, lastPoint); |
255 |
} |
256 |
} |
257 |
|
258 |
} catch (ReadDriverException e) {
|
259 |
e.printStackTrace(); |
260 |
return;
|
261 |
} |
262 |
} else {
|
263 |
System.out.println("Encontrado:" + geometry.toString() |
264 |
+ " en regla de dangles");
|
265 |
} |
266 |
} |
267 |
|
268 |
public double getClusterTolerance() { |
269 |
return clusterTol;
|
270 |
} |
271 |
|
272 |
public void setClusterTolerance(double clusterTolerance) { |
273 |
this.clusterTol = clusterTolerance;
|
274 |
} |
275 |
|
276 |
public boolean acceptsOriginLyr(FLyrVect lyr) { |
277 |
try {
|
278 |
int shapeType = lyr.getShapeType();
|
279 |
return (FGeometryUtil.getDimensions(shapeType) == 1); |
280 |
} catch (ReadDriverException e) {
|
281 |
e.printStackTrace(); |
282 |
return false; |
283 |
} |
284 |
} |
285 |
|
286 |
} |