Revision 1289 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
21 21
*/
22 22
package org.gvsig.raster.tools.algorithm.maskthreshold;
23 23

  
24
import java.awt.geom.Point2D;
24
import java.awt.geom.Rectangle2D;
25 25
import java.util.HashMap;
26 26

  
27 27
import javax.swing.SwingUtilities;
28 28

  
29 29
import org.gvsig.fmap.dal.coverage.RasterLocator;
30 30
import org.gvsig.fmap.dal.coverage.dataset.Buffer;
31
import org.gvsig.fmap.dal.coverage.datastruct.Extent;
32 31
import org.gvsig.fmap.dal.coverage.datastruct.NoData;
33 32
import org.gvsig.fmap.dal.coverage.exception.InvalidSetViewException;
34 33
import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException;
......
92 91
	public static String      THRESHOLD        = "Threshold";
93 92
	public static String      FIXED_VALUE      = "FixedValue";
94 93
	public static String      OPERATION        = "Operation";
95
	public static String      SATISFY          = "Satisfy";
94
	public static String      SATISFY_METHOD   = "Satisfy_Method";
96 95
	
97 96
	public static String      PATH             = "Path";
98 97
	public static String      FILENAME         = "FileName";
99 98
	public static String      TIME             = "Time";
100 99
	
