Revision 10626 trunk/extensions/extGeoProcessing/src/com/iver/cit/gvsig/geoprocess/impl/spatialjoin/fmap/IntersectSpatialJoinVisitor.java

View differences:

IntersectSpatialJoinVisitor.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
......
77 80
import java.util.Iterator;
78 81
import java.util.Map;
79 82

  
83
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
80 84
import com.hardcode.gdbms.engine.values.Value;
81 85
import com.hardcode.gdbms.engine.values.ValueFactory;
82
import com.iver.cit.gvsig.fmap.DriverException;
86
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileReadException;
87
import com.iver.cit.gvsig.exceptions.visitors.ProcessVisitorException;
88
import com.iver.cit.gvsig.exceptions.visitors.StartVisitorException;
89
import com.iver.cit.gvsig.exceptions.visitors.StopVisitorException;
90
import com.iver.cit.gvsig.exceptions.visitors.VisitorException;
83 91
import com.iver.cit.gvsig.fmap.core.IFeature;
84 92
import com.iver.cit.gvsig.fmap.core.IGeometry;
85 93
import com.iver.cit.gvsig.fmap.drivers.FieldDescription;
86 94
import com.iver.cit.gvsig.fmap.drivers.ILayerDefinition;
87 95
import com.iver.cit.gvsig.fmap.drivers.LayerDefinition;
88 96
import com.iver.cit.gvsig.fmap.drivers.SHPLayerDefinition;
89
import com.iver.cit.gvsig.fmap.edition.EditionException;
90 97
import com.iver.cit.gvsig.fmap.layers.FLayer;
91 98
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
92 99
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
93 100
import com.iver.cit.gvsig.fmap.operations.strategies.Strategy;
94
import com.iver.cit.gvsig.fmap.operations.strategies.VisitException;
95 101
import com.iver.cit.gvsig.geoprocess.core.fmap.DefinitionUtils;
96 102
import com.iver.cit.gvsig.geoprocess.core.fmap.FeatureFactory;
97 103
import com.iver.cit.gvsig.geoprocess.core.fmap.FeatureProcessor;
......
100 106

  
101 107
/**
102 108
 * This visitor implements Intersect Geometry Spatial Join.
103
 * 
109
 *
104 110
 * It is a particular case of 1-M relationship (a feature of layer A always be
105 111
 * related to M feature of layer B)
106 112
 * It allows to apply a sumarization function over numeric values of target
107 113
 * layer (sum, avg, min, max). If it doesnt find any feature of target layer
108 114
 * wich intersects with a given feature of source layer, these values will have
109 115
 * 0d value.
110
 * 
116
 *
111 117
 * @author azabala
112
 * 
118
 *
113 119
 */
