Revision 17836

View differences:

trunk/extensions/extRemoteSensing/src/org/gvsig/remotesensing/principalcomponents/PCImageProcess.java
42 42

  
43 43
import java.io.IOException;
44 44

  
45
import org.gvsig.gui.beans.incrementabletask.IIncrementable;
46
import org.gvsig.gui.beans.incrementabletask.IncrementableEvent;
47
import org.gvsig.gui.beans.incrementabletask.IncrementableListener;
48
import org.gvsig.gui.beans.incrementabletask.IncrementableTask;
45
import org.gvsig.fmap.raster.layers.FLyrRasterSE;
49 46
import org.gvsig.raster.buffer.RasterBuffer;
50
import org.gvsig.raster.buffer.RasterBufferInvalidAccessException;
51 47
import org.gvsig.raster.dataset.GeoRasterWriter;
52 48
import org.gvsig.raster.dataset.IBuffer;
53
import org.gvsig.raster.dataset.IRasterDataSource;
54 49
import org.gvsig.raster.dataset.NotSupportedExtensionException;
55 50
import org.gvsig.raster.dataset.io.RasterDriverException;
56 51
import org.gvsig.raster.grid.Grid;
57 52
import org.gvsig.raster.grid.GridException;
58 53
import org.gvsig.raster.grid.GridExtent;
59
import org.gvsig.raster.process.CancelEvent;
60
import org.gvsig.raster.process.RasterTask;
61
import org.gvsig.raster.process.RasterTaskQueue;
62 54
import org.gvsig.raster.util.RasterNotLoadException;
63 55
import org.gvsig.raster.util.RasterToolsUtil;
64
import org.gvsig.rastertools.RasterModule;
56
import org.gvsig.rastertools.RasterProcess;
65 57
import org.gvsig.rastertools.clipping.WriterBufferServer;
66 58

  
67 59
import Jama.Matrix;
......
77 69
 *	@author Diego Guerrero Sevilla (diego.guerrero@uclm.es)
78 70
 *	@version 19/10/2007 
79 71
 */
