root / trunk / libraries / libRaster / src / org / gvsig / raster / grid / filter / enhancement / EnhancementListManager.java @ 12333
History | View | Annotate | Download (8.28 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.shared.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.getParam("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 |
|
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 |
} |
106 |
|
107 |
public static RasterFilter createEnhancedFilter(boolean removeEnds, IStatistics stats, double tailTrim, int[] renderBands) { |
108 |
RasterFilter filter = new LinearEnhancementFloatFilter();
|
109 |
if (filter != null) { |
110 |
filter.addParam("stats", stats);
|
111 |
if (removeEnds) {
|
112 |
filter.addParam("remove", new Boolean(true)); |
113 |
} else {
|
114 |
filter.addParam("remove", new Boolean(false)); |
115 |
} |
116 |
filter.addParam("tailTrim", new Double(tailTrim)); |
117 |
filter.addParam("renderBands", renderBands);
|
118 |
} |
119 |
|
120 |
return filter;
|
121 |
} |
122 |
|
123 |
/**
|
124 |
* Obtiene un Array de Strings a partir de una pila de filtros. Cada elemento
|
125 |
* del array tendr? la forma de elemento=valor.
|
126 |
*/
|
127 |
public ArrayList getStringsFromFilterList(ArrayList filterList, RasterFilter rf) { |
128 |
if (rf instanceof LinearEnhancementFilter) { |
129 |
filterList.add("filter.enhanced.active=true");
|
130 |
filterList.add("filter.enhanced.tailTrim=" + ((LinearEnhancementFilter) rf).getTailTrim().toString());
|
131 |
StringBuffer values = new StringBuffer(); |
132 |
for (int i = 0; i < ((LinearEnhancementFilter) rf).renderBands.length; i++) { |
133 |
values.append(((LinearEnhancementFilter) rf).renderBands[i]); |
134 |
if (i < ((LinearEnhancementFilter) rf).renderBands.length - 1) |
135 |
values.append(",");
|
136 |
} |
137 |
filterList.add("filter.enhanced.renderbands=" + values.toString());
|
138 |
filterList.add("filter.enhanced.remove=" + ((LinearEnhancementFilter) rf).getRemoveEnds().toString());
|
139 |
} |
140 |
|
141 |
return filterList;
|
142 |
} |
143 |
|
144 |
/*
|
145 |
* (non-Javadoc)
|
146 |
* @see org.gvsig.raster.grid.filter.IRasterFilterListManager#createFilterListFromStrings(java.util.ArrayList, java.lang.String, int)
|
147 |
*/
|
148 |
public int createFilterListFromStrings(ArrayList filters, String fil, int filteri) { |
149 |
if (fil.startsWith("filter.enhanced.active") && RasterFilterListManager.getValue(fil).equals("true")) { |
150 |
filters.remove(filteri); |
151 |
double tailTrim = 0D; |
152 |
int[] renderBands = new int[] { 0, 0, 0 }; |
153 |
|
154 |
for (int propFilter = 0; propFilter < filters.size(); propFilter++) { |
155 |
String elem = (String) filters.get(propFilter); |
156 |
|
157 |
if (elem.startsWith("filter.enhanced.tailTrim")) { |
158 |
try {
|
159 |
tailTrim = new Double(RasterFilterListManager.getValue(elem)).doubleValue(); |
160 |
} catch (NumberFormatException ex) { |
161 |
// tailTrim sigue valiendo 0
|
162 |
} |
163 |
} |
164 |
|
165 |
if (elem.startsWith("filter.enhanced.renderbands")) { |
166 |
String value = RasterFilterListManager.getValue(elem);
|
167 |
String[] valueList = value.split(","); |
168 |
renderBands = new int[valueList.length]; |
169 |
for (int i = 0; i < renderBands.length; i++) { |
170 |
try {
|
171 |
renderBands[i] = Integer.parseInt(valueList[i]);
|
172 |
} catch (NumberFormatException e) { |
173 |
// No a?ade el valor
|
174 |
} |
175 |
} |
176 |
// filters.remove(propFilter);
|
177 |
// propFilter--;
|
178 |
} |
179 |
|
180 |
if (elem.startsWith("filter.enhanced.remove")) { |
181 |
addEnhancedFilter(Boolean.valueOf(RasterFilterListManager.getValue(elem)).booleanValue(), stats, tailTrim, renderBands);
|
182 |
filters.remove(propFilter); |
183 |
propFilter--; |
184 |
} |
185 |
} |
186 |
|
187 |
filteri = -1;
|
188 |
} |
189 |
return filteri;
|
190 |
} |
191 |
|
192 |
/*
|
193 |
* (non-Javadoc)
|
194 |
* @see org.gvsig.raster.grid.filter.IRasterFilterListManager#getRasterFilterList()
|
195 |
*/
|
196 |
public ArrayList getRasterFilterList() { |
197 |
ArrayList filters = new ArrayList(); |
198 |
filters.add(LinearEnhancementFilter.class); |
199 |
return filters;
|
200 |
} |
201 |
|
202 |
public void addFilter(Class classFilter, Params params) { |
203 |
if (classFilter.equals(LinearEnhancementFilter.class)) {
|
204 |
boolean removeEnds = false; |
205 |
double tailTrim = 0.0; |
206 |
int[] renderBands = { 0, 1, 2 }; |
207 |
|
208 |
for (int i = 0; i < params.getNumParams(); i++) { |
209 |
if (params.getParam(i).id.equals("RenderBands")) { |
210 |
String[] bands = new String((String) params.getParam(i).defaultValue).split(" "); |
211 |
renderBands[0] = new Integer(bands[0]).intValue(); |
212 |
renderBands[1] = new Integer(bands[1]).intValue(); |
213 |
renderBands[2] = new Integer(bands[2]).intValue(); |
214 |
continue;
|
215 |
} |
216 |
if (params.getParam(i).id.equals("RemoveEnds")) { |
217 |
removeEnds = new Boolean((String) params.getParam(i).defaultValue).booleanValue(); |
218 |
continue;
|
219 |
} |
220 |
if (params.getParam(i).id.equals("TailTrim")) |
221 |
tailTrim = Double.parseDouble((String) params.getParam(i).defaultValue) / 100.0; |
222 |
} |
223 |
|
224 |
addEnhancedFilter(removeEnds, (IStatistics) filterList.getParam("IStatistics"), tailTrim, renderBands);
|
225 |
} |
226 |
} |
227 |
} |