Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extGeoProcessing / src / com / iver / cit / gvsig / geoprocess / impl / spatialjoin / fmap / IntersectsFinderFeatureVisitor.java @ 10626

History | View | Annotate | Download (6.31 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: IntersectsFinderFeatureVisitor.java 10626 2007-03-06 16:55:54Z caballero $
47
 * $Log$
48
 * Revision 1.2  2007-03-06 16:47:58  caballero
49
 * Exceptions
50
 *
51
 * Revision 1.1  2006/06/20 18:20:45  azabala
52
 * first version in cvs
53
 *
54
 * Revision 1.2  2006/06/02 18:21:28  azabala
55
 * *** empty log message ***
56
 *
57
 * Revision 1.1  2006/05/24 21:09:47  azabala
58
 * primera version en cvs despues de refactoring orientado a crear un framework extensible de geoprocessing
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.impl.spatialjoin.fmap;
72

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

    
78
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
79
import com.hardcode.gdbms.engine.values.NumericValue;
80
import com.hardcode.gdbms.engine.values.Value;
81
import com.iver.cit.gvsig.exceptions.visitors.ProcessVisitorException;
82
import com.iver.cit.gvsig.exceptions.visitors.StartVisitorException;
83
import com.iver.cit.gvsig.exceptions.visitors.VisitorException;
84
import com.iver.cit.gvsig.fmap.core.IGeometry;
85
import com.iver.cit.gvsig.fmap.layers.FBitSet;
86
import com.iver.cit.gvsig.fmap.layers.FLayer;
87
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
88
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
89
import com.iver.cit.gvsig.fmap.layers.layerOperations.VectorialData;
90
import com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor;
91
import com.iver.cit.gvsig.geoprocess.core.fmap.SummarizationFunction;
92
import com.vividsolutions.jts.geom.Geometry;
93
/**
94
 * Given a feature of a JOIN source layer,
95
 *  it looks for intersecting geometries of JOIN target layer and applies
96
 *  a sumarization function to its numeric fields.
97
 *
98
 * @author azabala
99
 *
100
 */
101
public class IntersectsFinderFeatureVisitor implements FeatureVisitor {
102
        /**
103
         * Reads atributes of target layer
104
         */
105
        SelectableDataSource targetRecordset;
106
        /**
107
         * Geometry of layer A whose intersections of layer B
108
         * we are looking for
109
         */
110
        private Geometry queryGeometry;
111
        /**
112
         * It has indexes of intersections
113
         */
114
        private List intersectIndexes = null;
115
        /**
116
         * Relates a set of sumarization functions with a numeric field
117
         * name
118
         */
119
        Map fields_sumarizationFun = null;
120
        /**
121
         * When its not null, has the selection of the source layer
122
         */
123
        private FBitSet selection;
124

    
125
        /**
126
         * Constructor from a given geometry
127
         * @param geometry geometry whose intersections we are looking for
128
         * @param functions sumarization functions for each numeric value of target layer
129
         */
130
        public IntersectsFinderFeatureVisitor(Geometry geometry, Map functions) {
131
                this.queryGeometry = geometry;
132
                this.fields_sumarizationFun = functions;
133
                intersectIndexes = new ArrayList();
134
        }
135

    
136
        public IntersectsFinderFeatureVisitor(Map functions) {
137
                this.fields_sumarizationFun = functions;
138
                intersectIndexes = new ArrayList();
139
        }
140

    
141
        public void visit(IGeometry g, int index) throws VisitorException, ProcessVisitorException {
142
                if(g == null)
143
                        return;
144
                if(selection != null){
145
                        if(! selection.get(index)){
146
                                //dont process feature because is not selected
147
                                return;
148
                        }
149
                }
150
                Geometry jtsGeo = g.toJTSGeometry();
151
                if (queryGeometry.intersects(jtsGeo)) {
152
                        intersectIndexes.add(new Integer(index));
153
                        try {
154
                                applySumarizeFunction(index);
155
                        } catch (ReadDriverException e) {
156
                                throw new ProcessVisitorException(targetRecordset.getName(),e,"Error al acceder a los atributos de la capa destino en un spatial join");
157
                        }
158
                }
159
        }
160

    
161
        public String getProcessDescription() {
162
                return "Looking intersects and sumarizing for a spatial join";
163
        }
164

    
165
        private void applySumarizeFunction(int recordIndex) throws ReadDriverException {
166
                Iterator fieldsIt = fields_sumarizationFun.keySet().iterator();
167
                while (fieldsIt.hasNext()) {
168
                        String field = (String) fieldsIt.next();
169
                        int fieldIndex = targetRecordset.getFieldIndexByName(field);
170
                        Value valToSumarize = targetRecordset.getFieldValue(recordIndex,
171
                                        fieldIndex);
172
                        SummarizationFunction[] functions =
173
                                (SummarizationFunction[]) fields_sumarizationFun.get(field);
174
                        for (int i = 0; i < functions.length; i++) {
175
                                functions[i].process((NumericValue) valToSumarize);
176
                        }// for
177
                }// while
178
        }
179

    
180
        public List getIntersectIndexes() {
181
                return intersectIndexes;
182
        }
183

    
184
        public int getNumIntersections() {
185
                return intersectIndexes.size();
186
        }
187

    
188
        public boolean hasFoundIntersections() {
189
                return intersectIndexes.size() > 0;
190
        }
191

    
192
        public void clearIntersections(){
193
                intersectIndexes.clear();
194
        }
195

    
196
        public void stop(FLayer layer) throws VisitorException {
197
        }
198

    
199
        public boolean start(FLayer layer) throws StartVisitorException {
200
                if (layer instanceof AlphanumericData && layer instanceof VectorialData) {
201
                        try {
202
                                targetRecordset = ((AlphanumericData) layer).getRecordset();
203
                        } catch (ReadDriverException e) {
204
                                return false;
205
                        }
206
                        return true;
207
                }
208
                return false;
209
        }
210

    
211
        public void setQueryGeometry(Geometry queryGeometry) {
212
                this.queryGeometry = queryGeometry;
213
        }
214

    
215
        public void setSelection(FBitSet selection) {
216
                this.selection = selection;
217
        }
218

    
219
}