|
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 int percent = 0;
|
|
68 |
private NoData byteNoData = null;
|
|
69 |
private NoData shortNoData = null;
|
|
70 |
private NoData intNoData = null;
|
|
71 |
private NoData floatNoData = null;
|
|
72 |
private NoData doubleNoData = null;
|
|
73 |
|
|
74 |
public static void registerParameters() {
|
|
75 |
RASTER_STORE1 = RasterBaseAlgorithmLibrary.registerInputParameter(RASTER_STORE1, RasterDataStore.class);
|
|
76 |
PATH = RasterBaseAlgorithmLibrary.registerInputParameter(PATH, String.class);
|
|
77 |
DATATYPE = RasterBaseAlgorithmLibrary.registerInputParameter(DATATYPE, Integer.class);
|
|
78 |
ADJUST_DEC2INT = RasterBaseAlgorithmLibrary.registerInputParameter(ADJUST_DEC2INT, Integer.class);
|
|
79 |
ADJUST_BIG2SMALL = RasterBaseAlgorithmLibrary.registerInputParameter(ADJUST_BIG2SMALL, Integer.class);
|
|
80 |
|
|
81 |
RESULT_DSTORE = RasterBaseAlgorithmLibrary.registerOutputParameter(RESULT_DSTORE, RasterDataStore.class);
|
|
82 |
FILENAME = RasterBaseAlgorithmLibrary.registerOutputParameter(FILENAME, String.class);
|
|
83 |
TIME = RasterBaseAlgorithmLibrary.registerOutputParameter(TIME, Long.class);
|
|
84 |
}
|
|
85 |
|
|
86 |
/*
|
|
87 |
* (non-Javadoc)
|
|
88 |
* @see org.gvsig.rastertools.RasterProcess#init()
|
|
89 |
*/
|
|
90 |
public void init() {
|
|
91 |
store = getParam(RASTER_STORE1) != null ? (RasterDataStore)getParam(RASTER_STORE1) : null;
|
|
92 |
filename = getStringParam(PATH);
|
|
93 |
datatype = (Integer)getParam(DATATYPE);
|
|
94 |
dec2int = (Integer)getParam(ADJUST_DEC2INT);
|
|
95 |
big2smal = (Integer)getParam(ADJUST_BIG2SMALL);
|
|
96 |
byteNoData = RasterLocator.getManager().getDataStructFactory().createDefaultNoData(
|
|
97 |
store.getBandCount(), Buffer.TYPE_BYTE);
|
|
98 |
shortNoData = RasterLocator.getManager().getDataStructFactory().createDefaultNoData(
|
|
99 |
store.getBandCount(), Buffer.TYPE_SHORT);
|
|
100 |
intNoData = RasterLocator.getManager().getDataStructFactory().createDefaultNoData(
|
|
101 |
store.getBandCount(), Buffer.TYPE_INT);
|
|
102 |
floatNoData = RasterLocator.getManager().getDataStructFactory().createDefaultNoData(
|
|
103 |
store.getBandCount(), Buffer.TYPE_FLOAT);
|
|
104 |
doubleNoData = RasterLocator.getManager().getDataStructFactory().createDefaultNoData(
|
|
105 |
store.getBandCount(), Buffer.TYPE_DOUBLE);
|
|
106 |
}
|
|
107 |
|
|
108 |
public void process() throws ProcessInterruptedException {
|
|
109 |
long t1 = new java.util.Date().getTime();
|
|
110 |
insertLineLog(Messages.getText("layer_datatype"));
|
|
111 |
int result = 1;
|
|
112 |
try {
|
|
113 |
if (store == null)
|
|
114 |
throw new LayerDatatypeException("intput_not_valid");
|
|
115 |
|
|
116 |
int inDataType = store.getDataType()[0];
|
|
117 |
Buffer buf = RasterLocator.getManager().createBuffer(
|
|
118 |
datatype,
|
|
119 |
(int)store.getWidth(),
|
|
120 |
(int)store.getHeight(),
|
|
121 |
store.getBandCount(),
|
|
122 |
true);
|
|
123 |
|
|
124 |
RasterQuery query = RasterLocator.getManager().createQuery();
|
|
125 |
query.setAllDrawableBands();
|
|
126 |
query.setAreaOfInterest();
|
|
127 |
Buffer sourceBuffer;
|
|
128 |
try {
|
|
129 |
sourceBuffer = store.query(query);
|
|
130 |
} catch (RasterDriverException e) {
|
|
131 |
throw new LayerDatatypeException("");
|
|
132 |
} catch (InvalidSetViewException e) {
|
|
133 |
throw new LayerDatatypeException("");
|
|
134 |
}
|
|
135 |
double v = 0;
|
|
136 |
|
|
137 |
for (int row = 0; row < buf.getHeight(); row++) {
|
|
138 |
for (int col = 0; col < buf.getWidth(); col++) {
|
|
139 |
for (int iBand = 0; iBand < buf.getBandCount(); iBand++) {
|
|
140 |
switch (inDataType) {
|
|
141 |
case Buffer.TYPE_BYTE:
|
|
142 |
|
|
143 |
break;
|
|
144 |
case Buffer.TYPE_SHORT:
|
|
145 |
|
|
146 |
break;
|
|
147 |
case Buffer.TYPE_INT:
|
|
148 |
|
|
149 |
break;
|
|
150 |
case Buffer.TYPE_FLOAT:
|
|
151 |
v = getDecimalValue(sourceBuffer.getElemFloat(row, col, iBand), inDataType);
|
|
152 |
break;
|
|
153 |
case Buffer.TYPE_DOUBLE:
|
|
154 |
v = getDecimalValue(sourceBuffer.getElemDouble(row, col, iBand), inDataType);
|
|
155 |
break;
|
|
156 |
}
|
|
157 |
|
|
158 |
}
|
|
159 |
}
|
|
160 |
}
|
|
161 |
|
|
162 |
if(result != 0) {
|
|
163 |
if (incrementableTask != null) {
|
|
164 |
incrementableTask.processFinalize();
|
|
165 |
setProgressActive(false);
|
|
166 |
}
|
|
167 |
messageBoxError("transformation_not_possible", this);
|
|
168 |
return;
|
|
169 |
}
|
|
170 |
|
|
171 |
long t2 = new java.util.Date().getTime();
|
|
172 |
milis = t2 - t1;
|
|
173 |
|
|
174 |
SwingUtilities.invokeLater(new Runnable() {
|
|
175 |
public void run() {
|
|
176 |
if (externalActions != null) {
|
|
177 |
HashMap<String, Object> map = new HashMap<String, Object>();
|
|
178 |
map.put(FILENAME, filename);
|
|
179 |
map.put(TIME, new Long(milis));
|
|
180 |
externalActions.end(map);
|
|
181 |
}
|
|
182 |
}
|
|
183 |
});
|
|
184 |
} catch (LayerDatatypeException e) {
|
|
185 |
if (incrementableTask != null)
|
|
186 |
incrementableTask.processFinalize();
|
|
187 |
messageBoxError("error_changing_the_datatype", this, e);
|
|
188 |
}
|
|
189 |
}
|
|
190 |
|
|
191 |
public double getDecimalValue(double value, int inDataType) {
|
|
192 |
if(inDataType == Buffer.TYPE_FLOAT || inDataType == Buffer.TYPE_DOUBLE) {
|
|
193 |
switch (dec2int) {
|
|
194 |
case 0:return (double)((long)value);
|
|
195 |
case 1:return Math.round(value);
|
|
196 |
case 2:return Math.ceil(value);
|
|
197 |
case 3:return Math.floor(value);
|
|
198 |
}
|
|
199 |
}
|
|
200 |
return value;
|
|
201 |
}
|
|
202 |
|
|
203 |
private void writePixelFromByte(int row, int col, int band, byte value, Buffer dst) {
|
|
204 |
switch (dst.getDataType()) {
|
|
205 |
case Buffer.TYPE_BYTE: dst.setElem(row, col, band, value);break;
|
|
206 |
case Buffer.TYPE_SHORT: dst.setElem(row, col, band, (short)value);break;
|
|
207 |
case Buffer.TYPE_INT: dst.setElem(row, col, band, (int)value);break;
|
|
208 |
case Buffer.TYPE_FLOAT: dst.setElem(row, col, band, (float)value);break;
|
|
209 |
case Buffer.TYPE_DOUBLE: dst.setElem(row, col, band, (double)value);break;
|
|
210 |
}
|
|
211 |
}
|
|
212 |
|
|
213 |
private void writePixelFromShort(int row, int col, int band, short value, Buffer dst) {
|
|
214 |
switch (dst.getDataType()) {
|
|
215 |
case Buffer.TYPE_BYTE:
|
|
216 |
if(value > Byte.MAX_VALUE) {
|
|
217 |
switch (big2smal) {
|
|
218 |
case 0:dst.setElem(row, col, band, (byte)value);break;
|
|
219 |
case 1:dst.setElem(row, col, band, Byte.MAX_VALUE);break;
|
|
220 |
case 2:dst.setElem(row, col, band, (Byte)byteNoData.getValue());break;
|
|
221 |
}
|
|
222 |
}
|
|
223 |
break;
|
|
224 |
case Buffer.TYPE_SHORT: dst.setElem(row, col, band, value);break;
|
|
225 |
case Buffer.TYPE_INT: dst.setElem(row, col, band, (int)value);break;
|
|
226 |
case Buffer.TYPE_FLOAT: dst.setElem(row, col, band, (float)value);break;
|
|
227 |
case Buffer.TYPE_DOUBLE: dst.setElem(row, col, band, (double)value);break;
|
|
228 |
}
|
|
229 |
}
|
|
230 |
|
|
231 |
private void writePixelFromInt(int row, int col, int band, int value, Buffer dst) {
|
|
232 |
switch (dst.getDataType()) {
|
|
233 |
case Buffer.TYPE_BYTE:
|
|
234 |
if(value > Byte.MAX_VALUE) {
|
|
235 |
switch (big2smal) {
|
|
236 |
case 0:dst.setElem(row, col, band, (byte)value);break;
|
|
237 |
case 1:dst.setElem(row, col, band, Byte.MAX_VALUE);break;
|
|
238 |
case 2:dst.setElem(row, col, band, (Byte)byteNoData.getValue());break;
|
|
239 |
}
|
|
240 |
}
|
|
241 |
break;
|
|
242 |
case Buffer.TYPE_SHORT:
|
|
243 |
if(value > Short.MAX_VALUE) {
|
|
244 |
switch (big2smal) {
|
|
245 |
case 0:dst.setElem(row, col, band, (short)value);break;
|
|
246 |
case 1:dst.setElem(row, col, band, Short.MAX_VALUE);break;
|
|
247 |
case 2:dst.setElem(row, col, band, (Short)shortNoData.getValue());break;
|
|
248 |
}
|
|
249 |
}
|
|
250 |
break;
|
|
251 |
case Buffer.TYPE_INT: dst.setElem(row, col, band, (int)value);break;
|
|
252 |
case Buffer.TYPE_FLOAT: dst.setElem(row, col, band, (float)value);break;
|
|
253 |
case Buffer.TYPE_DOUBLE: dst.setElem(row, col, band, (double)value);break;
|
|
254 |
}
|
|
255 |
}
|
|
256 |
|
|
257 |
/*
|
|
258 |
* (non-Javadoc)
|
|
259 |
* @see org.gvsig.raster.tools.app.basic.raster.process.RasterProcess#getResult()
|
|
260 |
*/
|
|
261 |
public Object getResult() {
|
|
262 |
HashMap<String, Object> map = new HashMap<String, Object>();
|
|
263 |
map.put(FILENAME, filename);
|
|
264 |
map.put(TIME, new Long(milis));
|
|
265 |
return map;
|
|
266 |
}
|
|
267 |
|
|
268 |
/*
|
|
269 |
* (non-Javadoc)
|
|
270 |
* @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getPercent()
|
|
271 |
*/
|
|
272 |
public int getPercent() {
|
|
273 |
return percent;
|
|
274 |
}
|
|
275 |
|
|
276 |
/*
|
|
277 |
* (non-Javadoc)
|
|
278 |
* @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getTitle()
|
|
279 |
*/
|
|
280 |
public String getTitle() {
|
|
281 |
return Messages.getText("layer_datatype");
|
|
282 |
}
|
|
283 |
}
|
0 |
284 |
|