Revision 22351

View differences:

org.gvsig.raster.roimask/tags/org.gvsig.raster.roimask-2.2.245/org.gvsig.raster.roimask.algorithm/pom.xml
1
<?xml version="1.0" encoding="ISO-8859-1"?>
2
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3
	<modelVersion>4.0.0</modelVersion>
4
	<artifactId>org.gvsig.raster.roimask.algorithm</artifactId>
5
	<packaging>jar</packaging>
6
	<name>org.gvsig.raster.roimask.algorithm</name>
7
	<parent>
8
		<groupId>org.gvsig</groupId>
9
		<artifactId>org.gvsig.raster.roimask</artifactId>
10
		<version>2.2.245</version>
11
	</parent>
12
    <dependencies>
13
		<dependency>
14
			<groupId>org.gvsig</groupId>
15
			<artifactId>org.gvsig.raster.algorithm</artifactId>
16
            <scope>compile</scope>
17
		</dependency>
18
		<dependency>
19
            <groupId>org.gvsig</groupId>
20
            <artifactId>org.gvsig.i18n</artifactId>
21
            <scope>compile</scope>
22
        </dependency>
23
        <dependency>
24
            <groupId>org.gvsig</groupId>
25
            <artifactId>org.gvsig.tools.lib</artifactId>
26
            <scope>compile</scope>
27
        </dependency>
28
        <dependency>
29
            <groupId>org.gvsig</groupId>
30
            <artifactId>org.gvsig.ui</artifactId>
31
            <scope>compile</scope>
32
        </dependency>
33
        		<dependency>
34
			<groupId>org.gvsig</groupId>
35
			<artifactId>org.gvsig.raster.lib.api</artifactId>
36
            <scope>compile</scope>
37
		</dependency>
38
        <dependency>
39
            <groupId>org.gvsig</groupId>
40
            <artifactId>org.gvsig.raster.lib.impl</artifactId>
41
            <scope>compile</scope>
42
        </dependency>
43
        <dependency>
44
            <groupId>org.gvsig</groupId>
45
            <artifactId>org.gvsig.metadata.lib.basic.api</artifactId>
46
            <scope>compile</scope>
47
        </dependency>
48
        
49
        <dependency>
50
            <groupId>org.gvsig</groupId>
51
            <artifactId>org.gvsig.fmap.geometry.api</artifactId>
52
            <scope>compile</scope>
53
        </dependency>
54
    <!--
55
        <dependency>
56
            <groupId>org.gvsig</groupId>
57
            <artifactId>org.gvsig.fmap.geometry.generalpath</artifactId>
58
            <scope>runtime</scope>
59
        </dependency>
60
    -->
61
        <dependency>
62
            <groupId>org.gvsig</groupId>
63
            <artifactId>org.gvsig.compat.api</artifactId>
64
            <scope>compile</scope>
65
        </dependency>
66
        <dependency>
67
            <groupId>org.gvsig</groupId>
68
            <artifactId>org.gvsig.compat.se</artifactId>
69
            <scope>compile</scope>
70
        </dependency>
71
        <dependency>
72
            <groupId>org.gvsig</groupId>
73
            <artifactId>org.gvsig.projection.api</artifactId>
74
            <scope>compile</scope>
75
        </dependency>
76
        <dependency>
77
            <groupId>org.gvsig</groupId>
78
            <artifactId>org.gvsig.projection.cresques.impl</artifactId>
79
            <scope>runtime</scope>
80
        </dependency>
81
        <dependency>
82
            <groupId>org.gvsig</groupId>
83
            <artifactId>org.gvsig.fmap.dal.api</artifactId>
84
            <scope>compile</scope>
85
        </dependency>
86
        <dependency>
87
            <groupId>org.gvsig</groupId>
88
            <artifactId>org.gvsig.fmap.dal.impl</artifactId>
89
            <scope>compile</scope>
90
        </dependency>
91
        <dependency>
92
            <groupId>org.gvsig</groupId>
93
            <artifactId>org.gvsig.fmap.dal.file.lib</artifactId>
94
            <scope>compile</scope>
95
        </dependency>
96
        <dependency>
97
            <groupId>org.gvsig</groupId>
98
            <artifactId>org.gvsig.fmap.dal.spi</artifactId>
99
            <scope>compile</scope>
100
        </dependency>
101
	</dependencies>
102
</project>
0 103

  
org.gvsig.raster.roimask/tags/org.gvsig.raster.roimask-2.2.245/org.gvsig.raster.roimask.algorithm/src/main/resources/META-INF/services/org.gvsig.tools.library.Library
1
org.gvsig.raster.roimask.algorithm.ROIMaskAlgorithmLibrary
0 2

  
org.gvsig.raster.roimask/tags/org.gvsig.raster.roimask-2.2.245/org.gvsig.raster.roimask.algorithm/src/main/java/org/gvsig/raster/roimask/algorithm/ROIMaskAlgorithmLibrary.java
1
package org.gvsig.raster.roimask.algorithm;
2

  
3
import org.gvsig.i18n.Messages;
4
import org.gvsig.raster.algorithm.RasterBaseAlgorithmLibrary;
5
import org.gvsig.tools.library.AbstractLibrary;
6
import org.gvsig.tools.library.LibraryException;
7

  
8
/**
9
 * Initialization of ROIMaskAlgorithmLibrary library.
10
 */