101
	public static String[]    MASK_METHODS     = new String[]{"Greater", "Lower", 
100
	public static String[]    MASK_OPERATIONS  = new String[]{"Greater", "Lower", 
102 101
		                                                      "GreaterOrEquals", "LoweOrEquals"};
103 102
	public static String[]    OUTPUT_METHODS   = new String[]{"NoData", "FixedValue", 
104 103
		                                                      "Transparency", "Opacity", 
......
139 138
		BAND2 = RasterBaseAlgorithmLibrary.registerInputParameter(BAND2, Integer.class);
140 139
		BAND3 = RasterBaseAlgorithmLibrary.registerInputParameter(BAND3, Integer.class);
141 140
		
142
		SATISFY = RasterBaseAlgorithmLibrary.registerInputParameter(SATISFY, Integer.class);
141
		SATISFY_METHOD = RasterBaseAlgorithmLibrary.registerInputParameter(SATISFY_METHOD, Integer.class);
143 142
		
144 143
		THRESHOLD = RasterBaseAlgorithmLibrary.registerInputParameter(THRESHOLD, Double.class);
145 144
		FIXED_VALUE = RasterBaseAlgorithmLibrary.registerInputParameter(FIXED_VALUE, Double.class);
......
166 165
		band2 = (Integer)getIntParam(BAND2); //Default 0
167 166
		band3 = (Integer)getIntParam(BAND3); //Default 0
168 167
		
169
		int satisfy = (Integer)getIntParam(SATISFY); //Default 0
168
		int satisfy = (Integer)getIntParam(SATISFY_METHOD); //Default 0
170 169
		
171 170
		if(satisfy == 0) {
172 171
			opSatisfy = new NoDataOp();
......
232 231
			else
233 232
				checkBuffer();
234 233
			
235
			Buffer sourceBuffer = inputBuffer != null ? inputBuffer : readROBuffer(inputStore, band1);
236
			Buffer operationBuffer = null;
237 234
			Buffer resultBuffer = null;
238
			Buffer outBuffer = null;
235
			Rectangle2D[] pxBBox = null;
239 236
			
240
			Point2D init1 = new Point2D.Double(0, 0);
241
			Point2D end1 = new Point2D.Double(inputStore.getWidth() - 1, inputStore.getHeight() - 1);
242
			int[] beginStore1 = new int[2];
243
			int[] beginStore2 = new int[2];
244
			
245
			if(operationStore != null) {
246
				if(!inputStore.getExtent().intersects(operationStore.getExtent()))
247
					throw new MaskthresholdException("Extents don't intersect");
248
				if(inputStore.getCellSize() != operationStore.getCellSize())
249
					throw new MaskthresholdException("Cell sizes are not equal");
250
				
251
				Point2D init2 = new Point2D.Double(0, 0);
252
				Point2D end2 = new Point2D.Double(operationStore.getWidth() - 1, operationStore.getHeight() - 1);
253
				
254
				Extent extentIntersect = inputStore.getExtent().intersection(operationStore.getExtent());
255
				Point2D p1 = new Point2D.Double(extentIntersect.getULX(), extentIntersect.getULY());
256
				Point2D p2 = new Point2D.Double(extentIntersect.getLRX(), extentIntersect.getLRY());
257
				init1 = inputStore.worldToRaster(p1);
258
				end1 = inputStore.worldToRaster(p2);
259
				init2 = operationStore.worldToRaster(p1);
260
				end2 = operationStore.worldToRaster(p2);
261
				
262
				beginStore2[0] = (int)init2.getX();
263
				beginStore2[1] = (int)init2.getY();
264
				
265
				int w = (int)Math.max(end1.getX() - init1.getX(), end2.getX() - init2.getX());
266
				int h = (int)Math.max(end1.getY() - init1.getY(), end2.getY() - init2.getY());
267
				
268
				resultBuffer = RasterLocator.getManager().createBuffer(
269
						inputStore.getDataType()[0], w, h, resultNumBands, true);
270
				
271
				operationBuffer = readROBuffer(operationStore, band2);
237
			if(inputStore != null) {
238
				inputBuffer = readROBuffer(inputStore, band1);
239
				RasterDataStore[] storeList = new RasterDataStore[]{inputStore, operationStore, outputStore};
240
				pxBBox = getIntersectionInPxCoords(storeList);
272 241
			} else {
273
				resultBuffer = RasterLocator.getManager().createBuffer(
274
						inputStore.getDataType()[0], (int)inputStore.getWidth(), (int)inputStore.getWidth(), resultNumBands, true);
242
				pxBBox = new Rectangle2D[]{
243
					new Rectangle2D.Double(0, 0, inputBuffer.getWidth(), inputBuffer.getHeight())
244
					};
275 245
			}
276 246
			
277
			if(outputStore != null) {
278
				if(inputStore.getCellSize() != outputStore.getCellSize())
279
					throw new MaskthresholdException("Cell sizes are not equal");
280
				if( inputStore.getExtent().intersects(operationStore.getExtent()) && 
281
						operationStore.getExtent().intersects(outputStore.getExtent()) && 
282
						outputStore.getExtent().intersects(inputStore.getExtent())) {
283
					outBuffer = readROBuffer(outputStore, band3);
284
				} else { 
285
					throw new MaskthresholdException("Extents don't intersect");
286
				}
247
			if(operationStore != null) 
248
				operationBuffer = readROBuffer(operationStore, band2);
249
			
250
			int dataType = Buffer.TYPE_DOUBLE;
251
			
252
			if(outputStore != null) { 
253
				outputBuffer = readROBuffer(outputStore, band3);
254
				dataType = outputBuffer.getDataType();
287 255
			}
288

  
289
			beginStore1[0] = (int)init1.getX();
290
			beginStore1[1] = (int)init1.getY();
291 256
			
292
			write(sourceBuffer, 
293
					operationBuffer, 
294
					outBuffer,
295
					resultBuffer, 
296
					beginStore1, 
297
					beginStore2);
257
			resultBuffer = RasterLocator.getManager().createBuffer(
258
					dataType, 
259
					(int)pxBBox[0].getWidth(), 
260
					(int)pxBBox[0].getHeight(), 
261
					resultNumBands, true);
298 262
			
263
			write(resultBuffer, pxBBox);
264
			
299 265
			exportRaster(filename, resultBuffer, inputStore.getCellSize(), 
300 266
					inputStore.getExtent().getULX(), inputStore.getExtent().getULY());
301 267
			
......
343 309
			throw new MaskthresholdException("Not valid parameters. Layer method have output data store");
344 310

  
345 311
		if (inputStore == null)
346
			throw new MaskthresholdException("Capa no valida.");
312
			throw new MaskthresholdException("Layer not valid");
313
		
314
		if(operationStore != null) {
315
			if(!inputStore.getExtent().intersects(operationStore.getExtent()))
316
				throw new MaskthresholdException("Extents don't intersect");
317
			if(inputStore.getCellSize() != operationStore.getCellSize())
318
				throw new MaskthresholdException("Cell sizes are not equal");
319
		}
347 320
	}
348 321
	
349 322
	/**
......
361 334
			throw new MaskthresholdException("Not valid parameters. Layer method have output data store");
362 335

  
363 336
		if (inputBuffer == null)
364
			throw new MaskthresholdException("Capa no valida.");
337
			throw new MaskthresholdException("Layer not valir");
338
		
339
		if(outputBuffer != null && 
340
		  ((inputBuffer.getWidth() != outputBuffer.getWidth()) || 
341
		   (inputBuffer.getHeight() != outputBuffer.getHeight()) ||
342
		   (inputStore.getCellSize() != outputStore.getCellSize())) )
343
			throw new MaskthresholdException("Buffer sizes not valid");
365 344
	}
366 345
	
367 346
	/**
......
390 369
		}
391 370
	}
392 371
	
393
	private void write(
394
			Buffer sourceBuffer, 
395
			Buffer operationBuffer, 
396
			Buffer outBuffer, 
397
			Buffer result, 
398
			int[] initSource, 
399
			int[] initOperation) {
372
	private void write(Buffer result, Rectangle2D[] pxBbox) {
400 373
		if(nodata == null)
401 374
			nodata = RasterLocator.getManager().getDataStructFactory().createDefaultNoData(1, result.getDataType());
402
		int colSource = initSource[0];
403
		int rowSource = initSource[1];
404
		int colOperation = initOperation[0];
405
		int rowOperation = initOperation[1];
375
		int colSource = (int)pxBbox[0].getX();
376
		int rowSource = (int)pxBbox[0].getY();
377
		int colOperation = colSource;
378
		int rowOperation = rowSource;
379
		if(pxBbox.length > 1) {
380
			colOperation = (int)pxBbox[1].getX();
381
			rowOperation = (int)pxBbox[1].getY();
382
		}
406 383

  
407 384
		opSatisfy.fixedValue = opNotSatisfy.fixedValue = fixedValue;
408 385
		opSatisfy.result = opNotSatisfy.result = result;
409
		opSatisfy.outBuffer = opNotSatisfy.outBuffer = outBuffer;
386
		opSatisfy.outBuffer = opNotSatisfy.outBuffer = outputBuffer;
410 387
		opSatisfy.band = opNotSatisfy.band = band3;
411 388

  
412 389
		for (int row = 0; row < result.getHeight(); row++) {
413
			colOperation = initOperation[0];
414
			colSource = initSource[0];
390
			colOperation = (int)pxBbox[1].getX();
391
			colSource = (int)pxBbox[0].getX();
415 392
			for (int col = 0; col < result.getWidth(); col++) {
416
				double op1 = getOperationValue(sourceBuffer, rowSource, colSource);
417
				double op2 = getOperationValue(operationBuffer, rowOperation, colOperation);
393
				double op1 = getValue(inputBuffer, rowSource, colSource);
394
				double op2 = getValue(operationBuffer, rowOperation, colOperation);
418 395
				if(operation.compare(op1, op2)) {
419 396
					opSatisfy.row = row;
420 397
					opSatisfy.col = col;
......
441 418
	 * @param colOperation
442 419
	 * @return
443 420
	 */
444
	private double getOperationValue(Buffer operationBuffer, int rowOperation, int colOperation) {
445
		if(operationBuffer == null)
421
	private double getValue(Buffer buf, int rowOperation, int colOperation) {
422
		if(buf == null)
446 423
			return threshold;
447
		if(operationBuffer.getDataType() == Buffer.TYPE_BYTE)
448
			return operationBuffer.getElemByte(rowOperation, colOperation, 0);
449
		if(operationBuffer.getDataType() == Buffer.TYPE_FLOAT)
450
			return operationBuffer.getElemFloat(rowOperation, colOperation, 0);
451
		if(operationBuffer.getDataType() == Buffer.TYPE_DOUBLE)
452
			return operationBuffer.getElemDouble(rowOperation, colOperation, 0);
453
		if(operationBuffer.getDataType() == Buffer.TYPE_SHORT)
454
			return operationBuffer.getElemShort(rowOperation, colOperation, 0);
455
		if(operationBuffer.getDataType() == Buffer.TYPE_INT)
456
			return operationBuffer.getElemInt(rowOperation, colOperation, 0);
424
		if(buf.getDataType() == Buffer.TYPE_BYTE)
425
			return buf.getElemByte(rowOperation, colOperation, 0);
426
		if(buf.getDataType() == Buffer.TYPE_FLOAT)
427
			return buf.getElemFloat(rowOperation, colOperation, 0);
428
		if(buf.getDataType() == Buffer.TYPE_DOUBLE)
429
			return buf.getElemDouble(rowOperation, colOperation, 0);
430
		if(buf.getDataType() == Buffer.TYPE_SHORT)
431
			return buf.getElemShort(rowOperation, colOperation, 0);
432
		if(buf.getDataType() == Buffer.TYPE_INT)
433
			return buf.getElemInt(rowOperation, colOperation, 0);
457 434
		return 0;
458 435
	}
459 436
	
......
504 481
		public void invoke() {
505 482
			if(result.getDataType() == Buffer.TYPE_BYTE) {
506 483
				if(band < 0) {
507
					for (int i = 0; i < band; i++) {
484
					for (int i = 0; i < result.getBandCount(); i++) {
508 485
						byte vSource = outBuffer.getElemByte(row, col, i);
509 486
						result.setElem(row, col, i, vSource);
510 487
					}
......
514 491
				}
515 492
			} else if(result.getDataType() == Buffer.TYPE_FLOAT) {
516 493
				if(band < 0) {
517
					for (int i = 0; i < band; i++) {
494
					for (int i = 0; i < result.getBandCount(); i++) {
518 495
						float vSource = outBuffer.getElemFloat(row, col, i);
519 496
						result.setElem(row, col, i, vSource);
520 497
					}
......
524 501
				}
525 502
			} else if(result.getDataType() == Buffer.TYPE_DOUBLE) {
526 503
				if(band < 0) {
527
					for (int i = 0; i < band; i++) {
504
					for (int i = 0; i < result.getBandCount(); i++) {
528 505
						double vSource = outBuffer.getElemDouble(row, col, i);
529 506
						result.setElem(row, col, i, vSource);
530 507
					}
......
534 511
				}
535 512
			} else if(result.getDataType() == Buffer.TYPE_SHORT) {
536 513
				if(band < 0) {
537
					for (int i = 0; i < band; i++) {
514
					for (int i = 0; i < result.getBandCount(); i++) {
538 515
						float vSource = outBuffer.getElemShort(row, col, i);
539 516
						result.setElem(row, col, i, vSource);
540 517
					}
......
544 521
				}
545 522
			} else if(result.getDataType() == Buffer.TYPE_INT) {
546 523
				if(band < 0) {
547
					for (int i = 0; i < band; i++) {
524
					for (int i = 0; i < result.getBandCount(); i++) {
548 525
						int vSource = outBuffer.getElemInt(row, col, i);
549 526
						result.setElem(row, col, i, vSource);
550 527
					}
......
599 576
		public void invoke() {
600 577
			if(result.getDataType() == Buffer.TYPE_BYTE)
601 578
				if(band < 0) {
602
					for (int i = 0; i < band; i++) {
579
					for (int i = 0; i < result.getBandCount(); i++) {
603 580
						result.setElem(row, col, i, nodata.getValue().byteValue());
604 581
					}
605 582
				} else {
......
607 584
				}
608 585
			else if(result.getDataType() == Buffer.TYPE_FLOAT)
609 586
				if(band < 0) {
610
					for (int i = 0; i < band; i++) {
587
					for (int i = 0; i < result.getBandCount(); i++) {
611 588
						result.setElem(row, col, i, nodata.getValue().floatValue());
612 589
					}
613 590
				} else {
......
615 592
				}
616 593
			else if(result.getDataType() == Buffer.TYPE_DOUBLE)
617 594
				if(band < 0) {
618
					for (int i = 0; i < band; i++) {
595
					for (int i = 0; i < result.getBandCount(); i++) {
619 596
						result.setElem(row, col, i, nodata.getValue().doubleValue());
620 597
					}
621 598
				} else {
......
623 600
				}
624 601
			else if(result.getDataType() == Buffer.TYPE_SHORT)
625 602
				if(band < 0) {
626
					for (int i = 0; i < band; i++) {
603
					for (int i = 0; i < result.getBandCount(); i++) {
627 604
						result.setElem(row, col, i, nodata.getValue().shortValue());
628 605
					}
629 606
				} else {
......
631 608
				}
632 609
			else if(result.getDataType() == Buffer.TYPE_INT)
633 610
				if(band < 0) {
634
					for (int i = 0; i < band; i++) {
611
					for (int i = 0; i < result.getBandCount(); i++) {
635 612
						result.setElem(row, col, i, nodata.getValue().intValue());
636 613
					}
637 614
				} else {

Also available in: Unified diff