gvsig-raster / org.gvsig.raster.cache / trunk / org.gvsig.raster.cache / org.gvsig.raster.cache.lib.impl / deprecated / buffer / impl / BufferCacheManagerImpl.java @ 1939
History | View | Annotate | Download (11.6 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.BufferNoData; |
10 |
import org.gvsig.raster.cache.buffer.BufferParam; |
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.gvsig.raster.cache.buffer.impl.task.DefaultTaskEventManager; |
20 |
import org.gvsig.raster.cache.buffer.task.TaskEventManager; |
21 |
import org.slf4j.Logger; |
22 |
import org.slf4j.LoggerFactory; |
23 |
|
24 |
public class BufferCacheManagerImpl implements BufferCacheManager { |
25 |
private static final Logger logger = LoggerFactory.getLogger(BufferCacheManagerImpl.class); |
26 |
|
27 |
/**
|
28 |
* This variable represents which buffer is going to be used. By default has a
|
29 |
* DONT_FORCE value. That means that the algorithm to select the type of buffer
|
30 |
* is applied. The other types of constants force the use of a kind of buffer.
|
31 |
*/
|
32 |
public static int forceBuffer = DONT_FORCE; |
33 |
|
34 |
/**
|
35 |
* Tama?o aproximado de cach? en Megas. Si este valor es alto cabr?n muchas p?ginas en memoria
|
36 |
* a la vez y si es bajo cabr?n pocas. Hay que tener en cuenta que al instanciar se convertira en bytes
|
37 |
* para su mejor tratamiento. Al llamar al constructor esta variable contendr? el tama?o exacto
|
38 |
* de la cache en bytes. El tama?o aqu? especificado es aproximado. Este variar? dependiendo de los
|
39 |
* par?metros del raster a cachear ya que las p?ginas deben tener una altura potencia de 2.
|
40 |
*/
|
41 |
public static long cacheSize = 25; |
42 |
/**
|
43 |
* Tama?o m?ximo de la p?gina en Megas. Hay que tener en cuenta que al instanciar se convertira en bytes
|
44 |
* para su mejor tratamiento. Al llamar al constructor esta variable contendr? el tama?o exacto
|
45 |
* de la p?gina en bytes
|
46 |
*/
|
47 |
public static double pageSize = 4; |
48 |
/**
|
49 |
* N?mero de p?ginas que tiene cada conjunto de cach?
|
50 |
*/
|
51 |
public static int pagsPerGroup = 5; |
52 |
/**
|
53 |
* En la generaci?n autom?tica de clases esta variable representa el n?mero de
|
54 |
* clases en las que se hace la divisi?n.
|
55 |
*/
|
56 |
public static int defaultNumberOfClasses = 64; |
57 |
|
58 |
/**
|
59 |
* Genera instancias del buffer de datos adecuado al tama?o del raster. Si no hay muchos datos
|
60 |
* (menos de cacheMemorySize) crear? un buffer en memoria. Si hay m?s de esta cantidad
|
61 |
* entonces crearemos un buffer cacheado (RasterCache). A partir de la cantidad se?alada
|
62 |
* por multicacheMemorySize haremos un buffer cacheado donde cada p?gina no ocupa todo
|
63 |
* el ancho del raster ya que este ser? muy grande. La gesti?n de una cache donde cada
|
64 |
* pagina ha de partir una l?nea lleva una complejidad a?adida.
|
65 |
*
|
66 |
* @param params Par?metros de la carga
|
67 |
* @return Objeto RasterBuffer
|
68 |
*/
|
69 |
public Buffer createBuffer(BufferParam params) { |
70 |
//Opci?n de cachear siempre activada (Solo DEBUG)
|
71 |
if(forceBuffer == BufferCacheManager.BAND_CACHE)
|
72 |
return new RasterCacheBuffer(params.getDataType(), |
73 |
(int)params.getWidth(),
|
74 |
(int)params.getHeight(),
|
75 |
params.getBandCount()); |
76 |
|
77 |
if(forceBuffer == BufferCacheManager.BAND_VERT_CACHE)
|
78 |
return new RasterVertCacheBuffer(params.getDataType(), |
79 |
(int)params.getWidth(),
|
80 |
(int)params.getHeight(),
|
81 |
params.getBandCount()); |
82 |
|
83 |
if (forceBuffer == BufferCacheManager.READ_ONLY_CACHE)
|
84 |
return new RasterReadOnlyBuffer(params.getDataSource(), |
85 |
params.getX(), |
86 |
params.getY(), |
87 |
params.getWidth(), |
88 |
params.getHeight(), |
89 |
params.getBandList()); |
90 |
|
91 |
if (forceBuffer == BufferCacheManager.MEMORY_BUFFER) {
|
92 |
RasterMemoryBuffer b = new RasterMemoryBuffer(params.getDataType(),
|
93 |
(int)params.getWidth(),
|
94 |
(int)params.getHeight(),
|
95 |
params.getBandCount(), |
96 |
true);
|
97 |
try {
|
98 |
//Para el buffer en memoria ya no necesitamos el proveedor de datos
|
99 |
if(params.getDataSource() != null) |
100 |
params.getDataSource().close(); |
101 |
} catch (IOException e) { |
102 |
logger.debug("I can't close the dataset", b, e);
|
103 |
} |
104 |
return b;
|
105 |
} |
106 |
|
107 |
if(forceBuffer == BufferCacheManager.DONT_FORCE) {
|
108 |
if(params.getAccessType() == RasterBuffer.READ_ONLY)
|
109 |
return new RasterReadOnlyBuffer(params.getDataSource(), |
110 |
params.getX(), |
111 |
params.getY(), |
112 |
params.getWidth(), |
113 |
params.getHeight(), |
114 |
params.getBandList()); |
115 |
|
116 |
|
117 |
if(params.getAccessType() == RasterBuffer.READ_WRITE || params.getAccessType() == RasterBuffer.READ_WRITE_CVERT) {
|
118 |
long size = (RasterBuffer.getBytesFromRasterBufType((int)params.getDataType()) * (int)params.getWidth() * (int)params.getHeight() * (int)params.getBandCount()) / 1024; |
119 |
long ms1 = BufferCacheManagerImpl.cacheSize * 1024; |
120 |
if(size <= ms1) {
|
121 |
RasterMemoryBuffer b = new RasterMemoryBuffer(params.getDataType(),
|
122 |
(int)params.getWidth(),
|
123 |
(int)params.getHeight(),
|
124 |
params.getBandCount(), |
125 |
true);
|
126 |
try {
|
127 |
//Para el buffer en memoria ya no necesitamos el proveedor de datos
|
128 |
if(params.getDataSource() != null) |
129 |
params.getDataSource().close(); |
130 |
} catch (IOException e) { |
131 |
logger.debug("I can't close the dataset", b, e);
|
132 |
} |
133 |
return b;
|
134 |
} else {
|
135 |
if(params.getAccessType() == RasterBuffer.READ_WRITE) {
|
136 |
return new RasterCacheBuffer(params.getDataType(), |
137 |
(int)params.getWidth(),
|
138 |
(int)params.getHeight(),
|
139 |
params.getBandCount()); |
140 |
} |
141 |
if(params.getAccessType() == RasterBuffer.READ_WRITE_CVERT) {
|
142 |
return new RasterVertCacheBuffer(params.getDataType(), |
143 |
(int)params.getWidth(),
|
144 |
(int)params.getHeight(),
|
145 |
params.getBandCount()); |
146 |
} |
147 |
} |
148 |
} |
149 |
} |
150 |
return null; |
151 |
} |
152 |
|
153 |
/**
|
154 |
* Genera una instancia del buffer de solo lectura. Este buffer consta de una cache y unos apuntadores
|
155 |
* a las p?ginas en disco. Cuando se accede a los datos se carga en memoria la p?gina pedida.
|
156 |
*
|
157 |
* @param file
|
158 |
* File to read data
|
159 |
*/
|
160 |
public Buffer createReadOnlyBuffer(String file) { |
161 |
BufferDataSource ds = null;
|
162 |
try {
|
163 |
ds = new BufferDataSourceImpl(file);
|
164 |
} catch (IOException e) { |
165 |
e.printStackTrace(); |
166 |
} |
167 |
return new RasterReadOnlyBuffer(ds); |
168 |
} |
169 |
|
170 |
/*
|
171 |
* (non-Javadoc)
|
172 |
* @see org.gvsig.raster.cache.buffer.BufferCacheManager#createMemoryBuffer(org.gvsig.raster.cache.buffer.BufferParam)
|
173 |
*/
|
174 |
public Buffer createMemoryBuffer(BufferParam params) { |
175 |
return new RasterMemoryBuffer(params.getDataType(), |
176 |
(int)params.getWidth(),
|
177 |
(int)params.getHeight(),
|
178 |
params.getBandCount(), |
179 |
true);
|
180 |
} |
181 |
|
182 |
/*
|
183 |
* (non-Javadoc)
|
184 |
* @see org.gvsig.raster.cache.buffer.BufferCacheManager#createMemoryBuffer(int, int, int, int, boolean)
|
185 |
*/
|
186 |
public Buffer createMemoryBuffer(int dataType, int width, int height, int bandNr, boolean malloc) { |
187 |
return new RasterMemoryBuffer(dataType, width, height, bandNr, malloc); |
188 |
} |
189 |
|
190 |
/**
|
191 |
* Creates a new instance of an interpolation object.
|
192 |
* @param buf IRasterBuffer
|
193 |
* @return IBufferInterpolation
|
194 |
*/
|
195 |
public BufferInterpolation createInterpolation(Buffer buf) { |
196 |
return new BufferInterpolationImpl(buf); |
197 |
} |
198 |
|
199 |
/*
|
200 |
* (non-Javadoc)
|
201 |
* @see org.gvsig.raster.cache.buffer.BufferCacheManager#createBufferParams(int, int, int, int)
|
202 |
*/
|
203 |
public BufferParam createBufferParams(int w, int h, int bandCount, int dataType) { |
204 |
return new BufferParamImpl(w, h, bandCount, dataType); |
205 |
} |
206 |
|
207 |
/*
|
208 |
* (non-Javadoc)
|
209 |
* @see org.gvsig.raster.cache.buffer.BufferCacheManager#createBufferParams(int, int)
|
210 |
*/
|
211 |
public BufferParam createBufferParams(int w, int h) { |
212 |
return new BufferParamImpl(w, h); |
213 |
} |
214 |
|
215 |
/*
|
216 |
* (non-Javadoc)
|
217 |
* @see org.gvsig.raster.cache.buffer.BufferCacheManager#createBufferParams(java.lang.String, int, int, int, int)
|
218 |
*/
|
219 |
public BufferParam createBufferParams(String file, int x, int y, int w, int h, int[] bands) throws IOException { |
220 |
return new BufferParamImpl(file, x, y, w, h, bands); |
221 |
} |
222 |
|
223 |
/*
|
224 |
* (non-Javadoc)
|
225 |
* @see org.gvsig.raster.cache.buffer.BufferCacheManager#createBufferParams(java.lang.String, int, int, int, int)
|
226 |
*/
|
227 |
public BufferParam createBufferParams(String file, int x, int y, int w, int h) throws IOException { |
228 |
return new BufferParamImpl(file, x, y, w, h, null); |
229 |
} |
230 |
|
231 |
/*
|
232 |
* (non-Javadoc)
|
233 |
* @see org.gvsig.raster.cache.buffer.BufferCacheManager#createBufferParams(java.lang.String[], int, int, int, int, int[])
|
234 |
*/
|
235 |
public BufferParam createBufferParams(String[] files, int x, int y, int w, int h, int[] bands) throws IOException { |
236 |
return new BufferParamImpl(files, x, y, w, h, bands); |
237 |
} |
238 |
|
239 |
/*
|
240 |
* (non-Javadoc)
|
241 |
* @see org.gvsig.raster.cache.buffer.BufferCacheManager#createNoData(java.lang.Number, java.lang.Number, java.lang.String, int)
|
242 |
*/
|
243 |
public BufferNoData createNoData(Number noData, Number nativeNoData, String fileName, int bandCount) { |
244 |
return new DefaultNoData(noData, nativeNoData, fileName, bandCount); |
245 |
} |
246 |
|
247 |
/*
|
248 |
* (non-Javadoc)
|
249 |
* @see org.gvsig.raster.cache.buffer.BufferCacheManager#createNoData(java.lang.Number, java.lang.Number, java.lang.String)
|
250 |
*/
|
251 |
public BufferNoData createNoData(Number noData, Number nativeNoData, String fileName) { |
252 |
return new DefaultNoData(noData, nativeNoData, fileName); |
253 |
} |
254 |
|
255 |
/*
|
256 |
* (non-Javadoc)
|
257 |
* @see org.gvsig.raster.cache.buffer.BufferCacheManager#createDefaultNoData(int, int)
|
258 |
*/
|
259 |
public BufferNoData createDefaultNoData(int bandCount, int dataType) { |
260 |
switch (dataType) {
|
261 |
case Buffer.TYPE_BYTE: |
262 |
return new DefaultNoData(new Byte(BufferNoData.defaultByteNoDataValue), new Byte(BufferNoData.defaultByteNoDataValue), null); |
263 |
case Buffer.TYPE_SHORT: |
264 |
return new DefaultNoData(new Short(BufferNoData.defaultShortNoDataValue), new Short(BufferNoData.defaultShortNoDataValue), null); |
265 |
case Buffer.TYPE_INT: |
266 |
return new DefaultNoData(new Integer(BufferNoData.defaultIntegerNoDataValue), new Integer(BufferNoData.defaultIntegerNoDataValue), null); |
267 |
case Buffer.TYPE_FLOAT: |
268 |
return new DefaultNoData(new Float(BufferNoData.defaultFloatNoDataValue), new Float(BufferNoData.defaultFloatNoDataValue), null); |
269 |
case Buffer.TYPE_DOUBLE: |
270 |
return new DefaultNoData(new Double(BufferNoData.defaultDoubleNoDataValue), new Double(BufferNoData.defaultDoubleNoDataValue), null); |
271 |
} |
272 |
return null; |
273 |
} |
274 |
|
275 |
/*
|
276 |
* (non-Javadoc)
|
277 |
* @see org.gvsig.raster.cache.buffer.BufferCacheManager#createHistogram(int, double[], double[], int, int)
|
278 |
*/
|
279 |
public BufferHistogram createHistogram(int nBands, double[] min, double[] max, int dataType, int numberOfClasses) { |
280 |
return new BufferHistogramImpl(nBands, min, max, dataType, numberOfClasses); |
281 |
} |
282 |
|
283 |
/*
|
284 |
* (non-Javadoc)
|
285 |
* @see org.gvsig.raster.cache.buffer.BufferCacheManager#createHistogram(int, int, double[], double[])
|
286 |
*/
|
287 |
public BufferHistogram createHistogram(int nBands, int nClasses, double[] min, double[] max) { |
288 |
return new BufferHistogramImpl(nBands, nClasses, min, max); |
289 |
} |
290 |
|
291 |
/*
|
292 |
* (non-Javadoc)
|
293 |
* @see org.gvsig.raster.cache.buffer.BufferCacheManager#createTask(java.lang.Object)
|
294 |
*/
|
295 |
public TaskEventManager createTask(Object process) { |
296 |
return new DefaultTaskEventManager(process); |
297 |
} |
298 |
|
299 |
/*
|
300 |
* (non-Javadoc)
|
301 |
* @see org.gvsig.raster.cache.buffer.BufferCacheManager#getTask()
|
302 |
*/
|
303 |
public TaskEventManager getTask() {
|
304 |
return DefaultTaskEventManager.get();
|
305 |
} |
306 |
} |