Statistics
| Revision:

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

History | View | Annotate | Download (8.04 KB)

1
/*
2
 * Created on 01-mar-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: NearestSpatialJoinVisitor.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.6  2006/05/01 19:08:41  azabala
55
 * documentacion, y algunos metodos pasados de private a "friendly" -sin modificador-
56
 *
57
 * Revision 1.5  2006/03/21 19:29:36  azabala
58
 * *** empty log message ***
59
 *
60
 * Revision 1.4  2006/03/15 18:34:31  azabala
61
 * *** empty log message ***
62
 *
63
 * Revision 1.3  2006/03/14 18:32:46  fjp
64
 * Cambio con LayerDefinition para que sea compatible con la definici?n de tablas tambi?n.
65
 *
66
 * Revision 1.2  2006/03/07 21:01:33  azabala
67
 * *** empty log message ***
68
 *
69
 * Revision 1.1  2006/03/06 19:48:39  azabala
70
 * *** empty log message ***
71
 *
72
 * Revision 1.1  2006/03/05 19:59:47  azabala
73
 * *** empty log message ***
74
 *
75
 *
76
 */
77
package com.iver.cit.gvsig.geoprocess.spatialjoin.fmap;
78

    
79
import com.hardcode.gdbms.engine.values.Value;
80
import com.iver.cit.gvsig.fmap.DriverException;
81
import com.iver.cit.gvsig.fmap.core.IFeature;
82
import com.iver.cit.gvsig.fmap.core.IGeometry;
83
import com.iver.cit.gvsig.fmap.drivers.ILayerDefinition;
84
import com.iver.cit.gvsig.fmap.edition.EditionException;
85
import com.iver.cit.gvsig.fmap.layers.FLayer;
86
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
87
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
88
import com.iver.cit.gvsig.fmap.operations.strategies.Strategy;
89
import com.iver.cit.gvsig.fmap.operations.strategies.VisitException;
90
import com.iver.cit.gvsig.geoprocess.core.fmap.DefinitionUtils;
91
import com.iver.cit.gvsig.geoprocess.core.fmap.FeatureFactory;
92
import com.iver.cit.gvsig.geoprocess.core.fmap.FeatureProcessor;
93
import com.iver.cit.gvsig.geoprocess.core.fmap.GeoprocessException;
94

    
95
/**
96
 * This visitor implement Nearest Geometry Spatial Join.
97
 * 
98
 * It is a particular case of 1-1 relationship
99
 * (a feature of layer A always be related to a feature of layer B)
100
 * 
101
 * If two features of layer B are at the same distance of a feature of layer A,
102
 * we will took the first one analized.
103
 * 
104
 * In this visitor we apply a secuential scaning strategy: given a geometry 
105
 * of layer A, we check distances with all geometries of layer B return
106
 * the geometry at the min distance.
107
 * 
108
 * 
109
 * @author azabala
110
 *
111
 */
