svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libRaster / src / org / gvsig / raster / grid / filter / enhancement / EnhancementListManager.java @ 29786
History | View | Annotate | Download (8.91 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.enhancement; |
20 |
|
21 |
import java.util.ArrayList; |
22 |
|
23 |
import org.gvsig.raster.dataset.FileNotOpenException; |
24 |
import org.gvsig.raster.dataset.Params; |
25 |
import org.gvsig.raster.dataset.RasterDriverException; |
26 |
import org.gvsig.raster.grid.filter.FilterTypeException; |
27 |
import org.gvsig.raster.grid.filter.IRasterFilterListManager; |
28 |
import org.gvsig.raster.grid.filter.RasterFilter; |
29 |
import org.gvsig.raster.grid.filter.RasterFilterList; |
30 |
import org.gvsig.raster.grid.filter.RasterFilterListManager; |
31 |
import org.gvsig.raster.grid.filter.statistics.StatisticsListManager; |
32 |
import org.gvsig.raster.hierarchy.IStatistics; |
33 |
import org.gvsig.tools.ToolsLocator; |
34 |
import org.gvsig.tools.extensionpoint.ExtensionPoint; |
35 |
import org.gvsig.tools.extensionpoint.ExtensionPointManager; |
36 |
/**
|
37 |
* Gestor de la pila de filtros para el filtro de realce.
|
38 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
39 |
*/
|
40 |
public class EnhancementListManager implements IRasterFilterListManager { |
41 |
protected RasterFilterList filterList = null; |
42 |
private RasterFilterListManager filterListManager = null; |
43 |
private IStatistics stats = null; |
44 |
|
45 |
/**
|
46 |
* Constructor
|
47 |
* @param filterListManager
|
48 |
*/
|
49 |
public EnhancementListManager(RasterFilterListManager filterListManager) {
|
50 |
this.filterListManager = filterListManager;
|
51 |
this.filterList = filterListManager.getFilterList();
|
52 |
stats = (IStatistics)filterList.getEnvParam("IStatistics");
|
53 |
} |
54 |
|
55 |
/**
|
56 |
* Asigna el objeto con las estadisticas.
|
57 |
* @param stats
|
58 |
*/
|
59 |
public void setStatistics(IStatistics stats) { |
60 |
this.stats = stats;
|
61 |
} |
62 |
|
63 |
/**
|
64 |
* Registra EnhancementListManager en los puntos de extension de RasterFilter
|
65 |
*/
|
66 |
public static void register() { |
67 |
ExtensionPointManager extensionPoints =ToolsLocator.getExtensionPointManager(); |
68 |
ExtensionPoint point=extensionPoints.get("RasterFilter");
|
69 |
point.append("Enhancement", "", EnhancementListManager.class); |
70 |
} |
71 |
|
72 |
/**
|
73 |
* A?ade un filtro de realce.
|
74 |
* La forma de inserci?n del filtro es fija ya que la inserci?n de un realce lleva implicita
|
75 |
* la inserci?n de un filtro de recorte de colas (tailtrim), aunque no en todos los casos.
|
76 |
* Si ya existe un filtro de realce en la lista se obtiene la posici?n de este.
|
77 |
* Si es necesario un recorte de colas entonces se comprueba si existe un uno reemplazandose
|
78 |
* por el nuevo y sino se insertar? uno. Al final reemplazamos el realce que existia.
|
79 |
*
|
80 |
* Si por el contrario no existen realce ni trim se a?aden ambos al final de la lista.
|
81 |
* @param removeEnds eliminar extremos en los m?ximos y m?nimos
|
82 |
* @param stats Objeto de estadisticas asociado
|
83 |
* @param tailTrim porcentaje de recorte de colas. Ser? un valor entre 0 y 1.
|
84 |
* @param insertionMode Modo de inserci?n
|
85 |
* @param renderBands bandas RGB mostradas en la visualizaci?n.
|
86 |
* @throws FilterTypeException
|
87 |
*/
|
88 |
public void addEnhancedFilter(boolean removeEnds, IStatistics stats, double tailTrim, int[] renderBands) throws FilterTypeException { |
89 |
try {
|
90 |
if (tailTrim == 0) { // En este caso siempre es necesario el m?ximo y |
91 |
// m?nimo
|
92 |
if (!stats.isCalculated()) {
|
93 |
try {
|
94 |
stats.calcFullStatistics(); |
95 |
} catch (FileNotOpenException e) {
|
96 |
// No podemos aplicar el filtro
|
97 |
return;
|
98 |
} catch (RasterDriverException e) {
|
99 |
// No podemos aplicar el filtro
|
100 |
return;
|
101 |
} |
102 |
} |
103 |
} else {
|
104 |
StatisticsListManager slm = new StatisticsListManager(filterListManager, stats);
|
105 |
slm.addTailFilter(tailTrim, 0D, removeEnds, stats);
|
106 |
} |
107 |
|
108 |
RasterFilter filter = createEnhancedFilter(removeEnds, stats, tailTrim, renderBands); |
109 |
if (filter != null) |
110 |
filterList.add(filter); |
111 |
} catch (InterruptedException e) { |
112 |
//Si se ha interrumpido no a?adimos el filtro
|
113 |
} |
114 |
} |
115 |
|
116 |
public static RasterFilter createEnhancedFilter(boolean removeEnds, IStatistics stats, double tailTrim, int[] renderBands) { |
117 |
RasterFilter filter = new LinearEnhancementFloatFilter();
|
118 |
if (filter != null) { |
119 |
filter.addParam("stats", stats);
|
120 |
if (removeEnds) {
|
121 |
filter.addParam("remove", new Boolean(true)); |
122 |
} else {
|
123 |
filter.addParam("remove", new Boolean(false)); |
124 |
} |
125 |
filter.addParam("tailTrim", new Double(tailTrim)); |
126 |
filter.addParam("renderBands", renderBands);
|
127 |
} |
128 |
|
129 |
return filter;
|
130 |
} |
131 |
|
132 |
/**
|
133 |
* Obtiene un Array de Strings a partir de una pila de filtros. Cada elemento
|
134 |
* del array tendr? la forma de elemento=valor.
|
135 |
*/
|
136 |
public ArrayList getStringsFromFilterList(ArrayList filterList, RasterFilter rf) { |
137 |
if (rf instanceof LinearEnhancementFilter) { |
138 |
filterList.add("filter.enhanced.active=true");
|
139 |
filterList.add("filter.enhanced.tailTrim=" + ((LinearEnhancementFilter) rf).getTailTrim().toString());
|
140 |
StringBuffer values = new StringBuffer(); |
141 |
for (int i = 0; i < ((LinearEnhancementFilter) rf).renderBands.length; i++) { |
142 |
values.append(((LinearEnhancementFilter) rf).renderBands[i]); |
143 |
if (i < ((LinearEnhancementFilter) rf).renderBands.length - 1) |
144 |
values.append(",");
|
145 |
} |
146 |
filterList.add("filter.enhanced.renderbands=" + values.toString());
|
147 |
filterList.add("filter.enhanced.remove=" + ((LinearEnhancementFilter) rf).getRemoveEnds().toString());
|
148 |
} |
149 |
|
150 |
return filterList;
|
151 |
} |
152 |
|
153 |
/*
|
154 |
* (non-Javadoc)
|
155 |
* @see org.gvsig.raster.grid.filter.IRasterFilterListManager#createFilterListFromStrings(java.util.ArrayList, java.lang.String, int)
|
156 |
*/
|
157 |
public int createFilterListFromStrings(ArrayList filters, String fil, int filteri) throws FilterTypeException { |
158 |
if (fil.startsWith("filter.enhanced.active") && RasterFilterListManager.getValue(fil).equals("true") && stats != null) { |
159 |
filters.remove(filteri); |
160 |
double tailTrim = 0D; |
161 |
int[] renderBands = new int[] { 0, 0, 0 }; |
162 |
|
163 |
for (int propFilter = 0; propFilter < filters.size(); propFilter++) { |
164 |
String elem = (String) filters.get(propFilter); |
165 |
|
166 |
if (elem.startsWith("filter.enhanced.tailTrim")) { |
167 |
try {
|
168 |
tailTrim = new Double(RasterFilterListManager.getValue(elem)).doubleValue(); |
169 |
} catch (NumberFormatException ex) { |
170 |
// tailTrim sigue valiendo 0
|
171 |
} |
172 |
} |
173 |
|
174 |
if (elem.startsWith("filter.enhanced.renderbands")) { |
175 |
String value = RasterFilterListManager.getValue(elem);
|
176 |
String[] valueList = value.split(","); |
177 |
renderBands = new int[valueList.length]; |
178 |
for (int i = 0; i < renderBands.length; i++) { |
179 |
try {
|
180 |
renderBands[i] = Integer.parseInt(valueList[i]);
|
181 |
} catch (NumberFormatException e) { |
182 |
// No a?ade el valor
|
183 |
} |
184 |
} |
185 |
// filters.remove(propFilter);
|
186 |
// propFilter--;
|
187 |
} |
188 |
|
189 |
if (elem.startsWith("filter.enhanced.remove")) { |
190 |
addEnhancedFilter(Boolean.valueOf(RasterFilterListManager.getValue(elem)).booleanValue(), stats, tailTrim, renderBands);
|
191 |
filters.remove(propFilter); |
192 |
propFilter--; |
193 |
} |
194 |
} |
195 |
|
196 |
filteri = -1;
|
197 |
} |
198 |
return filteri;
|
199 |
} |
200 |
|
201 |
/*
|
202 |
* (non-Javadoc)
|
203 |
* @see org.gvsig.raster.grid.filter.IRasterFilterListManager#getRasterFilterList()
|
204 |
*/
|
205 |
public ArrayList getRasterFilterList() { |
206 |
ArrayList filters = new ArrayList(); |
207 |
filters.add(LinearEnhancementFilter.class); |
208 |
return filters;
|
209 |
} |
210 |
|
211 |
public void addFilter(Class classFilter, Params params) throws FilterTypeException { |
212 |
if (classFilter.equals(LinearEnhancementFilter.class)) {
|
213 |
boolean removeEnds = false; |
214 |
double tailTrim = 0.0; |
215 |
int[] renderBands = { 0, 1, 2 }; |
216 |
|
217 |
for (int i = 0; i < params.getNumParams(); i++) { |
218 |
if (params.getParam(i).id.equals("RenderBands") && |
219 |
params.getParam(i).defaultValue instanceof String) { |
220 |
String[] bands = new String((String) params.getParam(i).defaultValue).split(" "); |
221 |
renderBands[0] = new Integer(bands[0]).intValue(); |
222 |
renderBands[1] = new Integer(bands[1]).intValue(); |
223 |
renderBands[2] = new Integer(bands[2]).intValue(); |
224 |
continue;
|
225 |
} |
226 |
if (params.getParam(i).id.equals("RemoveEnds") && |
227 |
params.getParam(i).defaultValue instanceof Boolean) { |
228 |
removeEnds = ((Boolean) params.getParam(i).defaultValue).booleanValue();
|
229 |
continue;
|
230 |
} |
231 |
if (params.getParam(i).id.equals("TailTrim") && |
232 |
params.getParam(i).defaultValue instanceof Double) |
233 |
tailTrim = ((Double) params.getParam(i).defaultValue).doubleValue() / 100.0; |
234 |
} |
235 |
|
236 |
addEnhancedFilter(removeEnds, (IStatistics) filterList.getEnvParam("IStatistics"), tailTrim, renderBands);
|
237 |
} |
238 |
} |
239 |
} |