Revision 4447 trunk/extensions/extGeoProcessing/src/com/iver/gvsig/geoprocessing/impl/clip/ClipGeoprocess.java

View differences:

ClipGeoprocess.java
40 40
 *
41 41
 *   +34 963163400
42 42
 *   dac@iver.es
43
 */
44
/* CVS MESSAGES:
43
 
45 44
*
46 45
* $Id$
47 46
* $Log$
48
* Revision 1.7  2006-03-14 18:32:46  fjp
47
* Revision 1.8  2006-03-15 18:30:39  azabala
48
* *** empty log message ***
49
*
50
* Revision 1.7  2006/03/14 18:32:46  fjp
49 51
* Cambio con LayerDefinition para que sea compatible con la definici?n de tablas tambi?n.
50 52
*
51 53
* Revision 1.6  2006/03/07 21:01:33  azabala
......
74 76

  
75 77
import com.iver.cit.gvsig.fmap.core.IFeature;
76 78
import com.iver.cit.gvsig.fmap.core.IGeometry;
79
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
77 80
import com.iver.cit.gvsig.fmap.drivers.ILayerDefinition;
78 81
import com.iver.cit.gvsig.fmap.drivers.ITableDefinition;
79 82
import com.iver.cit.gvsig.fmap.edition.ISchemaManager;
80 83
import com.iver.cit.gvsig.fmap.edition.IWriter;
81 84
import com.iver.cit.gvsig.fmap.layers.FBitSet;
82 85
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
86
import com.iver.cit.gvsig.fmap.operations.CancellableMonitorable;
87
import com.iver.cit.gvsig.fmap.operations.DefaultCancellableMonitorable;
83 88
import com.iver.cit.gvsig.fmap.operations.strategies.Strategy;
84 89
import com.iver.cit.gvsig.fmap.operations.strategies.StrategyManager;
85 90
import com.iver.cit.gvsig.fmap.operations.strategies.VisitException;
......
89 94
import com.iver.gvsig.geoprocessing.model.IOverlayGeoprocess;
90 95
import com.iver.gvsig.geoprocessing.schemabuilder.DefinitionUtils;
91 96
import com.iver.gvsig.geoprocessing.schemabuilder.XTypes;
97
import com.iver.utiles.swing.threads.IMonitorableTask;
92 98
import com.vividsolutions.jts.geom.Geometry;
99

  
93 100
/**
94
 * A clip between two layers is the intersection
95
 * of the first layer with the convex hull
96
 * of the second layer.
101
 * A clip between two layers is the intersection of the first layer with the
102
 * convex hull of the second layer.
97 103
 * 
98
 * It is an individual case of OverlayGeoprocess.
99
 * When we would have piped geoprocesses, we could
100
 * model it like a pipe of a ConvexHull geoprocess and
101
 * an intersection geoprocess.
104
 * It is an individual case of OverlayGeoprocess. When we would have piped
105
 * geoprocesses, we could model it like a pipe of a ConvexHull geoprocess and an
106
 * intersection geoprocess.
107
 * 
102 108
 * @author azabala
103
 *
109
 * 
104 110
 */
