Revision 1321 org.gvsig.raster.tools/trunk/org.gvsig.raster.tools/org.gvsig.raster.tools.algorithm/org.gvsig.raster.tools.algorithm.maskthreshold/src/main/java/org/gvsig/raster/tools/algorithm/maskthreshold/MaskthresholdProcess.java

View differences:

MaskthresholdProcess.java
22 22
package org.gvsig.raster.tools.algorithm.maskthreshold;
23 23

  
24 24
import java.awt.geom.Rectangle2D;
25
import java.io.File;
26 25
import java.util.HashMap;
27 26

  
28 27
import javax.swing.SwingUtilities;
......
47 46
 * 
48 47
 * Possible use cases:
49 48
 * <UL>
50
 * <LI>Fixed-oplayer:  A input layer, an operator layer and a fixed value if the condition is satisfied.
49
 * <LI><b>Fixed-oplayer:</b>  A input layer, an operator layer and a fixed value if the condition is satisfied.
51 50
 * If the condition is not satisfied the value is set to NoData</LI>
52 51
 * 
53
 * <LI>Fixed-threshold:  A input layer, a threshold and a fixed value if the condition is satisfied.
52
 * <LI><b>Fixed-threshold:</b>  A input layer, a threshold and a fixed value if the condition is satisfied.
54 53
 * If the condition is not satisfied the value is set to NoData</LI>
55 54
 *
56
 * <LI>Replicate-oplayer:  A input layer, an operator layer and a output layer. If the condition is satisfied.
55
 * <LI><b>Replicate-oplayer:</b>  A input layer, an operator layer and a output layer. If the condition is satisfied.
57 56
 * the result layer replicates the output bands. If the condition is not satisfied the values is set to NoData</LI>
58 57
 * 
59
 * <LI>Replicate-threshold:  A input layer, a threshold layer and a output layer. If the condition is satisfied.
58
 * <LI><b>Replicate-threshold:</b>  A input layer, a threshold layer and a output layer. If the condition is satisfied.
60 59
 * the result layer replicates the output bands. If the condition is not satisfied the values is set to NoData</LI>
61 60
 * 
62
 * <LI>Transparent-oplayer: A input layer, an operator layer and a RGB output layer. If the condition is satisfied
61
 * <LI><b>Transparent-oplayer:</b> A input layer, an operator layer and a RGB output layer. If the condition is satisfied
63 62
 * the layer result replicates the output RGB with transparency values. If the condition is not satisfied 
64 63
 * the layer result only replicates the output RGB.</LI>
65 64
 * 
66
 * <LI>Transparent-threshold: A input layer, a threshold and a RGB output layer. If the condition is satisfied
65
 * <LI><b>Transparent-threshold:</b> A input layer, a threshold and a RGB output layer. If the condition is satisfied
67 66
 * the layer result replicates the output RGB with transparency values. If the condition is not satisfied 
68 67
 * the layer result only replicates the output RGB.</LI>
69 68
 * 
70
 * <LI>Opacity-oplayer: A input layer, an operator layer, a RGB output layer. If the condition is satisfied
69
 * <LI><b>Opacity-oplayer:</b> A input layer, an operator layer, a RGB output layer. If the condition is satisfied
71 70
 * the layer result replicates the output layer values and if not this pixels are set to transparent </LI>
72 71
 * 
73
 * <LI>Opacity-threshold: A input layer, an operator layer, a threshold. If the condition is satisfied
72
 * <LI><b>Opacity-threshold:</b> A input layer, an operator layer, a threshold. If the condition is satisfied
74 73
 * the layer result replicates the output layer values and if not this pixels are set to transparent </LI>
75 74
 * </UL>
76 75
 * 
77
 *
76
 * <table>
77
 * <tr>
78
 * <th>band1</th><th>band2</th><th>threshold</th><th>output</th>
79
 * </tr>
80
 * <tr>
81
 * <td>-1</td><td>-1</td><td>-</td><td>el n?mero de bandas de la capa de operaci?n debe 
82
 *                                   ser mayor o igual a la de entrada. Compara la banda 
83
 *                                   i de la capa de entrada con la banda i de la de operaci?n</td>
84
 * </tr>
85
 * <tr>
86
 * <td>-1</td><td>0</td><td>-</td><td>Compara todas las bandas de la capa de entrada con la 
87
 *                                   banda cero de la capa de operaci?n</td>