11
public class ROIMaskAlgorithmLibrary extends AbstractLibrary {
12
	public static final String         PROCESS_LABEL   = "ROIMaskProcess";
13
	
14
    @Override
15
    protected void doInitialize() throws LibraryException {
16
        // Nothing to do
17
    }
18

  
19
    @Override
20
    protected void doPostInitialize() throws LibraryException {
21
    	//Registers the process and its parameters
22
    	RasterBaseAlgorithmLibrary.register(PROCESS_LABEL, ROIMaskProcess.class);
23
    	ROIMaskProcess.registerParameters();
24
    	
25
        Messages.addResourceFamily(
26
            "org.gvsig.raster.roimask.algorithm", 
27
            ROIMaskAlgorithmLibrary.class.getClassLoader(), 
28
            ROIMaskAlgorithmLibrary.class.getClass().getName());
29
        //registerGeoProcess(new RasterReprojectAlgorithmLibrary());
30
        
31
        Messages.addResourceFamily("org.gvsig.raster.roimask.algorithm.i18n.text",
32
        		ROIMaskAlgorithmLibrary.class.getClassLoader(),
33
        		ROIMaskAlgorithmLibrary.class.getClass().getName()); 
34
    }
35
}
0 36

  
org.gvsig.raster.roimask/tags/org.gvsig.raster.roimask-2.2.245/org.gvsig.raster.roimask.algorithm/src/main/java/org/gvsig/raster/roimask/algorithm/ROIMaskException.java
1
package org.gvsig.raster.roimask.algorithm;
2

  
3
import org.gvsig.raster.algorithm.process.ProcessException;
4

  
5

  
6
/**
7
 * This exception is thrown if happen problems processing data with this algorithm.
8
 */
9
public class ROIMaskException extends ProcessException {
10
	private static final long serialVersionUID = -3022090543908771484L;
11
	
12
	public ROIMaskException(String msg){
13
		super(msg);
14
	}
15
	
16
	public ROIMaskException(String msg, Throwable e){
17
		super(msg, e);
18
	}
19
}
0 20

  
org.gvsig.raster.roimask/tags/org.gvsig.raster.roimask-2.2.245/org.gvsig.raster.roimask.algorithm/src/main/java/org/gvsig/raster/roimask/algorithm/ROIMaskProcess.java
1
package org.gvsig.raster.roimask.algorithm;
2

  
3
import java.awt.geom.Rectangle2D;
4

  
5
import org.gvsig.fmap.dal.coverage.RasterLibrary;
6
import org.gvsig.fmap.dal.coverage.RasterLocator;
7
import org.gvsig.fmap.dal.coverage.dataset.Buffer;
8
import org.gvsig.fmap.dal.coverage.dataset.BufferParam;
9
import org.gvsig.fmap.dal.coverage.datastruct.Extent;
10
import org.gvsig.fmap.dal.coverage.datastruct.NoData;
11
import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException;
12
import org.gvsig.fmap.dal.coverage.exception.QueryException;
13
import org.gvsig.fmap.dal.coverage.store.RasterDataStore;
14
import org.gvsig.fmap.dal.coverage.store.RasterQuery;
15
import org.gvsig.fmap.dal.coverage.store.props.ColorInterpretation;
16
import org.gvsig.i18n.Messages;
17
import org.gvsig.raster.algorithm.process.DataProcess;
18
import org.gvsig.raster.algorithm.process.ProcessException;
19
import org.gvsig.raster.roi.ROI;
20

  
21
/**
22
 * Process 
23
 */
