root / trunk / libraries / libTopology / src / org / gvsig / topology / errorfixes / SplitSelfIntersectingPolygonFix.java @ 23039
History | View | Annotate | Download (4.92 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.errorfixes; |
50 |
|
51 |
import java.util.ArrayList; |
52 |
import java.util.Arrays; |
53 |
import java.util.List; |
54 |
|
55 |
import org.gvsig.exceptions.BaseException; |
56 |
import org.gvsig.fmap.core.NewFConverter; |
57 |
import org.gvsig.jts.JtsUtil; |
58 |
import org.gvsig.jts.SnapLineStringSelfIntersectionChecker; |
59 |
import org.gvsig.topology.Messages; |
60 |
import org.gvsig.topology.TopologyError; |
61 |
|
62 |
import com.hardcode.gdbms.engine.values.Value; |
63 |
import com.iver.cit.gvsig.fmap.core.DefaultFeature; |
64 |
import com.iver.cit.gvsig.fmap.core.FMultiPoint2D; |
65 |
import com.iver.cit.gvsig.fmap.core.IFeature; |
66 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
67 |
import com.vividsolutions.jts.geom.Geometry; |
68 |
import com.vividsolutions.jts.geom.LinearRing; |
69 |
import com.vividsolutions.jts.geom.MultiPoint; |
70 |
import com.vividsolutions.jts.geom.Polygon; |
71 |
|
72 |
public class SplitSelfIntersectingPolygonFix extends SplitSelfIntersectingLineFix { |
73 |
|
74 |
|
75 |
public List<IFeature>[] fixAlgorithm(TopologyError error) throws BaseException { |
76 |
double clusterTolerance = error.getTopology().getClusterTolerance();
|
77 |
|
78 |
IGeometry selfIntersectingPoints = error.getGeometry(); |
79 |
FMultiPoint2D shape = (FMultiPoint2D) selfIntersectingPoints. |
80 |
getInternalShape(); |
81 |
|
82 |
MultiPoint selfIntersections = (MultiPoint)NewFConverter.toJtsGeometry(shape); |
83 |
|
84 |
IFeature originFeature = error.getFeature1(); |
85 |
Value[] attributes = originFeature.getAttributes();
|
86 |
IGeometry originGeometry = originFeature.getGeometry(); |
87 |
Geometry jtsGeo = originGeometry.toJTSGeometry(); |
88 |
|
89 |
Polygon[] polygons = JtsUtil.extractPolygons(jtsGeo); |
90 |
for(int i = 0; i < polygons.length; i++){ |
91 |
Polygon polygon = polygons[i];
|
92 |
|
93 |
if(!polygon.disjoint(selfIntersections)){
|
94 |
|
95 |
List<LinearRing> shells = new ArrayList<LinearRing>(); |
96 |
List<LinearRing> holes = new ArrayList<LinearRing>(); |
97 |
|
98 |
LinearRing shell = (LinearRing) polygon.getExteriorRing(); |
99 |
SnapLineStringSelfIntersectionChecker checker = |
100 |
new SnapLineStringSelfIntersectionChecker(shell, clusterTolerance);
|
101 |
Geometry[] splittedGeometries = checker.clean();
|
102 |
|
103 |
// shells.addAll((Collection<? extends LinearRing>) Arrays.asList(splittedGeometries));
|
104 |
for(int k = 0; k < splittedGeometries.length; k++){ |
105 |
shells.add((LinearRing)splittedGeometries[k]); |
106 |
} |
107 |
|
108 |
|
109 |
for(int j = 0; j < polygon.getNumInteriorRing(); j++){ |
110 |
LinearRing hole = (LinearRing) polygon.getInteriorRingN(j); |
111 |
checker = new SnapLineStringSelfIntersectionChecker(hole,
|
112 |
clusterTolerance); |
113 |
splittedGeometries = checker.clean(); |
114 |
|
115 |
// shells.addAll((Collection<? extends LinearRing>) Arrays.asList(splittedGeometries));
|
116 |
for(int k = 0; k < splittedGeometries.length; k++){ |
117 |
shells.add((LinearRing)splittedGeometries[k]); |
118 |
} |
119 |
} |
120 |
|
121 |
Geometry editedGeometry = JtsUtil.buildPolygons(shells, holes); |
122 |
Polygon[] correctedPolygons = JtsUtil.extractPolygons(editedGeometry); |
123 |
IFeature[] newFeatures = new IFeature[correctedPolygons.length]; |
124 |
|
125 |
for(int j = 0; j < correctedPolygons.length; j++){ |
126 |
Polygon pol = correctedPolygons[j];
|
127 |
IGeometry igeom = NewFConverter.toFMap(pol); |
128 |
newFeatures[j] = new DefaultFeature(igeom,
|
129 |
attributes, |
130 |
originFeature.getID()+j); |
131 |
} |
132 |
List<IFeature> editedFeatures = new ArrayList<IFeature>(); |
133 |
editedFeatures.addAll(Arrays.asList(newFeatures));
|
134 |
return (List<IFeature>[]) new List[]{editedFeatures}; |
135 |
}//if ! disjoints
|
136 |
} |
137 |
return null; |
138 |
} |
139 |
|
140 |
|
141 |
public String getEditionDescription() { |
142 |
return Messages.getText("SPLIT_SELFINTERSECTING_POLYGON_FIX"); |
143 |
} |
144 |
|
145 |
} |