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 / MultiProviderStatistics.java @ 8703

History | View | Annotate | Download (7.1 KB)

1 2443 nbrodin
/* 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 2579 nbrodin
                        if(forceToRecalc)
135
                                statList[nDataset].forceToRecalc();
136 2443 nbrodin
                        statList[nDataset].calculate(scale);
137
                }
138
139
                try {
140
                        constructStats(nBands);
141
                } catch (ArrayIndexOutOfBoundsException e) {
142
                        throw new RasterDriverException("Error en el acceso al array de m?ximos y m?nimos");
143
                }
144
145
                calculated = true;
146
                forceToRecalc = false;
147
                try {
148
                        provider.saveObjectToRmf(Statistics.class, this);
149
                } catch (RmfSerializerException e) {
150
                        // No salva a rmf
151
                }
152
153
        }
154
155
        public void resetPercent() {
156
                for (int i = 0; i < statList.length; i++) {
157
                        statList[i].resetPercent();
158
                }
159
        }
160
161
        public int getPercent() {
162
                int sum = 0;
163
                for (int i = 0; i < statList.length; i++) {
164
                        sum += statList[i].getPercent();
165
                }
166
                return sum / statList.length;
167
        }
168
169
        public void forceToRecalc() {
170
                this.forceToRecalc = true;
171
        }
172
173
        /**
174
         * Construye todos los datos del DatasetStadistics actual a partir de toda la
175
         * lista de DatasetStatistics que tiene este objeto.
176
         *
177
         * @param len
178
         * @throws ArrayIndexOutOfBoundsException
179
         */
180
        private void constructStats(int len) throws ArrayIndexOutOfBoundsException {
181
                max = new double[len];
182
                min = new double[len];
183
                secondMax = new double[len];
184
                secondMin = new double[len];
185
                maxByteUnsigned = new double[len];
186
                minByteUnsigned = new double[len];
187
                secondMaxByteUnsigned = new double[len];
188
                secondMinByteUnsigned = new double[len];
189
                mean = new double[len];
190
                variance = new double[len];
191
                nValues = new long[len];
192
193
                int count = 0;
194
                for(int i = 0; i < statList.length; i ++) {
195
                        if(statList[i].getMax() == null)
196
                                return;
197
                        for(int j = 0; j < statList[i].getMax().length; j ++) {
198
                                max[count] = statList[i].getMax()[j];
199
                                min[count] = statList[i].getMin()[j];
200
                                secondMax[count] = statList[i].getSecondMax()[j];
201
                                secondMin[count] = statList[i].getSecondMin()[j];
202
                                maxByteUnsigned[count] = statList[i].getMaxByteUnsigned()[j];
203
                                minByteUnsigned[count] = statList[i].getMinByteUnsigned()[j];
204
                                secondMaxByteUnsigned[count] = statList[i].getSecondMaxByteUnsigned()[j];
205
                                secondMinByteUnsigned[count] = statList[i].getSecondMinByteUnsigned()[j];
206
                                mean[count] = statList[i].getMean()[j];
207
                                variance[count] = statList[i].getVariance()[j];
208
                                nValues[count] = statList[i].getNumberOfValues()[j];
209
                                count ++;
210
                        }
211
                }
212
        }
213
214
        /**
215
         * Obtiene el flag que informa de si las estad?sticas est?n calculadas o no.
216
         * @return true indica que est?n calculadas y false que no lo est?n
217
         */
218
        public boolean isCalculated() {
219
                return calculated;
220
        }
221
222
        public int getBandCount() {
223
                if(bandCount <= 0) {
224
                        for(int i = 0; i < statList.length; i ++)
225
                                bandCount += statList[i].getBandCount();
226
                }
227
                return bandCount;
228
        }
229
230
        public void setBandCount(int bandCount) {
231
                this.bandCount = bandCount;
232
        }
233
234
235
        /**
236
         * Returns the number of providers.
237
         * @return
238
         */
239
        public int getNumberOfProviders() {
240
                return statList != null ? statList.length :0;
241
        }
242
243
        public Statistics cloneStatistics() {
244
                MultiProviderStatistics s = new MultiProviderStatistics();
245
                s.statList = statList;
246
                s.nBands = nBands;
247
                return super.cloneStatistics(s);
248
        }
249
250
}