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