Statistics
| Revision:

root / trunk / extensions / extGeoProcessing / src / com / iver / cit / gvsig / geoprocess / spatialjoin / fmap / SpatiallyIndexedSpatialJoinVisitor.java @ 5628

History | View | Annotate | Download (5.93 KB)

1
/*
2
 * Created on 02-may-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: SpatiallyIndexedSpatialJoinVisitor.java 5628 2006-06-02 18:21:28Z azabala $
47
* $Log$
48
* Revision 1.2  2006-06-02 18:21:28  azabala
49
* *** empty log message ***
50
*
51
* Revision 1.1  2006/05/24 21:09:47  azabala
52
* primera version en cvs despues de refactoring orientado a crear un framework extensible de geoprocessing
53
*
54
* Revision 1.1  2006/05/02 18:58:47  azabala
55
* first version in cvs
56
*
57
*
58
*/
59
package com.iver.cit.gvsig.geoprocess.spatialjoin.fmap;
60

    
61
import java.awt.geom.Rectangle2D;
62
import java.util.List;
63

    
64
import com.iver.cit.gvsig.fmap.DriverException;
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.drivers.DriverIOException;
68
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
69
import com.iver.cit.gvsig.fmap.layers.ReadableVectorial;
70
import com.iver.cit.gvsig.fmap.operations.strategies.VisitException;
71
import com.iver.cit.gvsig.fmap.spatialindex.INearestNeighbourFinder;
72
import com.iver.cit.gvsig.fmap.spatialindex.ISpatialIndex;
73
import com.iver.cit.gvsig.geoprocess.core.fmap.FeatureProcessor;
74
import com.vividsolutions.jts.geom.Geometry;
75
/**
76
 * This  visitor does spatial join with nearest neighbour 
77
 * criteria by using an spatial index with nearest neighbour
78
 * searching capabilities.<br>
79
 * By now, RTreeSptLib is the only spatial index that is also
80
 * a INearestNeigbourFinder implementation.
81
 * <br>
82
 * @author azabala
83
 *
84
 */
85
public class SpatiallyIndexedSpatialJoinVisitor extends
86
                NearestSpatialJoinVisitor {
87
        /**
88
         * Geometry.distance() is a costly operation. Thats the reason
89
         * for we are only using a nearest neighbour.
90
         * TODO SpatialIndex works with Rectangle2D, and this may be a simplification
91
         * that drives to errors. Make aditional probes to use a number of default
92
         * neighbours
93
         */
94
        static final int DEFAULT_NUM_NEIGBOURS = 1;
95
        /**
96
         * Number of neighbours that nearestFinder must
97
         * found.
98
         */
99
        int numOfNeighbours = DEFAULT_NUM_NEIGBOURS;
100
        /**
101
         * Spetialized instance in nearest neighbour searchs.
102
         */
103
        private INearestNeighbourFinder nearestFinder;
104
        /**
105
         * Constructor
106
         * @param sourceLayer
107
         * @param targetLayer
108
         * @param processor
109
         * @throws DriverException
110
         */
111
        public SpatiallyIndexedSpatialJoinVisitor(FLyrVect sourceLayer, 
112
                        FLyrVect targetLayer, 
113
                        FeatureProcessor processor) throws DriverException {
114
                super(sourceLayer, targetLayer, processor);
115
//                lo comentamos porque debe ser llamado externamente
116
//                initialize();
117
        }
118
        
119
        public void initialize(){
120
                ISpatialIndex spatialIndex =
121
                        targetLayer.getISpatialIndex();
122
                if(spatialIndex instanceof INearestNeighbourFinder)
123
                        nearestFinder = (INearestNeighbourFinder)spatialIndex;
124
                else
125
                        throw new IllegalArgumentException("La segunda capa de spatial join ha de tener un ?ndice espacial de tipo INearestNeighbourFinder para usar este visitor");
126
        }
127
        
128
        
129
        
130
        /**
131
         * Processes a Feature of source layer, looking for its nearest feature of
132
         * target layer and taking attributes from it
133
         */
134
        public void visit(IGeometry g, int sourceIndex) throws VisitException {
135
                if(g == null)
136
                        return;
137
                try {
138
long t0 = System.currentTimeMillis();                        
139
                //no se si el rtree har? la busqueda bien si el rectangulo
140
                //de busqueda no est? insertado. Hacer las pruebas (si no,
141
                //a?adimos ahora y borramos luego)
142
                Geometry gJts = g.toJTSGeometry();
143
                Rectangle2D rect = g.getBounds2D();        
144
                List nearestLst = nearestFinder.
145
                        findNNearest(numOfNeighbours, rect);
146
                int targetIndex = -1;//index of nearest neighbour
147
                double nearestDistance = Double.MAX_VALUE;
148
                ReadableVectorial rv = targetLayer.getSource();
149
                for(int i = 0; i < nearestLst.size(); i++){
150
                        int index2 = ((Integer)nearestLst.get(i)).intValue();
151
                        IGeometry g2 = rv.getShape(index2);
152
                        Geometry g2Jts = g2.toJTSGeometry();
153
                        double dist = gJts.distance(g2Jts);
154
                        if(dist <= nearestDistance){
155
                                //by adding <=, we follow the convention that
156
                                //if two features are at the same distance, take
157
                                //the last as nearest neighbour
158
                                nearestDistance = dist;
159
                                targetIndex = index2;
160
                        }//if
161
                }//for
162
                if(targetIndex == -1)
163
                        throw new VisitException("Problemas durante el spatial join, no se encontr? un vecino mas proximo");
164
                IFeature joinedFeature;
165
                
166
                        joinedFeature = createFeature(g, sourceIndex, targetIndex);
167
                        this.featureProcessor.processFeature(joinedFeature);
168
long t1 = System.currentTimeMillis();
169
System.out.println((t1-t0)+" en spatial join con indice espacial");
170
                } catch (DriverException e) {
171
                        throw new VisitException("Error de driver al escribir un feature resultante de un spatial join", e);
172
                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
173
                        throw new VisitException("Error de driver al escribir un feature resultante de un spatial join", e);
174
                } catch (DriverIOException e) {
175
                        throw new VisitException("Error accediendo a datos de la capa destino en spatial join", e);
176
                }
177
                
178
        }
179

    
180
}
181