Statistics
| Revision:

root / trunk / extensions / extGeoProcessing / src / com / iver / gvsig / geoprocessing / impl / intersection / IntersectVisitor.java @ 4512

History | View | Annotate | Download (7.57 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 4512 2006-03-21 19:30:56Z azabala $
47
* $Log$
48
* Revision 1.3  2006-03-21 19:29:18  azabala
49
* *** empty log message ***
50
*
51
* Revision 1.2  2006/03/07 21:01:33  azabala
52
* *** empty log message ***
53
*
54
* Revision 1.1  2006/03/06 19:48:39  azabala
55
* *** empty log message ***
56
*
57
* Revision 1.2  2006/03/05 19:58:47  azabala
58
* *** empty log message ***
59
*
60
* Revision 1.1  2006/02/26 20:54:38  azabala
61
* *** empty log message ***
62
*
63
*
64
*/
65
package com.iver.gvsig.geoprocessing.impl.intersection;
66

    
67
import com.hardcode.gdbms.engine.data.driver.DriverException;
68
import com.hardcode.gdbms.engine.values.Value;
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.core.v02.FConverter;
72
import com.iver.cit.gvsig.fmap.edition.EditionException;
73
import com.iver.cit.gvsig.fmap.layers.FBitSet;
74
import com.iver.cit.gvsig.fmap.layers.FLayer;
75
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
76
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
77
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
78
import com.iver.cit.gvsig.fmap.layers.layerOperations.VectorialData;
79
import com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor;
80
import com.iver.cit.gvsig.fmap.operations.strategies.Strategy;
81
import com.iver.cit.gvsig.fmap.operations.strategies.VisitException;
82
import com.iver.gvsig.geoprocessing.model.FeatureProcessor;
83
import com.iver.gvsig.geoprocessing.schemabuilder.FeatureFactory;
84
import com.vividsolutions.jts.geom.Geometry;
85
import com.vividsolutions.jts.geom.Polygon;
86
/**
87
 * Computes intersections of features of a layer with features of an overlay
88
 * layer layer.
89
 * 
90
 * @author azabala
91
 * 
92
 * FIXME Crear una clase abstracta: OverlayVisitor, pues todos los visitors
93
 * de un overlay son muy parecidos.
94
 * 
95
 */
96
public class IntersectVisitor implements FeatureVisitor {
97
        
98
        /**
99
         * Allows to get attributes of first layer features which is being
100
         * intersected
101
         */
102
        SelectableDataSource firstRs;
103
        /**
104
         * Number of fields of first layer recordset
105
         */
106
        int numFieldsA;
107
        
108
        
109
        /**
110
         * looks for overlay features of the processed feauture by spatial criteria
111
         * (queryByEnvelope)
112
         */
113
        FLyrVect overlayLayer;
114
        /**
115
         * Strategy to process overlay layer
116
         */
117
        Strategy strategy;
118
        /**
119
         * flag to decide if process onlye features selected
120
         * in overlay layer.
121
         */
122
        boolean onlyOverlaySelection;
123
        
124
        /**
125
         * Gets attributes of second layer features which are being intersected.
126
         */
127
        SelectableDataSource secondRs;
128
        
129
        /**
130
         * Number of fields of second layer recordset
131
         */
132
        int numFieldsB;
133
        
134
        /**
135
         * It processes features resulting of intersetions.
136
         * It could saves them in persistent datastore, caching 
137
         * them, reprocess them, reprojects them, etc.
138
         */
139
        FeatureProcessor featureProcessor;
140

    
141
        public IntersectVisitor(FLyrVect overlayLayer, 
142
                        FeatureProcessor processor, 
143
                        Strategy strategy,
144
                        boolean onlyOverlaySelection) 
145
                                                                                                        throws DriverException, com.iver.cit.gvsig.fmap.DriverException{
146
                this.overlayLayer = overlayLayer;
147
                this.featureProcessor = processor;
148
                this.strategy = strategy;
149
                this.onlyOverlaySelection = onlyOverlaySelection;
150
                secondRs = overlayLayer.getRecordset();
151
                numFieldsB = secondRs.getFieldCount();
152
        }
153
        
154
        
155
        public void visit(IGeometry g, final int index) throws VisitException {
156
                
157
                final Geometry firstJts = g.toJTSGeometry();
158
                try {
159
                        //FIXME Change this and use FeatureIterator or add process(Rectangle2D, FeatureVisitor)
160
                        //to Strategy
161
                        FBitSet overlaps = overlayLayer.queryByRect(g.getBounds2D());
162
                        if(onlyOverlaySelection){
163
                                overlaps.and(overlayLayer.getRecordset().getSelection());
164
                        }
165
                        strategy.process(new FeatureVisitor(){
166
                                public void visit(IGeometry g, int indexOverlay) throws VisitException {
167
                                        // FIXME must we cache jts features?
168
                                        Geometry overlayJts = g.toJTSGeometry();
169
                                        if (firstJts.intersects(overlayJts)) {
170
                                                Geometry newGeoJts = firstJts.intersection(overlayJts);
171
                                                if (!(newGeoJts instanceof Polygon)) {
172
                                                        // intersection of adjacent polygons is a linestring
173
                                                        //but we are not interested in it
174
                                                        return;
175
                                                }
176
                                                IFeature intersectionFeature;
177
                                                try {
178
                                                        intersectionFeature = createFeature(newGeoJts, index, indexOverlay);
179
                                                } catch (DriverException e) {
180
                                                        throw new VisitException("Error al crear el feature resultante de la interseccion");
181
                                                }
182
                                                featureProcessor.processFeature(intersectionFeature);
183
                                        }// if intersects
184
                                }
185
                                public void stop(FLayer layer) {
186
                                }
187
                                public boolean start(FLayer layer) {
188
                                        return true;
189
                                }
190
                                
191
                                public String getProcessDescription() {
192
                                        return "Computing intersections of a polygon with its adjacents";
193
                                }
194
                        }, overlaps);        
195
                                
196
                } catch (com.iver.cit.gvsig.fmap.DriverException e) {
197
                        throw new VisitException("Error buscando los overlays que intersectan con un feature");
198
                }
199
        }
200

    
201
        public void stop(FLayer layer) {
202
                featureProcessor.finish();
203
        }
204

    
205
        public boolean start(FLayer layer) {
206
                if(layer instanceof AlphanumericData && layer instanceof VectorialData){
207
                        try {
208
                                this.firstRs = ((AlphanumericData)layer).getRecordset();
209
                                numFieldsA = firstRs.getFieldCount();
210
                                this.featureProcessor.start();
211
                        } catch (com.iver.cit.gvsig.fmap.DriverException e) {
212
                                return false;
213
                        } catch (DriverException e) {
214
                                return false;
215
                        } catch (EditionException e) {
216
                                return false;
217
                        }
218
                        return true;
219
                }
220
                return false;
221
        }
222
        
223
        /**
224
         * FIXME Meter este metodo en FeatureFactory, porque lo estoy copypasteando
225
         * en todos los Visitor
226
         */
227
        private IFeature createFeature(Geometry jtsGeometry, 
228
                                                                        int firstLayerIndex, 
229
                                                                        int overlayLayerIndex) throws DriverException{
230
                IFeature solution = null;
231
                IGeometry intersectGeometry = FConverter.jts_to_igeometry(jtsGeometry);
232
                Value[] featureAttr = new Value[numFieldsA + numFieldsB];
233
                for (int indexField = 0; indexField < numFieldsA; indexField++) {
234
                                featureAttr[indexField] = firstRs.getFieldValue(firstLayerIndex, 
235
                                                                                                                                        indexField);
236
                }
237
                for (int indexFieldB = 0; indexFieldB < numFieldsB; indexFieldB++) {
238
                                featureAttr[numFieldsA + indexFieldB] = secondRs.getFieldValue(overlayLayerIndex, indexFieldB);
239
                }
240
                solution = FeatureFactory.createFeature(featureAttr, intersectGeometry);
241
                return solution;
242
        }
243

    
244

    
245
        public void setFeatureProcessor(FeatureProcessor featureProcessor) {
246
                this.featureProcessor = featureProcessor;
247
        }
248
        
249
        public String getProcessDescription() {
250
                return "Computing intersections between two layers";
251
        }
252

    
253
}
254