Statistics
| Revision:

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

History | View | Annotate | Download (5.27 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.Histogram;
28

    
29
/**
30
 * Clase para la gesti?n de histogramas de un raster. Es la encargada del calculo de un histograma
31
 * total o parcial de un raster a partir de los datos de disco. Adem?s tambi?n es la encargada de gestionar
32
 * salvar este histograma a .rmf. En caso de que solicite un histograma del raster completo este ir?
33
 * a buscarlo al fichero rmf asociado antes de calcularlo por si ya existise. Al realizar el calculo
34
 * del histograma de la imagen completa este ser? salvado en el fichero .rmf asociado al raster.
35
 * 
36
 * @author Nacho Brodin (nachobrodin@gmail.com)
37
 */
38
public class DatasetHistogram {
39
        /**
40
         * Histograma de la imagen completa
41
         */
42
        private Histogram                                        histogram = null;
43
        /**
44
         * Dataset del cual se calcula el histograma
45
         */
46
        private RasterDataset                                dataset = null;
47
        
48
        private int percent = 0;
49
        private boolean canceled = false;
50
        
51
        /**
52
         * Constructor
53
         * @param dataset
54
         */
55
        public DatasetHistogram(RasterDataset dataset){
56
                this.dataset = dataset;
57
        }
58

    
59
        /**
60
         * Obtiene el minimo valor de las estadisticas de un histograma.
61
         * @return double
62
         */
63
        public double getMinimum() {
64
                return dataset.getStatistics().getMinimun();
65
        }
66

    
67
        /**
68
         * Obtiene el maximo valor de las estadisticas de un histograma.
69
         * @return double
70
         */
71
        public double getMaximum() {
72
                return dataset.getStatistics().getMaximun();
73
        }
74
        /**
75
         * Obtiene el histograma. Si puede conseguirlo del fichero rmf ir? all? a 
76
         * buscarlo sino lo calcular?.
77
         * @return histograma 
78
         */
79
        public Histogram getHistogram() throws FileNotOpenException, RasterDriverException {
80
                try {
81
                        if (dataset != null) {
82

    
83
                                dataset.getStatistics().calcFullStatistics();
84

    
85
                                if (isCanceled()) 
86
                                        return null;
87

    
88
                                if (dataset.getDataType() == IBuffer.TYPE_BYTE)
89
                                        histogram = new Histogram(dataset.getBandCount(), 255, dataset.getStatistics().getMinimun(), dataset.getStatistics().getMaximun());
90
                                else
91
                                        histogram = new Histogram(dataset.getBandCount(), RasterLibrary.defaultNumberOfClasses, dataset.getStatistics().getMinimun(), dataset.getStatistics().getMaximun());
92
                                                                        
93
                                return getHistogramByDataType();
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 el histograma teniendo en cuenta la lista de clases
103
         * @return Histograma correspondiente a la lista de clases
104
         */
105
        private Histogram getHistogramByDataType() throws InvalidSetViewException, FileNotOpenException, RasterDriverException {
106
                percent = 0;
107
                for (int band = 0; band < dataset.getBandCount(); band++) {
108
                        for (int line = 0; line < dataset.getHeight(); line++) {
109
                                Object obj = dataset.readCompleteLine(line, band);
110
                                switch(dataset.getDataType()) {
111
                                        case IBuffer.TYPE_BYTE:
112
                                                byte[] bLine = (byte[])obj;
113
                                                for (int k = 0; k < bLine.length; k++)
114
                                                        histogram.incrementPxValue(band, (double)(bLine[k] & 0xff));
115
                                                break;
116
                                        case IBuffer.TYPE_SHORT:
117
                                                short[] sLine = (short[])obj;
118
                                                for (int k = 0; k < sLine.length; k++)
119
                                                        histogram.incrementPxValue(band, (double)(sLine[k] & 0xffff));
120
                                                break;
121
                                        case IBuffer.TYPE_INT:
122
                                                int[] iLine = (int[])obj;
123
                                                for (int k = 0; k < iLine.length; k++)
124
                                                        histogram.incrementPxValue(band, (double)(iLine[k] & 0xffffffff));
125
                                                break;
126
                                        case IBuffer.TYPE_FLOAT:
127
                                                float[] fLine = (float[])obj;
128
                                                for (int k = 0; k < fLine.length; k++)
129
                                                        histogram.incrementPxValue(band, (double)fLine[k]);
130
                                                break;
131
                                        case IBuffer.TYPE_DOUBLE:
132
                                                double[] dLine = (double[])obj;
133
                                                for (int k = 0; k < dLine.length; k++)
134
                                                        histogram.incrementPxValue(band, (double)dLine[k]);
135
                                                break;
136
                                }
137
                                if (isCanceled()) 
138
                                        return null;
139
                                percent = ((band * dataset.getHeight() + line) * 100) / (dataset.getBandCount() * dataset.getHeight());
140
                        }
141
                }
142
                percent = 100;
143
                return histogram;
144
        }
145

    
146
        public void resetPercent() {
147
                percent=0;
148
        }
149

    
150
        public int getPercent() {
151
                return percent;
152
        }
153
        
154
        public void setCanceled(boolean value) {
155
                canceled = value;
156
                if (dataset != null)
157
                        dataset.getStatistics().setCanceled(value);
158
        }
159
        
160
        public boolean isCanceled() {
161
                return canceled;
162
        }
163
}