Statistics
| Revision:

gvsig-raster / org.gvsig.raster.cache / trunk / org.gvsig.raster.cache / org.gvsig.raster.cache.lib.impl / src / main / java / org / gvsig / raster / cache / buffer / impl / BufferCacheManagerImpl.java @ 1001

History | View | Annotate | Download (10.8 KB)

1
package org.gvsig.raster.cache.buffer.impl;
2

    
3
import java.io.IOException;
4

    
5
import org.gvsig.raster.cache.buffer.Buffer;
6
import org.gvsig.raster.cache.buffer.BufferCacheManager;
7
import org.gvsig.raster.cache.buffer.BufferDataSource;
8
import org.gvsig.raster.cache.buffer.BufferInterpolation;
9
import org.gvsig.raster.cache.buffer.BufferParam;
10
import org.gvsig.raster.cache.buffer.BufferNoData;
11
import org.gvsig.raster.cache.buffer.histogram.BufferHistogram;
12
import org.gvsig.raster.cache.buffer.impl.datastruct.DefaultNoData;
13
import org.gvsig.raster.cache.buffer.impl.histogram.BufferHistogramImpl;
14
import org.gvsig.raster.cache.buffer.impl.io.BufferDataSourceImpl;
15
import org.gvsig.raster.cache.buffer.impl.memory.RasterMemoryBuffer;
16
import org.gvsig.raster.cache.buffer.impl.rocache.RasterReadOnlyBuffer;
17
import org.gvsig.raster.cache.buffer.impl.stripecache.horizontal.RasterCacheBuffer;
18
import org.gvsig.raster.cache.buffer.impl.stripecache.vertical.RasterVertCacheBuffer;
19
import org.slf4j.Logger;
20
import org.slf4j.LoggerFactory;
21

    
22
public class BufferCacheManagerImpl implements BufferCacheManager {
23
        private static final Logger    logger = LoggerFactory.getLogger(BufferCacheManagerImpl.class);
24
        
25
        /**
26
         * This variable represents which buffer is going to be used. By default has a
27
         * DONT_FORCE value. That means that the algorithm to select the type of buffer 
28
         * is applied. The other types of constants force the use of a kind of buffer.  
29
         */
30
        public static int                   forceBuffer                = DONT_FORCE;
31
        
32
        /**
33
         * Tama?o aproximado de cach? en Megas. Si este valor es alto cabr?n muchas p?ginas en memoria
34
         * a la vez y si es bajo cabr?n pocas. Hay que tener en cuenta que al instanciar se convertira en bytes
35
         * para su mejor tratamiento. Al llamar al constructor esta variable contendr? el tama?o exacto
36
         * de la cache en bytes. El tama?o aqu? especificado es aproximado. Este variar? dependiendo de los
37
         * par?metros del raster a cachear ya que las p?ginas deben tener una altura potencia de 2.
38
         */
39
        public static long              cacheSize                 = 25;
40
        /**
41
         * Tama?o m?ximo de la p?gina en Megas. Hay que tener en cuenta que al instanciar se convertira en bytes
42
         * para su mejor tratamiento. Al llamar al constructor esta variable contendr? el tama?o exacto
43
         * de la p?gina en bytes
44
         */
45
        public static double            pageSize                  = 4;
46
        /**
47
         * N?mero de p?ginas que tiene cada conjunto de cach?
48
         */
49
        public static int               pagsPerGroup              = 5;
50
        /**
51
         * En la generaci?n autom?tica de clases esta variable representa el n?mero de
52
         * clases en las que se hace la divisi?n.
53
         */
54
        public static int               defaultNumberOfClasses    = 64;
55
        
56
        /**
57
         * Genera instancias del buffer de datos adecuado al tama?o del raster. Si no hay muchos datos
58
         * (menos de cacheMemorySize) crear? un buffer en memoria. Si hay m?s de esta cantidad
59
         * entonces crearemos un buffer cacheado (RasterCache). A partir de la cantidad se?alada
60
         * por multicacheMemorySize haremos un buffer cacheado donde cada p?gina no ocupa todo
61
         * el ancho del raster ya que este ser? muy grande. La gesti?n de una cache donde cada
62
         * pagina ha de partir una l?nea lleva una complejidad a?adida.
63
         *  
64
         * @param params Par?metros de la carga
65
         * @return Objeto RasterBuffer
66
         */
67
        public Buffer createBuffer(BufferParam params) {
68
                //Opci?n de cachear siempre activada (Solo DEBUG)
69
                if(forceBuffer == BufferCacheManager.BAND_CACHE)
70
                        return new RasterCacheBuffer(params.getDataType(), 
71
                                        (int)params.getWidth(), 
72
                                        (int)params.getHeight(), 
73
                                        params.getBandCount());
74
                
75
                if(forceBuffer == BufferCacheManager.BAND_VERT_CACHE)
76
                        return new RasterVertCacheBuffer(params.getDataType(), 
77
                                        (int)params.getWidth(), 
78
                                        (int)params.getHeight(), 
79
                                        params.getBandCount());
80
                
81
                if (forceBuffer == BufferCacheManager.READ_ONLY_CACHE)
82
                        return new RasterReadOnlyBuffer(params.getDataSource(), 
83
                                        params.getX(), 
84
                                        params.getY(), 
85
                                        params.getWidth(), 
86
                                        params.getHeight(),
87
                                        params.getBandList());
88
                
89
                if (forceBuffer == BufferCacheManager.MEMORY_BUFFER) {
90
                        RasterMemoryBuffer b = new RasterMemoryBuffer(params.getDataType(), 
91
                                        (int)params.getWidth(), 
92
                                        (int)params.getHeight(), 
93
                                        params.getBandCount(), 
94
                                        true);
95
                        try {
96
                                //Para el buffer en memoria ya no necesitamos el proveedor de datos
97
                                if(params.getDataSource() != null)
98
                                        params.getDataSource().close();
99
                        } catch (IOException e) {
100
                                logger.debug("I can't close the dataset", b, e);
101
                        }
102
                        return b;
103
                }
104
                        
105
                if(forceBuffer == BufferCacheManager.DONT_FORCE) {
106
                        if(params.getAccessType() == RasterBuffer.READ_ONLY) 
107
                                return new RasterReadOnlyBuffer(params.getDataSource(), 
108
                                                params.getX(), 
109
                                                params.getY(), 
110
                                                params.getWidth(), 
111
                                                params.getHeight(),
112
                                                params.getBandList());
113
                        
114
                        
115
                        if(params.getAccessType() == RasterBuffer.READ_WRITE || params.getAccessType() == RasterBuffer.READ_WRITE_CVERT) {
116
                                long size = (RasterBuffer.getBytesFromRasterBufType((int)params.getDataType()) * (int)params.getWidth() * (int)params.getHeight() * (int)params.getBandCount()) / 1024;
117
                                long ms1 = BufferCacheManagerImpl.cacheSize * 1024;
118
                                if(size <= ms1) {
119
                                        RasterMemoryBuffer b =  new RasterMemoryBuffer(params.getDataType(), 
120
                                                        (int)params.getWidth(), 
121
                                                        (int)params.getHeight(), 
122
                                                        params.getBandCount(), 
123
                                                        true);
124
                                        try {
125
                                                //Para el buffer en memoria ya no necesitamos el proveedor de datos
126
                                                if(params.getDataSource() != null)
127
                                                        params.getDataSource().close();
128
                                        } catch (IOException e) {
129
                                                logger.debug("I can't close the dataset", b, e);
130
                                        }
131
                                        return b;
132
                                } else {
133
                                        if(params.getAccessType() == RasterBuffer.READ_WRITE) {
134
                                                return new RasterCacheBuffer(params.getDataType(), 
135
                                                                (int)params.getWidth(), 
136
                                                                (int)params.getHeight(), 
137
                                                                params.getBandCount());
138
                                        }
139
                                        if(params.getAccessType() == RasterBuffer.READ_WRITE_CVERT) {
140
                                                return new RasterVertCacheBuffer(params.getDataType(), 
141
                                                                (int)params.getWidth(), 
142
                                                                (int)params.getHeight(), 
143
                                                                params.getBandCount());
144
                                        }
145
                                }
146
                        }
147
                }
148
                return null;
149
        }
150
        
151
        /**
152
         * Genera una instancia del buffer de solo lectura. Este buffer consta de una cache y unos apuntadores
153
         * a las p?ginas en disco. Cuando se accede a los datos se carga en memoria la p?gina pedida.
154
         *  
155
         * @param  file 
156
         *         File to read data
157
         */
158
        public Buffer createReadOnlyBuffer(String file) {
159
                BufferDataSource ds = null;
160
                try {
161
                        ds = new BufferDataSourceImpl(file);
162
                } catch (IOException e) {
163
                        e.printStackTrace();
164
                }
165
                return new RasterReadOnlyBuffer(ds);
166
        }
167
        
168
        /*
169
         * (non-Javadoc)
170
         * @see org.gvsig.raster.cache.buffer.BufferCacheManager#createMemoryBuffer(org.gvsig.raster.cache.buffer.BufferParam)
171
         */
172
        public Buffer createMemoryBuffer(BufferParam params) {
173
                return new RasterMemoryBuffer(params.getDataType(), 
174
                                                                        (int)params.getWidth(), 
175
                                                                        (int)params.getHeight(), 
176
                                                                        params.getBandCount(), 
177
                                                                        true);
178
        }
179
        
180
        /*
181
         * (non-Javadoc)
182
         * @see org.gvsig.raster.cache.buffer.BufferCacheManager#createMemoryBuffer(int, int, int, int, boolean)
183
         */
184
        public Buffer createMemoryBuffer(int dataType, int width, int height, int bandNr, boolean malloc) {
185
                return new RasterMemoryBuffer(dataType, width, height, bandNr, malloc);
186
        }
187
        
188
        /**
189
         * Creates a new instance of an interpolation object.
190
         * @param buf IRasterBuffer
191
         * @return IBufferInterpolation
192
         */
193
        public BufferInterpolation createInterpolation(Buffer buf) {
194
                return new BufferInterpolationImpl(buf);
195
        }
196
        
197
        /*
198
         * (non-Javadoc)
199
         * @see org.gvsig.raster.cache.buffer.BufferCacheManager#createBufferParams(int, int, int, int)
200
         */
201
        public BufferParam createBufferParams(int w, int h, int bandCount, int dataType) {
202
                return new BufferParamImpl(w, h, bandCount, dataType);
203
        }
204

    
205
        /*
206
         * (non-Javadoc)
207
         * @see org.gvsig.raster.cache.buffer.BufferCacheManager#createBufferParams(int, int)
208
         */
209
        public BufferParam createBufferParams(int w, int h) {
210
                return new BufferParamImpl(w, h);
211
        }
212
        
213
        /*
214
         * (non-Javadoc)
215
         * @see org.gvsig.raster.cache.buffer.BufferCacheManager#createBufferParams(java.lang.String, int, int, int, int)
216
         */
217
        public BufferParam createBufferParams(String file, int x, int y, int w, int h, int[] bands) throws IOException {
218
                return new BufferParamImpl(file, x, y, w, h, bands);
219
        }
220
        
221
        /*
222
         * (non-Javadoc)
223
         * @see org.gvsig.raster.cache.buffer.BufferCacheManager#createBufferParams(java.lang.String, int, int, int, int)
224
         */
225
        public BufferParam createBufferParams(String file, int x, int y, int w, int h) throws IOException {
226
                return new BufferParamImpl(file, x, y, w, h, null);
227
        }
228
        
229
        /*
230
         * (non-Javadoc)
231
         * @see org.gvsig.raster.cache.buffer.BufferCacheManager#createBufferParams(java.lang.String[], int, int, int, int, int[])
232
         */
233
        public BufferParam createBufferParams(String[] files, int x, int y, int w, int h, int[] bands) throws IOException {
234
                return new BufferParamImpl(files, x, y, w, h, bands);
235
        }
236
        
237
        /*
238
         * (non-Javadoc)
239
         * @see org.gvsig.raster.cache.buffer.BufferCacheManager#createNoData(java.lang.Number, java.lang.Number, java.lang.String, int)
240
         */
241
        public BufferNoData createNoData(Number noData, Number nativeNoData, String fileName, int bandCount) {
242
                return new DefaultNoData(noData, nativeNoData, fileName, bandCount);
243
        }
244
        
245
        /*
246
         * (non-Javadoc)
247
         * @see org.gvsig.raster.cache.buffer.BufferCacheManager#createNoData(java.lang.Number, java.lang.Number, java.lang.String)
248
         */
249
        public BufferNoData createNoData(Number noData, Number nativeNoData, String fileName) {
250
                return new DefaultNoData(noData, nativeNoData, fileName);
251
        }
252
        
253
        /*
254
         * (non-Javadoc)
255
         * @see org.gvsig.raster.cache.buffer.BufferCacheManager#createDefaultNoData(int, int)
256
         */
257
        public BufferNoData createDefaultNoData(int bandCount, int dataType) {
258
                switch (dataType) {
259
                case Buffer.TYPE_BYTE:
260
                        return new DefaultNoData(new Byte(BufferNoData.defaultByteNoDataValue), new Byte(BufferNoData.defaultByteNoDataValue), null);
261
                case Buffer.TYPE_SHORT:
262
                        return new DefaultNoData(new Short(BufferNoData.defaultShortNoDataValue), new Short(BufferNoData.defaultShortNoDataValue), null);
263
                case Buffer.TYPE_INT:
264
                        return new DefaultNoData(new Integer(BufferNoData.defaultIntegerNoDataValue), new Integer(BufferNoData.defaultIntegerNoDataValue), null);
265
                case Buffer.TYPE_FLOAT:
266
                        return new DefaultNoData(new Float(BufferNoData.defaultFloatNoDataValue), new Float(BufferNoData.defaultFloatNoDataValue), null);
267
                case Buffer.TYPE_DOUBLE:
268
                        return new DefaultNoData(new Double(BufferNoData.defaultDoubleNoDataValue), new Double(BufferNoData.defaultDoubleNoDataValue), null);
269
                }
270
                return null;
271
        }
272
        
273
        public BufferHistogram createHistogram(int nBands, double[] min, double[] max, int dataType, int numberOfClasses) {
274
                return new BufferHistogramImpl(nBands, min, max, dataType, numberOfClasses);
275
        }
276
        
277
        public BufferHistogram createHistogram(int nBands, int nClasses, double[] min, double[] max) {
278
                return new BufferHistogramImpl(nBands, nClasses, min, max);
279
        }
280
}