Statistics
| Revision:

gvsig-raster / org.gvsig.raster / trunk / org.gvsig.raster / org.gvsig.raster.lib / org.gvsig.raster.lib.impl / src / main / java / org / gvsig / raster / impl / grid / filter / enhancement / EnhancementListManager.java @ 1426

History | View | Annotate | Download (10.3 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22
package org.gvsig.raster.impl.grid.filter.enhancement;
23

    
24
import java.util.ArrayList;
25
import java.util.List;
26

    
27
import org.gvsig.fmap.dal.coverage.RasterLibrary;
28
import org.gvsig.fmap.dal.coverage.datastruct.Params;
29
import org.gvsig.fmap.dal.coverage.exception.FileNotOpenException;
30
import org.gvsig.fmap.dal.coverage.exception.FilterTypeException;
31
import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException;
32
import org.gvsig.fmap.dal.coverage.exception.RasterDriverException;
33
import org.gvsig.fmap.dal.coverage.grid.RasterFilter;
34
import org.gvsig.fmap.dal.coverage.grid.RasterFilterList;
35
import org.gvsig.fmap.dal.coverage.grid.RasterFilterListManager;
36
import org.gvsig.fmap.dal.coverage.store.props.Statistics;
37
import org.gvsig.raster.impl.grid.filter.RasterFilterListManagerImpl;
38
import org.gvsig.raster.impl.grid.filter.statistics.StatisticsListManager;
39
import org.gvsig.raster.impl.store.ParamImpl;
40
import org.gvsig.tools.ToolsLocator;
41
import org.gvsig.tools.extensionpoint.ExtensionPoint;
42
import org.gvsig.tools.extensionpoint.ExtensionPointManager;
43
/**
44
 * Gestor de la pila de filtros para el filtro de realce.
45
 * @author Nacho Brodin (nachobrodin@gmail.com)
46
 */
47
public class EnhancementListManager implements RasterFilterListManager {
48
        protected RasterFilterList          filterList        = null;
49
        private RasterFilterListManagerImpl filterListManager = null;
50
        private Statistics                  stats             = null;
51

    
52
        /**
53
         * Constructor
54
         * @param filterListManager
55
         */
56
        public EnhancementListManager(RasterFilterListManagerImpl filterListManager) {
57
                this.filterListManager = filterListManager;
58
                this.filterList = filterListManager.getFilterList();
59
                stats = (Statistics)filterList.getEnvParam("IStatistics");
60
        }
61
        
62
        public Class<?> getFilterClassByID(String id) {
63
                if(id.compareTo("enhanced") == 0)
64
                        return LinearEnhancementFilter.class;
65
                return null;
66
        }
67

    
68
        /**
69
         * Asigna el objeto con las estadisticas.
70
         * @param stats
71
         */
72
        public void setStatistics(Statistics stats) {
73
                this.stats = stats;
74
        }
75

    
76
        /**
77
         * Registra EnhancementListManager en los puntos de extension de RasterFilter
78
         */
79
        public static void register() {
80
                ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager();
81
                ExtensionPoint point = extensionPoints.get("RasterFilter");
82
                point.append("Enhancement", "", EnhancementListManager.class);
83
        }
84

    
85
        /**
86
         * A?ade un filtro de realce.
87
         * La forma de inserci?n del filtro es fija ya que la inserci?n de un realce lleva implicita
88
         * la inserci?n de un filtro de recorte de colas (tailtrim), aunque no en todos los casos.
89
         * Si ya existe un filtro de realce en la lista se obtiene la posici?n de este.
90
         * Si es necesario un recorte de colas entonces se comprueba si existe un uno reemplazandose
91
         * por el nuevo y sino se insertar? uno. Al final reemplazamos el realce que existia.
92
         *
93
         * Si por el contrario no existen realce ni trim se a?aden ambos al final de la lista.
94
         * @param removeEnds eliminar extremos en los m?ximos y m?nimos
95
         * @param stats Objeto de estadisticas asociado
96
         * @param tailTrim porcentaje de recorte de colas. Ser? un valor entre 0 y 1.
97
         * @param insertionMode Modo de inserci?n
98
         * @param renderBands bandas RGB mostradas en la visualizaci?n.
99
         * @throws FilterTypeException
100
         */
101
        public void addEnhancedFilter(boolean removeEnds, Statistics stats, double tailTrim, int[] renderBands) throws FilterTypeException {
102
                try {
103
                        if (tailTrim == 0) { // En este caso siempre es necesario el m?ximo y
104
                                // m?nimo
105
                                if (!stats.isCalculated()) {
106
                                        try {
107
                                                stats.calculate(RasterLibrary.statisticsScale);
108
                                        } catch (FileNotOpenException e) {
109
                                                // No podemos aplicar el filtro
110
                                                return;
111
                                        } catch (RasterDriverException e) {
112
                                                // No podemos aplicar el filtro
113
                                                return;
114
                                        }
115
                                }
116
                        } else {
117
                                StatisticsListManager slm = new StatisticsListManager(filterListManager, stats);
118
                                slm.addTailFilter(tailTrim, 0D, removeEnds, stats);
119
                        }
120

    
121
                        RasterFilter filter = createEnhancedFilter(removeEnds, stats, tailTrim, renderBands);
122
                        if (filter != null)
123
                                filterList.add(filter);
124
                } catch (ProcessInterruptedException e) {
125
                        //Si se ha interrumpido no a?adimos el filtro
126
                }
127
        }
128

    
129
        public static RasterFilter createEnhancedFilter(boolean removeEnds, Statistics stats, double tailTrim, int[] renderBands) {
130
                RasterFilter filter = new LinearEnhancementFloatFilter();
131
                if (filter != null) {
132
                        filter.addParam("stats", stats);
133
                        if (removeEnds) {
134
                                filter.addParam("remove", new Boolean(true));
135
                        } else {
136
                                filter.addParam("remove", new Boolean(false));
137
                        }
138
                        filter.addParam("tailTrim", new Double(tailTrim));
139
                        filter.addParam("renderBands", renderBands);
140
                }
141

    
142
                return filter;
143
        }
144

    
145
        /**
146
         * Obtiene un Array de Strings a partir de una pila de filtros. Cada elemento
147
         * del array tendr? la forma de elemento=valor.
148
         */
149
        public List<String> getStringsFromFilterList(List<String> filterList, RasterFilter rf) {
150
                if (rf instanceof LinearEnhancementFilter) {
151
                        filterList.add("filter.enhanced.active=true");
152
                        filterList.add("filter.enhanced.tailTrim=" + ((LinearEnhancementFilter) rf).getTailTrim().toString());
153
                        StringBuffer values = new StringBuffer();
154
                        for (int i = 0; i < ((LinearEnhancementFilter) rf).renderBands.length; i++) {
155
                                values.append(((LinearEnhancementFilter) rf).renderBands[i]);
156
                                if (i < ((LinearEnhancementFilter) rf).renderBands.length - 1)
157
                                        values.append(",");
158
                        }
159
                        filterList.add("filter.enhanced.renderbands=" + values.toString());
160
                        filterList.add("filter.enhanced.remove=" + ((LinearEnhancementFilter) rf).getRemoveEnds().toString());
161
                }
162

    
163
                return filterList;
164
        }
165

    
166
        public int createFilterListFromStrings(List<String> filters, String fil, int filteri) throws FilterTypeException {
167
                if (fil.startsWith("filter.enhanced.active") && RasterFilterListManagerImpl.getValue(fil).equals("true") && stats != null) {
168
                        filters.remove(filteri);
169
                        double tailTrim = 0D;
170
                        int[] renderBands = new int[] { 0, 0, 0 };
171

    
172
                        for (int propFilter = 0; propFilter < filters.size(); propFilter++) {
173
                                String elem = (String) filters.get(propFilter);
174

    
175
                                if (elem.startsWith("filter.enhanced.tailTrim")) {
176
                                        try {
177
                                                tailTrim = new Double(RasterFilterListManagerImpl.getValue(elem)).doubleValue();
178
                                        } catch (NumberFormatException ex) {
179
                                                // tailTrim sigue valiendo 0
180
                                        }
181
                                }
182

    
183
                                if (elem.startsWith("filter.enhanced.renderbands")) {
184
                                        String value = RasterFilterListManagerImpl.getValue(elem);
185
                                        String[] valueList = value.split(",");
186
                                        renderBands = new int[valueList.length];
187
                                        for (int i = 0; i < renderBands.length; i++) {
188
                                                try {
189
                                                        renderBands[i] = Integer.parseInt(valueList[i]);
190
                                                } catch (NumberFormatException e) {
191
                                                        // No a?ade el valor
192
                                                }
193
                                        }
194
                                        // filters.remove(propFilter);
195
                                        // propFilter--;
196
                                }
197

    
198
                                if (elem.startsWith("filter.enhanced.remove")) {
199
                                        addEnhancedFilter(Boolean.valueOf(RasterFilterListManagerImpl.getValue(elem)).booleanValue(), stats, tailTrim, renderBands);
200
                                        filters.remove(propFilter);
201
                                        propFilter--;
202
                                }
203
                        }
204

    
205
                        filteri = -1;
206
                }
207
                return filteri;
208
        }
209

    
210
        public List<Class<?>> getRasterFilterList() {
211
                List<Class<?>> filters = new ArrayList<Class<?>>();
212
                filters.add(LinearEnhancementFilter.class);
213
                return filters;
214
        }
215

    
216
        public void addFilter(Class<?> classFilter, Params params) throws FilterTypeException {
217
                if (LinearEnhancementFilter.class.isAssignableFrom(classFilter)) {
218
                        boolean removeEnds = false;
219
                        double tailTrim = 0.0;
220
                        int[] renderBands = { 0, 1, 2 };
221

    
222
                        for (int i = 0; i < params.getNumParams(); i++) {
223
                                if (((ParamImpl)params.getParam(i)).getId().equals("RenderBands") &&
224
                                        ((ParamImpl)params.getParam(i)).getDefaultValue() instanceof String) {
225
                                        String[] bands = new String((String) ((ParamImpl)params.getParam(i)).getDefaultValue()).split(" ");
226
                                        renderBands[0] = new Integer(bands[0]).intValue();
227
                                        renderBands[1] = new Integer(bands[1]).intValue();
228
                                        renderBands[2] = new Integer(bands[2]).intValue();
229
                                        continue;
230
                                }
231
                                if (((ParamImpl)params.getParam(i)).getId().equals("RemoveEnds") &&
232
                                        ((ParamImpl)params.getParam(i)).getDefaultValue() instanceof Boolean) {
233
                                        removeEnds = ((Boolean) ((ParamImpl)params.getParam(i)).getDefaultValue()).booleanValue();
234
                                        continue;
235
                                }
236
                                if (((ParamImpl)params.getParam(i)).getId().equals("TailTrim") &&
237
                                        ((ParamImpl)params.getParam(i)).getDefaultValue() instanceof Double)
238
                                        tailTrim = ((Double) ((ParamImpl)params.getParam(i)).getDefaultValue()).doubleValue() / 100.0;
239
                        }
240

    
241
                        addEnhancedFilter(removeEnds, (Statistics) filterList.getEnvParam("IStatistics"), tailTrim, renderBands);
242
                }
243
        }
244
        
245
        /*
246
         * (non-Javadoc)
247
         * @see org.gvsig.fmap.dal.coverage.grid.RasterFilterListManager#addFilter(org.gvsig.fmap.dal.coverage.datastruct.Params)
248
         */
249
        public void addFilter(Params params) throws FilterTypeException {
250
                addFilter(LinearEnhancementFilter.class, params);
251
        }
252
        
253
        /*
254
         * (non-Javadoc)
255
         * @see org.gvsig.fmap.dal.coverage.grid.RasterFilterListManager#createFilter(org.gvsig.fmap.dal.coverage.datastruct.Params)
256
         */
257
        public RasterFilter createFilter(Params params) {
258
                //TODO: crear un filtro de este tipo
259
                return null;
260
        }
261
        
262
        /*
263
         * (non-Javadoc)
264
         * @see org.gvsig.fmap.dal.coverage.grid.RasterFilterListManager#getFilterList()
265
         */
266
        public RasterFilterList getFilterList() {
267
                return filterList;
268
        }
269
        
270
        /*
271
         * (non-Javadoc)
272
         * @see org.gvsig.fmap.dal.coverage.grid.RasterFilterListManager#setFilterList(org.gvsig.fmap.dal.coverage.grid.RasterFilterList)
273
         */
274
        public void setFilterList(RasterFilterList filterList) {
275
                this.filterList = filterList;
276
        }
277
}