Statistics
| Revision:

root / trunk / libraries / libRaster / tmp / cache / CacheDataServer.java @ 10740

History | View | Annotate | Download (14.2 KB)

1

    
2
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
3
 *
4
 * Copyright (C) 2006 IVER T.I. and Generalitat Valenciana.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
19
 */
20
package org.gvsig.raster.dataaccess.cache;
21

    
22
import java.io.BufferedInputStream;
23
import java.io.BufferedOutputStream;
24
import java.io.DataInputStream;
25
import java.io.DataOutputStream;
26
import java.io.File;
27
import java.io.FileInputStream;
28
import java.io.FileNotFoundException;
29
import java.io.FileOutputStream;
30
import java.io.IOException;
31

    
32
import org.gvsig.raster.driver.Band;
33
import org.gvsig.raster.driver.BandFoundInListException;
34
import org.gvsig.raster.driver.BandList;
35
import org.gvsig.raster.driver.GeoRasterWriter;
36
import org.gvsig.raster.driver.IBuffer;
37
import org.gvsig.raster.driver.IDataWriter;
38
import org.gvsig.raster.driver.NotSupportedExtensionException;
39
import org.gvsig.raster.driver.RasterDataset;
40
import org.gvsig.raster.driver.RasterDriverException;
41
import org.gvsig.raster.shared.Extent;
42
import org.gvsig.raster.shared.RasterLibrary;
43

    
44

    
45
/** 
46
 * Servidor de datos de cach?. Esta clase es la encargada de volcar a disco las p?ginas de cach? y recuperar
47
 * las cuando le son solicitadas. Por ello debe implementar el interfaz ICacheDataSource con dos m?todos
48
 * loadPage y savePage. Ambos a partir del n?mero de p?gina recuperaran o salvaran de/a disco una p?gina. 
49
 * 
50
 * @author Nacho Brodin (brodin_ign@gva.es)
51
 *
52
 */
