root / trunk / libraries / libRaster / src / org / gvsig / raster / buffer / cache / RasterReadOnlyHugeBuffer.java @ 11396
History | View | Annotate | Download (14.8 KB)
1 |
package org.gvsig.raster.buffer.cache; |
---|---|
2 |
|
3 |
import java.awt.geom.Point2D; |
4 |
import java.io.File; |
5 |
import java.io.FileNotFoundException; |
6 |
|
7 |
import org.gvsig.raster.RasterLibrary; |
8 |
import org.gvsig.raster.buffer.IBand; |
9 |
import org.gvsig.raster.buffer.RasterBuffer; |
10 |
import org.gvsig.raster.buffer.RasterMemoryBuffer; |
11 |
import org.gvsig.raster.dataset.Band; |
12 |
import org.gvsig.raster.dataset.BandList; |
13 |
import org.gvsig.raster.dataset.BandNotFoundInListException; |
14 |
import org.gvsig.raster.dataset.FileNotExistsException; |
15 |
import org.gvsig.raster.dataset.IBuffer; |
16 |
import org.gvsig.raster.dataset.InvalidSetViewException; |
17 |
import org.gvsig.raster.dataset.NotSupportedExtensionException; |
18 |
import org.gvsig.raster.dataset.RasterDataset; |
19 |
import org.gvsig.raster.dataset.RasterDriverException; |
20 |
import org.gvsig.raster.shared.Extent; |
21 |
|
22 |
/**
|
23 |
*
|
24 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
25 |
*
|
26 |
*/
|
27 |
public class RasterReadOnlyHugeBuffer extends RasterBuffer { |
28 |
/**
|
29 |
* Nombre de fichero de raster en disco
|
30 |
*/
|
31 |
private String fileName = null; |
32 |
/**
|
33 |
* Pagina cargada
|
34 |
*/
|
35 |
private IBuffer page = null; |
36 |
/**
|
37 |
* N?mero de p?gina cargada en IBuffer
|
38 |
*/
|
39 |
private int loadPage = -1; |
40 |
/**
|
41 |
* Lista de extents de cada p?gina
|
42 |
*/
|
43 |
//private Extent[] extentList = null;
|
44 |
private int heightLastPage = 0; |
45 |
|
46 |
private RasterDataset dataset = null; |
47 |
|
48 |
private int bitsPag = 0; |
49 |
/**
|
50 |
* N?mero de p?ginas
|
51 |
*/
|
52 |
private int nPages = 0; |
53 |
/**
|
54 |
* Lista de bandas
|
55 |
*/
|
56 |
private BandList bandList = null; |
57 |
private int[] drawableBands = null; |
58 |
/**
|
59 |
* Extensi?n asociada al buffer
|
60 |
*/
|
61 |
private int minX = 0, minY = 0, maxX = 0, maxY = 0; |
62 |
|
63 |
/**
|
64 |
* Para extraer el desplazamiento de una direcci?n (l?nea de raster) hay que hacer una operaci?n And con
|
65 |
* con la altura de la p?gina -1. Por ejemplo, una p?gina de 16 l?neas de altura el desplazamiento ser?
|
66 |
* 16 - 1 = 15 porque 15 en binario es 1111.
|
67 |
*
|
68 |
* Si queremos acceder a la linea del raster n?mero 83 (1010011) realizando la operaci?n And con el valor del
|
69 |
* desplazamiento obtenemos (0001111 & 1010011 = 0000011), es decir el valor 3 en decimal. Esto quiere decir
|
70 |
* que la l?nea 83 del raster es la 3 de su p?gina.
|
71 |
*/
|
72 |
private int offset = 1; |
73 |
|
74 |
/**
|
75 |
* Constructor. Asigna las variables de inicializaci?n y crea la estructura de
|
76 |
* la cach? con los datos pasados.
|
77 |
* @param dataType Tipo de dato
|
78 |
* @param width Ancho
|
79 |
* @param height Alto
|
80 |
* @param bandNr Banda
|
81 |
* @param orig
|
82 |
* @throws RasterDriverException
|
83 |
* @throws NotSupportedExtensionException
|
84 |
* @throws FileNotFoundException
|
85 |
*/
|
86 |
public RasterReadOnlyHugeBuffer(int dataType, int width, int height, int nBand) |
87 |
throws FileNotFoundException, NotSupportedExtensionException, RasterDriverException { |
88 |
|
89 |
this.dataType = dataType;
|
90 |
this.width = width;
|
91 |
this.height = height;
|
92 |
this.nBands = nBand;
|
93 |
} |
94 |
|
95 |
/**
|
96 |
* Asigna los parametros relativos al buffer. Estos son el nombre del fichero asociado
|
97 |
* y la extensi?n asignada
|
98 |
* @param fileName Nombre del fichero asociado
|
99 |
* @param ext Extensi?n
|
100 |
*/
|
101 |
public void setBufferParams(String fileName, Extent ext) |
102 |
throws InvalidSetViewException, FileNotExistsException, NotSupportedExtensionException {
|
103 |
if(!new File(fileName).exists()) |
104 |
throw new FileNotExistsException("File:" + fileName); |
105 |
|
106 |
try {
|
107 |
dataset = RasterDataset.open(null, fileName);
|
108 |
} catch (RasterDriverException e) {
|
109 |
throw new FileNotExistsException("File:" + fileName); |
110 |
} |
111 |
|
112 |
if( ext.minX() < dataset.getExtent().minX() || ext.minY() < dataset.getExtent().minY() ||
|
113 |
ext.maxX() > dataset.getExtent().maxX() || ext.maxY() > dataset.getExtent().maxY()) |
114 |
throw new InvalidSetViewException(""); |
115 |
|
116 |
Point2D p1 = dataset.worldToRaster(new Point2D.Double(minX, maxY)); |
117 |
Point2D p2 = dataset.worldToRaster(new Point2D.Double(maxX, minY)); |
118 |
|
119 |
this.fileName = fileName;
|
120 |
this.minX = (int)p1.getX(); |
121 |
this.minY = (int)p1.getY(); |
122 |
this.maxX = (int)p2.getX(); |
123 |
this.maxY = (int)p2.getY(); |
124 |
|
125 |
init(); |
126 |
} |
127 |
|
128 |
/**
|
129 |
* Asigna los parametros relativos al buffer. Estos son el nombre del fichero asociado
|
130 |
* y la extensi?n asignada
|
131 |
* @param fileName Nombre del fichero asociado
|
132 |
* @param ext Extensi?n
|
133 |
*/
|
134 |
public void setBufferParams(String fileName, int minX, int minY, int maxX, int maxY) |
135 |
throws InvalidSetViewException, FileNotExistsException, NotSupportedExtensionException {
|
136 |
if(!new File(fileName).exists()) |
137 |
throw new FileNotExistsException("File:" + fileName); |
138 |
|
139 |
try {
|
140 |
dataset = RasterDataset.open(null, fileName);
|
141 |
} catch (RasterDriverException e) {
|
142 |
throw new FileNotExistsException("File:" + fileName); |
143 |
} |
144 |
|
145 |
this.fileName = fileName;
|
146 |
this.minX = minX;
|
147 |
this.minY = minY;
|
148 |
this.maxX = maxX;
|
149 |
this.maxY = maxY;
|
150 |
|
151 |
init(); |
152 |
} |
153 |
|
154 |
/**
|
155 |
* Acciones de inicializaci?n.
|
156 |
* Crear el buffer de memoria y la lista de bandas, as? comno los bits empleados
|
157 |
* por cada p?gina de memoria y el desplazamiento que supone para el calculo
|
158 |
* de direcciones.
|
159 |
*/
|
160 |
private void init() { |
161 |
//Calculo de los bits por p?gina
|
162 |
int h = RasterLibrary.blockHeight;
|
163 |
while(h > 1) { |
164 |
h >>= 1;
|
165 |
bitsPag ++; |
166 |
} |
167 |
|
168 |
//Calculo del desplazamiento
|
169 |
offset = RasterLibrary.blockHeight - 1;
|
170 |
|
171 |
page = new RasterMemoryBuffer(dataType, width, RasterLibrary.blockHeight, dataset.getBandCount(), true); |
172 |
|
173 |
nPages = (int)Math.ceil((double)dataset.getHeight() / (double)RasterLibrary.blockHeight); |
174 |
//extentList = calcExtentPages(dataset, nPages);
|
175 |
double aux = ((double)dataset.getHeight() / (double)RasterLibrary.blockHeight); |
176 |
heightLastPage = (int)((aux - (int)aux) * RasterLibrary.blockHeight); |
177 |
|
178 |
drawableBands = new int[dataset.getBandCount()]; |
179 |
for (int i = 0; i < drawableBands.length; i++) |
180 |
drawableBands[i] = i; |
181 |
|
182 |
bandList = new BandList();
|
183 |
for(int i = 0; i < dataset.getBandCount();i++) { |
184 |
try{
|
185 |
Band band = new Band(dataset.getFName(), i, dataset.getDataType());
|
186 |
band.setPositionToDrawInBuffer(i); |
187 |
bandList.addBand(band, i); |
188 |
}catch(BandNotFoundInListException ex) {
|
189 |
//No a?adimos la banda
|
190 |
} |
191 |
} |
192 |
} |
193 |
|
194 |
|
195 |
/**
|
196 |
* Calcula la extensi?n de las p?ginas en coordenadas del mundo real
|
197 |
* @param dataset Dataset
|
198 |
* @param nPages N?mero de p?ginas en que se divide el raster
|
199 |
* @return Extensi?n de cada p?gina
|
200 |
*/
|
201 |
private Extent[] calcExtentPages(RasterDataset dataset, int nPages) { |
202 |
Extent datasetExtent = dataset.getExtent(); |
203 |
double h = (RasterLibrary.blockHeight * dataset.getExtent().height()) / dataset.getHeight();
|
204 |
Extent[] ext = new Extent[nPages]; |
205 |
|
206 |
double minX = datasetExtent.getMin().getX();
|
207 |
double maxX = datasetExtent.getMax().getX();
|
208 |
double maxY = datasetExtent.getMax().getY();
|
209 |
double minY = maxY - h;
|
210 |
for (int i = 0; i < ext.length; i++) { |
211 |
ext[i] = new Extent(minX, maxY, maxX, minY);
|
212 |
maxY = minY; |
213 |
minY -= h; |
214 |
if(minY < datasetExtent.minY())
|
215 |
minY = datasetExtent.minY(); |
216 |
} |
217 |
return ext;
|
218 |
} |
219 |
|
220 |
public void malloc(int dataType, int width, int height, int bandNr) { |
221 |
} |
222 |
|
223 |
/*
|
224 |
* (non-Javadoc)
|
225 |
* @see org.gvsig.raster.driver.IBuffer#isBandSwitchable()
|
226 |
*/
|
227 |
public boolean isBandSwitchable() { |
228 |
return false; |
229 |
} |
230 |
|
231 |
private void loadPage(int pag) { |
232 |
if(pag == nPages - 1) { |
233 |
page = new RasterMemoryBuffer(dataType, width, heightLastPage, dataset.getBandCount(), true); |
234 |
dataset.getWindowRaster(0, RasterLibrary.blockHeight * pag, dataset.getWidth(), heightLastPage, bandList, page);
|
235 |
} else {
|
236 |
page = new RasterMemoryBuffer(dataType, width, RasterLibrary.blockHeight, dataset.getBandCount(), true); |
237 |
dataset.getWindowRaster(0, RasterLibrary.blockHeight * pag, dataset.getWidth(), RasterLibrary.blockHeight, bandList, page);
|
238 |
} |
239 |
|
240 |
loadPage = pag; |
241 |
} |
242 |
|
243 |
//*********************************************************
|
244 |
|
245 |
public byte[][] getLineByte(int line) { |
246 |
int pag = line >> bitsPag;
|
247 |
if(pag != loadPage)
|
248 |
loadPage(pag); |
249 |
return page.getLineByte(line & offset);
|
250 |
} |
251 |
|
252 |
public short[][] getLineShort(int line) { |
253 |
int pag = line >> bitsPag;
|
254 |
if(pag != loadPage)
|
255 |
loadPage(pag); |
256 |
return page.getLineShort(line & offset);
|
257 |
} |
258 |
|
259 |
public int[][] getLineInt(int line) { |
260 |
int pag = line >> bitsPag;
|
261 |
if(pag != loadPage)
|
262 |
loadPage(pag); |
263 |
return page.getLineInt(line & offset);
|
264 |
} |
265 |
|
266 |
public float[][] getLineFloat(int line) { |
267 |
int pag = line >> bitsPag;
|
268 |
if(pag != loadPage)
|
269 |
loadPage(pag); |
270 |
return page.getLineFloat(line & offset);
|
271 |
} |
272 |
|
273 |
public double[][] getLineDouble(int line) { |
274 |
int pag = line >> bitsPag;
|
275 |
if(pag != loadPage)
|
276 |
loadPage(pag); |
277 |
return page.getLineDouble(line & offset);
|
278 |
} |
279 |
|
280 |
//*********************************************************
|
281 |
|
282 |
public byte[] getLineFromBandByte(int line, int band) { |
283 |
int pag = line >> bitsPag;
|
284 |
if(pag != loadPage)
|
285 |
loadPage(pag); |
286 |
return page.getLineFromBandByte(line & offset, band);
|
287 |
} |
288 |
|
289 |
public short[] getLineFromBandShort(int line, int band) { |
290 |
int pag = line >> bitsPag;
|
291 |
if(pag != loadPage)
|
292 |
loadPage(pag); |
293 |
return page.getLineFromBandShort(line & offset, band);
|
294 |
} |
295 |
|
296 |
public int[] getLineFromBandInt(int line, int band) { |
297 |
int pag = line >> bitsPag;
|
298 |
if(pag != loadPage)
|
299 |
loadPage(pag); |
300 |
return page.getLineFromBandInt(line & offset, band);
|
301 |
} |
302 |
|
303 |
public float[] getLineFromBandFloat(int line, int band) { |
304 |
int pag = line >> bitsPag;
|
305 |
if(pag != loadPage)
|
306 |
loadPage(pag); |
307 |
return page.getLineFromBandFloat(line & offset, band);
|
308 |
} |
309 |
|
310 |
public double[] getLineFromBandDouble(int line, int band) { |
311 |
int pag = line >> bitsPag;
|
312 |
if(pag != loadPage)
|
313 |
loadPage(pag); |
314 |
return page.getLineFromBandDouble(line & offset, band);
|
315 |
} |
316 |
|
317 |
//*********************************************************
|
318 |
|
319 |
public byte getElemByte(int line, int col, int band) { |
320 |
int pag = line >> bitsPag;
|
321 |
if(pag != loadPage)
|
322 |
loadPage(pag); |
323 |
return page.getElemByte(line & offset, col, band);
|
324 |
} |
325 |
|
326 |
public short getElemShort(int line, int col, int band) { |
327 |
int pag = line >> bitsPag;
|
328 |
if(pag != loadPage)
|
329 |
loadPage(pag); |
330 |
return page.getElemShort(line & offset, col, band);
|
331 |
} |
332 |
|
333 |
public int getElemInt(int line, int col, int band) { |
334 |
int pag = line >> bitsPag;
|
335 |
if(pag != loadPage)
|
336 |
loadPage(pag); |
337 |
return page.getElemInt(line & offset, col, band);
|
338 |
} |
339 |
|
340 |
public float getElemFloat(int line, int col, int band) { |
341 |
int pag = line >> bitsPag;
|
342 |
if(pag != loadPage)
|
343 |
loadPage(pag); |
344 |
return page.getElemFloat(line & offset, col, band);
|
345 |
} |
346 |
|
347 |
public double getElemDouble(int line, int col, int band) { |
348 |
int pag = line >> bitsPag;
|
349 |
if(pag != loadPage)
|
350 |
loadPage(pag); |
351 |
return page.getElemDouble(line & offset, col, band);
|
352 |
} |
353 |
|
354 |
//*********************************************************
|
355 |
|
356 |
public void getElemByte(int line, int col, byte[] data) { |
357 |
int pag = line >> bitsPag;
|
358 |
if(pag != loadPage)
|
359 |
loadPage(pag); |
360 |
page.getElemByte(line & offset, col, data); |
361 |
} |
362 |
|
363 |
public void getElemShort(int line, int col, short[] data) { |
364 |
int pag = line >> bitsPag;
|
365 |
if(pag != loadPage)
|
366 |
loadPage(pag); |
367 |
page.getElemShort(line & offset, col, data); |
368 |
} |
369 |
|
370 |
public void getElemInt(int line, int col, int[] data) { |
371 |
int pag = line >> bitsPag;
|
372 |
if(pag != loadPage)
|
373 |
loadPage(pag); |
374 |
page.getElemInt(line & offset, col, data); |
375 |
} |
376 |
|
377 |
public void getElemFloat(int line, int col, float[] data) { |
378 |
int pag = line >> bitsPag;
|
379 |
if(pag != loadPage)
|
380 |
loadPage(pag); |
381 |
page.getElemFloat(line & offset, col, data); |
382 |
} |
383 |
|
384 |
public void getElemDouble(int line, int col, double[] data) { |
385 |
int pag = line >> bitsPag;
|
386 |
if(pag != loadPage)
|
387 |
loadPage(pag); |
388 |
page.getElemDouble(line & offset, col, data); |
389 |
} |
390 |
|
391 |
//***********************************************
|
392 |
//Obtiene una banda entera
|
393 |
|
394 |
public IBand getBand(int band){ |
395 |
return null; |
396 |
} |
397 |
|
398 |
/*
|
399 |
* (non-Javadoc)
|
400 |
* @see org.gvsig.fmap.driver.IBuffer#getBandBuffer(int)
|
401 |
*/
|
402 |
public IBuffer getBandBuffer(int iBand){ |
403 |
return null; |
404 |
} |
405 |
|
406 |
public void replicateBand(int orig, int dest) { |
407 |
} |
408 |
|
409 |
/*
|
410 |
* (non-Javadoc)
|
411 |
* @see org.gvsig.fmap.dataaccess.buffer.RasterBuffer#switchBands(int[])
|
412 |
*/
|
413 |
public void switchBands(int[] bandPosition){ |
414 |
|
415 |
} |
416 |
|
417 |
//*********************************************************
|
418 |
|
419 |
public RasterBuffer adjustRasterNearestNeighbourInterpolation(int w, int h, int[] bands) { |
420 |
return null; |
421 |
} |
422 |
|
423 |
public RasterBuffer adjustRasterBilinearInterpolation(int w, int h, int[] bands) { |
424 |
return null; |
425 |
} |
426 |
|
427 |
public RasterBuffer adjustRasterInverseDistanceInterpolation(int w, int h, int[] bands) { |
428 |
return null; |
429 |
} |
430 |
|
431 |
public RasterBuffer adjustRasterBicubicSplineInterpolation(int w, int h, int[] bands) { |
432 |
return null; |
433 |
} |
434 |
|
435 |
public RasterBuffer adjustRasterBSplineInterpolation(int w, int h, int[] bands) { |
436 |
return null; |
437 |
} |
438 |
|
439 |
/*
|
440 |
* (non-Javadoc)
|
441 |
* @see org.gvsig.fmap.driver.IBuffer#cloneBuffer()
|
442 |
*/
|
443 |
public IBuffer cloneBuffer(){
|
444 |
return null; |
445 |
} |
446 |
|
447 |
|
448 |
/*
|
449 |
* (non-Javadoc)
|
450 |
* @see org.gvsig.fmap.driver.IBuffer#mallocOneBand(int, int, int, int)
|
451 |
*/
|
452 |
public void mallocOneBand(int dataType, int width, int height, int band) { |
453 |
|
454 |
} |
455 |
|
456 |
/*
|
457 |
* (non-Javadoc)
|
458 |
* @see org.gvsig.fmap.driver.IBuffer#copyBand(int, org.gvsig.fmap.dataaccess.buffer.IBand)
|
459 |
*/
|
460 |
public void copyBand(int nBand, IBand band) { |
461 |
|
462 |
} |
463 |
|
464 |
/*
|
465 |
* (non-Javadoc)
|
466 |
* @see org.gvsig.fmap.driver.IBuffer#assignBand(int, org.gvsig.fmap.dataaccess.buffer.IBand)
|
467 |
*/
|
468 |
public void assignBand(int nBand, IBand band) { |
469 |
|
470 |
} |
471 |
|
472 |
/*
|
473 |
* (non-Javadoc)
|
474 |
* @see org.gvsig.fmap.driver.IBuffer#createBand(byte)
|
475 |
*/
|
476 |
public IBand createBand(byte defaultValue) { |
477 |
|
478 |
return null; |
479 |
} |
480 |
|
481 |
public void assignBandToNotValid(int iBand) {} |
482 |
public void assign(int band, byte value) {} |
483 |
public void assign(int band, short value) {} |
484 |
public void assign(int band, int value) {} |
485 |
public void assign(int band, float value) {} |
486 |
public void assign(int band, double value) {} |
487 |
public void interchangeBands(int band1, int band2) {} |
488 |
public void setElem(int line, int col, int band, byte data) {} |
489 |
public void setElem(int line, int col, int band, short data) {} |
490 |
public void setElem(int line, int col, int band, int data) {} |
491 |
public void setElem(int line, int col, int band, float data) {} |
492 |
public void setElem(int line, int col, int band, double data) {} |
493 |
public void setElemByte(int line, int col, byte[] data) {} |
494 |
public void setElemDouble(int line, int col, double[] data) {} |
495 |
public void setElemFloat(int line, int col, float[] data) {} |
496 |
public void setElemInt(int line, int col, int[] data) {} |
497 |
public void setElemShort(int line, int col, short[] data) {} |
498 |
public void setLineByte(byte[][] data, int line) {} |
499 |
public void setLineDouble(double[][] data, int line) {} |
500 |
public void setLineFloat(float[][] data, int line) {} |
501 |
public void setLineInBandByte(byte[] data, int line, int band) {} |
502 |
public void setLineInBandDouble(double[] data, int line, int band) {} |
503 |
public void setLineInBandFloat(float[] data, int line, int band) {} |
504 |
public void setLineInBandInt(int[] data, int line, int band) {} |
505 |
public void setLineInBandShort(short[] data, int line, int band) {} |
506 |
public void setLineInt(int[][] data, int line) {} |
507 |
public void setLineShort(short[][] data, int line) {} |
508 |
|
509 |
} |