svn-gvsig-desktop / trunk / libraries / libRaster / src / org / gvsig / raster / grid / filter / RasterFilterList.java @ 11892
History | View | Annotate | Download (13.8 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2006 IVER T.I. and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*/
|
19 |
package org.gvsig.raster.grid.filter; |
20 |
|
21 |
import java.util.ArrayList; |
22 |
import java.util.Stack; |
23 |
import java.util.TreeMap; |
24 |
|
25 |
import org.gvsig.raster.dataset.IBuffer; |
26 |
|
27 |
|
28 |
/**
|
29 |
* Esta clase representa la lista de filtros que debe ser manejada desde el
|
30 |
* RasterFilterListManager.
|
31 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
32 |
*
|
33 |
*/
|
34 |
public class RasterFilterList { |
35 |
public final static int UNDEFINED = -1; |
36 |
public final static int ADD_END = 0; |
37 |
public final static int ADD_END_AND_DELETE = 1; |
38 |
public final static int MODIFY_LAST = 2; |
39 |
|
40 |
private IBuffer rasterBuf = null; |
41 |
private int typeFilter = -1; |
42 |
private TreeMap params = new TreeMap(); |
43 |
|
44 |
//Pila de objetos Filter (Contiene un RasterFilter)
|
45 |
private ArrayList list = new ArrayList(); |
46 |
|
47 |
private Stack status = new Stack(); |
48 |
|
49 |
|
50 |
/**
|
51 |
* A?ade un par?metro a la lista de filtros. Estos par?metros luego
|
52 |
* pueden ser utilizados por los managers que se registren
|
53 |
* @param key Nombre del par?metro que coincide con el nombre de la clase.
|
54 |
* @param value Objeto
|
55 |
*/
|
56 |
public void addParam(String key, Object value) { |
57 |
params.put(key, value); |
58 |
} |
59 |
|
60 |
/**
|
61 |
* Obtiene un par?metro de la lista de filtros.
|
62 |
* @param key Identificador del par?metro. Coincide con el nombre de la clase del par?metro.
|
63 |
*/
|
64 |
public Object getParam(String key) { |
65 |
return params.get(key);
|
66 |
} |
67 |
|
68 |
/**
|
69 |
* A?ade un filtro al final de la lista
|
70 |
* @param filter filtro a?adido
|
71 |
*/
|
72 |
public void add(RasterFilter filter) { |
73 |
list.add(filter); |
74 |
} |
75 |
|
76 |
/**
|
77 |
* A?ade un filtro en la lista en la posici?n indicada.
|
78 |
* @param filter filtro a?adido
|
79 |
* @param pos posici?n
|
80 |
*/
|
81 |
public void add(RasterFilter filter, int pos) { |
82 |
try {
|
83 |
list.add(pos, filter); |
84 |
} catch (IndexOutOfBoundsException e) { |
85 |
add(filter); |
86 |
} |
87 |
} |
88 |
|
89 |
/**
|
90 |
* Elimina un filtro a partir de su nombre
|
91 |
* @param name Nombre del filtro a eliminar
|
92 |
*/
|
93 |
public void remove(String name) { |
94 |
for (int i = list.size() - 1; i >= 0; i--){ |
95 |
if(((RasterFilter)list.get(i)).getName().equals(name))
|
96 |
list.remove(i); |
97 |
} |
98 |
} |
99 |
|
100 |
/**
|
101 |
* Elimina un filtro concreto de la pila
|
102 |
* @param type
|
103 |
*/
|
104 |
public void remove(RasterFilter filter) { |
105 |
for (int i = list.size() - 1; i >= 0; i--) { |
106 |
if (((RasterFilter) list.get(i)).equals(filter))
|
107 |
list.remove(i); |
108 |
} |
109 |
} |
110 |
|
111 |
/**
|
112 |
* Elimina un filtro por clase.
|
113 |
* @param baseFilterClass
|
114 |
*/
|
115 |
public void remove(Class baseFilterClass) { |
116 |
for(int i = 0; i < lenght(); i++) { |
117 |
if(baseFilterClass.isInstance(((RasterFilter) list.get(i)))) {
|
118 |
list.remove(i); |
119 |
i --; |
120 |
} |
121 |
} |
122 |
} |
123 |
|
124 |
/**
|
125 |
* Devuelve el tipo de dato de retorno al aplicar la pila de filtros
|
126 |
* @return
|
127 |
*/
|
128 |
public int getOutDataType() { |
129 |
if(list.size() > 0) |
130 |
return ((RasterFilter)list.get(list.size() - 1)).getOutRasterDataType(); |
131 |
else
|
132 |
return rasterBuf.getDataType();
|
133 |
} |
134 |
|
135 |
/**
|
136 |
* Devuelve el raster resultado de la aplicacion de la pila de filtros
|
137 |
* @return
|
138 |
*/
|
139 |
public IBuffer getResult() {
|
140 |
return rasterBuf;
|
141 |
} |
142 |
|
143 |
/**
|
144 |
* Devuelve true si el filtro 'a' est? por encima del 'b' en la lista y false si no lo est?
|
145 |
* @param a Nombre de filtro
|
146 |
* @param b Nombre de filtro
|
147 |
* @return
|
148 |
*/
|
149 |
private boolean isOnTop(String a, String b) { |
150 |
int posA = -1; |
151 |
int posB = -1; |
152 |
|
153 |
for (int i = 0; i < list.size(); i++) { |
154 |
if (list.get(i).equals(a))
|
155 |
posA = i; |
156 |
|
157 |
if (list.get(i).equals(b))
|
158 |
posB = i; |
159 |
|
160 |
if ((posA != -1) && (posB != -1)) |
161 |
break;
|
162 |
} |
163 |
|
164 |
if (posA < posB)
|
165 |
return true; |
166 |
else
|
167 |
return false; |
168 |
} |
169 |
|
170 |
/**
|
171 |
* Dado un modo de inserci?n se obtiene el tipo de dato del filtro a introducir.
|
172 |
* Se insertionMode es ADD_END quiere decir que se a?adir? al final de la lista
|
173 |
* por lo que devuelve el tipo de la salida del ?ltimo filtro. Si es ADD_END_AND_DELETE
|
174 |
* lo mismo y si es MODIFY_LAST quiere decir que buscar? si hay alguno de este tipo y
|
175 |
* si lo hay devuelve el tipo de dato del anterior filtro en la lista.
|
176 |
*
|
177 |
* @param name Nombre del filtro a insertar en la lista. Este debe corresponder con la variable
|
178 |
* genericName definida para cada tipo de filtro
|
179 |
*
|
180 |
* @param insertionMode Modo de inserci?n en la lista. Debe corresponder con alguna de las
|
181 |
* constantes definidas en RasterFilterList ADD_END, ADD_END_AND_DELETE, MODIFY_LAST o UNDEFINED
|
182 |
*
|
183 |
*/
|
184 |
public int getDataTypeInFilter(String name, int insertionMode) { |
185 |
switch(insertionMode) {
|
186 |
|
187 |
//Devuelve el tipo de salida del primer filtro que sea != del q se quiere insertar
|
188 |
case ADD_END_AND_DELETE:
|
189 |
for(int i = list.size() - 1; i >= 0; i--) { |
190 |
if(((RasterFilter) list.get(i)).getName().compareTo(name) != 0) |
191 |
return ((RasterFilter) list.get(i)).getOutRasterDataType();
|
192 |
} |
193 |
return getInitDataType();
|
194 |
|
195 |
//Devuelve el tipo del filtro anterior al ?ltimo que coincide con el tipo pasado. Si no existe
|
196 |
//devolver? el tipo de salida del ?ltimo filtro de la lista
|
197 |
case MODIFY_LAST:
|
198 |
for(int i = list.size() - 1; i >= 0; i--) { |
199 |
if(((RasterFilter) list.get(i)).getName().compareTo(name) == 0) { |
200 |
if(i > 0) |
201 |
return ((RasterFilter) list.get(i - 1)).getOutRasterDataType(); |
202 |
else
|
203 |
return getInitDataType();
|
204 |
} |
205 |
} |
206 |
|
207 |
//Devuelve el tipo de salida del ?ltimo filtro de la lista
|
208 |
case ADD_END:
|
209 |
if(list.size() == 0) |
210 |
return getInitDataType();
|
211 |
else
|
212 |
return ((RasterFilter) list.get(list.size() - 1)).getOutRasterDataType(); |
213 |
} |
214 |
|
215 |
return getInitDataType();
|
216 |
} |
217 |
|
218 |
/**
|
219 |
* Obtiene la cantidad de filtros en la lista
|
220 |
* @return N?mero de filtros apilados
|
221 |
*/
|
222 |
public int lenght() { |
223 |
return list.size();
|
224 |
} |
225 |
|
226 |
/**
|
227 |
* Obtiene el filtro apilado de la posici?n i o null si el indice es incorrecto
|
228 |
* @param i Posici?n a acceder en la pila
|
229 |
* @return Filtro
|
230 |
*/
|
231 |
public RasterFilter get(int i) { |
232 |
if(i >= list.size() || i < 0) |
233 |
return null; |
234 |
return ((RasterFilter) list.get(i));
|
235 |
} |
236 |
|
237 |
/**
|
238 |
* Obtiene el filtro apilado de nombre name o null si no existe
|
239 |
* @param i Nombre del filtro buscado
|
240 |
* @return Filtro
|
241 |
*/
|
242 |
public RasterFilter get(String name) { |
243 |
for (int i = list.size() - 1; i >= 0; i--){ |
244 |
if(((RasterFilter) list.get(i)).getName().equals(name))
|
245 |
return ((RasterFilter) list.get(i));
|
246 |
} |
247 |
return null; |
248 |
} |
249 |
|
250 |
/**
|
251 |
* Obtiene el filtro apilado que corresponde con el nombre
|
252 |
* @param name Nombre de filtro
|
253 |
* @return Filtro en caso de que exista un filtro apilado de ese tipo
|
254 |
* o null si no hay ninguno.
|
255 |
*/
|
256 |
public RasterFilter getByName(String name) { |
257 |
for(int i = 0; i < lenght(); i++) { |
258 |
if(((RasterFilter) list.get(i)).getName().equals(name))
|
259 |
return ((RasterFilter) list.get(i));
|
260 |
} |
261 |
return null; |
262 |
} |
263 |
|
264 |
/**
|
265 |
* Obtiene el primer filtro de la lista que es instancia de la clase pasada por
|
266 |
* par?metro
|
267 |
* @param baseFilterClass Filtro base
|
268 |
* @return RasterFilter
|
269 |
*/
|
270 |
public RasterFilter getFilterByBaseClass(Class baseFilterClass) { |
271 |
for(int i = 0; i < lenght(); i++) { |
272 |
if(baseFilterClass.isInstance(((RasterFilter) list.get(i))))
|
273 |
return ((RasterFilter) list.get(i));
|
274 |
} |
275 |
return null; |
276 |
} |
277 |
|
278 |
/**
|
279 |
* Obtiene el tipo del filtro de la pila de la posici?n i
|
280 |
* @param i Posici?n a acceder en la pila
|
281 |
* @return tipo de filtro
|
282 |
*/
|
283 |
public String getName(int i) { |
284 |
return ((RasterFilter) list.get(i)).getName();
|
285 |
} |
286 |
|
287 |
/**
|
288 |
* Elimina todos los filtros de la pila
|
289 |
*/
|
290 |
public void clear() { |
291 |
list.clear(); |
292 |
} |
293 |
|
294 |
/**
|
295 |
* Sustituye un filtro de una posici?n de la pila por otro
|
296 |
* @param filter
|
297 |
* @param i
|
298 |
*/
|
299 |
public void replace(RasterFilter filter, String name) { |
300 |
for (int i = list.size() - 1; i >= 0; i--) { |
301 |
if(((RasterFilter) list.get(i)).getName().equals(name)){
|
302 |
list.remove(i); |
303 |
list.add(i, filter); |
304 |
} |
305 |
} |
306 |
} |
307 |
|
308 |
/**
|
309 |
* Sustituye un filtro de una posici?n de la pila por otro
|
310 |
* @param filter
|
311 |
* @param i
|
312 |
*/
|
313 |
public void replace(RasterFilter filter, int i) { |
314 |
list.remove(i); |
315 |
list.add(i, filter); |
316 |
} |
317 |
|
318 |
/**
|
319 |
* Asigna el raster de entrada inicial
|
320 |
* @param raster
|
321 |
*/
|
322 |
public void setInitRasterBuf(IBuffer raster) { |
323 |
rasterBuf = (IBuffer) raster; |
324 |
typeFilter = rasterBuf.getDataType(); |
325 |
} |
326 |
|
327 |
/**
|
328 |
* Devuelve el tipo de datos inicial de la lista
|
329 |
* @return Tipo de dato del raster inicial
|
330 |
*/
|
331 |
public int getInitDataType() { |
332 |
return typeFilter;
|
333 |
} |
334 |
|
335 |
/**
|
336 |
* Asigna el tipo de dato inicial
|
337 |
* @param dt
|
338 |
*/
|
339 |
public void setInitDataType(int dt) { |
340 |
this.typeFilter = dt;
|
341 |
} |
342 |
|
343 |
/**
|
344 |
* M?todo que devuelve true si el tipo de filtro pasado por par?metro est? en la
|
345 |
* pila y false si no lo est?.
|
346 |
* @param type Tipo de par?metro a comprobar
|
347 |
* @return true si est? en la pila y false si no lo est?
|
348 |
*/
|
349 |
public boolean isActive(String name) { |
350 |
for (int i = list.size() - 1; i >= 0; i--) { |
351 |
if (((RasterFilter) list.get(i)).getName().equals(name))
|
352 |
return true; |
353 |
} |
354 |
return false; |
355 |
} |
356 |
|
357 |
/**
|
358 |
* M?todo que devuelve true si el tipo de filtro pasado por par?metro est? en la
|
359 |
* pila y false si no lo est?.
|
360 |
* @param filter Tipo de filtro a comprobar
|
361 |
* @return true si est? en la pila y false si no lo est?
|
362 |
*/
|
363 |
public boolean isActive(RasterFilter filter) { |
364 |
for (int i = list.size() - 1; i >= 0; i--) { |
365 |
if (((RasterFilter) list.get(i)).equals(filter)) {
|
366 |
return true; |
367 |
} |
368 |
} |
369 |
return false; |
370 |
} |
371 |
|
372 |
/**
|
373 |
* Devuelve la posici?n en la lista de una clase de filtro concreta
|
374 |
* @param c Clase a buscar en la lista
|
375 |
* @return posici?n en la lista
|
376 |
*/
|
377 |
public int getPosition(Class c) { |
378 |
for(int i = 0; i < list.size(); i++) |
379 |
if(c.isInstance(list.get(i)))
|
380 |
return i;
|
381 |
return -1; |
382 |
} |
383 |
|
384 |
/**
|
385 |
* Aplica los filtros de la pila sobre el buffer correspondiente
|
386 |
* @param dataType
|
387 |
*/
|
388 |
private void executeFilterByDataType(int dataType) { |
389 |
for (int i = 0; i < list.size(); i++) { |
390 |
RasterFilter filter = ((RasterFilter) list.get(i)); |
391 |
|
392 |
filter.addParam("raster", rasterBuf);
|
393 |
filter.execute(); |
394 |
|
395 |
if (filter.getResult("raster") != null) |
396 |
this.rasterBuf = (IBuffer) filter.getResult("raster"); |
397 |
} |
398 |
} |
399 |
|
400 |
/**
|
401 |
* Aplica los filtros sobre un RasterBuf
|
402 |
* @return IBuffer de salida
|
403 |
*/
|
404 |
public IBuffer execute() {
|
405 |
if (rasterBuf == null) |
406 |
return null; |
407 |
executeFilterByDataType(rasterBuf.getDataType()); |
408 |
return rasterBuf;
|
409 |
} |
410 |
|
411 |
/**
|
412 |
* Muestra el contenido de la pila de filtros para depuraci?n
|
413 |
*/
|
414 |
public void show() { |
415 |
System.out.println("--------------------------------------------"); |
416 |
|
417 |
for (int i = list.size() - 1; i >= 0; i--) { |
418 |
System.out.println("FILTRO:" + i + " NAME:" + ((RasterFilter) list.get(i)).getName() + " FIL:" + ((RasterFilter) list.get(i)).toString()); |
419 |
} |
420 |
} |
421 |
|
422 |
public void resetPercent() { |
423 |
for (int i = 0; i < list.size(); i++) |
424 |
((RasterFilter) list.get(i)).resetPercent(); |
425 |
} |
426 |
|
427 |
public int getPercent() { |
428 |
int percent = 0; |
429 |
if (list.size() == 0) |
430 |
return 0; |
431 |
for (int i = 0; i < list.size(); i++) |
432 |
percent += ((RasterFilter) list.get(i)).getPercent(); |
433 |
|
434 |
percent = percent / list.size(); |
435 |
return percent;
|
436 |
} |
437 |
|
438 |
/**
|
439 |
* Metodo para poder cancelar el proceso de calculo de histograma.
|
440 |
*/
|
441 |
public void setCanceled(boolean value) { |
442 |
for (int i = 0; i < list.size(); i++) |
443 |
((RasterFilter) list.get(i)).setCanceled(value); |
444 |
} |
445 |
|
446 |
/**
|
447 |
* Metodo para saber si se ha cancelado un proceso de calculo de histograma
|
448 |
* @return boolean
|
449 |
*/
|
450 |
public boolean isCanceled(int process) { |
451 |
for (int i = 0; i < list.size(); i++) |
452 |
if (((RasterFilter) list.get(i)).isCanceled())
|
453 |
return true; |
454 |
return false; |
455 |
} |
456 |
|
457 |
/**
|
458 |
* Guarda el estado de la lista de filtros en una pila, que se podr?
|
459 |
* ir recuperando con popStatus()
|
460 |
*/
|
461 |
public void pushStatus() { |
462 |
status.push(getStatusCloned()); |
463 |
} |
464 |
|
465 |
/**
|
466 |
* Obtiene el estado actual de los filtros, el ArrayList devuelto es una
|
467 |
* clonaci?n del original, asi no compartiran datos.
|
468 |
* @return
|
469 |
*/
|
470 |
public ArrayList getStatusCloned() { |
471 |
ArrayList newArray = new ArrayList(); |
472 |
for (int i = 0; i < list.size(); i++) { |
473 |
try {
|
474 |
newArray.add(((RasterFilter) list.get(i)).clone()); |
475 |
} catch (CloneNotSupportedException e) { |
476 |
System.out.println("No se ha podido clonar"); |
477 |
} |
478 |
} |
479 |
return newArray;
|
480 |
} |
481 |
|
482 |
/**
|
483 |
* Define el estado actual de los filtros
|
484 |
* @param newArray
|
485 |
*/
|
486 |
public void setStatus(ArrayList newArray) { |
487 |
list.clear(); |
488 |
for (int i = 0; i < newArray.size(); i++) { |
489 |
list.add((RasterFilter) newArray.get(i)); |
490 |
} |
491 |
} |
492 |
|
493 |
/**
|
494 |
* Recupera un estado guardado con antelaci?n mediante el m?todo pushStatus()
|
495 |
*/
|
496 |
public void popStatus() { |
497 |
if (status.size() <= 0) |
498 |
return;
|
499 |
|
500 |
setStatus((ArrayList) status.pop());
|
501 |
} |
502 |
} |