24
public class ROIMaskProcess extends DataProcess {
25
	public static String      RASTER_STORE1     = "RasterStore1";
26
	public static String      BUFFER            = "Buffer";
27
	public static String      PATH              = "Path";
28
	public static String      FILENAME          = "FileName";
29
	public static String      ROIS              = "Rois";
30
	public static String      INVERSE           = "Inverse";
31
	public static String      ALPHA             = "Alpha";
32
	public static String      ALPHA_BAND        = "AlphaBand";
33
	public static String      NODATA            = "NoData";
34
	
35
	private RasterDataStore   store             = null;
36
	private String            filename          = null;
37
	
38
	private ROI[]             rois              = null;  
39
	private boolean           inverse           = false;
40
	private int               alpha             = 0;
41
	private NoData            nodata            = null;
42
	
43
	private double            noDataResult      = 0;
44
	
45
	/**
46
	 * This buffer is just to test
47
	 */
48
	private Buffer            bufferForTest     = null;
49
	
50
	public static void registerParameters() {
51
		registerInputParameter(RASTER_STORE1, RasterDataStore.class, ROIMaskAlgorithmLibrary.PROCESS_LABEL);
52
		registerInputParameter(PATH, String.class, ROIMaskAlgorithmLibrary.PROCESS_LABEL);
53
		registerInputParameter(ROIS, ROI[].class, ROIMaskAlgorithmLibrary.PROCESS_LABEL);
54
		registerInputParameter(INVERSE, Boolean.class, ROIMaskAlgorithmLibrary.PROCESS_LABEL);
55
		registerInputParameter(ALPHA, Integer.class, ROIMaskAlgorithmLibrary.PROCESS_LABEL);
56
		registerInputParameter(NODATA, NoData.class, ROIMaskAlgorithmLibrary.PROCESS_LABEL);
57
		
58
		registerOutputParameter(FILENAME, String.class, ROIMaskAlgorithmLibrary.PROCESS_LABEL);
59
		registerOutputParameter(ALPHA_BAND, Buffer.class, ROIMaskAlgorithmLibrary.PROCESS_LABEL);
60
		registerOutputParameter(BUFFER, Buffer.class, ROIMaskAlgorithmLibrary.PROCESS_LABEL);
61
	}
62
	
63
	public void init() {
64
		store = getParam(RASTER_STORE1) != null ? (RasterDataStore)getParam(RASTER_STORE1) : null;
65
		filename = getStringParam(PATH);
66
		inverse = getBooleanParam(INVERSE);
67
		alpha = getIntParam(ALPHA);
68
		
69
		nodata = getParam(NODATA) != null ? (NoData)getParam(NODATA) : null;
70
		rois = getParam(ROIS) != null ? (ROI[])getParam(ROIS) : null;
71
		
72
		switch (store.getDataType()[0]) {
73
		case Buffer.TYPE_BYTE:
74
			if(nodata.getValue() != null)
75
				noDataResult = nodata.getValue().byteValue();
76
			else
77
				noDataResult = RasterLibrary.defaultByteNoDataValue;
78
			break;
79
		case Buffer.TYPE_SHORT:
80
			if(nodata.getValue() != null)
81
				noDataResult = nodata.getValue().shortValue();
82
			else
83
				noDataResult = RasterLibrary.defaultShortNoDataValue;			
84
			break;
85
		case Buffer.TYPE_INT:
86
			if(nodata.getValue() != null)
87
				noDataResult = nodata.getValue().intValue();
88
			else
89
				noDataResult = RasterLibrary.defaultIntegerNoDataValue;			
90
			break;
91
		case Buffer.TYPE_FLOAT:
92
			if(nodata.getValue() != null)
93
				noDataResult = nodata.getValue().floatValue();
94
			else
95
				noDataResult = RasterLibrary.defaultFloatNoDataValue;			
96
			break;
97
		case Buffer.TYPE_DOUBLE:
98
			if(nodata.getValue() != null)
99
				noDataResult = nodata.getValue().doubleValue();
100
			else
101
				noDataResult = RasterLibrary.defaultDoubleNoDataValue;			
102
			break;
103
		}
104
	}
105

  
106
	public void process() throws ProcessInterruptedException, ProcessException {
107
		insertLineLog(Messages.getText("applying_mask"));
108
		try {
109
			if (store == null)
110
				throw new ROIMaskException("Store not found");
111
			
112
			RasterQuery query = RasterLocator.getManager().createQuery();
113
			
114
			Extent windowExtent = getExtentResult(getOutputWindow(), null, store);
115
			Rectangle2D sourcePxBBox = null;
116
			if(isOutputRescaled()) {
117
				sourcePxBBox = new Rectangle2D.Double(0, 0, getOutputWidth(), getOutputHeight());
118
				query.setSupersamplingOption(true);
119
			} else {
120
				sourcePxBBox = getSourcePxBox(windowExtent, store);
121
				query.setSupersamplingOption(false);
122
			}
123
			double cellSize = windowExtent.width() / sourcePxBBox.getWidth();
124
			
125
			if(isForPreviews()) {
126
				query.setReadOnly(false);
127
				query.forceRGBRequest();
128
				query.setDrawableBands(store.getRender().getRenderColorInterpretation().buildRenderBands());
129
			} else {
130
				query.setAllDrawableBands();
131
				query.setReadOnly(true);
132
			}
133
			
134
			int w = (int)sourcePxBBox.getWidth();
135
			int h = (int)sourcePxBBox.getHeight();
136
			
137
			query.setAreaOfInterest(windowExtent, w, h);
138
			Buffer sourceBuffer = null;
139
			try {
140
				sourceBuffer = store.query(query);
141
				sourceBuffer.setDataExtent(windowExtent.toRectangle2D());
142
			} catch (QueryException e) {
143
				throw new ROIMaskException("Error reading data", e);
144
			} 
145

  
146
			bufferForTest = sourceBuffer;
147
			ColorInterpretation ci = store.getColorInterpretation();
148
			int ouputAlphaBandNumber = -1;
149
			
150
			if(rois != null && rois.length > 0) {
151
				nodata.setNoDataTransparent(false);
152
				if(ci.isRGBA()) {
153
					bufferForTest = processRGB(windowExtent, sourceBuffer, w, h, cellSize, ci.getAlphaBand());
154
					ouputAlphaBandNumber = bufferForTest.getBandCount() - 1;
155
					ci = RasterLocator.getManager().getDataStructFactory().createColorInterpretation(ColorInterpretation.ARGB);
156
				} else if(ci.isRGB()) {
157
					bufferForTest = processRGB(windowExtent, sourceBuffer, w, h, cellSize, -1);
158
					ouputAlphaBandNumber = bufferForTest.getBandCount() - 1;
159
					ci = RasterLocator.getManager().getDataStructFactory().createColorInterpretation(ColorInterpretation.ARGB);
160
				} else {
161
					bufferForTest = processMDT(windowExtent, sourceBuffer, cellSize);
162
					ci = RasterLocator.getManager().getDataStructFactory().createColorInterpretation(ColorInterpretation.GRAYSCALE);
163
				}
164
			} 
165
			
166
			if(!isForPreviews()) {
167
				super.exportRaster(filename, 
168
						bufferForTest, 
169
						ci,
170
						windowExtent,
171
						nodata,
172
						store.getProjection());
173
				addOutputValue(FILENAME, filename);
174
			} else {
175
				addOutputValue(BUFFER, bufferForTest);
176
			}
177
				
178
			addOutputValue(ALPHA_BAND, ouputAlphaBandNumber);
179
		} catch (ROIMaskException e) {
180
			if (incrementableTask != null)
181
				incrementableTask.processFinalize();
182
			messageBoxError("Error in mask process", this, e);
183
		}
184
	}
185
	
186
	private Buffer processRGB(Extent extent, Buffer sourceBuffer, int w, int h, double cellsize, int inputAlphaBandNumber) throws ProcessInterruptedException, ProcessException {
187
		BufferParam params = RasterLocator.getManager().getBufferFactory().createBufferParams(
188
				w, h, 4, store.getDataType()[0], true);
189
		Buffer outputBuffer = null;
190
		try {
191
			outputBuffer = RasterLocator.getManager().getBufferFactory().createBuffer(params);
192
		} catch (Exception e) {
193
			throw new ProcessException("Error creating buffer", e);
194
		} 
195

  
196
		for (int row = 0; row < h; row++) {
197
			for (int col = 0; col < w; col++) {
198
				double wcX = extent.minX() + ((((double) col) * extent.width()) / ((double) w));
199
				double wcY = extent.minY() + ((((double) (h - (row))) * extent.height()) / ((double) h));
200
				
201
				for (int iBand = 0; iBand < 3; iBand++) {
202
					outputBuffer.setElem(row, col, iBand, sourceBuffer.getElemByte(row, col, iBand));
203
				}
204
				
205
				boolean insideRoi = false;
206
				for (int i = 0; i < rois.length; i++) {
207
					if (((ROI) rois[i]).isInside(wcX, wcY, cellsize, cellsize)) {
208
						if (inverse) {
209
							if(inputAlphaBandNumber != -1)
210
								outputBuffer.setElem(row, col, 3, sourceBuffer.getElemByte(row, col, inputAlphaBandNumber));
211
							else
212
								outputBuffer.setElem(row, col, 3, (byte) 255);
213
						} else {
214
							outputBuffer.setElem(row, col, 3, (byte) (255 - alpha));
215
						}
216
						insideRoi = true;
217
					}
218
				}
219

  
220
				if(!insideRoi) {
221
					if (inverse) {
222
						outputBuffer.setElem(row, col, 3, (byte) (255 - alpha));
223
					} else {
224
						if(inputAlphaBandNumber != -1)
225
							outputBuffer.setElem(row, col, 3, sourceBuffer.getElemByte(row, col, inputAlphaBandNumber));
226
						else
227
							outputBuffer.setElem(row, col, 3, (byte) 255);
228
					}
229
				}
230
			}
231
			updatePercent(row, h);
232
		}
233
		return outputBuffer;
234
	}
235
	
236
	private Buffer processMDT(Extent extent, Buffer inputBuffer, double cellsize) throws ProcessInterruptedException, ProcessException {
237
		int w = inputBuffer.getWidth();
238
		int h = inputBuffer.getHeight();
239
		Buffer outputBuffer = null;
240
		
241
		BufferParam params = RasterLocator.getManager().getBufferFactory().createBufferParams(
242
				w, h, inputBuffer.getBandCount(), inputBuffer.getDataType(), true);
243
		try {
244
			outputBuffer = RasterLocator.getManager().getBufferFactory().createBuffer(params);
245
		} catch (Exception e) {
246
			throw new ProcessException("Error creating buffer", e);
247
		} 
248
		
249
		for (int row = 0; row < h; row++) {
250
			for (int col = 0; col < w; col++) {
251
				for (int nband = 0; nband < inputBuffer.getBandCount(); nband++) {
252
					double wcX = extent.minX() + ((((double) col) * extent.width()) / ((double) w));
253
					double wcY = extent.minY() + ((((double) (h - (row))) * extent.height()) / ((double) h));
254
					boolean insideRoi = false;
255
					for (int i = 0; i < rois.length; i++) {
256
						if (((ROI) rois[i]).isInside(wcX, wcY, cellsize, cellsize)) {
257
							if (inverse)
258
								 writePixel(inputBuffer, outputBuffer, col, row, nband);
259
							else
260
								writeNoData(outputBuffer, col, row, nband);
261
							insideRoi = true;
262
						}
263
					}
264

  
265
					if(!insideRoi) {
266
						if (inverse)
267
							writeNoData(outputBuffer, col, row, nband);
268
						else {
269
							writePixel(inputBuffer, outputBuffer, col, row, nband);
270
						}
271
					}
272
				}
273
			}
274
			updatePercent(row, h);
275
		}
276
		return outputBuffer;
277
	}
278
	
279
	private void writePixel(Buffer inputBuffer, Buffer outputBuffer, int col, int row, int nband) {
280
		switch (inputBuffer.getDataType()) {
281
		case Buffer.TYPE_BYTE:
282
			outputBuffer.setElem(row, col, nband, inputBuffer.getElemByte(row, col, nband));
283
			break;
284
		case Buffer.TYPE_SHORT:
285
			outputBuffer.setElem(row, col, nband, inputBuffer.getElemShort(row, col, nband));
286
			break;
287
		case Buffer.TYPE_INT:
288
			outputBuffer.setElem(row, col, nband, inputBuffer.getElemInt(row, col, nband));
289
			break;
290
		case Buffer.TYPE_FLOAT:
291
			outputBuffer.setElem(row, col, nband, inputBuffer.getElemFloat(row, col, nband));
292
			break;
293
		case Buffer.TYPE_DOUBLE:
294
			outputBuffer.setElem(row, col, nband, inputBuffer.getElemDouble(row, col, nband));
295
			break;
296
		}
297
	}
298
	
299
	private void writeNoData(Buffer outputBuffer, int col, int row, int nband) {
300
		switch (outputBuffer.getDataType()) {
301
		case Buffer.TYPE_BYTE:
302
			outputBuffer.setElem(row, col, nband, (byte)noDataResult);
303
			break;
304
		case Buffer.TYPE_SHORT:
305
			outputBuffer.setElem(row, col, nband, (short)noDataResult);
306
			break;
307
		case Buffer.TYPE_INT:
308
			outputBuffer.setElem(row, col, nband, (int)noDataResult);
309
			break;
310
		case Buffer.TYPE_FLOAT:
311
			outputBuffer.setElem(row, col, nband, (float)noDataResult);
312
			break;
313
		case Buffer.TYPE_DOUBLE:
314
			outputBuffer.setElem(row, col, nband, (double)noDataResult);
315
			break;
316
		}
317
	}
318
	
319
	public String getTitle() {
320
		return Messages.getText("mask_process");
321
	}
322
}
0 323

  
org.gvsig.raster.roimask/tags/org.gvsig.raster.roimask-2.2.245/org.gvsig.raster.roimask.lib/pom.xml
1
<?xml version="1.0" encoding="ISO-8859-1"?>
2
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3

  
4
	<modelVersion>4.0.0</modelVersion>
