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