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 / BandListImpl.java @ 959

History | View | Annotate | Download (13.2 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.util.ArrayList;
25
import java.util.List;
26

    
27
import org.gvsig.fmap.dal.coverage.dataset.Buffer;
28
import org.gvsig.fmap.dal.coverage.datastruct.BandList;
29
import org.gvsig.fmap.dal.coverage.datastruct.DatasetBand;
30
import org.gvsig.fmap.dal.coverage.exception.BandNotFoundInListException;
31
import org.gvsig.tools.ToolsLocator;
32
import org.gvsig.tools.dynobject.DynStruct;
33
import org.gvsig.tools.persistence.PersistenceManager;
34
import org.gvsig.tools.persistence.Persistent;
35
import org.gvsig.tools.persistence.PersistentState;
36
import org.gvsig.tools.persistence.exception.PersistenceException;
37

    
38
/**
39
 * Esta clase gestiona una lista de bandas que puede pertenecer
40
 * a un fichero (la tiene un Dataset) o puede ser una lista 
41
 * de bandas de multiples ficheros (la tiene un MultiRasterDataset). Las bandas son
42
 * almacenadas en un array donde la posici?n en este es el n?mero de banda.  
43
 * @author Nacho Brodin (nachobrodin@gmail.com)
44
 *
45
 */
46
public class BandListImpl implements BandList, Persistent {
47
        public static final String      PERSISTENT_NAME        = "BandListImpl_Persistent";
48
    public static final String      PERSISTENT_DESCRIPTION = "BandListImpl Persistent";
49
    
50
        //Band array
51
        private ArrayList<DatasetBand> bands         = new ArrayList<DatasetBand>();
52
        private int[]                       drawableBands = null;
53
                
54
        public BandListImpl() {
55
                
56
        }
57
        
58
        public BandListImpl(String fName, int bandCount, int dataType) {
59
                int[] drawBands = new int[bandCount];
60
                for (int i = 0; i < bandCount; i++) {
61
                        try {
62
                                addBand(new DatasetBandImpl(fName, i, dataType, bandCount), i);
63
                        } catch (BandNotFoundInListException e1) {
64
                        }
65
                        drawBands[i] = i;
66
                }
67
                setDrawableBands(drawBands);
68
        }
69
        
70
        /*
71
         *  (non-Javadoc)
72
         * @see java.lang.Object#clone()
73
         */
74
        public Object clone() {
75
                BandListImpl result = new BandListImpl();
76
                
77
                ArrayList<DatasetBand> b = new ArrayList<DatasetBand>();
78
                for (int i = 0; i < bands.size(); i++) 
79
                        b.add((DatasetBand)((DatasetBandImpl)bands.get(i)).clone());
80
                result.setBandArray(b);
81
                
82
                if(drawableBands != null) {
83
                        int[] drawBands = new int[drawableBands.length];
84
                        for (int i = 0; i < drawableBands.length; i++) 
85
                                drawBands[i] = drawableBands[i];
86
                        result.setDrawableBands(drawBands);
87
                }
88
                
89
                return result;
90
        }
91
        
92
        /**
93
         * Asigna el ?ltimo array de bandas a escribir solicitadas.
94
         * @param drawableBands
95
         */
96
        public void setDrawableBands(int[] drawableBands) {
97
                clearDrawableBands();
98
                this.drawableBands = drawableBands;
99
                for(int i = 0; i< drawableBands.length; i++) {
100
                        if(drawableBands[i] < 0 || drawableBands[i] >= getBandCount())
101
                                continue;
102
                        addDrawableBand(i, drawableBands[i]);
103
                }
104
        }
105
        
106
        /**
107
         * Asigna el array de bandas
108
         * @param bands
109
         */
110
        public void setBandArray(ArrayList<DatasetBand> bands) {
111
                this.bands = bands;
112
        }
113
        
114
        /**
115
         * Obtiene el ?ltimo array de bandas a escribir solicitadas.
116
         * @return
117
         */
118
        public int[] getDrawableBands() {        
119
                return drawableBands;
120
        }
121
        
122
        /**
123
         * Encuentra una banda en la lista.
124
         * @param file Fichero al que pertenece la banda buscada.
125
         * @param pos Posici?n que ocupa en el fichero.
126
         * @return true si se ha hallado la banda y false si no se 
127
         * ha encontrado
128
         */
129
        public boolean findBand(DatasetBand band) {
130
                for(int i = 0; i < bands.size(); i++) {
131
                        DatasetBand b = (DatasetBand)bands.get(i); 
132
                        if(        b.getFileName().equals(band.getFileName()) &&
133
                                b.getPosition() == band.getPosition())
134
                                return true;
135
                }
136
                return false;
137
        }
138
                
139
        /**
140
         * Obtiene la lista de bandas a pintar sobre el buffer de 
141
         * salida en forma de array. Cada elemento del array es una banda
142
         * del RasterBuf de salida que ha de dibujarse y el valor que contiene
143
         * ese elemento es la banda de la imagen que va pintada en esa banda 
144
         * del RasterBuf.
145
         * <table border=1>
146
         * <tr><td>Elem</td><td>Valor</td></tr>
147
         * <tr><td>0</td><td>1</td></tr>
148
         * <tr><td>1</td><td>0</td></tr>
149
         * <tr><td>2</td><td>-1</td></tr>
150
         * <tr><td>3</td><td>0</td></tr>
151
         * <tr><td>4</td><td>2</td></tr>
152
         * </table>
153
         * El RasterBuf tendra en la banda 0 dibujada la banda 1 de este GeoRasterFile, 
154
         * en la banda 1 la 0 del dataset, en la banda 2 no habr? ninguna de este dataset,
155
         * en la 3 la 0 y en la 4 la 2 de este GeoRasterFile.
156
         * @return lista de bandas a dibujar o un array de un elemento con valor -1.
157
         */
158
        public int[] bandsToDrawList(int nBandsDataImage) {
159
                
160
                int[] bandsToRead = new int[nBandsDataImage];
161
                for(int dataImageBand = 0;dataImageBand < nBandsDataImage;dataImageBand++) {
162
                        bandsToRead[dataImageBand] = -1;
163
                        for(int band = 0;band < bands.size();band++) {
164
                                if(        ((DatasetBand)bands.get(band)) != null &&
165
                                        ((DatasetBand)bands.get(band)).getBufferBandListToDraw() != null) {
166
                                        for(int dest=0;dest<((DatasetBand)bands.get(band)).getBufferBandListToDraw().length;dest++) {
167
                                                if(dataImageBand == ((DatasetBand)bands.get(band)).getBufferBandListToDraw()[dest])
168
                                                        bandsToRead[dataImageBand] = band;
169
                                        }
170
                                }
171
                        }
172
                }
173
        
174
                return bandsToRead;
175
        }
176
        
177
        /**
178
         * Checkea si alguna banda de la lista se est?a dibujando en el buffer de salida.
179
         * @return        true si alguna banda se est? dibujando y false si no
180
         */
181
        public boolean isDrawingAnyBand() {
182
                for(int band = 0;band < bands.size();band++) {
183
                        if(((DatasetBand)bands.get(band)).isDrawing())
184
                                return true;
185
                }
186
                return false;
187
        }
188
        
189
        //******************************
190
        //Setters and Getters
191
        //******************************
192
        
193
        /**
194
         * A?ade una banda a la lista.
195
         * @param b banda a a?adir.
196
         */
197
        public void addBand(DatasetBand b, int pos)throws BandNotFoundInListException {
198
                bands.add(b);
199
        }
200
        
201
        /**
202
         * A?ade la lista de bandas pasada por par?metro a la lista
203
         * actual. Si alguna banda ya existe no la a?ade y continua.
204
         * @param bl Lista de bandas
205
         */
206
        public void addBandList(BandList bl) {
207
                for(int i = 0; i < bl.getBandCount(); i++) {
208
                        if(!findBand(bl.getBand(i)))
209
                                bands.add(bl.getBand(i));
210
                }
211
        }
212
        
213
        /**
214
         * ELimina todas las bandas que tienen un nombre determinado.
215
         * @param name Nombre de las bandas a eliminar
216
         */
217
        public void removeBands(String name) {
218
                for(int i = 0; i < getBandCount(); i++) {
219
                        DatasetBand band = getBand(i);
220
                        if(band.getFileName().equals(name))
221
                                bands.remove(i);
222
                }
223
        }
224
        
225
        /**
226
         * Resetea la asignaci?n de dibujado de las bandas de la imagen
227
         * sobre el DataImage cuando se hace un update para esta banda.
228
         */
229
        public void clearDrawableBands() {
230
                drawableBands = null;
231
                for(int i = 0; i < getBandCount(); i++)
232
                        ((DatasetBand)bands.get(i)).clearDrawableBands();
233
        }
234
        
235
        /**
236
         * Para este GeoRasterFile asigna que bandas se pintaran
237
         * sobre el RasterBuf cuando se haga un update. Especificamos a 
238
         * trav?s de los par?metros para que posici?n del RasterBuf ir? 
239
         * dibujada con que banda del fichero de imagen.
240
         * @param posRasterBuf        Posici?n del RasterBuf que queremos pintar.
241
         * @param imageBand        Banda de la imagen que se pintar?
242
         */
243
        public void addDrawableBand(int posRasterBuf, int imageBand) {
244
                try {
245
                        ((DatasetBand)bands.get(imageBand)).setPositionToDrawInBuffer(posRasterBuf);
246
                } catch(IndexOutOfBoundsException exc) {
247
                        //No hacemos nada simplemente no inserta la banda.
248
                }
249
        }
250
        
251
        /**
252
         * Obtiene el n?mero de bandas de un RasterBuf sobre las que se pintara
253
         * alguna banda de este fichero cuando se llama a un updateBuffer. 
254
         * @return N?mero de bandas. Cero en caso de no tener ninguna asignada. 
255
         */
256
        public int getDrawableBandsCount() {
257
                int nbands = 0;
258
                for(int i = 0;i < bands.size();i++) {
259
                        DatasetBand b = (DatasetBand)bands.get(i);
260
                        if(b.getBufferBandListToDraw() != null)
261
                                nbands += b.getBufferBandListToDraw().length;
262
                }
263
                return nbands;
264
        }
265
        
266
        /**
267
         * Obtiene la banda de la posici?n i.
268
         * @param i Posici?n de la banda a obtener.
269
         * @return Banda.
270
         */
271
        public DatasetBand getBand(int i) {
272
                if(i < 0 || i >= bands.size())
273
                        return null;
274
                return (DatasetBand)bands.get(i);
275
        }
276
        
277
        /**
278
         * Obtiene el n?mero de bandas.
279
         * @return entero con el n?mero de bandas.
280
         */
281
        public int getBandCount() {
282
                return bands.size();
283
        }
284
        
285
        /*
286
         * (non-Javadoc)
287
         * @see org.gvsig.fmap.dal.coverage.datastruct.BandList#getBufferBandToDraw(java.lang.String, int)
288
         */
289
        public int[] getBufferBandToDraw(String fileName, int band) {
290
                for(int i = 0; i < bands.size(); i++) {
291
                        if(((DatasetBand)bands.get(i)).getFileName().equals(fileName))
292
                                if(((DatasetBand)bands.get(i)).getPosition() == band)
293
                                        return ((DatasetBand)bands.get(i)).getBufferBandListToDraw();
294
                }
295
                int[] r = {-1};
296
                return r;
297
        }
298
        
299
        /*
300
         * (non-Javadoc)
301
         * @see org.gvsig.fmap.dal.coverage.datastruct.BandList#getLocalBufferBandToDraw(java.lang.String, int)
302
         */
303
        public int[] getLocalBufferBandToDraw(String fileName, int band) {
304
                for(int i = 0; i < bands.size(); i++) {
305
                        if(((DatasetBand)bands.get(i)).getFileName().equals(fileName))
306
                                if(((DatasetBand)bands.get(i)).getPosition() == band)
307
                                        return ((DatasetBand)bands.get(i)).getLocalBufferBandListToDraw();
308
                }
309
                int[] r = {-1};
310
                return r;
311
        }
312
        
313
        /**
314
         * Obtiene en un array de String la lista de nombres de ficheros
315
         * @return lista de nombres de los ficheros del GeoRasterMultiFile
316
         */
317
        public String[] getBandStringList() {
318
                String[] list = new String[bands.size()];
319
                for(int i = 0; i < bands.size(); i++)
320
                        list[i] = ((DatasetBand)bands.get(i)).getFileName();
321
                return list;
322
        }
323
        
324
        /**
325
         * Obtiene en un array de enteros con la lista de la posici?n de bandas
326
         * @return lista de la posici?n de bandas
327
         */
328
        public int[] getBandPositionList() {
329
                int[] list = new int[bands.size()];
330
                for(int i = 0; i < bands.size(); i++)
331
                        list[i] = ((DatasetBand)bands.get(i)).getPosition();
332
                return list;
333
        }
334
        
335
        /**
336
         * Obtiene el tipo de dato de las bandas. Esta llamada supone que todos los tipos de dato
337
         * de las bandas son igual por lo que devolver? el primero. 
338
         * @return Entero que representa el tipo de datos de las bandas
339
         */
340
        public int getBandsDataType() {
341
                if(bands.size() <= 0)
342
                        return Buffer.TYPE_UNDEFINED;
343
                return ((DatasetBand)bands.get(0)).getDataType();
344
        }
345
        
346
        /**
347
         * Obtiene la posici?n del fichero en la lista a partir del nombre 
348
         * @param fileName Nombre del fichero
349
         * @return Posici?n del fichero o -1 si no existe. 
350
         */
351
        public int getFileNumber(String fileName) {
352
                if(fileName == null)
353
                        return -1;
354
                String aux = "";
355
                int count = -1;
356
                for(int i = 0; i < bands.size(); i++) {
357
                        if(((DatasetBand)bands.get(i)).getFileName().indexOf(aux) != 0) {
358
                                count ++;
359
                                aux = ((DatasetBand)bands.get(i)).getFileName();
360
                        }
361
                        if(((DatasetBand)bands.get(i)).getFileName().indexOf(fileName) == 0)
362
                                return count;
363
                }
364
                return -1;
365
        }
366
        
367
        /**
368
         * Limpia la lista de bandas
369
         */
370
        public void clear() {
371
                bands.clear();
372
                drawableBands = null;
373
        }
374
        
375
        /**
376
         *Muestra la lista de bandas en modo texto
377
         */
378
        public void show() {
379
                System.out.println("BandCount: " + getBandCount());
380
                System.out.println("DrawableBandsCount: " + getDrawableBandsCount());
381
                for (int i = 0; i < getBandCount(); i++) {
382
                        System.out.println("");
383
                        System.out.println("***********************");
384
                        System.out.println("Band: " + i);
385
                        System.out.println("DataType: " + ((DatasetBand)getBand(i)).getDataType());
386
                        System.out.println("FileName: " + ((DatasetBand)getBand(i)).getFileName());
387
                        System.out.println("Position: " + ((DatasetBand)getBand(i)).getPosition());
388
                        if(((DatasetBand)getBand(i)).getBufferBandListToDraw() != null) {
389
                                System.out.print("Band Dst: ");
390
                                for (int j = 0; j < ((DatasetBand)getBand(i)).getBufferBandListToDraw().length; j++)
391
                                        System.out.print(((DatasetBand)getBand(i)).getBufferBandListToDraw()[j] + " ");
392
                        }
393
                        System.out.println();
394
                }
395
                
396
        }
397
        
398
        @SuppressWarnings("unchecked")
399
        public void loadFromState(PersistentState state)
400
                        throws PersistenceException {
401
                List<DatasetBand> rois = state.getList("bands");
402
                this.bands = new ArrayList<DatasetBand>();
403
                this.bands.addAll(rois);
404

    
405
                drawableBands = (int[])state.getIntArray("drawableBands");
406
        }
407

    
408
        public void saveToState(PersistentState state) throws PersistenceException {
409
                state.set("bands", bands);                
410
                state.set("drawableBands", drawableBands);
411
        }        
412
        
413
        public static void registerPersistence() {
414
                PersistenceManager manager = ToolsLocator.getPersistenceManager();
415
                DynStruct definition = manager.getDefinition(PERSISTENT_NAME);
416
                if( definition == null ) {
417
                        definition = manager.addDefinition(
418
                                        BandListImpl.class,
419
                                        PERSISTENT_NAME,
420
                                        PERSISTENT_DESCRIPTION,
421
                                        null, 
422
                                        null
423
                        );
424
                        definition.addDynFieldList("bands").setClassOfItems(DatasetBand.class).setMandatory(true);
425
                        definition.addDynFieldList("drawableBands").setClassOfItems(int.class).setMandatory(false);
426
                }
427
        }
428

    
429
}