5
	<artifactId>org.gvsig.raster.roimask.lib</artifactId>
6
	<packaging>pom</packaging>
7
	<name>org.gvsig.raster.roimask.lib</name>
8
	<parent>
9
		<groupId>org.gvsig</groupId>
10
		<artifactId>org.gvsig.raster.roimask</artifactId>
11
		<version>2.2.245</version>
12
	</parent>
13
	<dependencies>
14
		<dependency>
15
            <groupId>org.gvsig</groupId>
16
            <artifactId>org.gvsig.tools.lib</artifactId>
17
            <scope>compile</scope>
18
        </dependency>
19
	</dependencies>
20
	<modules>
21
		<module>org.gvsig.raster.roimask.lib.api</module>
22
		<module>org.gvsig.raster.roimask.lib.impl</module>
23
	</modules>
24
</project>
0 25

  
org.gvsig.raster.roimask/tags/org.gvsig.raster.roimask-2.2.245/org.gvsig.raster.roimask.lib/org.gvsig.raster.roimask.lib.impl/src/main/resources/META-INF/services/org.gvsig.tools.library.Library
1
org.gvsig.raster.roimask.lib.impl.ROIMaskDefaultImplLibrary
0 2

  
org.gvsig.raster.roimask/tags/org.gvsig.raster.roimask-2.2.245/org.gvsig.raster.roimask.lib/org.gvsig.raster.roimask.lib.impl/src/main/java/org/gvsig/raster/roimask/lib/impl/ROIMaskDefaultImplLibrary.java
1
package org.gvsig.raster.roimask.lib.impl;
2

  
3
import org.gvsig.raster.roimask.lib.ROIMaskLibrary;
4
import org.gvsig.raster.roimask.lib.ROIMaskLocator;
5
import org.gvsig.raster.roimask.lib.impl.regionalpha.RegionAlphaListManager;
6
import org.gvsig.tools.library.AbstractLibrary;
7
import org.gvsig.tools.library.LibraryException;
8

  
9
/**
10
 * Library for default implementation initialization and configuration.
11
 * 
12
 * @author gvSIG team
13
 * @version $Id$
14
 */
