Statistics
| Revision:

gvsig-raster / org.gvsig.raster / branches / org.gvsig.raster_dataaccess_refactoring / org.gvsig.raster.lib / org.gvsig.raster.lib.impl / src / main / java / org / gvsig / raster / impl / store / properties / MultiProviderStatistics.java @ 2321

History | View | Annotate | Download (7.04 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 java.util.List;
25

    
26
import org.gvsig.fmap.dal.coverage.RasterLibrary;
27
import org.gvsig.fmap.dal.coverage.exception.FileNotOpenException;
28
import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException;
29
import org.gvsig.fmap.dal.coverage.exception.RasterDriverException;
30
import org.gvsig.fmap.dal.coverage.exception.RmfSerializerException;
31
import org.gvsig.fmap.dal.coverage.store.props.Statistics;
32
import org.gvsig.raster.impl.provider.RasterProvider;
33

    
34

    
35
/**
36
 * Estadisticas asociadas a un fichero raster formado por multiples ficheros.
37
 *
38
 * @author Nacho Brodin (nachobrodin@gmail.com)
39
 */
40
public class MultiProviderStatistics extends AbstractStatistics {
41
        protected Statistics[]                 statList      = null;
42
        protected int                          nBands        = 0;
43
        protected boolean                      forceToRecalc = false;
44
        protected RasterProvider               provider      = null;
45

    
46
        /**
47
         * Constructor
48
         */
49
        public MultiProviderStatistics() {
50
        }
51
        
52
        /**
53
         * Constructor
54
         */
55
        public MultiProviderStatistics(List<RasterProvider> providerList, RasterProvider provider) {
56
                this(providerList);
57
                this.provider = provider;
58
        }
59
        
60
        /**
61
         * Constructor
62
         */
63
        @SuppressWarnings("unused")
64
        private MultiProviderStatistics(RasterProvider[] providerList) {
65
                statList = new Statistics[providerList.length];
66
                
67
                for(int i = 0; i < providerList.length; i ++) {
68
                        statList[i] = providerList[i].getStatistics();
69
                        this.nBands += providerList[i].getBandCount();
70
                }
71
        }
72

    
73
        /**
74
         * Constructor
75
         */
76
        private MultiProviderStatistics(List<RasterProvider> providerList) {
77
                statList = new Statistics[providerList.size()];
78
                for(int i = 0; i < providerList.size(); i ++) {
79
                        if(providerList.get(i) != null) {
80
                                statList[i] = providerList.get(i).getStatistics();
81
                                this.nBands += providerList.get(i).getBandCount();
82
                        }
83
                }
84
        }
85

    
86
        /**
87
         * Constructor
88
         */
89
        @SuppressWarnings("unused")
90
        private MultiProviderStatistics(Statistics[] statList, int nBands) {
91
                this.nBands = nBands;
92
                this.statList = statList;
93
                for(int nDataset = 0; nDataset < statList.length; nDataset ++) {
94
                        try {
95
                                statList[nDataset].calculate(RasterLibrary.statisticsScale);
96
                        } catch (FileNotOpenException e) {
97
                        } catch (RasterDriverException e) {
98
                        } catch (ProcessInterruptedException e) {
99
                        }
100
                }
101
                constructStats(nBands);
102
        }
103

    
104
        /**
105
         * Adds a new data store to the statistics list
106
         * @param provider
107
         */
108
        public void addProvider(RasterProvider provider) {
109
                Statistics[] statListAux = new Statistics[statList.length + 1];
110
                for(int i = 0; i < statList.length; i ++) {
111
                        statListAux[i] = statList[i];
112
                }
113
                statListAux[statList.length] = provider.getStatistics();
114
                statList = statListAux;
115
        }
116

    
117
        /**
118
         * Calcula las estadisticas recorriendo todo el fichero.
119
         */
120
        public void calculate(double scale)throws FileNotOpenException, RasterDriverException, ProcessInterruptedException {
121
                if (!forceToRecalc) {
122
                        if (!isCalculated()) {
123
                                try {
124
                                        provider.loadObjectFromRmf(SimpleProviderStatistics.class, this);
125
                                } catch (RmfSerializerException e) {
126
                                        // Si no se puede cargar del RMF, recalcularemos las estadisticas.
127
                                }
128
                        }
129
                        if (isCalculated())
130
                                return;
131
                }
132
                
133
                for(int nDataset = 0; nDataset < statList.length; nDataset ++) {
134
                        statList[nDataset].calculate(scale);
135
                }
136

    
137
                try {
138
                        constructStats(nBands);
139
                } catch (ArrayIndexOutOfBoundsException e) {
140
                        throw new RasterDriverException("Error en el acceso al array de m?ximos y m?nimos");
141
                }
142

    
143
                calculated = true;
144
                forceToRecalc = false;
145
                try {
146
                        provider.saveObjectToRmf(Statistics.class, this);
147
                } catch (RmfSerializerException e) {
148
                        // No salva a rmf
149
                }
150
                
151
        }
152
        
153
        public void resetPercent() {
154
                for (int i = 0; i < statList.length; i++) {
155
                        statList[i].resetPercent();
156
                }
157
        }
158

    
159
        public int getPercent() {
160
                int sum = 0;
161
                for (int i = 0; i < statList.length; i++) {
162
                        sum += statList[i].getPercent();
163
                }
164
                return sum / statList.length;
165
        }
166

    
167
        public void forceToRecalc() {
168
                this.forceToRecalc = true;
169
        }
170

    
171
        /**
172
         * Construye todos los datos del DatasetStadistics actual a partir de toda la
173
         * lista de DatasetStatistics que tiene este objeto.
174
         *
175
         * @param len
176
         * @throws ArrayIndexOutOfBoundsException
177
         */
178
        private void constructStats(int len) throws ArrayIndexOutOfBoundsException {
179
                max = new double[len];
180
                min = new double[len];
181
                secondMax = new double[len];
182
                secondMin = new double[len];
183
                maxByteUnsigned = new double[len];
184
                minByteUnsigned = new double[len];
185
                secondMaxByteUnsigned = new double[len];
186
                secondMinByteUnsigned = new double[len];
187
                mean = new double[len];
188
                variance = new double[len];
189
                nValues = new long[len];
190

    
191
                int count = 0;
192
                for(int i = 0; i < statList.length; i ++) {
193
                        if(statList[i].getMax() == null)
194
                                return;
195
                        for(int j = 0; j < statList[i].getMax().length; j ++) {
196
                                max[count] = statList[i].getMax()[j];
197
                                min[count] = statList[i].getMin()[j];
198
                                secondMax[count] = statList[i].getSecondMax()[j];
199
                                secondMin[count] = statList[i].getSecondMin()[j];
200
                                maxByteUnsigned[count] = statList[i].getMaxByteUnsigned()[j];
201
                                minByteUnsigned[count] = statList[i].getMinByteUnsigned()[j];
202
                                secondMaxByteUnsigned[count] = statList[i].getSecondMaxByteUnsigned()[j];
203
                                secondMinByteUnsigned[count] = statList[i].getSecondMinByteUnsigned()[j];
204
                                mean[count] = statList[i].getMean()[j];
205
                                variance[count] = statList[i].getVariance()[j];
206
                                nValues[count] = statList[i].getNumberOfValues()[j];
207
                                count ++;
208
                        }
209
                }
210
        }
211

    
212
        /**
213
         * Obtiene el flag que informa de si las estad?sticas est?n calculadas o no.
214
         * @return true indica que est?n calculadas y false que no lo est?n
215
         */
216
        public boolean isCalculated() {
217
                return calculated;
218
        }
219

    
220
        public int getBandCount() {
221
                if(bandCount <= 0) {
222
                        for(int i = 0; i < statList.length; i ++)
223
                                bandCount += statList[i].getBandCount();
224
                }
225
                return bandCount;
226
        }
227
        
228
        public void setBandCount(int bandCount) {
229
                this.bandCount = bandCount;
230
        }
231
        
232
        
233
        /**
234
         * Returns the number of providers.
235
         * @return
236
         */
237
        public int getNumberOfProviders() {
238
                return statList != null ? statList.length :0;
239
        }
240

    
241
        public Statistics cloneStatistics() {
242
                MultiProviderStatistics s = new MultiProviderStatistics();
243
                s.statList = statList;
244
                s.nBands = nBands;
245
                return super.cloneStatistics(s);
246
        }
247

    
248
}