Statistics
| Revision:

root / trunk / libraries / libRaster / src / org / gvsig / raster / dataset / properties / DatasetHistogram.java @ 11068

History | View | Annotate | Download (6.54 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2007 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.dataset.properties;
20

    
21
import org.gvsig.raster.RasterLibrary;
22
import org.gvsig.raster.dataset.FileNotOpenException;
23
import org.gvsig.raster.dataset.IBuffer;
24
import org.gvsig.raster.dataset.InvalidSetViewException;
25
import org.gvsig.raster.dataset.RasterDataset;
26
import org.gvsig.raster.dataset.RasterDriverException;
27
import org.gvsig.raster.util.DataClassList;
28
import org.gvsig.raster.util.Histogram;
29

    
30
/**
31
 * Clase para la gesti?n de histogramas de un raster. Es la encargada del calculo de un histograma
32
 * total o parcial de un raster a partir de los datos de disco. Adem?s tambi?n es la encargada de gestionar
33
 * salvar este histograma a .rmf. En caso de que solicite un histograma del raster completo este ir?
34
 * a buscarlo al fichero rmf asociado antes de calcularlo por si ya existise. Al realizar el calculo
35
 * del histograma de la imagen completa este ser? salvado en el fichero .rmf asociado al raster.
36
 * 
37
 * @author Nacho Brodin (nachobrodin@gmail.com)
38
 */
39
public class DatasetHistogram {
40
        /**
41
         * clases en las que se divide el histograma
42
         */
43
        private DataClassList                                 classes = null;
44
        /**
45
         * Histograma de la imagen completa
46
         */
47
        private Histogram                                        histogram = null;
48
        /**
49
         * Dataset del cual se calcula el histograma
50
         */
51
        private RasterDataset                                dataset = null;
52
        
53
        private int percent = 0;
54
        private boolean canceled = false;
55
        
56
        /**
57
         * Constructor
58
         * @param dataset
59
         */
60
        public DatasetHistogram(RasterDataset dataset){
61
                this.dataset = dataset;
62
        }
63
        
64
        /**
65
         * Asigna una lista de clases sobre la que se calcular? la petici?n de
66
         * histograma.
67
         * @param classes
68
         */
69
        public void setClasses(DataClassList classes){
70
                this.classes = classes;
71
        }
72
        
73
        /**
74
         * Obtiene el histograma. Si puede conseguirlo del fichero rmf ir? all? a 
75
         * buscarlo sino lo calcular?.
76
         * @return histograma 
77
         */
78
        public Histogram getHistogram() throws FileNotOpenException, RasterDriverException {
79
                try {
80
                        if(dataset != null){
81
                                if(classes == null){
82
                                        if(dataset.getDataType() == IBuffer.TYPE_BYTE){
83
                                                histogram = new Histogram(dataset.getBandCount(), 256);
84
                                                return getHistogramByValue();
85
                                        }else{
86
                                                DatasetStatistics stats = dataset.getStatistics();
87
                                                stats.calcFullStatistics();
88
                                                classes = DataClassList.getClasses(stats.getMinimun(), stats.getMaximun(), RasterLibrary.defaultNumberOfClasses);
89
                                        }
90
                                                
91
                                }
92
                                histogram = new Histogram(dataset.getBandCount(), classes.length());
93
                                return getHistogramByClass();
94
                        }
95
                } catch (InvalidSetViewException e) {
96
                        //La vista se selecciona autom?ticamente no deber?a darse esta excepci?n
97
                }
98
                return null;
99
        }
100
        
101
        /**
102
         * Obtiene un histograma para valores de tipo byte. El resto de tipos
103
         * de datos se separan por clases, bien introducidas por el usuario
104
         * bien autocalculadas.
105
         * 
106
         * @return histograma
107
         */
108
        private Histogram getHistogramByValue() throws InvalidSetViewException, FileNotOpenException, RasterDriverException {
109
                percent = 0;
110
                if(dataset.getDataType() != IBuffer.TYPE_BYTE)
111
                        return null;
112
                
113
                for (int band = 0; band < dataset.getBandCount(); band++) {
114
                        for (int line = 0; line < dataset.getHeight(); line++) {
115
                                Object obj = dataset.readCompleteLine(line, band);
116
                                byte[] bLine = (byte[])obj;
117
                                for (int k = 0; k < bLine.length; k++)
118
                                        histogram.incrementPxValue(band, (bLine[k] & 0xff));
119
                                if (isCanceled()) return null;
120
                                percent = ((band * dataset.getHeight() + line) * 100) / (dataset.getBandCount() * dataset.getHeight());
121
                        }
122
                } 
123
                percent = 100;
124
                return histogram;
125
        }
126
        
127
        /**
128
         * Obtiene el histograma teniendo en cuenta la lista de clases
129
         * @return Histograma correspondiente a la lista de clases
130
         */
131
        private Histogram getHistogramByClass() throws InvalidSetViewException, FileNotOpenException, RasterDriverException {
132
                if(classes == null)
133
                        return null;
134
                for (int band = 0; band < dataset.getBandCount(); band++) {
135
                        for (int line = 0; line < dataset.getHeight(); line++) {
136
                                Object obj = dataset.readCompleteLine(line, band);
137
                                switch(dataset.getDataType()){
138
                                case IBuffer.TYPE_BYTE:                byte[] bLine = (byte[])obj;
139
                                                                                        for (int k = 0; k < bLine.length; k++) {
140
                                                                                                int pixelValue = classes.getClassPosition((double)bLine[k]);
141
                                                                                                if(pixelValue >= 0)
142
                                                                                                        histogram.incrementPxValue(band, pixelValue);
143
                                                                                                
144
                                                                                        }
145
                                                                                        break;
146
                                case IBuffer.TYPE_SHORT:        short[] sLine = (short[])obj;
147
                                                                                        for (int k = 0; k < sLine.length; k++) {
148
                                                                                                int pixelValue = classes.getClassPosition((double)sLine[k]);
149
                                                                                                if(pixelValue >= 0)
150
                                                                                                        histogram.incrementPxValue(band, pixelValue);
151
                                                                                        }
152
                                                                                        break;
153
                                case IBuffer.TYPE_INT:                int[] iLine = (int[])obj;
154
                                                                                        for (int k = 0; k < iLine.length; k++) {
155
                                                                                                int pixelValue = classes.getClassPosition((double)iLine[k]);
156
                                                                                                if(pixelValue >= 0)
157
                                                                                                        histogram.incrementPxValue(band, pixelValue);
158
                                                                                        }
159
                                                                                        break;
160
                                case IBuffer.TYPE_FLOAT:        float[] fLine = (float[])obj;
161
                                                                                        for (int k = 0; k < fLine.length; k++) {
162
                                                                                                int pixelValue = classes.getClassPosition((double)fLine[k]);
163
                                                                                                if(pixelValue >= 0)
164
                                                                                                        histogram.incrementPxValue(band, pixelValue);
165
                                                                                        }
166
                                                                                        break;
167
                                case IBuffer.TYPE_DOUBLE:        double[] dLine = (double[])obj;
168
                                                                                        for (int k = 0; k < dLine.length; k++) {
169
                                                                                                int pixelValue = classes.getClassPosition((double)dLine[k]);
170
                                                                                                if(pixelValue >= 0)
171
                                                                                                        histogram.incrementPxValue(band, pixelValue);
172
                                                                                        }
173
                                                                                        break;
174
                                }
175
                                
176
                        }
177
                } 
178
                return histogram;
179
        }
180

    
181
        public void resetPercent() {
182
                percent=0;
183
        }
184

    
185
        public int getPercent() {
186
                return percent;
187
        }
188
        
189
        public void setCanceled(boolean value) {
190
                canceled = value;
191
        }
192
        
193
        public boolean isCanceled() {
194
                return canceled;
195
        }
196
}