root / trunk / extensions / extGeoProcessing / src / com / iver / cit / gvsig / geoprocess / spatialjoin / fmap / NearestSpatialJoinVisitor.java @ 5412
History | View | Annotate | Download (7.64 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: NearestSpatialJoinVisitor.java 5412 2006-05-24 21:15:07Z azabala $
|
47 |
* $Log$
|
48 |
* Revision 1.1 2006-05-24 21:09:47 azabala
|
49 |
* primera version en cvs despues de refactoring orientado a crear un framework extensible de geoprocessing
|
50 |
*
|
51 |
* Revision 1.6 2006/05/01 19:08:41 azabala
|
52 |
* documentacion, y algunos metodos pasados de private a "friendly" -sin modificador-
|
53 |
*
|
54 |
* Revision 1.5 2006/03/21 19:29:36 azabala
|
55 |
* *** empty log message ***
|
56 |
*
|
57 |
* Revision 1.4 2006/03/15 18:34:31 azabala
|
58 |
* *** empty log message ***
|
59 |
*
|
60 |
* Revision 1.3 2006/03/14 18:32:46 fjp
|
61 |
* Cambio con LayerDefinition para que sea compatible con la definici?n de tablas tambi?n.
|
62 |
*
|
63 |
* Revision 1.2 2006/03/07 21:01:33 azabala
|
64 |
* *** empty log message ***
|
65 |
*
|
66 |
* Revision 1.1 2006/03/06 19:48:39 azabala
|
67 |
* *** empty log message ***
|
68 |
*
|
69 |
* Revision 1.1 2006/03/05 19:59:47 azabala
|
70 |
* *** empty log message ***
|
71 |
*
|
72 |
*
|
73 |
*/
|
74 |
package com.iver.cit.gvsig.geoprocess.spatialjoin.fmap; |
75 |
|
76 |
import com.hardcode.gdbms.engine.values.Value; |
77 |
import com.iver.cit.gvsig.fmap.DriverException; |
78 |
import com.iver.cit.gvsig.fmap.core.IFeature; |
79 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
80 |
import com.iver.cit.gvsig.fmap.drivers.ILayerDefinition; |
81 |
import com.iver.cit.gvsig.fmap.edition.EditionException; |
82 |
import com.iver.cit.gvsig.fmap.layers.FLayer; |
83 |
import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
84 |
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource; |
85 |
import com.iver.cit.gvsig.fmap.operations.strategies.Strategy; |
86 |
import com.iver.cit.gvsig.fmap.operations.strategies.VisitException; |
87 |
import com.iver.cit.gvsig.geoprocess.core.fmap.DefinitionUtils; |
88 |
import com.iver.cit.gvsig.geoprocess.core.fmap.FeatureFactory; |
89 |
import com.iver.cit.gvsig.geoprocess.core.fmap.FeatureProcessor; |
90 |
import com.iver.cit.gvsig.geoprocess.core.fmap.GeoprocessException; |
91 |
|
92 |
/**
|
93 |
* This visitor implement Nearest Geometry Spatial Join.
|
94 |
*
|
95 |
* It is a particular case of 1-1 relationship
|
96 |
* (a feature of layer A always be related to a feature of layer B)
|
97 |
*
|
98 |
* If two features of layer B are at the same distance of a feature of layer A,
|
99 |
* we will took the first one analized.
|
100 |
*
|
101 |
* In this visitor we apply a secuential scaning strategy: given a geometry
|
102 |
* of layer A, we check distances with all geometries of layer B return
|
103 |
* the geometry at the min distance.
|
104 |
*
|
105 |
*
|
106 |
* @author azabala
|
107 |
*
|
108 |
*/
|
109 |
public class NearestSpatialJoinVisitor implements SpatialJoinVisitor { |
110 |
/**
|
111 |
* Needed to create layer definition
|
112 |
*/
|
113 |
FLyrVect sourceLayer; |
114 |
/**
|
115 |
* Reads data of features for the source layer
|
116 |
*/
|
117 |
SelectableDataSource sourceRecordset; |
118 |
|
119 |
/**
|
120 |
* Reads data of features for the target layer
|
121 |
*/
|
122 |
FLyrVect targetLayer; |
123 |
|
124 |
/**
|
125 |
* Recordset of this layer
|
126 |
*/
|
127 |
SelectableDataSource targetRecordset; |
128 |
|
129 |
/**
|
130 |
* Strategy to do querys against target Layer
|
131 |
*/
|
132 |
Strategy strategy; |
133 |
|
134 |
/**
|
135 |
* Processes results of dissolve operations (save them in a file, or cache
|
136 |
* them in memory, etc)
|
137 |
*/
|
138 |
FeatureProcessor featureProcessor; |
139 |
|
140 |
/**
|
141 |
* It looks for nearest feature to a given feature
|
142 |
*/
|
143 |
private NearestFeatureVisitor lookForNearestVisitor;
|
144 |
|
145 |
/**
|
146 |
* Schema of the result layer
|
147 |
*/
|
148 |
ILayerDefinition layerDefinition; |
149 |
/**
|
150 |
* flag to process only selections of second layer
|
151 |
*/
|
152 |
boolean onlySecondLayerSelection;
|
153 |
|
154 |
/**
|
155 |
* Constructor. It receives layer with which we want to do a spatial join
|
156 |
*
|
157 |
* @param targetRecordset
|
158 |
* @throws DriverException
|
159 |
*/
|
160 |
public NearestSpatialJoinVisitor(FLyrVect sourceLayer,
|
161 |
FLyrVect targetLayer, |
162 |
FeatureProcessor processor) throws DriverException {
|
163 |
this.sourceLayer = sourceLayer;
|
164 |
this.sourceRecordset = sourceLayer.getRecordset();
|
165 |
this.targetLayer = targetLayer;
|
166 |
this.targetRecordset = targetLayer.getRecordset();
|
167 |
this.featureProcessor = processor;
|
168 |
this.lookForNearestVisitor = new NearestFeatureVisitor(); |
169 |
} |
170 |
|
171 |
/**
|
172 |
* Processes a Feature of source layer, looking for its nearest feature of
|
173 |
* target layer and taking attributes from it
|
174 |
*/
|
175 |
public void visit(IGeometry g, int sourceIndex) throws VisitException { |
176 |
lookForNearestVisitor.setQueryGeometry(g.toJTSGeometry()); |
177 |
try {
|
178 |
if(onlySecondLayerSelection)
|
179 |
strategy.process(lookForNearestVisitor, targetRecordset.getSelection()); |
180 |
else
|
181 |
strategy.process(lookForNearestVisitor); |
182 |
int targetIndex = lookForNearestVisitor.getNearestFeatureIndex();
|
183 |
IFeature joinedFeature = createFeature(g, sourceIndex, targetIndex); |
184 |
this.featureProcessor.processFeature(joinedFeature);
|
185 |
} catch (DriverException e) {
|
186 |
throw new VisitException( |
187 |
"Problemas accediendo a los datos durante un nearest spatial join");
|
188 |
} catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
|
189 |
throw new VisitException( |
190 |
"Problemas accediendo a los datos durante un nearest spatial join");
|
191 |
} |
192 |
} |
193 |
|
194 |
public void stop(FLayer layer) { |
195 |
this.featureProcessor.finish();
|
196 |
|
197 |
} |
198 |
|
199 |
public boolean start(FLayer layer) { |
200 |
try {
|
201 |
this.featureProcessor.start();
|
202 |
} catch (EditionException e) {
|
203 |
return false; |
204 |
} |
205 |
return true; |
206 |
} |
207 |
|
208 |
public String getProcessDescription() { |
209 |
return "Spatial joining by nearest criteria"; |
210 |
} |
211 |
|
212 |
IFeature createFeature(IGeometry g, int sourceLayerIndex, int targetLayerIndex) throws DriverException, com.hardcode.gdbms.engine.data.driver.DriverException { |
213 |
IFeature solution = null;
|
214 |
int numFieldsA = sourceRecordset.getFieldCount();
|
215 |
int numFieldsB = targetRecordset.getFieldCount();
|
216 |
Value[] featureAttr = new Value[numFieldsA + numFieldsB]; |
217 |
for (int indexField = 0; indexField < numFieldsA; indexField++) { |
218 |
featureAttr[indexField] = sourceRecordset |
219 |
.getFieldValue(sourceLayerIndex,indexField); |
220 |
} |
221 |
for (int indexFieldB = 0; indexFieldB < numFieldsB; indexFieldB++) { |
222 |
featureAttr[numFieldsA + indexFieldB] = |
223 |
targetRecordset.getFieldValue(targetLayerIndex, indexFieldB); |
224 |
} |
225 |
solution = FeatureFactory.createFeature(featureAttr, g); |
226 |
return solution;
|
227 |
} |
228 |
|
229 |
public ILayerDefinition getResultLayerDefinition() throws GeoprocessException{ |
230 |
if(layerDefinition == null){ |
231 |
try {
|
232 |
layerDefinition = DefinitionUtils. |
233 |
mergeLayerDefinitions(sourceLayer, targetLayer); |
234 |
} catch (Exception e) { |
235 |
throw new GeoprocessException("Problemas al crear el esquema de la capa solucion de un spatial join"); |
236 |
} |
237 |
} |
238 |
return layerDefinition;
|
239 |
} |
240 |
|
241 |
public void setFeatureProcessor(FeatureProcessor processor) { |
242 |
this.featureProcessor = processor;
|
243 |
} |
244 |
|
245 |
public void setCancelableStrategy(Strategy secondLyrStrategy) { |
246 |
this.strategy = secondLyrStrategy;
|
247 |
|
248 |
} |
249 |
|
250 |
public void setOnlySecondLyrSelection(boolean onlySecondLayerSelection) { |
251 |
this.onlySecondLayerSelection = onlySecondLayerSelection;
|
252 |
} |
253 |
|
254 |
} |