Statistics
| Revision:

root / trunk / libraries / libCq_CMS_praster / src / org / cresques / io / data / Grid.java @ 8026

History | View | Annotate | Download (7.79 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2006 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.cresques.io.data;
20

    
21
import org.cresques.io.GeoRasterFile;
22
import org.cresques.io.datastruct.Histogram;
23
import org.cresques.io.datastruct.Statistic;
24
import org.cresques.io.exceptions.FileFoundInListException;
25
import org.cresques.px.Extent;
26
import org.cresques.px.PxRaster;
27
import org.cresques.util.Utilities;
28

    
29
public class Grid implements IQueryableRaster, IWritableRaster  {
30
        
31
        private GeoRasterMultiFile                 grmf = new GeoRasterMultiFile(null);
32
        private RasterBuf                                rasterBuf = null;
33
        private Histogram                                histogram = null;
34
        private PxRaster                                pxRaster = null;
35
        private Statistic                                stats = null;
36
        private int                                                width = 0;
37
        private int                                                height = 0;
38
        
39
        /**
40
         * Constructor
41
         */
42
        public Grid(){}
43
        
44
        /**
45
         * Constructor
46
         * @param grf Lista de geoRasterFile
47
         */
48
        public Grid(GeoRasterFile[] grf){
49
                for(int i = 0; i< grf.length; i++)
50
                        addFile(grf[i]);
51
        }
52
        
53
        /**
54
         * Constructor
55
         * @param grf GeoRasterFile
56
         */
57
        public Grid(GeoRasterFile grf){
58
                addFile(grf);
59
        }
60

    
61
        /**
62
         * Asigna el renderizador para el calculo de histogramas a partir 
63
         * de la visualizaci?n.
64
         * @param pxR Renderizador.
65
         */
66
        public void addRenderizer(PxRaster pxR){
67
                this.pxRaster = pxR;
68
                this.stats = pxR.stats;
69
                this.getHistogram().addRenderizer(pxR);
70
        }
71
        
72
        /**
73
         * A?ade un GeoRasterFile al Grid
74
         * @param grf GeoRasterFile a a?adir
75
         */
76
        public void addFile(GeoRasterFile grf){
77
                try{
78
                        grmf.addFile(grf);
79
                        width = grf.getWidth();
80
                        height = grf.getHeight();
81
                }catch(FileFoundInListException e){
82
                        //El fichero est? en la lista por lo que no lo a?adimos
83
                }
84
        }
85
        
86
        /**
87
         * Elimina un GeoRasterFile del Grid
88
         * @param grf GeoRasterFile a eliminar
89
         */
90
        public void removeFile(GeoRasterFile grf){
91
                grmf.removeFile(grf);
92
        }
93
        
94
        /**
95
         * Obtiene la estructura que contiene la lista de ficheros del Grid
96
         * @return GeoRasterMultiFile
97
         */
98
        public GeoRasterMultiFile getGeoRasterMultiFile(){
99
                return grmf;
100
        }
101
        
102
        /**
103
         * Libera el buffer de memoria
104
         */
105
        public void free(){
106
                rasterBuf = null;
107
                System.gc();
108
        }
109
        
110
        /**
111
         * Resetea la asignaci?n de dibujado de las bandas de la imagen
112
         * sobre el DataImage cuando se hace un update para esta banda.
113
         */
114
        public void clearDrawableBand(){
115
                for(int i = 0; i< grmf.getFileCount(); i++)
116
                        grmf.getBands().clearDrawableBand();
117
        }
118
        
119
        /**
120
         * Para este GeoRasterFile asigna que bandas se pintaran
121
         * sobre el RasterBuf cuando se haga un update. Cada posici?n del vector es una banda
122
         * del rasterBuf y el contenido de esa posici?n es la banda de la imagen que se dibujar?
123
         * sobre ese RasterBuf.
124
         * @param drawableBands        .
125
         */
126
        public void addDrawableBands(int[] drawableBands){
127
                for(int i = 0; i< drawableBands.length; i++)
128
                        grmf.getBands().addDrawableBand(i, drawableBands[i]);                                
129
        }
130
        
131
        /**
132
         * Asigna el ?rea de interes en coordenadas del mundo real. Si las coordenadas exceden del tama?o de la imagen
133
         * estas coordenadas son ajustadas el extent.
134
         * @param x Coordenada X, esquina superior izquierda
135
         * @param y Coordenada Y, esquina superior izquierda
136
         * @param w Ancho del ?rea 
137
         * @param h Alto del ?rea
138
         * @throws ArrayIndexOutOfBoundsException
139
         */
140
        public void setAreaOfInterest(double x, double y, double w, double h) throws ArrayIndexOutOfBoundsException{
141
                Extent ext = new Extent(x, y, x + w, y + h);
142
                ext = Utilities.calculateAdjustedView(ext, grmf.getExtent());
143
                
144
                rasterBuf = grmf.getWindowRaster(x, y, w, h);
145
                //width = rasterBuf.getWidth();
146
                //height = rasterBuf.getHeight();
147
        }
148
        
149
        /**
150
         * Asigna el ?rea de interes en coordenadas pixel. Si las coordenadas exceden del tama?o de la imagen
151
         * lanza una excepci?n.
152
         * @param x Coordenada X, esquina superior izquierda
153
         * @param y Coordenada Y, esquina superior izquierda
154
         * @param w Ancho del ?rea 
155
         * @param h Alto del ?rea
156
         * @throws ArrayIndexOutOfBoundsException
157
         */
158
        public void setAreaOfInterest(int x, int y, int w, int h) throws ArrayIndexOutOfBoundsException{
159
                if((x + w) > getWidth() || (y + h) > getHeight())
160
                        throw new ArrayIndexOutOfBoundsException("Out of Image");
161
                
162
                rasterBuf = grmf.getWindowRaster(x, y, w, h);
163
                //width = rasterBuf.getWidth();
164
                //height = rasterBuf.getHeight();
165
        }
166
                
167
        /**
168
         * Ajusta el ?rea del grid a un ancho y un alto dado en pixeles. Este ajuste se har? 
169
         * en relaci?n a un m?todo de interpolaci?n definido en el par?metro.
170
         * @param w Ancho de la nueva imagen.
171
         * @param h Alto de la nueva imagen.
172
         * @param interpolation M?todo de interpolaci?n que se usar? en el ajuste.
173
         * @param bands Bandas que se desea en el nuevo RasterBuf ajustado. Si este par?metro es null
174
         * se usar?n todas las bandas de la imagen.
175
         */
176
        public void setAdjustedWindow(int w, int h, int interpolationMethod, int[] bands){
177
                if(w == width && h == height)
178
                        return;
179
                switch(interpolationMethod){
180
                case IQueryableRaster.INTERPOLATION_NearestNeighbour:
181
                                rasterBuf = rasterBuf.adjustRasterNearestNeighbour(w, h, bands);
182
                                break;
183
                case IQueryableRaster.INTERPOLATION_Bilinear:
184
                                System.out.println("Method not implemented yet");
185
                                break;
186
                case IQueryableRaster.INTERPOLATION_InverseDistance:
187
                                System.out.println("Method not implemented yet");
188
                                break;
189
                case IQueryableRaster.INTERPOLATION_BicubicSpline:
190
                                System.out.println("Method not implemented yet");
191
                                break;
192
                case IQueryableRaster.INTERPOLATION_BSpline:
193
                                System.out.println("Method not implemented yet");
194
                                break;
195
                }
196
                //width = rasterBuf.getWidth();
197
                //height = rasterBuf.getHeight();
198
        }
199
        
200
        public RasterBuf getData(int x, int y, int w, int h) {
201

    
202
                return null;
203
        }
204

    
205
        /**
206
         * Obtiene el tipo de datos del grid.
207
         * @return entero que representa el tipo de dato de las celdas del grid.
208
         */
209
        public int getDataType() {
210
                return grmf.getDataType()[0];
211
        }
212

    
213
        /**
214
         * Obtiene la altura del grid.
215
         * @return altura en celdas del grid.
216
         */
217
        public int getHeight() {
218
                return height;
219
        }
220

    
221
        /**
222
         * Obtiene la anchura del grid.
223
         * @return anchura en celdas del grid.
224
         */
225
        public int getWidth() {
226
                return width;
227
        }
228

    
229
        public double getXCellSize() {
230

    
231
                return 0;
232
        }
233

    
234
        public double getYCellSize() {
235

    
236
                return 0;
237
        }
238

    
239
        public boolean isNoData(int x, int y, int band) {
240

    
241
                return false;
242
        }
243

    
244
        public void setDataType(int dt) {
245
                
246
        }
247
        
248
        public int getBandCount(){
249
                if(rasterBuf != null)
250
                        return rasterBuf.getBandCount();
251
                else
252
                        return this.getGeoRasterMultiFile().getBandCount();
253
        }
254

    
255
        /**
256
         * Obtiene el buffer de datos del grid
257
         * @return RasterBuf
258
         */
259
        public RasterBuf getRasterBuf() {
260
                return rasterBuf;
261
        }
262
        
263
        /**
264
         * Obtiene el objeto histograma asociado al grid
265
         * @return
266
         */
267
        public Histogram getHistogram() {
268
                if(histogram == null)
269
                        histogram = new Histogram(this);
270
                
271
                return histogram;
272
        }
273

    
274
        /**
275
         * Obtiene la clase para estadisticas
276
         * @return
277
         */
278
        public Statistic getStatistic() {
279
                return stats;
280
        }
281
        
282
        /**
283
         * Evalua si una coordenada pixel cae dentro de la imagen.
284
         * @param x coordenada pixel X
285
         * @param y coordenada pixel Y
286
         */
287
        public boolean isPixelInGrid(int x, int y){
288
                return (x >= 0 && y >= 0 && x <= getWidth() && y <= getHeight());
289
        }
290
        
291
        /**
292
         * Extent del grid
293
         */
294
        public Extent getExtent(){
295
                return grmf.getExtent();
296
        }
297
}
298