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