Statistics
| Revision:

root / trunk / extensions / extGeoProcessing / src / com / iver / gvsig / geoprocessing / impl / spatialjoin / NearestSpatialJoinVisitor.java @ 4447

History | View | Annotate | Download (7 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 4447 2006-03-15 18:35:56Z azabala $
47
 * $Log$
48
 * Revision 1.4  2006-03-15 18:34:31  azabala
49
 * *** empty log message ***
50
 *
51
 * Revision 1.3  2006/03/14 18:32:46  fjp
52
 * Cambio con LayerDefinition para que sea compatible con la definici?n de tablas tambi?n.
53
 *
54
 * Revision 1.2  2006/03/07 21:01:33  azabala
55
 * *** empty log message ***
56
 *
57
 * Revision 1.1  2006/03/06 19:48:39  azabala
58
 * *** empty log message ***
59
 *
60
 * Revision 1.1  2006/03/05 19:59:47  azabala
61
 * *** empty log message ***
62
 *
63
 *
64
 */
65
package com.iver.gvsig.geoprocessing.impl.spatialjoin;
66

    
67
import com.hardcode.gdbms.engine.values.Value;
68
import com.iver.cit.gvsig.fmap.DriverException;
69
import com.iver.cit.gvsig.fmap.core.IFeature;
70
import com.iver.cit.gvsig.fmap.core.IGeometry;
71
import com.iver.cit.gvsig.fmap.drivers.ILayerDefinition;
72
import com.iver.cit.gvsig.fmap.edition.EditionException;
73
import com.iver.cit.gvsig.fmap.layers.FLayer;
74
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
75
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
76
import com.iver.cit.gvsig.fmap.operations.strategies.Strategy;
77
import com.iver.cit.gvsig.fmap.operations.strategies.VisitException;
78
import com.iver.gvsig.geoprocessing.model.FeatureProcessor;
79
import com.iver.gvsig.geoprocessing.model.GeoprocessException;
80
import com.iver.gvsig.geoprocessing.schemabuilder.DefinitionUtils;
81
import com.iver.gvsig.geoprocessing.schemabuilder.FeatureFactory;
82

    
83
/**
84
 * This visitor implement Nearest Geometry Spatial Join.
85
 * 
86
 * It is a particular case of 1-1 relationship
87
 * (a feature of layer A always be related to a feature of layer B)
88
 * 
89
 * If two features of layer B are at the same distance of a feature of layer A,
90
 * we will took the first one analized.
91
 * @author azabala
92
 *
93
 */
94
public class NearestSpatialJoinVisitor implements SpatialJoinVisitor {
95
        /**
96
         * Needed to create layer definition
97
         */
98
        private FLyrVect sourceLayer;
99
        /**
100
         * Reads data of features for the source layer
101
         */
102
        private SelectableDataSource sourceRecordset;
103

    
104
        /**
105
         * Reads data of features for the target layer
106
         */
107
        private FLyrVect targetLayer;
108

    
109
        /**
110
         * Recordset of this layer
111
         */
112
        private SelectableDataSource targetRecordset;
113

    
114
        /**
115
         * Strategy to do querys against target Layer
116
         */
117
        private Strategy strategy;
118

    
119
        /**
120
         * Processes results of dissolve operations (save them in a file, or cache
121
         * them in memory, etc)
122
         */
123
        private FeatureProcessor featureProcessor;
124

    
125
        /**
126
         * It looks for nearest feature to a given feature
127
         */
128
        private NearestFeatureVisitor lookForNearestVisitor;
129
        
130
        private ILayerDefinition layerDefinition;
131
        
132
        private boolean onlySecondLayerSelection;
133

    
134
        /**
135
         * Constructor. It receives layer with which we want to do a spatial join
136
         * 
137
         * @param targetRecordset
138
         */
139
        public NearestSpatialJoinVisitor(FLyrVect sourceLayer,
140
                        FLyrVect targetLayer,
141
                        FeatureProcessor processor) {
142
                this.sourceLayer = sourceLayer;
143
                this.sourceRecordset = sourceLayer.getRecordset();
144
                this.targetLayer = targetLayer;
145
                this.targetRecordset = targetLayer.getRecordset();
146
                this.featureProcessor = processor;
147
                this.lookForNearestVisitor = new NearestFeatureVisitor();
148
        }
149

    
150
        /**
151
         * Processes a Feature of source layer, looking for its nearest feature of
152
         * target layer and taking attributes from it
153
         */
154
        public void visit(IGeometry g, int sourceIndex) throws VisitException {
155
                lookForNearestVisitor.setQueryGeometry(g.toJTSGeometry());
156
                try {
157
                        if(onlySecondLayerSelection)
158
                                strategy.process(lookForNearestVisitor, targetRecordset.getSelection());
159
                        else
160
                                strategy.process(lookForNearestVisitor);
161
                        int targetIndex = lookForNearestVisitor.getNearestFeatureIndex();
162
                        IFeature joinedFeature = createFeature(g, sourceIndex, targetIndex);
163
                        this.featureProcessor.processFeature(joinedFeature);
164
                } catch (DriverException e) {
165
                        throw new VisitException(
166
                                        "Problemas accediendo a los datos durante un nearest spatial join");
167
                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
168
                        throw new VisitException(
169
                        "Problemas accediendo a los datos durante un nearest spatial join");
170
                }
171
        }
172

    
173
        public void stop(FLayer layer) {
174
                this.featureProcessor.finish();
175

    
176
        }
177

    
178
        public boolean start(FLayer layer) {
179
                try {
180
                        this.featureProcessor.start();
181
                } catch (EditionException e) {
182
                        return false;
183
                }
184
                return true;
185
        }
186
        
187
        public String getProcessDescription() {
188
                return "Spatial joining by nearest criteria";
189
        }
190

    
191
        private IFeature createFeature(IGeometry g, int sourceLayerIndex, int targetLayerIndex) throws DriverException, com.hardcode.gdbms.engine.data.driver.DriverException {
192
                IFeature solution = null;
193
                int numFieldsA = sourceRecordset.getFieldCount();
194
                int numFieldsB = targetRecordset.getFieldCount();
195
                Value[] featureAttr = new Value[numFieldsA + numFieldsB];
196
                for (int indexField = 0; indexField < numFieldsA; indexField++) {
197
                        featureAttr[indexField] = sourceRecordset
198
                                .getFieldValue(sourceLayerIndex,indexField);
199
                }
200
                for (int indexFieldB = 0; indexFieldB < numFieldsB; indexFieldB++) {
201
                        featureAttr[numFieldsA + indexFieldB] = 
202
                                targetRecordset.getFieldValue(targetLayerIndex, indexFieldB);
203
                }
204
                solution = FeatureFactory.createFeature(featureAttr, g);
205
                return solution;
206
        }
207
        
208
        public ILayerDefinition getResultLayerDefinition() throws GeoprocessException{
209
                if(layerDefinition == null){
210
                        try {
211
                                layerDefinition = DefinitionUtils.
212
                                mergeLayerDefinitions(sourceLayer, targetLayer);
213
                        } catch (Exception e) {
214
                                throw new GeoprocessException("Problemas al crear el esquema de la capa solucion de un spatial join");
215
                        } 
216
                }
217
                return layerDefinition;
218
        }
219

    
220
        public void setFeatureProcessor(FeatureProcessor processor) {
221
                this.featureProcessor = processor;
222
        }
223

    
224
        public void setCancelableStrategy(Strategy secondLyrStrategy) {
225
                this.strategy = secondLyrStrategy;
226
                
227
        }
228

    
229
        public void setOnlySecondLyrSelection(boolean onlySecondLayerSelection) {
230
                this.onlySecondLayerSelection = onlySecondLayerSelection;
231
        }
232

    
233
}