Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libTopology / src / org / gvsig / topology / errorfixes / ExtendDangleToNearestVertexFix.java @ 20238

History | View | Annotate | Download (8.24 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.List;
54

    
55
import org.gvsig.exceptions.BaseException;
56
import org.gvsig.fmap.core.FLyrUtil;
57
import org.gvsig.jts.JtsUtil;
58
import org.gvsig.topology.Messages;
59
import org.gvsig.topology.TopologyError;
60

    
61
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
62
import com.iver.cit.gvsig.drivers.featureiterators.FeatureListIntIterator;
63
import com.iver.cit.gvsig.fmap.core.FGeometry;
64
import com.iver.cit.gvsig.fmap.core.FPoint2D;
65
import com.iver.cit.gvsig.fmap.core.IFeature;
66
import com.iver.cit.gvsig.fmap.core.IGeometry;
67
import com.iver.cit.gvsig.fmap.core.v02.FConverter;
68
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
69
import com.iver.cit.gvsig.fmap.spatialindex.INearestNeighbourFinder;
70
import com.vividsolutions.jts.algorithms.SnapCGAlgorithms;
71
import com.vividsolutions.jts.geom.Coordinate;
72
import com.vividsolutions.jts.geom.Geometry;
73
import com.vividsolutions.jts.geom.GeometryCollection;
74
import com.vividsolutions.jts.geom.LineString;
75
import com.vividsolutions.jts.geom.util.LinearComponentExtracter;
76
import com.vividsolutions.jts.geom.util.GeometryEditor.CoordinateOperation;
77
import com.vividsolutions.jts.operation.distance.DistanceOp;
78

    
79
/**
80
 * From a given 'must not have dangles' topology error, extends
81
 * the dangling segment until it touchs another line in the specified distance
82
 * radius.
83
 * @author Alvaro Zabala
84
 *
85
 */
86
public class ExtendDangleToNearestVertexFix extends AbstractTopologyErrorFix {
87

    
88
        private double searchRadius;
89
        
90
        public ExtendDangleToNearestVertexFix(double searchRadius){
91
                super();
92
                this.searchRadius = searchRadius;
93
        }
94
        
95
        public ExtendDangleToNearestVertexFix() {
96
        }
97

    
98
        public IFeature[] fixAlgorithm(TopologyError error) throws BaseException {
99
                FLyrVect originLyr = error.getOriginLayer();
100
                double clusterTolerance = error.getTopology().getClusterTolerance();
101
                FGeometry errorGeometry = (FGeometry) error.getGeometry();
102
                FPoint2D dangle = (FPoint2D) errorGeometry.getInternalShape();
103
                Coordinate jtsDangle = new Coordinate(dangle.getX(), dangle.getY());
104
                
105
                IFeature originFeature = error.getFeature1();
106
                
107
                IFeature nearestFeature = getNearestFeature(dangle, originLyr, originFeature);
108
                
109
                if(nearestFeature != null){
110
                        Geometry nearestJts = nearestFeature.getGeometry().toJTSGeometry();
111
                        Geometry originalJts = originFeature.getGeometry().toJTSGeometry();
112
                        
113
                        Coordinate closestPoint = getClosestPoint(originalJts, nearestJts);
114
                        double distance = DistanceOp.distance(originalJts, nearestJts);
115
                        
116
                        if(distance <= searchRadius){
117
                                Geometry editedGeometry = extendGeometry(originalJts, jtsDangle, closestPoint, clusterTolerance);
118
                                if(editedGeometry != null){
119
                                        IGeometry newGeom = FConverter.jts_to_igeometry(editedGeometry);
120
                                        
121
                                        originFeature.setGeometry(newGeom);
122
                                        
123
                                        return new IFeature[]{originFeature};
124
                                }//if editedGeometry
125
                        }//if distance        
126
                }//if nearest feature
127
                return null;
128
        }
129
        
130
        
131
        protected Coordinate getClosestPoint(Geometry original, Geometry nearestGeometry){
132
                return DistanceOp.closestPoints(original, nearestGeometry)[1]; 
133
        }
134
        
135
        private Geometry extendGeometry(Geometry g, Coordinate dangle, final Coordinate closestPoint, double clusterTolerance){
136
                
137
                List<LineString> geometries2process = new ArrayList<LineString>();
138
                if (g instanceof LineString)
139
                        geometries2process.add((LineString) g);
140
                else if (g instanceof GeometryCollection) {
141
                        GeometryCollection geomCol = (GeometryCollection) g;
142
                        List lines = LinearComponentExtracter.getLines(geomCol);
143
                        geometries2process.addAll(lines);
144
                }
145
                
146
                for(int i = 0; i < geometries2process.size(); i++){
147
                        LineString lineString = geometries2process.get(i);
148
                        if(JtsUtil.isEndPoint(lineString, dangle, clusterTolerance)){
149
                                int numPoints = lineString.getNumPoints();
150
                                Coordinate start = lineString.getCoordinateN(0);
151
                                Coordinate end = lineString.getCoordinateN(numPoints - 1);
152
                                Geometry editedGeometry = null;
153
                                if(SnapCGAlgorithms.snapEquals2D(dangle, start, clusterTolerance)){
154
                                        editedGeometry = JtsUtil.GEOMETRY_EDITOR.edit(lineString, new CoordinateOperation(){
155
                                                public Coordinate[] edit(Coordinate[] coordinates, Geometry geometry) {
156
                                                        Coordinate[] newCoordinates = new Coordinate[coordinates.length + 1];
157
                                                        System.arraycopy(coordinates, 0, newCoordinates, 1, coordinates.length);
158
                                                        newCoordinates[0] = closestPoint;
159
                                                        return newCoordinates;
160
                                                }});
161
                                        
162
                                }else{
163
                                        editedGeometry = JtsUtil.GEOMETRY_EDITOR.edit(lineString, new CoordinateOperation(){
164
                                                public Coordinate[] edit(Coordinate[] coordinates, Geometry geometry) {
165
                                                        Coordinate[] newCoordinates = new Coordinate[coordinates.length + 1];
166
                                                        System.arraycopy(coordinates, 0, newCoordinates, 0, coordinates.length);
167
                                                        newCoordinates[coordinates.length] = closestPoint;
168
                                                        return newCoordinates;
169
                                                }});
170
                                }
171
                                return editedGeometry;
172
                        }//if isEndPoint
173
                }//for
174
                //at this point we havent found a linestring end point for the dangle error
175
                //TODO launch inconsistent exception?
176
                return null;
177
        }
178

    
179
        
180
        
181
        private IFeature getNearestFeature(FPoint2D dangle, FLyrVect originLyr, IFeature originFeature) throws ReadDriverException{
182
                IGeometry originalGeometry = originFeature.getGeometry();
183
                Geometry originalJts = originalGeometry.toJTSGeometry();
184
                
185
                INearestNeighbourFinder nnFinder =
186
                        FLyrUtil.getNearestNeighbourFinder(originLyr);
187
                
188
                int numberOfNearest = 2;
189
                Rectangle2D dangleRect = new Rectangle2D.Double(dangle.getX(), dangle.getY(), 1, 1);
190
//                List nearestIndexes = nnFinder.
191
//                        findNNearest(numberOfNearest, new Point2D.Double(dangle.getX(), dangle.getY()));
192
                List nearestIndexes = nnFinder.findNNearest(numberOfNearest, dangleRect);
193
                
194
                //we look for the nearest neighbour to the dangle, excepting the originFeature
195
                //if the distance to the point is lesser than the search radius, we snap it
196
                double nearestDistance = Double.MAX_VALUE;
197
                
198
                FeatureListIntIterator it = new FeatureListIntIterator(nearestIndexes, originLyr.getSource() );
199
                IFeature nearestFeature = null;
200
                while(it.hasNext()){
201
                        IFeature feature = it.next();
202
                        if(feature.getID().equalsIgnoreCase(originFeature.getID()))
203
                                continue;
204
                        IGeometry igeometry = feature.getGeometry();
205
                        Geometry jtsGeo = igeometry.toJTSGeometry();
206
                        double dist = originalJts.distance(jtsGeo);
207
                        if (dist <= nearestDistance) {
208
                                // by adding <=, we follow the convention that
209
                                // if two features are at the same distance, take
210
                                // the last as nearest neighbour
211
                                nearestDistance = dist;
212
                                nearestFeature = feature;
213
                        }// if
214
                }//while
215
                return nearestFeature;
216
        }
217
        
218
        public String getEditionDescription() {
219
                return Messages.getText("EXTEND_DANGLE_FIX");
220
        }
221

    
222
        public double getSearchRadius() {
223
                return searchRadius;
224
        }
225

    
226
        public void setSearchRadius(double searchRadius) {
227
                this.searchRadius = searchRadius;
228
        }
229
}