Statistics
| Revision:

root / trunk / libraries / libRaster / src / org / gvsig / raster / dataset / BandList.java @ 10973

History | View | Annotate | Download (8.46 KB)

1 10939 nacho
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2006 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 */
19
package org.gvsig.raster.dataset;
20
21
import java.util.ArrayList;
22
23
/**
24
 * Esta clase gestiona una lista de bandas que puede pertenecer
25
 * a un fichero (la tiene un geoRasterFile) o puede ser una lista
26
 * de bandas de multiples ficheros (la tiene un multifiledataset)
27
 * @author Nacho Brodin (nachobrodin@gmail.com)
28
 *
29
 */
30
public class BandList {
31
        //Band array
32
        private ArrayList bands = new ArrayList();
33
        private int[]        drawableBands = null;
34
35
        /**
36
         * Asigna el ?ltimo array de bandas a escribir solicitadas.
37
         * @param drawableBands
38
         */
39
        public void setDrawableArray(int[] drawableBands){
40
                this.drawableBands = drawableBands;
41
        }
42
43
        /**
44
         * Obtiene el ?ltimo array de bandas a escribir solicitadas.
45
         * @return
46
         */
47
        public int[] getDrawableArray(){
48
                return this.drawableBands;
49
        }
50
51
        /**
52
         * Encuentra una banda en la lista.
53
         * @param file Fichero al que pertenece la banda buscada.
54
         * @param pos Posici?n que ocupa en el fichero.
55
         * @return true si se ha hallado la banda y false si no se
56
         * ha encontrado
57
         */
58
        public boolean findBand(Band band){
59
                for(int i = 0; i < bands.size(); i++){
60
                        Band b = (Band)bands.get(i);
61
                        if(        b.getFileName().equals(band.getFileName()) &&
62
                                b.getPosition() == band.getPosition())
63
                                return true;
64
                }
65
                return false;
66
        }
67
68
        /**
69
         * Obtiene la lista de bandas a pintar sobre el buffer de
70
         * salida en forma de array. Cada elemento del array es una banda
71
         * del RasterBuf de salida que ha de dibujarse y el valor que contiene
72
         * ese elemento es la banda de la imagen que va pintada en esa banda
73
         * del RasterBuf.
74
         * <table border=1>
75
         * <tr><td>Elem</td><td>Valor</td></tr>
76
         * <tr><td>0</td><td>1</td></tr>
77
         * <tr><td>1</td><td>0</td></tr>
78
         * <tr><td>2</td><td>-1</td></tr>
79
         * <tr><td>3</td><td>0</td></tr>
80
         * <tr><td>4</td><td>2</td></tr>
81
         * </table>
82
         * El RasterBuf tendra en la banda 0 dibujada la banda 1 de este GeoRasterFile,
83
         * en la banda 1 la 0 del dataset, en la banda 2 no habr? ninguna de este dataset,
84
         * en la 3 la 0 y en la 4 la 2 de este GeoRasterFile.
85
         * @return lista de bandas a dibujar o un array de un elemento con valor -1.
86
         */
87
        public int[] bandsToDrawList(int nBandsDataImage){
88
89
                int[] bandsToRead = new int[nBandsDataImage];
90
                for(int dataImageBand = 0;dataImageBand < nBandsDataImage;dataImageBand++){
91
                        bandsToRead[dataImageBand] = -1;
92
                        for(int band = 0;band < bands.size();band++){
93
                                if(        ((Band)bands.get(band)) != null &&
94
                                        ((Band)bands.get(band)).getDataImageBandToDraw() != null){
95
                                        for(int dest=0;dest<((Band)bands.get(band)).getDataImageBandToDraw().length;dest++){
96
                                                if(dataImageBand == ((Band)bands.get(band)).getDataImageBandToDraw()[dest])
97
                                                        bandsToRead[dataImageBand] = band;
98
                                        }
99
                                }
100
                        }
101
                }
102
103
                return bandsToRead;
104
        }
105
106
        /**
107
         * Checkea si alguna banda de la lista se est?a dibujando en el buffer de salida.
108
         * @return        true si alguna banda se est? dibujando y false si no
109
         */
110
        public boolean isDrawingAnyBand(){
111
                for(int band = 0;band < bands.size();band++){
112
                        if(((Band)bands.get(band)).isDrawing())
113
                                return true;
114
                }
115
                return false;
116
        }
117
118
        //******************************
119
        //Setters and Getters
120
        //******************************
121
122
        /**
123
         * A?ade una banda a la lista.
124
         * @param b banda a a?adir.
125
         */
126
        public void addBand(Band b, int pos)throws BandFoundInListException{
127
                bands.add(b);
128
        }
129
130
        /**
131
         * A?ade la lista de bandas pasada por par?metro a la lista
132
         * actual. Si alguna banda ya existe no la a?ade y continua.
133
         * @param bl Lista de bandas
134
         */
135
        public void addBandList(BandList bl){
136
                for(int i = 0; i < bl.getBandCount(); i++){
137
                        if(!findBand(bl.getBand(i)))
138
                                bands.add(bl.getBand(i));
139
                }
140
        }
141
142
        /**
143
         * ELimina todas las bandas que tienen un nombre determinado.
144
         * @param name Nombre de las bandas a eliminar
145
         */
146
        public void removeBands(String name){
147
                for(int i = 0; i < getBandCount(); i++){
148
                        Band band = getBand(i);
149
                        if(band.getFileName().equals(name))
150
                                bands.remove(i);
151
                }
152
        }
153
154
        /**
155
         * Resetea la asignaci?n de dibujado de las bandas de la imagen
156
         * sobre el DataImage cuando se hace un update para esta banda.
157
         */
158
        public void clearDrawableBand(){
159
                for(int i = 0; i < getBandCount(); i++)
160
                        ((Band)bands.get(i)).clearDrawableBands();
161
        }
162
163
        /**
164
         * Para este GeoRasterFile asigna que bandas se pintaran
165
         * sobre el RasterBuf cuando se haga un update. Especificamos a
166
         * trav?s de los par?metros para que posici?n del RasterBuf ir?
167
         * dibujada con que banda del fichero de imagen.
168
         * @param posRasterBuf        Posici?n del RasterBuf que queremos pintar.
169
         * @param imageBand        Banda de la imagen que se pintar?
170
         */
171
        public void addDrawableBand(int posRasterBuf, int imageBand){
172
                try{
173
                        ((Band)bands.get(imageBand)).setPositionToDrawInBuffer(posRasterBuf);
174
                }catch(IndexOutOfBoundsException exc){
175
                        //No hacemos nada simplemente no inserta la banda.
176
                }
177
        }
178
179
        /**
180
         * Obtiene el n?mero de bandas de un RasterBuf sobre las que se pintara
181
         * alguna banda de este fichero cuando se llama a un updateBuffer.
182
         * @return N?mero de bandas. Cero en caso de no tener ninguna asignada.
183
         */
184
        public int getDrawableBandsCount(){
185
                int nbands = 0;
186
                for(int i = 0;i<bands.size();i++){
187
                        Band b = (Band)bands.get(i);
188
                        if(b.getDataImageBandToDraw() != null)
189
                                nbands += b.getDataImageBandToDraw().length;
190
                }
191
                return nbands;
192
        }
193
194
        /**
195
         * Obtiene la banda de la posici?n i.
196
         * @param i Posici?n de la banda a obtener.
197
         * @return Banda.
198
         */
199
        public Band getBand(int i){
200
                if(i < 0 || i >= bands.size())
201
                        return null;
202
                return (Band)bands.get(i);
203
        }
204
205
        /**
206
         * Obtiene el n?mero de bandas.
207
         * @return entero con el n?mero de bandas.
208
         */
209
        public int getBandCount(){
210
                return bands.size();
211
        }
212
213
        /**
214
         * A partir de un nombre de fichero y un n?mero de banda obtiene la banda o bandas del buffer de salida
215
         * donde se pinta.
216
         * @param fileName Nombre de fichero
217
         * @param band N?mero de banda de la imagen
218
         * @return banda o bandas del buffer de salida donde se dibuja.
219
         */
220
        public int[] getBufferBandToDraw(String fileName, int band){
221
                for(int i = 0; i < bands.size(); i++){
222
                        if(((Band)bands.get(i)).getFileName().equals(fileName))
223
                                if(((Band)bands.get(i)).getPosition() == band)
224
                                        return ((Band)bands.get(i)).getDataImageBandToDraw();
225
                }
226
                int[] r = {-1};
227
                return r;
228
        }
229
        /**
230
         * Obtiene en un array de String la lista de nombres de ficheros
231
         * @return lista de nombres de los ficheros del GeoRasterMultiFile
232
         */
233
        public String[] getBandStringList(){
234
                String[] list = new String[bands.size()];
235
                for(int i = 0; i < bands.size(); i++)
236
                        list[i] = ((Band)bands.get(i)).getFileName();
237
                return list;
238
        }
239
240
        /**
241
         * Obtiene en un array de enteros con la lista de la posici?n de bandas
242
         * @return lista de la posici?n de bandas
243
         */
244
        public int[] getBandPositionList(){
245
                int[] list = new int[bands.size()];
246
                for(int i = 0; i < bands.size(); i++)
247
                        list[i] = ((Band)bands.get(i)).getPosition();
248
                return list;
249
        }
250
251
        /**
252
         * Obtiene el tipo de dato de las bandas. Esta llamada supone que todos los tipos de dato
253
         * de las bandas son igual por lo que devolver? el primero.
254
         * @return Entero que representa el tipo de datos de las bandas
255
         */
256
        public int getBandsDataType(){
257
                if(bands.size() <= 0)
258
                        return IBuffer.TYPE_UNDEFINED;
259
                return ((Band)bands.get(0)).getDataType();
260
        }
261
262
        /**
263
         * Obtiene la posici?n del fichero en la lista a partir del nombre
264
         * @param fileName Nombre del fichero
265
         * @return Posici?n del fichero o -1 si no existe.
266
         */
267
        public int getFileNumber(String fileName){
268
                String aux = "";
269
                int count = -1;
270
                for(int i = 0; i < bands.size(); i++){
271
                        if(((Band)bands.get(i)).getFileName().indexOf(aux) != 0){
272
                                count ++;
273
                                aux = ((Band)bands.get(i)).getFileName();
274
                        }
275
                        if(((Band)bands.get(i)).getFileName().indexOf(fileName) == 0)
276
                                return count;
277
                }
278
                return -1;
279
        }
280
}