Statistics
| Revision:

gvsig-raster / org.gvsig.raster / branches / org.gvsig.raster_dataaccess_refactoring / org.gvsig.raster.lib / org.gvsig.raster.lib.impl / src / main / java / org / gvsig / raster / impl / grid / filter / enhancement / LinearEnhancementFilter.java @ 2311

History | View | Annotate | Download (6.17 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 org.gvsig.fmap.dal.coverage.RasterLibrary;
25
import org.gvsig.fmap.dal.coverage.dataset.Buffer;
26
import org.gvsig.fmap.dal.coverage.datastruct.Params;
27
import org.gvsig.fmap.dal.coverage.exception.FileNotOpenException;
28
import org.gvsig.fmap.dal.coverage.exception.FilterAddException;
29
import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException;
30
import org.gvsig.fmap.dal.coverage.exception.RasterDriverException;
31
import org.gvsig.fmap.dal.coverage.grid.filter.BaseRasterFilter;
32
import org.gvsig.fmap.dal.coverage.store.props.Statistics;
33
import org.gvsig.raster.impl.store.ParamsImpl;
34
/**
35
 * Clase base para los filtros de realzado lineal. Lee el m?nimo y m?xmo de la
36
 * clase Statistic que ser?n calculados por PercentTailTrimFilter o
37
 * ComputeMinMaxFilter dependiendo de si est? activado el recorte de colas o no.
38
 * En Statistic tambi?n est?n los segundos valores despu?s del m?nimo y m?ximo
39
 * que son los que se utilizan con la opci?n eliminar extremos activada. Estos
40
 * se usaran en vez del m?nimo y m?ximo cuando la variable removeExtrema est? a
41
 * true.
42
 *
43
 * @author Nacho Brodin (nachobrodin@gmail.com)
44
 */
45
public class LinearEnhancementFilter extends BaseRasterFilter {
46
        protected double[]                 scale        = new double[3];
47
        protected double[]                 offset       = new double[3];
48
        protected Statistics               stats        = null;
49
        protected double[]                 minBandValue        = null;
50
        protected double[]                 maxBandValue        = null;
51
        protected boolean                  removeEnds   = false;
52
        protected double                   tailTrim     = 0D;
53
        protected int                      nbands       = 3;
54
        public static String[]             names        = new String[] {"enhanced"};
55

    
56
        /**
57
         * Construye un LinearEnhancementFilter
58
         */
59
        public LinearEnhancementFilter() {
60
                setName(names[0]);
61
        }
62

    
63
        public void pre() throws FilterAddException {
64
                super.pre();
65
                stats = (Statistics) params.get("stats");
66
                removeEnds = ((Boolean) params.get("remove")).booleanValue();
67
                tailTrim = ((Double) params.get("tailTrim")).doubleValue();
68
                // En realidad esto no se deberia hacer, pero por logica, un valor del 50%
69
                // devolveria una capa en blanco, asi que evitamos un resultado supuestamente
70
                // no esperado
71
                if ((tailTrim >= 0.5) && (tailTrim < 0.51))
72
                        tailTrim = 0.51;
73
                if ((tailTrim > 0.49) && (tailTrim < 0.50))
74
                        tailTrim = 0.49;
75
                
76
                try {
77
                        stats.calculate(RasterLibrary.statisticsScale);
78
                } catch (FileNotOpenException e) {
79
                        exec = false;
80
                } catch (RasterDriverException e) {
81
                        exec = false;
82
                } catch (ProcessInterruptedException e) {
83
                        exec = false;
84
                }
85
                double[][] tailTrimByBand = (double[][]) stats.getTailTrimValue(tailTrim);
86
                if ((tailTrim != 0) && (tailTrimByBand != null)) { // Max y Min con recorte de colas
87
                        scale = new double[tailTrimByBand.length];
88
                        offset = new double[tailTrimByBand.length];
89
                        minBandValue = new double[tailTrimByBand.length];
90
                        maxBandValue = new double[tailTrimByBand.length];
91
                        for (int i = 0; i < tailTrimByBand.length; i++) {
92
                                minBandValue[i] = tailTrimByBand[i][0];
93
                                maxBandValue[i] = tailTrimByBand[i][1];
94
                        }
95
                } else {
96
                        scale = new double[stats.getMin().length];
97
                        offset = new double[stats.getMin().length];
98
                        if (removeEnds) { // Si est? activado eliminar extremos gastamos el 2? m?ximo/m?nimo
99
                                if(raster.getDataType() == Buffer.TYPE_BYTE) {
100
                                        minBandValue = stats.getSecondMinByteUnsigned();
101
                                        maxBandValue = stats.getSecondMaxByteUnsigned();
102
                                } else {
103
                                        minBandValue = stats.getSecondMin();
104
                                        maxBandValue = stats.getSecondMax();
105
                                }
106
                        } else { // Si no est? activado eliminar extremos
107
                                if(raster.getDataType() == Buffer.TYPE_BYTE) {
108
                                        minBandValue = stats.getMinByteUnsigned();
109
                                        maxBandValue = stats.getMaxByteUnsigned();
110
                                } else {
111
                                        minBandValue = stats.getMin();
112
                                        maxBandValue = stats.getMax();
113
                                }
114
                        }
115
                }
116

    
117
                for (int i = 0; i < minBandValue.length; i++) {
118
                        scale[i] = 255D / (maxBandValue[i] - minBandValue[i]);
119
                        offset[i] = (255D * minBandValue[i]) / (minBandValue[i] - maxBandValue[i]);
120
                }
121

    
122
                nbands = stats.getBandCount();
123
                createBufferResult(Buffer.TYPE_BYTE, raster.getBandCount());
124
        }
125

    
126
        /**
127
         * Obtiene true si est? activado el flag de eliminar extremos y false si no lo
128
         * est?
129
         */
130
        public Boolean getRemoveEnds() {
131
                return new Boolean(removeEnds);
132
        }
133

    
134
        /**
135
         * Obtiene el porcentaje de recorte de colas aplicado o 0 si no tiene.
136
         * @return
137
         */
138
        public Double getTailTrim(){
139
                return new Double(tailTrim);
140
        }
141

    
142
        public int getOutRasterDataType() {
143
                return Buffer.TYPE_BYTE;
144
        }
145

    
146
        public String getGroup() {
147
                return "radiometricos";
148
        }
149

    
150
        public Params getUIParams(String nameFilter) {
151
                Params params = new ParamsImpl();
152
                params.setParam("RemoveEnds",
153
                                new Boolean(removeEnds),
154
                                Params.CHECK,
155
                                null);
156
                params.setParam("TailTrim",
157
                                new Double(Math.round(tailTrim * 100.0)),
158
                                Params.SLIDER,
159
                                new String[]{ "0", "100", "0", "1", "25" }); //min, max, valor defecto, intervalo peque?o, intervalo grande;
160
                return params;
161
        }
162

    
163
        public void post() {
164
                // En caso de que nadie apunte a raster, se liberar? su memoria.
165
                raster = null;
166
        }
167

    
168
        public int getInRasterDataType() {
169
                return 0;
170
        }
171

    
172
        public void process(int x, int y) {
173
        }
174

    
175
        public String[] getNames() {
176
                return names;
177
        }
178
        
179
        public boolean isVisible() {
180
                return true;
181
        }
182
}