gvsig-raster / org.gvsig.raster.tools / trunk / org.gvsig.raster.tools / org.gvsig.raster.tools.algorithm / org.gvsig.raster.tools.algorithm.layerdatatype / src / main / java / org / gvsig / raster / tools / algorithm / layerdatatype / LayerDatatypeProcess.java @ 1747
History | View | Annotate | Download (12.8 KB)
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.tools.algorithm.layerdatatype; |
23 |
|
24 |
import java.util.HashMap; |
25 |
|
26 |
import javax.swing.SwingUtilities; |
27 |
|
28 |
import org.gvsig.fmap.dal.coverage.RasterLocator; |
29 |
import org.gvsig.fmap.dal.coverage.dataset.Buffer; |
30 |
import org.gvsig.fmap.dal.coverage.datastruct.NoData; |
31 |
import org.gvsig.fmap.dal.coverage.exception.InvalidSetViewException; |
32 |
import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException; |
33 |
import org.gvsig.fmap.dal.coverage.exception.RasterDriverException; |
34 |
import org.gvsig.fmap.dal.coverage.store.RasterDataStore; |
35 |
import org.gvsig.fmap.dal.coverage.store.RasterQuery; |
36 |
import org.gvsig.i18n.Messages; |
37 |
import org.gvsig.raster.tools.algorithm.base.RasterBaseAlgorithmLibrary; |
38 |
import org.gvsig.raster.tools.algorithm.base.process.RasterProcess; |
39 |
|
40 |
/**
|
41 |
* Process to change a <code>RasterDataStore</code> of data type
|
42 |
*
|
43 |
* 10/12/2007
|
44 |
* @author Nacho Brodin nachobrodin@gmail.com
|
45 |
*/
|
46 |
public class LayerDatatypeProcess extends RasterProcess { |
47 |
public static String RASTER_STORE1 = "RasterStore1"; |
48 |
public static String PATH = "Path"; |
49 |
public static String FILENAME = "FileName"; |
50 |
public static String DATATYPE = "Datatype"; |
51 |
public static String ADJUST_DEC2INT = "AdjustDec2Int"; |
52 |
public static String ADJUST_BIG2SMALL = "AdjustBig2Small"; |
53 |
public static String TIME = "Time"; |
54 |
|
55 |
public static String[] DEC2INT_OPTIONS = new String[]{"Trunk", "Round", |
56 |
"Ceil", "Floor"}; |
57 |
public static String[] BIG2SMALL_OPTIONS = new String[]{"Trunk", "Maxvalue", |
58 |
"NoData"};
|
59 |
public static String RESULT_DSTORE = "RESULT_DSTORE"; |
60 |
|
61 |
private RasterDataStore store = null; |
62 |
private String filename = null; |
63 |
private int datatype = 0; |
64 |
private int dec2int = 0; |
65 |
private int big2smal = 0; |
66 |
private long milis = 0; |
67 |
private NoData byteNoData = null; |
68 |
private NoData shortNoData = null; |
69 |
private NoData intNoData = null; |
70 |
private NoData floatNoData = null; |
71 |
|
72 |
public static void registerParameters() { |
73 |
RASTER_STORE1 = RasterBaseAlgorithmLibrary.registerInputParameter(RASTER_STORE1, RasterDataStore.class); |
74 |
PATH = RasterBaseAlgorithmLibrary.registerInputParameter(PATH, String.class);
|
75 |
DATATYPE = RasterBaseAlgorithmLibrary.registerInputParameter(DATATYPE, Integer.class);
|
76 |
ADJUST_DEC2INT = RasterBaseAlgorithmLibrary.registerInputParameter(ADJUST_DEC2INT, Integer.class);
|
77 |
ADJUST_BIG2SMALL = RasterBaseAlgorithmLibrary.registerInputParameter(ADJUST_BIG2SMALL, Integer.class);
|
78 |
|
79 |
RESULT_DSTORE = RasterBaseAlgorithmLibrary.registerOutputParameter(RESULT_DSTORE, RasterDataStore.class); |
80 |
FILENAME = RasterBaseAlgorithmLibrary.registerOutputParameter(FILENAME, String.class);
|
81 |
TIME = RasterBaseAlgorithmLibrary.registerOutputParameter(TIME, Long.class);
|
82 |
} |
83 |
|
84 |
/*
|
85 |
* (non-Javadoc)
|
86 |
* @see org.gvsig.rastertools.RasterProcess#init()
|
87 |
*/
|
88 |
public void init() { |
89 |
store = getParam(RASTER_STORE1) != null ? (RasterDataStore)getParam(RASTER_STORE1) : null; |
90 |
filename = getStringParam(PATH); |
91 |
datatype = (Integer)getParam(DATATYPE);
|
92 |
dec2int = (Integer)getParam(ADJUST_DEC2INT);
|
93 |
big2smal = (Integer)getParam(ADJUST_BIG2SMALL);
|
94 |
byteNoData = RasterLocator.getManager().getDataStructFactory().createDefaultNoData( |
95 |
store.getBandCount(), Buffer.TYPE_BYTE);
|
96 |
shortNoData = RasterLocator.getManager().getDataStructFactory().createDefaultNoData( |
97 |
store.getBandCount(), Buffer.TYPE_SHORT);
|
98 |
intNoData = RasterLocator.getManager().getDataStructFactory().createDefaultNoData( |
99 |
store.getBandCount(), Buffer.TYPE_INT);
|
100 |
floatNoData = RasterLocator.getManager().getDataStructFactory().createDefaultNoData( |
101 |
store.getBandCount(), Buffer.TYPE_FLOAT);
|
102 |
} |
103 |
|
104 |
public void process() throws ProcessInterruptedException { |
105 |
long t1 = new java.util.Date().getTime(); |
106 |
insertLineLog(Messages.getText("layer_datatype"));
|
107 |
try {
|
108 |
if (store == null) |
109 |
throw new LayerDatatypeException("intput_not_valid"); |
110 |
|
111 |
int inDataType = store.getDataType()[0]; |
112 |
Buffer buf = RasterLocator.getManager().createBuffer(
|
113 |
datatype, |
114 |
(int)store.getWidth(),
|
115 |
(int)store.getHeight(),
|
116 |
store.getBandCount(), |
117 |
true);
|
118 |
|
119 |
RasterQuery query = RasterLocator.getManager().createQuery(); |
120 |
query.setAllDrawableBands(); |
121 |
query.setAreaOfInterest(); |
122 |
Buffer sourceBuffer;
|
123 |
try {
|
124 |
sourceBuffer = store.query(query); |
125 |
} catch (RasterDriverException e) {
|
126 |
throw new LayerDatatypeException(""); |
127 |
} catch (InvalidSetViewException e) {
|
128 |
throw new LayerDatatypeException(""); |
129 |
} |
130 |
double v = 0; |
131 |
|
132 |
|
133 |
switch (inDataType) {
|
134 |
case Buffer.TYPE_BYTE: |
135 |
for (int row = 0; row < buf.getHeight(); row++) { |
136 |
for (int col = 0; col < buf.getWidth(); col++) { |
137 |
for (int iBand = 0; iBand < buf.getBandCount(); iBand++) { |
138 |
writePixelFromByte(row, col, iBand, sourceBuffer.getElemByte(row, col, iBand), buf); |
139 |
} |
140 |
} |
141 |
updatePercent(row, buf.getHeight()); |
142 |
} |
143 |
break;
|
144 |
case Buffer.TYPE_SHORT: |
145 |
for (int row = 0; row < buf.getHeight(); row++) { |
146 |
for (int col = 0; col < buf.getWidth(); col++) { |
147 |
for (int iBand = 0; iBand < buf.getBandCount(); iBand++) { |
148 |
writePixelFromShort(row, col, iBand, sourceBuffer.getElemShort(row, col, iBand), buf); |
149 |
} |
150 |
} |
151 |
updatePercent(row, buf.getHeight()); |
152 |
} |
153 |
break;
|
154 |
case Buffer.TYPE_INT: |
155 |
for (int row = 0; row < buf.getHeight(); row++) { |
156 |
for (int col = 0; col < buf.getWidth(); col++) { |
157 |
for (int iBand = 0; iBand < buf.getBandCount(); iBand++) { |
158 |
writePixelFromInt(row, col, iBand, sourceBuffer.getElemInt(row, col, iBand), buf); |
159 |
} |
160 |
} |
161 |
updatePercent(row, buf.getHeight()); |
162 |
} |
163 |
break;
|
164 |
case Buffer.TYPE_FLOAT: |
165 |
for (int row = 0; row < buf.getHeight(); row++) { |
166 |
for (int col = 0; col < buf.getWidth(); col++) { |
167 |
for (int iBand = 0; iBand < buf.getBandCount(); iBand++) { |
168 |
v = getDecimalValue(sourceBuffer.getElemFloat(row, col, iBand), inDataType); |
169 |
writePixelFromDouble(row, col, iBand, v, buf); |
170 |
} |
171 |
} |
172 |
updatePercent(row, buf.getHeight()); |
173 |
} |
174 |
break;
|
175 |
case Buffer.TYPE_DOUBLE: |
176 |
for (int row = 0; row < buf.getHeight(); row++) { |
177 |
for (int col = 0; col < buf.getWidth(); col++) { |
178 |
for (int iBand = 0; iBand < buf.getBandCount(); iBand++) { |
179 |
v = getDecimalValue(sourceBuffer.getElemDouble(row, col, iBand), inDataType); |
180 |
writePixelFromDouble(row, col, iBand, v, buf); |
181 |
} |
182 |
} |
183 |
updatePercent(row, buf.getHeight()); |
184 |
} |
185 |
break;
|
186 |
} |
187 |
|
188 |
super.exportRaster(filename,
|
189 |
buf, |
190 |
sourceBuffer.getStore().getCellSize(), |
191 |
sourceBuffer.getStore().getExtent().getULX(), |
192 |
sourceBuffer.getStore().getExtent().getULY()); |
193 |
|
194 |
long t2 = new java.util.Date().getTime(); |
195 |
milis = t2 - t1; |
196 |
|
197 |
SwingUtilities.invokeLater(new Runnable() { |
198 |
public void run() { |
199 |
if (externalActions != null) { |
200 |
HashMap<String, Object> map = new HashMap<String, Object>(); |
201 |
map.put(FILENAME, filename); |
202 |
map.put(TIME, new Long(milis)); |
203 |
externalActions.end(map); |
204 |
} |
205 |
} |
206 |
}); |
207 |
} catch (LayerDatatypeException e) {
|
208 |
if (incrementableTask != null) |
209 |
incrementableTask.processFinalize(); |
210 |
messageBoxError("error_changing_the_datatype", this, e); |
211 |
} |
212 |
} |
213 |
|
214 |
public double getDecimalValue(double value, int inDataType) { |
215 |
if(inDataType == Buffer.TYPE_FLOAT || inDataType == Buffer.TYPE_DOUBLE) { |
216 |
switch (dec2int) {
|
217 |
case 0:return (double)((long)value); |
218 |
case 1:return Math.round(value); |
219 |
case 2:return Math.ceil(value); |
220 |
case 3:return Math.floor(value); |
221 |
} |
222 |
} |
223 |
return value;
|
224 |
} |
225 |
|
226 |
private void writePixelFromByte(int row, int col, int band, byte value, Buffer dst) { |
227 |
switch (dst.getDataType()) {
|
228 |
case Buffer.TYPE_BYTE: dst.setElem(row, col, band, value);break; |
229 |
case Buffer.TYPE_SHORT: dst.setElem(row, col, band, (short)value);break; |
230 |
case Buffer.TYPE_INT: dst.setElem(row, col, band, (int)value);break; |
231 |
case Buffer.TYPE_FLOAT: dst.setElem(row, col, band, (float)value);break; |
232 |
case Buffer.TYPE_DOUBLE: dst.setElem(row, col, band, (double)value);break; |
233 |
} |
234 |
} |
235 |
|
236 |
private void writePixelFromShort(int row, int col, int band, short value, Buffer dst) { |
237 |
switch (dst.getDataType()) {
|
238 |
case Buffer.TYPE_BYTE: |
239 |
dst.setElem(row, col, band, (byte)value);
|
240 |
if(value > Byte.MAX_VALUE) { |
241 |
switch (big2smal) {
|
242 |
case 1:dst.setElem(row, col, band, Byte.MAX_VALUE);break; |
243 |
case 2:dst.setElem(row, col, band, (Byte)byteNoData.getValue());break; |
244 |
} |
245 |
} |
246 |
break;
|
247 |
case Buffer.TYPE_SHORT: dst.setElem(row, col, band, value);break; |
248 |
case Buffer.TYPE_INT: dst.setElem(row, col, band, (int)value);break; |
249 |
case Buffer.TYPE_FLOAT: dst.setElem(row, col, band, (float)value);break; |
250 |
case Buffer.TYPE_DOUBLE: dst.setElem(row, col, band, (double)value);break; |
251 |
} |
252 |
} |
253 |
|
254 |
private void writePixelFromInt(int row, int col, int band, int value, Buffer dst) { |
255 |
switch (dst.getDataType()) {
|
256 |
case Buffer.TYPE_BYTE: |
257 |
dst.setElem(row, col, band, (byte)value);
|
258 |
if(value > Byte.MAX_VALUE) { |
259 |
switch (big2smal) {
|
260 |
case 1:dst.setElem(row, col, band, Byte.MAX_VALUE);break; |
261 |
case 2:dst.setElem(row, col, band, (Byte)byteNoData.getValue());break; |
262 |
} |
263 |
} |
264 |
break;
|
265 |
case Buffer.TYPE_SHORT: |
266 |
dst.setElem(row, col, band, (short)value);
|
267 |
if(value > Short.MAX_VALUE) { |
268 |
switch (big2smal) {
|
269 |
case 1:dst.setElem(row, col, band, Short.MAX_VALUE);break; |
270 |
case 2:dst.setElem(row, col, band, (Short)shortNoData.getValue());break; |
271 |
} |
272 |
} |
273 |
break;
|
274 |
case Buffer.TYPE_INT: dst.setElem(row, col, band, (int)value);break; |
275 |
case Buffer.TYPE_FLOAT: dst.setElem(row, col, band, (float)value);break; |
276 |
case Buffer.TYPE_DOUBLE: dst.setElem(row, col, band, (double)value);break; |
277 |
} |
278 |
} |
279 |
|
280 |
private void writePixelFromDouble(int row, int col, int band, double value, Buffer dst) { |
281 |
switch (dst.getDataType()) {
|
282 |
case Buffer.TYPE_BYTE: |
283 |
dst.setElem(row, col, band, (byte)value);
|
284 |
if(value > Byte.MAX_VALUE) { |
285 |
switch (big2smal) {
|
286 |
case 1:dst.setElem(row, col, band, Byte.MAX_VALUE);break; |
287 |
case 2:dst.setElem(row, col, band, (Byte)byteNoData.getValue());break; |
288 |
} |
289 |
} |
290 |
break;
|
291 |
case Buffer.TYPE_SHORT: |
292 |
dst.setElem(row, col, band, (short)value);
|
293 |
if(value > Short.MAX_VALUE) { |
294 |
switch (big2smal) {
|
295 |
case 1:dst.setElem(row, col, band, Short.MAX_VALUE);break; |
296 |
case 2:dst.setElem(row, col, band, (Short)shortNoData.getValue());break; |
297 |
} |
298 |
} |
299 |
break;
|
300 |
case Buffer.TYPE_INT: |
301 |
dst.setElem(row, col, band, (int)value);
|
302 |
if(value > Integer.MAX_VALUE) { |
303 |
switch (big2smal) {
|
304 |
case 1:dst.setElem(row, col, band, Integer.MAX_VALUE);break; |
305 |
case 2:dst.setElem(row, col, band, (Integer)intNoData.getValue());break; |
306 |
} |
307 |
} |
308 |
break;
|
309 |
case Buffer.TYPE_FLOAT: |
310 |
dst.setElem(row, col, band, (float)value);
|
311 |
if(value > Float.MAX_VALUE) { |
312 |
switch (big2smal) {
|
313 |
case 1:dst.setElem(row, col, band, Float.MAX_VALUE);break; |
314 |
case 2:dst.setElem(row, col, band, (Float)floatNoData.getValue());break; |
315 |
} |
316 |
} |
317 |
break;
|
318 |
case Buffer.TYPE_DOUBLE: dst.setElem(row, col, band, (double)value);break; |
319 |
} |
320 |
} |
321 |
|
322 |
/*
|
323 |
* (non-Javadoc)
|
324 |
* @see org.gvsig.raster.tools.app.basic.raster.process.RasterProcess#getResult()
|
325 |
*/
|
326 |
public Object getResult() { |
327 |
HashMap<String, Object> map = new HashMap<String, Object>(); |
328 |
map.put(FILENAME, filename); |
329 |
map.put(TIME, new Long(milis)); |
330 |
return map;
|
331 |
} |
332 |
|
333 |
/*
|
334 |
* (non-Javadoc)
|
335 |
* @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getPercent()
|
336 |
*/
|
337 |
public int getPercent() { |
338 |
return percent;
|
339 |
} |
340 |
|
341 |
/*
|
342 |
* (non-Javadoc)
|
343 |
* @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getTitle()
|
344 |
*/
|
345 |
public String getTitle() { |
346 |
return Messages.getText("layer_datatype"); |
347 |
} |
348 |
} |