15
public class ROIMaskDefaultImplLibrary extends AbstractLibrary {
16
	
17
	public ROIMaskDefaultImplLibrary() {
18
		registerAsImplementationOf(ROIMaskLibrary.class);
19
	}
20

  
21
	protected void doInitialize() throws LibraryException {
22
        //Register the default GeometryManager
23
		ROIMaskLocator.registerManager(DefaultROIMaskManager.class);
24
	}
25

  
26
    @Override
27
    protected void doPostInitialize() throws LibraryException {
28
    	RegionAlphaListManager.register();
29
    }
30

  
31
}
0 32

  
org.gvsig.raster.roimask/tags/org.gvsig.raster.roimask-2.2.245/org.gvsig.raster.roimask.lib/org.gvsig.raster.roimask.lib.impl/src/main/java/org/gvsig/raster/roimask/lib/impl/DefaultROIMaskManager.java
1
package org.gvsig.raster.roimask.lib.impl;
2

  
3
import org.gvsig.raster.roimask.lib.ROIMaskManager;
4
import org.slf4j.Logger;
5
import org.slf4j.LoggerFactory;
6

  
7
/**
8
 * Default {@link ROIMaskManager} implementation.
9
 * 
10
 * @author gvSIG Team
11
 * @version $Id$
12
 */