88
 * </tr>
89
 * <tr>
90
 * <td>-1</td><td>-</td><td>value</td><td>Compara todas las bandas de la capa de entrada con el valor 
91
 *                                   de threshold</td>
92
 * </tr>
93
 * <tr>
94
 * <td>0</td><td>-1</td><td>-</td><td>Caso no permitido</td>
95
 * </tr>
96
 * <tr>
97
 * <td>0</td><td>-</td><td>value</td><td>Compara la banda cero de la capa de entrada con el valor
98
 *                                   de threshold</td>
99
 * </tr>                                  
100
 * </table>  
101
 * 
102
 * Input parameters
103
 * <UL>
104
 * <LI><b>RASTER_STORE1:</b>(Store method) Input store.</LI>
105
 * <LI><b>RASTER_STORE2:</b>(Store method) Operation store. If this parameter is null then THRESHOLD is mandatory</LI>
106
 * <LI><b>RASTER_STORE3:</b>(Store method) Output store. If this parameter is null then FIXED_VALUE is mandatory</LI>
107
 * <LI><b>RASTER_BUFFER1:</b>(Buffer method) Input buffer</LI>
108
 * <LI><b>RASTER_BUFFER2:</b>(Buffer method) Operation buffer. If this parameter is null then THRESHOLD is mandatory</LI>
109
 * <LI><b>RASTER_BUFFER3:</b>(Buffer method) Output buffer. If this parameter is null then FIXED_VALUE is mandatory</LI>
110
 * <LI><b>BAND1:</b> RASTER_STORE1 band. Band to compare with the threshold or operation band. If BAND1 is lesser than 0 then all
111
 * bands will be compared. If BAND1 is lesser than zero and BAND2 is lesser than zero then the store </LI>
112
 * <LI><b>BAND2:</b> RASTER_STORE2 band. Band to compare with the input store band</LI>
113
 * <LI><b>BAND3:</b> RASTER:STORE3 band. Band to put in the output when the method is "layer bands selecteds"</LI>
114
 * <LI><b>THRESHOLD:</b> Threshold to compare with the input data</LI>
115
 * <LI><b>FIXED_VALUE:</b> Value to put in the output when the method is "Fixed Value" or "NoData"</LI>
116
 * <LI><b>OPERATION:</b> Operation. Index in list MASK_OPERATIONS</LI>
117
 * <LI><b>SATISFY_METHOD:</b> Output method. Index in list OUTPUT_METHODS</LI>
118
 * <LI><b>PATH:</b></LI>
119
 * </UL>
120
 * 
121
 * Possible output parameters
122
 * <UL>
123
 * <LI><b>BUFFER:</b></LI>
124
 * <LI><b>FILENAME:</b></LI>
125
 * <LI><b>TIME:</b></LI>
126
 * </UL>
78 127
 * 10/12/2007
79 128
 * @author Nacho Brodin nachobrodin@gmail.com
80 129
 */
81 130
public class MaskthresholdProcess extends RasterProcess {
82 131
	//Possible input parameters
83
	public static String      RASTER_STORE1    = "RasterStore1";
84
	public static String      RASTER_STORE2    = "RasterStore2";
85
	public static String      RASTER_STORE3    = "RasterStore3";
86
	public static String      RASTER_BUFFER1   = "Buffer1";
87
	public static String      RASTER_BUFFER2   = "Buffer2";
88
	public static String      RASTER_BUFFER3   = "Buffer3";
89
	public static String      BAND1            = "Band1";
132
	public static String      RASTER_STORE1    = "RasterStore1"; //Input store
133
	public static String      RASTER_STORE2    = "RasterStore2"; //Operation store if exists (threshold)
134
	public static String      RASTER_STORE3    = "RasterStore3"; //Output store if exists (fixed value)
135
	public static String      RASTER_BUFFER1   = "Buffer1";      //Intput buffer
136
	public static String      RASTER_BUFFER2   = "Buffer2";      //Operation buffer if exists (threshold)
137
	public static String      RASTER_BUFFER3   = "Buffer3";      //Output buffer if exists (fixed value)
138
	public static String      BAND1            = "Band1";       
90 139
	public static String      BAND2            = "Band2";
91 140
	public static String      BAND3            = "Band3";
92 141
	public static String      THRESHOLD        = "Threshold";
......
142 191
		BAND3 = RasterBaseAlgorithmLibrary.registerInputParameter(BAND3, Integer.class);
143 192
		
144 193
		SATISFY_METHOD = RasterBaseAlgorithmLibrary.registerInputParameter(SATISFY_METHOD, Integer.class);
145
		
146 194
		THRESHOLD = RasterBaseAlgorithmLibrary.registerInputParameter(THRESHOLD, Double.class);
147 195
		FIXED_VALUE = RasterBaseAlgorithmLibrary.registerInputParameter(FIXED_VALUE, Double.class);
148 196
		OPERATION = RasterBaseAlgorithmLibrary.registerInputParameter(OPERATION, Integer.class);
149 197
		
150 198
		PATH = RasterBaseAlgorithmLibrary.registerInputParameter(PATH, String.class);
199
		
151 200
		FILENAME = RasterBaseAlgorithmLibrary.registerOutputParameter(FILENAME, String.class);
152 201
		BUFFER = RasterBaseAlgorithmLibrary.registerOutputParameter(BUFFER, Buffer.class);
202
		TIME = RasterBaseAlgorithmLibrary.registerOutputParameter(TIME, Long.class);
153 203
	}
