Statistics
| Revision:

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

History | View | Annotate | Download (5.67 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
import org.gvsig.raster.util.IHistogramable;
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
        public static final int                CANCEL_CREATEHISTOGRAM = IHistogramable.CANCEL_HISTOGRAM;
41
        private boolean[]                        cancel = new boolean[1];
42
        
43
        /**
44
         * Histograma de la imagen completa
45
         */
46
        private Histogram                        histogram = null;
47
        /**
48
         * Dataset del cual se calcula el histograma
49
         */
50
        private RasterDataset                dataset = null;
51
        private int                                 percent = 0;
52
        
53
        /**
54
         * Constructor
55
         * @param dataset
56
         */
57
        public DatasetHistogram(RasterDataset dataset){
58
                this.dataset = dataset;
59
        }
60

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

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

    
85
                                dataset.getStatistics().calcFullStatistics();
86

    
87
                                if (isCanceled(CANCEL_CREATEHISTOGRAM)) 
88
                                        return null;
89

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

    
148
        public void resetPercent() {
149
                percent=0;
150
        }
151

    
152
        public int getPercent() {
153
                return percent;
154
        }
155
        
156
        public void setCanceled(boolean value, int process) {
157
                if(process == CANCEL_CREATEHISTOGRAM || process == 0) {
158
                        cancel[0] = value;
159
                        if (dataset != null && dataset.getStatistics() != null)
160
                                dataset.getStatistics().setCanceled(value, DatasetStatistics.CANCEL_FULLSTAT);
161
                }
162
                
163
                
164
        }
165
        
166
        public boolean isCanceled(int process) {
167
                if(process == CANCEL_CREATEHISTOGRAM)
168
                        return cancel[0];
169
                return false;
170
        }
171
}