13
public class DefaultROIMaskManager implements ROIMaskManager {
14
	private static DefaultROIMaskManager   internalInstance  = new DefaultROIMaskManager();
15
	private static final Logger                       logger            = LoggerFactory.getLogger(DefaultROIMaskManager.class);
16
	
17
	/**
18
	 * Gets an instance of this object for internal use.
19
	 * @return DefaultRasterManager
20
	 */
21
	public static DefaultROIMaskManager getInstance() {
22
		return internalInstance;
23
	}
24
}
0 25

  
org.gvsig.raster.roimask/tags/org.gvsig.raster.roimask-2.2.245/org.gvsig.raster.roimask.lib/org.gvsig.raster.roimask.lib.impl/src/main/java/org/gvsig/raster/roimask/lib/impl/regionalpha/RegionAlphaFilter.java
1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22
package org.gvsig.raster.roimask.lib.impl.regionalpha;
23

  
24
import java.util.List;
25

  
26
import org.gvsig.fmap.dal.coverage.RasterLocator;
27
import org.gvsig.fmap.dal.coverage.dataset.Buffer;
28
import org.gvsig.fmap.dal.coverage.datastruct.Extent;
29
import org.gvsig.fmap.dal.coverage.datastruct.Params;
30
import org.gvsig.fmap.dal.coverage.exception.FilterAddException;
31
import org.gvsig.fmap.dal.coverage.grid.filter.BaseRasterFilter;
32
import org.gvsig.fmap.dal.coverage.store.props.ColorInterpretation;
33
import org.gvsig.raster.roi.ROI;
34

  
35
/**
36
 * 
37
 * @author BorSanZa - Borja S?nchez Zamorano 
38
 */
