Statistics
| Revision:

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 11788 bsanchez
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2 10740 nacho
 *
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 11788 bsanchez
import org.gvsig.raster.dataset.FileNotOpenException;
24 11750 bsanchez
import org.gvsig.raster.dataset.Params;
25 28498 nbrodin
import org.gvsig.raster.dataset.RasterDriverException;
26 17108 nbrodin
import org.gvsig.raster.grid.filter.FilterTypeException;
27 10740 nacho
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 11788 bsanchez
import org.gvsig.raster.grid.filter.statistics.StatisticsListManager;
32 12383 nacho
import org.gvsig.raster.hierarchy.IStatistics;
33 26873 vcaballero
import org.gvsig.tools.ToolsLocator;
34
import org.gvsig.tools.extensionpoint.ExtensionPoint;
35
import org.gvsig.tools.extensionpoint.ExtensionPointManager;
36 10740 nacho
/**
37
 * Gestor de la pila de filtros para el filtro de realce.
38
 * @author Nacho Brodin (nachobrodin@gmail.com)
39
 */
40 11652 bsanchez
public class EnhancementListManager implements IRasterFilterListManager {
41 22294 bsanchez
        protected RasterFilterList      filterList        = null;
42
        private RasterFilterListManager filterListManager = null;
43
        private IStatistics             stats             = null;
44 11963 bsanchez
45 10740 nacho
        /**
46
         * Constructor
47
         * @param filterListManager
48
         */
49 11652 bsanchez
        public EnhancementListManager(RasterFilterListManager filterListManager) {
50 10740 nacho
                this.filterListManager = filterListManager;
51
                this.filterList = filterListManager.getFilterList();
52 15778 nbrodin
                stats = (IStatistics)filterList.getEnvParam("IStatistics");
53 10740 nacho
        }
54 11963 bsanchez
55 11721 nacho
        /**
56
         * Asigna el objeto con las estadisticas.
57
         * @param stats
58
         */
59
        public void setStatistics(IStatistics stats) {
60
                this.stats = stats;
61
        }
62 11963 bsanchez
63 22294 bsanchez
        /**
64
         * Registra EnhancementListManager en los puntos de extension de RasterFilter
65
         */
66 12122 bsanchez
        public static void register() {
67 26873 vcaballero
                ExtensionPointManager extensionPoints =ToolsLocator.getExtensionPointManager();
68
                ExtensionPoint point=extensionPoints.get("RasterFilter");
69
                point.append("Enhancement", "", EnhancementListManager.class);
70 11710 bsanchez
        }
71 11963 bsanchez
72 11803 bsanchez
        /**
73 11818 nacho
         * 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 12333 bsanchez
         * la inserci?n de un filtro de recorte de colas (tailtrim), aunque no en todos los casos.
76 11818 nacho
         * Si ya existe un filtro de realce en la lista se obtiene la posici?n de este.
77 12333 bsanchez
         * Si es necesario un recorte de colas entonces se comprueba si existe un uno reemplazandose
78 11818 nacho
         * por el nuevo y sino se insertar? uno. Al final reemplazamos el realce que existia.
79 12333 bsanchez
         *
80 11818 nacho
         * Si por el contrario no existen realce ni trim se a?aden ambos al final de la lista.
81 12333 bsanchez
         * @param removeEnds eliminar extremos en los m?ximos y m?nimos
82 11818 nacho
         * @param stats Objeto de estadisticas asociado
83
         * @param tailTrim porcentaje de recorte de colas. Ser? un valor entre 0 y 1.
84 12333 bsanchez
         * @param insertionMode Modo de inserci?n
85 11818 nacho
         * @param renderBands bandas RGB mostradas en la visualizaci?n.
86 26873 vcaballero
         * @throws FilterTypeException
87 11803 bsanchez
         */
88 17108 nbrodin
        public void addEnhancedFilter(boolean removeEnds, IStatistics stats, double tailTrim, int[] renderBands) throws FilterTypeException {
89 13409 nacho
                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 11802 nacho
                                }
103 13409 nacho
                        } else {
104
                                StatisticsListManager slm = new StatisticsListManager(filterListManager, stats);
105
                                slm.addTailFilter(tailTrim, 0D, removeEnds, stats);
106 11802 nacho
                        }
