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

View differences:

BufferGeoprocess.java
45 45
 *
46 46
 * $Id$
47 47
 * $Log$
48
 * Revision 1.3  2006-07-21 11:04:50  azabala
48
 * Revision 1.4  2007-03-06 16:47:58  caballero
49
 * Exceptions
50
 *
51
 * Revision 1.3  2006/07/21 11:04:50  azabala
49 52
 * Unified createTask and process() methods
50 53
 *
51 54
 * Revision 1.2  2006/06/29 07:33:57  fjp
......
115 118

  
116 119
import org.cresques.cts.IProjection;
117 120

  
121
import com.hardcode.gdbms.driver.exceptions.InitializeDriverException;
122
import com.hardcode.gdbms.driver.exceptions.InitializeWriterException;
123
import com.hardcode.gdbms.driver.exceptions.OpenDriverException;
124
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
125
import com.hardcode.gdbms.driver.exceptions.SchemaEditionException;
126
import com.hardcode.gdbms.engine.data.driver.DriverException;
118 127
import com.iver.andami.PluginServices;
119
import com.iver.cit.gvsig.fmap.DriverException;
120
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
128
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileReadException;
129
import com.iver.cit.gvsig.exceptions.layers.LoadLayerException;
130
import com.iver.cit.gvsig.exceptions.visitors.ProcessVisitorException;
131
import com.iver.cit.gvsig.exceptions.visitors.VisitorException;
121 132
import com.iver.cit.gvsig.fmap.drivers.FieldDescription;
122 133
import com.iver.cit.gvsig.fmap.drivers.ILayerDefinition;
123
import com.iver.cit.gvsig.fmap.drivers.LayerDefinition;
124 134
import com.iver.cit.gvsig.fmap.drivers.SHPLayerDefinition;
125 135
import com.iver.cit.gvsig.fmap.drivers.shp.IndexedShpDriver;
126
import com.iver.cit.gvsig.fmap.edition.EditionException;
127 136
import com.iver.cit.gvsig.fmap.edition.writers.shp.ShpWriter;
128 137
import com.iver.cit.gvsig.fmap.layers.FBitSet;
129 138
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
130 139
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
131 140
import com.iver.cit.gvsig.fmap.operations.strategies.Strategy;
132 141
import com.iver.cit.gvsig.fmap.operations.strategies.StrategyManager;
133
import com.iver.cit.gvsig.fmap.operations.strategies.VisitException;
134 142
import com.iver.cit.gvsig.geoprocess.core.fmap.AbstractGeoprocess;
135 143
import com.iver.cit.gvsig.geoprocess.core.fmap.FeaturePersisterProcessor2;
136 144
import com.iver.cit.gvsig.geoprocess.core.fmap.GeometryPersisterProcessor;
......
149 157
 * All the points interior to this buffer area has to be at a distance inferior
150 158
 * to "buffer distance" to the original geometry. This buffer distance could be
151 159
 * constant, or it could be a function of the value of a feature attribute.<br>
152
 * 
160
 *
153 161
 * @author azabala
154
 * 
162
 *
155 163
 */
156 164
public class BufferGeoprocess extends AbstractGeoprocess implements
157 165
		IOneLayerGeoprocess {
......
167 175
	 */
168 176
	public static final byte ATTRIBUTE_DISTANCE_STRATEGY = 1;
169 177

  
170
	
178

  
171 179
	/**
172 180
	 * Schema of the result layer
173 181
	 */
......
185 193
	 * layer, to avoid intersections in a polygon layer. By default, is true.<br>
186 194
	 */
187 195
	private boolean dissolveBuffers = false;
188
	
196

  
189 197
	/**
190 198
	 * Iterates over input layer geometries to compute buffered geometries by
191 199
	 * using a distance buffer function.
......
194 202

  
195 203
	/**
196 204
	 * Constructor
197
	 * 
205
	 *
198 206
	 * @param bufferLayer
199 207
	 *            layer to buffer
200 208
	 */
......
245 253
						"Buffer por dist constante sin distancia");
246 254
			}
247 255
		}// else
248
		
256

  
249 257
		Byte capflag = (Byte) params.get("cap");
250 258
		if(capflag != null){
251 259
			bufferVisitor.setTypeOfCap(capflag.byteValue());
252 260
		}
253
		
261

  
254 262
		Byte typePolFlag = (Byte) params.get("typePolBuffer");
255 263
		if(typePolFlag != null){
256 264
			bufferVisitor.setTypeOfBuffer(typePolFlag.byteValue());
257 265
		}
258
		
266

  
259 267
		Integer numRingsFlag = (Integer) params.get("numRings");
260 268
		if(numRingsFlag != null){
261 269
			bufferVisitor.setNumberOfRadialBuffers(numRingsFlag.intValue());
262 270
		}
263 271
	}
264
	
265 272

  
273

  
266 274
	/**
267 275
	 * Checks preconditions to this geoprocess (input layer mustnt be null), etc
268 276
	 */
