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 / DataStoreHistogram.java @ 162
History | View | Annotate | Download (7.66 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.RasterLibrary; |
25 |
import org.gvsig.fmap.dal.coverage.dataset.Buffer; |
26 |
import org.gvsig.fmap.dal.coverage.datastruct.BufferHistogram; |
27 |
import org.gvsig.fmap.dal.coverage.exception.FileNotOpenException; |
28 |
import org.gvsig.fmap.dal.coverage.exception.HistogramException; |
29 |
import org.gvsig.fmap.dal.coverage.exception.InvalidSetViewException; |
30 |
import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException; |
31 |
import org.gvsig.fmap.dal.coverage.exception.RasterDriverException; |
32 |
import org.gvsig.fmap.dal.coverage.exception.RmfSerializerException; |
33 |
import org.gvsig.fmap.dal.coverage.store.props.Histogram; |
34 |
import org.gvsig.raster.impl.datastruct.BufferHistogramImpl; |
35 |
import org.gvsig.raster.impl.process.RasterTask; |
36 |
import org.gvsig.raster.impl.process.RasterTaskQueue; |
37 |
import org.gvsig.raster.impl.provider.DefaultRasterProvider; |
38 |
|
39 |
/**
|
40 |
* Clase para la gesti?n de histogramas de un raster. Es la encargada del calculo de un histograma
|
41 |
* total o parcial de un raster a partir de los datos de disco. Adem?s tambi?n es la encargada de gestionar
|
42 |
* salvar este histograma a .rmf. En caso de que solicite un histograma del raster completo este ir?
|
43 |
* a buscarlo al fichero rmf asociado antes de calcularlo por si ya existise. Al realizar el calculo
|
44 |
* del histograma de la imagen completa este ser? salvado en el fichero .rmf asociado al raster.
|
45 |
*
|
46 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
47 |
*/
|
48 |
public class DataStoreHistogram implements Histogram { |
49 |
/**
|
50 |
* Histograma de la imagen completa
|
51 |
*/
|
52 |
private BufferHistogramImpl histogram = null; |
53 |
/**
|
54 |
* Dataset del cual se calcula el histograma
|
55 |
*/
|
56 |
private DefaultRasterProvider provider = null; |
57 |
private int percent = 0; |
58 |
|
59 |
/**
|
60 |
* Constructor
|
61 |
* @param dataset
|
62 |
*/
|
63 |
public DataStoreHistogram(DefaultRasterProvider provider){
|
64 |
this.provider = provider;
|
65 |
} |
66 |
|
67 |
/**
|
68 |
* Obtiene el minimo valor de las estadisticas de un histograma.
|
69 |
* @return double
|
70 |
*/
|
71 |
public double getMinimum() { |
72 |
return provider.getStatistics().getMinimun();
|
73 |
} |
74 |
|
75 |
/**
|
76 |
* Obtiene el maximo valor de las estadisticas de un histograma.
|
77 |
* @return double
|
78 |
*/
|
79 |
public double getMaximum() { |
80 |
return provider.getStatistics().getMaximun();
|
81 |
} |
82 |
/**
|
83 |
* Obtiene el histograma. Si puede conseguirlo del fichero rmf ir? all? a
|
84 |
* buscarlo sino lo calcular?.
|
85 |
* @return histograma
|
86 |
*/
|
87 |
public BufferHistogram getBufferHistogram() throws HistogramException, ProcessInterruptedException { |
88 |
RasterTask task = RasterTaskQueue.get(Thread.currentThread().toString());
|
89 |
try {
|
90 |
if (provider != null) { |
91 |
|
92 |
try {
|
93 |
provider.getStatistics().calculate(); |
94 |
} catch (FileNotOpenException e) {
|
95 |
throw new HistogramException(""); |
96 |
} catch (RasterDriverException e) {
|
97 |
throw new HistogramException(""); |
98 |
} |
99 |
|
100 |
try {
|
101 |
histogram = (BufferHistogramImpl) provider.loadObjectFromRmf(BufferHistogram.class, histogram); |
102 |
if (histogram != null) |
103 |
return histogram;
|
104 |
} catch (RmfSerializerException e) {
|
105 |
//No carga desde rmf. No afecta a la funcionalidad
|
106 |
} |
107 |
|
108 |
if(task.getEvent() != null) |
109 |
task.manageEvent(task.getEvent()); |
110 |
|
111 |
histogram = new BufferHistogramImpl(provider.getBandCount(), provider.getStatistics().getMin(), provider.getStatistics().getMax(), provider.getDataType()[0]); |
112 |
|
113 |
try {
|
114 |
histogram = (BufferHistogramImpl)getHistogramByDataType(); |
115 |
} catch (FileNotOpenException e) {
|
116 |
throw new HistogramException(""); |
117 |
} catch (RasterDriverException e) {
|
118 |
throw new HistogramException(""); |
119 |
} |
120 |
|
121 |
try {
|
122 |
provider.saveObjectToRmf(BufferHistogram.class, histogram); |
123 |
} catch (RmfSerializerException e) {
|
124 |
//No salva a rmf. No afecta a la funcionalidad
|
125 |
} |
126 |
return histogram;
|
127 |
|
128 |
} |
129 |
} catch (InvalidSetViewException e) {
|
130 |
//La vista se selecciona autom?ticamente no deber?a darse esta excepci?n
|
131 |
} |
132 |
return null; |
133 |
} |
134 |
|
135 |
/**
|
136 |
* Obtiene el histograma teniendo en cuenta la lista de clases
|
137 |
* @return Histograma correspondiente a la lista de clases
|
138 |
*/
|
139 |
private BufferHistogram getHistogramByDataType()
|
140 |
throws InvalidSetViewException, FileNotOpenException, RasterDriverException, ProcessInterruptedException {
|
141 |
RasterTask task = RasterTaskQueue.get(Thread.currentThread().toString());
|
142 |
percent = 0;
|
143 |
int type = provider.getDataType()[0]; |
144 |
int h = RasterLibrary.blockHeight;
|
145 |
|
146 |
histogram.setNoDataValue(provider.getNoDataValue()); |
147 |
for (int block = 0; block < provider.getHeight(); block += h) { |
148 |
Object buf = null; |
149 |
try {
|
150 |
buf = provider.readBlock(block, h); |
151 |
} catch (InvalidSetViewException e) {
|
152 |
// La vista se asigna autom?ticamente
|
153 |
} |
154 |
|
155 |
int hB = h;
|
156 |
if ((block + hB) > provider.getHeight())
|
157 |
hB = Math.abs(((int)provider.getHeight()) - block); |
158 |
|
159 |
switch (type) {
|
160 |
case Buffer.TYPE_BYTE: |
161 |
byte[][][] bBlock = (byte[][][]) buf; |
162 |
for (int iBand = 0; iBand < provider.getBandCount(); iBand++) |
163 |
for (int col = 0; col < provider.getWidth(); col++) |
164 |
for (int row = 0; row < hB; row++) |
165 |
histogram.incrementPxValue(iBand, (double) bBlock[iBand][row][col]);
|
166 |
break;
|
167 |
case Buffer.TYPE_SHORT: |
168 |
short[][][] sBlock = (short[][][]) buf; |
169 |
for (int iBand = 0; iBand < provider.getBandCount(); iBand++) |
170 |
for (int col = 0; col < provider.getWidth(); col++) |
171 |
for (int row = 0; row < hB; row++) |
172 |
histogram.incrementPxValue(iBand, (double) sBlock[iBand][row][col]);
|
173 |
break;
|
174 |
case Buffer.TYPE_INT: |
175 |
int[][][] iBlock = (int[][][]) buf; |
176 |
for (int iBand = 0; iBand < provider.getBandCount(); iBand++) |
177 |
for (int col = 0; col < provider.getWidth(); col++) |
178 |
for (int row = 0; row < hB; row++) |
179 |
histogram.incrementPxValue(iBand, (double) iBlock[iBand][row][col]);
|
180 |
break;
|
181 |
case Buffer.TYPE_FLOAT: |
182 |
float[][][] fBlock = (float[][][]) buf; |
183 |
for (int iBand = 0; iBand < provider.getBandCount(); iBand++) |
184 |
for (int col = 0; col < provider.getWidth(); col++) |
185 |
for (int row = 0; row < hB; row++) |
186 |
histogram.incrementPxValue(iBand, (double) (fBlock[iBand][row][col]));
|
187 |
break;
|
188 |
case Buffer.TYPE_DOUBLE: |
189 |
double[][][] dBlock = (double[][][]) buf; |
190 |
for (int iBand = 0; iBand < provider.getBandCount(); iBand++) |
191 |
for (int col = 0; col < provider.getWidth(); col++) |
192 |
for (int row = 0; row < hB; row++) |
193 |
histogram.incrementPxValue(iBand, (double) (dBlock[iBand][row][col]));
|
194 |
break;
|
195 |
} |
196 |
if (task.getEvent() != null) |
197 |
task.manageEvent(task.getEvent()); |
198 |
percent += ((h * 100) / provider.getHeight());
|
199 |
} |
200 |
percent = 100;
|
201 |
return histogram;
|
202 |
} |
203 |
|
204 |
/**
|
205 |
* Pone a cero el porcentaje de progreso del proceso de calculo de histograma
|
206 |
*/
|
207 |
public void resetPercent() { |
208 |
percent = 0;
|
209 |
} |
210 |
|
211 |
/**
|
212 |
* Obtiene el porcentaje de progreso del proceso de calculo de histograma
|
213 |
* @return porcentaje de progreso
|
214 |
*/
|
215 |
public int getPercent() { |
216 |
return percent;
|
217 |
} |
218 |
} |