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