Revision 10626 trunk/extensions/extGeoProcessing/src/com/iver/cit/gvsig/geoprocess/impl/spatialjoin/fmap/NearestSpatialJoinVisitor.java
NearestSpatialJoinVisitor.java | ||
---|---|---|
45 | 45 |
* |
46 | 46 |
* $Id$ |
47 | 47 |
* $Log$ |
48 |
* Revision 1.1 2006-06-20 18:20:45 azabala |
|
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 |
|
49 | 52 |
* first version in cvs |
50 | 53 |
* |
51 | 54 |
* Revision 1.2 2006/06/02 18:21:28 azabala |
... | ... | |
83 | 86 |
import java.util.Arrays; |
84 | 87 |
import java.util.List; |
85 | 88 |
|
89 |
import com.hardcode.gdbms.driver.exceptions.ReadDriverException; |
|
86 | 90 |
import com.hardcode.gdbms.engine.values.Value; |
87 | 91 |
import com.hardcode.gdbms.engine.values.ValueFactory; |
88 |
import com.iver.cit.gvsig.fmap.DriverException; |
|
92 |
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileReadException; |
|
93 |
import com.iver.cit.gvsig.exceptions.visitors.ProcessVisitorException; |
|
94 |
import com.iver.cit.gvsig.exceptions.visitors.StartVisitorException; |
|
95 |
import com.iver.cit.gvsig.exceptions.visitors.VisitorException; |
|
89 | 96 |
import com.iver.cit.gvsig.fmap.core.IFeature; |
90 | 97 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
91 | 98 |
import com.iver.cit.gvsig.fmap.drivers.FieldDescription; |
92 | 99 |
import com.iver.cit.gvsig.fmap.drivers.ILayerDefinition; |
93 |
import com.iver.cit.gvsig.fmap.edition.EditionException; |
|
94 | 100 |
import com.iver.cit.gvsig.fmap.layers.FLayer; |
95 | 101 |
import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
96 | 102 |
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource; |
97 | 103 |
import com.iver.cit.gvsig.fmap.operations.strategies.Strategy; |
98 |
import com.iver.cit.gvsig.fmap.operations.strategies.VisitException; |
|
99 | 104 |
import com.iver.cit.gvsig.geoprocess.core.fmap.DefinitionUtils; |
100 | 105 |
import com.iver.cit.gvsig.geoprocess.core.fmap.FeatureFactory; |
101 | 106 |
import com.iver.cit.gvsig.geoprocess.core.fmap.FeatureProcessor; |
... | ... | |
104 | 109 |
|
105 | 110 |
/** |
106 | 111 |
* This visitor implement Nearest Geometry Spatial Join. |
107 |
*
|
|
112 |
* |
|
108 | 113 |
* It is a particular case of 1-1 relationship |
109 | 114 |
* (a feature of layer A always be related to a feature of layer B) |
110 |
*
|
|
115 |
* |
|
111 | 116 |
* If two features of layer B are at the same distance of a feature of layer A, |
112 | 117 |
* we will took the first one analized. |
113 |
*
|
|
114 |
* In this visitor we apply a secuential scaning strategy: given a geometry
|
|
118 |
* |
|
119 |
* In this visitor we apply a secuential scaning strategy: given a geometry |
|
115 | 120 |
* of layer A, we check distances with all geometries of layer B return |
116 | 121 |
* the geometry at the min distance. |
117 |
*
|
|
118 |
*
|
|
122 |
* |
|
123 |
* |
|
119 | 124 |
* @author azabala |
120 | 125 |
* |
121 | 126 |
*/ |
... | ... | |
154 | 159 |
* It looks for nearest feature to a given feature |
155 | 160 |
*/ |
156 | 161 |
private NearestFeatureVisitor lookForNearestVisitor; |
157 |
|
|
162 |
|
|
158 | 163 |
/** |
159 | 164 |
* Schema of the result layer |
160 | 165 |
*/ |
... | ... | |
166 | 171 |
|
167 | 172 |
/** |
168 | 173 |
* Constructor. It receives layer with which we want to do a spatial join |
169 |
*
|
|
174 |
* |
|
170 | 175 |
* @param targetRecordset |
171 |
* @throws DriverException
|
|
176 |
* @throws ReadDriverException TODO
|
|
172 | 177 |
*/ |
173 | 178 |
public NearestSpatialJoinVisitor(FLyrVect sourceLayer, |
174 | 179 |
FLyrVect targetLayer, |
175 |
FeatureProcessor processor) throws DriverException { |
|
180 |
FeatureProcessor processor) throws ReadDriverException {
|
|
176 | 181 |
this.sourceLayer = sourceLayer; |
177 | 182 |
this.sourceRecordset = sourceLayer.getRecordset(); |
178 | 183 |
this.targetLayer = targetLayer; |
... | ... | |
185 | 190 |
* Processes a Feature of source layer, looking for its nearest feature of |
186 | 191 |
* target layer and taking attributes from it |
187 | 192 |
*/ |
188 |
public void visit(IGeometry g, int sourceIndex) throws VisitException { |
|
193 |
public void visit(IGeometry g, int sourceIndex) throws VisitorException, ProcessVisitorException {
|
|
189 | 194 |
if(g == null) |
190 |
return;
|
|
195 |
return; |
|
191 | 196 |
lookForNearestVisitor.setQueryGeometry(g.toJTSGeometry()); |
192 | 197 |
try { |
193 | 198 |
if(onlySecondLayerSelection) |
... | ... | |
196 | 201 |
strategy.process(lookForNearestVisitor); |
197 | 202 |
int targetIndex = lookForNearestVisitor.getNearestFeatureIndex(); |
198 | 203 |
double shortestDistance = lookForNearestVisitor.getShortestDist(); |
199 |
IFeature joinedFeature = createFeature(g, sourceIndex,
|
|
200 |
targetIndex, shortestDistance);
|
|
204 |
IFeature joinedFeature = createFeature(g, sourceIndex, |
|
205 |
targetIndex, shortestDistance); |
|
201 | 206 |
this.featureProcessor.processFeature(joinedFeature); |
202 |
} catch (DriverException e) { |
|
203 |
throw new VisitException(
|
|
207 |
} catch (ReadDriverException e) {
|
|
208 |
throw new ProcessVisitorException(targetRecordset.getName(),e,
|
|
204 | 209 |
"Problemas accediendo a los datos durante un nearest spatial join"); |
205 |
} catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
|
|
206 |
throw new VisitException(
|
|
210 |
} catch (ExpansionFileReadException e) {
|
|
211 |
throw new ProcessVisitorException(targetRecordset.getName(),e,
|
|
207 | 212 |
"Problemas accediendo a los datos durante un nearest spatial join"); |
208 | 213 |
} |
209 | 214 |
} |
210 | 215 |
|
211 |
public void stop(FLayer layer) { |
|
216 |
public void stop(FLayer layer) throws VisitorException {
|
|
212 | 217 |
this.featureProcessor.finish(); |
213 | 218 |
|
214 | 219 |
} |
215 | 220 |
|
216 |
public boolean start(FLayer layer) { |
|
217 |
try { |
|
218 |
this.featureProcessor.start(); |
|
219 |
} catch (EditionException e) { |
|
220 |
return false; |
|
221 |
} |
|
221 |
public boolean start(FLayer layer) throws StartVisitorException { |
|
222 |
this.featureProcessor.start(); |
|
222 | 223 |
return true; |
223 | 224 |
} |
224 |
|
|
225 |
|
|
225 | 226 |
public String getProcessDescription() { |
226 | 227 |
return "Spatial joining by nearest criteria"; |
227 | 228 |
} |
228 | 229 |
|
229 |
IFeature createFeature(IGeometry g, int sourceLayerIndex, |
|
230 |
int targetLayerIndex, double shortestDist) |
|
231 |
throws DriverException, |
|
232 |
com.hardcode.gdbms.engine.data.driver.DriverException { |
|
230 |
IFeature createFeature(IGeometry g, int sourceLayerIndex, |
|
231 |
int targetLayerIndex, double shortestDist) |
|
232 |
throws ReadDriverException { |
|
233 | 233 |
IFeature solution = null; |
234 | 234 |
int numFieldsA = sourceRecordset.getFieldCount(); |
235 | 235 |
int numFieldsB = targetRecordset.getFieldCount(); |
... | ... | |
239 | 239 |
.getFieldValue(sourceLayerIndex,indexField); |
240 | 240 |
} |
241 | 241 |
for (int indexFieldB = 0; indexFieldB < numFieldsB; indexFieldB++) { |
242 |
featureAttr[numFieldsA + indexFieldB] =
|
|
242 |
featureAttr[numFieldsA + indexFieldB] = |
|
243 | 243 |
targetRecordset.getFieldValue(targetLayerIndex, indexFieldB); |
244 | 244 |
} |
245 |
featureAttr[numFieldsA + numFieldsB] =
|
|
245 |
featureAttr[numFieldsA + numFieldsB] = |
|
246 | 246 |
ValueFactory.createValue(shortestDist); |
247 | 247 |
solution = FeatureFactory.createFeature(featureAttr, g); |
248 | 248 |
return solution; |
249 | 249 |
} |
250 |
|
|
250 |
|
|
251 | 251 |
public ILayerDefinition getResultLayerDefinition() throws GeoprocessException{ |
252 | 252 |
if(layerDefinition == null){ |
253 | 253 |
try { |
254 | 254 |
layerDefinition = DefinitionUtils. |
255 |
mergeLayerDefinitions(sourceLayer,
|
|
255 |
mergeLayerDefinitions(sourceLayer, |
|
256 | 256 |
targetLayer); |
257 | 257 |
//spatial join, in difference of union, intersection or difference |
258 | 258 |
//adds an additional field to mergeLayerDefinitions result: |
... | ... | |
270 | 270 |
FieldDescription[] newDescs = new FieldDescription[fieldDescs.size()]; |
271 | 271 |
fieldDescs.toArray(newDescs); |
272 | 272 |
layerDefinition.setFieldsDesc(newDescs); |
273 |
|
|
273 |
|
|
274 | 274 |
} catch (Exception e) { |
275 | 275 |
throw new GeoprocessException("Problemas al crear el esquema de la capa solucion de un spatial join"); |
276 |
}
|
|
276 |
} |
|
277 | 277 |
} |
278 | 278 |
return layerDefinition; |
279 | 279 |
} |
... | ... | |
284 | 284 |
|
285 | 285 |
public void setCancelableStrategy(Strategy secondLyrStrategy) { |
286 | 286 |
this.strategy = secondLyrStrategy; |
287 |
|
|
287 |
|
|
288 | 288 |
} |
289 | 289 |
|
290 | 290 |
public void setOnlySecondLyrSelection(boolean onlySecondLayerSelection) { |
Also available in: Unified diff