39
public class RegionAlphaFilter extends BaseRasterFilter {
40
	public static String[]     names           = new String[] { "regionalpha" };
41
	private RegionAlphaUI      regionAlphaUI   = null;
42
	private List<ROI>          roiList         = null;
43
	protected boolean[]        selectedROI     = null;
44
	protected int              alpha           = 255;
45
	protected boolean          inverse         = false;
46

  
47
	/* Variables que hacen falta en el process */
48
	protected double           cellsize        = 0D;
49
	protected Extent           bufferExtent     = null;
50
	
51
	/**
52
	 * Constructor
53
	 */
54
	public RegionAlphaFilter() {
55
		super();
56
		setName(names[0]);
57
	}
58

  
59
	public String getGroup() {
60
		return "mascaras";
61
	}
62

  
63
	public String[] getNames() {
64
		return names;
65
	}
66

  
67
	public Params getUIParams(String nameFilter) {
68
		Params params = RasterLocator.getManager().createParams(
69
				"Panel", getRegionAlphaUI(), -1, null);
70
		params.setParam("FilterName", nameFilter, -1, null);
71
		params.setParam("Alpha",
72
				new Integer(alpha),
73
				Params.SLIDER,
74
				new String[]{ "0", "255", "0", "1", "25" }); //min, max, valor defecto, intervalo peque?o, intervalo grande;
75

  
76
		return params;
77
	}
78
	
79
	private RegionAlphaUI getRegionAlphaUI() {
80
		if (regionAlphaUI == null) {
81
			regionAlphaUI = new RegionAlphaUI();
82
			regionAlphaUI.setROIList(getROIList());
83
		}
84
		return regionAlphaUI;
85
	}
86
	
87
	@SuppressWarnings("unchecked")
88
	protected List<ROI> getROIList() {
89
		if(roiList == null)
90
			roiList = (List<ROI>)getEnv().get("SrcROI");
91
		return roiList;
92
	}
93

  
94
	public void pre() throws FilterAddException {
95
		super.pre();
96
		
97
		Boolean inverseBoolean = (Boolean) params.get("inverse");
98
		if (inverseBoolean != null)
99
			inverse = inverseBoolean.booleanValue();
100
		
101
		alpha = ((Integer) params.get("alpha")).intValue();
102
		selectedROI = (boolean[]) params.get("selected_roi");
103
		
104
		if(raster.getDataExtent() == null)
105
			throw new FilterAddException("Buffer extension cannot be null");
106
		
107
		bufferExtent = RasterLocator.getManager().getDataStructFactory().createExtent(raster.getDataExtent());
108
		cellsize = raster.getDataExtent().getWidth() / raster.getWidth();
109
		
110
		if(hasInputTransparency())
111
			createBufferResult(raster.getDataType(), raster.getBandCount());
112
		else
113
			createBufferResult(raster.getDataType(), raster.getBandCount() + 1);
114
	}
115
	
116
	/**
117
	 * Gets the result of this filter
118
	 */
119
	public Object getResult(String name) {
120
		if (name.equals(RESULT_TRANSPARENCY)) {
121
			String[] values = new String[rasterResult.getBandCount()];
122
			for (int i = 0; i < values.length; i++) {
123
				values[i] = ColorInterpretation.UNDEF_BAND;
124
			}
125
			ColorInterpretation ci = RasterLocator.getManager().getDataStructFactory().createColorInterpretation(values);
126
			ci.setColorInterpValue(rasterResult.getBandCount() - 1, ColorInterpretation.ALPHA_BAND);
127
			transparency.setColorInterpretation(ci);
128
			transparency.activeTransparency();
129
			return transparency;
130
		}
131

  
132
		return super.getResult(name);
133
	}
134

  
135
	public void post() {
136
	}
137

  
138
	public void process(int x, int y) {
139
	}
140
	
141
	public int getInRasterDataType() {
142
		return Buffer.TYPE_BYTE;
143
	}
144
	
145
	public int getOutRasterDataType() {
146
		return Buffer.TYPE_BYTE;
147
	}
148
}
0 149

  
org.gvsig.raster.roimask/tags/org.gvsig.raster.roimask-2.2.245/org.gvsig.raster.roimask.lib/org.gvsig.raster.roimask.lib.impl/src/main/java/org/gvsig/raster/roimask/lib/impl/regionalpha/RegionAlphaListManager.java
1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22
package org.gvsig.raster.roimask.lib.impl.regionalpha;
23

  
24
import java.util.ArrayList;
25
import java.util.List;
26

  
27
import org.gvsig.fmap.dal.coverage.datastruct.Params;
28
import org.gvsig.fmap.dal.coverage.exception.FilterTypeException;
29
import org.gvsig.fmap.dal.coverage.grid.AbstractRasterFilterManager;
30
import org.gvsig.fmap.dal.coverage.grid.RasterFilter;
31
import org.gvsig.fmap.dal.coverage.grid.RasterFilterList;
32
import org.gvsig.fmap.dal.coverage.grid.RegistrableFilterListener;
33

  
34
public class RegionAlphaListManager extends AbstractRasterFilterManager {
35
	public static String   ID = "RegionAlpha";
36
	
37
	/**
38
	 * Default constructor. Sets the filter list.
39
	 * @param filterList
40
	 */
41
	public RegionAlphaListManager(RasterFilterList filterList) {
42
		super(filterList);
43
	}
44

  
45
	public static void register() {
46
		AbstractRasterFilterManager.register(ID, RegionAlphaListManager.class, RegionAlphaUI.class);
47
	}
48
	
49
	public String getManagerID() {
50
		return ID;
51
	}
52
	
53
	public boolean isDataTypeSupported(int dataType) {
54
		return true;
55
	}
56
	
57
	public Class<?> getFilterClassByID(String id) {
58
		if( id.compareTo("regionalpha") == 0)
59
			return RegionAlphaFilter.class;
60
		return null;
61
	}
62

  
63
	public void addRegionAlphaFilter(boolean[] selectedROI, int alpha, Boolean inverse) throws FilterTypeException {
64
		RasterFilter filter = new RegionAlphaByteFilter();
65

  
66
		//Cuando el filtro esta creado, tomamos los valores y lo a?adimos a la pila
67
		if (filter != null) {
68
			filter.addParam("inverse", inverse);
69
			filter.addParam("alpha", Integer.valueOf(alpha));
70
			filter.addParam("selected_roi", selectedROI);
71
			getFilterList().add(filter);
72
		}
73
	}
74

  
75
	public List<Class<?>> getRasterFilterList() {
76
		List<Class<?>> filters = new ArrayList<Class<?>>();
77
		filters.add(RegionAlphaFilter.class);
78
		return filters;
79
	}
80
	
81
	public RasterFilter createFilter(Params params) {
82
		boolean[] selectedROI = ((boolean[]) params.getParamById("selected_roi").getDefaultValue());
83
		Boolean inverse = ((Boolean) params.getParamById("inverse").getDefaultValue());
84
		Integer alpha = ((Integer) params.getParamById("Alpha").getDefaultValue());
85
		
86
		RasterFilter filter = new RegionAlphaByteFilter();
87
		filter.addParam("selected_roi", selectedROI);
88
		filter.addParam("inverse", inverse);
89
		filter.addParam("alpha", alpha);
90
		return filter;
91
	}
92
	
93
	public void addFilter(Params params) throws FilterTypeException {
94
		addFilter(RegionAlphaFilter.class, params);
95
	}
96

  
97
	public void addFilter(Class<?> classFilter, Params params) throws FilterTypeException {
98
		if (classFilter.equals(RegionAlphaFilter.class)) {
99
			boolean[] selectedROI = null;
100
			Boolean inverse = new Boolean(false);
101
			int alpha = 255;
102

  
103
			Params paramsUI = null;
104
			for (int i = 0; i < params.getNumParams(); i++) {
105
				if (params.getParam(i).getId().equals("Panel") &&
106
					params.getParam(i).getDefaultValue() instanceof RegistrableFilterListener) {
107
					paramsUI = ((RegistrableFilterListener) params.getParam(i).getDefaultValue()).getParams();
108
				}
109
				if (params.getParam(i).getId().equals("Alpha") && params.getParam(i).getDefaultValue() instanceof Integer) {
110
					alpha = ((Integer) params.getParam(i).getDefaultValue()).intValue();
111
				}
112
			}
113

  
114
			if (paramsUI != null) {
115
				for (int i = 0; i < paramsUI.getNumParams(); i++) {
116
					if (paramsUI.getParam(i).getId().equals("selected_roi"))
117
						selectedROI = (boolean[]) paramsUI.getParam(i).getDefaultValue();
118
					if (paramsUI.getParam(i).getId().equals("inverse"))
119
						inverse = (Boolean) paramsUI.getParam(i).getDefaultValue();
120
				}
121
			}
122
			addRegionAlphaFilter(selectedROI, alpha, inverse);
123
		}
124
	}
125
}
0 126

  
org.gvsig.raster.roimask/tags/org.gvsig.raster.roimask-2.2.245/org.gvsig.raster.roimask.lib/org.gvsig.raster.roimask.lib.impl/src/main/java/org/gvsig/raster/roimask/lib/impl/regionalpha/RegionAlphaByteFilter.java
1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22
package org.gvsig.raster.roimask.lib.impl.regionalpha;
23

  
24
import org.gvsig.raster.roi.ROI;
25

  
26
/**
27
 * Writes the alpha band filtering with the ROI
28
 * @author Nacho Brodin (nachobrodin@gmail.com)
29
 */