80
public class PCImageProcess implements Runnable, IIncrementable, IncrementableListener{
81

  
82
	private IncrementableTask incrementableTask = null;
72
public class PCImageProcess extends RasterProcess{
83 73
	
84 74
	private double[] 			autovalues 			= null;
85 75
	private boolean[] 			selectedPCs 		= null;
......
88 78
	private Matrix 				autoVectorMatrix	= null;
89 79
	private RasterBuffer		rasterResult		= null;
90 80
	private int 				percent 		  	= 0;
91
	private Thread 				blinker				= null;
92 81
	private WriterBufferServer  writerBufferServer;
93
	private RasterTask			rasterTask 			= new RasterTask(this);
94 82
	private View 				view				=null;
95
	private IRasterDataSource	raster				=null;
83
	private FLyrRasterSE		raster				=null;
96 84
	
97 85

  
98 86
	/**
99 87
	 * Connstructor
100
	 * @param pcProcess		proceso de estadisticas 
101
	 * @param seledtedPCs	componentes seleccionados
102
	 * @param layerName		path con el fichero de salida
103 88
	 */
104
	public PCImageProcess(PCStatisticsProcess pcProcess, boolean seledtedPCs[], String layerName) {
105
		this.inputGrid = pcProcess.getInputGrid();
106
		this.autovalues = pcProcess.getResult();
107
		this.autoVectorMatrix = pcProcess.getAutoVectorMatrix();
108
		this.selectedPCs = seledtedPCs;
109
		this.layerName = layerName;
110
		this.raster= pcProcess.getRaster();
111
		this.view= pcProcess.getView();
89
	public PCImageProcess() {
112 90
	}
113 91
	
92
	
114 93
	/**
115
	 * Lanza el proceso.
94
	 * @return buffer resultante tras la transformacion
116 95
	 */
117
	public void start() {
118
		blinker = new Thread(this);
119
		blinker.start();
96
	public RasterBuffer getBufferResult(){
97
		return rasterResult;	
120 98
	}
121 99
	
100
	/*
101
	 * (non-Javadoc)
102
	 * @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getLabel()
103
	 */
104
	public String getLabel() {
105
		return  PluginServices.getText(this,"procesando");
106
	}
107

  
108
	/*
109
	 * (non-Javadoc)
110
	 * @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getLog()
111
	 */
112
	public String getLog() {
113
		if (writerBufferServer==null)
114
			return PluginServices.getText(this,"generando_pcs")+"...";
115
		else
116
			return PluginServices.getText(this,"escribiendo_resultado")+"...";
117
	}
118

  
119
	/*
120
	 * (non-Javadoc)
121
	 * @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getPercent()
122
	 */
123
	public int getPercent() {
124
		if(writerBufferServer==null)
125
			return percent;
126
		return writerBufferServer.getPercent();
127
	}
128

  
122 129
	
130
	/*
131
	 * (non-Javadoc)
132
	 * @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getTitle()
133
	 */
134
	public String getTitle() {
135
		return PluginServices.getText(this,"principal_components");
136
	}
137

  
123 138
	/**
124
	 * Ejecuta el proceso.
139
	 * Par?metros obligatorios al proceso:
140
	 * <UL>
141
	 * <LI>raster: raster con la capa original</LI>
142
	 * <LI>grid: grid solo con las bandas seleccionadas </LI>
143
	 * <LI>autovalues: vector de autovalores </LI>
144
	 * <LI>autoVectorMatrix: matriz de autovectores  </LI>
145
	 * <LI>selectedPCs:  componentes seleccionados </LI>
146
	 * <LI>layerName: nombre fichero de salida </LI>
147
	 * <LI>view:vista sobre la que se carga la capa resultante </LI>
148
	 * </UL> 
125 149
	 */
126
	public void run() {
150
	public void init() {
151
		raster= (FLyrRasterSE)getLayerParam("layer");
152
		inputGrid= (Grid)getParam("grid");
153
		autovalues= (double[])getParam("autovalores");
154
		autoVectorMatrix= (Matrix) getParam("autovectores");
155
		selectedPCs= (boolean[]) getParam("compselected");
156
		layerName= getStringParam("filename");
157
		view= (View)getParam("view");
158
	}
159

  
160

  
161
	public void process() throws InterruptedException {
127 162
		
128
		RasterTaskQueue.register(rasterTask);
129
		
130
		try{
131 163
			//	ResultExtent de la primera banda del array 
132 164
			GridExtent layerExtent = null;
133 165
			layerExtent= inputGrid.getGridExtent();
......
322 354
		
323 355
			// escritutra a fichero
324 356
			writeToFile();		
325
		}finally{
326
			incrementableTask.processFinalize();
327
			RasterTaskQueue.remove(rasterTask);
328
		}
329 357
	}
330 358
	
359
	
331 360
	/**
332 361
	 * Escritura del resultado en disco.
333 362
	 */
......
338 367
			int endIndex = fileName.lastIndexOf(".");
339 368
			if (endIndex < 0)
340 369
				 endIndex = fileName.length();
341
			 
342 370
			GeoRasterWriter grw = null;
343 371
			writerBufferServer = new WriterBufferServer(rasterResult);
344 372
			grw = GeoRasterWriter.getWriter(writerBufferServer, layerName, rasterResult.getBandCount(),raster.getAffineTransform(), rasterResult.getWidth(), rasterResult.getHeight(), rasterResult.getDataType(), GeoRasterWriter.getWriter(layerName).getParams(), null);
......
348 376
			rasterResult.free();
349 377
			String viewName = PluginServices.getMDIManager().getWindowInfo(view).getTitle();
350 378
			RasterToolsUtil.loadLayer(viewName, layerName, null);
351
			
352
			RasterModule.layerCount++;
353
				
379
	
354 380
		} catch (NotSupportedExtensionException e) {
355 381
			RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_writer_notsupportedextension"), this, e);
356 382
		} catch (IOException e) {
......
360 386
		} catch (RasterNotLoadException e) {
361 387
				RasterToolsUtil.messageBoxError("error_cargar_capa", this, e);
362 388
		} catch (RasterDriverException e) {
363
			// TODO Auto-generated catch block
364 389
			e.printStackTrace();
365 390
		}
366
	}
367
	
368
	
369
	/**
370
	 * @return buffer resultante tras la transformacion
371
	 */
372
	public RasterBuffer getBufferResult(){
373
		return rasterResult;
374
		
375
	}
376
	
377
	/*
378
	 * (non-Javadoc)
379
	 * @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getLabel()
380
	 */
381
	public String getLabel() {
382
		return  PluginServices.getText(this,"procesando");
383
	}
384 391

  
385
	/*
386
	 * (non-Javadoc)
387
	 * @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getLog()
388
	 */
389
	public String getLog() {
390
		if (writerBufferServer==null)
391
			return PluginServices.getText(this,"generando_pcs")+"...";
392
		else
393
			return PluginServices.getText(this,"escribiendo_resultado")+"...";
394 392
	}
395

  
396
	/*
397
	 * (non-Javadoc)
398
	 * @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getPercent()
399
	 */
400
	public int getPercent() {
401
		if(writerBufferServer==null)
402
			return percent;
403
		return writerBufferServer.getPercent();
404
	}
405

  
406 393
	
407
	/*
408
	 * (non-Javadoc)
409
	 * @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getTitle()
410
	 */
411
	public String getTitle() {
412
		return PluginServices.getText(this,"principal_components");
413
	}
394
}
414 395

  
415 396
	
416
	/*
417
	 * (non-Javadoc)
418
	 * @see org.gvsig.gui.beans.incrementabletask.IIncrementable#actionCanceled
419
	 */
420
	public void actionCanceled(IncrementableEvent e) {
421
			rasterTask.setEvent(new CancelEvent(this));
422
	}
423 397

  
424
	
425
	/*
426
	 * (non-Javadoc)
427
	 * @see org.gvsig.gui.beans.incrementabletask.IIncrementable#actionResumed
428
	 */
429
	public void actionResumed(IncrementableEvent e) {
430
	}
431

  
432
	/*
433
	 * (non-Javadoc)
434
	 * @see org.gvsig.gui.beans.incrementabletask.IIncrementable#actionSuspended
435
	 */
436
	public void actionSuspended(IncrementableEvent e) {
437
	}
438
	
439
	
440
	/*
441
	 * (non-Javadoc)
442
	 * @see org.gvsig.gui.beans.incrementabletask.IIncrementable#setIncrementableTask
443
	 */
444
	public void setIncrementableTask(IncrementableTask incrementableTask) {
445
		this.incrementableTask = incrementableTask;
446
		this.incrementableTask.addIncrementableListener(this);
447
	}
448

  
449
	
450
}
trunk/extensions/extRemoteSensing/src/org/gvsig/remotesensing/principalcomponents/PCStatisticsProcess.java
40 40

  
41 41
package org.gvsig.remotesensing.principalcomponents;
42 42

  
43
import org.gvsig.gui.beans.incrementabletask.IIncrementable;
44
import org.gvsig.gui.beans.incrementabletask.IncrementableEvent;
45
import org.gvsig.gui.beans.incrementabletask.IncrementableListener;
46
import org.gvsig.gui.beans.incrementabletask.IncrementableTask;
43
import org.gvsig.fmap.raster.layers.FLyrRasterSE;
47 44
import org.gvsig.raster.buffer.BufferFactory;
48 45
import org.gvsig.raster.buffer.RasterBuffer;
49
import org.gvsig.raster.buffer.RasterBufferInvalidAccessException;
50 46
import org.gvsig.raster.buffer.RasterBufferInvalidException;
51 47
import org.gvsig.raster.dataset.IRasterDataSource;
52 48
import org.gvsig.raster.grid.Grid;
53 49
import org.gvsig.raster.grid.GridException;
54
import org.gvsig.raster.process.CancelEvent;
55
import org.gvsig.raster.process.RasterTask;
56
import org.gvsig.raster.process.RasterTaskQueue;
57 50
import org.gvsig.raster.util.RasterToolsUtil;
51
import org.gvsig.rastertools.RasterProcess;
58 52
import org.gvsig.remotesensing.principalcomponents.gui.PrincipalComponentCalculusPanel;
59 53

  
60 54
import Jama.EigenvalueDecomposition;
......
73 67
 *	@version 19/10/2007 
74 68
 */
75 69

  
76
public class PCStatisticsProcess implements Runnable, IIncrementable, IncrementableListener{
70
public class PCStatisticsProcess extends RasterProcess{
77 71
	
78 72
	private Grid 								inputGrid 			= null;
79 73
	private double								autovalors[]			= null;
80 74
	private Matrix 								coVarMatrix			= null;
81 75
	private Matrix 								autoVectorMatrix	= null;
82
	private IncrementableTask					incrementableTask 	= null;
83 76
	private int 								percent 		  	= 0;
84 77
	private boolean 							cancel 				= false;
85
	private Thread 								blinker				= null;
86
	private View 								view				= null;
87
	private RasterTask							rasterTask = new RasterTask(this);
88
	private IRasterDataSource					raster				= null;
89 78
	private	boolean 							selectedBands[]		 =null;
79
	private FLyrRasterSE						rasterSE			= null;
80
	private View								view				= null;
90 81
	
91
	
92 82
	/**
93 83
	 * Constructor
94
	 * @param raster raster al que se aplica la transformacion de PC
95
	 * @param view vista actual de la aplicacion
96
	 * @param selectedBands bandas  de la imagen seleccionadas para el proceso
97 84
	 */
98
	public PCStatisticsProcess(IRasterDataSource raster,View view,boolean selectedBands[]) {
99
		this.raster= raster;
100
		this.view = view;
101
		this.selectedBands= selectedBands;
85
	public PCStatisticsProcess() {
102 86
	}
103 87
	
104 88
	
......
108 92
	public void calculate() {
109 93
		 //Calculo de matriz de covarianza:
110 94
		double coVar[][]= covarianceOptimize();
111
		
112 95
		 // Calculo de autovectores:
113 96
		coVarMatrix = new Matrix(coVar);
114 97
		EigenvalueDecomposition eigenvalueDecomp = new EigenvalueDecomposition(coVarMatrix); 
115
		
116 98
		//Autovectores
117 99
		autoVectorMatrix= eigenvalueDecomp.getV();
118
    	
119 100
		// Autovalores
120 101
		autovalors= eigenvalueDecomp.getRealEigenvalues();	
121 102
	}
......
361 342
		return coV;
362 343
	}
363 344
	
364
	
365
	/**
366
	 * Lanza el proceso.
367
	 *
368
	 */
369
	public void start() {
370
		cancel = false;
371
		blinker = new Thread(this);
372
		blinker.start();
373
	}
374
	
375
	
376
	/**
377
	 * Proceso.
378
	 */
379
	public void run() {
380
		
381
		RasterTaskQueue.register(rasterTask); //Registro de la tarea
382
		
383
		try{
384
			// Se establece el grid con las bandas seleccionadas
385
			setGrid();
386
			//	Calculo de matriz de covarianza:
387
			double coVar[][]= covarianceOptimize();
388
			
389
			 // Calculo de autovectores:
390
			coVarMatrix = new Matrix(coVar);
391
			EigenvalueDecomposition eigenvalueDecomp = new EigenvalueDecomposition(coVarMatrix); 
392
			//Autovectores
393
			autoVectorMatrix= eigenvalueDecomp.getV();
394
	    
395
			// Autovalores
396
			autovalors= eigenvalueDecomp.getRealEigenvalues();	
397 345

  
398
			}finally{
399
				
400
				if (incrementableTask!=null){
401
					incrementableTask.processFinalize();
402
				}
403
				
404
				if(!cancel){
405
					PrincipalComponentCalculusPanel altPrincipalComponentCalculusPanel = new PrincipalComponentCalculusPanel(view,this);
406
					PluginServices.getMDIManager().addWindow(altPrincipalComponentCalculusPanel);
407
				}
408
				RasterTaskQueue.remove(rasterTask);
409
			}
410
	}
411

  
412 346
	
413 347
	/**
414 348
	 * Construye el grid con las bandas seleccionadas
415 349
	 */
416 350
	public void setGrid(){
417 351
	
418
		BufferFactory dataSource = new BufferFactory(raster);	
352
		IRasterDataSource dsetCopy = null; 
353
		dsetCopy = rasterSE.getDataSource().copy();
354
		BufferFactory bufferFactory = new BufferFactory(dsetCopy);
355
	
419 356
		int longitud=0;
420 357
		for (int i=0; i<selectedBands.length;i++)
421 358
				if (selectedBands[i]) longitud++;
......
428 365
				     j++;
429 366
				}
430 367
		try {
431
				inputGrid = new Grid(dataSource, bands);	
368
				inputGrid = new Grid(bufferFactory, bands);	
432 369
		} catch (RasterBufferInvalidException e) {
433 370
					e.printStackTrace();			
434 371
		}
......
468 405
		return PluginServices.getText(this,"principal_components");
469 406
	}
470 407

  
471
	/*
472
	 * (non-Javadoc)
473
	 * @see org.gvsig.gui.beans.incrementabletask.IIncrementable#actionCanceled
408
	/**
409
	 * @return grid 
474 410
	 */
475
	public void actionCanceled(IncrementableEvent e) {
476
		cancel=true;
477
		rasterTask.setEvent(new CancelEvent(this));	
411
	public Grid getInputGrid() {
412
		return inputGrid;
478 413
	}
479

  
480
	/*
481
	 * (non-Javadoc)
482
	 * @see org.gvsig.gui.beans.incrementabletask.IIncrementable#actionResumed
483
	 */
484
	public void actionResumed(IncrementableEvent e) {
414
	
415
	/**
416
	 * @return raster de entrada
417
	 * */
418
	public FLyrRasterSE getRasterLayer(){
419
		return rasterSE;
485 420
	}
486 421

  
487
	/*
488
	 * (non-Javadoc)
489
	 * @see org.gvsig.gui.beans.incrementabletask.IIncrementable#actionSuspended
490
	 */
491
	public void actionSuspended(IncrementableEvent e) {
492
	}
493 422
	
494
	
495
	/*
496
	 * (non-Javadoc)
497
	 * @see org.gvsig.gui.beans.incrementabletask.IIncrementable#setIncrementableTask
498
	 */
499
	public void setIncrementableTask(IncrementableTask incrementableTask) {
500
		this.incrementableTask = incrementableTask;
501
		this.incrementableTask.addIncrementableListener(this);
423
	public void init() {
424
		// Se toman los parametros del proceso
425
		selectedBands = (boolean []) getParam("selectedBands");
426
		view = (View)getParam("view");
427
		rasterSE = getLayerParam("layer");
428
		
502 429
	}
503 430

  
504 431

  
505 432
	/**
506
	 * @return grid 
507
	 */
508
	public Grid getInputGrid() {
509
		return inputGrid;
433
	 *  Proceso de calculo de estadisticas para Principal Component
434
	 * */
435
	public void process() throws InterruptedException {
436
		try{
437
			// Se establece el grid con las bandas seleccionadas
438
			setGrid();
439
			//	Calculo de matriz de covarianza:
440
			double coVar[][]= covarianceOptimize();
441
			 // Calculo de autovectores:
442
			coVarMatrix = new Matrix(coVar);
443
			EigenvalueDecomposition eigenvalueDecomp = new EigenvalueDecomposition(coVarMatrix); 
444
			//Autovectores
445
			autoVectorMatrix= eigenvalueDecomp.getV();
446
			// Autovalores
447
			autovalors= eigenvalueDecomp.getRealEigenvalues();	
448

  
449
			}finally{
450
				if(!cancel){
451
					incrementableTask.processFinalize();
452
					PrincipalComponentCalculusPanel altPrincipalComponentCalculusPanel = new PrincipalComponentCalculusPanel(view,this);
453
					PluginServices.getMDIManager().addWindow(altPrincipalComponentCalculusPanel);
454
				}
455
			}
456
		
510 457
	}
511 458
	
512
	
513 459
	/**
514
	 * @return r?ster de entrada
515
	 */
516
	public IRasterDataSource getRaster(){
517
		return raster;	
518
	}
519
	
520
	/**
521 460
	 * @return vista de la aplicacion
522 461
	 */
523 462
	public View getView(){

Also available in: Unified diff