Statistics
| Revision:

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

History | View | Annotate | Download (4.37 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.RasterDriverException;
23
import org.gvsig.raster.dataset.RasterMultiDataset;
24
import org.gvsig.raster.util.DataClassList;
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
         * clases en las que se divide el histograma
37
         */
38
        private DataClassList                         classes = null;
39
        
40
        /**
41
         * Histograma de la imagen completa
42
         */
43
        private Histogram                                                histogram = null;
44
        /**
45
         * Dataset del cual se calcula el histograma
46
         */
47
        private RasterMultiDataset        multiDataset = null;
48
        
49
        /**
50
         * Constructor
51
         * @param dataset
52
         */
53
        public DatasetListHistogram(RasterMultiDataset dataset){
54
                this.multiDataset = dataset;
55
        }
56
        
57
        /**
58
         * Asigna una lista de clases sobre la que se calcular? la petici?n de
59
         * histograma.
60
         * @param classes
61
         */
62
        public void setClasses(DataClassList classes){
63
                this.classes = classes;
64
                for (int iDataset = 0; iDataset < multiDataset.getDatasetCount(); iDataset++)
65
                        multiDataset.getDataset(iDataset).getHistogram().setClasses(classes);
66
        }
67
        
68
        /**
69
         * Obtiene el histograma. Pregunta a todos los datasets que forman el multidataset
70
         * por su histograma y luego los fusiona.
71
         * @return histograma 
72
         */
73
        public Histogram getHistogram()throws FileNotOpenException, RasterDriverException{
74
                if(multiDataset != null){
75
                        //Obtenemos los histogramas de cada dataset
76
                        int nBands = 0;
77
                        Histogram[] hList = new Histogram[multiDataset.getDatasetCount()];
78
                        for (int i = 0; i < hList.length; i++) {
79
                                multiDataset.getDataset(i).getHistogram().setClasses(classes);
80
                                hList[i] = multiDataset.getDataset(i).getHistogram().getHistogram();
81
                                if (isCanceled() || (hList[i] == null)) return null;
82
                                nBands += hList[i].getNumBands();
83
                        }
84
                        
85
                        if(hList[0].getNumBands() == 0)
86
                                return null;
87
                        
88
                        histogram = new Histogram(nBands, hList[0].getNumValues());
89
                        //histogram = new long[nBands][hList[0][0].length];
90
                        int band = 0;
91
                        for (int iDataset = 0; iDataset < hList.length; iDataset++) {
92
                                for (int iBand = 0; iBand < hList[iDataset].getNumBands(); iBand++) {
93
                                        for (int iPxValue = 0; iPxValue < hList[iDataset].getBandLenght(iBand); iPxValue ++) {
94
                                                if (isCanceled()) return null;
95
                                                histogram.setHistogramValue(band, iPxValue, hList[iDataset].getHistogramValue(iBand, iPxValue));
96
                                        }
97
                                        band ++;
98
                                }        
99
                        }
100
                        
101
                        return histogram;
102
                }
103
                return null;
104
        }
105

    
106
        public void resetPercent() {
107
                for (int i = 0; i < multiDataset.getDatasetCount(); i++) {
108
                        multiDataset.getDataset(i).resetPercent();
109
                }
110
        }
111

    
112
        public int getPercent() {
113
                int percent = 0;
114
                for (int i = 0; i < multiDataset.getDatasetCount(); i++) {
115
                        percent += multiDataset.getDataset(i).getPercent();
116
                }
117
                percent = percent / multiDataset.getDatasetCount();
118
                return percent;
119
        }
120
        
121
        /**
122
         * Metodo para poder cancelar el proceso.
123
         */
124
        public void setCanceled(boolean value) {
125
                for (int i = 0; i < multiDataset.getDatasetCount(); i++) {
126
                        multiDataset.getDataset(i).setCanceled(value);
127
                }
128
        }
129

    
130
        /**
131
         * Metodo para saber si se ha cancelado un proceso
132
         * @return boolean
133
         */
134
        public boolean isCanceled() {
135
                boolean cancel = false;
136
                for (int i = 0; i < multiDataset.getDatasetCount(); i++) {
137
                        if (multiDataset.getDataset(i).isCanceled()) {
138
                                cancel = true;
139
                                break;
140
                        }
141
                }
142
                return cancel;
143
        }
144
}