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 / MultiDatasetStatistics.java @ 120

History | View | Annotate | Download (8.12 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.ArrayList;
25

    
26
import org.gvsig.fmap.dal.coverage.exception.FileNotOpenException;
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.exception.RmfSerializerException;
30
import org.gvsig.fmap.dal.coverage.store.MultiRasterStore;
31
import org.gvsig.fmap.dal.coverage.store.props.Statistics;
32
import org.gvsig.raster.impl.provider.RasterProvider;
33
import org.gvsig.raster.impl.store.DefaultMultiRasterStore;
34

    
35

    
36
/**
37
 * Estadisticas asociadas a un fichero raster formado por multiples ficheros.
38
 *
39
 * @author Nacho Brodin (nachobrodin@gmail.com)
40
 */
41
public class MultiDatasetStatistics extends AbstractStatistics {
42

    
43
        protected DatasetStatistics[]     statList      = null;
44
        protected RasterProvider[]        providerList  = null;
45
        protected int                     nDataset      = 0;
46

    
47
        /**
48
         * Carga el objeto DatasetListStatistics desde los datos del RMF.
49
         * @param datasource
50
         * @return
51
         * @throws RmfSerializerException
52
         */
53
        public static MultiDatasetStatistics loadDatasetListStatistics(MultiRasterStore store) throws RmfSerializerException {
54
                DatasetStatistics[] stats = new DatasetStatistics[store.getDataStoreCount()];
55
                DefaultMultiRasterStore storeImpl = (DefaultMultiRasterStore)store;
56
                
57
                for (int i = 0; i < storeImpl.getProviders().size(); i++) {
58
                        Statistics statFile = (storeImpl.getProvider(i)).getStatistics();
59
                        stats[i] = (DatasetStatistics)(storeImpl.getProvider(i)).loadObjectFromRmf(DatasetStatistics.class, statFile);
60

    
61
                        //Con que un dataset no tenga la estadistica calculada se pone a no calculado el DatasetListStatistics
62
                        if(!stats[i].isCalculated())
63
                                return null;
64
                }
65
                MultiDatasetStatistics result = new MultiDatasetStatistics(stats);
66
                result.setCalculated(true);
67
                return result;
68
        }
69
        
70
        /**
71
         * Constructor
72
         */
73
        public MultiDatasetStatistics() {
74
        }
75
        
76
        /**
77
         * Constructor
78
         */
79
        public MultiDatasetStatistics(RasterProvider provider) {
80
                this.providerList = new RasterProvider[]{provider};
81
                statList = new DatasetStatistics[]{provider.getStatistics()};
82
        }
83
        
84
        /**
85
         * Constructor
86
         */
87
        public MultiDatasetStatistics(RasterProvider[] datasetList) {
88
                this.providerList = datasetList;
89
                statList = new DatasetStatistics[datasetList.length];
90
                for(int i = 0; i < datasetList.length; i ++)
91
                        statList[i] = this.providerList[i].getStatistics();
92
        }
93

    
94
        /**
95
         * Constructor
96
         */
97
        public MultiDatasetStatistics(ArrayList<RasterProvider> datasetList) {
98
                statList = new DatasetStatistics[datasetList.size()];
99
                this.providerList = new RasterProvider[datasetList.size()];
100
                for(int i = 0; i < datasetList.size(); i ++){
101
                        this.providerList[i] = datasetList.get(i);
102
                        if(this.providerList[i] != null)
103
                                statList[i] = this.providerList[i].getStatistics();
104
                }
105
        }
106

    
107
        /**
108
         * Constructor
109
         */
110
        public MultiDatasetStatistics(DatasetStatistics[] statList) {
111
                this.statList = statList;
112
                providerList = new RasterProvider[statList.length];
113
                for(int i = 0; i < statList.length; i ++)
114
                        providerList[i] = this.statList[i].getDataProvider();
115
                int len = 0;
116
                for(nDataset = 0; nDataset < statList.length; nDataset ++) {
117
                        try {
118
                                statList[nDataset].calcFullStatistics();
119
                        } catch (FileNotOpenException e) {
120
                        } catch (RasterDriverException e) {
121
                        } catch (ProcessInterruptedException e) {
122
                        }
123
                        len += providerList[nDataset].getBandCount();
124
                }
125
                constructStats(len);
126
        }
127

    
128
        /**
129
         * Adds a new data store to the statistics list
130
         * @param provider
131
         */
132
        public void addProvider(RasterProvider provider) {
133
                DatasetStatistics[] statListAux = new DatasetStatistics[providerList.length + 1];
134
                RasterProvider[] datasetListAux = new RasterProvider[providerList.length + 1];
135
                for(int i = 0; i < providerList.length; i ++) {
136
                        datasetListAux[i] = providerList[i];
137
                        statListAux[i] = statList[i];
138
                }
139
                datasetListAux[providerList.length] = provider;
140
                statListAux[providerList.length] = provider.getStatistics();
141
                providerList = datasetListAux;
142
                statList = statListAux;
143
        }
144

    
145
        /**
146
         * Calcula las estadisticas recorriendo todo el fichero.
147
         */
148
        public void calcFullStatistics()throws FileNotOpenException, RasterDriverException, ProcessInterruptedException {
149
//                long t2;
150
//                long t1 = new Date().getTime();
151
                int len = 0;
152
                for(nDataset = 0; nDataset < statList.length; nDataset ++) {
153
                        statList[nDataset].calcFullStatistics();
154
                        len += providerList[nDataset].getBandCount();
155
                }
156

    
157
                try {
158
                        constructStats(len);
159
                } catch (ArrayIndexOutOfBoundsException e) {
160
                        throw new RasterDriverException("Error en el acceso al array de m?ximos y m?nimos");
161
                }
162

    
163
                calculated = true;
164
//                t2 = new Date().getTime();
165
//                System.out.println("Estadisticas MultiFile: " + ((t2 - t1) / 1000D) + ", secs.");
166
        }
167
        
168
        /*
169
         * (non-Javadoc)
170
         * @see org.gvsig.raster.impl.dataset.properties.AbstractStatistics#resetPercent()
171
         */
172
        public void resetPercent() {
173
                for (int i = 0; i < statList.length; i++) {
174
                        statList[i].resetPercent();
175
                }
176
        }
177

    
178
        /*
179
         * (non-Javadoc)
180
         * @see org.gvsig.raster.impl.dataset.properties.AbstractStatistics#getPercent()
181
         */
182
        public int getPercent() {
183
                try {
184
                        return (statList[nDataset].getPercent() / statList.length);
185
                } catch (ArrayIndexOutOfBoundsException e) {
186
                        return 0;
187
                }
188
        }
189

    
190
        /**
191
         * Cuando se llama a este m?todo fuerza que la siguiente petici?n de estad?sticas
192
         * no sea le?da de RMF y sean recalculadas por completo.
193
         * @param forceToRecalc
194
         */
195
        public void forceToRecalc() {
196
                for(int i = 0; i < statList.length; i ++)
197
                        statList[i].forceToRecalc();
198
        }
199

    
200
        /**
201
         * Construye todos los datos del DatasetStadistics actual a partir de toda la
202
         * lista de DatasetStatistics que tiene este objeto.
203
         *
204
         * @param len
205
         * @throws ArrayIndexOutOfBoundsException
206
         */
207
        private void constructStats(int len) throws ArrayIndexOutOfBoundsException {
208
                max = new double[len];
209
                min = new double[len];
210
                secondMax = new double[len];
211
                secondMin = new double[len];
212
                maxByteUnsigned = new double[len];
213
                minByteUnsigned = new double[len];
214
                secondMaxByteUnsigned = new double[len];
215
                secondMinByteUnsigned = new double[len];
216
                mean = new double[len];
217
                variance = new double[len];
218

    
219
                int count = 0;
220
                for(int i = 0; i < statList.length; i ++) {
221
                        if(statList[i].getMax() == null)
222
                                return;
223
                        for(int j = 0; j < statList[i].getMax().length; j ++) {
224
                                max[count] = statList[i].getMax()[j];
225
                                min[count] = statList[i].getMin()[j];
226
                                secondMax[count] = statList[i].getSecondMax()[j];
227
                                secondMin[count] = statList[i].getSecondMin()[j];
228
                                maxByteUnsigned[count] = statList[i].getMaxByteUnsigned()[j];
229
                                minByteUnsigned[count] = statList[i].getMinByteUnsigned()[j];
230
                                secondMaxByteUnsigned[count] = statList[i].getSecondMaxByteUnsigned()[j];
231
                                secondMinByteUnsigned[count] = statList[i].getSecondMinByteUnsigned()[j];
232
                                mean[count] = statList[i].getMean()[j];
233
                                variance[count] = statList[i].getVariance()[j];
234
                                count ++;
235
                        }
236
                }
237
        }
238

    
239
        /**
240
         * Obtiene el flag que informa de si las estad?sticas est?n calculadas o no.
241
         * @return true indica que est?n calculadas y false que no lo est?n
242
         */
243
        public boolean isCalculated() {
244
                return calculated;
245
        }
246

    
247
        /**
248
         * N?mero de bandas
249
         * @return
250
         */
251
        public int getBandCount() {
252
                int bandCount = 0;
253
                for(int i = 0; i < statList.length; i ++)
254
                        bandCount += statList[i].getBandCount();
255
                return bandCount;
256
        }
257
}