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 / RemoteDataStoreStatistics.java @ 1419

History | View | Annotate | Download (6.46 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.dataset.Buffer;
25
import org.gvsig.fmap.dal.coverage.exception.FileNotOpenException;
26
import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException;
27
import org.gvsig.fmap.dal.coverage.exception.RasterDriverException;
28
import org.gvsig.fmap.dal.coverage.exception.RmfSerializerException;
29
import org.gvsig.raster.impl.process.RasterTask;
30
import org.gvsig.raster.impl.process.RasterTaskQueue;
31
import org.gvsig.raster.impl.provider.RasterProvider;
32
import org.gvsig.raster.impl.provider.RemoteRasterProvider;
33
/**
34
 * Statistics for a remote raster provider.
35
 *  
36
 * @author Nacho Brodin (nachobrodin@gmail.com)
37
 */
38
public class RemoteDataStoreStatistics extends SimpleProviderStatistics {
39
        
40
        /**
41
         * Constructor. Asigna el fichero asociado.
42
         */
43
        public RemoteDataStoreStatistics(RasterProvider prov) {
44
                super(prov);
45
        }
46
        
47
        /*
48
         * (non-Javadoc)
49
         * @see org.gvsig.raster.impl.store.properties.SimpleProviderStatistics#calculate(double)
50
         */
51
        public void calculate(double scale) throws FileNotOpenException, RasterDriverException, ProcessInterruptedException {
52
                if (provider == null || !(provider instanceof RemoteRasterProvider))
53
                        return;
54
                
55
                RemoteRasterProvider prov = (RemoteRasterProvider)provider;
56
                
57
                RasterTask task = RasterTaskQueue.get(Thread.currentThread().getId() + "");
58
                percent = 0;
59
                
60
                bandCount = provider.getBandCount();
61
                max = new double[bandCount];
62
                min = new double[bandCount];
63
                secondMax = new double[bandCount];
64
                secondMin = new double[bandCount];
65
                maxByteUnsigned = new double[bandCount];
66
                minByteUnsigned = new double[bandCount];
67
                secondMaxByteUnsigned = new double[bandCount];
68
                secondMinByteUnsigned = new double[bandCount];
69
                mean = new double[bandCount];
70
                variance = new double[bandCount];
71

    
72
                long[] iValues = new long[bandCount];
73
                boolean[] initializedBand = new boolean[bandCount];
74
                int[] type = new int[bandCount];
75
                
76
                double z = 0;
77
                double rgb = 0;
78

    
79
                for (int iBand = 0; iBand < bandCount; iBand++) {
80
                        max[iBand] = Double.NEGATIVE_INFINITY;
81
                        min[iBand] = Double.POSITIVE_INFINITY;
82
                        secondMax[iBand] = Double.NEGATIVE_INFINITY;
83
                        secondMin[iBand] = Double.POSITIVE_INFINITY;
84
                        maxByteUnsigned[iBand] = 0;
85
                        minByteUnsigned[iBand] = 255;
86
                        secondMaxByteUnsigned[iBand] = 0;
87
                        secondMinByteUnsigned[iBand] = 255;
88
                        initializedBand[iBand] = false;
89
                        type[iBand] = provider.getDataType()[iBand];
90
                }
91

    
92
                Buffer buf = prov.getBufferLastRequest();
93

    
94
                if(buf == null)
95
                        return;
96

    
97
                for (int iBand = 0; iBand < buf.getBandCount(); iBand++) {
98
                        for (int col = 0; col < buf.getWidth(); col++) {
99
                                for (int row = 0; row < buf.getHeight(); row++) {
100
                                        z = (buf.getDataType() == Buffer.TYPE_BYTE) ? buf.getElemByte(row, col, iBand) :
101
                                                (buf.getDataType() == Buffer.TYPE_SHORT) ? buf.getElemShort(row, col, iBand) :
102
                                                (buf.getDataType() == Buffer.TYPE_DOUBLE) ? buf.getElemDouble(row, col, iBand) :
103
                                                (buf.getDataType() == Buffer.TYPE_FLOAT) ? buf.getElemFloat(row, col, iBand) :
104
                                                (buf.getDataType() == Buffer.TYPE_INT) ? buf.getElemInt(row, col, iBand) :
105
                                                0;
106

    
107
                                                if ((provider.getNoDataValue().isDefined()) && 
108
                                                        (z == provider.getNoDataValue().getValue().doubleValue()))
109
                                                        continue;
110

    
111
                                                if (Double.isNaN(z))
112
                                                        continue;
113

    
114
                                                rgb = 0;
115
                                                if(buf.getDataType() == Buffer.TYPE_BYTE) {
116
                                                        rgb = ((byte) z) & 0xff;
117
                                                        mean[iBand] += rgb;
118
                                                        variance[iBand] += rgb * rgb;
119
                                                } else {
120
                                                        //rgb = (b != null) ? ((byte) z) & 0xff : 0;
121
                                                        mean[iBand] += z;
122
                                                        variance[iBand] += z * z;
123
                                                }
124
                                                iValues[iBand]++;
125

    
126
                                                if (!initializedBand[iBand]) {
127
                                                        secondMin[iBand] = min[iBand];
128
                                                        secondMax[iBand] = max[iBand];
129
                                                        min[iBand] = z;
130
                                                        max[iBand] = z;
131
                                                        secondMinByteUnsigned[iBand] = minByteUnsigned[iBand];
132
                                                        secondMaxByteUnsigned[iBand] = maxByteUnsigned[iBand];
133
                                                        minByteUnsigned[iBand] = rgb;
134
                                                        maxByteUnsigned[iBand] = rgb;
135
                                                        initializedBand[iBand] = true;
136
                                                        continue;
137
                                                }
138

    
139
                                                if (z < secondMin[iBand]) {
140
                                                        if (z < min[iBand]) {
141
                                                                secondMin[iBand] = min[iBand];
142
                                                                min[iBand] = z;
143
                                                        } else {
144
                                                                if (z > min[iBand])
145
                                                                        secondMin[iBand] = z;
146
                                                        }
147
                                                }
148

    
149
                                                if (z > secondMax[iBand]) {
150
                                                        if (z > max[iBand]) {
151
                                                                secondMax[iBand] = max[iBand];
152
                                                                max[iBand] = z;
153
                                                        } else {
154
                                                                if (z < max[iBand])
155
                                                                        secondMax[iBand] = z;
156
                                                        }
157
                                                }
158

    
159
                                                if (rgb < secondMinByteUnsigned[iBand]) {
160
                                                        if (rgb < minByteUnsigned[iBand]) {
161
                                                                secondMinByteUnsigned[iBand] = minByteUnsigned[iBand];
162
                                                                minByteUnsigned[iBand] = rgb;
163
                                                        } else {
164
                                                                if (rgb > minByteUnsigned[iBand])
165
                                                                        secondMinByteUnsigned[iBand] = rgb;
166
                                                        }
167
                                                }
168

    
169
                                                if (rgb > secondMaxByteUnsigned[iBand]) {
170
                                                        if (rgb > maxByteUnsigned[iBand]) {
171
                                                                secondMaxByteUnsigned[iBand] = maxByteUnsigned[iBand];
172
                                                                maxByteUnsigned[iBand] = rgb;
173
                                                        } else {
174
                                                                if (rgb < maxByteUnsigned[iBand])
175
                                                                        secondMaxByteUnsigned[iBand] = rgb;
176
                                                        }
177
                                                }
178
                                }
179
                        }
180
                        if (task.getEvent() != null)
181
                                task.manageEvent(task.getEvent());
182
                        percent = ((bandCount * 100) / (int)buf.getBandCount());
183
                }
184
                        
185
                percent = 100;
186

    
187
                for (int iBand = 0; iBand < bandCount; iBand++) {
188
                        if (iValues[iBand] > 0) {
189
                                mean[iBand] = mean[iBand] / (double) iValues[iBand];
190
                                variance[iBand] = variance[iBand] / (double) iValues[iBand] - mean[iBand] * mean[iBand];
191
                        }
192
                }
193

    
194
                calculated = true;
195
                forceToRecalc = false;
196
                try {
197
                        provider.saveObjectToRmf(RemoteDataStoreStatistics.class, this);
198
                } catch (RmfSerializerException e) {
199
                        // No salva a rmf
200
                }
201
        }
202

    
203
}