Statistics
| Revision:

svn-gvsig-desktop / tags / v1_1_Build_1014 / libraries / libCq_CMS_praster / src / org / cresques / io / data / GeoRasterMultiFile.java @ 13593

History | View | Annotate | Download (14.1 KB)

1
/* 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.cresques.io.data;
20

    
21
import java.awt.geom.Point2D;
22
import java.io.File;
23
import java.util.ArrayList;
24

    
25
import org.cresques.io.GeoRasterFile;
26
import org.cresques.io.exceptions.BandFoundInListException;
27
import org.cresques.io.exceptions.FileFoundInListException;
28
import org.cresques.io.exceptions.NotSupportedExtensionException;
29
import org.cresques.px.Extent;
30

    
31
/**
32
 * Clase que representa una imagen de raster georreferenciada formada por varias
33
 * imagenes de disco que tienen la misma extensi?n. Contiene funcionalidades para 
34
 * abrir ficheros, gestionar el extent, pintar el raster sobre un DataImage con 
35
 * su gesti?n de bandas correspondiente.
36
 *  
37
 * @author Nacho Brodin (brodin_ign@gva.es)
38
 *
39
 */
40
public class GeoRasterMultiFile{
41
        //File list
42
        private ArrayList                 files = new ArrayList();
43
        private String                        name = null;
44
        //Band list
45
        private BandList                bandList = new BandList();
46
                        
47
        public GeoRasterMultiFile(String name){
48
                this.name = name;
49
        }
50
        
51
        /**
52
         * Add a file to the list.
53
         * @param f file to add.
54
         */
55
        public void addFile(GeoRasterFile f)throws FileFoundInListException{
56
                if(findFile(f))
57
                        throw new FileFoundInListException("The file already is in list.");
58
                files.add(f);
59
                addBands(f);
60
        }
61
        
62
        /**
63
         * A?ade un fichero a la lista a partir de su nombre
64
         * @param f fichero a a?adir.
65
         */
66
        public void addFile(String fileName)throws FileFoundInListException, NotSupportedExtensionException{
67
                if(findFile(fileName))
68
                        throw new FileFoundInListException("The file already is in list.");
69
                GeoRasterFile f = GeoRasterFile.openFile(null, fileName);
70
                files.add(f);
71
                addBands(f);
72
        }
73
        
74
        /**
75
         * A?ade el fichero a lista de georrasterfiles y sus bandas a la lista de bandas
76
         * @param grf
77
         */
78
        private void addBands(GeoRasterFile grf){
79
                if(grf == null)
80
                        return;
81
                
82
                int dataType = grf.getDataType();
83
                for(int i = 0; i < grf.getBandCount();i++){
84
                        try{
85
                                Band band = new Band(grf.getName(), i, dataType);
86
                                bandList.addBand(band, i);
87
                        }catch(BandFoundInListException ex){
88
                                //No a?adimos la banda
89
                        }
90
                }
91
        }
92
        
93
        /**
94
         * Elimina un fichero a la lista a partir de su nombre
95
         * @param fileName        Nombre del fichero a eliminar.
96
         */
97
        public void removeFile(String fileName){
98
                for(int i=0;i<files.size();i++){
99
                        if(((GeoRasterFile)files.get(i)).getName().equals(fileName)){
100
                                files.remove(i);
101
                                bandList.removeBands(fileName);
102
                                return;
103
                        }
104
                }
105
        }
106
        
107
        /**
108
         * Elimina un fichero a la lista
109
         * @param file Fichero a eliminar
110
         */
111
        public void removeFile(GeoRasterFile file){
112
                for(int i=0;i<files.size();i++){
113
                        if(((GeoRasterFile)files.get(i)).getName().equals(file.getName())){
114
                                files.remove(i);
115
                                bandList.removeBands(file.getName());
116
                                return;
117
                        }
118
                }
119
        }
120
                
121
        /**
122
         * Obtiene el n?mero de ficheros en la lista
123
         * @return integer.
124
         */
125
        public int getFileCount(){
126
                return files.size();
127
        }
128
        
129
        /**
130
         * Encuentra un fichero en la lista.
131
         * @param file Fichero b?scado.
132
         * @return true si se ha hallado el fichero y false si no se 
133
         * ha encontrado
134
         */
135
        public boolean findFile(GeoRasterFile file){
136
                for(int i = 0;i<files.size();i++){
137
                        GeoRasterFile grf = (GeoRasterFile)files.get(i); 
138
                        if(        grf.getName().equals(file.getName()))
139
                                return true;
140
                }
141
                return false;
142
        }
143
        
144
        /**
145
         * Encuentra un fichero en la lista.
146
         * @param file Fichero b?scado.
147
         * @return true si se ha hallado el fichero y false si no se 
148
         * ha encontrado
149
         */
150
        public boolean findFile(String fileName){
151
                for(int i = 0;i<files.size();i++){
152
                        if(((GeoRasterFile)files.get(i)).getName().equals(fileName))
153
                                return true;
154
                }
155
                return false;
156
        }
157
                            
158
        /**
159
         * @see org.javaGeoRaster.io.GeoData
160
         */
161
        public void close(){
162
                for(int i = 0; i < files.size(); i++)
163
                        ((GeoRasterFile)files.get(i)).close();
164
        }
165
        
166
        /**
167
         * Obtiene en un array de String la lista de nombres de ficheros
168
         * @return lista de nombres de los ficheros del GeoRasterMultiFile
169
         */
170
        public String[] getNameFileStringList(){
171
                String[] list = new String[files.size()];
172
                for(int i = 0; i < files.size(); i++)
173
                        list[i] = ((GeoRasterFile)files.get(i)).getName();
174
                return list;
175
        }
176
        
177
        /**
178
         * Obtiene una ventana de datos de la imagen a partir de coordenadas reales. 
179
         * No aplica supersampleo ni subsampleo sino que devuelve una matriz de igual tama?o a los
180
         * pixeles de disco. 
181
         * @param x Posici?n X superior izquierda
182
         * @param y Posici?n Y superior izquierda
183
         * @param w Ancho en coordenadas reales
184
         * @param h Alto en coordenadas reales
185
         * @return Buffer de datos
186
         */
187
        public RasterBuf getWindowRaster(double x, double y, double w, double h) {
188
                
189
                Extent selectedExtent = new Extent(x, y, x + w, y - h);
190

    
191
                int width = (int)((selectedExtent.width() * ((GeoRasterFile)files.get(0)).getWidth()) / ((GeoRasterFile)files.get(0)).getExtent().width());
192
                int height = (int)((selectedExtent.height() * ((GeoRasterFile)files.get(0)).getHeight()) / ((GeoRasterFile)files.get(0)).getExtent().height());
193
                                        
194
                RasterBuf raster = new RasterBuf(getDataType()[0], width, height, bandList.getDrawableBandsCount(), false);
195
                
196
                for(int i = 0; i < files.size(); i++)
197
                        raster = ((GeoRasterFile)files.get(i)).getWindowRaster(x, y, w, h, bandList, raster);
198
                                
199
                return raster;
200
        }
201
        
202
        /**
203
         * Obtiene una ventana de datos de la imagen a partir de coordenadas reales. 
204
         * Aplica supersampleo o subsampleo en funci?n del tama?o del buffer. Esta operaci?n la gestiona
205
         * el driver.
206
         * @param minX Valor m?nimo de la X en coordenadas reales
207
         * @param minY Valor m?nimo de la Y en coordenadas reales
208
         * @param maxX Valor m?ximo de la X en coordenadas reales
209
         * @param maxY Valor m?ximo de la Y en coordenadas reales
210
         * @param bufWidth ancho del buffer lde datos
211
         * @param bufHeight alto del buffer de datos
212
         * @param bandList
213
         * @return Buffer de datos
214
         */
215
        public RasterBuf getWindowRaster(double minX, double minY, double maxX, double maxY, 
216
                                                                        int bufWidth, int bufHeight) {
217
                RasterBuf raster = new RasterBuf(getDataType()[0], bufWidth, bufHeight, bandList.getDrawableBandsCount(), true);
218
                
219
                for(int i = 0; i < files.size(); i++)
220
                        raster = ((GeoRasterFile)files.get(i)).getWindowRaster(minX, minY, maxX, maxY, bufWidth, bufHeight, bandList, raster);
221
                                                        
222
                return raster;
223
        }
224
        
225
        /**
226
         * Obtiene una ventana de datos de la imagen a partir de coordenadas reales. 
227
         * No aplica supersampleo ni subsampleo sino que devuelve una matriz de igual tama?o a los
228
         * pixeles de disco. 
229
         * @param x Posici?n X superior izquierda
230
         * @param y Posici?n Y superior izquierda
231
         * @param w Ancho en coordenadas pixel
232
         * @param h Alto en coordenadas pixel
233
         * @param bandList
234
         * @return Buffer de datos
235
         */
236
        public RasterBuf getWindowRaster(int x, int y, int w, int h) {
237
                                
238
                RasterBuf raster = new RasterBuf(getDataType()[0], w, h, bandList.getDrawableBandsCount(), true);
239
                
240
                for(int i = 0; i < files.size(); i++)
241
                        raster = ((GeoRasterFile)files.get(i)).getWindowRaster(x, y, w, h, bandList, raster);
242
                                                        
243
                return raster;
244
        }
245
        
246
        /**
247
         * Obtiene una ventana de datos de la imagen a partir de coordenadas reales. 
248
         * No aplica supersampleo ni subsampleo sino que devuelve una matriz de igual tama?o a los
249
         * pixeles de disco. 
250
         * @param x Posici?n X superior izquierda
251
         * @param y Posici?n Y superior izquierda
252
         * @param w Ancho en coordenadas reales
253
         * @param h Alto en coordenadas reales
254
         * @return Buffer de datos
255
         */
256
        public RasterBuf getWindowRasterWithNoData(double x, double y, double w, double h) {
257
                
258
                Extent selectedExtent = new Extent(x, y, x + w, y - h);
259

    
260
                //Leemos pixels completos aunque el valor obtenido sea decimal. Esto se consigue redondeando
261
                //por arriba el m?s alto y por abajo el menor y luego restandolos
262
                
263
                Point2D p1 = ((GeoRasterFile)files.get(0)).worldToRaster(new Point2D.Double(x, y));
264
                Point2D p2 = ((GeoRasterFile)files.get(0)).worldToRaster(new Point2D.Double(x + w, y - h));
265
                int width = (int)Math.abs(Math.ceil(p2.getX()) - Math.floor(p1.getX())); 
266
                int height = (int)Math.abs(Math.floor(p1.getY()) - Math.ceil(p2.getY()));
267
                
268
                RasterBuf raster = new RasterBuf(getDataType()[0], width, height, bandList.getDrawableBandsCount(), true);
269
                
270
                //Si no vamos a ajustar el extent al raster inicializamos el buffer a noData ya que este puede ser
271
                //m?s grande y salirse de los l?mites.
272
                initBufferToNoData(raster, bandList);                        
273
                                
274
                for(int i = 0; i < files.size(); i++)
275
                        raster = ((GeoRasterFile)files.get(i)).getWindowRasterWithNoData(x, y, w, h, bandList, raster);
276
                                                        
277
                return raster;
278
        }
279
                        
280
        /**
281
         * Inicializa el buffer a valores NoData
282
         * @param raster Buffer a inicializar
283
         * @param bandList Lista de bandas
284
         */
285
        private void initBufferToNoData(RasterBuf raster, BandList bandList){
286
                for(int i = 0; i < bandList.getDrawableBandsCount(); i++){
287
                        switch(getDataType()[0]){
288
                        case RasterBuf.TYPE_BYTE:raster.assign(i, raster.getByteNoDataValue());break;
289
                        case RasterBuf.TYPE_SHORT:raster.assign(i, raster.getShortNoDataValue());break;
290
                        case RasterBuf.TYPE_INT:raster.assign(i, raster.getIntNoDataValue());break;
291
                        case RasterBuf.TYPE_FLOAT:raster.assign(i, raster.getFloatNoDataValue());break;
292
                        case RasterBuf.TYPE_DOUBLE:raster.assign(i, raster.getNoDataValue());break;
293
                        }
294
                }        
295
        }
296
        //******************************
297
        //Setters and Getters
298
        //******************************
299
        
300
        /**
301
         * Calcula el tama?o de los ficheros en disco
302
         * @return tama?o en bytes de todos los ficheros de la lista
303
         */
304
        public long getFileSize(){
305
                int len = 0;
306
                for(int i=0;i<files.size();i++){
307
                        if(((GeoRasterFile)files.get(i)) != null){
308
                                File f = new File(((GeoRasterFile)files.get(i)).getName());
309
                                len += f.length();
310
                        }
311
                }
312
                return len;
313
        }
314
        
315
        /**
316
         * Obtiene la altura de la imagen a partir de la primera
317
         * @return altura
318
         */
319
        public int getHeight() {
320
                for(int i=0;i<files.size();i++)
321
                        if(((GeoRasterFile)files.get(i)) != null)
322
                                return ((GeoRasterFile)files.get(i)).getHeight();
323
                return 0;
324
        }
325

    
326
        /**
327
         * Obtiene la anchura de la imagen a partir de la primera
328
         * @return anchura
329
         */
330
        public int getWidth() {
331
                for(int i=0;i<files.size();i++)
332
                        if(((GeoRasterFile)files.get(i)) != null)
333
                                return ((GeoRasterFile)files.get(i)).getWidth();
334
                return 0;        }
335
        
336
        /**
337
         * Obtiene el n?mero de bandas del fichero
338
         * @return
339
         */
340
        public int getBandCount(){
341
                return bandList.getBandCount();
342
        }
343

    
344
        /**
345
         * Obtiene el tipo de dato por banda
346
         * @return tipo de dato por banda
347
         */
348
        public int[] getDataType() {
349
                int[] dt = new int[getFileCount()];
350
                for(int i=0;i<files.size();i++)
351
                        dt[i] = ((GeoRasterFile)files.get(i)).getDataType();
352
                                
353
            if(dt.length == 0)
354
                    return null;
355
            else
356
                    return dt;
357
        }
358
        
359
        /**
360
         * Obtiene fichero de la posici?n i.
361
         * @param i Posici?n del fichero a obtener.
362
         * @return GeoRasterFileDataset.
363
         */
364
        public GeoRasterFile getFile(int i){
365
                return (GeoRasterFile)files.get(i);
366
        }
367
        
368
        /**
369
         * Obtiene fichero de nombre fileName.
370
         * @param i Posici?n del fichero a obtener.
371
         * @return GeoRasterFile.
372
         */
373
        public GeoRasterFile getFile(String fileName){
374
                for(int i=0;i<files.size();i++){
375
                        if(((GeoRasterFile)files.get(i)).getName().equals(fileName))
376
                                return (GeoRasterFile)files.get(i); 
377
                }
378
                return null;                
379
        }
380
        
381
        /**
382
         * Asigna el nombre al GeoRasterMultiFile
383
         * @param name Nombre del GeoRasterMultiFile
384
         */
385
        public void setName(String name){
386
                this.name = name;
387
        }
388
        
389
        /**
390
         * Obtiene la lista de bandas
391
         * @return BandList
392
         */
393
        public BandList getBands(){
394
                return bandList;
395
        }
396
        
397
        /**
398
         * Obtiene la coordenada X m?nima de toda la lista
399
         * @return Coordenada X m?nima
400
         */
401
        public double getMinX(){
402
                double minx = Double.MAX_VALUE;
403
                for(int i = 0; i < files.size(); i++){
404
                        double aux = ((GeoRasterFile)files.get(i)).getExtent().getMin().getX();
405
                        if(aux < minx)
406
                                minx = aux;
407
                }
408
                return minx;
409
        }
410
        
411
        /**
412
         * Obtiene la coordenada Y m?nima de toda la lista
413
         * @return Coordenada Y m?nima
414
         */
415
        public double getMinY(){
416
                double miny = Double.MAX_VALUE;
417
                for(int i = 0; i < files.size(); i++){
418
                        double aux = ((GeoRasterFile)files.get(i)).getExtent().getMin().getY();
419
                        if(aux < miny)
420
                                miny = aux;
421
                }
422
                return miny;
423
        }
424
        
425
        /**
426
         * Obtiene la coordenada Y m?xima de toda la lista
427
         * @return Coordenada Y m?xima
428
         */
429
        public double getMaxX(){
430
                double maxx = Double.MIN_VALUE;
431
                for(int i = 0; i < files.size(); i++){
432
                        double aux = ((GeoRasterFile)files.get(i)).getExtent().getMin().getY();
433
                        if(aux > maxx)
434
                                maxx = aux;
435
                }
436
                return maxx;
437
        }
438

    
439
        /**
440
         * Obtiene la coordenada Y m?xima de toda la lista
441
         * @return Coordenada Y m?xima
442
         */
443
        public double getMaxY(){
444
                double maxy = Double.MIN_VALUE;
445
                for(int i = 0; i < files.size(); i++){
446
                        double aux = ((GeoRasterFile)files.get(i)).getExtent().getMin().getY();
447
                        if(aux > maxy)
448
                                maxy = aux;
449
                }
450
                return maxy;
451
        }
452
        
453
        /**
454
         * Obtiene el extent del multi fichero. Este corresponde al primer
455
         * GeoRasterFile de la lista.
456
         * @return Extent
457
         */
458
        public Extent getExtent(){
459
                if(files.size() == 0)
460
                        return null;
461
                else
462
                        return ((GeoRasterFile)files.get(0)).getExtent();
463
        }
464
        
465
        /**
466
         * Convierte un punto desde coordenadas pixel a coordenadas del mundo.
467
         * @param pt Punto a transformar
468
         * @return punto transformado en coordenadas del mundo
469
         */
470
        public Point2D rasterToWorld(Point2D pt) {
471
                return ((GeoRasterFile)files.get(0)).rasterToWorld(pt);
472
        }
473
        
474
        /**
475
         * Convierte un punto desde del mundo a coordenadas pixel.
476
         * @param pt Punto a transformar
477
         * @return punto transformado en coordenadas pixel
478
         */
479
        public Point2D worldToRaster(Point2D pt) {
480
                return ((GeoRasterFile)files.get(0)).worldToRaster(pt);
481
        }
482
}