root / trunk / libraries / libCresques / src / org / cresques / io / data / Metadata.java @ 13130
History | View | Annotate | Download (7.11 KB)
1 |
/*
|
---|---|
2 |
* Cresques Mapping Suite. Graphic Library for constructing mapping applications.
|
3 |
*
|
4 |
* Copyright (C) 2004-5.
|
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
19 |
*
|
20 |
* For more information, contact:
|
21 |
*
|
22 |
* cresques@gmail.com
|
23 |
*/
|
24 |
package org.cresques.io.data; |
25 |
|
26 |
import java.util.ArrayList; |
27 |
|
28 |
import org.cresques.filter.Transparency.TransparencyRange; |
29 |
|
30 |
/**
|
31 |
* Guarda en un Array los metadatos de los distintos tipos de imagenes.
|
32 |
*
|
33 |
* NODATA_VALUES=255 255 255 1
|
34 |
* AREA_OR_POINT=Point 4
|
35 |
* TIFFTAG_XRESOLUTION=72 4
|
36 |
* TIFFTAG_YRESOLUTION=72 4
|
37 |
* TIFFTAG_RESOLUTIONUNIT=2 (pixels/inch) 4
|
38 |
*
|
39 |
* STATISTICS_MINIMUM=0
|
40 |
* STATISTICS_MAXIMUM=221
|
41 |
* STATISTICS_MEAN=35.910196078431
|
42 |
* STATISTICS_MEDIAN=30
|
43 |
* STATISTICS_MODE=0
|
44 |
* STATISTICS_STDDEV=29.609849452294
|
45 |
* STATISTICS_HISTONUMBINS=256
|
46 |
* STATISTICS_HISTOMIN=0
|
47 |
* STATISTICS_HISTOMAX=255
|
48 |
* LAYER_TYPE=athematic
|
49 |
* STATISTICS_HISTOBINVALUES=30285675|0|0|22050| ...
|
50 |
*
|
51 |
* @author Nacho Brodin (brodin_ign@gva.es)
|
52 |
*/
|
53 |
public class Metadata{ |
54 |
/**
|
55 |
* Valores para los identificadores de los metadatos
|
56 |
*/
|
57 |
private ArrayList metadata = new ArrayList(); |
58 |
/**
|
59 |
* Valores de los metadatos
|
60 |
*/
|
61 |
private ArrayList values = new ArrayList(); |
62 |
/**
|
63 |
* Interpretaci?n de color para cada banda
|
64 |
*/
|
65 |
private String[] colorInterpretation = null; |
66 |
/**
|
67 |
* true si la imagen tiene una banda con el identificador de interpretaci?n de color a Alpha
|
68 |
*/
|
69 |
private boolean isAlphaBand = false; |
70 |
/**
|
71 |
* Valores no data para cada banda de la imagen (si los tiene)
|
72 |
*/
|
73 |
private double[] noDataByBand = null; |
74 |
/**
|
75 |
* Metadatos
|
76 |
*/
|
77 |
private String[] metadataString = null; |
78 |
|
79 |
public Metadata(String[] metadata){ |
80 |
metadataString = metadata; |
81 |
for(int i = 0;i<metadata.length;i++){ |
82 |
String[] value = metadata[i].split("="); |
83 |
this.metadata.add(value[0]); |
84 |
this.values.add(value[1]); |
85 |
} |
86 |
} |
87 |
|
88 |
/**
|
89 |
* Crea un objeto TransparencyRange a partir de los rangos de transparencia
|
90 |
* @return
|
91 |
*/
|
92 |
public TransparencyRange parserNodataByBand(){
|
93 |
int bandR = getBand("Red"); |
94 |
int bandG = getBand("Green"); |
95 |
int bandB = getBand("Blue"); |
96 |
|
97 |
if(bandR < 0 && bandG < 0 && bandB < 0) |
98 |
return null; |
99 |
|
100 |
//Esta comprobaci?n es para los raster con paleta (gif). Cuando se trate la paleta y no se usen como
|
101 |
//imagenes de una banda habr? que cambiar esto
|
102 |
if((colorInterpretation.length == 1) && colorInterpretation[0].equals("Palette")) |
103 |
return null; |
104 |
|
105 |
if(noDataByBand == null) |
106 |
return null; |
107 |
|
108 |
//Si todos los valores nodata por banda son -1 es que no hay ninguno asignado
|
109 |
int count =0; |
110 |
for(int i = 0; i < noDataByBand.length; i++) |
111 |
if(noDataByBand[i] < 0) |
112 |
count ++; |
113 |
|
114 |
if(count == noDataByBand.length)
|
115 |
return null; |
116 |
|
117 |
TransparencyRange tr = new TransparencyRange();
|
118 |
int[] red = new int[2]; |
119 |
int[] green = new int[2]; |
120 |
int[] blue = new int[2]; |
121 |
|
122 |
if(bandR >= 0){ |
123 |
red[0] = red[1] = (int)noDataByBand[bandR]; |
124 |
tr.setRed(red); |
125 |
} |
126 |
if(bandG >= 0){ |
127 |
green[0] = green[1] = (int)noDataByBand[bandG]; |
128 |
tr.setGreen(green); |
129 |
} |
130 |
if(bandB >= 0){ |
131 |
blue[0] = blue[1] = (int)noDataByBand[bandB]; |
132 |
tr.setBlue(blue); |
133 |
} |
134 |
|
135 |
tr.setAnd(true);
|
136 |
tr.loadStrEntryFromValues(); |
137 |
|
138 |
return tr;
|
139 |
} |
140 |
|
141 |
/**
|
142 |
* Parsea los metadatos NODATA_VALUES si existe alguno y devuelve un objeto TransparencyRange.
|
143 |
* @param nodata
|
144 |
* @return Vector de enteros con los valores RGBA o null si no tiene este metadato o no es parseable
|
145 |
* en este formato.
|
146 |
*/
|
147 |
public TransparencyRange[] parserNodataInMetadata(){ |
148 |
//Esta comprobaci?n es para los raster con paleta (gif). Cuando se trate la paleta y no se usen como
|
149 |
//imagenes de una banda habr? que cambiar esto
|
150 |
if((colorInterpretation.length == 1) && colorInterpretation[0].equals("Palette")) |
151 |
return null; |
152 |
|
153 |
int count = 0; |
154 |
for(int i = 0; i < metadata.size(); i++){ |
155 |
if(((String)metadata.get(i)).equals("NODATA_VALUES")) |
156 |
count ++; |
157 |
} |
158 |
if(count == 0) |
159 |
return null; |
160 |
|
161 |
TransparencyRange[] trList = new TransparencyRange[count]; |
162 |
|
163 |
count = 0;
|
164 |
for(int i = 0; i < metadata.size(); i++){ |
165 |
TransparencyRange tr = new TransparencyRange();
|
166 |
int[] red = new int[2]; |
167 |
int[] green = new int[2]; |
168 |
int[] blue = new int[2]; |
169 |
|
170 |
if(((String)metadata.get(i)).equals("NODATA_VALUES")){ |
171 |
String data = (String)values.get(i); |
172 |
String[] dataValues = data.split(" "); |
173 |
try{
|
174 |
red[0] = red[1] = Integer.parseInt(dataValues[0]); |
175 |
green[0] = green[1] = Integer.parseInt(dataValues[1]); |
176 |
blue[0] = blue[1] = Integer.parseInt(dataValues[2]); |
177 |
}catch(NumberFormatException exc){ |
178 |
return null; |
179 |
} |
180 |
} |
181 |
tr.setAnd(true);
|
182 |
tr.setRed(red); |
183 |
tr.setGreen(green); |
184 |
tr.setBlue(blue); |
185 |
tr.loadStrEntryFromValues(); |
186 |
trList[count] = tr; |
187 |
count ++; |
188 |
} |
189 |
|
190 |
return trList;
|
191 |
} |
192 |
|
193 |
/**
|
194 |
* Inicializa el vector de cadenas que contendr?n el nombre de la interpretaci?n
|
195 |
* de color asignada a cada banda. Este valor es el devuelto por la imagen.
|
196 |
* @param values N?mero de valores
|
197 |
*/
|
198 |
public void initColorInterpretation(int values){ |
199 |
colorInterpretation = new String[values]; |
200 |
} |
201 |
|
202 |
/**
|
203 |
* Asigna un valor para la interpretaci?n de color de una banda
|
204 |
* @param band Banda
|
205 |
* @param value valor
|
206 |
*/
|
207 |
public void setColorInterpValue(int band, String value){ |
208 |
try{
|
209 |
colorInterpretation[band] = value; |
210 |
if(value.equals("Alpha")) |
211 |
isAlphaBand = true;
|
212 |
}catch(ArrayIndexOutOfBoundsException ex){ |
213 |
//No asignamos el elemento
|
214 |
} |
215 |
} |
216 |
|
217 |
/**
|
218 |
* Obtiene la posici?n de la banda que contiene el identificador pasado por par?metro
|
219 |
* o -1 si no tiene dicho identificador.
|
220 |
* @return Posici?n de la banda que contiene el identificador o -1 si no lo tiene.
|
221 |
*/
|
222 |
public int getBand(String id){ |
223 |
if(colorInterpretation != null){ |
224 |
for(int i = 0; i < colorInterpretation.length; i++) |
225 |
if(colorInterpretation[i].equals(id))
|
226 |
return i;
|
227 |
} |
228 |
return -1; |
229 |
} |
230 |
|
231 |
/**
|
232 |
* Obtiene true si existe una banda de alpha
|
233 |
* @return
|
234 |
*/
|
235 |
public boolean isAlphaBand() { |
236 |
return isAlphaBand;
|
237 |
} |
238 |
|
239 |
/**
|
240 |
* Inicializa los valores no data;
|
241 |
* @param values
|
242 |
*/
|
243 |
public void initNoDataByBand(int values){ |
244 |
noDataByBand = new double[values]; |
245 |
for(int i = 0; i < values; i++) |
246 |
noDataByBand[i] = -1;
|
247 |
} |
248 |
|
249 |
/**
|
250 |
* Asigna un valor para el valor noData por banda
|
251 |
* @param band Banda
|
252 |
* @param value valor
|
253 |
*/
|
254 |
public void setNoDataValue(int band, double value){ |
255 |
try{
|
256 |
noDataByBand[band] = value; |
257 |
}catch(ArrayIndexOutOfBoundsException ex){ |
258 |
//No asignamos el elemento
|
259 |
} |
260 |
} |
261 |
|
262 |
/**
|
263 |
* Obtiene los metadatos en forma de vector de cadenas
|
264 |
* @return Vector de cadenas en el que cada cadena es atributo=valor
|
265 |
*/
|
266 |
public String[] getMetadataString() { |
267 |
return metadataString;
|
268 |
} |
269 |
|
270 |
} |