114 120
public class IntersectSpatialJoinVisitor implements SpatialJoinVisitor {
115
	
121

  
116 122
	/**
117 123
	 * Needed to create layer definition
118 124
	 */
......
150 156
	private FeatureProcessor featureProcessor;
151 157

  
152 158
	private LayerDefinition resultLayerDefinition;
153
	
159

  
154 160
	private boolean onlySecondLyrSelection;
155 161

  
156 162
	public IntersectSpatialJoinVisitor(FLyrVect sourceLayer,
157 163
										FLyrVect targetLayer,
158
										Map fields_sumarizeFunct, 
159
										FeatureProcessor processor) throws DriverException {
164
										Map fields_sumarizeFunct,
165
										FeatureProcessor processor) throws ReadDriverException {
160 166
		this.sourceLayer = sourceLayer;
161 167
		this.sourceRecordset = sourceLayer.getRecordset();
162 168
		this.featureProcessor = processor;
......
165 171
		this.visitor = new IntersectsFinderFeatureVisitor(fields_sumarizeFunc);
166 172
	}
167 173

  
168
	public void visit(IGeometry g, int index) throws VisitException {
174
	public void visit(IGeometry g, int index) throws VisitorException, ProcessVisitorException {
169 175
		if(g == null)
170 176
			return;
171 177
		IFeature solution = null;
172 178
		visitor.setQueryGeometry(g.toJTSGeometry());
173 179
		try {
174 180
			if(onlySecondLyrSelection)
175
				visitor.setSelection(targetRecordset.getSelection());	
181
				visitor.setSelection(targetRecordset.getSelection());
176 182
			strategy.process(visitor, g.getBounds2D());
177 183
			solution = createFeature(g, index, visitor.getNumIntersections());
178 184
			featureProcessor.processFeature(solution);
179 185
			resetFunctions();
180 186
			visitor.clearIntersections();
181
		} catch (DriverException e) {
182
			throw new VisitException(
187
		} catch (ReadDriverException e) {
188
			throw new ProcessVisitorException(targetRecordset.getName(),e,
183 189
					"Error al buscar las intersecciones de una geometria durante un spatial join");
184
		} catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
185
			throw new VisitException(
186
			"Error al buscar las intersecciones de una geometria durante un spatial join");
190
		} catch (ExpansionFileReadException e) {
191
			throw new ProcessVisitorException(targetRecordset.getName(),e,
192
				"Error al buscar las intersecciones de una geometria durante un spatial join");
187 193
		}
188 194
	}
189
	
195

  
190 196
	public String getProcessDescription() {
191 197
		return "Spatial joining by intersects criteria";
192 198
	}
193
	
199

  
194 200
	public void resetFunctions(){
195 201
		Iterator fieldsIt = fields_sumarizeFunc.keySet().iterator();
196 202
		while (fieldsIt.hasNext()) {
197 203
			String field = (String) fieldsIt.next();
198
			SummarizationFunction[] functions = 
204
			SummarizationFunction[] functions =
199 205
				(SummarizationFunction[]) fields_sumarizeFunc.get(field);
200 206
			for (int i = 0; i < functions.length; i++) {
201 207
				functions[i].reset();
202 208
			}// for
203 209
		}// while
204 210
	}
205
	
206
	
211

  
212

  
207 213
	private IFeature createFeature(IGeometry g, int index, int numIntersections)
208
			throws com.hardcode.gdbms.engine.data.driver.DriverException {
214
			throws ReadDriverException {
209 215
		IFeature solution = null;
210 216
		int numSourceFields = this.sourceRecordset.getFieldCount();
211 217
		ArrayList values = new ArrayList();
......
216 222
		Iterator fieldsIt = fields_sumarizeFunc.keySet().iterator();
217 223
		while (fieldsIt.hasNext()) {
218 224
			String field = (String) fieldsIt.next();
219
			SummarizationFunction[] functions = 
225
			SummarizationFunction[] functions =
220 226
				(SummarizationFunction[]) fields_sumarizeFunc.get(field);
221 227
			for (int i = 0; i < functions.length; i++) {
222 228
				values.add(functions[i].getSumarizeValue());
......
229 235
		return solution;
230 236
	}
231 237

  
232
	public void stop(FLayer layer) {
238
	public void stop(FLayer layer) throws StopVisitorException {
233 239
		featureProcessor.finish();
234 240

  
235 241
	}
236 242

  
237
	public boolean start(FLayer layer) {
238
			try {
239
				this.featureProcessor.start();
240
			} catch (EditionException e) {
241
				return false;
242
			}
243
			return true;
243
	public boolean start(FLayer layer) throws StartVisitorException {
244
		this.featureProcessor.start();
245
		return true;
244 246
	}
245
	
246
	
247

  
248

  
247 249
	public ILayerDefinition getResultLayerDefinition(){
248 250
		if(this.resultLayerDefinition == null){
249 251
			ArrayList fields = new ArrayList();
......
252 254
			//new attributes
253 255
			try {
254 256
				resultLayerDefinition.setShapeType(sourceLayer.getShapeType());
255
			} catch (DriverException e) {
257
			} catch (ReadDriverException e) {
256 258
				// TODO Auto-generated catch block
257 259
				e.printStackTrace();
258 260
			}
259
			
261

  
260 262
			//first layer attributes
261 263
			int numFields = 0;
262 264
			try {
263 265
				numFields = sourceRecordset.getFieldCount();
264
			} catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
266
			} catch (ReadDriverException e) {
265 267
				// TODO Auto-generated catch block
266 268
				e.printStackTrace();
267 269
			}
268
			
270

  
269 271
			FieldDescription fieldDesc = null;
270 272
			for(int i = 0; i < numFields; i++){
271 273
				fieldDesc = new FieldDescription();
......
276 278
					fieldDesc.setFieldLength(DefinitionUtils.
277 279
							getDataTypeLength(fieldType));
278 280
					fieldDesc.setFieldDecimalCount(DefinitionUtils.NUM_DECIMALS);
279
				} catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
281
				} catch (ReadDriverException e) {
280 282
					// TODO Auto-generated catch block
281 283
					e.printStackTrace();
282 284
				}
283 285
				fields.add(fieldDesc);
284 286
			}//for
285
			
287

  
286 288
			//target layer attributes
287 289
			Iterator fieldsIt = fields_sumarizeFunc.keySet().iterator();
288 290
			while(fieldsIt.hasNext()){
......
307 309
					fields.add(fieldDesc);
308 310
				}//for
309 311
			}//while
310
			
312

  
311 313
			//finally, we add to the result schema of M-N spatial join
312 314
			//the number of features intersected of layer b
313 315
			fieldDesc = new FieldDescription();
......
316 318
			fieldDesc.setFieldLength(DefinitionUtils.
317 319
					getDataTypeLength(XTypes.INTEGER));
318 320
			fields.add(fieldDesc);
319
			
321

  
320 322
			FieldDescription[] fieldsDesc = null;
321 323
			if(fields.size() == 0){
322 324
				fieldsDesc = new FieldDescription[0];

Also available in: Unified diff