Statistics
| Revision:

gvsig-raster / org.gvsig.raster / trunk / org.gvsig.raster / org.gvsig.raster.lib / org.gvsig.raster.lib.impl / src / main / java / org / gvsig / raster / impl / store / properties / MultiProviderHistogramComputer.java @ 1676

History | View | Annotate | Download (5.99 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22
package org.gvsig.raster.impl.store.properties;
23

    
24
import org.gvsig.fmap.dal.coverage.datastruct.BufferHistogram;
25
import org.gvsig.fmap.dal.coverage.exception.FileNotOpenException;
26
import org.gvsig.fmap.dal.coverage.exception.HistogramException;
27
import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException;
28
import org.gvsig.fmap.dal.coverage.exception.RasterDriverException;
29
import org.gvsig.fmap.dal.coverage.store.props.HistogramComputer;
30
import org.gvsig.fmap.dal.coverage.store.props.Histogramable;
31
import org.gvsig.raster.impl.datastruct.BufferHistogramImpl;
32
import org.gvsig.raster.impl.provider.RasterProvider;
33

    
34

    
35
/**
36
 * Clase para la gesti?n de histogramas de un raster formado por una lista de ficheros.
37
 * Para devolver un histograma pedir? el histograma a todos los ficheros que
38
 * componen el multifichero.
39
 * 
40
 * @author Nacho Brodin (nachobrodin@gmail.com)
41
 */
42
public class MultiProviderHistogramComputer implements HistogramComputer {
43
        private BufferHistogram             histogram   = null;
44
        private RasterProvider              provider    = null;
45
        private boolean                     refresh     = false;
46
        private int                         percent     = 0;
47
        private int                         files       = 0;
48
        private double                      scale       = 1;
49
        
50
        /**
51
         * Constructor
52
         * @param dataset
53
         */
54
        public MultiProviderHistogramComputer(RasterProvider provider) {
55
                this.provider = provider;
56
                files = provider.getInternalProviderCount();
57
        }
58
        
59
        public void setScaleHistogram(double scale) {
60
                this.scale = scale;
61
        }
62
        
63
        /**
64
         * Obtiene el histograma. Pregunta a todos los datasets que forman el multidataset
65
         * por su histograma y luego los fusiona.
66
         * @return histograma 
67
         * @throws HistogramException 
68
         */
69
        public BufferHistogram getBufferHistogram()
70
        throws ProcessInterruptedException, HistogramException {
71
                if (provider != null) {
72
                        if(histogram == null || refresh) {
73
                                //Obtenemos los histogramas de cada dataset
74
                                BufferHistogram[] hList = new BufferHistogram[provider.getInternalProviderCount()];
75

    
76
                                for (int i = 0; i < hList.length; i++) {
77
                                        RasterProvider internalProvider = provider.getInternalProvider(i);
78
                                        hList[i] = ((Histogramable)internalProvider).getHistogramComputer().getBufferHistogram();
79
                                        
80
                                        if (hList[i] == null) 
81
                                                return null;
82
                                }
83

    
84
                                if (hList[0].getNumBands() == 0)
85
                                        return null;
86

    
87
                                try {
88
                                        provider.getStatistics().calculate(scale);
89
                                } catch (FileNotOpenException e) {
90
                                        throw new HistogramException("");
91
                                } catch (RasterDriverException e) {
92
                                        throw new HistogramException("");
93
                                }
94

    
95
                                histogram = new BufferHistogramImpl(provider.getBandCount(), 
96
                                                provider.getStatistics().getMin(), 
97
                                                provider.getStatistics().getMax(), 
98
                                                provider.getDataType()[0]);
99
                                
100
                                /*for (int i = 0; i < hList.length; i++) {
101
                                        histogram.addTable(hList[i].getTable());
102
                                }*/
103

    
104
                                int band = 0;
105
                                for (int iDataset = 0; iDataset < hList.length; iDataset++) {
106
                                        for (int iBand = 0; iBand < hList[iDataset].getNumBands(); iBand++) {
107
                                                for (int iPxValue = 0; iPxValue < hList[iDataset].getBandLenght(iBand); iPxValue ++) {
108
                                                        histogram.setHistogramValueByPos(band, iPxValue, (long) hList[iDataset].getHistogramValueByPos(iBand, iPxValue));
109
                                                }
110
                                                band ++;
111
                                        }        
112
                                }
113
                        }
114
                }
115
                return histogram;
116
        }
117

    
118
        /**
119
         * Pone a cero el porcentaje de progreso del proceso de calculo de histograma
120
         */
121
        public void resetPercent() {
122
                for (int i = 0; i < provider.getInternalProviderCount(); i++) {
123
                        ((Histogramable)provider.getInternalProvider(i)).getHistogramComputer().resetPercent();
124
                }
125
        }
126

    
127
        /**
128
         * Obtiene el porcentaje de proceso en la construcci?n del histograma,
129
         * @return porcentaje de progreso
130
         */
131
        public int getPercent() {
132
                int partial = 0;
133
                for (int i = 0; i < provider.getInternalProviderCount(); i++)
134
                        partial += ((Histogramable)provider.getInternalProvider(i)).getHistogramComputer().getPercent();
135
                percent += (int)(partial / provider.getInternalProviderCount());
136
                return percent;
137
        }
138

    
139
        /*
140
         * (non-Javadoc)
141
         * @see org.gvsig.fmap.dal.coverage.dataset.Histogram#getMaximum()
142
         */
143
        public double getMaximum() {
144
                double max = 0;
145
                for (int i = 0; i < provider.getInternalProviderCount(); i++) {
146
                        double m = provider.getInternalProvider(i).getHistogramComputer().getMaximum();
147
                        if(m > max)
148
                                max = m;
149
                }
150
                return max;
151
        }
152

    
153
        /*
154
         * (non-Javadoc)
155
         * @see org.gvsig.fmap.dal.coverage.dataset.Histogram#getMinimum()
156
         */
157
        public double getMinimum() {
158
                double min = Double.POSITIVE_INFINITY;
159
                for (int i = 0; i < provider.getInternalProviderCount(); i++) {
160
                        double m = provider.getInternalProvider(i).getHistogramComputer().getMinimum();
161
                        if(m < min)
162
                                min = m;
163
                }
164
                return min;
165
        }
166

    
167
        /*
168
         * (non-Javadoc)
169
         * @see org.gvsig.fmap.dal.coverage.store.props.HistogramComputer#refreshHistogram()
170
         */
171
        public void refreshHistogram() {
172
                refresh = true;
173
        }
174
        
175
        /**
176
         * Returns the number of providers.
177
         * @return
178
         */
179
        public int getNumberOfProviders() {
180
                return files;
181
        }
182

    
183
        public String getLog() {
184
                return null;
185
        }
186

    
187
        public boolean isCancelable() {
188
                return true;
189
        }
190

    
191
        public boolean isPausable() {
192
                return false;
193
        }
194

    
195
        public void setPercent(int value) {
196
                
197
        }
198
}