Statistics
| Revision:

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

History | View | Annotate | Download (9.67 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: IntersectSpatialJoinVisitor.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.4  2006/03/21 19:29:36  azabala
55
 * *** empty log message ***
56
 *
57
 * Revision 1.3  2006/03/14 18:32:46  fjp
58
 * Cambio con LayerDefinition para que sea compatible con la definici?n de tablas tambi?n.
59
 *
60
 * Revision 1.2  2006/03/07 21:01:33  azabala
61
 * *** empty log message ***
62
 *
63
 * Revision 1.1  2006/03/06 19:48:39  azabala
64
 * *** empty log message ***
65
 *
66
 * Revision 1.1  2006/03/05 19:59:47  azabala
67
 * *** empty log message ***
68
 *
69
 *
70
 */
71
package com.iver.cit.gvsig.geoprocess.spatialjoin.fmap;
72

    
73
import java.util.ArrayList;
74
import java.util.Iterator;
75
import java.util.Map;
76

    
77
import com.hardcode.gdbms.engine.values.Value;
78
import com.iver.cit.gvsig.fmap.DriverException;
79
import com.iver.cit.gvsig.fmap.core.IFeature;
80
import com.iver.cit.gvsig.fmap.core.IGeometry;
81
import com.iver.cit.gvsig.fmap.drivers.FieldDescription;
82
import com.iver.cit.gvsig.fmap.drivers.ILayerDefinition;
83
import com.iver.cit.gvsig.fmap.drivers.LayerDefinition;
84
import com.iver.cit.gvsig.fmap.drivers.SHPLayerDefinition;
85
import com.iver.cit.gvsig.fmap.edition.EditionException;
86
import com.iver.cit.gvsig.fmap.layers.FLayer;
87
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
88
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
89
import com.iver.cit.gvsig.fmap.operations.strategies.Strategy;
90
import com.iver.cit.gvsig.fmap.operations.strategies.VisitException;
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.SummarizationFunction;
94
import com.iver.cit.gvsig.geoprocess.core.fmap.XTypes;
95

    
96
/**
97
 * This visitor implement Intersect Geometry Spatial Join.
98
 * 
99
 * It is a particular case of 1-M relationship (a feature of layer A always be
100
 * related to M feature of layer B)
101
 * 
102
 * @author azabala
103
 * 
104
 */
105
public class IntersectSpatialJoinVisitor implements SpatialJoinVisitor {
106
        
107
        /**
108
         * Needed to create layer definition
109
         */
110
        private FLyrVect sourceLayer;
111
        /**
112
         * Reads data of features for the source layer
113
         */
114
        private SelectableDataSource sourceRecordset;
115

    
116
        /**
117
         * Recordset of this layer
118
         */
119
        private SelectableDataSource targetRecordset;
120

    
121
        /**
122
         * Strategy to do querys against target Layer
123
         */
124
        private Strategy strategy;
125

    
126
        /**
127
         * Maps for each numerical field of target layer its sumarization functions
128
         */
129
        private Map fields_sumarizeFunc;
130

    
131
        /**
132
         * Visitor that finds features of target layer that intersects with a given
133
         * feature of source layer
134
         */
135
        private IntersectsFinderFeatureVisitor visitor;
136

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

    
143
        private LayerDefinition resultLayerDefinition;
144
        
145
        private boolean onlySecondLyrSelection;
146

    
147
        public IntersectSpatialJoinVisitor(FLyrVect sourceLayer,
148
                                                                                FLyrVect targetLayer,
149
                                                                                Map fields_sumarizeFunct, 
150
                                                                                FeatureProcessor processor) throws DriverException {
151
                this.sourceLayer = sourceLayer;
152
                this.sourceRecordset = sourceLayer.getRecordset();
153
                this.featureProcessor = processor;
154
                this.fields_sumarizeFunc = fields_sumarizeFunct;
155
                this.targetRecordset = targetLayer.getRecordset();
156
                this.visitor = new IntersectsFinderFeatureVisitor(fields_sumarizeFunc);
157
        }
158

    
159
        public void visit(IGeometry g, int index) throws VisitException {
160
                if(g == null)
161
                        return;
162
                IFeature solution = null;
163
                visitor.setQueryGeometry(g.toJTSGeometry());
164
                try {
165
                        if(onlySecondLyrSelection)
166
                                visitor.setSelection(targetRecordset.getSelection());        
167
                        strategy.process(visitor, g.getBounds2D());
168
                        solution = createFeature(g, index, visitor.getNumIntersections());
169
                        featureProcessor.processFeature(solution);
170
                        resetFunctions();
171
                } catch (DriverException e) {
172
                        throw new VisitException(
173
                                        "Error al buscar las intersecciones de una geometria durante un spatial join");
174
                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
175
                        throw new VisitException(
176
                        "Error al buscar las intersecciones de una geometria durante un spatial join");
177
                }
178
        }
179
        
180
        public String getProcessDescription() {
181
                return "Spatial joining by intersects criteria";
182
        }
183
        
184
        public void resetFunctions(){
185
                Iterator fieldsIt = fields_sumarizeFunc.keySet().iterator();
186
                while (fieldsIt.hasNext()) {
187
                        String field = (String) fieldsIt.next();
188
                        SummarizationFunction[] functions = 
189
                                (SummarizationFunction[]) fields_sumarizeFunc.get(field);
190
                        for (int i = 0; i < functions.length; i++) {
191
                                functions[i].reset();
192
                        }// for
193
                }// while
194
        }
195
        
196
        
197
        private IFeature createFeature(IGeometry g, int index, int numIntersections)
198
                        throws com.hardcode.gdbms.engine.data.driver.DriverException {
199
                IFeature solution = null;
200
                int numSourceFields = this.sourceRecordset.getFieldCount();
201
                ArrayList values = new ArrayList();
202
                for (int i = 0; i < numSourceFields; i++) {
203
                        values.add(sourceRecordset.getFieldValue(index, i));
204
                }
205
                //target layer
206
                Iterator fieldsIt = fields_sumarizeFunc.keySet().iterator();
207
                while (fieldsIt.hasNext()) {
208
                        String field = (String) fieldsIt.next();
209
                        SummarizationFunction[] functions = 
210
                                (SummarizationFunction[]) fields_sumarizeFunc.get(field);
211
                        for (int i = 0; i < functions.length; i++) {
212
                                values.add(functions[i].getSumarizeValue());
213
                        }// for
214
                }// while
215
                //values.add(ValueFactory.createValue(numIntersections));
216
                Value[] attrs = new Value[values.size()];
217
                values.toArray(attrs);
218
                solution = FeatureFactory.createFeature(attrs, g);
219
                return solution;
220
        }
221

    
222
        public void stop(FLayer layer) {
223
                featureProcessor.finish();
224

    
225
        }
226

    
227
        public boolean start(FLayer layer) {
228
                        try {
229
                                this.featureProcessor.start();
230
                        } catch (EditionException e) {
231
                                return false;
232
                        }
233
                        return true;
234
        }
235
        
236
        
237
        public ILayerDefinition getResultLayerDefinition(){
238
                if(this.resultLayerDefinition == null){
239
                        ArrayList fields = new ArrayList();
240
                        resultLayerDefinition = new SHPLayerDefinition();
241
                        //result layer will be exactly similar to firstLayer with
242
                        //new attributes
243
                        try {
244
                                resultLayerDefinition.setShapeType(sourceLayer.getShapeType());
245
                        } catch (DriverException e) {
246
                                // TODO Auto-generated catch block
247
                                e.printStackTrace();
248
                        }
249
                        
250
                        //first layer attributes
251
                        int numFields = 0;
252
                        try {
253
                                numFields = sourceRecordset.getFieldCount();
254
                        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
255
                                // TODO Auto-generated catch block
256
                                e.printStackTrace();
257
                        }
258
                        FieldDescription fieldDesc = null;
259
                        for(int i = 0; i < numFields; i++){
260
                                fieldDesc = new FieldDescription();
261
                                try {
262
                                        fieldDesc.setFieldName(sourceRecordset.getFieldName(i));
263
                                        fieldDesc.setFieldType(sourceRecordset.getFieldType(i));
264
                                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
265
                                        // TODO Auto-generated catch block
266
                                        e.printStackTrace();
267
                                }
268
                                fields.add(fieldDesc);
269
                        }
270
                        //target layer attributes
271
                        
272
                        Iterator fieldsIt = fields_sumarizeFunc.keySet().iterator();
273
                        while(fieldsIt.hasNext()){
274
                                String field = (String) fieldsIt.next();
275
                                SummarizationFunction[] functions =
276
                                        (SummarizationFunction[]) fields_sumarizeFunc.get(field);
277
                                for(int i = 0; i < functions.length; i++){
278
                                        fieldDesc = new FieldDescription();
279
                                        fieldDesc.setFieldLength(10);
280
                                        fieldDesc.setFieldDecimalCount(4);
281
                                        //to avoid truncation of field names (f.example shp)
282
                                        //we only catch three first letters
283
                                        String shortName = null;
284
                                        if(field.length() > 3)
285
                                                shortName = field.substring(0,3);
286
                                        else
287
                                                shortName = field;
288
                                        fieldDesc.setFieldName(
289
                                                        shortName + "_" + functions[i].toString());
290
                                        fieldDesc.setFieldType(XTypes.DOUBLE);
291
                                        
292
                                        fields.add(fieldDesc);
293
                                }//for
294
                        }//while
295
                        
296
                        FieldDescription[] fieldsDesc = null;
297
                        if(fields.size() == 0){
298
                                fieldsDesc = new FieldDescription[0];
299
                        }else{
300
                                fieldsDesc = new FieldDescription[fields.size()];
301
                                fields.toArray(fieldsDesc);
302
                        }
303
                        resultLayerDefinition.setFieldsDesc(fieldsDesc);
304
                }//if result == null
305
                return resultLayerDefinition;
306
        }
307

    
308
        public void setFeatureProcessor(FeatureProcessor processor) {
309
                this.featureProcessor = processor;
310
        }
311

    
312
        public void setCancelableStrategy(Strategy secondLyrStrategy) {
313
                this.strategy = secondLyrStrategy;
314
        }
315

    
316
        public void setOnlySecondLyrSelection(boolean onlySecondLayerSelection) {
317
                this.onlySecondLyrSelection = onlySecondLayerSelection;
318
        }
319

    
320
}