gvsig-raster / org.gvsig.raster / branches / org.gvsig.raster_dataaccess_refactoring / org.gvsig.raster.lib / org.gvsig.raster.lib.impl / src / main / java / org / gvsig / raster / impl / datastruct / BandListImpl.java @ 2308
History | View | Annotate | Download (13.2 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
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., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
*/
|
22 |
package org.gvsig.raster.impl.datastruct; |
23 |
|
24 |
import java.util.ArrayList; |
25 |
import java.util.List; |
26 |
|
27 |
import org.gvsig.fmap.dal.coverage.dataset.Buffer; |
28 |
import org.gvsig.fmap.dal.coverage.datastruct.BandList; |
29 |
import org.gvsig.fmap.dal.coverage.datastruct.DatasetBand; |
30 |
import org.gvsig.fmap.dal.coverage.exception.BandNotFoundInListException; |
31 |
import org.gvsig.tools.ToolsLocator; |
32 |
import org.gvsig.tools.dynobject.DynStruct; |
33 |
import org.gvsig.tools.persistence.PersistenceManager; |
34 |
import org.gvsig.tools.persistence.Persistent; |
35 |
import org.gvsig.tools.persistence.PersistentState; |
36 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
37 |
|
38 |
/**
|
39 |
* Esta clase gestiona una lista de bandas que puede pertenecer
|
40 |
* a un fichero (la tiene un Dataset) o puede ser una lista
|
41 |
* de bandas de multiples ficheros. Las bandas son
|
42 |
* almacenadas en un array donde la posici?n en este es el n?mero de banda.
|
43 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
44 |
*
|
45 |
*/
|
46 |
public class BandListImpl implements BandList, Persistent { |
47 |
public static final String PERSISTENT_NAME = "BandListImpl_Persistent"; |
48 |
public static final String PERSISTENT_DESCRIPTION = "BandListImpl Persistent"; |
49 |
|
50 |
//Band array
|
51 |
private ArrayList<DatasetBand> bands = new ArrayList<DatasetBand>(); |
52 |
private int[] drawableBands = null; |
53 |
|
54 |
public BandListImpl() {
|
55 |
|
56 |
} |
57 |
|
58 |
public BandListImpl(String fName, int bandCount, int dataType) { |
59 |
int[] drawBands = new int[bandCount]; |
60 |
for (int i = 0; i < bandCount; i++) { |
61 |
try {
|
62 |
addBand(new DatasetBandImpl(fName, i, dataType, bandCount), i);
|
63 |
} catch (BandNotFoundInListException e1) {
|
64 |
} |
65 |
drawBands[i] = i; |
66 |
} |
67 |
setDrawableBands(drawBands); |
68 |
} |
69 |
|
70 |
/*
|
71 |
* (non-Javadoc)
|
72 |
* @see java.lang.Object#clone()
|
73 |
*/
|
74 |
public Object clone() { |
75 |
BandListImpl result = new BandListImpl();
|
76 |
|
77 |
ArrayList<DatasetBand> b = new ArrayList<DatasetBand>(); |
78 |
for (int i = 0; i < bands.size(); i++) |
79 |
b.add((DatasetBand)((DatasetBandImpl)bands.get(i)).clone()); |
80 |
result.setBandArray(b); |
81 |
|
82 |
if(drawableBands != null) { |
83 |
int[] drawBands = new int[drawableBands.length]; |
84 |
for (int i = 0; i < drawableBands.length; i++) |
85 |
drawBands[i] = drawableBands[i]; |
86 |
result.setDrawableBands(drawBands); |
87 |
} |
88 |
|
89 |
return result;
|
90 |
} |
91 |
|
92 |
/**
|
93 |
* Asigna el ?ltimo array de bandas a escribir solicitadas.
|
94 |
* @param drawableBands
|
95 |
*/
|
96 |
public void setDrawableBands(int[] drawableBands) { |
97 |
clearDrawableBands(); |
98 |
this.drawableBands = drawableBands;
|
99 |
for(int i = 0; i< drawableBands.length; i++) { |
100 |
if(drawableBands[i] < 0 || drawableBands[i] >= getBandCount()) |
101 |
continue;
|
102 |
addDrawableBand(i, drawableBands[i]); |
103 |
} |
104 |
} |
105 |
|
106 |
/**
|
107 |
* Asigna el array de bandas
|
108 |
* @param bands
|
109 |
*/
|
110 |
public void setBandArray(ArrayList<DatasetBand> bands) { |
111 |
this.bands = bands;
|
112 |
} |
113 |
|
114 |
/**
|
115 |
* Obtiene el ?ltimo array de bandas a escribir solicitadas.
|
116 |
* @return
|
117 |
*/
|
118 |
public int[] getDrawableBands() { |
119 |
return drawableBands;
|
120 |
} |
121 |
|
122 |
/**
|
123 |
* Encuentra una banda en la lista.
|
124 |
* @param file Fichero al que pertenece la banda buscada.
|
125 |
* @param pos Posici?n que ocupa en el fichero.
|
126 |
* @return true si se ha hallado la banda y false si no se
|
127 |
* ha encontrado
|
128 |
*/
|
129 |
public boolean findBand(DatasetBand band) { |
130 |
for(int i = 0; i < bands.size(); i++) { |
131 |
DatasetBand b = (DatasetBand)bands.get(i); |
132 |
if( b.getFileName().equals(band.getFileName()) &&
|
133 |
b.getPosition() == band.getPosition()) |
134 |
return true; |
135 |
} |
136 |
return false; |
137 |
} |
138 |
|
139 |
/**
|
140 |
* Obtiene la lista de bandas a pintar sobre el buffer de
|
141 |
* salida en forma de array. Cada elemento del array es una banda
|
142 |
* del RasterBuf de salida que ha de dibujarse y el valor que contiene
|
143 |
* ese elemento es la banda de la imagen que va pintada en esa banda
|
144 |
* del RasterBuf.
|
145 |
* <table border=1>
|
146 |
* <tr><td>Elem</td><td>Valor</td></tr>
|
147 |
* <tr><td>0</td><td>1</td></tr>
|
148 |
* <tr><td>1</td><td>0</td></tr>
|
149 |
* <tr><td>2</td><td>-1</td></tr>
|
150 |
* <tr><td>3</td><td>0</td></tr>
|
151 |
* <tr><td>4</td><td>2</td></tr>
|
152 |
* </table>
|
153 |
* El RasterBuf tendra en la banda 0 dibujada la banda 1 de este GeoRasterFile,
|
154 |
* en la banda 1 la 0 del dataset, en la banda 2 no habr? ninguna de este dataset,
|
155 |
* en la 3 la 0 y en la 4 la 2 de este GeoRasterFile.
|
156 |
* @return lista de bandas a dibujar o un array de un elemento con valor -1.
|
157 |
*/
|
158 |
public int[] bandsToDrawList(int nBandsDataImage) { |
159 |
|
160 |
int[] bandsToRead = new int[nBandsDataImage]; |
161 |
for(int dataImageBand = 0;dataImageBand < nBandsDataImage;dataImageBand++) { |
162 |
bandsToRead[dataImageBand] = -1;
|
163 |
for(int band = 0;band < bands.size();band++) { |
164 |
if( ((DatasetBand)bands.get(band)) != null && |
165 |
((DatasetBand)bands.get(band)).getBufferBandListToDraw() != null) {
|
166 |
for(int dest=0;dest<((DatasetBand)bands.get(band)).getBufferBandListToDraw().length;dest++) { |
167 |
if(dataImageBand == ((DatasetBand)bands.get(band)).getBufferBandListToDraw()[dest])
|
168 |
bandsToRead[dataImageBand] = band; |
169 |
} |
170 |
} |
171 |
} |
172 |
} |
173 |
|
174 |
return bandsToRead;
|
175 |
} |
176 |
|
177 |
/**
|
178 |
* Checkea si alguna banda de la lista se est?a dibujando en el buffer de salida.
|
179 |
* @return true si alguna banda se est? dibujando y false si no
|
180 |
*/
|
181 |
public boolean isDrawingAnyBand() { |
182 |
for(int band = 0;band < bands.size();band++) { |
183 |
if(((DatasetBand)bands.get(band)).isDrawing())
|
184 |
return true; |
185 |
} |
186 |
return false; |
187 |
} |
188 |
|
189 |
//******************************
|
190 |
//Setters and Getters
|
191 |
//******************************
|
192 |
|
193 |
|
194 |
public void addBand(DatasetBand b, int pos)throws BandNotFoundInListException { |
195 |
bands.add(b); |
196 |
} |
197 |
|
198 |
public void addBand(DatasetBand b)throws BandNotFoundInListException { |
199 |
bands.add(b); |
200 |
} |
201 |
|
202 |
/**
|
203 |
* A?ade la lista de bandas pasada por par?metro a la lista
|
204 |
* actual. Si alguna banda ya existe no la a?ade y continua.
|
205 |
* @param bl Lista de bandas
|
206 |
*/
|
207 |
public void addBandList(BandList bl) { |
208 |
for(int i = 0; i < bl.getBandCount(); i++) { |
209 |
if(!findBand(bl.getBand(i)))
|
210 |
bands.add(bl.getBand(i)); |
211 |
} |
212 |
} |
213 |
|
214 |
/**
|
215 |
* ELimina todas las bandas que tienen un nombre determinado.
|
216 |
* @param name Nombre de las bandas a eliminar
|
217 |
*/
|
218 |
public void removeBands(String name) { |
219 |
for(int i = 0; i < getBandCount(); i++) { |
220 |
DatasetBand band = getBand(i); |
221 |
if(band.getFileName().equals(name))
|
222 |
bands.remove(i); |
223 |
} |
224 |
} |
225 |
|
226 |
/**
|
227 |
* Resetea la asignaci?n de dibujado de las bandas de la imagen
|
228 |
* sobre el DataImage cuando se hace un update para esta banda.
|
229 |
*/
|
230 |
public void clearDrawableBands() { |
231 |
drawableBands = null;
|
232 |
for(int i = 0; i < getBandCount(); i++) |
233 |
((DatasetBand)bands.get(i)).clearDrawableBands(); |
234 |
} |
235 |
|
236 |
/**
|
237 |
* Para este GeoRasterFile asigna que bandas se pintaran
|
238 |
* sobre el RasterBuf cuando se haga un update. Especificamos a
|
239 |
* trav?s de los par?metros para que posici?n del RasterBuf ir?
|
240 |
* dibujada con que banda del fichero de imagen.
|
241 |
* @param posRasterBuf Posici?n del RasterBuf que queremos pintar.
|
242 |
* @param imageBand Banda de la imagen que se pintar?
|
243 |
*/
|
244 |
public void addDrawableBand(int posRasterBuf, int imageBand) { |
245 |
try {
|
246 |
((DatasetBand)bands.get(imageBand)).addPositionToDrawInBuffer(posRasterBuf); |
247 |
} catch(IndexOutOfBoundsException exc) { |
248 |
//No hacemos nada simplemente no inserta la banda.
|
249 |
} |
250 |
} |
251 |
|
252 |
/**
|
253 |
* Obtiene el n?mero de bandas de un RasterBuf sobre las que se pintara
|
254 |
* alguna banda de este fichero cuando se llama a un updateBuffer.
|
255 |
* @return N?mero de bandas. Cero en caso de no tener ninguna asignada.
|
256 |
*/
|
257 |
public int getDrawableBandsCount() { |
258 |
int nbands = 0; |
259 |
for(int i = 0;i < bands.size();i++) { |
260 |
DatasetBand b = (DatasetBand)bands.get(i); |
261 |
if(b.getBufferBandListToDraw() != null) |
262 |
nbands += b.getBufferBandListToDraw().length; |
263 |
} |
264 |
return nbands;
|
265 |
} |
266 |
|
267 |
/**
|
268 |
* Obtiene la banda de la posici?n i.
|
269 |
* @param i Posici?n de la banda a obtener.
|
270 |
* @return Banda.
|
271 |
*/
|
272 |
public DatasetBand getBand(int i) { |
273 |
if(i < 0 || i >= bands.size()) |
274 |
return null; |
275 |
return (DatasetBand)bands.get(i);
|
276 |
} |
277 |
|
278 |
/**
|
279 |
* Obtiene el n?mero de bandas.
|
280 |
* @return entero con el n?mero de bandas.
|
281 |
*/
|
282 |
public int getBandCount() { |
283 |
return bands.size();
|
284 |
} |
285 |
|
286 |
/*
|
287 |
* (non-Javadoc)
|
288 |
* @see org.gvsig.fmap.dal.coverage.datastruct.BandList#getBufferBandToDraw(java.lang.String, int)
|
289 |
*/
|
290 |
public int[] getBufferBandToDraw(String fileName, int band) { |
291 |
for(int i = 0; i < bands.size(); i++) { |
292 |
if(((DatasetBand)bands.get(i)).getFileName().equals(fileName))
|
293 |
if(((DatasetBand)bands.get(i)).getPosition() == band)
|
294 |
return ((DatasetBand)bands.get(i)).getBufferBandListToDraw();
|
295 |
} |
296 |
int[] r = {-1}; |
297 |
return r;
|
298 |
} |
299 |
|
300 |
/*
|
301 |
* (non-Javadoc)
|
302 |
* @see org.gvsig.fmap.dal.coverage.datastruct.BandList#getLocalBufferBandToDraw(java.lang.String, int)
|
303 |
*/
|
304 |
public int[] getLocalBufferBandToDraw(String fileName, int band) { |
305 |
for(int i = 0; i < bands.size(); i++) { |
306 |
if(((DatasetBand)bands.get(i)).getFileName().equals(fileName))
|
307 |
if(((DatasetBand)bands.get(i)).getPosition() == band)
|
308 |
return ((DatasetBand)bands.get(i)).getLocalBufferBandListToDraw();
|
309 |
} |
310 |
int[] r = {-1}; |
311 |
return r;
|
312 |
} |
313 |
|
314 |
/**
|
315 |
* Obtiene en un array de String la lista de nombres de ficheros
|
316 |
* @return lista de nombres de los ficheros del GeoRasterMultiFile
|
317 |
*/
|
318 |
public String[] getBandStringList() { |
319 |
String[] list = new String[bands.size()]; |
320 |
for(int i = 0; i < bands.size(); i++) |
321 |
list[i] = ((DatasetBand)bands.get(i)).getFileName(); |
322 |
return list;
|
323 |
} |
324 |
|
325 |
/**
|
326 |
* Obtiene en un array de enteros con la lista de la posici?n de bandas
|
327 |
* @return lista de la posici?n de bandas
|
328 |
*/
|
329 |
public int[] getBandPositionList() { |
330 |
int[] list = new int[bands.size()]; |
331 |
for(int i = 0; i < bands.size(); i++) |
332 |
list[i] = ((DatasetBand)bands.get(i)).getPosition(); |
333 |
return list;
|
334 |
} |
335 |
|
336 |
/**
|
337 |
* Obtiene el tipo de dato de las bandas. Esta llamada supone que todos los tipos de dato
|
338 |
* de las bandas son igual por lo que devolver? el primero.
|
339 |
* @return Entero que representa el tipo de datos de las bandas
|
340 |
*/
|
341 |
public int getBandsDataType() { |
342 |
if(bands.size() <= 0) |
343 |
return Buffer.TYPE_UNDEFINED; |
344 |
return ((DatasetBand)bands.get(0)).getDataType(); |
345 |
} |
346 |
|
347 |
/**
|
348 |
* Obtiene la posici?n del fichero en la lista a partir del nombre
|
349 |
* @param fileName Nombre del fichero
|
350 |
* @return Posici?n del fichero o -1 si no existe.
|
351 |
*/
|
352 |
public int getFileNumber(String fileName) { |
353 |
if(fileName == null) |
354 |
return -1; |
355 |
String aux = ""; |
356 |
int count = -1; |
357 |
for(int i = 0; i < bands.size(); i++) { |
358 |
if(((DatasetBand)bands.get(i)).getFileName().indexOf(aux) != 0) { |
359 |
count ++; |
360 |
aux = ((DatasetBand)bands.get(i)).getFileName(); |
361 |
} |
362 |
if(((DatasetBand)bands.get(i)).getFileName().indexOf(fileName) == 0) |
363 |
return count;
|
364 |
} |
365 |
return -1; |
366 |
} |
367 |
|
368 |
/**
|
369 |
* Limpia la lista de bandas
|
370 |
*/
|
371 |
public void clear() { |
372 |
bands.clear(); |
373 |
drawableBands = null;
|
374 |
} |
375 |
|
376 |
/**
|
377 |
*Muestra la lista de bandas en modo texto
|
378 |
*/
|
379 |
public void show() { |
380 |
System.out.println("BandCount: " + getBandCount()); |
381 |
System.out.println("DrawableBandsCount: " + getDrawableBandsCount()); |
382 |
for (int i = 0; i < getBandCount(); i++) { |
383 |
System.out.println(""); |
384 |
System.out.println("***********************"); |
385 |
System.out.println("Band: " + i); |
386 |
System.out.println("DataType: " + ((DatasetBand)getBand(i)).getDataType()); |
387 |
System.out.println("FileName: " + ((DatasetBand)getBand(i)).getFileName()); |
388 |
System.out.println("Position: " + ((DatasetBand)getBand(i)).getPosition()); |
389 |
if(((DatasetBand)getBand(i)).getBufferBandListToDraw() != null) { |
390 |
System.out.print("Band Dst: "); |
391 |
for (int j = 0; j < ((DatasetBand)getBand(i)).getBufferBandListToDraw().length; j++) |
392 |
System.out.print(((DatasetBand)getBand(i)).getBufferBandListToDraw()[j] + " "); |
393 |
} |
394 |
System.out.println();
|
395 |
} |
396 |
|
397 |
} |
398 |
|
399 |
@SuppressWarnings("unchecked") |
400 |
public void loadFromState(PersistentState state) |
401 |
throws PersistenceException {
|
402 |
List<DatasetBand> rois = state.getList("bands"); |
403 |
this.bands = new ArrayList<DatasetBand>(); |
404 |
this.bands.addAll(rois);
|
405 |
|
406 |
drawableBands = (int[])state.getIntArray("drawableBands"); |
407 |
} |
408 |
|
409 |
public void saveToState(PersistentState state) throws PersistenceException { |
410 |
state.set("bands", bands);
|
411 |
state.set("drawableBands", drawableBands);
|
412 |
} |
413 |
|
414 |
public static void registerPersistence() { |
415 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
416 |
DynStruct definition = manager.getDefinition(PERSISTENT_NAME); |
417 |
if( definition == null ) { |
418 |
definition = manager.addDefinition( |
419 |
BandListImpl.class, |
420 |
PERSISTENT_NAME, |
421 |
PERSISTENT_DESCRIPTION, |
422 |
null,
|
423 |
null
|
424 |
); |
425 |
definition.addDynFieldList("bands").setClassOfItems(DatasetBand.class).setMandatory(true); |
426 |
definition.addDynFieldList("drawableBands").setClassOfItems(int.class).setMandatory(false); |
427 |
} |
428 |
} |
429 |
|
430 |
} |