svn-gvsig-desktop / trunk / libraries / libRaster / src / org / gvsig / raster / grid / filter / RasterFilterList.java @ 11864
History | View | Annotate | Download (13.8 KB)
1 | 10740 | nacho | /* 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 | 11864 | bsanchez | import java.util.Stack; |
23 | 11716 | nacho | import java.util.TreeMap; |
24 | 10740 | nacho | |
25 | 10939 | nacho | import org.gvsig.raster.dataset.IBuffer; |
26 | 11818 | nacho | |
27 | |||
28 | 10740 | nacho | /**
|
29 | * Esta clase representa la lista de filtros que debe ser manejada desde el
|
||
30 | * RasterFilterListManager.
|
||
31 | * @author Nacho Brodin (nachobrodin@gmail.com)
|
||
32 | 11818 | nacho | *
|
33 | 10740 | nacho | */
|
34 | public class RasterFilterList { |
||
35 | 11767 | nacho | 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 | 10740 | nacho | |
40 | 11818 | nacho | 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 | 11864 | bsanchez | |
47 | private Stack status = new Stack(); |
||
48 | 10740 | nacho | |
49 | 11811 | bsanchez | |
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 | 10740 | nacho | |
68 | 11818 | nacho | /**
|
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 | 10740 | nacho | |
124 | 11811 | bsanchez | /**
|
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 | 10740 | nacho | |
135 | 11811 | bsanchez | /**
|
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 | 10740 | nacho | |
143 | 11811 | bsanchez | /**
|
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 | 10740 | nacho | |
153 | 11811 | bsanchez | for (int i = 0; i < list.size(); i++) { |
154 | if (list.get(i).equals(a))
|
||
155 | posA = i; |
||
156 | 10740 | nacho | |
157 | 11811 | bsanchez | if (list.get(i).equals(b))
|
158 | posB = i; |
||
159 | 10740 | nacho | |
160 | 11811 | bsanchez | if ((posA != -1) && (posB != -1)) |
161 | break;
|
||
162 | } |
||
163 | 10740 | nacho | |
164 | 11811 | bsanchez | if (posA < posB)
|
165 | return true; |
||
166 | else
|
||
167 | return false; |
||
168 | } |
||
169 | 10740 | nacho | |
170 | 11811 | bsanchez | /**
|
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 | 10740 | nacho | |
218 | 11811 | bsanchez | /**
|
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 | 10740 | nacho | |
226 | 11811 | bsanchez | /**
|
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 | 10740 | nacho | |
264 | 11818 | nacho | /**
|
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 | 10740 | nacho | |
287 | 11818 | nacho | /**
|
288 | * Elimina todos los filtros de la pila
|
||
289 | */
|
||
290 | public void clear() { |
||
291 | list.clear(); |
||
292 | } |
||
293 | 10740 | nacho | |
294 | 11818 | nacho | /**
|
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 | 10740 | nacho | |
318 | 11818 | nacho | /**
|
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 | 10740 | nacho | |
327 | 11818 | nacho | /**
|
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 | 11827 | nacho | |
335 | /**
|
||
336 | * Asigna el tipo de dato inicial
|
||
337 | * @param dt
|
||
338 | */
|
||
339 | public void setInitDataType(int dt) { |
||
340 | this.typeFilter = dt;
|
||
341 | } |
||
342 | 10740 | nacho | |
343 | 11818 | nacho | /**
|
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 | 10740 | nacho | |
357 | 11818 | nacho | /**
|
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 | 10740 | nacho | |
384 | 11811 | bsanchez | /**
|
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 | 10740 | nacho | |
392 | 11811 | bsanchez | // System.err.println("SACANDO FILTRO "+i+" "+filter.toString());
|
393 | filter.addParam("raster", rasterBuf);
|
||
394 | filter.execute(); |
||
395 | 10740 | nacho | |
396 | 11811 | bsanchez | if (filter.getResult("raster") != null) |
397 | this.rasterBuf = (IBuffer) filter.getResult("raster"); |
||
398 | } |
||
399 | } |
||
400 | 10740 | nacho | |
401 | 11811 | bsanchez | /**
|
402 | * Aplica los filtros sobre un RasterBuf
|
||
403 | * @return IBuffer de salida
|
||
404 | */
|
||
405 | public IBuffer execute() {
|
||
406 | if (rasterBuf == null) |
||
407 | return null; |
||
408 | executeFilterByDataType(rasterBuf.getDataType()); |
||
409 | return rasterBuf;
|
||
410 | } |
||
411 | |||
412 | /**
|
||
413 | * Muestra el contenido de la pila de filtros para depuraci?n
|
||
414 | */
|
||
415 | public void show() { |
||
416 | System.out.println("--------------------------------------------"); |
||
417 | |||
418 | for (int i = list.size() - 1; i >= 0; i--) { |
||
419 | System.out.println("FILTRO:" + i + " NAME:" + ((RasterFilter) list.get(i)).getName() + " FIL:" + ((RasterFilter) list.get(i)).toString()); |
||
420 | } |
||
421 | } |
||
422 | |||
423 | public void resetPercent() { |
||
424 | for (int i = 0; i < list.size(); i++) |
||
425 | ((RasterFilter) list.get(i)).resetPercent(); |
||
426 | } |
||
427 | |||
428 | public int getPercent() { |
||
429 | int percent = 0; |
||
430 | 11864 | bsanchez | if (list.size() == 0) |
431 | return 0; |
||
432 | 11811 | bsanchez | for (int i = 0; i < list.size(); i++) |
433 | percent += ((RasterFilter) list.get(i)).getPercent(); |
||
434 | 11864 | bsanchez | |
435 | 11811 | bsanchez | percent = percent / list.size(); |
436 | return percent;
|
||
437 | } |
||
438 | 10740 | nacho | |
439 | 11811 | bsanchez | /**
|
440 | * Metodo para poder cancelar el proceso de calculo de histograma.
|
||
441 | */
|
||
442 | public void setCanceled(boolean value) { |
||
443 | for (int i = 0; i < list.size(); i++) |
||
444 | ((RasterFilter) list.get(i)).setCanceled(value); |
||
445 | } |
||
446 | 10740 | nacho | |
447 | 11811 | bsanchez | /**
|
448 | * Metodo para saber si se ha cancelado un proceso de calculo de histograma
|
||
449 | * @return boolean
|
||
450 | */
|
||
451 | public boolean isCanceled(int process) { |
||
452 | for (int i = 0; i < list.size(); i++) |
||
453 | if (((RasterFilter) list.get(i)).isCanceled())
|
||
454 | return true; |
||
455 | return false; |
||
456 | 11864 | bsanchez | } |
457 | |||
458 | /**
|
||
459 | * Guarda el estado de la lista de filtros en una pila, que se podr?
|
||
460 | * ir recuperando con popStatus()
|
||
461 | */
|
||
462 | public void pushStatus() { |
||
463 | status.push(getStatusCloned()); |
||
464 | } |
||
465 | |||
466 | /**
|
||
467 | * Obtiene el estado actual de los filtros, el ArrayList devuelto es una
|
||
468 | * clonaci?n del original, asi no compartiran datos.
|
||
469 | * @return
|
||
470 | */
|
||
471 | public ArrayList getStatusCloned() { |
||
472 | ArrayList newArray = new ArrayList(); |
||
473 | for (int i = 0; i < list.size(); i++) { |
||
474 | try {
|
||
475 | newArray.add(((RasterFilter) list.get(i)).clone()); |
||
476 | } catch (CloneNotSupportedException e) { |
||
477 | System.out.println("No se ha podido clonar"); |
||
478 | } |
||
479 | } |
||
480 | return newArray;
|
||
481 | } |
||
482 | |||
483 | /**
|
||
484 | * Define el estado actual de los filtros
|
||
485 | * @param newArray
|
||
486 | */
|
||
487 | public void setStatus(ArrayList newArray) { |
||
488 | list.clear(); |
||
489 | for (int i = 0; i < newArray.size(); i++) { |
||
490 | list.add((RasterFilter) newArray.get(i)); |
||
491 | } |
||
492 | } |
||
493 | |||
494 | /**
|
||
495 | * Recupera un estado guardado con antelaci?n mediante el m?todo pushStatus()
|
||
496 | */
|
||
497 | public void popStatus() { |
||
498 | if (status.size() <= 0) |
||
499 | return;
|
||
500 | |||
501 | setStatus((ArrayList) status.pop());
|
||
502 | } |
||
503 | 11811 | bsanchez | } |