30
public class RegionAlphaByteFilter extends RegionAlphaFilter {
31
	public void process(int x, int y) {
32
		if(bufferExtent == null) {
33
			for (int j = 0; j < raster.getBandCount(); j++) 
34
				rasterResult.setElem(y, x, j, raster.getElemByte(y, x, j));
35
			return;
36
		}
37
		
38
		double wcX = bufferExtent.minX() + ((((double) x) * bufferExtent.width()) / ((double) raster.getWidth()));
39
		double wcY = bufferExtent.minY() + ((((double) (raster.getHeight() - (y))) * bufferExtent.height()) / ((double) raster.getHeight()));
40
		
41
		for (int iBand = 0; iBand < numberOfBandsToProcess(); iBand++) {
42
			rasterResult.setElem(y, x, iBand, raster.getElemByte(y, x, iBand));
43
		}
44
		
45
		for (int i = 0; i < getROIList().size(); i++) {
46
			if (selectedROI[i] && ((ROI) getROIList().get(i)).isInside(wcX, wcY, cellsize, cellsize)) {
47
				if (inverse) {
48
					if(hasInputTransparency())
49
						rasterResult.setElem(y, x, rasterResult.getBandCount() - 1, raster.getElemByte(y, x, transparency.getAlphaBandNumber()));
50
					else
51
						rasterResult.setElem(y, x, rasterResult.getBandCount() - 1, (byte) 255);
52
				} else {
53
					rasterResult.setElem(y, x, rasterResult.getBandCount() - 1, (byte) (255 - alpha));
54
				}
55
				return;
56
			}
57
		}
58
		
59
		if (inverse) {
60
			rasterResult.setElem(y, x, rasterResult.getBandCount() - 1, (byte) (255 - alpha));
61
		} else {
62
			if(hasInputTransparency())
63
				rasterResult.setElem(y, x, rasterResult.getBandCount() - 1, raster.getElemByte(y, x, transparency.getAlphaBandNumber()));
64
			else
65
				rasterResult.setElem(y, x, rasterResult.getBandCount() - 1, (byte) 255);
66
		}
67
	}
68
}
0 69

  
org.gvsig.raster.roimask/tags/org.gvsig.raster.roimask-2.2.245/org.gvsig.raster.roimask.lib/org.gvsig.raster.roimask.lib.impl/src/main/java/org/gvsig/raster/roimask/lib/impl/regionalpha/RegionAlphaUI.java
1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff