Statistics
| Revision:

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

History | View | Annotate | Download (24.9 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.DataClassList;
31
import org.gvsig.raster.util.Histogram;
32
import org.gvsig.raster.util.HistogramException;
33
import org.gvsig.raster.util.RasterUtilities;
34

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
488
        //*********************************************************
489
        
490
        public RasterBuffer adjustRasterNearestNeighbourInterpolation(int w, int h, int[] bands) {
491
                return null;
492
        }
493

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

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

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

    
506
        public RasterBuffer adjustRasterBSplineInterpolation(int w, int h, int[] bands) {
507
                return null;
508
        }
509

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

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

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

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

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

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

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

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

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

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

    
782
        public int getPercent() {
783
                return super.percent;
784
        }
785
}