gvsig-raster / org.gvsig.raster / branches / org.gvsig.raster.2.4 / org.gvsig.raster / org.gvsig.raster.lib / org.gvsig.raster.lib.buffer / org.gvsig.raster.lib.buffer.impl / src / main / java / org / gvsig / raster / lib / buffer / impl / AbstractTiledBand.java @ 6507
History | View | Annotate | Download (6.07 KB)
1 |
package org.gvsig.raster.lib.buffer.impl; |
---|---|
2 |
|
3 |
import java.io.IOException; |
4 |
import java.nio.Buffer; |
5 |
|
6 |
import org.gvsig.raster.lib.buffer.api.Band; |
7 |
import org.gvsig.raster.lib.buffer.api.BandInfo; |
8 |
import org.gvsig.raster.lib.buffer.api.BandTileManager; |
9 |
import org.gvsig.raster.lib.buffer.api.BufferLocator; |
10 |
import org.gvsig.raster.lib.buffer.api.NoData; |
11 |
import org.gvsig.raster.lib.buffer.impl.exceptions.CopyFromBandException; |
12 |
import org.gvsig.tools.dispose.DisposeUtils; |
13 |
import org.gvsig.tools.exception.BaseException; |
14 |
|
15 |
|
16 |
/**
|
17 |
* @author fdiaz
|
18 |
*
|
19 |
*/
|
20 |
public abstract class AbstractTiledBand extends AbstractBand implements Band { |
21 |
|
22 |
protected Buffer data; //data loaded |
23 |
protected int firstRowOfTile; |
24 |
protected int firstColumnOfTile; |
25 |
protected int rowsPerTile; |
26 |
protected int columnsPerTile; |
27 |
protected BandTileManager tileManager;
|
28 |
private boolean loaded; |
29 |
protected int rowsInTile; |
30 |
protected int columnsInTile; |
31 |
|
32 |
/**
|
33 |
* @param rows
|
34 |
* @param columns
|
35 |
* @param noData
|
36 |
* @param tileManager
|
37 |
*/
|
38 |
public AbstractTiledBand(int rows, int columns, NoData noData, BandTileManager tileManager) { |
39 |
this.rows = rows;
|
40 |
this.columns = columns;
|
41 |
this.loaded=false; |
42 |
this.rowsPerTile=tileManager.getRowsPerTile();
|
43 |
this.columnsPerTile=tileManager.getColumnsPerTile();
|
44 |
this.rowsInTile=rowsPerTile;
|
45 |
this.columnsInTile=columnsPerTile;
|
46 |
|
47 |
if (noData == null) { |
48 |
this.noData = BufferLocator.getBufferManager().createNoData(null, null); |
49 |
} else {
|
50 |
this.noData = noData;
|
51 |
} |
52 |
this.tileManager = tileManager;
|
53 |
} |
54 |
|
55 |
@Override
|
56 |
protected void doCopyFrom(Band source) throws CopyFromBandException { |
57 |
if (this.getColumns() != source.getColumns() || this.getRows() != source.getRows() |
58 |
|| this.getDataType()!=source.getDataType() ) {
|
59 |
throw new CopyFromBandException(source, this); |
60 |
} |
61 |
Object rowBuffer = this.createRowBuffer(); |
62 |
for(int row = 0; row<=this.rows; row++){ |
63 |
source.fetchRow(row, rowBuffer); |
64 |
this.putRow(row, rowBuffer);
|
65 |
} |
66 |
} |
67 |
|
68 |
protected void loadTile(int row, int column) { |
69 |
//The tile to be loaded must be inside the band's limits
|
70 |
if (loaded &&
|
71 |
row >= firstRowOfTile && |
72 |
row < firstRowOfTile + rowsPerTile && |
73 |
column >= firstColumnOfTile && |
74 |
column < firstColumnOfTile + columnsPerTile |
75 |
) { |
76 |
return;
|
77 |
} |
78 |
if(loaded) {
|
79 |
saveCurrentTile(); |
80 |
} |
81 |
loaded=false;
|
82 |
|
83 |
int currentTileRow = row / rowsPerTile; //Divisi?n entera |
84 |
firstRowOfTile = currentTileRow * rowsPerTile; |
85 |
int currentTileColumn = column / columnsPerTile; //Divisi?n entera |
86 |
firstColumnOfTile = currentTileColumn * columnsPerTile; |
87 |
Band loadedBand = null;
|
88 |
try {
|
89 |
rowsInTile = rowsPerTile; |
90 |
if(firstRowOfTile + rowsPerTile > this.rows){ |
91 |
rowsInTile = this.rows - firstRowOfTile;
|
92 |
} |
93 |
columnsInTile = columnsPerTile; |
94 |
if(firstColumnOfTile + columnsPerTile > this.columns){ |
95 |
columnsInTile = this.columns - firstColumnOfTile;
|
96 |
} |
97 |
loadedBand = this.tileManager.load(firstRowOfTile,
|
98 |
firstColumnOfTile, |
99 |
this.getDataType());
|
100 |
Object rowBuffer = loadedBand.createRowBuffer();
|
101 |
for(int loadedRow=0; loadedRow<loadedBand.getRows(); loadedRow++){ |
102 |
loadedBand.fetchRow(loadedRow, rowBuffer); |
103 |
int pos = loadedRow*loadedBand.getColumns();
|
104 |
System.arraycopy(rowBuffer, 0, data.array(), pos, loadedBand.getColumns()); |
105 |
} |
106 |
loaded=true;
|
107 |
|
108 |
} catch (IOException e) { |
109 |
throw new RuntimeException("Can't load current tile", e); |
110 |
} finally {
|
111 |
DisposeUtils.dispose(loadedBand); |
112 |
} |
113 |
} |
114 |
|
115 |
protected void saveCurrentTile() { |
116 |
try {
|
117 |
int rowsInTile = rowsPerTile;
|
118 |
if(firstRowOfTile + rowsPerTile > this.rows){ |
119 |
rowsInTile = this.rows - firstRowOfTile;
|
120 |
} |
121 |
int columnsInTile = columnsPerTile;
|
122 |
if(firstColumnOfTile + columnsPerTile > this.columns){ |
123 |
columnsInTile = this.columns - firstColumnOfTile;
|
124 |
} |
125 |
this.tileManager.save(data,
|
126 |
firstRowOfTile, rowsInTile, |
127 |
firstColumnOfTile, columnsInTile, |
128 |
this.getDataType());
|
129 |
} catch (UnsupportedOperationException e) { |
130 |
// Do nothing, operation not supported
|
131 |
} catch (IOException e) { |
132 |
throw new RuntimeException("Can't save current tile", e); |
133 |
} |
134 |
} |
135 |
|
136 |
protected abstract int getDataSize(); |
137 |
|
138 |
@Override
|
139 |
public boolean isReadOnly() { |
140 |
if(this.tileManager==null){ |
141 |
return false; |
142 |
} |
143 |
return !this.tileManager.isSupportedSave(); |
144 |
} |
145 |
|
146 |
|
147 |
@Override
|
148 |
public boolean isPaginated() { |
149 |
//FIXME: ?????
|
150 |
return true; |
151 |
} |
152 |
|
153 |
@Override
|
154 |
public BandInfo getBandInfo() {
|
155 |
return this.tileManager == null ? null : this.tileManager.getBandInfo(); |
156 |
} |
157 |
|
158 |
/**
|
159 |
* Calculate buffer size than must be allocated.
|
160 |
* @param rows
|
161 |
* @param columns
|
162 |
* @return
|
163 |
*/
|
164 |
protected int calculateBufferSizeToAllocate(int rows, int columns){ |
165 |
// If the number of requested cells is less than the default number of
|
166 |
// cells, it is unnecessary to reserve the maximum possible size.
|
167 |
|
168 |
int allocateRows = rowsInTile;
|
169 |
if(rows<rowsInTile){
|
170 |
allocateRows = rows; |
171 |
} |
172 |
|
173 |
int allocateColumns = columnsInTile;
|
174 |
//FIXME: Comprobar si se puede utilizar la condici?n de abajo antes de descomentarizarla
|
175 |
// if(columns<columnsInTile){
|
176 |
// allocateColumns = columns;
|
177 |
// }
|
178 |
|
179 |
return allocateRows*allocateColumns;
|
180 |
} |
181 |
|
182 |
@Override
|
183 |
public void doDispose() throws BaseException { |
184 |
super.doDispose();
|
185 |
data = null;
|
186 |
//FIXME: ?hacer disposables los pageManagers?
|
187 |
tileManager = null;
|
188 |
} |
189 |
|
190 |
} |