Statistics
| Revision:

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

History | View | Annotate | Download (10.5 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 12954 2007-08-07 16:10:20Z azabala $
47
 * $Log$
48
 * Revision 1.3  2007-08-07 16:07:06  azabala
49
 * centrilizing JTS in JTSFacade and allowing all geometry types (not only Polygon)
50
 *
51
 * Revision 1.2  2007/03/06 16:47:58  caballero
52
 * Exceptions
53
 *
54
 * Revision 1.1  2006/06/20 18:20:45  azabala
55
 * first version in cvs
56
 *
57
 * Revision 1.3  2006/06/08 18:25:20  azabala
58
 * modificaciones para admitir capas de shapeType MULTI
59
 *
60
 * Revision 1.2  2006/06/02 18:21:28  azabala
61
 * *** empty log message ***
62
 *
63
 * Revision 1.1  2006/05/24 21:10:40  azabala
64
 * primera version en cvs despues de refactoring orientado a crear un framework extensible de geoprocessing
65
 *
66
 * Revision 1.6  2006/05/01 19:12:17  azabala
67
 * optimizaciones haciendo uso de strategy.process(rectangle) (el bitset se estaba recorriendo secuencial, no aleatorio)
68
 *
69
 * Revision 1.5  2006/03/26 20:03:06  azabala
70
 * *** empty log message ***
71
 *
72
 * Revision 1.4  2006/03/23 21:04:36  azabala
73
 * *** empty log message ***
74
 *
75
 * Revision 1.3  2006/03/21 19:29:18  azabala
76
 * *** empty log message ***
77
 *
78
 * Revision 1.2  2006/03/07 21:01:33  azabala
79
 * *** empty log message ***
80
 *
81
 * Revision 1.1  2006/03/06 19:48:39  azabala
82
 * *** empty log message ***
83
 *
84
 * Revision 1.2  2006/03/05 19:58:47  azabala
85
 * *** empty log message ***
86
 *
87
 * Revision 1.1  2006/02/26 20:54:38  azabala
88
 * *** empty log message ***
89
 *
90
 *
91
 */
92
package com.iver.cit.gvsig.geoprocess.impl.intersection.fmap;
93

    
94
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
95
import com.hardcode.gdbms.engine.data.driver.DriverException;
96
import com.hardcode.gdbms.engine.values.Value;
97
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileReadException;
98
import com.iver.cit.gvsig.exceptions.visitors.ProcessVisitorException;
99
import com.iver.cit.gvsig.exceptions.visitors.StartVisitorException;
100
import com.iver.cit.gvsig.exceptions.visitors.VisitorException;
101
import com.iver.cit.gvsig.fmap.core.IFeature;
102
import com.iver.cit.gvsig.fmap.core.IGeometry;
103
import com.iver.cit.gvsig.fmap.core.v02.FConverter;
104
import com.iver.cit.gvsig.fmap.drivers.ILayerDefinition;
105
import com.iver.cit.gvsig.fmap.layers.FLayer;
106
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
107
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
108
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
109
import com.iver.cit.gvsig.fmap.layers.layerOperations.VectorialData;
110
import com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor;
111
import com.iver.cit.gvsig.fmap.operations.strategies.Strategy;
112
import com.iver.cit.gvsig.geoprocess.core.fmap.FeatureFactory;
113
import com.iver.cit.gvsig.geoprocess.core.fmap.FeatureProcessor;
114
import com.iver.cit.gvsig.geoprocess.core.fmap.XTypes;
115
import com.iver.cit.gvsig.geoprocess.core.util.JTSFacade;
116
import com.vividsolutions.jts.geom.Geometry;
117
import com.vividsolutions.jts.geom.MultiPolygon;
118
import com.vividsolutions.jts.geom.Polygon;
119
import com.vividsolutions.jts.precision.EnhancedPrecisionOp;
120

    
121
/**
122
 * Computes intersections of features of a layer with features of an overlay
123
 * layer.
124
 *
125
 * @author azabala
126
 *
127
 * FIXME Crear una clase abstracta: OverlayVisitor, pues todos los visitors de
128
 * un overlay son muy parecidos.
129
 *
130
 */
131
public class IntersectVisitor implements FeatureVisitor {
132

    
133
        /**
134
         * Allows to get attributes of first layer features which is being
135
         * intersected
136
         */
137
        SelectableDataSource firstRs;
138

    
139
        /**
140
         * Number of fields of first layer recordset
141
         */
142
        int numFieldsA;
143

    
144
        /**
145
         * looks for overlay features of the processed feauture by spatial criteria
146
         * (queryByEnvelope)
147
         */
148
        FLyrVect overlayLayer;
149

    
150
        /**
151
         * Strategy to process overlay layer
152
         */
153
        Strategy strategy;
154

    
155
        /**
156
         * flag to decide if process onlye features selected in overlay layer.
157
         */
158
        boolean onlyOverlaySelection;
159

    
160
        /**
161
         * Gets attributes of second layer features which are being intersected.
162
         */
163
        SelectableDataSource secondRs;
164

    
165
        /**
166
         * Number of fields of second layer recordset
167
         */
168
        int numFieldsB;
169

    
170
        /**
171
         * Schema of the result layer
172
         */
173
        ILayerDefinition layerDefinition;
174

    
175
        /**
176
         * It processes features resulting of intersetions. It could saves them in
177
         * persistent datastore, caching them, reprocess them, reprojects them, etc.
178
         */
179
        FeatureProcessor featureProcessor;
180

    
181
        /**
182
         * Constructor
183
         * @param overlayLayer
184
         * @param processor
185
         * @param strategy
186
         * @param onlyOverlaySelection
187
         * @throws ReadDriverException
188
         * @throws DriverException
189
         * @throws com.iver.cit.gvsig.fmap.DriverException
190
         */
191
        public IntersectVisitor(FLyrVect overlayLayer, FeatureProcessor processor,
192
                        Strategy strategy, boolean onlyOverlaySelection) throws ReadDriverException{
193
                this.overlayLayer = overlayLayer;
194
                this.featureProcessor = processor;
195
                this.strategy = strategy;
196
                this.onlyOverlaySelection = onlyOverlaySelection;
197
                secondRs = overlayLayer.getRecordset();
198
                numFieldsB = secondRs.getFieldCount();
199
        }
200

    
201
        public void visit(IGeometry g, final int index) throws VisitorException, ProcessVisitorException {
202
                if(g == null)
203
                        return;
204
                
205
//                if(g.getGeometryType() != XTypes.POLYGON &&
206
//                                g.getGeometryType() != XTypes.MULTI)
207
//                        return;
208
                
209
                final Geometry firstJts = g.toJTSGeometry();
210
                final boolean onlyOverlay = onlyOverlaySelection;
211
                try {
212
                        strategy.process(new FeatureVisitor(){
213
                                public void visit(IGeometry g, int indexOverlay) throws VisitorException, ProcessVisitorException {
214
                                        if(onlyOverlay){
215
                                                try {
216
                                                        if(!overlayLayer.getRecordset().getSelection().get(index))
217
                                                                return;
218
                                                } catch (ReadDriverException e) {
219
                                                        throw new ProcessVisitorException(overlayLayer.getName(),e,
220
                                                        "Error en interseccion: verificando si un posible overlay esta seleccionado");
221
                                                }//geometry g is not selected
222
                                        }
223
                                        
224
                                        
225
//                                        if(g.getGeometryType() != XTypes.POLYGON &&
226
//                                                        g.getGeometryType() != XTypes.MULTI)
227
//                                                return;
228
                                        
229
                                        
230
                                        Geometry overlayJts = g.toJTSGeometry();
231
                                        if (firstJts.intersects(overlayJts)) {
232
                                                
233
//                                                Geometry newGeoJts = EnhancedPrecisionOp.intersection(firstJts, overlayJts);
234
//                                                if (!(newGeoJts instanceof Polygon)
235
//                                                                && !(newGeoJts instanceof MultiPolygon)) {
236
//                                                        // intersection of adjacent polygons is a linestring
237
//                                                        // but we are not interested in it
238
//                                                        return;
239
//                                                }
240
                                                Geometry newGeoJts = JTSFacade.intersection(firstJts, overlayJts);
241
                                                if(JTSFacade.checkNull(newGeoJts))
242
                                                        return;
243
                                                
244
                                                IFeature intersectionFeature;
245
                                                try {
246
                                                        intersectionFeature = createFeature(newGeoJts,
247
                                                                        index, indexOverlay);
248
                                                } catch (ReadDriverException e) {
249
                                                        throw new ProcessVisitorException(overlayLayer.getName(),e,
250
                                                                        "Error al crear el feature resultante de la interseccion");
251
                                                }
252
                                                featureProcessor.processFeature(intersectionFeature);
253
                                        }// if intersects
254
                                }
255

    
256
                                public String getProcessDescription() {
257
                                        return "Computing intersections of a polygon with its adjacents";
258
                                }
259
                                public void stop(FLayer layer) throws VisitorException {
260
                                }
261
                                public boolean start(FLayer layer) throws StartVisitorException {
262
                                        return true;
263
                                }},g.getBounds2D());
264

    
265
                } catch (ReadDriverException e) {
266
                        throw new ProcessVisitorException(overlayLayer.getName(),e,
267
                                        "Error buscando los overlays que intersectan con un feature");
268
                } catch (ExpansionFileReadException e) {
269
                        throw new ProcessVisitorException(overlayLayer.getName(),e,
270
                                        "Error buscando los overlays que intersectan con un feature");
271
                }
272
        }
273

    
274
        public void stop(FLayer layer) throws VisitorException {
275
                featureProcessor.finish();
276
        }
277

    
278
        public boolean start(FLayer layer) throws StartVisitorException {
279
                if (layer instanceof AlphanumericData && layer instanceof VectorialData) {
280
                        try {
281
                                this.firstRs = ((AlphanumericData) layer).getRecordset();
282
                                numFieldsA = firstRs.getFieldCount();
283
                                this.featureProcessor.start();
284
                        } catch (ReadDriverException e) {
285
                                return false;
286
                        }
287
                        return true;
288
                }
289
                return false;
290
        }
291

    
292

    
293
        /**
294
         * TODO Meter este metodo en FeatureFactory, porque lo estoy copypasteando
295
         * en todos los Visitor
296
         *
297
         * FIXME El esquema es resultado de la mezcla de los esquemas de dos capas
298
         * ?Como saber qu? campo de ILayerDefinition se toma de firstRs y cual de secondRs?
299
         * Mirando DefinitionUtils, vemos que primero se recorre firstLayer,
300
         * y luego secondLayer. REVISAR
301
         */
302
        private IFeature createFeature(Geometry jtsGeometry, int firstLayerIndex,
303
                        int overlayLayerIndex) throws ReadDriverException {
304
                IFeature solution = null;
305
                IGeometry intersectGeometry = FConverter.jts_to_igeometry(jtsGeometry);
306

    
307

    
308
                Value[] featureAttr = new Value[numFieldsA + numFieldsB];
309
                for (int indexField = 0; indexField < numFieldsA; indexField++) {
310
                        featureAttr[indexField] = firstRs.getFieldValue(firstLayerIndex,
311
                                        indexField);
312
                }
313
                for (int indexFieldB = 0; indexFieldB < numFieldsB; indexFieldB++) {
314
                        featureAttr[numFieldsA + indexFieldB] = secondRs.getFieldValue(
315
                                        overlayLayerIndex, indexFieldB);
316
                }
317
                solution = FeatureFactory.createFeature(featureAttr, intersectGeometry);
318
                return solution;
319

    
320
        }
321

    
322
        public void setFeatureProcessor(FeatureProcessor featureProcessor) {
323
                this.featureProcessor = featureProcessor;
324
        }
325

    
326
        public String getProcessDescription() {
327
                return "Computing intersections between two layers";
328
        }
329

    
330
        public ILayerDefinition getLayerDefinition() {
331
                return layerDefinition;
332
        }
333

    
334
        public void setLayerDefinition(ILayerDefinition layerDefinition) {
335
                this.layerDefinition = layerDefinition;
336
        }
337

    
338
}