Statistics
| Revision:

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

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

    
143
        public void resetPercent() {
144
                percent=0;
145
        }
146

    
147
        public int getPercent() {
148
                return percent;
149
        }
150
        
151
        public void setCanceled(boolean value) {
152
                canceled = value;
153
        }
154
        
155
        public boolean isCanceled() {
156
                return canceled;
157
        }
158
}