root / trunk / libraries / libRaster / src / org / gvsig / raster / grid / filter / RasterFilterList.java @ 15778
History | View | Annotate | Download (12.1 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 |
* Esta clase representa la lista de filtros que debe ser manejada desde el
|
28 |
* RasterFilterListManager.
|
29 |
*
|
30 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
31 |
*/
|
32 |
public class RasterFilterList { |
33 |
private IBuffer rasterBuf = null; |
34 |
private int typeFilter = -1; |
35 |
private TreeMap environment = new TreeMap(); |
36 |
|
37 |
// Pila de objetos Filter (Contiene un RasterFilter)
|
38 |
private ArrayList list = new ArrayList(); |
39 |
private Stack status = new Stack(); |
40 |
|
41 |
/**
|
42 |
* A?ade un par?metro a la lista de filtros. Estos par?metros luego pueden ser
|
43 |
* utilizados por los managers que se registren
|
44 |
* @param key Nombre del par?metro que coincide con el nombre de la clase.
|
45 |
* @param value Objeto
|
46 |
*/
|
47 |
public void addEnvParam(String key, Object value) { |
48 |
environment.put(key, value); |
49 |
} |
50 |
|
51 |
/**
|
52 |
* Obtiene un par?metro de la lista de filtros.
|
53 |
* @param key Identificador del par?metro. Coincide con el nombre de la clase del par?metro.
|
54 |
*/
|
55 |
public Object getEnvParam(String key) { |
56 |
return environment.get(key);
|
57 |
} |
58 |
|
59 |
/**
|
60 |
* Controla que los tipos de entrada y salida de los filtros sean los
|
61 |
* correctos
|
62 |
*/
|
63 |
public void controlTypes() { |
64 |
RasterFilterListManager stackManager = new RasterFilterListManager(this); |
65 |
stackManager.controlTypes(); |
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 |
if (isActive(filter.getName())) {
|
74 |
replace(filter, filter.getName()); |
75 |
} else {
|
76 |
list.add(filter); |
77 |
controlTypes(); |
78 |
} |
79 |
filter.setEnv(environment); |
80 |
} |
81 |
|
82 |
/**
|
83 |
* Sustituye un filtro de una posici?n de la pila por otro
|
84 |
* @param filter
|
85 |
* @param i
|
86 |
*/
|
87 |
public void replace(RasterFilter filter, String name) { |
88 |
boolean changed = false; |
89 |
filter.setEnv(environment); |
90 |
for (int i = list.size() - 1; i >= 0; i--) |
91 |
if (((RasterFilter) list.get(i)).getName().equals(name)) {
|
92 |
list.remove(i); |
93 |
list.add(i, filter); |
94 |
changed = true;
|
95 |
} |
96 |
|
97 |
if (changed)
|
98 |
controlTypes(); |
99 |
} |
100 |
|
101 |
/**
|
102 |
* A?ade un filtro en la lista en la posici?n indicada.
|
103 |
* @param filter filtro a?adido
|
104 |
* @param pos posici?n
|
105 |
*/
|
106 |
public void add(RasterFilter filter, int pos) { |
107 |
try {
|
108 |
list.add(pos, filter); |
109 |
controlTypes(); |
110 |
} catch (IndexOutOfBoundsException e) { |
111 |
add(filter); |
112 |
} |
113 |
filter.setEnv(environment); |
114 |
} |
115 |
|
116 |
/**
|
117 |
* Elimina un filtro a partir de su nombre
|
118 |
* @param name Nombre del filtro a eliminar
|
119 |
*/
|
120 |
public void remove(String name) { |
121 |
boolean changed = false; |
122 |
for (int i = list.size() - 1; i >= 0; i--) |
123 |
if (((RasterFilter) list.get(i)).getName().equals(name)) {
|
124 |
list.remove(i); |
125 |
changed = true;
|
126 |
} |
127 |
if (changed)
|
128 |
controlTypes(); |
129 |
} |
130 |
|
131 |
/**
|
132 |
* Elimina un filtro por clase.
|
133 |
*
|
134 |
* @param baseFilterClass
|
135 |
*/
|
136 |
public void remove(Class baseFilterClass) { |
137 |
boolean changed = false; |
138 |
for (int i = 0; i < lenght(); i++) |
139 |
if (baseFilterClass.isInstance((RasterFilter) list.get(i))) {
|
140 |
list.remove(i); |
141 |
i--; |
142 |
changed = true;
|
143 |
} |
144 |
if (changed)
|
145 |
controlTypes(); |
146 |
} |
147 |
|
148 |
/**
|
149 |
* Devuelve el tipo de dato de retorno al aplicar la pila de filtros
|
150 |
* @return
|
151 |
*/
|
152 |
public int getOutDataType() { |
153 |
if (list.size() > 0) |
154 |
return ((RasterFilter) list.get(list.size() - 1)).getOutRasterDataType(); |
155 |
else
|
156 |
return rasterBuf.getDataType();
|
157 |
} |
158 |
|
159 |
/**
|
160 |
* Devuelve el raster resultado de la aplicacion de la pila de filtros
|
161 |
* @return
|
162 |
*/
|
163 |
public IBuffer getResult() {
|
164 |
return rasterBuf;
|
165 |
} |
166 |
|
167 |
/**
|
168 |
* Obtiene la cantidad de filtros en la lista
|
169 |
* @return N?mero de filtros apilados
|
170 |
*/
|
171 |
public int lenght() { |
172 |
return list.size();
|
173 |
} |
174 |
|
175 |
/**
|
176 |
* Obtiene el filtro apilado de la posici?n i o null si el indice es incorrecto
|
177 |
* @param i Posici?n a acceder en la pila
|
178 |
* @return Filtro
|
179 |
*/
|
180 |
public RasterFilter get(int i) { |
181 |
if (i >= list.size() || i < 0) |
182 |
return null; |
183 |
return (RasterFilter) list.get(i);
|
184 |
} |
185 |
|
186 |
/**
|
187 |
* Obtiene el filtro apilado de nombre name o null si no existe
|
188 |
* @param i Nombre del filtro buscado
|
189 |
* @return Filtro
|
190 |
*/
|
191 |
public RasterFilter get(String name) { |
192 |
for (int i = list.size() - 1; i >= 0; i--) { |
193 |
if (((RasterFilter) list.get(i)).getName().equals(name))
|
194 |
return (RasterFilter) list.get(i);
|
195 |
} |
196 |
return null; |
197 |
} |
198 |
|
199 |
/**
|
200 |
* Obtiene el filtro apilado que corresponde con el nombre
|
201 |
* @param name Nombre de filtro
|
202 |
* @return Filtro en caso de que exista un filtro apilado de ese tipo
|
203 |
* o null si no hay ninguno.
|
204 |
*/
|
205 |
public RasterFilter getByName(String name) { |
206 |
for (int i = 0; i < lenght(); i++) { |
207 |
if (((RasterFilter) list.get(i)).getName().equals(name))
|
208 |
return (RasterFilter) list.get(i);
|
209 |
} |
210 |
return null; |
211 |
} |
212 |
|
213 |
/**
|
214 |
* Obtiene el primer filtro de la lista que es instancia de la clase pasada por
|
215 |
* par?metro
|
216 |
* @param baseFilterClass Filtro base
|
217 |
* @return RasterFilter
|
218 |
*/
|
219 |
public RasterFilter getFilterByBaseClass(Class baseFilterClass) { |
220 |
for (int i = 0; i < lenght(); i++) { |
221 |
if (baseFilterClass.isInstance((RasterFilter) list.get(i)))
|
222 |
return (RasterFilter) list.get(i);
|
223 |
} |
224 |
return null; |
225 |
} |
226 |
|
227 |
/**
|
228 |
* Obtiene el tipo del filtro de la pila de la posici?n i
|
229 |
* @param i Posici?n a acceder en la pila
|
230 |
* @return tipo de filtro
|
231 |
*/
|
232 |
public String getName(int i) { |
233 |
return ((RasterFilter) list.get(i)).getName();
|
234 |
} |
235 |
|
236 |
/**
|
237 |
* Elimina todos los filtros de la pila
|
238 |
*/
|
239 |
public void clear() { |
240 |
list.clear(); |
241 |
} |
242 |
|
243 |
/**
|
244 |
* Sustituye un filtro de una posici?n de la pila por otro
|
245 |
* @param filter
|
246 |
* @param i
|
247 |
*/
|
248 |
public void replace(RasterFilter filter, int i) { |
249 |
filter.setEnv(environment); |
250 |
list.remove(i); |
251 |
list.add(i, filter); |
252 |
} |
253 |
|
254 |
/**
|
255 |
* Move un filtro determinado a la posici?n especificada.
|
256 |
* @param filter Filtro a mover
|
257 |
* @param position Posici?n a asignar
|
258 |
* @return Devuelve true si el filtro existia y lo ha movido y false si no lo ha hecho.
|
259 |
*/
|
260 |
public boolean move(Class filter, int position) { |
261 |
RasterFilter f = null;
|
262 |
for (int i = 0; i < list.size(); i++) { |
263 |
if(filter.isInstance(list.get(i))) {
|
264 |
f = (RasterFilter) list.get(i); |
265 |
list.remove(i); |
266 |
break;
|
267 |
} |
268 |
} |
269 |
if(f != null) { |
270 |
list.add(position, f); |
271 |
return true; |
272 |
} |
273 |
return false; |
274 |
} |
275 |
|
276 |
/**
|
277 |
* Asigna el raster de entrada inicial
|
278 |
* @param raster
|
279 |
*/
|
280 |
public void setInitRasterBuf(IBuffer raster) { |
281 |
rasterBuf = (IBuffer) raster; |
282 |
typeFilter = rasterBuf.getDataType(); |
283 |
} |
284 |
|
285 |
/**
|
286 |
* Devuelve el tipo de datos inicial de la lista
|
287 |
* @return Tipo de dato del raster inicial
|
288 |
*/
|
289 |
public int getInitDataType() { |
290 |
return typeFilter;
|
291 |
} |
292 |
|
293 |
/**
|
294 |
* Asigna el tipo de dato inicial
|
295 |
* @param dt
|
296 |
*/
|
297 |
public void setInitDataType(int dt) { |
298 |
this.typeFilter = dt;
|
299 |
} |
300 |
|
301 |
/**
|
302 |
* M?todo que devuelve true si el tipo de filtro pasado por par?metro est? en la
|
303 |
* pila y false si no lo est?.
|
304 |
* @param type Tipo de par?metro a comprobar
|
305 |
* @return true si est? en la pila y false si no lo est?
|
306 |
*/
|
307 |
public boolean isActive(String name) { |
308 |
for (int i = list.size() - 1; i >= 0; i--) { |
309 |
if (((RasterFilter) list.get(i)).getName().equals(name))
|
310 |
return true; |
311 |
} |
312 |
return false; |
313 |
} |
314 |
|
315 |
/**
|
316 |
* M?todo que devuelve true si el tipo de filtro pasado por par?metro est? en
|
317 |
* la pila y false si no lo est?.
|
318 |
*
|
319 |
* @param filter Tipo de filtro a comprobar
|
320 |
* @return true si est? en la pila y false si no lo est?
|
321 |
*/
|
322 |
public boolean isActive(RasterFilter filter) { |
323 |
for (int i = list.size() - 1; i >= 0; i--) { |
324 |
if (((RasterFilter) list.get(i)).equals(filter)) {
|
325 |
return true; |
326 |
} |
327 |
} |
328 |
return false; |
329 |
} |
330 |
|
331 |
/**
|
332 |
* Devuelve la posici?n en la lista de una clase de filtro concreta
|
333 |
*
|
334 |
* @param c Clase a buscar en la lista
|
335 |
* @return posici?n en la lista
|
336 |
*/
|
337 |
public int getPosition(Class c) { |
338 |
for (int i = 0; i < list.size(); i++) |
339 |
if (c.isInstance(list.get(i)))
|
340 |
return i;
|
341 |
return -1; |
342 |
} |
343 |
|
344 |
/**
|
345 |
* Aplica los filtros de la pila sobre el buffer correspondiente
|
346 |
* @param dataType
|
347 |
* @throws InterruptedException
|
348 |
*/
|
349 |
private void executeFilterByDataType(int dataType) throws InterruptedException { |
350 |
for (int i = 0; i < list.size(); i++) { |
351 |
RasterFilter filter = (RasterFilter) list.get(i); |
352 |
|
353 |
// TODO: Arquitectura. Quitar el ControlTypes y en este momento
|
354 |
// cerciorarse de si el tipo del filtro es totalmente el correcto o hay
|
355 |
// que recrearlo. Ejemplo:
|
356 |
// Si el filtro que tenemos antes de preprocesar es de tipo Byte y la
|
357 |
// entrada de datos es de tipo float, reconstruir solo este filtro para
|
358 |
// que sea de tipo float
|
359 |
|
360 |
filter.addParam("raster", rasterBuf);
|
361 |
filter.execute(); |
362 |
|
363 |
if (filter.getResult("raster") != null) |
364 |
this.rasterBuf = (IBuffer) filter.getResult("raster"); |
365 |
} |
366 |
} |
367 |
|
368 |
/**
|
369 |
* Aplica los filtros sobre un RasterBuf
|
370 |
* @return IBuffer de salida
|
371 |
* @throws InterruptedException
|
372 |
*/
|
373 |
public IBuffer execute() throws InterruptedException { |
374 |
if (rasterBuf == null) |
375 |
return null; |
376 |
executeFilterByDataType(rasterBuf.getDataType()); |
377 |
return rasterBuf;
|
378 |
} |
379 |
|
380 |
/**
|
381 |
* Muestra el contenido de la pila de filtros para depuraci?n
|
382 |
*/
|
383 |
public void show() { |
384 |
System.out.println("--------------------------------------------"); |
385 |
|
386 |
for (int i = 0; i < list.size() ; i++) { |
387 |
System.out.println("FILTRO:" + i + " NAME:" + ((RasterFilter) list.get(i)).getName() + " FIL:" + ((RasterFilter) list.get(i)).toString()); |
388 |
} |
389 |
} |
390 |
|
391 |
public void resetPercent() { |
392 |
for (int i = 0; i < list.size(); i++) |
393 |
((RasterFilter) list.get(i)).resetPercent(); |
394 |
} |
395 |
|
396 |
public int getPercent() { |
397 |
int percent = 0; |
398 |
if (list.size() == 0) |
399 |
return 0; |
400 |
for (int i = 0; i < list.size(); i++) |
401 |
percent += ((RasterFilter) list.get(i)).getPercent(); |
402 |
|
403 |
percent = percent / list.size(); |
404 |
return percent;
|
405 |
} |
406 |
|
407 |
/**
|
408 |
* Metodo para poder cancelar el proceso de calculo de histograma.
|
409 |
*/
|
410 |
public void setCanceled(boolean value) { |
411 |
for (int i = 0; i < list.size(); i++) |
412 |
((RasterFilter) list.get(i)).setCanceled(value); |
413 |
} |
414 |
|
415 |
/**
|
416 |
* Metodo para saber si se ha cancelado un proceso de calculo de histograma
|
417 |
* @return boolean
|
418 |
*/
|
419 |
public boolean isCanceled(int process) { |
420 |
for (int i = 0; i < list.size(); i++) |
421 |
if (((RasterFilter) list.get(i)).isCanceled())
|
422 |
return true; |
423 |
return false; |
424 |
} |
425 |
|
426 |
/**
|
427 |
* Guarda el estado de la lista de filtros en una pila, que se podr?
|
428 |
* ir recuperando con popStatus()
|
429 |
*/
|
430 |
public void pushStatus() { |
431 |
status.push(getStatusCloned()); |
432 |
} |
433 |
|
434 |
/**
|
435 |
* Obtiene el estado actual de los filtros, el ArrayList devuelto es una
|
436 |
* clonaci?n del original, asi no compartiran datos.
|
437 |
* @return
|
438 |
*/
|
439 |
public ArrayList getStatusCloned() { |
440 |
ArrayList newArray = new ArrayList(); |
441 |
for (int i = 0; i < list.size(); i++) { |
442 |
try {
|
443 |
newArray.add(((RasterFilter) list.get(i)).clone()); |
444 |
} catch (CloneNotSupportedException e) { |
445 |
System.out.println("No se ha podido clonar"); |
446 |
} |
447 |
} |
448 |
return newArray;
|
449 |
} |
450 |
|
451 |
/**
|
452 |
* Define el estado actual de los filtros
|
453 |
* @param newArray
|
454 |
*/
|
455 |
public void setStatus(ArrayList newArray) { |
456 |
list.clear(); |
457 |
for (int i = 0; i < newArray.size(); i++) { |
458 |
list.add(newArray.get(i)); |
459 |
} |
460 |
} |
461 |
|
462 |
/**
|
463 |
* Recupera un estado guardado con antelaci?n mediante el m?todo pushStatus()
|
464 |
*/
|
465 |
public void popStatus() { |
466 |
if (status.size() <= 0) |
467 |
return;
|
468 |
|
469 |
setStatus((ArrayList) status.pop());
|
470 |
} |
471 |
|
472 |
/**
|
473 |
* Obtiene el TreeMap con los par?metros
|
474 |
* @return TreeMap
|
475 |
*/
|
476 |
public TreeMap getEnv() { |
477 |
return environment;
|
478 |
} |
479 |
|
480 |
/**
|
481 |
* Asigna el TreeMap con los par?metros del entorno
|
482 |
* @param env
|
483 |
*/
|
484 |
public void setEnv(TreeMap env) { |
485 |
this.environment = env;
|
486 |
} |
487 |
} |