Statistics
| Revision:

root / trunk / libraries / libRaster / src / org / gvsig / raster / buffer / cache / RasterCache.java @ 11083

History | View | Annotate | Download (24.7 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

    
20
package org.gvsig.raster.buffer.cache;
21

    
22
import java.io.File;
23
import java.io.FileNotFoundException;
24
import java.io.IOException;
25

    
26
import org.gvsig.raster.buffer.IBand;
27
import org.gvsig.raster.buffer.RasterBand;
28
import org.gvsig.raster.buffer.RasterBuffer;
29
import org.gvsig.raster.dataset.IBuffer;
30
import org.gvsig.raster.util.RasterUtilities;
31

    
32
/*
33
 * TODO: OPTIMIZACION: Trabajo de optimizaci?n en la velocidad e acceso a cach?.
34
 * TODO: FUNCIONALIDAD: Acabar de implementar los m?todos de acceso a datos, intercambio de bandas, etc...
35
 */
36
/**
37
 * Esta clase representa un buffer de datos cacheado. El estar en cache significa que
38
 * solo una parte de los datos estar?n en memoria y que gestiona, dependiendo de las
39
 * peticiones, que partes hay que cargar a memoria y en que momento. Para esto el buffer
40
 * no se llena de golpe sino que utiliza una clase que sirve los datos desde la fuente. 
41
 * Esta clase servidora de datos debe implementar el interfaz ICacheDatasetSourcepara 
42
 * servir datos de la forma requerida.
43
 *   
44
 * @author Nacho Brodin (nachobrodin@gmail.com)
45
 *
46
 */
47
public class RasterCache extends RasterBuffer {
48

    
49
        private Cache                         cache = null;
50
        private LRUAlgorithm          lru = null;
51
                
52
        //TODO: FUNCIONALIDAD: Intercambio de bandas para el buffer cacheado
53
        
54
         public class CacheBand extends RasterBand{
55
                 public ICacheDataSource[] cacheDataServer = null;
56
                 
57
                 public CacheBand(int height, int width){
58
                         super(height, width);
59
                 }
60
                 
61
                 public Object getLine(int line) {
62
                         return null;
63
                 }
64
                 
65
                 public void setLine(int line, Object value){
66
                        
67
                 }
68
                 
69
                 public Object getBuf(){
70
                         return null;
71
                 }
72
                 
73
                 public void setFileName(int numBand){
74
                        for (int i = 0; i < cacheDataServer.length; i++) 
75
                                ((CacheDataServer)cacheDataServer[i]).setName(null, numBand, i);
76
                 }
77
        }
78
         
79
    /**
80
     * Constructor. Asigna las variables de inicializaci?n y crea la estructura de 
81
     * la cach? con los datos pasados.
82
     * @param dataType Tipo de dato del buffer
83
     * @param width Ancho del buffer
84
     * @param height Alto del buffer
85
     * @param nBands N?mero de bandas del buffer
86
     */
87
        public RasterCache(int dataType, int width, int height, int nBands){
88
                cache = new Cache(nBands, dataType, width, height);
89
                lru = new LRUAlgorithm(cache);
90
                
91
            this.dataType = dataType;
92
        this.width = width;
93
        this.height = height;
94
        this.nBands = nBands;
95
        }
96
        
97
        /*
98
     * (non-Javadoc)
99
     * @see org.gvsig.raster.driver.IBuffer#isBandSwitchable()
100
     */
101
    public boolean isBandSwitchable(){
102
            return true;
103
    }
104
    
105
        public void malloc(int dataType, int width, int height, int bandNr) {
106
        }
107

    
108
        /**
109
         * Obtiene la cach?
110
         * @return
111
         */
112
        public Cache getCache() {
113
                return cache;
114
        }
115
        
116
        /**
117
         * Asigna la cache
118
         * @param cache
119
         */
120
        public void setCache(Cache cache) {
121
                this.cache = cache;
122
                this.lru.setCache(cache);
123
        }
124
        
125
        //*********************************************************
126
        
127
        public byte[][] getLineByte(int line) {
128
                try {
129
                        lru.cacheAccess(line, true);
130
                } catch (InvalidPageNumberException e) {return null;}
131
                return cache.getAccessPage().getLineByte((line & cache.getOffset()));
132
        }
133

    
134
        public short[][] getLineShort(int line) {
135
                try {
136
                        lru.cacheAccess(line, true);
137
                } catch (InvalidPageNumberException e) {return null;}
138
                return cache.getAccessPage().getLineShort((line & cache.getOffset()));
139
        }
140

    
141
        public int[][] getLineInt(int line) {
142
                try {
143
                        lru.cacheAccess(line, true);
144
                } catch (InvalidPageNumberException e) {return null;}
145
                return cache.getAccessPage().getLineInt((line & cache.getOffset()));
146
        }
147

    
148
        public float[][] getLineFloat(int line) {
149
                try {
150
                        lru.cacheAccess(line, true);
151
                } catch (InvalidPageNumberException e) {return null;}
152
                return cache.getAccessPage().getLineFloat((line & cache.getOffset()));
153
        }
154

    
155
        public double[][] getLineDouble(int line) {
156
                try {
157
                        lru.cacheAccess(line, true);
158
                } catch (InvalidPageNumberException e) {return null;}
159
                return cache.getAccessPage().getLineDouble((line & cache.getOffset()));
160
        }
161

    
162
        //*********************************************************
163
        
164
        public void setLineByte(byte[][] data, int line) {
165
                try {
166
                        lru.cacheAccess(line, false);
167
                } catch (InvalidPageNumberException e) {return;}
168
                cache.getAccessPage().setLineByte(data, (line & cache.getOffset()));
169
        }
170

    
171
        public void setLineShort(short[][] data, int line) {
172
                try {
173
                        lru.cacheAccess(line, false);
174
                } catch (InvalidPageNumberException e) {return;}
175
                cache.getAccessPage().setLineShort(data, (line & cache.getOffset()));
176
        }
177

    
178
        public void setLineInt(int[][] data, int line) {
179
                try {
180
                        lru.cacheAccess(line, false);
181
                } catch (InvalidPageNumberException e) {return;}
182
                cache.getAccessPage().setLineInt(data, (line & cache.getOffset()));
183
        }
184

    
185
        public void setLineFloat(float[][] data, int line) {
186
                try {
187
                        lru.cacheAccess(line, false);
188
                } catch (InvalidPageNumberException e) {return;}
189
                cache.getAccessPage().setLineFloat(data, (line & cache.getOffset()));
190
        }
191

    
192
        public void setLineDouble(double[][] data, int line) {
193
                try {
194
                        lru.cacheAccess(line, false);
195
                } catch (InvalidPageNumberException e) {return;}
196
                cache.getAccessPage().setLineDouble(data, (line & cache.getOffset()));
197
        }
198

    
199
        //*********************************************************
200
        
201
        public byte[] getLineFromBandByte(int line, int band) {
202
                try {
203
                        lru.cacheAccess(line, true);
204
                } catch (InvalidPageNumberException e) {return null;}
205
                return cache.getAccessPage().getLineFromBandByte((line & cache.getOffset()), band);
206
        }
207

    
208
        public short[] getLineFromBandShort(int line, int band) {
209
                try {
210
                        lru.cacheAccess(line, true);
211
                } catch (InvalidPageNumberException e) {return null;}
212
                return cache.getAccessPage().getLineFromBandShort((line & cache.getOffset()), band);
213
        }
214

    
215
        public int[] getLineFromBandInt(int line, int band) {
216
                try {
217
                        lru.cacheAccess(line, true);
218
                } catch (InvalidPageNumberException e) {return null;}
219
                return cache.getAccessPage().getLineFromBandInt((line & cache.getOffset()), band);
220
        }
221

    
222
        public float[] getLineFromBandFloat(int line, int band) {
223
                try {
224
                        lru.cacheAccess(line, true);
225
                } catch (InvalidPageNumberException e) {return null;}
226
                return cache.getAccessPage().getLineFromBandFloat((line & cache.getOffset()), band);
227
        }
228

    
229
        public double[] getLineFromBandDouble(int line, int band) {
230
                try {
231
                        lru.cacheAccess(line, true);
232
                } catch (InvalidPageNumberException e) {return null;}
233
                return cache.getAccessPage().getLineFromBandDouble((line & cache.getOffset()), band);
234
        }
235

    
236
        //*********************************************************
237
        
238
        public void setLineInBandByte(byte[] data, int line, int band) {
239
                try {
240
                        lru.cacheAccess(line, false);
241
                } catch (InvalidPageNumberException e) {return;}
242
                cache.getAccessPage().setLineInBandByte(data, (line & cache.getOffset()), band);
243
        }
244

    
245
        public void setLineInBandShort(short[] data, int line, int band) {
246
                try {
247
                        lru.cacheAccess(line, false);
248
                } catch (InvalidPageNumberException e) {return;}
249
                cache.getAccessPage().setLineInBandShort(data, (line & cache.getOffset()), band);
250
        }
251

    
252
        public void setLineInBandInt(int[] data, int line, int band) {
253
                try {
254
                        lru.cacheAccess(line, false);
255
                } catch (InvalidPageNumberException e) {return;}
256
                cache.getAccessPage().setLineInBandInt(data, (line & cache.getOffset()), band);
257
        }
258

    
259
        public void setLineInBandFloat(float[] data, int line, int band) {
260
                try {
261
                        lru.cacheAccess(line, false);
262
                } catch (InvalidPageNumberException e) {return;}
263
                cache.getAccessPage().setLineInBandFloat(data, (line & cache.getOffset()), band);
264
        }
265

    
266
        public void setLineInBandDouble(double[] data, int line, int band) {
267
                try {
268
                        lru.cacheAccess(line, false);
269
                } catch (InvalidPageNumberException e) {return;}
270
                cache.getAccessPage().setLineInBandDouble(data, (line & cache.getOffset()), band);
271
        }
272

    
273
        //*********************************************************
274
        
275
        public byte getElemByte(int line, int col, int band) {
276
                try {
277
                        lru.cacheAccess(line, true);
278
                } catch (InvalidPageNumberException e) {
279
                        return (byte)getNoDataValue(); //No leemos el dato
280
                }
281
                return cache.getAccessPage().getElemByte((line & cache.getOffset()), col, band);
282
        }
283

    
284
        public short getElemShort(int line, int col, int band) {
285
                try {
286
                        lru.cacheAccess(line, true);
287
                } catch (InvalidPageNumberException e) {
288
                        return (short)getNoDataValue(); //No leemos el dato
289
                }
290
                return cache.getAccessPage().getElemShort((line & cache.getOffset()), col, band);
291
        }
292

    
293
        public int getElemInt(int line, int col, int band) {
294
                try {
295
                        lru.cacheAccess(line, true);
296
                } catch (InvalidPageNumberException e) {
297
                        return (int)getNoDataValue(); //No leemos el dato
298
                }
299
                return cache.getAccessPage().getElemInt((line & cache.getOffset()), col, band);
300
        }
301

    
302
        public float getElemFloat(int line, int col, int band) {
303
                try {
304
                        lru.cacheAccess(line, true);
305
                } catch (InvalidPageNumberException e) {
306
                        return (float)getNoDataValue(); //No leemos el dato
307
                }
308
                return cache.getAccessPage().getElemFloat((line & cache.getOffset()), col, band);
309
        }
310

    
311
        public double getElemDouble(int line, int col, int band) {
312
                try {
313
                        lru.cacheAccess(line, true);
314
                } catch (InvalidPageNumberException e) {
315
                        return (double)getNoDataValue(); //No leemos el dato
316
                }
317
                return cache.getAccessPage().getElemDouble((line & cache.getOffset()), col, band);
318
        }
319

    
320
        //*********************************************************
321
        
322
        public void setElem(int line, int col, int band, byte data) {
323
                try {
324
                        lru.cacheAccess(line, false);
325
                } catch (InvalidPageNumberException e) { return;}
326
                cache.getAccessPage().setElem((line & cache.getOffset()), col, band, data);
327
        }
328

    
329
        public void setElem(int line, int col, int band, short data) {
330
                try {
331
                        lru.cacheAccess(line, false);
332
                } catch (InvalidPageNumberException e) {return;}
333
                cache.getAccessPage().setElem((line & cache.getOffset()), col, band, data);
334
        }
335

    
336
        public void setElem(int line, int col, int band, int data) {
337
                try {
338
                        lru.cacheAccess(line, false);
339
                } catch (InvalidPageNumberException e) {return;}
340
                cache.getAccessPage().setElem((line & cache.getOffset()), col, band, data);
341
        }
342

    
343
        public void setElem(int line, int col, int band, float data) {
344
                try {
345
                        lru.cacheAccess(line, false);
346
                } catch (InvalidPageNumberException e) {return;}
347
                cache.getAccessPage().setElem((line & cache.getOffset()), col, band, data);
348
        }
349

    
350
        public void setElem(int line, int col, int band, double data) {
351
                try {
352
                        lru.cacheAccess(line, false);
353
                } catch (InvalidPageNumberException e) {return;}
354
                cache.getAccessPage().setElem((line & cache.getOffset()), col, band, data);
355
        }
356
        
357
        //*********************************************************
358

    
359
        public void getElemByte(int line, int col, byte[] data) {
360
                try {
361
                        lru.cacheAccess(line, true);
362
                } catch (InvalidPageNumberException e) {
363
                        for (int iBand = 0; iBand < data.length; iBand++)
364
                    data[iBand] = (byte)getNoDataValue();
365
                }
366
                cache.getAccessPage().getElemByte((line & cache.getOffset()), col, data);
367
        }
368

    
369
        public void getElemShort(int line, int col, short[] data) {
370
                try {
371
                        lru.cacheAccess(line, true);
372
                } catch (InvalidPageNumberException e) {
373
                        for (int iBand = 0; iBand < data.length; iBand++)
374
                    data[iBand] = (short)getNoDataValue();
375
                }
376
                cache.getAccessPage().getElemShort((line & cache.getOffset()), col, data);
377
        }
378

    
379
        public void getElemInt(int line, int col, int[] data) {
380
                try {
381
                        lru.cacheAccess(line, true);
382
                } catch (InvalidPageNumberException e) {
383
                        for (int iBand = 0; iBand < data.length; iBand++)
384
                    data[iBand] = (int)getNoDataValue();
385
                }
386
                cache.getAccessPage().getElemInt((line & cache.getOffset()), col, data);
387
        }
388

    
389
        public void getElemFloat(int line, int col, float[] data) {
390
                try {
391
                        lru.cacheAccess(line, true);
392
                } catch (InvalidPageNumberException e) {
393
                        for (int iBand = 0; iBand < data.length; iBand++)
394
                    data[iBand] = (float)getNoDataValue();
395
                }
396
                cache.getAccessPage().getElemFloat((line & cache.getOffset()), col, data);
397
        }
398

    
399
        public void getElemDouble(int line, int col, double[] data) {
400
                try {
401
                        lru.cacheAccess(line, true);
402
                } catch (InvalidPageNumberException e) {
403
                        for (int iBand = 0; iBand < data.length; iBand++)
404
                    data[iBand] = (double)getNoDataValue();
405
                }
406
                cache.getAccessPage().getElemDouble((line & cache.getOffset()), col, data);
407
        }
408
        
409
        //*********************************************************
410
        
411
        public void setElemByte(int line, int col, byte[] data) {
412
                try {
413
                        lru.cacheAccess(line, false);
414
                } catch (InvalidPageNumberException e) {return;}
415
                cache.getAccessPage().setElemByte((line & cache.getOffset()), col, data);
416
        }
417

    
418
        public void setElemShort(int line, int col, short[] data) {
419
                try {
420
                        lru.cacheAccess(line, false);
421
                } catch (InvalidPageNumberException e) {return;}
422
                cache.getAccessPage().setElemShort((line & cache.getOffset()), col, data);
423
        }
424

    
425
        public void setElemInt(int line, int col, int[] data) {
426
                try {
427
                        lru.cacheAccess(line, false);
428
                } catch (InvalidPageNumberException e) {return;}
429
                cache.getAccessPage().setElemInt((line & cache.getOffset()), col, data);
430
        }
431

    
432
        public void setElemFloat(int line, int col, float[] data) {
433
                try {
434
                        lru.cacheAccess(line, false);
435
                } catch (InvalidPageNumberException e) {return;}
436
                cache.getAccessPage().setElemFloat((line & cache.getOffset()), col, data);
437
        }
438

    
439
        public void setElemDouble(int line, int col, double[] data) {
440
                try {
441
                        lru.cacheAccess(line, false);
442
                } catch (InvalidPageNumberException e) {return;}
443
                cache.getAccessPage().setElemDouble((line & cache.getOffset()), col, data);
444
        }
445

    
446
    /*
447
     *  (non-Javadoc)
448
     * @see org.gvsig.fmap.driver.IBuffer#getBandBuffer(int)
449
     */
450
    public IBuffer getBandBuffer(int iBand){
451
            RasterCache rasterCache = new RasterCache(getDataType(), getWidth(), getHeight(), 1);
452
            CacheStruct cs = new CacheStruct();
453
            cs.setHPag(cache.getCacheStruct().getHPag());
454
            cs.setOffset(cache.getCacheStruct().getOffset());
455
            cs.setNPags(cache.getCacheStruct().getNPags());
456
            cs.setNBands(1);
457
            cs.setNGroups(cache.getCacheStruct().getNGroups());
458
            cs.setBitsPag(cache.getCacheStruct().getBitsPag());
459
            cs.setNTotalPags((int)(getHeight() / cache.getCacheStruct().getHPag()));
460
            cs.setDataType(cache.getCacheStruct().getDataType());
461
            
462
            Cache c = new Cache(cs, getWidth());
463
            rasterCache.setCache(c);
464
            
465
            IBand band = getBand(iBand);
466
            rasterCache.assignBand(0, band);
467
            return rasterCache;
468
    }
469
    
470
    /*
471
     *  (non-Javadoc)
472
     * @see org.gvsig.fmap.driver.IBuffer#replicateBand(int, int)
473
     */
474
        public void replicateBand(int orig, int dest) {
475
        }
476
                
477
        /*
478
     *  (non-Javadoc)
479
     * @see org.gvsig.fmap.dataaccess.buffer.RasterBuffer#switchBands(int[])
480
     */
481
    public void switchBands(int[] bandPosition){
482
            
483
    }
484

    
485
        //*********************************************************
486
        
487
        public RasterBuffer adjustRasterNearestNeighbourInterpolation(int w, int h, int[] bands) {
488
                return null;
489
        }
490

    
491
        public RasterBuffer adjustRasterBilinearInterpolation(int w, int h, int[] bands) {
492
                return null;
493
        }
494

    
495
        public RasterBuffer adjustRasterInverseDistanceInterpolation(int w, int h, int[] bands) {
496
                return null;
497
        }
498

    
499
        public RasterBuffer adjustRasterBicubicSplineInterpolation(int w, int h, int[] bands) {
500
                return null;
501
        }
502

    
503
        public RasterBuffer adjustRasterBSplineInterpolation(int w, int h, int[] bands) {
504
                return null;
505
        }
506

    
507
        //*********************************************************
508
        
509
        public void assign(int band, byte value) {
510
                for(int line = 0; line < height; line ++){
511
                        boolean beginLine = true;  //Para acelerar solo comprobar? si la p?gina est? en cach? cada vez que empieza una l?nea
512
                    for(int col = 0; col < width; col ++){
513
                            try {
514
                                    if(beginLine){
515
                                            lru.cacheAccess(line, false);
516
                                            beginLine = false;
517
                                    }
518
                            } catch (InvalidPageNumberException e) {return;}
519
                            cache.getAccessPage().setElem((line & cache.getOffset()), col, band, value);                
520
                    }
521
                }
522
        }
523

    
524
        public void assign(int band, short value) {
525
                for(int line = 0; line < height; line ++){
526
                        boolean beginLine = true;  //Para acelerar solo comprobar? si la p?gina est? en cach? cada vez que empieza una l?nea
527
                    for(int col = 0; col < width; col ++){
528
                            try {
529
                                    if(beginLine){
530
                                            lru.cacheAccess(line, false);
531
                                            beginLine = false;
532
                                    }
533
                            } catch (InvalidPageNumberException e) {return;}
534
                            cache.getAccessPage().setElem((line & cache.getOffset()), col, band, value);                
535
                    }
536
                }                
537
        }
538

    
539
        public void assign(int band, int value) {
540
                for(int line = 0; line < height; line ++){
541
                        boolean beginLine = true;  //Para acelerar solo comprobar? si la p?gina est? en cach? cada vez que empieza una l?nea
542
                    for(int col = 0; col < width; col ++){
543
                            try {
544
                                    if(beginLine){
545
                                            lru.cacheAccess(line, false);
546
                                            beginLine = false;
547
                                    }
548
                            } catch (InvalidPageNumberException e) {return;}
549
                            cache.getAccessPage().setElem((line & cache.getOffset()), col, band, value);                
550
                    }
551
                }        
552
        }
553

    
554
        public void assign(int band, float value) {
555
                for(int line = 0; line < height; line ++){
556
                        boolean beginLine = true;  //Para acelerar solo comprobar? si la p?gina est? en cach? cada vez que empieza una l?nea
557
                    for(int col = 0; col < width; col ++){
558
                            try {
559
                                    if(beginLine){
560
                                            lru.cacheAccess(line, false);
561
                                            beginLine = false;
562
                                    }
563
                            } catch (InvalidPageNumberException e) {return;}
564
                            cache.getAccessPage().setElem((line & cache.getOffset()), col, band, value);                
565
                    }
566
                }        
567
        }
568

    
569
        public void assign(int band, double value) {
570
                for(int line = 0; line < height; line ++){
571
                        boolean beginLine = true;  //Para acelerar solo comprobar? si la p?gina est? en cach? cada vez que empieza una l?nea
572
                    for(int col = 0; col < width; col ++){
573
                            try {
574
                                    if(beginLine){
575
                                            lru.cacheAccess(line, false);
576
                                            beginLine = false;
577
                                    }
578
                            } catch (InvalidPageNumberException e) {return;}
579
                            cache.getAccessPage().setElem((line & cache.getOffset()), col, band, value);                
580
                    }
581
                }
582
        }
583
    
584
    /*
585
     *  (non-Javadoc)
586
     * @see org.gvsig.fmap.driver.IBuffer#cloneBuffer()
587
     */
588
    public IBuffer cloneBuffer(){
589
            return null;
590
    }
591
    
592
    /*
593
     * (non-Javadoc)
594
     * @see org.gvsig.fmap.driver.IBuffer#interchangeBands(int, int)
595
     */
596
        public void interchangeBands(int band1, int band2) {
597
                IBand b1 = getBand(band1);
598
                IBand b2 = getBand(band2);
599
                
600
                try {
601
                        cache.assignBand(band2, ((CacheBand)b1).cacheDataServer);
602
                        cache.assignBand(band1, ((CacheBand)b2).cacheDataServer);
603
                } catch (IOException e) {
604
                        e.printStackTrace();
605
                }
606
                
607
        }
608
            
609
    /*
610
     *  (non-Javadoc)
611
     * @see org.gvsig.fmap.driver.IBuffer#mallocOneBand(int, int, int, int)
612
     */
613
    public void mallocOneBand(int dataType, int width, int height, int band) {
614
                        
615
        }
616

    
617
    /*
618
         * (non-Javadoc)
619
         * @see org.gvsig.raster.driver.IBuffer#getBand(int)
620
         */
621
    public IBand getBand(int band) {
622
            CacheBand cb = new CacheBand(getHeight(), getWidth());
623
            cb.cacheDataServer = new CacheDataServer[cache.getNTotalPags()];
624
            
625
            try {
626
                        cache.resetCache();
627
                } catch (IOException e) {
628
                        //TODO: EXCEPCIONES: Modificar el manejo de excepciones de RasterBuffer para q lance las apropiadas con cach?
629
                        return null;
630
                }
631
            for (int iPage = 0; iPage < cache.getNTotalPags(); iPage++)
632
                    cb.cacheDataServer[iPage] = cache.getHddPage(iPage, band);        
633
                    
634
            return cb;
635
    }
636
    
637
    /*
638
     *  (non-Javadoc)
639
     * @see org.gvsig.fmap.driver.IBuffer#copyBand(int, org.gvsig.fmap.dataaccess.buffer.IBand)
640
     */
641
        public void copyBand(int nBand, IBand band) {
642
                if(band instanceof CacheBand){
643
                        CacheBand cb = new CacheBand(band.getHeight(), band.getWidth());
644
                        cb.cacheDataServer = new CacheDataServer[((CacheBand)band).cacheDataServer.length];
645
                        
646
                        for (int iPage = 0; iPage < cb.cacheDataServer.length; iPage++) {
647
                                cb.cacheDataServer[iPage] = new CacheDataServer(null, nBand, iPage);
648
                                String path = ((CacheBand)band).cacheDataServer[iPage].getPath();
649
                                File f = new File(path);
650
                                if(f.exists()){
651
                                        try {
652
                                                RasterUtilities.copyFile(path, cb.cacheDataServer[iPage].getPath());
653
                                        } catch (FileNotFoundException e) {
654
                                                //TODO: EXCEPCIONES: Modificar el manejo de excepciones de RasterBuffer para q lance las apropiadas con cach?
655
                                                e.printStackTrace();
656
                                        } catch (IOException e) {
657
                                                e.printStackTrace();
658
                                        }
659
                                }
660
                        }
661
                        try {
662
                                cache.deleteBand(nBand);
663
                                cache.assignBand(nBand, cb.cacheDataServer);
664
                        } catch (IOException e) {
665
                                e.printStackTrace();
666
                        }
667
                                                
668
                }                
669
        }
670

    
671
        /*
672
         *  (non-Javadoc)
673
         * @see org.gvsig.fmap.driver.IBuffer#assignBand(int, org.gvsig.fmap.dataaccess.buffer.IBand)
674
         */
675
        public void assignBand(int nBand, IBand band) {
676
                if(band instanceof CacheBand) {
677
                        //((CacheBand)band).setFileName(nBand);
678
                        try {
679
                                cache.deleteBand(nBand);
680
                                cache.assignBand(nBand, ((CacheBand)band).cacheDataServer);
681
                        } catch (IOException e) {
682
                                //TODO: EXCEPCIONES: Modificar el manejo de excepciones de RasterBuffer para q lance las apropiadas con cach?
683
                                return;
684
                        }
685
                        
686
                }
687
        }
688

    
689
        /*
690
         *  (non-Javadoc)
691
         * @see org.gvsig.fmap.driver.IBuffer#createBand(byte)
692
         */
693
        public IBand createBand(byte defaultValue) {
694
                PageBandBuffer pageBuffer = new PageBandBuffer(getDataType(), getWidth(), cache.getHPag(), 1, true, 0);
695
                IBand band = null;
696
                try {
697
                        band = createBand(pageBuffer);
698
                } catch (IOException e) {
699
                        return null;
700
                }
701
                loadPage(new Byte(defaultValue), pageBuffer);
702
                return band;
703
        }
704
                
705
        /*
706
         *  (non-Javadoc)
707
         * @see org.gvsig.fmap.driver.IBuffer#assignBandToNotValid(int)
708
         */
709
        public void assignBandToNotValid(int iBand) {
710
                PageBandBuffer pageBuffer = new PageBandBuffer(getDataType(), getWidth(), cache.getHPag(), 1, true, 0);
711

    
712
                switch(getDataType()){
713
            case IBuffer.TYPE_BYTE:         loadPage(new Byte((byte)getNotValidValue()), pageBuffer);break;
714
            case IBuffer.TYPE_SHORT:        loadPage(new Short((short)getNotValidValue()), pageBuffer);break;
715
            case IBuffer.TYPE_INT:                loadPage(new Integer((int)getNotValidValue()), pageBuffer);break;
716
            case IBuffer.TYPE_FLOAT:        loadPage(new Float((float)getNotValidValue()), pageBuffer);break;
717
            case IBuffer.TYPE_DOUBLE:        loadPage(new Double((double)getNotValidValue()), pageBuffer);break;
718
            }        
719
                
720
                try {
721
                        CacheBand cb = (CacheBand)createBand(pageBuffer);
722
                        cb.setFileName(iBand);
723
                        assignBand(iBand, cb);
724
                } catch (IOException e) {
725
                        //TODO: EXCEPCIONES: Modificar el manejo de excepciones de RasterBuffer para q lance las apropiadas con cach?
726
                        e.printStackTrace();
727
                }
728
        }
729

    
730
        /**
731
         * Creaci?n de una banda a partir de una pagina de cache cargada con datos.
732
         * @param pageBuffer Pagina de cache cargada de datos
733
         * @param numBand N?mero de banda a la que representa
734
         * @return IBand
735
         * @throws IOException
736
         */
737
        private IBand createBand(PageBandBuffer pageBuffer) throws IOException{
738
                CacheDataServer[] ds = new CacheDataServer[cache.getNTotalPags()];
739
                for (int i = 0; i < cache.getNTotalPags(); i++) {
740
                        ds[i] = new CacheDataServer(null, 0, i);
741
                        ds[i].savePage(pageBuffer);
742
                }
743
                CacheBand band = new CacheBand(getHeight(), getWidth());
744
                band.cacheDataServer = ds;
745
                return band;
746
        }
747
        
748
        /**
749
         * Carga la p?gina con el valor pasado por par?metro. El valor ser? del mismo tipo de dato 
750
         * que el buffer actual.
751
         * @param value Valor a inicializar
752
         * @param pageBuffer P?gina
753
         */
754
        private void loadPage(Object value, PageBandBuffer pageBuffer){
755
                switch(getDataType()){
756
            case IBuffer.TYPE_BYTE: for(int i = 0 ; i < getWidth(); i ++)
757
                                                                    for(int j = 0 ; j < cache.getHPag(); j ++)
758
                                                                            pageBuffer.setElem(j, i, 0, ((Byte)value).byteValue());
759
                                                            break;
760
            case IBuffer.TYPE_SHORT: for(int i = 0 ; i < getWidth(); i ++)
761
                                                                        for(int j = 0 ; j < cache.getHPag(); j ++)
762
                                                                                pageBuffer.setElem(j, i, 0, ((Short)value).shortValue());
763
                                                                 break;
764
            case IBuffer.TYPE_INT:        for(int i = 0 ; i < getWidth(); i ++)
765
                                                                        for(int j = 0 ; j < cache.getHPag(); j ++)
766
                                                                                pageBuffer.setElem(j, i, 0, ((Integer)value).intValue());
767
                                                                break;
768
            case IBuffer.TYPE_FLOAT:for(int i = 0 ; i < getWidth(); i ++)
769
                                                                        for(int j = 0 ; j < cache.getHPag(); j ++)
770
                                                                                pageBuffer.setElem(j, i, 0, ((Float)value).floatValue());
771
                                                                        break;
772
            case IBuffer.TYPE_DOUBLE:for(int i = 0 ; i < getWidth(); i ++)
773
                                                                        for(int j = 0 ; j < cache.getHPag(); j ++)
774
                                                                                pageBuffer.setElem(j, i, 0, ((Double)value).doubleValue());
775
                                                                 break;
776
            }        
777
        }
778
}