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