Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libTopology / src / org / gvsig / topology / errorfixes / RemovePseudoNodeFix.java @ 25630

History | View | Annotate | Download (8.16 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.awt.geom.Rectangle2D;
52
import java.util.ArrayList;
53
import java.util.Arrays;
54
import java.util.Collection;
55
import java.util.Iterator;
56
import java.util.List;
57

    
58
import org.gvsig.exceptions.BaseException;
59
import org.gvsig.fmap.core.FGeometryUtil;
60
import org.gvsig.fmap.core.NewFConverter;
61
import org.gvsig.jts.JtsUtil;
62
import org.gvsig.topology.Messages;
63
import org.gvsig.topology.TopologyError;
64

    
65
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
66

    
67
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
import com.vividsolutions.jts.operation.linemerge.LineMerger;
83
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
        
96
        
97
        public void fix(TopologyError error) throws BaseException {
98
                EditableAdapter[] adapters = prepareEdition(error);
99

    
100
                List<IFeature> features2merge = new ArrayList<IFeature>();
101
                
102
                IFeature firstFeature = error.getFeature1();
103
                features2merge.add(firstFeature);
104

    
105
                Geometry firstJtsGeo = NewFConverter.toJtsGeometry(firstFeature.getGeometry());
106
                LineString[] firstLines = JtsUtil.extractLineStrings(firstJtsGeo);
107
                
108
                
109
                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
                
115
                Rectangle2D queryRect = FGeometryUtil.getSnapRectangle(x, y,
116
                                clusterTolerance);
117
                
118
                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
                
126
                IFeatureIterator featureIterator = originLyr.getSource().
127
                                getFeatureIterator(queryRect, fields, null, false);
128
                while (featureIterator.hasNext()) {
129
                        IFeature candidate = featureIterator.next();
130
                        if (candidate.getID().equals(firstFeature.getID()))
131
                                continue;
132

    
133
                        Geometry secondJtsGeo = NewFConverter.toJtsGeometry(candidate.getGeometry());
134
                        LineString[] secondLines = JtsUtil.extractLineStrings(secondJtsGeo);
135

    
136
                        lookingForNestedPrimitive: 
137
                                for (int i = 0; i < firstLines.length; i++) {
138
                                LineString linA = firstLines[i];
139
                                Coordinate firstA = linA.getCoordinateN(0);
140
                                Coordinate lastA = linA.getCoordinateN(linA.getNumPoints() - 1);
141
                                
142
                                for (int j = 0; j < secondLines.length; j++) {
143
                                        LineString linB = secondLines[j];
144
                                        
145
                                        Coordinate firstB = linB.getCoordinateN(0);
146
                                        Coordinate lastB = linB.getCoordinateN(linB.getNumPoints() - 1);
147
                                        
148
                                        //quizas deberia refinar esto, y comprobar que casan con el pseudonodo
149
                                        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
                double maxLength = -1;
168
                Iterator<IFeature> featuresIterator = features2merge.iterator();
169
                Geometry newJtsGeometry = null;
170
                Value[] attributes = null;
171

    
172
                LineMerger merger = new LineMerger();
173
                List geometries = new ArrayList();
174
                while (featuresIterator.hasNext()) {
175
                        IFeature feature = featuresIterator.next();
176
                        Geometry jtsGeometry = NewFConverter.toJtsGeometry(feature.getGeometry());//FIXME Este paso se podria ahorrar si creamos un FixFeature que cachee los JTS
177
                        double lenght = jtsGeometry.getLength();
178
                        if (lenght > maxLength) {
179
                                attributes = feature.getAttributes();
180
                                maxLength = lenght;
181
                        }
182

    
183
                        //with the union of the linestrings, we node them
184
                        if (newJtsGeometry == null)
185
                                newJtsGeometry = jtsGeometry;
186
                        else
187
                                newJtsGeometry = EnhancedPrecisionOp.union(newJtsGeometry,
188
                                                jtsGeometry);
189
                }
190
                //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
                String newId = error.getOriginLayer().getSource().getShapeCount()+"";
205
                DefaultFeature newFeature = new DefaultFeature(newIGeometry, attributes, newId);
206

    
207
//                adapters[0].startComplexRow();
208
                
209
                adapters[0].doAddRow(newFeature, EditionEvent.GRAPHIC);
210
                //now we remove the merged features
211
                for (int i = 0; i < features2merge.size(); i++) {
212
                        IFeature feature = features2merge.get(i);
213
                
214
//                        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
                }
219
                //and created a new feature, with the attributes of the largest line
220
//                adapters[0].addRow(newFeature,getEditionDescription(), EditionEvent.GRAPHIC);
221
                
222
//                adapters[0].endComplexRow(getEditionDescription());
223
                error.getTopology().removeError(error);
224

    
225
        }
226

    
227
        public String getEditionDescription() {
228
                return Messages.getText("REMOVE_PSEUDONODE_FIX");
229
        }
230

    
231
        @Override
232
        public List<IFeature>[] fixAlgorithm(TopologyError error)
233
                        throws BaseException {
234
                throw new NotImplementedException();
235
        }
236

    
237
}