Revision 23038
trunk/libraries/libTopology/src/org/gvsig/topology/topologyrules/LinesMustNotHavePseudonodes.java | ||
---|---|---|
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.awt.geom.Rectangle2D; |
|
54 |
import java.util.ArrayList; |
|
55 |
import java.util.List; |
|
56 |
|
|
57 |
import org.gvsig.fmap.core.FGeometryUtil; |
|
58 |
import org.gvsig.jts.JtsUtil; |
|
59 |
import org.gvsig.jts.SnappingCoordinateMapWithCounter; |
|
60 |
import org.gvsig.topology.AbstractTopologyRule; |
|
61 |
import org.gvsig.topology.IRuleWithClusterTolerance; |
|
62 |
import org.gvsig.topology.ITopologyErrorFix; |
|
63 |
import org.gvsig.topology.Messages; |
|
64 |
import org.gvsig.topology.Topology; |
|
65 |
import org.gvsig.topology.TopologyError; |
|
66 |
import org.gvsig.topology.TopologyRuleDefinitionException; |
|
67 |
import org.gvsig.topology.errorfixes.RemovePseudoNodeFix; |
|
68 |
|
|
69 |
import com.hardcode.gdbms.driver.exceptions.ReadDriverException; |
|
70 |
import com.iver.cit.gvsig.fmap.core.FShape; |
|
71 |
import com.iver.cit.gvsig.fmap.core.IFeature; |
|
72 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
|
73 |
import com.iver.cit.gvsig.fmap.core.ShapeFactory; |
|
74 |
import com.iver.cit.gvsig.fmap.core.SymbologyFactory; |
|
75 |
import com.iver.cit.gvsig.fmap.core.symbols.IMarkerSymbol; |
|
76 |
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol; |
|
77 |
import com.iver.cit.gvsig.fmap.drivers.IFeatureIterator; |
|
78 |
import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
|
79 |
import com.iver.cit.gvsig.util.SnappingCoordinateMap; |
|
80 |
import com.vividsolutions.jts.geom.Coordinate; |
|
81 |
import com.vividsolutions.jts.geom.Envelope; |
|
82 |
import com.vividsolutions.jts.geom.Geometry; |
|
83 |
import com.vividsolutions.jts.geom.GeometryCollection; |
|
84 |
import com.vividsolutions.jts.geom.LineString; |
|
85 |
import com.vividsolutions.jts.geom.MultiLineString; |
|
86 |
|
|
87 |
/** |
|
88 |
* This rule checks that lines of a line layer dont have pseudonodes ends (a |
|
89 |
* point that touchs one only another line). <br> |
|
90 |
* If a dangling node is an end point of a line that doesnt touch another line, |
|
91 |
* a pseudonode is a point that only touch one line. (we could dissolve these |
|
92 |
* two lines). |
|
93 |
*/ |
|
94 |
public class LinesMustNotHavePseudonodes extends AbstractTopologyRule implements |
|
95 |
IRuleWithClusterTolerance { |
|
96 |
|
|
97 |
final static String RULE_NAME = Messages.getText("must_not_have_pseudonodes"); |
|
98 |
|
|
99 |
double clusterTol; |
|
100 |
|
|
101 |
protected SnappingCoordinateMap pseudoNodesCoordMap = null; |
|
102 |
|
|
103 |
/** |
|
104 |
* Symbol for topology errors caused by a violation of this rule. |
|
105 |
*/ |
|
106 |
protected ISymbol errorSymbol = DEFAULT_ERROR_SYMBOL; |
|
107 |
|
|
108 |
private static List<ITopologyErrorFix> automaticErrorFixes = |
|
109 |
new ArrayList<ITopologyErrorFix>(); |
|
110 |
static{ |
|
111 |
automaticErrorFixes.add(new RemovePseudoNodeFix()); |
|
112 |
|
|
113 |
} |
|
114 |
|
|
115 |
protected static final Color DEFAULT_ERROR_COLOR = Color.PINK; |
|
116 |
|
|
117 |
|
|
118 |
protected static final IMarkerSymbol DEFAULT_ERROR_SYMBOL = |
|
119 |
(IMarkerSymbol) SymbologyFactory.createDefaultSymbolByShapeType(FShape.POINT, |
|
120 |
DEFAULT_ERROR_COLOR); |
|
121 |
static{ |
|
122 |
DEFAULT_ERROR_SYMBOL.setDescription(RULE_NAME); |
|
123 |
DEFAULT_ERROR_SYMBOL.setSize(3); |
|
124 |
} |
|
125 |
|
|
126 |
|
|
127 |
public LinesMustNotHavePseudonodes(Topology topology, FLyrVect originLyr, |
|
128 |
double clusterTolerance) { |
|
129 |
super(topology, originLyr); |
|
130 |
setClusterTolerance(clusterTolerance); |
|
131 |
|
|
132 |
|
|
133 |
} |
|
134 |
|
|
135 |
public LinesMustNotHavePseudonodes() { |
|
136 |
} |
|
137 |
|
|
138 |
public String getName() { |
|
139 |
return RULE_NAME; |
|
140 |
} |
|
141 |
|
|
142 |
public void checkPreconditions() throws TopologyRuleDefinitionException { |
|
143 |
try { |
|
144 |
int shapeType = this.originLyr.getShapeType(); |
|
145 |
if (FGeometryUtil.getDimensions(shapeType) != 1) |
|
146 |
throw new TopologyRuleDefinitionException( |
|
147 |
"LineMustNotHavePseudonodes requires a lineal geometry type"); |
|
148 |
} catch (ReadDriverException e) { |
|
149 |
e.printStackTrace(); |
|
150 |
throw new TopologyRuleDefinitionException( |
|
151 |
"Error leyendo el tipo de geometria del driver", e); |
|
152 |
} |
|
153 |
} |
|
154 |
|
|
155 |
public void ruleChecked(){ |
|
156 |
super.ruleChecked(); |
|
157 |
|
|
158 |
this.pseudoNodesCoordMap = new SnappingCoordinateMap(this.clusterTol); |
|
159 |
} |
|
160 |
|
|
161 |
public void validateFeature(IFeature feature) { |
|
162 |
IGeometry geom = feature.getGeometry(); |
|
163 |
int shapeType = geom.getGeometryType(); |
|
164 |
if (shapeType != FShape.LINE && shapeType != FShape.ARC |
|
165 |
&& shapeType != FShape.LINE + FShape.Z) |
|
166 |
return; |
|
167 |
|
|
168 |
Geometry jtsGeom = geom.toJTSGeometry(); |
|
169 |
|
|
170 |
process(jtsGeom, feature); |
|
171 |
} |
|
172 |
|
|
173 |
protected void process(Geometry geometry, IFeature feature) { |
|
174 |
if (geometry instanceof GeometryCollection) { |
|
175 |
GeometryCollection geomCol = (GeometryCollection) geometry; |
|
176 |
for (int i = 0; i < geomCol.getNumGeometries(); i++) { |
|
177 |
Geometry geomI = geomCol.getGeometryN(i); |
|
178 |
process(geomI, feature); |
|
179 |
} |
|
180 |
} else if (geometry instanceof LineString) { |
|
181 |
LineString lineString = (LineString) geometry; |
|
182 |
Envelope lnEnv = lineString.getEnvelopeInternal(); |
|
183 |
|
|
184 |
//We try to extend a bit the original envelope to ensure |
|
185 |
//recovering geometries in the limit |
|
186 |
double minX = lnEnv.getMinX() - 10; |
|
187 |
double minY = lnEnv.getMinY() - 10; |
|
188 |
double maxX = lnEnv.getMaxX() + 10; |
|
189 |
double maxY = lnEnv.getMaxY() + 10; |
|
190 |
|
|
191 |
Rectangle2D rect = new Rectangle2D.Double(minX, minY, maxX - minX, maxY - minY); |
|
192 |
|
|
193 |
SnappingCoordinateMapWithCounter coordinateMap = new SnappingCoordinateMapWithCounter( |
|
194 |
getClusterTolerance()); |
|
195 |
|
|
196 |
// we consideer pseudonode a coordinate with degree 2 (it only |
|
197 |
// connects two geometries) |
|
198 |
Coordinate firstPoint = lineString.getCoordinateN(0); |
|
199 |
coordinateMap.put(firstPoint, firstPoint); |
|
200 |
|
|
201 |
Coordinate lastPoint = lineString.getCoordinateN(lineString |
|
202 |
.getNumPoints() - 1); |
|
203 |
coordinateMap.put(lastPoint, lastPoint); |
|
204 |
|
|
205 |
try { |
|
206 |
IFeatureIterator neighbours = originLyr.getSource() |
|
207 |
.getFeatureIterator(rect, null, null, false); |
|
208 |
while (neighbours.hasNext()) { |
|
209 |
IFeature neighbourFeature = neighbours.next(); |
|
210 |
if (neighbourFeature.getID().equalsIgnoreCase( |
|
211 |
feature.getID())) |
|
212 |
continue; |
|
213 |
Geometry geom2 = neighbourFeature.getGeometry() |
|
214 |
.toJTSGeometry(); |
|
215 |
ArrayList<LineString> geometriesToProcess = new ArrayList<LineString>(); |
|
216 |
if (geom2 instanceof LineString) { |
|
217 |
geometriesToProcess.add((LineString) geom2); |
|
218 |
} else if (geom2 instanceof MultiLineString) { |
|
219 |
MultiLineString multiLine = (MultiLineString) geom2; |
|
220 |
int numLines = multiLine.getNumGeometries(); |
|
221 |
for (int i = 0; i < numLines; i++) { |
|
222 |
LineString line = (LineString) multiLine |
|
223 |
.getGeometryN(i); |
|
224 |
geometriesToProcess.add(line); |
|
225 |
} |
|
226 |
} else if (geom2 instanceof GeometryCollection) { |
|
227 |
MultiLineString multiLine = JtsUtil |
|
228 |
.convertToMultiLineString((GeometryCollection) geom2); |
|
229 |
int numLines = multiLine.getNumGeometries(); |
|
230 |
for (int i = 0; i < numLines; i++) { |
|
231 |
LineString line = (LineString) multiLine |
|
232 |
.getGeometryN(i); |
|
233 |
geometriesToProcess.add(line); |
|
234 |
} |
|
235 |
} else { |
|
236 |
System.out.println("Encontrado:" + geom2.toString() |
|
237 |
+ " en regla de dangles"); |
|
238 |
} |
|
239 |
|
|
240 |
int numGeometries = geometriesToProcess.size(); |
|
241 |
for (int i = 0; i < numGeometries; i++) { |
|
242 |
LineString lineString2 = geometriesToProcess.get(i); |
|
243 |
Coordinate firstPoint2 = lineString2.getCoordinateN(0); |
|
244 |
Coordinate lastPoint2 = lineString2.getCoordinateN(lineString2 |
|
245 |
.getNumPoints() - 1); |
|
246 |
|
|
247 |
coordinateMap.put(lastPoint2, lastPoint); |
|
248 |
coordinateMap.put(firstPoint2, firstPoint); |
|
249 |
|
|
250 |
}//for |
|
251 |
|
|
252 |
}//while |
|
253 |
|
|
254 |
int firstPointDegree = coordinateMap.getCount(firstPoint); |
|
255 |
int lastPointDegree = coordinateMap.getCount(lastPoint); |
|
256 |
|
|
257 |
if(firstPointDegree == 2){//A pseudonode is a node with degree 2, it only connects two lines |
|
258 |
|
|
259 |
//we dont add two times the same error |
|
260 |
Coordinate existingNode = (Coordinate) pseudoNodesCoordMap.get(firstPoint); |
|
261 |
if(existingNode == null){ |
|
262 |
IGeometry errorGeom = |
|
263 |
ShapeFactory.createPoint2D(firstPoint.x, |
|
264 |
firstPoint.y); |
|
265 |
TopologyError topologyError = |
|
266 |
new TopologyError(errorGeom, |
|
267 |
this, |
|
268 |
feature, |
|
269 |
topology); |
|
270 |
topologyError.setID(errorContainer.getErrorFid()); |
|
271 |
addTopologyError(topologyError); |
|
272 |
|
|
273 |
pseudoNodesCoordMap.put(firstPoint, firstPoint); |
|
274 |
} |
|
275 |
} |
|
276 |
|
|
277 |
if(lastPointDegree == 2){ |
|
278 |
Coordinate existingNode = (Coordinate) pseudoNodesCoordMap.get(lastPoint); |
|
279 |
if(existingNode == null){ |
|
280 |
IGeometry errorGeom = |
|
281 |
ShapeFactory.createPoint2D(lastPoint.x, |
|
282 |
lastPoint.y); |
|
283 |
TopologyError topologyError = |
|
284 |
new TopologyError(errorGeom, |
|
285 |
this, |
|
286 |
feature, |
|
287 |
topology); |
|
288 |
topologyError.setID(errorContainer.getErrorFid()); |
|
289 |
addTopologyError(topologyError); |
|
290 |
|
|
291 |
pseudoNodesCoordMap.put(lastPoint, lastPoint); |
|
292 |
} |
|
293 |
} |
|
294 |
|
|
295 |
} catch (ReadDriverException e) { |
|
296 |
e.printStackTrace(); |
|
297 |
return; |
|
298 |
} |
|
299 |
} else { |
|
300 |
System.out.println("Encontrado:" + geometry.toString() |
|
301 |
+ " en regla de dangles"); |
|
302 |
} |
|
303 |
} |
|
304 |
|
|
305 |
public double getClusterTolerance() { |
|
306 |
return clusterTol; |
|
307 |
} |
|
308 |
|
|
309 |
public void setClusterTolerance(double clusterTolerance) { |
|
310 |
this.clusterTol = clusterTolerance; |
|
311 |
pseudoNodesCoordMap = new SnappingCoordinateMap(clusterTolerance); |
|
312 |
} |
|
313 |
|
|
314 |
public boolean acceptsOriginLyr(FLyrVect lyr) { |
|
315 |
try { |
|
316 |
int shapeType = lyr.getShapeType(); |
|
317 |
return (FGeometryUtil.getDimensions(shapeType) == 1); |
|
318 |
} catch (ReadDriverException e) { |
|
319 |
e.printStackTrace(); |
|
320 |
return false; |
|
321 |
} |
|
322 |
} |
|
323 |
|
|
324 |
public List<ITopologyErrorFix> getAutomaticErrorFixes() { |
|
325 |
return automaticErrorFixes; |
|
326 |
} |
|
327 |
|
|
328 |
public ISymbol getDefaultErrorSymbol() { |
|
329 |
return DEFAULT_ERROR_SYMBOL; |
|
330 |
} |
|
331 |
|
|
332 |
public ISymbol getErrorSymbol() { |
|
333 |
return errorSymbol; |
|
334 |
} |
|
335 |
|
|
336 |
public void setErrorSymbol(ISymbol errorSymbol) { |
|
337 |
this.errorSymbol = errorSymbol; |
|
338 |
} |
|
339 |
|
|
340 |
} |
trunk/libraries/libTopology/src/org/gvsig/topology/topologyrules/PolygonsMustNotOverlap.java | ||
---|---|---|
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.awt.geom.Rectangle2D; |
|
54 |
import java.util.ArrayList; |
|
55 |
import java.util.HashMap; |
|
56 |
import java.util.List; |
|
57 |
import java.util.Map; |
|
58 |
|
|
59 |
import org.gvsig.fmap.core.FGeometryUtil; |
|
60 |
import org.gvsig.jts.JtsUtil; |
|
61 |
import org.gvsig.topology.AbstractTopologyRule; |
|
62 |
import org.gvsig.topology.IRuleWithClusterTolerance; |
|
63 |
import org.gvsig.topology.ITopologyErrorFix; |
|
64 |
import org.gvsig.topology.Messages; |
|
65 |
import org.gvsig.topology.Topology; |
|
66 |
import org.gvsig.topology.TopologyError; |
|
67 |
import org.gvsig.topology.TopologyRuleDefinitionException; |
|
68 |
import org.gvsig.topology.errorfixes.CreateFeatureOverlapPolygonFix; |
|
69 |
import org.gvsig.topology.errorfixes.MergeOverlapPolygonFix; |
|
70 |
import org.gvsig.topology.errorfixes.SubstractOverlapPolygonFix; |
|
71 |
|
|
72 |
import com.hardcode.gdbms.driver.exceptions.ReadDriverException; |
|
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.IFillSymbol; |
|
78 |
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol; |
|
79 |
import com.iver.cit.gvsig.fmap.core.v02.FConverter; |
|
80 |
import com.iver.cit.gvsig.fmap.drivers.IFeatureIterator; |
|
81 |
import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
|
82 |
import com.vividsolutions.jts.geom.Envelope; |
|
83 |
import com.vividsolutions.jts.geom.Geometry; |
|
84 |
import com.vividsolutions.jts.geom.Polygon; |
|
85 |
import com.vividsolutions.jts.precision.EnhancedPrecisionOp; |
|
86 |
|
|
87 |
/** |
|
88 |
* The polygons of a given layer must not overlaps each other (their |
|
89 |
* intersection must be a line or a point, not an area) |
|
90 |
* |
|
91 |
*/ |
|
92 |
public class PolygonsMustNotOverlap extends AbstractTopologyRule implements |
|
93 |
IRuleWithClusterTolerance { |
|
94 |
|
|
95 |
private double clusterTolerance; |
|
96 |
|
|
97 |
static final String RULE_NAME = Messages.getText("must_not_overlap"); |
|
98 |
|
|
99 |
private static List<ITopologyErrorFix> automaticErrorFixes = new ArrayList<ITopologyErrorFix>(); |
|
100 |
static { |
|
101 |
automaticErrorFixes.add(new SubstractOverlapPolygonFix()); |
|
102 |
automaticErrorFixes.add(new MergeOverlapPolygonFix()); |
|
103 |
automaticErrorFixes.add(new CreateFeatureOverlapPolygonFix()); |
|
104 |
} |
|
105 |
|
|
106 |
private static final Color DEFAULT_ERROR_COLOR = Color.BLACK; |
|
107 |
|
|
108 |
private static final IFillSymbol DEFAULT_ERROR_SYMBOL = (IFillSymbol) SymbologyFactory |
|
109 |
.createDefaultSymbolByShapeType(FShape.POLYGON, DEFAULT_ERROR_COLOR); |
|
110 |
static { |
|
111 |
DEFAULT_ERROR_SYMBOL.setDescription(RULE_NAME); |
|
112 |
} |
|
113 |
|
|
114 |
private ISymbol errorSymbol = DEFAULT_ERROR_SYMBOL; |
|
115 |
|
|
116 |
protected Map<ComputedTopologyError, ComputedTopologyError> errorEntries; |
|
117 |
|
|
118 |
/** |
|
119 |
* Constructor |
|
120 |
* |
|
121 |
* @param topology |
|
122 |
* Topology which owns this rule |
|
123 |
* @param originLyr |
|
124 |
* @param destinationLyr |
|
125 |
* @param clusterTolerance |
|
126 |
*/ |
|
127 |
public PolygonsMustNotOverlap(Topology topology, FLyrVect originLyr, |
|
128 |
double clusterTolerance) { |
|
129 |
super(topology, originLyr); |
|
130 |
setClusterTolerance(clusterTolerance); |
|
131 |
|
|
132 |
errorEntries = new HashMap<ComputedTopologyError, ComputedTopologyError>(); |
|
133 |
} |
|
134 |
|
|
135 |
public PolygonsMustNotOverlap() { |
|
136 |
errorEntries = new HashMap<ComputedTopologyError, ComputedTopologyError>(); |
|
137 |
} |
|
138 |
|
|
139 |
public String getName() { |
|
140 |
return RULE_NAME; |
|
141 |
} |
|
142 |
|
|
143 |
public void checkPreconditions() throws TopologyRuleDefinitionException { |
|
144 |
try { |
|
145 |
int shapeType = this.originLyr.getShapeType(); |
|
146 |
if (FGeometryUtil.getDimensions(shapeType) != 2) |
|
147 |
throw new TopologyRuleDefinitionException( |
|
148 |
"PolygonMustNotOverlap requires a polygonal geometry type"); |
|
149 |
} catch (ReadDriverException e) { |
|
150 |
e.printStackTrace(); |
|
151 |
throw new TopologyRuleDefinitionException( |
|
152 |
"Error leyendo el tipo de geometria del driver", e); |
|
153 |
} |
|
154 |
} |
|
155 |
|
|
156 |
public void validateFeature(IFeature feature) { |
|
157 |
IGeometry geom = feature.getGeometry(); |
|
158 |
int shapeType = geom.getGeometryType(); |
|
159 |
if (shapeType != FShape.CIRCLE && shapeType != FShape.ELLIPSE |
|
160 |
&& shapeType != FShape.MULTI && shapeType != FShape.POLYGON) |
|
161 |
return; |
|
162 |
process(geom, feature); |
|
163 |
} |
|
164 |
|
|
165 |
/** |
|
166 |
* Entry class in a map of detected errors, to avoid to change two times the |
|
167 |
* same error (for example, if a OVERLAPS b, create an error for a and an |
|
168 |
* error for b) |
|
169 |
* |
|
170 |
* @author Alvaro Zabala |
|
171 |
* |
|
172 |
*/ |
|
173 |
class ComputedTopologyError { |
|
174 |
String firstFeature; |
|
175 |
String secondFeature; |
|
176 |
|
|
177 |
public boolean equals(Object o) { |
|
178 |
if (!(o instanceof ComputedTopologyError)) |
|
179 |
return false; |
|
180 |
|
|
181 |
ComputedTopologyError other = (ComputedTopologyError) o; |
|
182 |
|
|
183 |
return other.firstFeature.equals(firstFeature) |
|
184 |
|| other.firstFeature.equals(secondFeature) |
|
185 |
&& other.secondFeature.equals(secondFeature) |
|
186 |
|| other.secondFeature.equals(firstFeature); |
|
187 |
} |
|
188 |
|
|
189 |
public int hashCode() { |
|
190 |
return 1; |
|
191 |
} |
|
192 |
} |
|
193 |
|
|
194 |
public void ruleChecked(){ |
|
195 |
errorEntries.clear(); |
|
196 |
} |
|
197 |
|
|
198 |
protected void process(IGeometry geometry, IFeature feature) { |
|
199 |
|
|
200 |
Geometry jtsGeom = geometry.toJTSGeometry(); |
|
201 |
|
|
202 |
Polygon[] polygons = JtsUtil.extractPolygons(jtsGeom); |
|
203 |
for (int i = 0; i < polygons.length; i++) { |
|
204 |
|
|
205 |
Polygon polygon = polygons[i]; |
|
206 |
Envelope env1 = polygon.getEnvelopeInternal(); |
|
207 |
|
|
208 |
// We try to extend a bit the original envelope to ensure |
|
209 |
// recovering geometries in the limit |
|
210 |
|
|
211 |
double delta = clusterTolerance + 10; |
|
212 |
double minX = env1.getMinX() - delta; |
|
213 |
double minY = env1.getMinY() - delta; |
|
214 |
double maxX = env1.getMaxX() + delta; |
|
215 |
double maxY = env1.getMaxY() + delta; |
|
216 |
|
|
217 |
Rectangle2D rect = new Rectangle2D.Double(minX, minY, maxX - minX, |
|
218 |
maxY - minY); |
|
219 |
|
|
220 |
try { |
|
221 |
IFeatureIterator neighbours = originLyr.getSource() |
|
222 |
.getFeatureIterator(rect, null, null, false); |
|
223 |
while (neighbours.hasNext()) { |
|
224 |
IFeature neighbourFeature = neighbours.next(); |
|
225 |
if (neighbourFeature.getID().equalsIgnoreCase( |
|
226 |
feature.getID())) |
|
227 |
continue; |
|
228 |
|
|
229 |
IGeometry geom2 = neighbourFeature.getGeometry(); |
|
230 |
Rectangle2D rect2 = geom2.getBounds2D(); |
|
231 |
if (rect.intersects(rect2)) { |
|
232 |
|
|
233 |
Geometry jts2 = geom2.toJTSGeometry(); |
|
234 |
Polygon[] geometriesToProcess = JtsUtil |
|
235 |
.extractPolygons(jts2); |
|
236 |
|
|
237 |
for (int j = 0; j < geometriesToProcess.length; j++) { |
|
238 |
Polygon poly2 = geometriesToProcess[j]; |
|
239 |
if (poly2.overlaps(polygon)) { |
|
240 |
ComputedTopologyError errorEntry = new ComputedTopologyError(); |
|
241 |
errorEntry.firstFeature = feature.getID(); |
|
242 |
errorEntry.secondFeature = neighbourFeature.getID(); |
|
243 |
|
|
244 |
if (this.errorEntries.get(errorEntry) == null) { |
|
245 |
Geometry errorGeomJts = EnhancedPrecisionOp.intersection(poly2, polygon); |
|
246 |
IGeometry errorGeom = FConverter.jts_to_igeometry(errorGeomJts); |
|
247 |
TopologyError topologyError = new TopologyError(errorGeom, |
|
248 |
errorContainer.getErrorFid(), |
|
249 |
this, |
|
250 |
feature, |
|
251 |
neighbourFeature, |
|
252 |
topology); |
|
253 |
addTopologyError(topologyError); |
|
254 |
errorEntries.put(errorEntry, errorEntry); |
|
255 |
}// if |
|
256 |
}// if |
|
257 |
}// for |
|
258 |
}// if |
|
259 |
}// while |
|
260 |
|
|
261 |
} catch (ReadDriverException e) { |
|
262 |
e.printStackTrace(); |
|
263 |
return; |
|
264 |
} |
|
265 |
|
|
266 |
} |
|
267 |
} |
|
268 |
|
|
269 |
public double getClusterTolerance() { |
|
270 |
return clusterTolerance; |
|
271 |
} |
|
272 |
|
|
273 |
public void setClusterTolerance(double clusterTolerance) { |
|
274 |
this.clusterTolerance = clusterTolerance; |
|
275 |
} |
|
276 |
|
|
277 |
public boolean acceptsOriginLyr(FLyrVect lyr) { |
|
278 |
try { |
|
279 |
int shapeType = lyr.getShapeType(); |
|
280 |
return (FGeometryUtil.getDimensions(shapeType) == 2); |
|
281 |
} catch (ReadDriverException e) { |
|
282 |
e.printStackTrace(); |
|
283 |
return false; |
|
284 |
} |
|
285 |
} |
|
286 |
|
|
287 |
public List<ITopologyErrorFix> getAutomaticErrorFixes() { |
|
288 |
return automaticErrorFixes; |
|
289 |
} |
|
290 |
|
|
291 |
public ISymbol getDefaultErrorSymbol() { |
|
292 |
return DEFAULT_ERROR_SYMBOL; |
|
293 |
} |
|
294 |
|
|
295 |
public ISymbol getErrorSymbol() { |
|
296 |
return errorSymbol; |
|
297 |
} |
|
298 |
|
|
299 |
public void setErrorSymbol(ISymbol errorSymbol) { |
|
300 |
this.errorSymbol = errorSymbol; |
|
301 |
} |
|
302 |
|
|
303 |
} |
Also available in: Unified diff