53
public class CacheDataServer implements ICacheDataSource{
54
        
55
        /**
56
         * Directorio temporal para la cach?. Si gastamos el mismo que andami este se ocupar? de gestionar su
57
         * destrucci?n al cerrar gvSIG.
58
         */
59
        private String                                 tempDirectoryPath = RasterLibrary.tempCacheDirectoryPath;
60
        private String                                 id = null; //Identificador de fichero.
61
         
62
        /**
63
         * Constructor. 
64
         * Crea el identificador para todos los trozos de cach? que se guardar?n en disco. 
65
         *
66
         */
67
        public CacheDataServer() {
68
                if(id == null)
69
                        id = Long.toString(System.currentTimeMillis());
70
        }
71

    
72
        /* (non-Javadoc)
73
         * @see org.gvsig.fmap.dataaccess.cache.ICacheDataSource#loadPage(int, org.gvsig.fmap.dataaccess.cache.PageBuffer)
74
         */
75
        public void loadPage(int nPag, PageBuffer pageBuffer) {
76
                String inFileName =  tempDirectoryPath + File.separator + id + "-" + nPag;
77
                                
78
                File file = new File(inFileName);
79
                if(!file.exists())
80
                        return;
81
        
82
                DataInputStream dis;
83
                try {
84
                        dis = new DataInputStream( new BufferedInputStream(new FileInputStream(file)));        
85
                        read(dis, pageBuffer);
86
                dis.close();
87
                }catch (FileNotFoundException e) {
88
                        return;
89
                }catch(IOException ex){
90
                return;
91
        } 
92
        }
93

    
94
        /* (non-Javadoc)
95
         * @see org.gvsig.fmap.dataaccess.cache.ICacheDataSource#savePage(int, org.gvsig.fmap.dataaccess.cache.PageBuffer)
96
         */
97
        public void savePage(int nPag, PageBuffer pageBuffer) throws IOException {
98
                createTempDirectory();
99
                String fileName =  tempDirectoryPath + File.separator + id + "-" + nPag;
100
                
101
                File f = new File(fileName);
102
                DataOutputStream dos = new DataOutputStream( new BufferedOutputStream(new FileOutputStream(f)));
103
                save(dos, pageBuffer);
104
        dos.close();
105
        }
106
        
107
        
108
        
109
        /**
110
         * Esta funci?n crea el directorio para temporales y devuelve el nombre de este.
111
         * @return
112
         */
113
        private String createTempDirectory(){
114
                File tempDirectory = new File(tempDirectoryPath);
115
            if (!tempDirectory.exists())
116
                    tempDirectory.mkdir();
117
            return tempDirectoryPath;
118
        }
119
        
120
        //TODO: TEST: Implementar y probar el cacheado para tipos de datos != de byte
121
        /**
122
         * Tilea un raster en disco para que sea accesible por la cach?.
123
         * @param fileName Nombre del fichero a tilear
124
         * @param pageLines N?mero de l?neas de cada tile
125
         * @throws IOException
126
         * @throws RasterDriverException 
127
         * @throws NotSupportedExtensionException 
128
         */
129
        public void cachear(String fileName, int pageLines)throws IOException, NotSupportedExtensionException, RasterDriverException{
130
                if(id == null)
131
                        id = Long.toString(System.currentTimeMillis());
132
                
133
                RasterDataset grf = RasterDataset.openFile(null, fileName);
134
                
135
                //Creamos un BandList con todas las bandas del fichero
136
                BandList bandList = new BandList();
137
                for(int i = 0; i < grf.getBandCount();i++){
138
                        try{
139
                                Band band = new Band(grf.getFName(), i, grf.getDataType());
140
                                bandList.addBand(band, i);
141
                                bandList.addDrawableBand(i, i);
142
                        }catch(BandFoundInListException ex){
143
                                //No a?adimos la banda
144
                        }
145
                }
146

    
147
                int pages = (int)Math.ceil(grf.getHeight() / pageLines);
148
        
149
                createTempDirectory();
150
                
151
                PageBuffer pageBuffer = new PageBuffer(grf.getDataType(), grf.getWidth(), pageLines, grf.getBandCount(), true);
152
                int y = 0;
153
                for(int i = 0; i < pages; i++){
154
                        grf.getWindowRaster(0, y, grf.getWidth(), pageLines, bandList, pageBuffer);
155
                        String outFileName =  tempDirectoryPath + File.separator + id + "-" + i;
156
                                                
157
                        File f = new File(outFileName);
158
                        DataOutputStream dos = new DataOutputStream( new BufferedOutputStream(new FileOutputStream(f)));
159
                        save(dos, pageBuffer);
160
            dos.close();
161
                        y += pageLines;
162
                }
163
                
164
                //Solo para pruebas
165
                //convertFromByteFileToTif(grf, pageBuffer, pageLines);
166
        }
167
        
168
        /**
169
         * Funci?n para pruebas.
170
         * Convierte los ficheros generados por la funci?n cachear en ficheros tif para comprobar que est?n
171
         * bien generados.
172
         * @param grf
173
         * @param pageBuffer
174
         * @param pageLines
175
         * @throws IOException
176
         */
177
        private void convertFromByteFileToTif(RasterDataset grf, PageBuffer pageBuffer, int pageLines)throws IOException{
178
                int w = pageBuffer.getWidth();
179
                int h = pageBuffer.getHeight();
180
                int pages = (int)Math.ceil(grf.getHeight() / pageLines);
181
                
182
                GeoRasterWriter grw = null;
183
                PageBuffer pageBuffer2 = new PageBuffer(grf.getDataType(), grf.getWidth(), pageLines, grf.getBandCount(), true);
184
                for(int i = 0; i < pages; i++){
185
                        String inFileName =  tempDirectoryPath + File.separator + id + "-" + i;
186
                        File f = new File(inFileName);
187
                        DataInputStream dis = new DataInputStream( new BufferedInputStream(new FileInputStream(f)));
188
                        byte[] b;
189
            switch(pageBuffer.getDataType()){
190
            case 0:        b = new byte[pageBuffer.getWidth() * pageBuffer.getHeight() * pageBuffer.getBandCount()];
191
                            dis.readFully(b);
192
                            for(int iBand = 0; iBand < pageBuffer.getBandCount(); iBand ++){
193
                                            for(int line = 0; line < pageBuffer.getHeight(); line ++){
194
                                                            byte[] linea = new byte[w];
195
                                                            for(int d = 0; d < linea.length; d ++)
196
                                                                    linea[d] = b[(iBand * w * h) + (line * w) + d];
197
                                                            pageBuffer2.setLineInBandByte(linea, line, iBand);
198
                                            }
199
                            }
200
                                    break;                
201
            }
202
            dis.close();
203
            IDataWriter dataWriter1 = new WriterBufferServer(pageBuffer2);
204
            grw = GeoRasterWriter.getWriter(dataWriter1, 
205
                                                                                        inFileName + ".tif", 
206
                                                                                        pageLines, //Block size 
207
                                                                                        pageBuffer2.getBandCount(),
208
                                                                                        new Extent(0, 0, pageBuffer2.getWidth() - 1, pageBuffer2.getHeight() - 1), 
209
                                                                                        0, 
210
                                                                                        pageBuffer2.getWidth(), 
211
                                                                                        pageBuffer2.getHeight(), 
212
                                                                                        pageBuffer2.getDataType());
213
            grw.dataWrite();
214
                        grw.writeClose();
215
                }
216
        }
217
        
218
        /**
219
         * Salva un PageBuffer sobre un stream DataOutpuStream dependiendo del tipo de dato del 
220
         * buffer.
221
         * @param dos DataOutputStream 
222
         * @param pageBuffer PageBuffer
223
         * @throws IOException
224
         */
225
        private void save(DataOutputStream dos, PageBuffer pageBuffer) throws IOException{
226
                switch(pageBuffer.getDataType()){
227
        case IBuffer.TYPE_BYTE:        
228
                for(int iBand = 0; iBand < pageBuffer.getBandCount(); iBand ++)
229
                        for(int line = 0; line < pageBuffer.getHeight(); line ++)
230
                                //for(int col = 0; col < pageBuffer.getWidth(); col ++)
231
                                                //dos.writeByte(pageBuffer.getElemByte(line, col, iBand));
232
                                dos.write(pageBuffer.getLineFromBandByte(line, iBand));
233
                break;                
234
        case IBuffer.TYPE_SHORT:        
235
                    for(int iBand = 0; iBand < pageBuffer.getBandCount(); iBand ++)
236
                                for(int line = 0; line < pageBuffer.getHeight(); line ++)
237
                                        for(int col = 0; col < pageBuffer.getWidth(); col ++)
238
                                                dos.writeShort(pageBuffer.getElemShort(line, col, iBand));
239
                        break;
240
        case IBuffer.TYPE_INT:        
241
                    for(int iBand = 0; iBand < pageBuffer.getBandCount(); iBand ++)
242
                                for(int line = 0; line < pageBuffer.getHeight(); line ++)
243
                                        for(int col = 0; col < pageBuffer.getWidth(); col ++)
244
                                                dos.writeInt(pageBuffer.getElemInt(line, col, iBand));
245
                        break;
246
        case IBuffer.TYPE_FLOAT:        
247
                    for(int iBand = 0; iBand < pageBuffer.getBandCount(); iBand ++)
248
                                for(int line = 0; line < pageBuffer.getHeight(); line ++)
249
                                        for(int col = 0; col < pageBuffer.getWidth(); col ++)
250
                                                dos.writeFloat(pageBuffer.getElemFloat(line, col, iBand));
251
                        break;
252
        case IBuffer.TYPE_DOUBLE:        
253
                    for(int iBand = 0; iBand < pageBuffer.getBandCount(); iBand ++)
254
                                for(int line = 0; line < pageBuffer.getHeight(); line ++)
255
                                        for(int col = 0; col < pageBuffer.getWidth(); col ++)
256
                                                dos.writeDouble(pageBuffer.getElemDouble(line, col, iBand));
257
                        break;
258
                        
259
        }
260
        }
261
        
262
        /**
263
         * Carga un PageBuffer desde un stream DataInputStream dependiendo del tipo de dato del 
264
         * buffer.
265
         * @param dis DataInputStream
266
         * @param pageBuffer PageBuffer
267
         * @throws IOException
268
         */
269
        private void read(DataInputStream dis, PageBuffer pageBuffer) throws IOException{
270
                int w = pageBuffer.getWidth();
271
                int h = pageBuffer.getHeight();
272
                int wxh = w * h;
273
                int j = 0;
274
                switch(pageBuffer.getDataType()){
275
                case IBuffer.TYPE_BYTE:        
276
                        byte[] b = new byte[pageBuffer.getWidth() * pageBuffer.getHeight() * pageBuffer.getBandCount()];
277
                        dis.readFully(b);
278
                        //while(j < b.length){ b[j] = dis.readByte(); j ++; }
279
                        for(int iBand = 0; iBand < pageBuffer.getBandCount(); iBand ++){
280
                                        for(int line = 0; line < pageBuffer.getHeight(); line ++){
281
                                                        byte[] linea = new byte[w];
282
                                                        for(int d = 0; d < linea.length; d ++)
283
                                                                linea[d] = b[(iBand * wxh) + (line * w) + d];
284
                                                        pageBuffer.setLineInBandByte(linea, line, iBand);
285
                                        }
286
                        }
287
                                break;
288
                case IBuffer.TYPE_SHORT:
289
                        short[] s = new short[pageBuffer.getWidth() * pageBuffer.getHeight() * pageBuffer.getBandCount()];
290
                        while(j < s.length){ s[j] = dis.readShort(); j ++; }
291
                        for(int iBand = 0; iBand < pageBuffer.getBandCount(); iBand ++){
292
                                        for(int line = 0; line < pageBuffer.getHeight(); line ++){
293
                                                        short[] linea = new short[w];
294
                                                        for(int x = 0; x < linea.length; x ++)
295
                                                                linea[x] = s[(iBand * wxh) + (line * w) + x];
296
                                                        pageBuffer.setLineInBandShort(linea, line, iBand);
297
                                        }
298
                        }
299
                                break;
300
                case IBuffer.TYPE_INT:
301
                        int[] i = new int[pageBuffer.getWidth() * pageBuffer.getHeight() * pageBuffer.getBandCount()];
302
                        while(j < i.length){ i[j] = dis.readInt(); j ++; }
303
                        for(int iBand = 0; iBand < pageBuffer.getBandCount(); iBand ++){
304
                                        for(int line = 0; line < pageBuffer.getHeight(); line ++){
305
                                                        int[] linea = new int[w];
306
                                                        for(int x = 0; x < linea.length; x ++)
307
                                                                linea[x] = i[(iBand * wxh) + (line * w) + x];
308
                                                        pageBuffer.setLineInBandInt(linea, line, iBand);
309
                                        }
310
                        }
311
                                break;
312
                case IBuffer.TYPE_FLOAT:
313
                        float[] f = new float[pageBuffer.getWidth() * pageBuffer.getHeight() * pageBuffer.getBandCount()];
314
                        while(j < f.length){ f[j] = dis.readFloat(); j ++; }
315
                        for(int iBand = 0; iBand < pageBuffer.getBandCount(); iBand ++){
316
                                        for(int line = 0; line < pageBuffer.getHeight(); line ++){
317
                                                        float[] linea = new float[w];
318
                                                        for(int x = 0; x < linea.length; x ++)
319
                                                                linea[x] = f[(iBand * wxh) + (line * w) + x];
320
                                                        pageBuffer.setLineInBandFloat(linea, line, iBand);
321
                                        }
322
                        }
323
                                break;
324
                case IBuffer.TYPE_DOUBLE:
325
                        double[] d = new double[pageBuffer.getWidth() * pageBuffer.getHeight() * pageBuffer.getBandCount()];
326
                        while(j < d.length){ d[j] = dis.readDouble(); j ++; }
327
                        for(int iBand = 0; iBand < pageBuffer.getBandCount(); iBand ++){
328
                                        for(int line = 0; line < pageBuffer.getHeight(); line ++){
329
                                                        double[] linea = new double[w];
330
                                                        for(int x = 0; x < linea.length; x ++)
331
                                                                linea[x] = d[(iBand * wxh) + (line * w) + x];
332
                                                        pageBuffer.setLineInBandDouble(linea, line, iBand);
333
                                        }
334
                        }
335
                                break;
336
                }
337
        }
338
        
339
        //---------------------------------------------------------------
340
        //TILEADO A BASE DE TIFF: Desechado porque es muy lento
341
        
342
        /*public void loadPage(int nPag, PageBuffer pageBuffer) {
343
                String fileName =  tempDirectoryPath + File.separator + id + "-" + nPag + ".tif";
344
                File file = new File(id);
345
                if(!file.exists())
346
                        return; //El trozo no ha sido volcado a disco por lo que no se carga.
347
                
348
                GeoRasterFile grf = GeoRasterFile.openFile(null, fileName);
349
                
350
                //Creamos un BandList con todas las bandas del fichero
351
                BandList bandList = new BandList();
352
                for(int i = 0; i < grf.getBandCount();i++){
353
                        try{
354
                                Band band = new Band(grf.getName(), i, grf.getDataType());
355
                                bandList.addBand(band, i);
356
                        }catch(BandFoundInListException ex){
357
                                //No a?adimos la banda
358
                        }
359
                }
360
                grf.getWindowRaster(0, 0, grf.getWidth(), grf.getHeight(), bandList, pageBuffer);
361
        }*/
362

    
363
        
364
        /*public void savePage(int nPag, PageBuffer pageBuffer) throws IOException {
365
                createTempDirectory();
366
                String fileName =  tempDirectoryPath + File.separator + id + "-" + nPag + ".tif";
367
                IDataWriter dataWriter = new WriterBufferServer(pageBuffer);
368
                GeoRasterWriter grw = GeoRasterWriter.getWriter(dataWriter, 
369
                                                                                                                fileName, 
370
                                                                                                                pageBuffer.getHeight(), //Block size 
371
                                                                                                                pageBuffer.getBandCount(),
372
                                                                                                                new Extent(0, 0, pageBuffer.getWidth() - 1, pageBuffer.getHeight() - 1), 
373
                                                                                                                0, 
374
                                                                                                                pageBuffer.getWidth(), 
375
                                                                                                                pageBuffer.getHeight(), 
376
                                                                                                                pageBuffer.getDataType());
377
                grw.dataWrite();
378
                grw.writeClose();
379
                
380
        }*/
381
}
382