root / trunk / libraries / libRaster / src / org / gvsig / raster / dataset / properties / DatasetHistogram.java @ 10960
History | View | Annotate | Download (6.09 KB)
1 | 10939 | nacho | /* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|---|---|
2 | *
|
||
3 | * Copyright (C) 2007 IVER T.I. and Generalitat Valenciana.
|
||
4 | *
|
||
5 | * This program is free software; you can redistribute it and/or
|
||
6 | * modify it under the terms of the GNU General Public License
|
||
7 | * as published by the Free Software Foundation; either version 2
|
||
8 | * of the License, or (at your option) any later version.
|
||
9 | *
|
||
10 | * This program is distributed in the hope that it will be useful,
|
||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
13 | * GNU General Public License for more details.
|
||
14 | *
|
||
15 | * You should have received a copy of the GNU General Public License
|
||
16 | * along with this program; if not, write to the Free Software
|
||
17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
||
18 | */
|
||
19 | package org.gvsig.raster.dataset.properties; |
||
20 | |||
21 | 10960 | nacho | import org.gvsig.raster.RasterLibrary; |
22 | 10939 | nacho | import org.gvsig.raster.dataset.FileNotOpenException; |
23 | import org.gvsig.raster.dataset.IBuffer; |
||
24 | import org.gvsig.raster.dataset.InvalidSetViewException; |
||
25 | import org.gvsig.raster.dataset.RasterDataset; |
||
26 | import org.gvsig.raster.dataset.RasterDriverException; |
||
27 | import org.gvsig.raster.util.DataClassList; |
||
28 | import org.gvsig.raster.util.Histogram; |
||
29 | |||
30 | /**
|
||
31 | * Clase para la gesti?n de histogramas de un raster. Es la encargada del calculo de un histograma
|
||
32 | * total o parcial de un raster a partir de los datos de disco. Adem?s tambi?n es la encargada de gestionar
|
||
33 | * salvar este histograma a .rmf. En caso de que solicite un histograma del raster completo este ir?
|
||
34 | * a buscarlo al fichero rmf asociado antes de calcularlo por si ya existise. Al realizar el calculo
|
||
35 | * del histograma de la imagen completa este ser? salvado en el fichero .rmf asociado al raster.
|
||
36 | *
|
||
37 | * @author Nacho Brodin (nachobrodin@gmail.com)
|
||
38 | */
|
||
39 | public class DatasetHistogram { |
||
40 | /**
|
||
41 | * clases en las que se divide el histograma
|
||
42 | */
|
||
43 | private DataClassList classes = null; |
||
44 | /**
|
||
45 | * Histograma de la imagen completa
|
||
46 | */
|
||
47 | private Histogram histogram = null; |
||
48 | /**
|
||
49 | * Dataset del cual se calcula el histograma
|
||
50 | */
|
||
51 | private RasterDataset dataset = null; |
||
52 | |||
53 | /**
|
||
54 | * Constructor
|
||
55 | * @param dataset
|
||
56 | */
|
||
57 | public DatasetHistogram(RasterDataset dataset){
|
||
58 | this.dataset = dataset;
|
||
59 | } |
||
60 | |||
61 | /**
|
||
62 | * Asigna una lista de clases sobre la que se calcular? la petici?n de
|
||
63 | * histograma.
|
||
64 | * @param classes
|
||
65 | */
|
||
66 | public void setClasses(DataClassList classes){ |
||
67 | this.classes = classes;
|
||
68 | } |
||
69 | |||
70 | /**
|
||
71 | * Obtiene el histograma. Si puede conseguirlo del fichero rmf ir? all? a
|
||
72 | * buscarlo sino lo calcular?.
|
||
73 | * @return histograma
|
||
74 | */
|
||
75 | public Histogram getHistogram() throws FileNotOpenException, RasterDriverException { |
||
76 | try {
|
||
77 | if(dataset != null){ |
||
78 | if(classes == null){ |
||
79 | if(dataset.getDataType() == IBuffer.TYPE_BYTE){
|
||
80 | histogram = new Histogram(dataset.getBandCount(), 256); |
||
81 | return getHistogramByValue();
|
||
82 | }else{
|
||
83 | DatasetStatistics stats = dataset.getStatistics(); |
||
84 | stats.calcFullStatistics(); |
||
85 | classes = DataClassList.getClasses(stats.getMinimun(), stats.getMaximun(), RasterLibrary.defaultNumberOfClasses); |
||
86 | } |
||
87 | |||
88 | } |
||
89 | histogram = new Histogram(dataset.getBandCount(), classes.length());
|
||
90 | return getHistogramByClass();
|
||
91 | } |
||
92 | } catch (InvalidSetViewException e) {
|
||
93 | //La vista se selecciona autom?ticamente no deber?a darse esta excepci?n
|
||
94 | } |
||
95 | return null; |
||
96 | } |
||
97 | |||
98 | /**
|
||
99 | * Obtiene un histograma para valores de tipo byte. El resto de tipos
|
||
100 | * de datos se separan por clases, bien introducidas por el usuario
|
||
101 | * bien autocalculadas.
|
||
102 | *
|
||
103 | * @return histograma
|
||
104 | */
|
||
105 | private Histogram getHistogramByValue() throws InvalidSetViewException, FileNotOpenException, RasterDriverException { |
||
106 | if(dataset.getDataType() != IBuffer.TYPE_BYTE)
|
||
107 | return null; |
||
108 | |||
109 | for (int band = 0; band < dataset.getBandCount(); band++) { |
||
110 | for (int line = 0; line < dataset.getHeight(); line++) { |
||
111 | Object obj = dataset.readCompleteLine(line, band);
|
||
112 | byte[] bLine = (byte[])obj; |
||
113 | for (int k = 0; k < bLine.length; k++) |
||
114 | histogram.incrementPxValue(band, (bLine[k] & 0xff));
|
||
115 | } |
||
116 | } |
||
117 | return histogram;
|
||
118 | } |
||
119 | |||
120 | /**
|
||
121 | * Obtiene el histograma teniendo en cuenta la lista de clases
|
||
122 | * @return Histograma correspondiente a la lista de clases
|
||
123 | */
|
||
124 | private Histogram getHistogramByClass() throws InvalidSetViewException, FileNotOpenException, RasterDriverException { |
||
125 | if(classes == null) |
||
126 | return null; |
||
127 | for (int band = 0; band < dataset.getBandCount(); band++) { |
||
128 | for (int line = 0; line < dataset.getHeight(); line++) { |
||
129 | Object obj = dataset.readCompleteLine(line, band);
|
||
130 | switch(dataset.getDataType()){
|
||
131 | case IBuffer.TYPE_BYTE: byte[] bLine = (byte[])obj; |
||
132 | for (int k = 0; k < bLine.length; k++) { |
||
133 | int pixelValue = classes.getClassPosition((double)bLine[k]); |
||
134 | if(pixelValue >= 0) |
||
135 | histogram.incrementPxValue(band, pixelValue); |
||
136 | |||
137 | } |
||
138 | break;
|
||
139 | case IBuffer.TYPE_SHORT: short[] sLine = (short[])obj; |
||
140 | for (int k = 0; k < sLine.length; k++) { |
||
141 | int pixelValue = classes.getClassPosition((double)sLine[k]); |
||
142 | if(pixelValue >= 0) |
||
143 | histogram.incrementPxValue(band, pixelValue); |
||
144 | } |
||
145 | break;
|
||
146 | case IBuffer.TYPE_INT: int[] iLine = (int[])obj; |
||
147 | for (int k = 0; k < iLine.length; k++) { |
||
148 | int pixelValue = classes.getClassPosition((double)iLine[k]); |
||
149 | if(pixelValue >= 0) |
||
150 | histogram.incrementPxValue(band, pixelValue); |
||
151 | } |
||
152 | break;
|
||
153 | case IBuffer.TYPE_FLOAT: float[] fLine = (float[])obj; |
||
154 | for (int k = 0; k < fLine.length; k++) { |
||
155 | int pixelValue = classes.getClassPosition((double)fLine[k]); |
||
156 | if(pixelValue >= 0) |
||
157 | histogram.incrementPxValue(band, pixelValue); |
||
158 | } |
||
159 | break;
|
||
160 | case IBuffer.TYPE_DOUBLE: double[] dLine = (double[])obj; |
||
161 | for (int k = 0; k < dLine.length; k++) { |
||
162 | int pixelValue = classes.getClassPosition((double)dLine[k]); |
||
163 | if(pixelValue >= 0) |
||
164 | histogram.incrementPxValue(band, pixelValue); |
||
165 | } |
||
166 | break;
|
||
167 | } |
||
168 | |||
169 | } |
||
170 | } |
||
171 | return histogram;
|
||
172 | } |
||
173 | } |