Statistics
| Revision:

gvsig-raster / org.gvsig.raster.cache / trunk / org.gvsig.raster.cache / org.gvsig.raster.cache.lib.impl / src / main / java / org / gvsig / raster / cache / buffer / impl / histogram / BufferHistogramComputer.java @ 1419

History | View | Annotate | Download (4.86 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.cache.buffer.impl.histogram;
23

    
24
import org.gvsig.raster.cache.buffer.Buffer;
25
import org.gvsig.raster.cache.buffer.exception.HistogramException;
26
import org.gvsig.raster.cache.buffer.exception.ProcessInterruptedException;
27
import org.gvsig.raster.cache.buffer.histogram.BufferHistogram;
28
import org.gvsig.raster.cache.buffer.histogram.HistogramComputer;
29

    
30

    
31
/**
32
 * This class computes the histogram for a <code>Buffer</code>
33
 * 
34
 * @author Nacho Brodin (nachobrodin@gmail.com)
35
 */
36
public class BufferHistogramComputer implements HistogramComputer {
37
        private BufferHistogram             histogram            = null;
38
        private Buffer                      buffer               = null;
39
        protected int                                    progressHistogram    = 0;
40
        private double[]                    limits               = null;
41
        private boolean                     refresh              = false;
42
        
43
        /**
44
         * Constructor
45
         * @param dataset
46
         */
47
        public BufferHistogramComputer(Buffer buffer) {
48
                this.buffer = buffer;
49
        }
50
        
51
        /*
52
         * (non-Javadoc)
53
         * @see org.gvsig.fmap.dal.coverage.store.props.HistogramComputer#getBufferHistogram()
54
         */
55
        public BufferHistogram getBufferHistogram(int numberOfClasses) throws ProcessInterruptedException, HistogramException {
56
                if(histogram == null || refresh) {
57
                        //RasterTask task = RasterTaskQueue.get(Thread.currentThread().getId() + "");
58
                        progressHistogram = 0;
59
                        double[][] limits = buffer.getStatistics().getAllBandsLimits();
60

    
61
                        BufferHistogramImpl hist = new BufferHistogramImpl(buffer.getBandCount(), limits[0], limits[1], buffer.getDataType(), numberOfClasses);
62

    
63
                        for (int iBand = 0; iBand < buffer.getBandCount(); iBand++) {
64
                                for (int row = 0; row < buffer.getHeight(); row++) {
65
                                        switch(buffer.getDataType()) {
66
                                        case Buffer.TYPE_BYTE:
67
                                                for (int col = 0; col < buffer.getWidth(); col++) 
68
                                                        hist.incrementPxValue(iBand, (double)(buffer.getElemByte(row, col, iBand)));
69
                                                break;
70
                                        case Buffer.TYPE_SHORT:
71
                                                for (int col = 0; col < buffer.getWidth(); col++) 
72
                                                        hist.incrementPxValue(iBand, (double)(buffer.getElemShort(row, col, iBand)));
73
                                                break;
74
                                        case Buffer.TYPE_INT:
75
                                                for (int col = 0; col < buffer.getWidth(); col++) 
76
                                                        hist.incrementPxValue(iBand, (double)(buffer.getElemInt(row, col, iBand)));
77
                                                break;
78
                                        case Buffer.TYPE_FLOAT:
79
                                                for (int col = 0; col < buffer.getWidth(); col++) 
80
                                                        hist.incrementPxValue(iBand, (double)buffer.getElemFloat(row, col, iBand));
81
                                                break;
82
                                        case Buffer.TYPE_DOUBLE:
83
                                                for (int col = 0; col < buffer.getWidth(); col++) 
84
                                                        hist.incrementPxValue(iBand, buffer.getElemDouble(row, col, iBand));
85
                                                break;
86
                                        }
87

    
88
                                        /*if (task.getEvent() != null)
89
                                                task.manageEvent(task.getEvent());*/
90

    
91
                                        progressHistogram = ((iBand * buffer.getHeight() + row) * 100) /(buffer.getHeight() * buffer.getBandCount());
92
                                }
93
                        }
94
                        progressHistogram = 100;
95
                        histogram = hist;
96
                }
97
                return histogram;
98
        }
99

    
100
        /*
101
         * (non-Javadoc)
102
         * @see org.gvsig.fmap.dal.coverage.store.props.HistogramComputer#resetPercent()
103
         */
104
        public void resetPercent() {
105
                progressHistogram = 0;
106
        }
107

    
108
        /*
109
         * (non-Javadoc)
110
         * @see org.gvsig.fmap.dal.coverage.store.props.HistogramComputer#getPercent()
111
         */
112
        public int getPercent() {
113
                return progressHistogram;
114
        }
115

    
116
        /*
117
         * (non-Javadoc)
118
         * @see org.gvsig.fmap.dal.coverage.store.props.HistogramComputer#getMinimum()
119
         */
120
        public double getMinimum() {
121
                if(limits == null)
122
                        try {
123
                                limits = buffer.getStatistics().getLimits();
124
                        } catch (ProcessInterruptedException e) {
125
                                return 0;
126
                        }
127
                return limits[0];
128
        }
129

    
130
        /*
131
         * (non-Javadoc)
132
         * @see org.gvsig.fmap.dal.coverage.store.props.HistogramComputer#getMaximum()
133
         */
134
        public double getMaximum() {
135
                if(limits == null)
136
                        try {
137
                                limits = buffer.getStatistics().getLimits();
138
                        } catch (ProcessInterruptedException e) {
139
                                return 0;
140
                        }
141
                return limits[1];
142
        }
143
        
144
        /*
145
         * (non-Javadoc)
146
         * @see org.gvsig.fmap.dal.coverage.store.props.HistogramComputer#refreshHistogram()
147
         */
148
        public void refreshHistogram() {
149
                refresh = true;
150
        }
151
}