svn-gvsig-desktop / trunk / libraries / libTopology / src / org / gvsig / topology / errorfixes / RemovePseudoNodeFix.java @ 25630
History | View | Annotate | Download (8.16 KB)
1 | 20136 | azabala | /*
|
---|---|---|---|
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.awt.geom.Rectangle2D; |
||
52 | import java.util.ArrayList; |
||
53 | 23039 | azabala | import java.util.Arrays; |
54 | import java.util.Collection; |
||
55 | 20136 | azabala | import java.util.Iterator; |
56 | import java.util.List; |
||
57 | |||
58 | import org.gvsig.exceptions.BaseException; |
||
59 | import org.gvsig.fmap.core.FGeometryUtil; |
||
60 | 23039 | azabala | import org.gvsig.fmap.core.NewFConverter; |
61 | import org.gvsig.jts.JtsUtil; |
||
62 | 20136 | azabala | import org.gvsig.topology.Messages; |
63 | import org.gvsig.topology.TopologyError; |
||
64 | |||
65 | 23039 | azabala | import sun.reflect.generics.reflectiveObjects.NotImplementedException; |
66 | |||
67 | 20136 | azabala | import com.hardcode.gdbms.engine.values.Value; |
68 | import com.iver.cit.gvsig.fmap.core.DefaultFeature; |
||
69 | import com.iver.cit.gvsig.fmap.core.FGeometry; |
||
70 | import com.iver.cit.gvsig.fmap.core.FPoint2D; |
||
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.drivers.IFeatureIterator; |
||
74 | import com.iver.cit.gvsig.fmap.edition.EditableAdapter; |
||
75 | import com.iver.cit.gvsig.fmap.edition.EditionEvent; |
||
76 | import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
||
77 | import com.iver.cit.gvsig.fmap.layers.SelectableDataSource; |
||
78 | import com.vividsolutions.jts.algorithms.SnapCGAlgorithms; |
||
79 | import com.vividsolutions.jts.geom.Coordinate; |
||
80 | import com.vividsolutions.jts.geom.Geometry; |
||
81 | import com.vividsolutions.jts.geom.LineString; |
||
82 | 23039 | azabala | import com.vividsolutions.jts.operation.linemerge.LineMerger; |
83 | 20136 | azabala | import com.vividsolutions.jts.precision.EnhancedPrecisionOp; |
84 | |||
85 | /**
|
||
86 | * Removes the pseudonode associated to a topology error by merging
|
||
87 | * two touching linestrings.
|
||
88 | *
|
||
89 | * It will preserve the alphanumeric attributes of the largest linestring.
|
||
90 | * @author Alvaro Zabala
|
||
91 | *
|
||
92 | */
|
||
93 | public class RemovePseudoNodeFix extends AbstractTopologyErrorFix { |
||
94 | |||
95 | 23039 | azabala | |
96 | |||
97 | 20136 | azabala | public void fix(TopologyError error) throws BaseException { |
98 | EditableAdapter[] adapters = prepareEdition(error);
|
||
99 | |||
100 | List<IFeature> features2merge = new ArrayList<IFeature>(); |
||
101 | 23039 | azabala | |
102 | 20136 | azabala | IFeature firstFeature = error.getFeature1(); |
103 | features2merge.add(firstFeature); |
||
104 | |||
105 | 23039 | azabala | Geometry firstJtsGeo = NewFConverter.toJtsGeometry(firstFeature.getGeometry()); |
106 | LineString[] firstLines = JtsUtil.extractLineStrings(firstJtsGeo);
|
||
107 | |||
108 | |||
109 | 20136 | azabala | FGeometry errorGeom = (FGeometry) error.getGeometry(); |
110 | FPoint2D pseudonode = (FPoint2D) errorGeom.getInternalShape(); |
||
111 | double x = pseudonode.getX();
|
||
112 | double y = pseudonode.getY();
|
||
113 | double clusterTolerance = error.getTopology().getClusterTolerance();
|
||
114 | 23039 | azabala | |
115 | 20136 | azabala | Rectangle2D queryRect = FGeometryUtil.getSnapRectangle(x, y,
|
116 | clusterTolerance); |
||
117 | 23039 | azabala | |
118 | 20136 | azabala | FLyrVect originLyr = error.getOriginLayer(); |
119 | SelectableDataSource recordset = originLyr.getRecordset(); |
||
120 | int numberOfFields = recordset.getFieldCount();
|
||
121 | String[] fields = new String[numberOfFields]; |
||
122 | for (int i = 0; i < numberOfFields; i++) { |
||
123 | fields[i] = recordset.getFieldName(i); |
||
124 | } |
||
125 | 23039 | azabala | |
126 | IFeatureIterator featureIterator = originLyr.getSource(). |
||
127 | getFeatureIterator(queryRect, fields, null, false); |
||
128 | 20136 | azabala | while (featureIterator.hasNext()) {
|
129 | IFeature candidate = featureIterator.next(); |
||
130 | if (candidate.getID().equals(firstFeature.getID()))
|
||
131 | continue;
|
||
132 | |||
133 | 23039 | azabala | Geometry secondJtsGeo = NewFConverter.toJtsGeometry(candidate.getGeometry()); |
134 | LineString[] secondLines = JtsUtil.extractLineStrings(secondJtsGeo);
|
||
135 | 20136 | azabala | |
136 | 20230 | azabala | lookingForNestedPrimitive: |
137 | 23039 | azabala | for (int i = 0; i < firstLines.length; i++) { |
138 | LineString linA = firstLines[i]; |
||
139 | 20136 | azabala | Coordinate firstA = linA.getCoordinateN(0);
|
140 | Coordinate lastA = linA.getCoordinateN(linA.getNumPoints() - 1);
|
||
141 | 23039 | azabala | |
142 | for (int j = 0; j < secondLines.length; j++) { |
||
143 | LineString linB = secondLines[j]; |
||
144 | |||
145 | 20136 | azabala | Coordinate firstB = linB.getCoordinateN(0);
|
146 | 20230 | azabala | Coordinate lastB = linB.getCoordinateN(linB.getNumPoints() - 1);
|
147 | |||
148 | 23039 | azabala | //quizas deberia refinar esto, y comprobar que casan con el pseudonodo
|
149 | 20136 | azabala | if (SnapCGAlgorithms.snapEquals2D(firstA, firstB,
|
150 | clusterTolerance) |
||
151 | || SnapCGAlgorithms.snapEquals2D(firstA, lastB, |
||
152 | clusterTolerance) |
||
153 | || SnapCGAlgorithms.snapEquals2D(lastA, firstB, |
||
154 | clusterTolerance) |
||
155 | || SnapCGAlgorithms.snapEquals2D(firstA, lastB, |
||
156 | clusterTolerance)) { |
||
157 | features2merge.add(candidate); |
||
158 | break lookingForNestedPrimitive;
|
||
159 | }// if
|
||
160 | }// for j
|
||
161 | }// for i
|
||
162 | |||
163 | }// while
|
||
164 | |||
165 | // at this point, features2merge has all the features whose geometries
|
||
166 | // we must to union
|
||
167 | 23039 | azabala | double maxLength = -1; |
168 | 20136 | azabala | Iterator<IFeature> featuresIterator = features2merge.iterator();
|
169 | Geometry newJtsGeometry = null;
|
||
170 | Value[] attributes = null; |
||
171 | |||
172 | 23039 | azabala | LineMerger merger = new LineMerger();
|
173 | List geometries = new ArrayList(); |
||
174 | 20136 | azabala | while (featuresIterator.hasNext()) {
|
175 | IFeature feature = featuresIterator.next(); |
||
176 | 23039 | azabala | Geometry jtsGeometry = NewFConverter.toJtsGeometry(feature.getGeometry());//FIXME Este paso se podria ahorrar si creamos un FixFeature que cachee los JTS
|
177 | 20136 | azabala | double lenght = jtsGeometry.getLength();
|
178 | if (lenght > maxLength) {
|
||
179 | attributes = feature.getAttributes(); |
||
180 | maxLength = lenght; |
||
181 | } |
||
182 | |||
183 | 23039 | azabala | //with the union of the linestrings, we node them
|
184 | 20136 | azabala | if (newJtsGeometry == null) |
185 | newJtsGeometry = jtsGeometry; |
||
186 | else
|
||
187 | newJtsGeometry = EnhancedPrecisionOp.union(newJtsGeometry, |
||
188 | jtsGeometry); |
||
189 | } |
||
190 | 23039 | azabala | //at this point, newJtsGeometry is a GeometryCollection with fully noded linestrings
|
191 | LineString[] lineStrings = JtsUtil.extractLineStrings(newJtsGeometry);
|
||
192 | geometries.addAll(Arrays.asList(lineStrings));
|
||
193 | merger.add(geometries); |
||
194 | |||
195 | Collection mergedLineStrings = merger.getMergedLineStrings();
|
||
196 | Geometry[] geoms = new Geometry[mergedLineStrings.size()]; |
||
197 | mergedLineStrings.toArray(geoms); |
||
198 | if(geoms.length == 1) |
||
199 | newJtsGeometry = geoms[0];
|
||
200 | else
|
||
201 | newJtsGeometry = JtsUtil.GEOMETRY_FACTORY.createGeometryCollection(geoms); |
||
202 | |||
203 | IGeometry newIGeometry = NewFConverter.toFMap(newJtsGeometry); |
||
204 | 25630 | azabala | String newId = error.getOriginLayer().getSource().getShapeCount()+""; |
205 | DefaultFeature newFeature = new DefaultFeature(newIGeometry, attributes, newId);
|
||
206 | 20136 | azabala | |
207 | 25630 | azabala | // adapters[0].startComplexRow();
|
208 | |||
209 | adapters[0].doAddRow(newFeature, EditionEvent.GRAPHIC);
|
||
210 | 23039 | azabala | //now we remove the merged features
|
211 | 20136 | azabala | for (int i = 0; i < features2merge.size(); i++) { |
212 | IFeature feature = features2merge.get(i); |
||
213 | 23039 | azabala | |
214 | 25630 | azabala | // adapters[0].removeRow(Integer.parseInt(feature.getID()),
|
215 | // getEditionDescription(),
|
||
216 | // EditionEvent.ROW_EDITION);
|
||
217 | adapters[0].doRemoveRow(Integer.parseInt(feature.getID()), EditionEvent.ROW_EDITION); |
||
218 | 20136 | azabala | } |
219 | 23039 | azabala | //and created a new feature, with the attributes of the largest line
|
220 | 25630 | azabala | // adapters[0].addRow(newFeature,getEditionDescription(), EditionEvent.GRAPHIC);
|
221 | 20401 | azabala | |
222 | 25630 | azabala | // adapters[0].endComplexRow(getEditionDescription());
|
223 | 20401 | azabala | error.getTopology().removeError(error); |
224 | 20136 | azabala | |
225 | } |
||
226 | |||
227 | public String getEditionDescription() { |
||
228 | return Messages.getText("REMOVE_PSEUDONODE_FIX"); |
||
229 | } |
||
230 | |||
231 | 23039 | azabala | @Override
|
232 | public List<IFeature>[] fixAlgorithm(TopologyError error) |
||
233 | throws BaseException {
|
||
234 | throw new NotImplementedException(); |
||
235 | } |
||
236 | |||
237 | 20136 | azabala | } |