154 204
	
155 205
	/*
......
168 218
		band1 = (Integer)getIntParam(BAND1); //Default 0
169 219
		band2 = (Integer)getIntParam(BAND2); //Default 0
170 220
		band3 = (Integer)getIntParam(BAND3); //Default 0
171
		
221

  
222
		//Caso no permitido. No se puede comparar una banda de la de entrada 
223
		//con todas de la capa de operaci?n. Evitamos el pete
224
		if(band2 < 0 && band1 >= 0) 
225
			band2 = 0;
226

  
172 227
		int satisfy = (Integer)getIntParam(SATISFY_METHOD); //Default 0
173
		
228

  
174 229
		if(satisfy == 0) {
175 230
			opSatisfy = new NoDataOp();
176 231
			opNotSatisfy = new FixedValueOp();
......
218 273
		threshold = (Double)getDoubleParam(THRESHOLD); //Default 0
219 274
		fixedValue = (Double)getDoubleParam(FIXED_VALUE); //Default 0
220 275
		
221
		if(getStringParam(FILENAME) != null) {
222
			filename = getStringParam(PATH) + File.separator + getStringParam(FILENAME);
223
			if(!filename.endsWith(".tif") && !filename.endsWith(".tiff"))
224
				filename += ".tif";
225
		}
276
		filename = getStringParam(PATH);
226 277
	}
227 278
	
228 279
	/**
......
245 296
				RasterDataStore[] storeList = new RasterDataStore[]{inputStore, operationStore, outputStore};
246 297
				pxBBox = getIntersectionInPxCoords(storeList);
247 298
			} else {
248
				pxBBox = new Rectangle2D[]{
249
					new Rectangle2D.Double(0, 0, inputBuffer.getWidth(), inputBuffer.getHeight())
250
					};
299
				Rectangle2D r = new Rectangle2D.Double(0, 0, inputBuffer.getWidth(), inputBuffer.getHeight());
300
				pxBBox = new Rectangle2D[]{r, r, r};
251 301
			}
252 302
			
253 303
			if(operationStore != null) 
......
320 370
				throw new MaskthresholdException("Extents don't intersect");
321 371
			if(inputStore.getCellSize() != operationStore.getCellSize())
322 372
				throw new MaskthresholdException("Cell sizes are not equal");
373
			if(band1 < 0 && band2 < 0) { // Si se comparan todas las bandas, la capa dos debe tener m?s bandas que la uno o las mismas
374
				if(outputStore.getBandCount() < inputStore.getBandCount())
375
					throw new MaskthresholdException("If exists operation layer and all bands have to be compared then the operation layer must have more or the same number of bands that the input layer.");
376
			}
323 377
		}
324 378
	}
325 379
	
......
340 394
		if (inputBuffer == null)
341 395
			throw new MaskthresholdException("Layer not valid");
342 396
		
343
		if(outputBuffer != null && 
344
		  ((inputBuffer.getWidth() != outputBuffer.getWidth()) || 
345
		   (inputBuffer.getHeight() != outputBuffer.getHeight()) ||
346
		   (inputStore.getCellSize() != outputStore.getCellSize())) )
347
			throw new MaskthresholdException("Buffer sizes not valid");
397
		if(outputBuffer != null) {
398
			if ((inputBuffer.getWidth() != outputBuffer.getWidth()) || 
399
					(inputBuffer.getHeight() != outputBuffer.getHeight()))
400
				throw new MaskthresholdException("Buffer sizes not valid");
401
			if(band1 < 0 && band2 < 0) { // Si se comparan todas las bandas, la capa dos debe tener m?s bandas que la uno o las mismas
402
				if(outputBuffer.getBandCount() < inputBuffer.getBandCount())
403
					throw new MaskthresholdException("If exists operation layer and all bands have to be compared then the operation layer must have more or the same number of bands that the input layer.");
404
			}
405
		}
348 406
	}
349 407
	
350 408
	/**
......
389 447
		opSatisfy.result = opNotSatisfy.result = result;
390 448
		opSatisfy.outBuffer = opNotSatisfy.outBuffer = outputBuffer;
391 449
		opSatisfy.band = opNotSatisfy.band = band3;
450
		
451
		int inputInitBand = band1 < 0 ? 0 : band1;
452
		int inputEndBand = band1 < 0 ? inputBuffer.getBandCount() : band1 + 1;
392 453

  
393 454
		for (int row = 0; row < result.getHeight(); row++) {
394 455
			if(pxBbox.length > 1) {
......
397 458
			}
398 459
			colSource = 0;
399 460
			for (int col = 0; col < result.getWidth(); col++) {
400
				double op1 = getValue(inputBuffer, rowSource, colSource);
401
				double op2 = getValue(operationBuffer, rowOperation, colOperation);
402
				if(operation.compare(op1, op2)) {
461
				boolean compare = true;
462
				for (int iBands = inputInitBand; iBands < inputEndBand; iBands++) {
463
					double op1 = getValue(inputBuffer, rowSource, colSource, iBands);
464
					double op2 = getValue(operationBuffer, rowOperation, colOperation, getBandToCompare(operationBuffer, iBands));
465
					if(!operation.compare(op1, op2)) {
466
						compare = false;
467
						break;
468
					}
469
				}
470
				if(compare) {
403 471
					opSatisfy.row = row;
404 472
					opSatisfy.col = col;
405 473
					opSatisfy.invoke();
......
418 486
	}
419 487

  
420 488
	/**
489
	 * Gets the band to operate. If the buffer is null then it does not 
490
	 * matter and it returns zero.
491
	 * @param buf
492
	 * @param inputBand
493
	 * @return
494
	 */
