root / trunk / libraries / libRaster / src / org / gvsig / raster / grid / filter / RasterFilterList.java @ 11067
History | View | Annotate | Download (10.1 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 | |||
23 | 10939 | nacho | import org.gvsig.raster.dataset.IBuffer; |
24 | 10740 | nacho | |
25 | /**
|
||
26 | * Esta clase representa la lista de filtros que debe ser manejada desde el
|
||
27 | * RasterFilterListManager.
|
||
28 | * @author Nacho Brodin (nachobrodin@gmail.com)
|
||
29 | *
|
||
30 | */
|
||
31 | public class RasterFilterList { |
||
32 | |||
33 | private IBuffer rasterBuf = null; |
||
34 | private int typeFilter = -1; |
||
35 | /*
|
||
36 | * TODO: FUNCIONALIDAD: Anulada las estad?sticas. Hay que incluirlas de nuevo.
|
||
37 | */
|
||
38 | //private Statistic stats = null;
|
||
39 | //Pila de objetos Filter (Contiene un RasterFilter)
|
||
40 | private ArrayList list = new ArrayList(); |
||
41 | |||
42 | |||
43 | /**
|
||
44 | * Constructor
|
||
45 | * @param stats
|
||
46 | */
|
||
47 | /*public RasterFilterStack(Statistic stats) {
|
||
48 | this.stats = stats;
|
||
49 | }*/
|
||
50 | |||
51 | /**
|
||
52 | * A?ade un filtro al final de la lista
|
||
53 | * @param filter filtro a?adido
|
||
54 | */
|
||
55 | public void add(RasterFilter filter) { |
||
56 | list.add(filter); |
||
57 | this.sort();
|
||
58 | } |
||
59 | |||
60 | /**
|
||
61 | * Elimina los filtros de una prioridad dada
|
||
62 | * @param priority Prioridad de los filtros a eliminar
|
||
63 | */
|
||
64 | public void remove(int priority) { |
||
65 | for (int i = list.size() - 1; i >= 0; i--) { |
||
66 | if(((RasterFilter)list.get(i)).getPriority() == priority)
|
||
67 | list.remove(i); |
||
68 | } |
||
69 | } |
||
70 | |||
71 | /**
|
||
72 | * Elimina un filtro a partir de su nombre
|
||
73 | * @param name Nombre del filtro a eliminar
|
||
74 | */
|
||
75 | public void remove(String name) { |
||
76 | for (int i = list.size() - 1; i >= 0; i--){ |
||
77 | if(((RasterFilter)list.get(i)).getName().equals(name))
|
||
78 | list.remove(i); |
||
79 | } |
||
80 | } |
||
81 | |||
82 | /**
|
||
83 | * Elimina un filtro concreto de la pila
|
||
84 | * @param type
|
||
85 | */
|
||
86 | public void remove(RasterFilter filter) { |
||
87 | for (int i = list.size() - 1; i >= 0; i--) { |
||
88 | if (((RasterFilter) list.get(i)).equals(filter))
|
||
89 | list.remove(i); |
||
90 | } |
||
91 | } |
||
92 | |||
93 | /**
|
||
94 | * Devuelve el tipo de dato de retorno al aplicar la pila de filtros
|
||
95 | * @return
|
||
96 | */
|
||
97 | public int getOutDataType() { |
||
98 | return rasterBuf.getDataType();
|
||
99 | } |
||
100 | |||
101 | /**
|
||
102 | * Devuelve el raster resultado de la aplicacion de la pila de filtros
|
||
103 | * @return
|
||
104 | */
|
||
105 | public IBuffer getResult() {
|
||
106 | return rasterBuf;
|
||
107 | } |
||
108 | |||
109 | /**
|
||
110 | * Devuelve true si el filtro 'a' est? por encima del 'b' en la lista y false si no lo est?
|
||
111 | * @param a Nombre de filtro
|
||
112 | * @param b Nombre de filtro
|
||
113 | * @return
|
||
114 | */
|
||
115 | private boolean isOnTop(String a, String b) { |
||
116 | int posA = -1; |
||
117 | int posB = -1; |
||
118 | |||
119 | for (int i = 0; i < list.size(); i++) { |
||
120 | if (list.get(i).equals(a))
|
||
121 | posA = i; |
||
122 | |||
123 | if (list.get(i).equals(b))
|
||
124 | posB = i; |
||
125 | |||
126 | if ((posA != -1) && (posB != -1)) |
||
127 | break;
|
||
128 | } |
||
129 | |||
130 | if (posA < posB)
|
||
131 | return true; |
||
132 | else
|
||
133 | return false; |
||
134 | } |
||
135 | |||
136 | /**
|
||
137 | * Dado un tipo de filtro calcula a partir de la lista en que posici?n est? situado y
|
||
138 | * obtiene el tipo de dato de la salida anterior. Esto le dir? al cliente de que tipo
|
||
139 | * es el filtro que tiene que introducir.
|
||
140 | * @param type
|
||
141 | */
|
||
142 | public int getDataTypeInFilter(String name) { |
||
143 | for (int i = 0; i < list.size(); i++) { |
||
144 | if(((RasterFilter) list.get(i)).getName().equals(name)){
|
||
145 | if(i == 0) |
||
146 | return typeFilter;
|
||
147 | else
|
||
148 | return ((RasterFilter) list.get(i - 1)).getOutRasterDataType(); |
||
149 | |||
150 | } |
||
151 | } |
||
152 | return this.typeFilter; |
||
153 | } |
||
154 | |||
155 | /**
|
||
156 | * Ordena los filtros en el orden establecido por el Manager
|
||
157 | */
|
||
158 | public void sort() { |
||
159 | ArrayList aux = new ArrayList(); |
||
160 | |||
161 | if(list.size() == 1) |
||
162 | return;
|
||
163 | |||
164 | while(list.size() > 0){ |
||
165 | int max = ((RasterFilter)list.get(0)).getPriority(); |
||
166 | for (int i = list.size() - 1; i >= 0; i--) { |
||
167 | if(((RasterFilter)list.get(i)).getPriority() >= max){
|
||
168 | aux.add(list.get(i)); |
||
169 | list.remove(i); |
||
170 | } |
||
171 | } |
||
172 | } |
||
173 | |||
174 | list = aux; |
||
175 | aux = null;
|
||
176 | } |
||
177 | |||
178 | /**
|
||
179 | * Obtiene la cantidad de filtros en la lista
|
||
180 | * @return N?mero de filtros apilados
|
||
181 | */
|
||
182 | public int lenght() { |
||
183 | return list.size();
|
||
184 | } |
||
185 | |||
186 | /**
|
||
187 | * Obtiene el filtro apilado de la posici?n i o null si el indice es incorrecto
|
||
188 | * @param i Posici?n a acceder en la pila
|
||
189 | * @return Filtro
|
||
190 | */
|
||
191 | public RasterFilter get(int i) { |
||
192 | if(i >= list.size() || i < 0) |
||
193 | return null; |
||
194 | return ((RasterFilter) list.get(i));
|
||
195 | } |
||
196 | |||
197 | /**
|
||
198 | * Obtiene el filtro apilado de nombre name o null si no existe
|
||
199 | * @param i Nombre del filtro buscado
|
||
200 | * @return Filtro
|
||
201 | */
|
||
202 | public RasterFilter get(String name) { |
||
203 | for (int i = list.size() - 1; i >= 0; i--){ |
||
204 | if(((RasterFilter) list.get(i)).getName().equals(name))
|
||
205 | return ((RasterFilter) list.get(i));
|
||
206 | } |
||
207 | return null; |
||
208 | } |
||
209 | |||
210 | /**
|
||
211 | * Obtiene el filtro apilado que corresponde con el nombre
|
||
212 | * @param name Nombre de filtro
|
||
213 | * @return Filtro en caso de que exista un filtro apilado de ese tipo
|
||
214 | * o null si no hay ninguno.
|
||
215 | */
|
||
216 | public RasterFilter getByName(String name) { |
||
217 | for(int i = 0; i < lenght(); i++){ |
||
218 | if(((RasterFilter) list.get(i)).getName().equals(name))
|
||
219 | return ((RasterFilter) list.get(i));
|
||
220 | } |
||
221 | return null; |
||
222 | } |
||
223 | |||
224 | /**
|
||
225 | * Obtiene el tipo del filtro de la pila de la posici?n i
|
||
226 | * @param i Posici?n a acceder en la pila
|
||
227 | * @return tipo de filtro
|
||
228 | */
|
||
229 | public String getName(int i) { |
||
230 | return ((RasterFilter) list.get(i)).getName();
|
||
231 | } |
||
232 | |||
233 | /**
|
||
234 | * Elimina todos los filtros de la pila
|
||
235 | */
|
||
236 | public void clear() { |
||
237 | list.clear(); |
||
238 | } |
||
239 | |||
240 | /**
|
||
241 | * Sustituye un filtro de una posici?n de la pila por otro
|
||
242 | * @param filter
|
||
243 | * @param i
|
||
244 | */
|
||
245 | public void replace(RasterFilter filter, String name) { |
||
246 | for (int i = list.size() - 1; i >= 0; i--) { |
||
247 | if(((RasterFilter) list.get(i)).getName().equals(name)){
|
||
248 | list.remove(i); |
||
249 | list.add(i, filter); |
||
250 | } |
||
251 | } |
||
252 | sort(); |
||
253 | } |
||
254 | |||
255 | /**
|
||
256 | * Sustituye un filtro de una posici?n de la pila por otro
|
||
257 | * @param filter
|
||
258 | * @param i
|
||
259 | */
|
||
260 | public void replace(RasterFilter filter, int i) { |
||
261 | list.remove(i); |
||
262 | list.add(i, filter); |
||
263 | sort(); |
||
264 | } |
||
265 | |||
266 | /**
|
||
267 | * Asigna el raster de entrada inicial
|
||
268 | * @param raster
|
||
269 | */
|
||
270 | public void setInitRasterBuf(IBuffer raster) { |
||
271 | rasterBuf = (IBuffer) raster; |
||
272 | typeFilter = rasterBuf.getDataType(); |
||
273 | } |
||
274 | |||
275 | /**
|
||
276 | * Devuelve el tipo de datos inicial de la lista
|
||
277 | * @return Tipo de dato del raster inicial
|
||
278 | */
|
||
279 | public int getInitDataType() { |
||
280 | return typeFilter;
|
||
281 | } |
||
282 | |||
283 | /**
|
||
284 | * M?todo que devuelve true si el tipo de filtro pasado por par?metro est? en la
|
||
285 | * pila y false si no lo est?.
|
||
286 | * @param type Tipo de par?metro a comprobar
|
||
287 | * @return true si est? en la pila y false si no lo est?
|
||
288 | */
|
||
289 | public boolean isActive(String name) { |
||
290 | for (int i = list.size() - 1; i >= 0; i--) { |
||
291 | if (((RasterFilter) list.get(i)).getName().equals(name))
|
||
292 | return true; |
||
293 | } |
||
294 | return false; |
||
295 | } |
||
296 | |||
297 | /**
|
||
298 | * M?todo que devuelve true si el tipo de filtro pasado por par?metro est? en la
|
||
299 | * pila y false si no lo est?.
|
||
300 | * @param filter Tipo de filtro a comprobar
|
||
301 | * @return true si est? en la pila y false si no lo est?
|
||
302 | */
|
||
303 | public boolean isActive(RasterFilter filter) { |
||
304 | for (int i = list.size() - 1; i >= 0; i--) { |
||
305 | if (((RasterFilter) list.get(i)).equals(filter)) {
|
||
306 | return true; |
||
307 | } |
||
308 | } |
||
309 | return false; |
||
310 | } |
||
311 | |||
312 | /**
|
||
313 | * Obtiene el objeto Statistic
|
||
314 | * @return
|
||
315 | */
|
||
316 | /*public Statistic getStats() {
|
||
317 | return this.stats;
|
||
318 | }*/
|
||
319 | |||
320 | /**
|
||
321 | * Aplica los filtros de la pila sobre el buffer correspondiente
|
||
322 | * @param dataType
|
||
323 | */
|
||
324 | private void executeFilterByDataType(int dataType) { |
||
325 | for (int i = list.size() - 1; i >= 0; i--) { |
||
326 | RasterFilter filter = ((RasterFilter) list.get(i)); |
||
327 | |||
328 | //System.err.println("SACANDO FILTRO "+i+" "+filter.toString());
|
||
329 | filter.addParam("raster", rasterBuf);
|
||
330 | filter.execute(); |
||
331 | |||
332 | /*if (filter.getResult("stats") != null) {
|
||
333 | this.stats = (Statistic) filter.getResult("stats");
|
||
334 | }*/
|
||
335 | |||
336 | if (filter.getResult("raster") != null) |
||
337 | this.rasterBuf = (IBuffer) filter.getResult("raster"); |
||
338 | } |
||
339 | } |
||
340 | |||
341 | /**
|
||
342 | * Aplica los filtros sobre un RasterBuf
|
||
343 | * @return IBuffer de salida
|
||
344 | */
|
||
345 | public IBuffer execute() {
|
||
346 | if(rasterBuf == null) |
||
347 | return null; |
||
348 | executeFilterByDataType(rasterBuf.getDataType()); |
||
349 | return rasterBuf;
|
||
350 | } |
||
351 | |||
352 | /**
|
||
353 | *Muestra el contenido de la pila de filtros para depuraci?n
|
||
354 | */
|
||
355 | public void show() { |
||
356 | System.out.println("--------------------------------------------"); |
||
357 | |||
358 | for (int i = list.size() - 1; i >= 0; i--) { |
||
359 | System.out.println("FILTRO:" + i + " NAME:" + |
||
360 | ((RasterFilter) list.get(i)).getName() + " PRI:" +
|
||
361 | ((RasterFilter) list.get(i)).getPriority() + " FIL:" +
|
||
362 | ((RasterFilter) list.get(i)).toString()); |
||
363 | } |
||
364 | } |
||
365 | |||
366 | } |