Statistics
| Revision:

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

History | View | Annotate | Download (6.31 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
        
55
        /**
56
         * Constructor
57
         * @param dataset
58
         */
59
        public DatasetHistogram(RasterDataset dataset){
60
                this.dataset = dataset;
61
        }
62
        
63
        /**
64
         * Asigna una lista de clases sobre la que se calcular? la petici?n de
65
         * histograma.
66
         * @param classes
67
         */
68
        public void setClasses(DataClassList classes){
69
                this.classes = classes;
70
        }
71
        
72
        /**
73
         * Obtiene el histograma. Si puede conseguirlo del fichero rmf ir? all? a 
74
         * buscarlo sino lo calcular?.
75
         * @return histograma 
76
         */
77
        public Histogram getHistogram() throws FileNotOpenException, RasterDriverException {
78
                try {
79
                        if(dataset != null){
80
                                if(classes == null){
81
                                        if(dataset.getDataType() == IBuffer.TYPE_BYTE){
82
                                                histogram = new Histogram(dataset.getBandCount(), 256);
83
                                                return getHistogramByValue();
84
                                        }else{
85
                                                DatasetStatistics stats = dataset.getStatistics();
86
                                                stats.calcFullStatistics();
87
                                                classes = DataClassList.getClasses(stats.getMinimun(), stats.getMaximun(), RasterLibrary.defaultNumberOfClasses);
88
                                        }
89
                                                
90
                                }
91
                                histogram = new Histogram(dataset.getBandCount(), classes.length());
92
                                return getHistogramByClass();
93
                        }
94
                } catch (InvalidSetViewException e) {
95
                        //La vista se selecciona autom?ticamente no deber?a darse esta excepci?n
96
                }
97
                return null;
98
        }
99
        
100
        /**
101
         * Obtiene un histograma para valores de tipo byte. El resto de tipos
102
         * de datos se separan por clases, bien introducidas por el usuario
103
         * bien autocalculadas.
104
         * 
105
         * @return histograma
106
         */
107
        private Histogram getHistogramByValue() throws InvalidSetViewException, FileNotOpenException, RasterDriverException {
108
                percent = 0;
109
                if(dataset.getDataType() != IBuffer.TYPE_BYTE)
110
                        return null;
111
                
112
                for (int band = 0; band < dataset.getBandCount(); band++) {
113
                        for (int line = 0; line < dataset.getHeight(); line++) {
114
                                Object obj = dataset.readCompleteLine(line, band);
115
                                byte[] bLine = (byte[])obj;
116
                                for (int k = 0; k < bLine.length; k++)
117
                                        histogram.incrementPxValue(band, (bLine[k] & 0xff));
118
                                percent = ((band * dataset.getHeight() + line) * 100) / (dataset.getBandCount() * dataset.getHeight());
119
                        }
120
                } 
121
                percent = 100;
122
                return histogram;
123
        }
124
        
125
        /**
126
         * Obtiene el histograma teniendo en cuenta la lista de clases
127
         * @return Histograma correspondiente a la lista de clases
128
         */
129
        private Histogram getHistogramByClass() throws InvalidSetViewException, FileNotOpenException, RasterDriverException {
130
                if(classes == null)
131
                        return null;
132
                for (int band = 0; band < dataset.getBandCount(); band++) {
133
                        for (int line = 0; line < dataset.getHeight(); line++) {
134
                                Object obj = dataset.readCompleteLine(line, band);
135
                                switch(dataset.getDataType()){
136
                                case IBuffer.TYPE_BYTE:                byte[] bLine = (byte[])obj;
137
                                                                                        for (int k = 0; k < bLine.length; k++) {
138
                                                                                                int pixelValue = classes.getClassPosition((double)bLine[k]);
139
                                                                                                if(pixelValue >= 0)
140
                                                                                                        histogram.incrementPxValue(band, pixelValue);
141
                                                                                                
142
                                                                                        }
143
                                                                                        break;
144
                                case IBuffer.TYPE_SHORT:        short[] sLine = (short[])obj;
145
                                                                                        for (int k = 0; k < sLine.length; k++) {
146
                                                                                                int pixelValue = classes.getClassPosition((double)sLine[k]);
147
                                                                                                if(pixelValue >= 0)
148
                                                                                                        histogram.incrementPxValue(band, pixelValue);
149
                                                                                        }
150
                                                                                        break;
151
                                case IBuffer.TYPE_INT:                int[] iLine = (int[])obj;
152
                                                                                        for (int k = 0; k < iLine.length; k++) {
153
                                                                                                int pixelValue = classes.getClassPosition((double)iLine[k]);
154
                                                                                                if(pixelValue >= 0)
155
                                                                                                        histogram.incrementPxValue(band, pixelValue);
156
                                                                                        }
157
                                                                                        break;
158
                                case IBuffer.TYPE_FLOAT:        float[] fLine = (float[])obj;
159
                                                                                        for (int k = 0; k < fLine.length; k++) {
160
                                                                                                int pixelValue = classes.getClassPosition((double)fLine[k]);
161
                                                                                                if(pixelValue >= 0)
162
                                                                                                        histogram.incrementPxValue(band, pixelValue);
163
                                                                                        }
164
                                                                                        break;
165
                                case IBuffer.TYPE_DOUBLE:        double[] dLine = (double[])obj;
166
                                                                                        for (int k = 0; k < dLine.length; k++) {
167
                                                                                                int pixelValue = classes.getClassPosition((double)dLine[k]);
168
                                                                                                if(pixelValue >= 0)
169
                                                                                                        histogram.incrementPxValue(band, pixelValue);
170
                                                                                        }
171
                                                                                        break;
172
                                }
173
                                
174
                        }
175
                } 
176
                return histogram;
177
        }
178
        
179
        public int getPercent() {
180
                return percent;
181
        }
182
}