107 13409 nacho
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 11792 bsanchez
                }
114 12180 bsanchez
        }
115
116
        public static RasterFilter createEnhancedFilter(boolean removeEnds, IStatistics stats, double tailTrim, int[] renderBands) {
117
                RasterFilter filter = new LinearEnhancementFloatFilter();
118 11803 bsanchez
                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 12180 bsanchez
                }
128 11803 bsanchez
129 12180 bsanchez
                return filter;
130 11803 bsanchez
        }
131 10740 nacho
132
        /**
133 11803 bsanchez
         * Obtiene un Array de Strings a partir de una pila de filtros. Cada elemento
134
         * del array tendr? la forma de elemento=valor.
135 10740 nacho
         */
136 11716 nacho
        public ArrayList getStringsFromFilterList(ArrayList filterList, RasterFilter rf) {
137 10740 nacho
                if (rf instanceof LinearEnhancementFilter) {
138 11803 bsanchez
                        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 11711 nacho
                                values.append(((LinearEnhancementFilter) rf).renderBands[i]);
143 11803 bsanchez
                                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 10740 nacho
                }
149 11803 bsanchez
150 10740 nacho
                return filterList;
151
        }
152
153 11716 nacho
        /*
154
         * (non-Javadoc)
155
         * @see org.gvsig.raster.grid.filter.IRasterFilterListManager#createFilterListFromStrings(java.util.ArrayList, java.lang.String, int)
156
         */
157 17108 nbrodin
        public int createFilterListFromStrings(ArrayList filters, String fil, int filteri) throws FilterTypeException {
158 13652 nacho
                if (fil.startsWith("filter.enhanced.active") && RasterFilterListManager.getValue(fil).equals("true") && stats != null) {
159 11803 bsanchez
                        filters.remove(filteri);
160
                        double tailTrim = 0D;
161
                        int[] renderBands = new int[] { 0, 0, 0 };
162 10740 nacho
163 11803 bsanchez
                        for (int propFilter = 0; propFilter < filters.size(); propFilter++) {
164
                                String elem = (String) filters.get(propFilter);
165 10740 nacho
166 11803 bsanchez
                                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 10740 nacho
174 11803 bsanchez
                                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 12166 bsanchez
                                        addEnhancedFilter(Boolean.valueOf(RasterFilterListManager.getValue(elem)).booleanValue(), stats, tailTrim, renderBands);
191 11803 bsanchez
                                        filters.remove(propFilter);
192
                                        propFilter--;
193
                                }
194
                        }
195
196
                        filteri = -1;
197
                }
198
                return filteri;
199 10740 nacho
        }
200
201 11652 bsanchez
        /*
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 11553 bsanchez
        }
210 11750 bsanchez
211 17108 nbrodin
        public void addFilter(Class classFilter, Params params) throws FilterTypeException {
212 11786 bsanchez
                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 26873 vcaballero
                                if (params.getParam(i).id.equals("RenderBands") &&
219 14249 nbrodin
                                        params.getParam(i).defaultValue instanceof String) {
220 12333 bsanchez
                                        String[] bands = new String((String) params.getParam(i).defaultValue).split(" ");
221 11811 bsanchez
                                        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 14249 nbrodin
                                if (params.getParam(i).id.equals("RemoveEnds") &&
227
                                        params.getParam(i).defaultValue instanceof Boolean) {
228
                                        removeEnds = ((Boolean) params.getParam(i).defaultValue).booleanValue();
229 11811 bsanchez
                                        continue;
230
                                }
231 14249 nbrodin
                                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 11786 bsanchez
                        }
235
236 15778 nbrodin
                        addEnhancedFilter(removeEnds, (IStatistics) filterList.getEnvParam("IStatistics"), tailTrim, renderBands);
237 11786 bsanchez
                }
238 11750 bsanchez
        }
239 11652 bsanchez
}