Statistics
| Revision:

gvsig-raster / org.gvsig.raster / trunk / org.gvsig.raster / org.gvsig.raster.lib / org.gvsig.raster.lib.impl / src / main / java / org / gvsig / raster / impl / datastruct / DefaultNoData.java @ 2616

History | View | Annotate | Download (8.5 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.impl.datastruct;
23

    
24
import java.io.File;
25
import java.io.IOException;
26
import java.util.ArrayList;
27
import java.util.List;
28
import java.util.logging.Level;
29
import java.util.logging.Logger;
30

    
31
import org.gvsig.fmap.dal.coverage.RasterLocator;
32
import org.gvsig.fmap.dal.coverage.dataset.Buffer;
33
import org.gvsig.fmap.dal.coverage.datastruct.NoData;
34
import org.gvsig.fmap.dal.coverage.exception.RmfSerializerException;
35
import org.gvsig.raster.impl.datastruct.serializer.NoDataRmfSerializer;
36
import org.gvsig.raster.impl.store.rmf.ClassSerializer;
37
import org.gvsig.raster.impl.store.rmf.RmfBlocksManager;
38

    
39
/**
40
 * This class represents the implementation of a nodata value.
41
 * 
42
 * @author Nacho Brodin (nachobrodin@gmail.com)
43
 */
44
public class DefaultNoData implements NoData {
45
        private List<Number>             noDataByBand        = new ArrayList<Number>();
46
        private List<Number>             nativeNoDataByBand  = new ArrayList<Number>();
47
        private int                      bandCount           = 1;
48
        private String                   fileName            = null;
49
        private boolean                  noDataAsTransparent = true;
50
        private int                      dataType            = Buffer.TYPE_UNDEFINED;
51
        private final static Logger      log                 = Logger.getLogger(DefaultNoData.class.toString());
52
        
53
        /**
54
         * Constructor
55
         */
56
        public DefaultNoData() {
57
        }
58
        
59
        /**
60
         * Constructor.
61
         * @param fileName
62
         *               Name of file owner of this nodata value. This string is useful to 
63
         *        save and load throw the rmf.
64
         */
65
        public DefaultNoData(String fileName) {
66
                this.fileName = fileName;
67
        }
68
        
69
        /**
70
         * Constructor. Only for rasters with one band
71
         * 
72
         * @param noData
73
         *        value to assign to all bands
74
         * @param nativeNoData
75
         *        The native is the original value
76
         *        saved in the head of the file or its metadata.
77
         * @param fileName
78
         *        Name of file owner of this nodata value. This string is useful to 
79
         *        save and load throw the rmf.
80
         */
81
        public DefaultNoData(Number noData, Number nativeNoData, String fileName) {
82
                setValue(noData);
83
                setNativeValue(nativeNoData);
84
                this.fileName = fileName;
85
                setDataType(noData);
86
        }
87
        
88
        private void setDataType(Number number) {
89
                if(number instanceof Byte)
90
                        dataType = Buffer.TYPE_BYTE;
91
                if(number instanceof Short)
92
                        dataType = Buffer.TYPE_SHORT;
93
                if(number instanceof Integer)
94
                        dataType = Buffer.TYPE_INT;
95
                if(number instanceof Float)
96
                        dataType = Buffer.TYPE_FLOAT;
97
                if(number instanceof Double)
98
                        dataType = Buffer.TYPE_DOUBLE;
99
        }
100
        
101
        /**
102
         * Constructor
103
         * 
104
         * @param noData
105
         *        value to assign to all bands
106
         * @param nativeNoData
107
         *        The native is the original value
108
         *        saved in the head of the file or its metadata.
109
         * @param fileName
110
         *        Name of file owner of this nodata value. This string is useful to
111
         *        save the rmf file
112
         * @param bandCount
113
         *        Number of bands of the file
114
         */
115
        public DefaultNoData(Number noData, Number nativeNoData, String fileName, int bandCount) {
116
                setValue(noData);
117
                setNativeValue(nativeNoData);
118
                this.fileName = fileName;
119
                setDataType(noData);
120
                if(bandCount > 1)
121
                        this.bandCount = bandCount;
122
        }
123
        
124
        public Number getValue() {
125
                try {
126
                        return noDataByBand.get(0);
127
                } catch (ArrayIndexOutOfBoundsException e) {
128
                        return null;
129
                }
130
        }
131
        
132
        public boolean isDefined() {
133
                return (getValue() != null);
134
        }
135
        
136
        public void setValue(Number noData) {
137
                noDataByBand.clear();
138
                for (int i = 0; i < bandCount; i++) {
139
                        noDataByBand.add(noData);        
140
                }
141
                setDataType(noData);
142
        }
143
        
144
        public Number getNativeValue() {
145
                try {
146
                        return nativeNoDataByBand.get(0);
147
                } catch (ArrayIndexOutOfBoundsException e) {
148
                        return null;
149
                }
150
        }
151
        
152
        public void setNativeValue(Number nativeNoDataValue) {
153
                nativeNoDataByBand.clear();
154
                for (int i = 0; i < bandCount; i++) {
155
                        nativeNoDataByBand.add(nativeNoDataValue);        
156
                }
157
                setDataType(nativeNoDataValue);
158
        }
159

    
160
        public void delete() {
161
                noDataByBand.clear();
162
                for (int i = 0; i < bandCount; i++) {
163
                        noDataByBand.add(null);        
164
                }
165
        }
166

    
167
        public void restore() {
168
                if(nativeNoDataByBand != null && nativeNoDataByBand.size() == bandCount) {
169
                        noDataByBand.clear();
170
                        for (int i = 0; i < bandCount; i++) {
171
                                noDataByBand.add(nativeNoDataByBand.get(i));        
172
                        }
173
                }
174
        }
175

    
176
        public void save() {
177
                try {
178
                        String fileRMF = RasterLocator.getManager().getFileUtils().getNameWithoutExtension(fileName) + ".rmf";
179
                        RmfBlocksManager blocksManager = new RmfBlocksManager(fileRMF);
180
                        ClassSerializer serializerObject = new NoDataRmfSerializer(this);
181

    
182
                        if (!blocksManager.checkRmf())
183
                                throw new RmfSerializerException("Error al comprobar el fichero Rmf");
184

    
185
                        blocksManager.addClient(serializerObject);
186
                        try {
187
                                blocksManager.write(true);
188
                        } catch (IOException e) {
189
                                throw new RmfSerializerException("Error al escribir el fichero Rmf", e);
190
                        }
191
                        blocksManager.removeAllClients();
192
                } catch (RmfSerializerException e) {
193
                        log.log(Level.SEVERE, "error_salvando_rmf", e);
194
                }
195
        }
196
        
197
        public void load() {
198
                String fileRMF = RasterLocator.getManager().getFileUtils().getNameWithoutExtension(fileName) + ".rmf";
199
                RmfBlocksManager blocksManager = new RmfBlocksManager(fileRMF);
200
                ClassSerializer serializerObject = new NoDataRmfSerializer(this);
201

    
202
                if (!blocksManager.checkRmf()) {
203
                        log.info("Error al comprobar el fichero Rmf " + fileRMF);
204
                }
205

    
206
                blocksManager.addClient(serializerObject);
207
                try {
208
                        if(new File(fileRMF).exists())
209
                                blocksManager.read(null);
210
                } catch (Exception e) {
211
                        log.log(Level.WARNING, "Error al escribir el fichero Rmf", e);
212
                }
213
                blocksManager.removeAllClients();
214
        }
215
        
216
        public void setFileName(String fileName) {
217
                this.fileName = fileName;
218
        }
219
        
220
        public boolean isNoDataTransparent() {
221
                return noDataAsTransparent;
222
        }
223

    
224
        public void setNoDataTransparent(boolean noDataAsTransparent) {
225
                this.noDataAsTransparent = noDataAsTransparent;
226
        }
227
        
228
        public int getDataType() {
229
                return dataType;
230
        }
231
        
232
        public void setDataType(int datatype) {
233
                this.dataType = datatype;
234
        }
235
        
236
        @SuppressWarnings("unchecked")
237
        public boolean compare(NoData noData) {        
238
                String f = ((DefaultNoData)noData).fileName;
239
                
240
                if((f == null || fileName == null) && f != fileName)
241
                        return false;
242
                
243
                if( fileName != null &&  f != null &&
244
                        fileName.compareTo(f) != 0)
245
                        return false;
246
                
247
                if(bandCount != ((DefaultNoData)noData).bandCount)
248
                        return false;
249
                
250
                if(dataType != ((DefaultNoData)noData).dataType)
251
                        return false;
252
                
253
                for (int i = 0; i < noDataByBand.size(); i++) {
254
                        if(((Comparable<Number>)noDataByBand.get(i)).compareTo(noData.getValue()) != 0)
255
                                return false;
256
                }
257
                for (int i = 0; i < nativeNoDataByBand.size(); i++) {
258
                        if(((Comparable<Number>)nativeNoDataByBand.get(i)).compareTo(noData.getValue()) != 0)
259
                                return false;
260
                }
261
                return true;
262
        }
263
        
264
        public Object clone() {
265
                DefaultNoData copy = new DefaultNoData(getValue(), getNativeValue(), fileName, bandCount);
266
                copy.noDataAsTransparent = noDataAsTransparent;
267
                return copy;
268
        }
269
        
270
        //***********************************************
271
        //Only for file with more than one band
272
        
273
        public Number getValueByBand(int nBand) {
274
                try {
275
                        return noDataByBand.get(nBand);
276
                } catch (IndexOutOfBoundsException e) {
277
                        return null;
278
                }
279
        }
280
        
281
        public void setValueByBand(Number noDataValue, int nBand) {
282
                noDataByBand.add(nBand, noDataValue);
283
                setDataType(noDataValue);
284
        }
285

    
286
        public int getBandCount() {
287
                return bandCount;
288
        }
289
        
290
        public void setBandCount(int bandCount) {
291
                this.bandCount = bandCount;
292
        }
293
        
294
}