105
public class ClipGeoprocess extends AbstractGeoprocess 
106
						implements IOverlayGeoprocess {
107
	
111
public class ClipGeoprocess extends AbstractGeoprocess implements
112
		IOverlayGeoprocess {
113

  
108 114
	/**
109 115
	 * overlay layer
110 116
	 */
111 117
	private FLyrVect overlayLayer;
118

  
112 119
	/**
113 120
	 * Schema of the result layer
114 121
	 */
122

  
115 123
	private ILayerDefinition resultLayerDefinition;
124

  
116 125
	/**
117 126
	 * flag to only clip selection of input layer
118 127
	 */
119 128
	private boolean onlyFirstLayerSelection = false;
129

  
120 130
	/**
121 131
	 * flag to only clip with selection of clipping layer
122 132
	 */
123 133
	private boolean onlyClipLayerSelection = false;
134

  
124 135
	/**
125
	 * processes input layer's features to clip its geometries
126
	 * with clipping layer bounding box
136
	 * processes input layer's features to clip its geometries with clipping
137
	 * layer bounding box
127 138
	 */
128 139
	private ClipVisitor visitor;
129
	
140

  
130 141
	/**
131
	 * Constructor 
132
	 *
142
	 * Constructor
143
	 * 
133 144
	 */
134
	public ClipGeoprocess(FLyrVect inputLayer){
145
	public ClipGeoprocess(FLyrVect inputLayer) {
135 146
		setFirstOperand(inputLayer);
136 147
	}
137
	
148

  
138 149
	public void setSecondOperand(FLyrVect secondLayer) {
139 150
		this.overlayLayer = secondLayer;
140 151

  
......
145 156
	}
146 157

  
147 158
	public void setParameters(Map params) throws GeoprocessException {
148
		Boolean firstLayerSelection = (Boolean) params.get("firstlayerselection");
149
		if(firstLayerSelection != null)
159
		Boolean firstLayerSelection = (Boolean) params
160
				.get("firstlayerselection");
161
		if (firstLayerSelection != null)
150 162
			this.onlyFirstLayerSelection = firstLayerSelection.booleanValue();
151
		
152
		Boolean secondLayerSelection = (Boolean) params.get("secondlayerselection");
153
		if(secondLayerSelection != null)
163

  
164
		Boolean secondLayerSelection = (Boolean) params
165
				.get("secondlayerselection");
166
		if (secondLayerSelection != null)
154 167
			this.onlyClipLayerSelection = secondLayerSelection.booleanValue();
155 168
	}
156 169

  
......
159 172
			throw new GeoprocessException("Clip: capa de entrada a null");
160 173
		if (overlayLayer == null)
161 174
			throw new GeoprocessException("Clip: capa de clip a null");
162
		if(this.writer == null || 
163
		   this.schemaManager == null){
164
			throw new GeoprocessException("Operacion de clip sin especificar capa de resultados");
175
		if (this.writer == null || this.schemaManager == null) {
176
			throw new GeoprocessException(
177
					"Operacion de clip sin especificar capa de resultados");
165 178
		}
166
		
179

  
167 180
		try {
168
			if(overlayLayer.getShapeType() != XTypes.POLYGON){
169
				throw new GeoprocessException("La capa de recorte no es de pol?gonos");
181
			if (overlayLayer.getShapeType() != XTypes.POLYGON) {
182
				throw new GeoprocessException(
183
						"La capa de recorte no es de pol?gonos");
170 184
			}
171 185
		} catch (com.iver.cit.gvsig.fmap.DriverException e) {
172
			throw new GeoprocessException("Error al tratar de chequear si la capa de recorte es de pol?gonos");
186
			throw new GeoprocessException(
187
					"Error al tratar de chequear si la capa de recorte es de pol?gonos");
173 188
		}
174 189
	}
175 190

  
176 191
	public void process() throws GeoprocessException {
177 192
		Geometry clippingGeometry = null;
178 193
		try {
179
			clippingGeometry = computeJtsClippingPoly();
194
			clippingGeometry = computeJtsClippingPoly(null);
180 195
		} catch (Exception e) {
181
			throw new GeoprocessException("Error calculando el convex hull de la clipping layer");
196
			throw new GeoprocessException(
197
					"Error calculando el convex hull de la clipping layer");
182 198
		}
183
		visitor = new ClipVisitor(clippingGeometry,
184
				resultLayerDefinition, 
185
				schemaManager, 
186
				writer);
187
		Strategy strategy = StrategyManager.
188
			getStrategy(firstLayer);
199
		visitor = new ClipVisitor(clippingGeometry, resultLayerDefinition,
200
				schemaManager, writer);
201
		Strategy strategy = StrategyManager.getStrategy(firstLayer);
189 202
		try {
190
			if(onlyFirstLayerSelection){
191
				strategy.process(visitor, 
192
						firstLayer.getRecordset().
193
						getSelection());
194
			}else{
203
			if (onlyFirstLayerSelection) {
204
				strategy.process(visitor, firstLayer.getRecordset()
205
						.getSelection());
206
			} else {
195 207
				strategy.process(visitor);
196 208
			}
197 209
		} catch (Exception e) {
198
			throw new GeoprocessException("Problemas al leer los datos de la capa a clipear");
210
			throw new GeoprocessException(
211
					"Problemas al leer los datos de la capa a clipear");
199 212
		}
200 213
	}
201 214

  
......
211 224

  
212 225
	}
213 226

  
227

  
214 228
	public ILayerDefinition createLayerDefinition() {
215 229
		//result layer definition will be the same that
216 230
		//input layer
217 231
		if(resultLayerDefinition == null){
218 232
			try {
219
				resultLayerDefinition = DefinitionUtils.
220
					createLayerDefinition(firstLayer);
233
				resultLayerDefinition = DefinitionUtils
234
						.createLayerDefinition(firstLayer);
221 235
			} catch (Exception e) {
222
				//TODO Quizas createLayerDefinition deberia lanzar
223
				//una excepcion
236
				// TODO Quizas createLayerDefinition deberia lanzar
237
				// una excepcion
224 238
				e.printStackTrace();
225 239
			}
226 240
		}
227 241
		return resultLayerDefinition;
228 242
	}
229
	
230
	
231
	private Geometry computeJtsClippingPoly() throws com.iver.cit.gvsig.fmap.DriverException, VisitException {
232
		ScalableUnionVisitor visitor = 
233
			new ScalableUnionVisitor();
234
		
235
		Strategy strategy = 
236
			StrategyManager.getStrategy(overlayLayer);
237
		if(onlyClipLayerSelection){
238
			strategy.process(visitor, 
239
					overlayLayer.getRecordset().getSelection());
240
		}else{
241
			strategy.process(visitor);
243

  
244
	/**
245
	 * Computes union of all geometries of the clipping layer
246
	 * 
247
	 * @param cancel
248
	 *            monitors cancellations and progress of clipping polygon
249
	 *            computation.
250
	 * @return
251
	 * @throws com.iver.cit.gvsig.fmap.DriverException
252
	 * @throws VisitException
253
	 */
254
	private Geometry computeJtsClippingPoly(CancellableMonitorable cancel)
255
			throws com.iver.cit.gvsig.fmap.DriverException, VisitException {
256
		ScalableUnionVisitor visitor = new ScalableUnionVisitor();
257

  
258
		Strategy strategy = StrategyManager.getStrategy(overlayLayer);
259
		if (onlyClipLayerSelection) {
260
			strategy.process(visitor, overlayLayer.getRecordset()
261
					.getSelection(), cancel);
262
		} else {
263
			strategy.process(visitor, cancel);
242 264
		}
243
		return 	visitor.getJtsConvexHull();
265
		return visitor.getJtsConvexHull();
244 266
	}
245 267

  
246 268
	public FBitSet getOverlays(IGeometry firstLayerGeometry, int index) {
......
248 270
		return null;
249 271
	}
250 272

  
251
	public IFeature[] computeIntersection(IGeometry firsLayerGeometry, int firstLayerFeatureIndex, IGeometry secondLayerGeometry, int secondLayerFeatureIndex) {
273
	public IFeature[] computeIntersection(IGeometry firsLayerGeometry,
274
			int firstLayerFeatureIndex, IGeometry secondLayerGeometry,
275
			int secondLayerFeatureIndex) {
252 276
		// TODO Auto-generated method stub
253 277
		return null;
254 278
	}
255 279

  
280
	public IMonitorableTask createTask() {
281
		final CancellableMonitorable cancelMonitor = 
282
			createCancelMonitor();
283

  
284
		return new IMonitorableTask() {
285
			String CLIP_MESSAGE = "Computing clip polygon: ";
286
			String INTERS_MESSAGE = "Clipping geometries: ";
287
			String currentMsg = CLIP_MESSAGE;
288
			private boolean finished = false;
289

  
290
			public int getInitialStep() {
291
				return cancelMonitor.getInitialStep();
292
			}
293

  
294
			public int getFinishStep() {
295
				return cancelMonitor.getFinalStep();
296
			}
297

  
298
			public int getCurrentStep() {
299
				return cancelMonitor.getCurrentStep();
300
			}
301

  
302
			public String getStatusMessage() {
303
				// FIXME Cambiar esto por un mecanismo de eventos,
304
				// de forma que la tarea lo que tenga sea un escuchador
305
				// que cambie el mensaje de estado segun los eventos
306
				// de tareas que se est?n realizando
307
				return "Clip Geoprocess...";
308
			}
309

  
310
			public String getNote() {
311
				// FIXME Cambiar esto por un mecanismo de eventos,
312
				// de forma que la tarea lo que tenga sea un escuchador
313
				// que cambie el mensaje de estado segun los eventos
314
				// de tareas que se est?n realizando
315
				return currentMsg  +
316
				getCurrentStep()+ 
317
				" de "+
318
				getFinishStep();
319
			}
320

  
321
			public void cancel() {
322
				((DefaultCancellableMonitorable) cancelMonitor)
323
						.setCanceled(true);
324
			}
325

  
326
			public void run() {
327
				
328
				Geometry clippingGeometry = null;
329
				try {
330
					clippingGeometry = computeJtsClippingPoly(cancelMonitor);
331
				} catch (Exception e) {
332
					//FIXME Ver que pasa con las excepciones que
333
					//que pueda lanzar un ITask
334
				}
335
				currentMsg = INTERS_MESSAGE;
336
				visitor = new ClipVisitor(clippingGeometry, resultLayerDefinition,
337
						schemaManager, writer);
338
				Strategy strategy = StrategyManager.getStrategy(firstLayer);
339
				try {
340
					if (onlyFirstLayerSelection) {
341
						strategy.process(visitor, firstLayer.getRecordset()
342
								.getSelection(), cancelMonitor);
343
					} else {
344
						strategy.process(visitor, cancelMonitor);
345
					}
346
				} catch (Exception e) {
347
					//FIXME Ver que pasa con las excepciones de las ITask
348
				}
349
				finished = true;
350
			}
351

  
352
			public boolean isDefined() {
353
				return cancelMonitor.isDeterminatedProcess();
354
			}
355

  
356
			public boolean isCanceled() {
357
				return cancelMonitor.isCanceled();
358
			}
359

  
360
			public boolean isFinished() {
361
				return finished;
362
			}
363
		};
364
	}
365

  
366
	private DefaultCancellableMonitorable createCancelMonitor() {
367
		DefaultCancellableMonitorable monitor = new DefaultCancellableMonitorable();
368
		monitor.setInitialStep(0);
369
		monitor.setDeterminatedProcess(true);
370
		int clipSteps = 0;
371
		if (onlyClipLayerSelection){
372
			FBitSet selection = overlayLayer.getRecordset().getSelection();
373
			clipSteps = selection.cardinality();
374
		}else{
375
			try {
376
				clipSteps = overlayLayer.getSource().getShapeCount();
377
			} catch (DriverIOException e) {
378
				// TODO Auto-generated catch block
379
				e.printStackTrace();
380
			}
381
		}
382
		int  firstSteps = 0;
383
		if (onlyFirstLayerSelection) {
384
			FBitSet selection = firstLayer.getRecordset().getSelection();
385
			firstSteps = selection.cardinality();
386
		} else {
387
			try {
388
				firstSteps = firstLayer.getSource()
389
								.getShapeCount();
390
			} catch (DriverIOException e) {
391
				e.printStackTrace();
392
			}
393
		}
394
		int totalSteps = clipSteps + firstSteps;
395
		monitor.setFinalStep(totalSteps);
396
		return monitor;
397
	}
398

  
256 399
}
257

  

Also available in: Unified diff