......
277 285

  
278 286
	/**
279 287
	 * Computes buffer geoprocess, and saves results in solution layer.
280
	 * 
288
	 *
281 289
	 */
282 290
	public void process() throws GeoprocessException {
283 291
		try {
......
292 300
	 * It is useful when you want to dissolve buffers, to avoid working
293 301
	 * with intermediate results in memory.
294 302
	 * @return
303
	 * @throws OpenDriverException
304
	 * @throws InitializeDriverException
305
	 * @throws LoadLayerException
295 306
	 * @throws IOException
296 307
	 * @throws DriverException
297 308
	 */
298
	public FLyrVect createTempLayer() throws IOException, DriverException {
309
	public FLyrVect createTempLayer() throws OpenDriverException, InitializeDriverException, LoadLayerException {
299 310
		FLyrVect solution = null;
300 311
		String fileName = ((ShpWriter) writer).getShpPath();
301 312
		File file = new File(fileName);
......
310 321
	}
311 322

  
312 323
	private void computeAndDissolveBuffer(Strategy strategy,
313
			CancellableMonitorable cancel) throws EditionException,
314
			DriverException, VisitException, IOException {
324
			CancellableMonitorable cancel) throws InitializeWriterException, SchemaEditionException, DriverException, OpenDriverException, InitializeDriverException, ReadDriverException, LoadLayerException, VisitorException, ExpansionFileReadException  {
315 325

  
316 326
		ShpWriter mainWriter = (ShpWriter) writer;
317 327
		writer = new ShpWriter();
......
320 330
		File newFile = new File(temp);
321 331
		((ShpWriter) writer).setFile(newFile);
322 332
		((ShpWriter) writer)
323
				.initialize((LayerDefinition) resultLayerDefinition);
333
				.initialize(resultLayerDefinition);
324 334
		((SHPLayerDefinition) resultLayerDefinition).setFile(newFile);
325 335
		schemaManager.createSchema(resultLayerDefinition);
326 336
		bufferVisitor.setBufferProcessor(new GeometryPersisterProcessor(
327 337
				resultLayerDefinition, schemaManager, writer));
328
		
338

  
329 339
		computeOnlyBuffers(strategy, cancel);
330 340
		FLyrVect tempLayer = createTempLayer();
331
		
341

  
332 342
		writer = mainWriter;
333 343
		FeaturePersisterProcessor2 processor = new FeaturePersisterProcessor2(
334 344
				writer);
335
		AdjacencyDissolveVisitor dissolveVisitor = 
345
		AdjacencyDissolveVisitor dissolveVisitor =
336 346
			new AdjacencyDissolveVisitor(null, processor);
337 347
		Strategy secondPassStrategy = StrategyManager.getStrategy(tempLayer);
338 348
		dissolveVisitor.setStrategy(secondPassStrategy);
339
long t0 = System.currentTimeMillis();		
349
long t0 = System.currentTimeMillis();
340 350
		secondPassStrategy.process(dissolveVisitor, cancel);
341 351
long t1 = System.currentTimeMillis();
342 352
System.out.println((t1-t0)+" en dissolver los buffers");
......
347 357
	 * actual writer.
348 358
	 * @param strategy
349 359
	 * @param cancel
360
	 * @throws ReadDriverException
350 361
	 * @throws EditionException
351 362
	 * @throws DriverException
352
	 * @throws VisitException
363
	 * @throws ProcessVisitorException
364
	 * @throws SchemaEditionException
365
	 * @throws ProcessWriterException
366
	 * @throws VisitorException
367
	 * @throws ExpansionFileReadException
353 368
	 */
354 369
	private void computeOnlyBuffers(Strategy strategy,
355
			CancellableMonitorable cancel) throws EditionException,
356
			DriverException, VisitException {
370
			CancellableMonitorable cancel) throws ReadDriverException, SchemaEditionException, ExpansionFileReadException, VisitorException {
357 371

  
358 372
		bufferVisitor.setBufferProcessor(new GeometryPersisterProcessor(
359 373
				this.resultLayerDefinition, this.schemaManager, this.writer));
......
367 381
	}
368 382

  
369 383
	/**
370
	 * Creates a LayerDefinition in function of: 
371
	 * a) input layer 
372
	 * b) user selections 
384
	 * Creates a LayerDefinition in function of:
385
	 * a) input layer
386
	 * b) user selections
373 387
	 * c) buffer geoprocess itselt operation
374
	 * 
388
	 *
375 389
	 * Users of this geoprocess may construct a Writer from this LayerDefinition
376 390
	 * (for example, to save to shp ShpWriter needs a File and a LayerDefinition
377 391
	 * -to create this file with the proper schema-)
378
	 * 
392
	 *
379 393
	 * FIXME
380 394
	 * We cant call this method before setParams(Map params). Launch a exception
381
	 * 
395
	 *
382 396
	 * ILayerDefinition wont be the same for dissolved buffers layers
383
	 * 
397
	 *
384 398
	 */
385 399
	public ILayerDefinition createLayerDefinition() {
386 400
		// In a buffer geoprocess we only well have a geometry
......
398 412
		}
399 413
		return resultLayerDefinition;
400 414
	}
401
	
415

  
402 416
	/**
403 417
	 * Auxilar method to create a layer definition for buffer's process which
404 418
	 * needs to create only internal or external buffers.
......
420 434
		resultLayerDefinition.setFieldsDesc(fields);
421 435
		return resultLayerDefinition;
422 436
	}
423
	
424
	
437

  
438

  
425 439
	/**
426 440
	 * Auxilar method to create a layer definition for buffer's process which
427 441
	 * needs to create internal and external buffers.
......
445 459
		fields[2].setFieldDecimalCount(4);
446 460
		fields[2].setFieldName("TO");
447 461
		fields[2].setFieldType(XTypes.DOUBLE);
448
		
462

  
449 463
		resultLayerDefinition.setFieldsDesc(fields);
450 464
		return resultLayerDefinition;
451 465
	}
......
462 476
			FBitSet selection = null;
463 477
			try {
464 478
				selection = firstLayer.getRecordset().getSelection();
465
			} catch (DriverException e) {
479
			} catch (ReadDriverException e) {
466 480
				// TODO Auto-generated catch block
467 481
				e.printStackTrace();
468 482
			}
......
471 485
		} else {
472 486
			try {
473 487
				monitor.setFinalStep(firstLayer.getSource().getShapeCount());
474
			} catch (DriverIOException e) {
488
			} catch (ReadDriverException e) {
489
				// TODO Auto-generated catch block
475 490
				e.printStackTrace();
476 491
			}
477 492
		}
......
509 524
		 * Concatenation string
510 525
		 */
511 526
		final String of = PluginServices.getText(this, "De");
512
		
527

  
513 528
		return new IMonitorableTask() {
514 529
			private boolean finished = false;
515 530
			String currentNote = note;
......
550 565
						//computeAndDissolveBuffer(strategy, cancelMonitor);
551 566
						ShpWriter mainWriter = (ShpWriter) writer;
552 567
						writer = new ShpWriter();
553
						String temp = System.getProperty("java.io.tmpdir") + 
554
								"/buffer" + 
568
						String temp = System.getProperty("java.io.tmpdir") +
569
								"/buffer" +
555 570
								System.currentTimeMillis() +
556 571
								".shp";
557 572
						File newFile = new File(temp);
558 573
						((ShpWriter) writer).setFile(newFile);
559 574
						((ShpWriter) writer).initialize(
560
								(LayerDefinition) resultLayerDefinition);
575
								resultLayerDefinition);
561 576
						((SHPLayerDefinition) resultLayerDefinition).setFile(newFile);
562 577
						schemaManager.createSchema(resultLayerDefinition);
563 578
						bufferVisitor.setBufferProcessor(new GeometryPersisterProcessor(
......
572 587
						writer = mainWriter;
573 588
						FeaturePersisterProcessor2 processor = new FeaturePersisterProcessor2(
574 589
								writer);
575
//						AdjacencyDissolveVisitor dissolveVisitor = 
590
//						AdjacencyDissolveVisitor dissolveVisitor =
576 591
//							new AdjacencyDissolveVisitor(null, processor);
577 592
						DissolveVisitor dissolveVisitor =
578 593
							createDissolveVisitor(processor);
579 594
						Strategy secondPassStrategy = StrategyManager.getStrategy(tempLayer);
580 595
						dissolveVisitor.setStrategy(secondPassStrategy);
581
				long t0 = System.currentTimeMillis();		
596
				long t0 = System.currentTimeMillis();
582 597
						secondPassStrategy.process(dissolveVisitor, cancelMonitor);
583 598
				long t1 = System.currentTimeMillis();
584 599
				System.out.println((t1-t0)+" en dissolver los buffers");
......
589 604
				} catch (Exception e) {
590 605
					e.printStackTrace();
591 606
					finished = true;
592
				} 
607
				}
593 608
			}
594 609

  
595 610
			/**
......
598 613
			 * criteria will be adjacency.
599 614
			 * If we computed many buffers, dissolve criteria will be
600 615
			 * adjacency and alphanumeric (value of FROM-TO or DISTANCE fields of the
601
			 * buffer geometry) 
616
			 * buffer geometry)
602 617
			 * @return
603 618
			 */
604 619
			private DissolveVisitor createDissolveVisitor(FeaturePersisterProcessor2 processor) {
......
610 625
				else{
611 626
					//Esto no funciona bien. Para dissolver buffers
612 627
					//con varios rings, da errores topol?gicos.
613
					
628

  
614 629
					//La ?nica soluci?n es calcular 1? la uni?n, luego
615 630
					//y luego los multirings
616
					
631

  
617 632
					if(bufferVisitor.getTypeOfBuffer() == BufferVisitor.BUFFER_INSIDE_OUTSIDE_POLY){
618 633
						return new DissolveVisitor("FROM", processor);
619 634
					}else{

Also available in: Unified diff