Statistics
| Revision:

root / trunk / libraries / libRaster / src / org / gvsig / raster / dataset / properties / DatasetListHistogram.java @ 11478

History | View | Annotate | Download (4.35 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.dataset.FileNotOpenException;
22
import org.gvsig.raster.dataset.RasterDataset;
23
import org.gvsig.raster.dataset.RasterDriverException;
24
import org.gvsig.raster.dataset.MultiRasterDataset;
25
import org.gvsig.raster.util.Histogram;
26

    
27
/**
28
 * Clase para la gesti?n de histogramas de un raster formado por una lista de ficheros.
29
 * Para devolver un histograma pedir? el histograma a todos los ficheros que
30
 * componen el multifichero.
31
 * 
32
 * @author Nacho Brodin (nachobrodin@gmail.com)
33
 */
34
public class DatasetListHistogram {
35
        /**
36
         * Histograma de la imagen completa
37
         */
38
        private Histogram                                                histogram = null;
39
        /**
40
         * Dataset del cual se calcula el histograma
41
         */
42
        private MultiRasterDataset        multiDataset = null;
43
        
44
        /**
45
         * Constructor
46
         * @param dataset
47
         */
48
        public DatasetListHistogram(MultiRasterDataset dataset){
49
                this.multiDataset = dataset;
50
        }
51
        
52
        /**
53
         * Obtiene el histograma. Pregunta a todos los datasets que forman el multidataset
54
         * por su histograma y luego los fusiona.
55
         * @return histograma 
56
         */
57
        public Histogram getHistogram()throws FileNotOpenException, RasterDriverException{
58
                if (multiDataset != null) {
59
                        //Obtenemos los histogramas de cada dataset
60
                        Histogram[] hList = new Histogram[multiDataset.getDatasetCount()];
61
                        double max = Double.NEGATIVE_INFINITY;
62
                        double min = Double.MAX_VALUE;
63
                        for (int i = 0; i < hList.length; i++) {
64
                                hList[i] = multiDataset.getDataset(i).getHistogram().getHistogram();
65
                                if (max < multiDataset.getDataset(i).getHistogram().getMaximum())
66
                                        max = multiDataset.getDataset(i).getHistogram().getMaximum();
67
                                if (min > multiDataset.getDataset(i).getHistogram().getMinimum())
68
                                        min = multiDataset.getDataset(i).getHistogram().getMinimum();
69
                                if (isCanceled(RasterDataset.CANCEL_HISTOGRAM) || (hList[i] == null)) 
70
                                        return null;
71
                        }
72

    
73
                        if (hList[0].getNumBands() == 0)
74
                                return null;
75
                        
76
                        histogram = new Histogram(multiDataset.getBandCount(), hList[0].getNumValues(), min, max);
77
                        
78
                        int band = 0;
79
                        for (int iDataset = 0; iDataset < hList.length; iDataset++) {
80
                                for (int iBand = 0; iBand < hList[iDataset].getNumBands(); iBand++) {
81
                                        for (int iPxValue = 0; iPxValue < hList[iDataset].getBandLenght(iBand); iPxValue ++) {
82
                                                if (isCanceled(RasterDataset.CANCEL_HISTOGRAM)) 
83
                                                        return null;
84
                                                histogram.setHistogramValueByPos(band, iPxValue, (long) hList[iDataset].getHistogramValueByPos(iBand, iPxValue));
85
                                        }
86
                                        band ++;
87
                                }        
88
                        }
89
                        
90
                        return histogram;
91
                }
92
                return null;
93
        }
94

    
95
        public void resetPercent() {
96
                for (int i = 0; i < multiDataset.getDatasetCount(); i++) {
97
                        multiDataset.getDataset(i).resetPercent();
98
                }
99
        }
100

    
101
        public int getPercent() {
102
                int percent = 0;
103
                for (int i = 0; i < multiDataset.getDatasetCount(); i++) {
104
                        percent += multiDataset.getDataset(i).getPercent();
105
                }
106
                percent = percent / multiDataset.getDatasetCount();
107
                return percent;
108
        }
109
        
110
        /**
111
         * Metodo para poder cancelar el proceso de calculo de histograma.
112
         */
113
        public void setCanceled(boolean value, int process) {
114
                for (int i = 0; i < multiDataset.getDatasetCount(); i++) 
115
                        multiDataset.getDataset(i).setCanceled(value, RasterDataset.CANCEL_HISTOGRAM);
116
        }
117

    
118
        /**
119
         * Metodo para saber si se ha cancelado un proceso de calculo de histograma
120
         * @return boolean
121
         */
122
        public boolean isCanceled(int process) {
123
                boolean cancel = false;
124
                for (int i = 0; i < multiDataset.getDatasetCount(); i++) {
125
                        if (multiDataset.getDataset(i).isCanceled(RasterDataset.CANCEL_HISTOGRAM)) {
126
                                cancel = true;
127
                                break;
128
                        }
129
                }
130
                return cancel;
131
        }
132
}