Statistics
| Revision:

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

History | View | Annotate | Download (9.13 KB)

1
/*
2
 * Created on 21-feb-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: IntersectVisitor.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:10:40  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:12:17  azabala
55
 * optimizaciones haciendo uso de strategy.process(rectangle) (el bitset se estaba recorriendo secuencial, no aleatorio)
56
 *
57
 * Revision 1.5  2006/03/26 20:03:06  azabala
58
 * *** empty log message ***
59
 *
60
 * Revision 1.4  2006/03/23 21:04:36  azabala
61
 * *** empty log message ***
62
 *
63
 * Revision 1.3  2006/03/21 19:29:18  azabala
64
 * *** empty log message ***
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.2  2006/03/05 19:58:47  azabala
73
 * *** empty log message ***
74
 *
75
 * Revision 1.1  2006/02/26 20:54:38  azabala
76
 * *** empty log message ***
77
 *
78
 *
79
 */
80
package com.iver.cit.gvsig.geoprocess.intersection.fmap;
81

    
82
import com.hardcode.gdbms.engine.data.driver.DriverException;
83
import com.hardcode.gdbms.engine.values.Value;
84
import com.iver.cit.gvsig.fmap.core.IFeature;
85
import com.iver.cit.gvsig.fmap.core.IGeometry;
86
import com.iver.cit.gvsig.fmap.core.v02.FConverter;
87
import com.iver.cit.gvsig.fmap.drivers.ILayerDefinition;
88
import com.iver.cit.gvsig.fmap.edition.EditionException;
89
import com.iver.cit.gvsig.fmap.layers.FLayer;
90
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
91
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
92
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
93
import com.iver.cit.gvsig.fmap.layers.layerOperations.VectorialData;
94
import com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor;
95
import com.iver.cit.gvsig.fmap.operations.strategies.Strategy;
96
import com.iver.cit.gvsig.fmap.operations.strategies.VisitException;
97
import com.iver.cit.gvsig.geoprocess.core.fmap.FeatureFactory;
98
import com.iver.cit.gvsig.geoprocess.core.fmap.FeatureProcessor;
99
import com.vividsolutions.jts.geom.Geometry;
100
import com.vividsolutions.jts.geom.MultiPolygon;
101
import com.vividsolutions.jts.geom.Polygon;
102
import com.vividsolutions.jts.precision.EnhancedPrecisionOp;
103

    
104
/**
105
 * Computes intersections of features of a layer with features of an overlay
106
 * layer.
107
 * 
108
 * @author azabala
109
 * 
110
 * FIXME Crear una clase abstracta: OverlayVisitor, pues todos los visitors de
111
 * un overlay son muy parecidos.
112
 * 
113
 */