112
public class NearestSpatialJoinVisitor implements SpatialJoinVisitor {
113
        /**
114
         * Needed to create layer definition
115
         */
116
         FLyrVect sourceLayer;
117
        /**
118
         * Reads data of features for the source layer
119
         */
120
         SelectableDataSource sourceRecordset;
121

    
122
        /**
123
         * Reads data of features for the target layer
124
         */
125
        FLyrVect targetLayer;
126

    
127
        /**
128
         * Recordset of this layer
129
         */
130
        SelectableDataSource targetRecordset;
131

    
132
        /**
133
         * Strategy to do querys against target Layer
134
         */
135
        Strategy strategy;
136

    
137
        /**
138
         * Processes results of dissolve operations (save them in a file, or cache
139
         * them in memory, etc)
140
         */
141
        FeatureProcessor featureProcessor;
142

    
143
        /**
144
         * It looks for nearest feature to a given feature
145
         */
146
        private NearestFeatureVisitor lookForNearestVisitor;
147
        
148
        /**
149
         * Schema of the result layer
150
         */
151
        ILayerDefinition layerDefinition;
152
        /**
153
         * flag to process only selections of second layer
154
         */
155
        boolean onlySecondLayerSelection;
156

    
157
        /**
158
         * Constructor. It receives layer with which we want to do a spatial join
159
         * 
160
         * @param targetRecordset
161
         * @throws DriverException 
162
         */
163
        public NearestSpatialJoinVisitor(FLyrVect sourceLayer,
164
                        FLyrVect targetLayer,
165
                        FeatureProcessor processor) throws DriverException {
166
                this.sourceLayer = sourceLayer;
167
                this.sourceRecordset = sourceLayer.getRecordset();
168
                this.targetLayer = targetLayer;
169
                this.targetRecordset = targetLayer.getRecordset();
170
                this.featureProcessor = processor;
171
                this.lookForNearestVisitor = new NearestFeatureVisitor();
172
        }
173

    
174
        /**
175
         * Processes a Feature of source layer, looking for its nearest feature of
176
         * target layer and taking attributes from it
177
         */
178
        public void visit(IGeometry g, int sourceIndex) throws VisitException {
179
                if(g == null)
180
                        return;
181
long t0 = System.currentTimeMillis();                
182
                lookForNearestVisitor.setQueryGeometry(g.toJTSGeometry());
183
                try {
184
                        if(onlySecondLayerSelection)
185
                                strategy.process(lookForNearestVisitor, targetRecordset.getSelection());
186
                        else
187
                                strategy.process(lookForNearestVisitor);
188
                        int targetIndex = lookForNearestVisitor.getNearestFeatureIndex();
189
long t1 = System.currentTimeMillis();
190
                        IFeature joinedFeature = createFeature(g, sourceIndex, targetIndex);
191
long t2 = System.currentTimeMillis();                        
192
                        this.featureProcessor.processFeature(joinedFeature);
193
long t3 = System.currentTimeMillis();
194
System.out.println((t1-t0)+" en buscar nearest");
195
System.out.println((t2-t1)+" en build feature");
196
System.out.println((t3-t2)+" en guardar feature");
197
                } catch (DriverException e) {
198
                        throw new VisitException(
199
                                        "Problemas accediendo a los datos durante un nearest spatial join");
200
                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
201
                        throw new VisitException(
202
                        "Problemas accediendo a los datos durante un nearest spatial join");
203
                }
204
        }
205

    
206
        public void stop(FLayer layer) {
207
                this.featureProcessor.finish();
208

    
209
        }
210

    
211
        public boolean start(FLayer layer) {
212
                try {
213
                        this.featureProcessor.start();
214
                } catch (EditionException e) {
215
                        return false;
216
                }
217
                return true;
218
        }
219
        
220
        public String getProcessDescription() {
221
                return "Spatial joining by nearest criteria";
222
        }
223

    
224
        IFeature createFeature(IGeometry g, int sourceLayerIndex, int targetLayerIndex) throws DriverException, com.hardcode.gdbms.engine.data.driver.DriverException {
225
                IFeature solution = null;
226
                int numFieldsA = sourceRecordset.getFieldCount();
227
                int numFieldsB = targetRecordset.getFieldCount();
228
                Value[] featureAttr = new Value[numFieldsA + numFieldsB];
229
                for (int indexField = 0; indexField < numFieldsA; indexField++) {
230
                        featureAttr[indexField] = sourceRecordset
231
                                .getFieldValue(sourceLayerIndex,indexField);
232
                }
233
                for (int indexFieldB = 0; indexFieldB < numFieldsB; indexFieldB++) {
234
                        featureAttr[numFieldsA + indexFieldB] = 
235
                                targetRecordset.getFieldValue(targetLayerIndex, indexFieldB);
236
                }
237
                solution = FeatureFactory.createFeature(featureAttr, g);
238
                return solution;
239
        }
240
        
241
        public ILayerDefinition getResultLayerDefinition() throws GeoprocessException{
242
                if(layerDefinition == null){
243
                        try {
244
                                layerDefinition = DefinitionUtils.
245
                                mergeLayerDefinitions(sourceLayer, targetLayer);
246
                        } catch (Exception e) {
247
                                throw new GeoprocessException("Problemas al crear el esquema de la capa solucion de un spatial join");
248
                        } 
249
                }
250
                return layerDefinition;
251
        }
252

    
253
        public void setFeatureProcessor(FeatureProcessor processor) {
254
                this.featureProcessor = processor;
255
        }
256

    
257
        public void setCancelableStrategy(Strategy secondLyrStrategy) {
258
                this.strategy = secondLyrStrategy;
259
                
260
        }
261

    
262
        public void setOnlySecondLyrSelection(boolean onlySecondLayerSelection) {
263
                this.onlySecondLayerSelection = onlySecondLayerSelection;
264
        }
265

    
266
}