Statistics
| Revision:

root / trunk / libraries / libCq_CMS_praster / src / org / cresques / io / datastruct / Metadata.java @ 8026

History | View | Annotate | Download (7.27 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.datastruct;
25

    
26
import java.util.ArrayList;
27

    
28
import org.cresques.filter.enhancement.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
                if(metadata == null)
81
                        return;
82
                metadataString = metadata;
83
                for(int i = 0;i<metadata.length;i++){
84
                        String[] value = metadata[i].split("=");
85
                        if(value.length >= 2){
86
                                this.metadata.add(value[0]);
87
                                this.values.add(value[1]);
88
                        }
89
                }
90
        }
91
        
92
        /**
93
         * Crea un objeto TransparencyRange a partir de los rangos de transparencia
94
         * @return
95
         */
96
        public TransparencyRange parserNodataByBand(){
97
                int bandR = getBand("Red");
98
                int bandG = getBand("Green");
99
                int bandB = getBand("Blue");
100
                
101
                if(bandR < 0 && bandG < 0 && bandB < 0)
102
                        return null;
103
                
104
                //Esta comprobaci?n es para los raster con paleta (gif). Cuando se trate la paleta y no se usen como
105
                //imagenes de una banda habr? que cambiar esto
106
                if((colorInterpretation.length == 1) && colorInterpretation[0].equals("Palette"))
107
                        return null;
108
                                
109
                if(noDataByBand == null)
110
                        return null;
111
                
112
                //Si todos los valores nodata por banda son -1 es que no hay ninguno asignado 
113
                int count =0;
114
                for(int i = 0; i < noDataByBand.length; i++)
115
                        if(noDataByBand[i] < 0)
116
                                count ++;
117
                
118
                if(count == noDataByBand.length)
119
                        return null;
120
                
121
                TransparencyRange tr = new TransparencyRange();
122
                int[] red = new int[2];
123
                int[] green = new int[2];
124
                int[] blue = new int[2];
125
                
126
                if(bandR >= 0){
127
                        red[0] = red[1] = (int)noDataByBand[bandR];
128
                        tr.setRed(red);
129
                }
130
                if(bandG >= 0){
131
                        green[0] = green[1] = (int)noDataByBand[bandG];
132
                        tr.setGreen(green);
133
                }
134
                if(bandB >= 0){
135
                        blue[0] = blue[1] = (int)noDataByBand[bandB];
136
                        tr.setBlue(blue);
137
                }
138
                
139
                tr.setAnd(true);
140
                tr.loadStrEntryFromValues();
141
                
142
                return tr;
143
        }
144
        
145
        /**
146
         * Parsea los metadatos NODATA_VALUES si existe alguno y devuelve un objeto TransparencyRange. 
147
         * @param nodata
148
         * @return Vector de enteros con los valores RGBA o null si no tiene este metadato o no es parseable
149
         * en este formato.
150
         */
151
        public TransparencyRange[] parserNodataInMetadata(){
152
                //Esta comprobaci?n es para los raster con paleta (gif). Cuando se trate la paleta y no se usen como
153
                //imagenes de una banda habr? que cambiar esto
154
                if((colorInterpretation.length == 1) && colorInterpretation[0].equals("Palette"))
155
                        return null;
156
                
157
                int count = 0;
158
                for(int i = 0; i < metadata.size(); i++){
159
                        if(((String)metadata.get(i)).equals("NODATA_VALUES"))
160
                                count ++;
161
                }
162
                if(count == 0)
163
                        return null;
164
                
165
                TransparencyRange[] trList = new TransparencyRange[count];
166
                
167
                count = 0;
168
                for(int i = 0; i < metadata.size(); i++){
169
                        TransparencyRange tr = new TransparencyRange();
170
                        int[] red = new int[2];
171
                        int[] green = new int[2];
172
                        int[] blue = new int[2];
173
                        
174
                        if(((String)metadata.get(i)).equals("NODATA_VALUES")){
175
                                String data = (String)values.get(i);
176
                                String[] dataValues = data.split(" ");
177
                                int[] values = new int[dataValues.length];
178
                                try{
179
                                        red[0] = red[1] = Integer.parseInt(dataValues[0]);
180
                                        green[0] = green[1] = Integer.parseInt(dataValues[1]);
181
                                        blue[0] = blue[1] = Integer.parseInt(dataValues[2]);
182
                                }catch(NumberFormatException exc){
183
                                        return null;
184
                                }
185
                        }
186
                        tr.setAnd(true);
187
                        tr.setRed(red);
188
                        tr.setGreen(green);
189
                        tr.setBlue(blue);
190
                        tr.loadStrEntryFromValues();
191
                        trList[count] = tr;
192
                        count ++;
193
                }
194
                
195
                return trList;
196
        }
197
        
198
        /**
199
         * Inicializa el vector de cadenas que contendr?n el nombre de la interpretaci?n 
200
         * de color asignada a cada banda. Este valor es el devuelto por la imagen.
201
         * @param values N?mero de valores
202
         */
203
        public void initColorInterpretation(int values){
204
                colorInterpretation = new String[values];
205
        }
206
        
207
        /**
208
         * Asigna un valor para la interpretaci?n de color de una banda
209
         * @param band Banda 
210
         * @param value valor
211
         */
212
        public void setColorInterpValue(int band, String value){
213
                try{
214
                        colorInterpretation[band] = value;
215
                        if(value.equals("Alpha"))
216
                                isAlphaBand = true;
217
                }catch(ArrayIndexOutOfBoundsException ex){
218
                        //No asignamos el elemento
219
                }
220
        }
221
        
222
        /**
223
         * Obtiene la posici?n de la banda que contiene el identificador pasado por par?metro 
224
         * o -1 si no tiene dicho identificador.
225
         * @return Posici?n de la banda que contiene el identificador o -1 si no lo tiene.
226
         */
227
        public int getBand(String id){
228
                if(colorInterpretation != null){
229
                        for(int i = 0; i < colorInterpretation.length; i++)
230
                                if(colorInterpretation[i].equals(id))
231
                                        return i;
232
                }
233
                return -1;
234
        }
235

    
236
        /**
237
         * Obtiene true si existe una banda de alpha
238
         * @return
239
         */
240
        public boolean isAlphaBand() {
241
                return isAlphaBand;
242
        }
243
        
244
        /**
245
         * Inicializa los valores no data;
246
         * @param values
247
         */
248
        public void initNoDataByBand(int values){
249
                noDataByBand = new double[values];
250
                for(int i = 0; i < values; i++)
251
                        noDataByBand[i] = -1;
252
        }
253
        
254
        /**
255
         * Asigna un valor para el valor noData por banda
256
         * @param band Banda 
257
         * @param value valor
258
         */
259
        public void setNoDataValue(int band, double value){
260
                try{
261
                        noDataByBand[band] = value;
262
                }catch(ArrayIndexOutOfBoundsException ex){
263
                        //No asignamos el elemento
264
                }
265
        }
266

    
267
        /**
268
         * Obtiene los metadatos en forma de vector de cadenas
269
         * @return Vector de cadenas en el que cada cadena es atributo=valor
270
         */
271
        public String[] getMetadataString() {
272
                return metadataString;
273
        }
274
        
275
}