114
public class IntersectVisitor implements FeatureVisitor {
115

    
116
        /**
117
         * Allows to get attributes of first layer features which is being
118
         * intersected
119
         */
120
        SelectableDataSource firstRs;
121

    
122
        /**
123
         * Number of fields of first layer recordset
124
         */
125
        int numFieldsA;
126

    
127
        /**
128
         * looks for overlay features of the processed feauture by spatial criteria
129
         * (queryByEnvelope)
130
         */
131
        FLyrVect overlayLayer;
132

    
133
        /**
134
         * Strategy to process overlay layer
135
         */
136
        Strategy strategy;
137

    
138
        /**
139
         * flag to decide if process onlye features selected in overlay layer.
140
         */
141
        boolean onlyOverlaySelection;
142

    
143
        /**
144
         * Gets attributes of second layer features which are being intersected.
145
         */
146
        SelectableDataSource secondRs;
147

    
148
        /**
149
         * Number of fields of second layer recordset
150
         */
151
        int numFieldsB;
152

    
153
        /**
154
         * Schema of the result layer
155
         */
156
        ILayerDefinition layerDefinition;
157

    
158
        /**
159
         * It processes features resulting of intersetions. It could saves them in
160
         * persistent datastore, caching them, reprocess them, reprojects them, etc.
161
         */
162
        FeatureProcessor featureProcessor;
163
        
164
        /**
165
         * Constructor
166
         * @param overlayLayer
167
         * @param processor
168
         * @param strategy
169
         * @param onlyOverlaySelection
170
         * @throws DriverException
171
         * @throws com.iver.cit.gvsig.fmap.DriverException
172
         */
173
        public IntersectVisitor(FLyrVect overlayLayer, FeatureProcessor processor,
174
                        Strategy strategy, boolean onlyOverlaySelection)
175
                        throws DriverException, com.iver.cit.gvsig.fmap.DriverException {
176
                this.overlayLayer = overlayLayer;
177
                this.featureProcessor = processor;
178
                this.strategy = strategy;
179
                this.onlyOverlaySelection = onlyOverlaySelection;
180
                secondRs = overlayLayer.getRecordset();
181
                numFieldsB = secondRs.getFieldCount();
182
        }
183

    
184
        public void visit(IGeometry g, final int index) throws VisitException {
185
                if(g == null)
186
                        return;
187
                final Geometry firstJts = g.toJTSGeometry();
188
                final boolean onlyOverlay = onlyOverlaySelection;
189
                try {
190
                        strategy.process(new FeatureVisitor(){
191
                                public void visit(IGeometry g, int indexOverlay) throws VisitException {
192
                                        if(onlyOverlay){
193
                                                try {
194
                                                        if(!overlayLayer.getRecordset().getSelection().get(index))
195
                                                                return;
196
                                                } catch (com.iver.cit.gvsig.fmap.DriverException e) {
197
                                                        throw new VisitException(
198
                                                        "Error en interseccion: verificando si un posible overlay esta seleccionado", e);
199
                                                }//geometry g is not selected
200
                                        }                                
201
                                        Geometry overlayJts = g.toJTSGeometry();                                        
202
                                        if (firstJts.intersects(overlayJts)) {
203
                                                Geometry newGeoJts = EnhancedPrecisionOp.intersection(firstJts, overlayJts);
204
                                                if (!(newGeoJts instanceof Polygon)
205
                                                                && !(newGeoJts instanceof MultiPolygon)) {
206
                                                        // intersection of adjacent polygons is a linestring
207
                                                        // but we are not interested in it
208
                                                        return;
209
                                                }
210
                                                IFeature intersectionFeature;
211
                                                try {
212
                                                        intersectionFeature = createFeature(newGeoJts,
213
                                                                        index, indexOverlay);
214
                                                } catch (DriverException e) {
215
                                                        throw new VisitException(
216
                                                                        "Error al crear el feature resultante de la interseccion");
217
                                                }
218
                                                featureProcessor.processFeature(intersectionFeature);
219
                                        }// if intersects        
220
                                }
221

    
222
                                public String getProcessDescription() {
223
                                        return "Computing intersections of a polygon with its adjacents";
224
                                }
225
                                public void stop(FLayer layer) {
226
                                }
227
                                public boolean start(FLayer layer) {
228
                                        return true;
229
                                }},g.getBounds2D());
230
                        
231
                } catch (com.iver.cit.gvsig.fmap.DriverException e) {
232
                        throw new VisitException(
233
                                        "Error buscando los overlays que intersectan con un feature");
234
                } 
235
        }
236

    
237
        public void stop(FLayer layer) {
238
                featureProcessor.finish();
239
        }
240

    
241
        public boolean start(FLayer layer) {
242
                if (layer instanceof AlphanumericData && layer instanceof VectorialData) {
243
                        try {
244
                                this.firstRs = ((AlphanumericData) layer).getRecordset();
245
                                numFieldsA = firstRs.getFieldCount();
246
                                this.featureProcessor.start();
247
                        } catch (com.iver.cit.gvsig.fmap.DriverException e) {
248
                                return false;
249
                        } catch (DriverException e) {
250
                                return false;
251
                        } catch (EditionException e) {
252
                                return false;
253
                        }
254
                        return true;
255
                }
256
                return false;
257
        }
258
        
259

    
260
        /**
261
         * TODO Meter este metodo en FeatureFactory, porque lo estoy copypasteando
262
         * en todos los Visitor
263
         * 
264
         * FIXME El esquema es resultado de la mezcla de los esquemas de dos capas
265
         * ?Como saber qu? campo de ILayerDefinition se toma de firstRs y cual de secondRs?
266
         * Mirando DefinitionUtils, vemos que primero se recorre firstLayer,
267
         * y luego secondLayer. REVISAR
268
         */
269
        private IFeature createFeature(Geometry jtsGeometry, int firstLayerIndex,
270
                        int overlayLayerIndex) throws DriverException {
271
                IFeature solution = null;
272
                IGeometry intersectGeometry = FConverter.jts_to_igeometry(jtsGeometry);
273
                
274
                
275
                Value[] featureAttr = new Value[numFieldsA + numFieldsB];
276
                for (int indexField = 0; indexField < numFieldsA; indexField++) {
277
                        featureAttr[indexField] = firstRs.getFieldValue(firstLayerIndex,
278
                                        indexField);
279
                }
280
                for (int indexFieldB = 0; indexFieldB < numFieldsB; indexFieldB++) {
281
                        featureAttr[numFieldsA + indexFieldB] = secondRs.getFieldValue(
282
                                        overlayLayerIndex, indexFieldB);
283
                }
284
                solution = FeatureFactory.createFeature(featureAttr, intersectGeometry);
285
                return solution;
286
                
287
        }
288

    
289
        public void setFeatureProcessor(FeatureProcessor featureProcessor) {
290
                this.featureProcessor = featureProcessor;
291
        }
292

    
293
        public String getProcessDescription() {
294
                return "Computing intersections between two layers";
295
        }
296

    
297
        public ILayerDefinition getLayerDefinition() {
298
                return layerDefinition;
299
        }
300

    
301
        public void setLayerDefinition(ILayerDefinition layerDefinition) {
302
                this.layerDefinition = layerDefinition;
303
        }
304

    
305
}