495
	private int getBandToCompare(Buffer buf, int inputBand) {
496
		if(buf == null)
497
			return 0;
498
		if(band2 < 0)
499
			return inputBand;
500
		else
501
			return band2;
502
	}
503

  
504
	/**
421 505
	 * Gets the operation value in using a double value. If there is a operation data store then
422 506
	 * returns the value of the pixel in this position else it returns a threshold value
423 507
	 * @param operationBuffer
......
425 509
	 * @param colOperation
426 510
	 * @return
427 511
	 */
428
	private double getValue(Buffer buf, int rowOperation, int colOperation) {
512
	private double getValue(Buffer buf, int rowOperation, int colOperation, int band) {
429 513
		if(buf == null)
430 514
			return threshold;
431 515
		if(buf.getDataType() == Buffer.TYPE_BYTE)
432
			return buf.getElemByte(rowOperation, colOperation, 0);
516
			return buf.getElemByte(rowOperation, colOperation, band);
433 517
		if(buf.getDataType() == Buffer.TYPE_FLOAT)
434
			return buf.getElemFloat(rowOperation, colOperation, 0);
518
			return buf.getElemFloat(rowOperation, colOperation, band);
435 519
		if(buf.getDataType() == Buffer.TYPE_DOUBLE)
436
			return buf.getElemDouble(rowOperation, colOperation, 0);
520
			return buf.getElemDouble(rowOperation, colOperation, band);
437 521
		if(buf.getDataType() == Buffer.TYPE_SHORT)
438
			return buf.getElemShort(rowOperation, colOperation, 0);
522
			return buf.getElemShort(rowOperation, colOperation, band);
439 523
		if(buf.getDataType() == Buffer.TYPE_INT)
440
			return buf.getElemInt(rowOperation, colOperation, 0);
524
			return buf.getElemInt(rowOperation, colOperation, band);
441 525
		return 0;
442 526
	}
443 527
	

Also available in: Unified diff