Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libRaster / src / org / gvsig / raster / grid / Grid.java @ 30008

History | View | Annotate | Download (38.6 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.gvsig.raster.grid;
20

    
21
import java.io.IOException;
22
import java.util.Arrays;
23

    
24
import org.gvsig.raster.buffer.BufferFactory;
25
import org.gvsig.raster.buffer.RasterBuffer;
26
import org.gvsig.raster.buffer.RasterBufferInvalidAccessException;
27
import org.gvsig.raster.buffer.RasterBufferInvalidException;
28
import org.gvsig.raster.dataset.IBuffer;
29
import org.gvsig.raster.dataset.IRasterDataSource;
30
import org.gvsig.raster.datastruct.Extent;
31
import org.gvsig.raster.datastruct.Transparency;
32
import org.gvsig.raster.grid.filter.RasterFilterList;
33

    
34
/**
35
 * Clase que representa una rejilla de datos raster. Este tipo de grid tiene el interfaz necesario
36
 * de acceso a la informaci?n raster para aplicaciones de analisis raster. Contiene m?todos de acceso al
37
 * dato tanto en lectura como en escritura y encapsula operaciones entre grids. Adem?s contiene
38
 * objetos que contienen las caracter?sticas asociadas a ese grid. Sobre un grid pueden
39
 * aplicarse tambi?n operaciones de filtrado.
40
 *
41
 * Podemos crear un Grid de cinco formas distitas:
42
 * <UL>
43
 * <LI>A partir de una fuente de datos (cargada en el constructor). Se har? una petici?n del extent pasado por par?metro con las bandas seleccionadas por par?metro.</LI>
44
 * <LI>A partir de un extensi?n de capa, una extensi?n de vista, un tipo de dato y un n?mero de datos
45
 * crea un Grid vacio util para escritura.</LI>
46
 * <LI>A partir de una fuente de datos (cargados). Datasource se usa como buffer de datos cargados y se selecciona si queremos el reader interpolado.</LI>
47
 * <LI>A partir de una fuente de datos (cargada en el constructor). Se har? una petici?n del extent completo de la fuente con las bandas seleccionadas por par?metro.</LI>
48
 * <LI>A partir de una fuente de datos (cargada en el constructor). Se har? una petici?n del extent completo de la fuente con todas las bandas disponibles.</LI>
49
 * </UL>
50
 *
51
 * @author Victor Olaya (volaya@ya.com)
52
 * @author Nacho Brodin (nachobrodin@gmail.com)
53
 */
54
public class Grid implements IQueryableGrid, IWritableGrid{
55

    
56
        public final static double                 DEG_45_IN_RAD = Math.PI / 180. * 45.;
57
        public final static double                 DEG_90_IN_RAD = Math.PI / 180. * 90.;
58
        public final static double                 DEG_180_IN_RAD = Math.PI ;
59
        public final static double                 DEG_270_IN_RAD = Math.PI / 180. * 270.;
60
        public final static double                 DEG_360_IN_RAD = Math.PI * 2.;
61

    
62
        /* neighbor's address*/                               /* N  NE   E  SE   S  SW   W  NW */
63
        private final static int                 m_iOffsetX []=        {  0,  1,  1,  1,  0, -1, -1, -1};
64
        private final static int                m_iOffsetY []=        {  1,  1,  0, -1, -1, -1,  0,  1};
65

    
66
        private double                                         m_dDist[];
67
        public double                                         _2DX, _6DX, _DX_2, _4DX_2;
68

    
69
        private int[]                                         bands = null;
70
        private int                                         dataType = IBuffer.TYPE_UNDEFINED;
71

    
72
        private RasterBuffer                        rasterBuf = null;
73
        private GridReader                                 reader = null;
74
        private GridWriter                                 writer = null;
75
        private GridExtent                                 windowExtent = null;
76
        private GridExtent                                 layerExtent = null;
77
        private GridStatistic                        statistic = null;
78
        private GridPalette[]                        palette = null;
79
        private RasterFilterList                filterList = null;
80

    
81

    
82

    
83
        /**
84
         * Crea un grid a partir de un MultiRasterDataset. Usa el extent de la fuente de datos como
85
         * extent completo y el extent pasado como par?metro como extensi?n de ventana.
86
         *
87
         * Cuando se construye el reader se carga el buffer con la extensi?n definida en windowExtent
88
         * y las bandas especificadas en bands.
89
         *
90
         * @param IRasterDataSource datasets que proporcionan los datos
91
         * @param bands n?mero de bandas requeridas
92
         * @param windowExtent Extensi?n de la ventana. Si este par?metro es null se usar? el
93
         * mismo que el de la capa.
94
         */
95
        public Grid(IRasterDataSource datasets, int[] bands, GridExtent windowExtent)
96
                        throws RasterBufferInvalidException{
97
                BufferFactory bufferFactory = new BufferFactory(datasets);
98
                double cellSize[] = calcCellSize(bufferFactory.getDataSource(), bufferFactory.getSourceWidth(),
99
                                                                                        bufferFactory.getSourceHeight());
100
                layerExtent = new GridExtent(        bufferFactory.getDataSource().getExtent(),
101
                                                                                cellSize[0],
102
                                                                                cellSize[1]);
103
                if(bufferFactory.getDataSource() != null && bufferFactory.getDataSource().getDataType() != null)
104
                        dataType = bufferFactory.getDataSource().getDataType()[0];
105
                this.bands = bands;
106

    
107
                if(windowExtent == null)
108
                        this.windowExtent = layerExtent;
109
                else
110
                        this.windowExtent = windowExtent;
111

    
112
                if (this.windowExtent.fitsIn(layerExtent))
113
                        reader = new GridNotInterpolated(bufferFactory, layerExtent, this.windowExtent, bands);
114
                else
115
                        reader = new GridInterpolated(bufferFactory, layerExtent, this.windowExtent, bands);
116
                rasterBuf = (RasterBuffer)bufferFactory.getRasterBuf();
117
                writer = new GridWriter(layerExtent, dataType, rasterBuf);
118

    
119
                init(bufferFactory);
120
        }
121

    
122
        /**
123
         * Carga el buffer de datos desde el reader para poder escribir sobre los datos de
124
         * la ventana original. Esto es ?til cuando el GridWriter se crea asociado a un raster
125
         * pero el buffer est? vacio, por ejemplo en el caso de crear un GridInterpolated. Un
126
         * GridInterpolated no carga ning?n buffer en memoria sino que accede a los datos en disco
127
         * a medida que se le van solicitando. Por ello, no es posible modificarlos sino cargamos
128
         * un buffer con los datos previamente.
129
         */
130
        public void loadWriterData() throws GridException {
131
                rasterBuf = RasterBuffer.getBuffer(dataType, reader.getNX(), reader.getNY(), bands.length, true);
132
                writer = new GridWriter(windowExtent, dataType, rasterBuf);
133
                int x = 0, y = 0;
134
                try{
135
                        switch(rasterBuf.getDataType()) {
136
                        case IBuffer.TYPE_BYTE: for (x = 0; x < getNX(); x++)
137
                                for (y = 0; y < getNY(); y++)
138
                                        writer.setCellValue(x, y, (reader.getCellValueAsByte(x, y)));
139
                                                                        break;
140
                        case IBuffer.TYPE_SHORT:for (x = 0; x < getNX(); x++)
141
                                for (y = 0; y < getNY(); y++)
142
                                        writer.setCellValue(x, y, (reader.getCellValueAsShort(x, y)));
143
                                                                        break;
144
                        case IBuffer.TYPE_INT:         for (x = 0; x < getNX(); x++)
145
                                for (y = 0; y < getNY(); y++)
146
                                        writer.setCellValue(x, y, (reader.getCellValueAsInt(x, y)));
147
                                                                        break;
148
                        case IBuffer.TYPE_FLOAT:for (x = 0; x < getNX(); x++)
149
                                for (y = 0; y < getNY(); y++)
150
                                        writer.setCellValue(x, y, (reader.getCellValueAsFloat(x, y)));
151
                                                                        break;
152
                        case IBuffer.TYPE_DOUBLE:for (x = 0; x < getNX(); x++)
153
                                for (y = 0; y < getNY(); y++)
154
                                        writer.setCellValue(x, y, (reader.getCellValueAsDouble(x, y)));
155
                                                                         break;
156
                        }
157
                } catch (RasterBufferInvalidException e) {
158
                        throw new GridException("Buffer de datos no v?lido " + x + " " + y, e);
159
                } catch (OutOfGridException e1) {
160
                        throw new GridException("Acceso fuera de los l?mites del Grid " + x + " " + y, e1);
161
                } catch (RasterBufferInvalidAccessException e) {
162
                        throw new GridException("Acceso al grid no v?lido " + x + " " + y, e);
163
                }
164
        }
165

    
166
        /**
167
         * Crea un grid vacio a partir de un extent y un tipo de datos. Se crea un buffer vacio en el
168
         * que puede escribirse a trav?s del writer. Los datos escritos pueden ser consultados con el
169
         * reader. No tiene una fuente de datos asociada ya que es un grid vacio basicamente para
170
         * escritura.
171
         * @param layerExtent Tama?o completo de la capa
172
         * @param windowExtent Ventana de datos.
173
         * @param dataType Tipo de datos del buffer
174
         * @param bands n?mero de bandas requeridas y orden de dibujado en el buffer
175
         */
176
        public Grid(GridExtent layerExtent,
177
                                GridExtent windowExtent,
178
                                int dataType,
179
                                int[] bands) throws RasterBufferInvalidException{
180
                this.windowExtent = windowExtent;
181
                this.layerExtent = layerExtent;
182
                this.dataType = dataType;
183

    
184
                rasterBuf = RasterBuffer.getBuffer(dataType, layerExtent.getNX(), layerExtent.getNY(), bands.length, true);
185

    
186
                if (windowExtent.fitsIn(layerExtent))
187
                        reader = new GridNotInterpolated(rasterBuf, layerExtent, windowExtent, bands);
188
                else
189
                        reader = new GridInterpolated(rasterBuf, layerExtent, windowExtent, bands);
190
                writer = new GridWriter(layerExtent, dataType, rasterBuf);
191
                init(null);
192
        }
193

    
194
        /**
195
         * Crea un grid a partir de un BufferFactory. El buffer debe estar cargado de datos y el extent
196
         * de este viene definido en el par?metro windowExtent.
197
         * @param bufferFactory Fuente de datos
198
         * @param windowExtent        Extent de los datos cargados en bufferFactory
199
         * @param notInterp Si es true fuerza a que el reader sea sin interpolaci?n. Si es false decide si
200
         * el reader es interpolado o no a partir de los extents de capa y la ventana seleccionada.
201
         */
202
        public Grid(BufferFactory bufferFactory, boolean notInterp) {
203
                double cellSize[] = calcCellSize(bufferFactory.getDataSource(), bufferFactory.getSourceWidth(),
204
                                bufferFactory.getSourceHeight());
205
                this.layerExtent = new GridExtent(        bufferFactory.getDataSource().getExtent(),
206
                                                                                        cellSize[0],
207
                                                                                        cellSize[1]);
208
                if(bufferFactory.getDataSource() != null && bufferFactory.getDataSource().getDataType() != null)
209
                        dataType = bufferFactory.getDataSource().getDataType()[0];
210
                bands = bufferFactory.getDrawableBands();
211

    
212
                this.windowExtent = new GridExtent(        bufferFactory.getDataExtent(),
213
                                                                                        cellSize[0],
214
                                                                                        cellSize[1]);
215

    
216
                rasterBuf = (RasterBuffer)bufferFactory.getRasterBuf();
217

    
218
                if(notInterp)
219
                        reader = new GridNotInterpolated(rasterBuf, layerExtent, windowExtent, bands);
220
                else if (windowExtent.fitsIn(layerExtent))
221
                        reader = new GridNotInterpolated(rasterBuf, layerExtent, windowExtent, bands);
222
                else
223
                        reader = new GridInterpolated(rasterBuf, layerExtent, windowExtent, bands);
224
                writer = new GridWriter(layerExtent, dataType, rasterBuf);
225
                init(bufferFactory);
226
        }
227

    
228
        /**
229
         * Crea un grid a partir de un BufferFactory. Se har? una petici?n del extent completo de la fuente.
230
         * bufferFactory tiene asociada una fuente de datos pero se ignorar? si tiene el buffer lleno. La instanciaci?n
231
         * de GridNotInterpolated har? que se haga la petici?n para cargar raster completo.
232
         * @param bufferFactory Fuente de datos
233
         * @param bands n?mero de bandas requeridas y orden de dibujado en el buffer
234
         */
235
        public Grid(BufferFactory bufferFactory, int[] bands)
236
                        throws RasterBufferInvalidException{
237
                //La petici?n es del raster completo
238
                double cellSize[] = calcCellSize(bufferFactory.getDataSource(), bufferFactory.getSourceWidth(),
239
                                bufferFactory.getSourceHeight());
240
                windowExtent = layerExtent = new GridExtent(bufferFactory.getDataSource().getExtent(),
241
                                                                                                        cellSize[0],
242
                                                                                                        cellSize[1]);
243
                if(bufferFactory.getDataSource() != null && bufferFactory.getDataSource().getDataType() != null)
244
                        dataType = bufferFactory.getDataSource().getDataType()[0];
245
                this.bands = bands;
246

    
247
                reader = new GridNotInterpolated(bufferFactory, layerExtent, windowExtent, bands);
248
                rasterBuf = (RasterBuffer)bufferFactory.getRasterBuf();
249
                writer = new GridWriter(windowExtent, dataType, rasterBuf);
250

    
251
                init(bufferFactory);
252
        }
253

    
254
        /**
255
         * Crea un grid a partir de un BufferFactory. Se har? una petici?n del extent completo de la fuente.
256
         * bufferFactory tiene asociada una fuente de datos pero se ignorar? si tiene el buffer lleno. La instanciaci?n
257
         * de GridNotInterpolated har? que se haga la petici?n para cargar raster completo. El buffer se cargar?
258
         * con todas las bandas disponibles.
259
         * @param bufferFactory Fuente de datos
260
         */
261
        public Grid(BufferFactory bufferFactory) throws RasterBufferInvalidException {
262
                //La petici?n es del raster completo
263
                double cellSize[] = calcCellSize(bufferFactory.getDataSource(), bufferFactory.getSourceWidth(),
264
                                bufferFactory.getSourceHeight());
265
                windowExtent = layerExtent = new GridExtent(bufferFactory.getDataSource().getExtent(),
266
                                                                                                        cellSize[0],
267
                                                                                                        cellSize[1]);
268
                if(bufferFactory.getDataSource() != null && bufferFactory.getDataSource().getDataType() != null)
269
                        dataType = bufferFactory.getDataSource().getDataType()[0];
270

    
271
                bands = new int[bufferFactory.getBandCount()];
272
                for(int i = 0; i < bufferFactory.getBandCount(); i ++)
273
                        bands[i] = i;
274
                reader = new GridNotInterpolated(bufferFactory, layerExtent, windowExtent, bands);
275
                rasterBuf = (RasterBuffer)bufferFactory.getRasterBuf();
276
                writer = new GridWriter(windowExtent, dataType, rasterBuf);
277

    
278
                init(bufferFactory);
279
        }
280

    
281
        /**
282
         * Calcula el tama?o de celda a partir de un dataset, un ancho y un alto en pixeles
283
         * @return
284
         */
285
        private double[] calcCellSize(IRasterDataSource mDataset, double w, double h) {
286
                double dCellsize[] = new double[2];
287
                try {
288
                        Extent e = mDataset.getExtent();
289
                        dCellsize[0] = (e.getLRX() - e.getULX()) / w;
290
                        dCellsize[1] = (e.getLRY() - e.getULY()) / h;
291

    
292
                        return dCellsize;
293
                } catch (NullPointerException e) {
294
                        dCellsize[0] = 1;
295
                        dCellsize[1] = 1;
296
                        return dCellsize;
297
                }
298
        }
299

    
300
        /**
301
         * Selecciona la forma de obtener datos con o sin interpolaci?n. Si pasamos el par?metro
302
         * false al aplicar un m?todo de consulta de datos este ser? aplicado sin interpolaci?n.
303
         * @param interpolation
304
         */
305
        public void switchToInterpolationMethod(boolean interpolation){
306
                //GridExtent layer = new GridExtent(0, 0, rasterBuf.getWidth(), rasterBuf.getHeight(), 1);
307
                if(interpolation)
308
                        reader = new GridInterpolated(rasterBuf, layerExtent, windowExtent, bands);
309
                else
310
                        reader = new GridNotInterpolated(rasterBuf, layerExtent, windowExtent, bands);
311
                init(null);
312
        }
313

    
314
        /**
315
         * Inicializaci?n de constantes
316
         */
317
        private void init(BufferFactory ds) {
318
                int i;
319
                GridTransparency transparency = null;
320

    
321
                double dCellSize = getCellSize();
322

    
323
                statistic = new GridStatistic(this);
324
                if (ds == null)
325
                        transparency = new GridTransparency();
326
                else {
327
                        Transparency transp = ds.getDataSource().getTransparencyFilesStatus();
328
                        if (transp != null)
329
                                transparency = new GridTransparency(transp);
330
                        this.palette = new GridPalette[ds.getColorTables().length];
331
                        for (int iPal = 0; iPal < ds.getColorTables().length; iPal++)
332
                                if (ds.getColorTables()[iPal] != null)
333
                                        this.palette[iPal] = new GridPalette(ds.getColorTables()[iPal]);
334
                }
335
                filterList = new RasterFilterList();
336
                if (ds != null) {
337
                        filterList.addEnvParam("IStatistics", ds.getDataSource().getStatistics());
338
                        filterList.addEnvParam("MultiRasterDataset", ds.getDataSource());
339
                }
340
                filterList.addEnvParam("Transparency", transparency);
341
                if(rasterBuf != null)
342
                        filterList.setInitRasterBuf(rasterBuf);
343

    
344
                m_dDist = new double[8];
345

    
346
                for (i = 0; i < 8; i++)
347
                        m_dDist[i] = Math.sqrt ( m_iOffsetX[i] * dCellSize * m_iOffsetX[i] * dCellSize
348
                                                                        + m_iOffsetY[i] * dCellSize * m_iOffsetY[i] * dCellSize );
349

    
350
                _2DX = dCellSize * 2.0;
351
                _6DX = dCellSize * 6.0;
352
                _DX_2 = dCellSize * dCellSize;
353
                _4DX_2 = 4.0 * _DX_2;
354
        }
355

    
356
        //************* Write Services *********************
357

    
358
        /*
359
         *  (non-Javadoc)
360
         * @see org.gvsig.fmap.grid.IWritableGrid#assign(byte)
361
         */
362
        public void assign(byte value)throws GridException {
363
                try {
364
                        writer.assign(value);
365
                } catch (RasterBufferInvalidAccessException e) {
366
                        throw new GridException("Error wrinting buffer");
367
                }
368
        }
369

    
370
        /*
371
         *  (non-Javadoc)
372
         * @see org.gvsig.fmap.grid.IWritableGrid#assign(short)
373
         */
374
        public void assign(short value)throws GridException {
375
                try {
376
                        writer.assign(value);
377
                } catch (RasterBufferInvalidAccessException e) {
378
                        throw new GridException("Error wrinting buffer");
379
                }
380
        }
381

    
382
        /*
383
         *  (non-Javadoc)
384
         * @see org.gvsig.fmap.grid.IWritableGrid#assign(int)
385
         */
386
        public void assign(int value)throws GridException {
387
                try {
388
                        writer.assign(value);
389
                } catch (RasterBufferInvalidAccessException e) {
390
                        throw new GridException("Error wrinting buffer");
391
                }
392
        }
393

    
394
        /*
395
         *  (non-Javadoc)
396
         * @see org.gvsig.fmap.grid.IWritableGrid#assign(float)
397
         */
398
        public void assign(float value)throws GridException {
399
                try {
400
                        writer.assign(value);
401
                } catch (RasterBufferInvalidAccessException e) {
402
                        throw new GridException("Error wrinting buffer");
403
                }
404
        }
405

    
406
        /*
407
         *  (non-Javadoc)
408
         * @see org.gvsig.fmap.grid.IWritableGrid#assign(double)
409
         */
410
        public void assign(double value)throws GridException {
411
                try {
412
                        writer.assign(value);
413
                } catch (RasterBufferInvalidAccessException e) {
414
                        throw new GridException("Error wrinting buffer");
415
                }
416
        }
417

    
418
        /*
419
         *  (non-Javadoc)
420
         * @see org.gvsig.fmap.grid.IWritableGrid#assign(org.gvsig.fmap.dataaccess.BufferFactory)
421
         */
422
        public void assign(BufferFactory bufferFactory) throws GridException {
423
                Grid window;
424
                try {
425
                        window = new Grid(bufferFactory.getDataSource(), bands, windowExtent);
426
                        write(window);
427
                        writer.setNoDataValue(window.getNoDataValue());
428
                } catch (RasterBufferInvalidException e) {
429
                        throw new GridException("Error writing buffer");
430
                }
431
        }
432

    
433
        /*
434
         *  (non-Javadoc)
435
         * @see org.gvsig.fmap.grid.IWritableGrid#assign(org.gvsig.fmap.grid.Grid)
436
         */
437
        public void assign(Grid driver) throws GridException {
438
                if (driver.getGridExtent().equals(layerExtent)){
439
                        write(driver);
440
                        writer.setNoDataValue(driver.getNoDataValue());
441
                }
442
        }
443

    
444
        /**
445
         * Sobreescribe los datos del grid actual con los datos del grid pasado por par?metro
446
         * @param g Grid desde donde se obtienen los datos
447
         */
448
        private void write(Grid g) throws GridException {
449
                try{
450
                        switch(rasterBuf.getDataType()){
451
                        case IBuffer.TYPE_BYTE: for (int x = 0; x < g.getNX(); x++)
452
                                for (int y = 0; y < g.getNY(); y++)
453
                                        writer.setCellValue(x, y, g.getCellValueAsByte(x, y));
454
                                                                        break;
455
                        case IBuffer.TYPE_SHORT:for (int x = 0; x < g.getNX(); x++)
456
                                for (int y = 0; y < g.getNY(); y++)
457
                                        writer.setCellValue(x, y, g.getCellValueAsShort(x, y));
458
                                                                        break;
459
                        case IBuffer.TYPE_INT:         for (int x = 0; x < g.getNX(); x++)
460
                                for (int y = 0; y < g.getNY(); y++)
461
                                        writer.setCellValue(x, y, g.getCellValueAsInt(x, y));
462
                                                                        break;
463
                        case IBuffer.TYPE_FLOAT:for (int x = 0; x < g.getNX(); x++)
464
                                for (int y = 0; y < g.getNY(); y++)
465
                                        writer.setCellValue(x, y, g.getCellValueAsFloat(x, y));
466
                                                                        break;
467
                        case IBuffer.TYPE_DOUBLE:for (int x = 0; x < g.getNX(); x++)
468
                                for (int y = 0; y < g.getNY(); y++)
469
                                        writer.setCellValue(x, y, g.getCellValueAsDouble(x, y));
470
                                                                        break;
471
                        }
472
                } catch (OutOfGridException e) {
473
                        throw new GridException("");
474
                }
475
        }
476

    
477
        /*
478
         *  (non-Javadoc)
479
         * @see org.gvsig.fmap.grid.IWritableGrid#assignNoData()
480
         */
481
        public void assignNoData(){
482
                try {
483
                        switch(rasterBuf.getDataType()){
484
                        case IBuffer.TYPE_BYTE: writer.assign((byte)rasterBuf.getNoDataValue());break;
485
                        case IBuffer.TYPE_SHORT: writer.assign((short)rasterBuf.getNoDataValue());break;
486
                        case IBuffer.TYPE_INT: writer.assign((int)rasterBuf.getNoDataValue());break;
487
                        case IBuffer.TYPE_FLOAT: writer.assign((float)rasterBuf.getNoDataValue());break;
488
                        case IBuffer.TYPE_DOUBLE: writer.assign(rasterBuf.getNoDataValue());break;
489
                        }
490
                } catch (RasterBufferInvalidAccessException e) {
491
                        //No hacemos nada. El tipo de dato al que se accede no es controlado por el usuario por lo
492
                        //que no le mandamos la excepci?n hacia arriba.
493
                        e.printStackTrace();
494
                }
495
        }
496

    
497
        /*
498
         *  (non-Javadoc)
499
         * @see org.gvsig.fmap.grid.IWritableGrid#setCellValue(int, int, byte)
500
         */
501
        public void setCellValue(int x, int y, byte value)throws OutOfGridException{
502
                writer.setCellValue(x, y, value);
503
        }
504

    
505
        /*
506
         *  (non-Javadoc)
507
         * @see org.gvsig.fmap.grid.IWritableGrid#setCellValue(int, int, short)
508
         */
509
        public void setCellValue(int x, int y, short value)throws OutOfGridException{
510
                writer.setCellValue(x, y, value);
511
        }
512

    
513
        /*
514
         *  (non-Javadoc)
515
         * @see org.gvsig.fmap.grid.IWritableGrid#setCellValue(int, int, int)
516
         */
517
        public void setCellValue(int x, int y, int value)throws OutOfGridException{
518
                writer.setCellValue(x, y, value);
519
        }
520

    
521
        /*
522
         *  (non-Javadoc)
523
         * @see org.gvsig.fmap.grid.IWritableGrid#setCellValue(int, int, float)
524
         */
525
        public void setCellValue(int x, int y, float value)throws OutOfGridException{
526
                writer.setCellValue(x, y, value);
527
        }
528

    
529
        /*
530
         *  (non-Javadoc)
531
         * @see org.gvsig.fmap.grid.IWritableGrid#setCellValue(int, int, double)
532
         */
533
        public void setCellValue(int x, int y, double value)throws OutOfGridException{
534
                writer.setCellValue(x, y, value);
535
        }
536

    
537
        /*
538
         *  (non-Javadoc)
539
         * @see org.gvsig.fmap.grid.IWritableGrid#add(org.gvsig.fmap.grid.Grid)
540
         */
541
        public void add(Grid g) throws GridException {
542
                if (g.getGridExtent().equals(getGridExtent())) {
543
                        boolean interp = (reader instanceof GridInterpolated);
544
                        switchToInterpolationMethod(false);
545
                        try{
546
                                switch(rasterBuf.getDataType()){
547
                                case IBuffer.TYPE_BYTE: for (int x = 0; x < g.getNX(); x++)
548
                                        for (int y = 0; y < g.getNY(); y++)
549
                                                writer.setCellValue(x, y, reader.getCellValueAsByte(x, y) + g.getCellValueAsByte(x,y));
550
                                                                                break;
551
                                case IBuffer.TYPE_SHORT:for (int x = 0; x < g.getNX(); x++)
552
                                        for (int y = 0; y < g.getNY(); y++)
553
                                                writer.setCellValue(x, y, reader.getCellValueAsShort(x, y) + g.getCellValueAsShort(x,y));
554
                                                                                break;
555
                                case IBuffer.TYPE_INT:         for (int x = 0; x < g.getNX(); x++)
556
                                        for (int y = 0; y < g.getNY(); y++)
557
                                                writer.setCellValue(x, y, reader.getCellValueAsInt(x, y) + g.getCellValueAsInt(x,y));
558
                                                                                break;
559
                                case IBuffer.TYPE_FLOAT:for (int x = 0; x < g.getNX(); x++)
560
                                        for (int y = 0; y < g.getNY(); y++)
561
                                                writer.setCellValue(x, y, reader.getCellValueAsFloat(x, y) + g.getCellValueAsFloat(x,y));
562
                                                                                break;
563
                                case IBuffer.TYPE_DOUBLE:for (int x = 0; x < g.getNX(); x++)
564
                                        for (int y = 0; y < g.getNY(); y++)
565
                                                writer.setCellValue(x, y, reader.getCellValueAsDouble(x, y) + g.getCellValueAsDouble(x,y));
566
                                                                                break;
567
                                }
568
                        } catch (OutOfGridException e) {
569
                                throw new GridException("");
570
                        } catch (RasterBufferInvalidAccessException e1) {
571
                                throw new GridException("");
572
                        } catch (RasterBufferInvalidException e) {
573
                                throw new GridException("");
574
                        }
575
                        switchToInterpolationMethod(interp);
576
                }
577
        }
578

    
579
        /*
580
         *  (non-Javadoc)
581
         * @see org.gvsig.fmap.grid.IWritableGrid#addToCellValue(int, int, byte)
582
         */
583
        public void addToCellValue(int x, int y, byte value)
584
                throws GridException {
585
                try {
586
                        writer.setCellValue(x, y, (byte)(reader.getCellValueAsByte(x, y) + value));
587
                } catch (OutOfGridException e) {
588
                        throw new GridException("");
589
                } catch (RasterBufferInvalidAccessException e) {
590
                        throw new GridException("");
591
                } catch (RasterBufferInvalidException e) {
592
                        throw new GridException("");
593
                }
594
        }
595

    
596
        /*
597
         *  (non-Javadoc)
598
         * @see org.gvsig.fmap.grid.IWritableGrid#addToCellValue(int, int, short)
599
         */
600
        public void addToCellValue(int x, int y, short value)
601
                throws GridException {
602
                try {
603
                        writer.setCellValue(x, y, (short)(reader.getCellValueAsShort(x, y) + value));
604
                } catch (OutOfGridException e) {
605
                        throw new GridException("");
606
                } catch (RasterBufferInvalidAccessException e) {
607
                        throw new GridException("");
608
                } catch (RasterBufferInvalidException e) {
609
                        throw new GridException("");
610
                }
611
        }
612

    
613
        /*
614
         *  (non-Javadoc)
615
         * @see org.gvsig.fmap.grid.IWritableGrid#addToCellValue(int, int, int)
616
         */
617
        public void addToCellValue(int x, int y, int value)
618
                throws GridException {
619
                try {
620
                        writer.setCellValue(x, y, (reader.getCellValueAsInt(x, y) + value));
621
                } catch (OutOfGridException e) {
622
                        throw new GridException("");
623
                } catch (RasterBufferInvalidAccessException e) {
624
                        throw new GridException("");
625
                } catch (RasterBufferInvalidException e) {
626
                        throw new GridException("");
627
                }
628
        }
629

    
630
        /*
631
         *  (non-Javadoc)
632
         * @see org.gvsig.fmap.grid.IWritableGrid#addToCellValue(int, int, float)
633
         */
634
        public void addToCellValue(int x, int y, float value)
635
                throws GridException {
636
                try {
637
                        writer.setCellValue(x, y, (reader.getCellValueAsFloat(x, y) + value));
638
                } catch (OutOfGridException e) {
639
                        throw new GridException("");
640
                } catch (RasterBufferInvalidAccessException e) {
641
                        throw new GridException("");
642
                } catch (RasterBufferInvalidException e) {
643
                        throw new GridException("");
644
                }
645
        }
646

    
647
        /*
648
         *  (non-Javadoc)
649
         * @see org.gvsig.fmap.grid.IWritableGrid#addToCellValue(int, int, double)
650
         */
651
        public void addToCellValue(int x, int y, double value)
652
                throws GridException {
653
                try {
654
                        writer.setCellValue(x, y, (reader.getCellValueAsDouble(x, y) + value));
655
                } catch (OutOfGridException e) {
656
                        throw new GridException("");
657
                } catch (RasterBufferInvalidAccessException e) {
658
                        throw new GridException("");
659
                } catch (RasterBufferInvalidException e) {
660
                        throw new GridException("");
661
                }
662
        }
663

    
664
        /*
665
         *  (non-Javadoc)
666
         * @see org.gvsig.fmap.grid.IWritableGrid#multiply(double)
667
         */
668
        public void multiply(double value) throws GridException {
669
                boolean interp = (reader instanceof GridInterpolated);
670
                switchToInterpolationMethod(false);
671
                try{
672
                        switch(rasterBuf.getDataType()){
673
                        case IBuffer.TYPE_BYTE: for (int x = 0; x < getNX(); x++)
674
                                for (int y = 0; y < getNY(); y++)
675
                                        writer.setCellValue(x, y, (byte)(reader.getCellValueAsByte(x, y) * value));
676
                                                                        break;
677
                        case IBuffer.TYPE_SHORT:for (int x = 0; x < getNX(); x++)
678
                                for (int y = 0; y < getNY(); y++)
679
                                        writer.setCellValue(x, y, (short)(reader.getCellValueAsShort(x, y) * value));
680
                                                                        break;
681
                        case IBuffer.TYPE_INT:         for (int x = 0; x < getNX(); x++)
682
                                for (int y = 0; y < getNY(); y++)
683
                                        writer.setCellValue(x, y, (int)(reader.getCellValueAsInt(x, y) * value));
684
                                                                        break;
685
                        case IBuffer.TYPE_FLOAT:for (int x = 0; x < getNX(); x++)
686
                                for (int y = 0; y < getNY(); y++)
687
                                        writer.setCellValue(x, y, (float)(reader.getCellValueAsFloat(x, y) * value));
688
                                                                        break;
689
                        case IBuffer.TYPE_DOUBLE:for (int x = 0; x < getNX(); x++)
690
                                for (int y = 0; y < getNY(); y++)
691
                                        writer.setCellValue(x, y, (reader.getCellValueAsDouble(x, y) * value));
692
                                                                        break;
693
                        }
694
                } catch (RasterBufferInvalidException e) {
695
                        throw new GridException("Buffer de datos no v?lido", e);
696
                } catch (OutOfGridException e1) {
697
                        throw new GridException("Acceso fuera de los l?mites del Grid", e1);
698
                } catch (RasterBufferInvalidAccessException e) {
699
                        throw new GridException("Acceso al grid no v?lido", e);
700
                }
701
                //Restauramos el reader que hab?a
702
                switchToInterpolationMethod(interp);
703
        }
704

    
705
        /*
706
         *  (non-Javadoc)
707
         * @see org.gvsig.fmap.grid.IWritableGrid#setNoDataValue(double)
708
         */
709
        public void setNoDataValue(double dNoDataValue){
710
                writer.setNoDataValue((float) dNoDataValue);
711
        }
712

    
713
        /*
714
         *  (non-Javadoc)
715
         * @see org.gvsig.fmap.grid.IWritableGrid#setNoData(int, int)
716
         */
717
        public void setNoData(int x, int y){
718
                writer.setNoData(x,y);
719
        }
720

    
721
        public void ExportToArcViewASCIIFile(String sFilename){
722
                try {
723
                        writer.ExportToArcViewASCIIFile(sFilename);
724
                } catch (NumberFormatException e) {
725
                        e.printStackTrace();
726
                } catch (IOException e) {
727
                        e.printStackTrace();
728
                }
729
        }
730

    
731
        //************* Query Services *********************
732

    
733
        /*
734
         *  (non-Javadoc)
735
         * @see org.gvsig.fmap.grid.IQueryableGrid#isNoDataValue(double)
736
         */
737
        public boolean isNoDataValue(double noDataValue){
738
                return (reader.getNoDataValue() == noDataValue);
739
        }
740

    
741
        /*
742
         *  (non-Javadoc)
743
         * @see org.gvsig.fmap.grid.IQueryableGrid#isInGrid(int, int)
744
         */
745
        public boolean isInGrid(int x, int y){
746
                return reader.isCellInGrid(x, y);
747
        }
748

    
749
        /*
750
         *  (non-Javadoc)
751
         * @see org.gvsig.fmap.grid.IQueryableGrid#getCellSize()
752
         */
753
        public double getCellSize() {
754
                return reader.getCellSize();
755
        }
756

    
757
        /*
758
         *  (non-Javadoc)
759
         * @see org.gvsig.fmap.grid.IQueryableGrid#getCellValueAsByte(int, int)
760
         */
761
        public byte getCellValueAsByte(int x, int y)throws GridException {
762
                try {
763
                        return  reader.getCellValueAsByte(x, y);
764
                } catch (RasterBufferInvalidAccessException e) {
765
                        throw new GridException("Position: (" + x + "," + y + ") not valid");
766
                } catch (RasterBufferInvalidException e) {
767
                        throw new GridException("Buffer not valid");
768
                }
769
        }
770

    
771
        /*
772
         *  (non-Javadoc)
773
         * @see org.gvsig.fmap.grid.IQueryableGrid#getCellValueAsShort(int, int)
774
         */
775
        public short getCellValueAsShort(int x, int y)throws GridException {
776
                try {
777
                        return reader.getCellValueAsShort(x, y);
778
                } catch (RasterBufferInvalidAccessException e) {
779
                        throw new GridException("Position: (" + x + "," + y + ") not valid");
780
                } catch (RasterBufferInvalidException e) {
781
                        throw new GridException("Buffer not valid");
782
                }
783
        }
784

    
785
        /*
786
         *  (non-Javadoc)
787
         * @see org.gvsig.fmap.grid.IQueryableGrid#getCellValueAsInt(int, int)
788
         */
789
        public int getCellValueAsInt(int x, int y)throws GridException {
790
                try {
791
                        return reader.getCellValueAsInt(x, y);
792
                } catch (RasterBufferInvalidAccessException e) {
793
                        throw new GridException("Position: (" + x + "," + y + ") not valid");
794
                } catch (RasterBufferInvalidException e) {
795
                        throw new GridException("Buffer not valid");
796
                }
797
        }
798

    
799
        /*
800
         *  (non-Javadoc)
801
         * @see org.gvsig.fmap.grid.IQueryableGrid#getCellValueAsFloat(int, int)
802
         */
803
        public float getCellValueAsFloat(int x, int y)throws GridException {
804
                try {
805
                        return reader.getCellValueAsFloat(x, y);
806
                } catch (RasterBufferInvalidAccessException e) {
807
                        throw new GridException("Position: (" + x + "," + y + ") not valid");
808
                } catch (RasterBufferInvalidException e) {
809
                        throw new GridException("Buffer not valid");
810
                }
811
        }
812

    
813
        /*
814
         *  (non-Javadoc)
815
         * @see org.gvsig.fmap.grid.IQueryableGrid#getCellValueAsDouble(int, int)
816
         */
817
        public double getCellValueAsDouble(int x, int y)throws GridException {
818
                try {
819
                        return reader.getCellValueAsDouble(x, y);
820
                } catch (RasterBufferInvalidAccessException e) {
821
                        throw new GridException("Position: (" + x + "," + y + ") not valid");
822
                } catch (RasterBufferInvalidException e) {
823
                        throw new GridException("Buffer not valid");
824
                }
825
        }
826

    
827
        /*
828
         *  (non-Javadoc)
829
         * @see org.gvsig.fmap.grid.IQueryableGrid#getCellValueAsDouble(int, int)
830
         */
831
        public double getCellValue(int x, int y)throws GridException {
832
                try {
833
                        return reader.getCellValue(x, y);
834
                } catch (RasterBufferInvalidAccessException e) {
835
                        throw new GridException("Position: (" + x + "," + y + ") not valid");
836
                } catch (RasterBufferInvalidException e) {
837
                        throw new GridException("Buffer not valid");
838
                }
839
        }
840

    
841
        /*
842
         *  (non-Javadoc)
843
         * @see org.gvsig.fmap.grid.IQueryableGrid#getNoDataValue()
844
         */
845
        public double getNoDataValue() {
846
                return rasterBuf.getNoDataValue();
847
        }
848

    
849
        /*
850
         *  (non-Javadoc)
851
         * @see org.gvsig.fmap.grid.IQueryableGrid#getMinValue()
852
         */
853
        public double getMinValue() throws GridException {
854
                if (!statistic.isStatisticsCalculated())
855
                        statistic.calculateStatistics();
856
                return statistic.getMin();
857
        }
858

    
859
        /*
860
         *  (non-Javadoc)
861
         * @see org.gvsig.fmap.grid.IQueryableGrid#getMaxValue()
862
         */
863
        public double getMaxValue() throws GridException {
864
                if (!statistic.isStatisticsCalculated())
865
                        statistic.calculateStatistics();
866
                return statistic.getMax();
867
        }
868

    
869
        /*
870
         *  (non-Javadoc)
871
         * @see org.gvsig.fmap.grid.IQueryableGrid#getMeanValue()
872
         */
873
        public double getMeanValue() throws GridException {
874
                if (!statistic.isStatisticsCalculated())
875
                        statistic.calculateStatistics();
876
                return statistic.getMean();
877
        }
878

    
879
        /*
880
         *  (non-Javadoc)
881
         * @see org.gvsig.fmap.grid.IQueryableGrid#getVariance()
882
         */
883
        public double getVariance() throws GridException {
884
                if (!statistic.isStatisticsCalculated())
885
                        statistic.calculateStatistics();
886
                return statistic.getVariance();
887
        }
888

    
889
        /*
890
         *  (non-Javadoc)
891
         * @see org.gvsig.fmap.grid.IQueryableGrid#setInterpolationMethod(int)
892
         */
893
        public void setInterpolationMethod(int iMethod){
894
                if (reader instanceof GridInterpolated)
895
                        ((GridInterpolated) reader).setInterpolationMethod(iMethod);
896
                else{
897
                        this.switchToInterpolationMethod(true);
898
                        ((GridInterpolated) reader).setInterpolationMethod(iMethod);
899
                }
900
        }
901

    
902
        /*
903
         *  (non-Javadoc)
904
         * @see org.gvsig.fmap.grid.IQueryableGrid#getNX()
905
         */
906
        public int getNX(){
907
                return reader.getNX();
908
        }
909

    
910
        /*
911
         *  (non-Javadoc)
912
         * @see org.gvsig.fmap.grid.IQueryableGrid#getNY()
913
         */
914
        public int getNY(){
915
                return reader.getNY();
916
        }
917

    
918
        /*
919
         *  (non-Javadoc)
920
         * @see org.gvsig.fmap.grid.IQueryableGrid#getLayerNX()
921
         */
922
        public int getLayerNX(){
923
                return layerExtent.getNX();
924
        }
925

    
926
        /*
927
         *  (non-Javadoc)
928
         * @see org.gvsig.fmap.grid.IQueryableGrid#getLayerNY()
929
         */
930
        public int getLayerNY(){
931
                return layerExtent.getNY();
932
        }
933

    
934
        public int getDataType(){
935
                return dataType;
936
        }
937

    
938
        private boolean getSubMatrix3x3(int x, int y, double subMatrix[])
939
                throws GridException {
940
                int        i;
941
                int iDir;
942
                double        z, z2;
943

    
944
                z = getCellValueAsDouble(x, y);
945

    
946
                if(isNoDataValue(z))
947
                        return false;
948
                else {
949
                        //SubMatrix[4]        = 0.0;
950
                        for(i = 0; i < 4; i++) {
951
                                iDir = 2 * i;
952
                                z2 = getCellValueAsDouble(x + m_iOffsetX[iDir], y + m_iOffsetY[iDir]);
953
                                if( !isNoDataValue(z2))
954
                                        subMatrix[i] =  z2 - z;
955
                                else {
956
                                        z2 = getCellValueAsDouble(x + m_iOffsetX[(iDir + 4) % 8], y + m_iOffsetY[(iDir  + 4) % 8]);
957
                                        if( !isNoDataValue(z2))
958
                                                subMatrix[i] = z - z2;
959
                                        else
960
                                                subMatrix[i] = 0.0;
961
                                }
962
                        }
963

    
964
                        return true;
965
                }
966
        }
967

    
968
        /*
969
         * (non-Javadoc)
970
         * @see org.gvsig.raster.grid.IQueryableGrid#getSlope(int, int)
971
         */
972
        public double getSlope(int x, int y)throws GridException {
973
                double        zm[], G, H;
974

    
975
                zm = new double[4];
976

    
977
                try {
978
                        if( getSubMatrix3x3(x, y, zm) ) {
979
                                G = (zm[0] - zm[2]) / _2DX;
980
                                H = (zm[1] - zm[3]) / _2DX;
981
                                return Math.atan(Math.sqrt(G * G + H * H));
982
                        } else
983
                                return rasterBuf.getNoDataValue();
984
                } catch (GridException e) {
985
                        throw new GridException("Problems accesing 3x3 submatrix");
986
                }
987
        }
988

    
989
        /*
990
         * (non-Javadoc)
991
         * @see org.gvsig.raster.grid.IQueryableGrid#getAspect(int, int)
992
         */
993
        public double getAspect(int x, int y)throws GridException {
994
                double        zm[], G, H, dAspect;
995
                zm = new double[4];
996

    
997
                try {
998
                        if( getSubMatrix3x3(x, y, zm) ) {
999
                                G = (zm[0] - zm[2]) / _2DX;
1000
                                H = (zm[1] - zm[3]) / _2DX;
1001
                                if(G != 0.0)
1002
                                        dAspect = DEG_180_IN_RAD + Math.atan2(H, G);
1003
                                else
1004
                                        dAspect = H > 0.0 ? DEG_270_IN_RAD : (H < 0.0 ? DEG_90_IN_RAD : -1.0);
1005
                                return dAspect;
1006
                        }
1007
                        else
1008
                                return rasterBuf.getNoDataValue();
1009
                } catch (GridException e) {
1010
                        throw new GridException("Problems accesing 3x3 submatrix");
1011
                }
1012
        }
1013

    
1014
        public static int getXOffsetInDir(int iDir){
1015
                return m_iOffsetX[iDir];
1016
        }
1017

    
1018
        public static int getYOffsetInDir(int iDir){
1019
                return m_iOffsetY[iDir];
1020
        }
1021

    
1022
        public double getDistToNeighborInDir(int iDir){
1023
                return m_dDist[iDir];
1024
        }
1025

    
1026
        public static double getUnitDistToNeighborInDir(int iDir){
1027
                return( (iDir % 2 != 0) ? Math.sqrt(2.0)  : 1.0 );
1028
        }
1029

    
1030
        /*
1031
         *  (non-Javadoc)
1032
         * @see org.gvsig.fmap.grid.IQueryableGrid#getDirToNextDownslopeCell(int, int)
1033
         */
1034
        public int getDirToNextDownslopeCell(int x, int y)throws GridException{
1035
                return getDirToNextDownslopeCell(x, y, true);
1036
        }
1037

    
1038
        /*
1039
         *  (non-Javadoc)
1040
         * @see org.gvsig.fmap.grid.IQueryableGrid#getDirToNextDownslopeCell(int, int, boolean)
1041
         */
1042
        public int getDirToNextDownslopeCell(int x, int y, boolean bForceDirToNoDataCell)
1043
                throws GridException{
1044

    
1045
                int                i, iDir;
1046
                double        z, z2, dSlope, dMaxSlope;
1047

    
1048
                z = getCellValueAsDouble(x, y);
1049

    
1050
                if(isNoDataValue(z))
1051
                        return -1;
1052

    
1053
                dMaxSlope = 0.0;
1054
                for(iDir=-1, i=0; i<8; i++) {
1055
                        z2 = getCellValueAsDouble(x + m_iOffsetX[i], y + m_iOffsetY[i]);
1056
                        if(isNoDataValue(z2)) {
1057
                                if (bForceDirToNoDataCell)
1058
                                        return i;
1059
                                else
1060
                                        return -1;
1061
                        }
1062
                        else {
1063
                                dSlope        = (z - z2) / getDistToNeighborInDir(i);
1064
                                if( dSlope > dMaxSlope ) {
1065
                                        iDir = i;
1066
                                        dMaxSlope = dSlope;
1067
                                }
1068
                        }
1069
                }
1070
                return iDir;
1071
        }
1072

    
1073
        public GridCell[] getSortedArrayOfCells()throws GridException{
1074
                int i;
1075
                int iX,iY;
1076
                int iNX =  getNX();
1077
                int iCells = getNX() * getNY();
1078
                GridCell [] cells = null;
1079
                GridCell cell = null;
1080

    
1081
                cells = new GridCell[iCells];
1082

    
1083
                for (i = 0; i < iCells; i++) {
1084
                        iX = i % iNX;
1085
                        iY = i / iNX;
1086
                        switch(getDataType()){
1087
                        case IBuffer.TYPE_BYTE: cell = new GridCell(iX, iY, getCellValueAsByte(iX, iY)); break;
1088
                        case IBuffer.TYPE_SHORT: cell = new GridCell(iX, iY, getCellValueAsShort(iX, iY)); break;
1089
                        case IBuffer.TYPE_INT: cell = new GridCell(iX, iY, getCellValueAsInt(iX, iY)); break;
1090
                        case IBuffer.TYPE_FLOAT: cell = new GridCell(iX, iY, getCellValueAsFloat(iX, iY)); break;
1091
                        case IBuffer.TYPE_DOUBLE: cell = new GridCell(iX, iY, getCellValueAsDouble(iX, iY)); break;
1092
                        }
1093

    
1094
                        cells[i] = cell;
1095
                }
1096

    
1097
                Arrays.sort(cells);
1098

    
1099
                return cells;
1100
        }
1101

    
1102
        /**
1103
         * Obtiene la paleta para el caso de un MDT. Esta funci?n evita el tener que obtener
1104
         * un array de paletas y buscar en el cuando se trata de un caso simple de MDT de una
1105
         * sola banda. Comprueba que se trata de un raster monobanda con paleta asociada antes
1106
         * de devolverla.
1107
         * @return GridPalette asociada al Grid
1108
         */
1109
        public GridPalette        getMDTPalette() {
1110
                if(        rasterBuf != null &&
1111
                        rasterBuf.getBandCount() == 1 &&
1112
                        palette != null)
1113
                        return palette[0];
1114
                return null;
1115
        }
1116

    
1117
        /**
1118
         * Obtiene la lista de paletas asociadas al grid
1119
         * @return Lista de objetos GridPalette
1120
         */
1121
        public GridPalette[] getPalettes() {
1122
                return palette;
1123
        }
1124

    
1125
        /**
1126
         * Obtiene el n?mero de bandas del grid o 0 si no tiene buffer de
1127
         * datos asociado.
1128
         * @return N?mero de bandas
1129
         */
1130
        public int getBandCount() {
1131
                if(rasterBuf != null)
1132
                        return rasterBuf.getBandCount();
1133
                return 0;
1134
        }
1135

    
1136
        /**
1137
         * Asigna la lista de paletas asociadas al grid
1138
         * @param palette Lista de objetos GridPalette
1139
         */
1140
        public void setPalettes(GridPalette[] palette) {
1141
                this.palette = palette;
1142
        }
1143

    
1144
        /**
1145
         * Obtiene la lista de filtros.
1146
         * @return Lista de filtros.
1147
         */
1148
        public RasterFilterList getFilterList(){
1149
                return filterList;
1150
        }
1151

    
1152
        /**
1153
         * Asigna la lista de filtros
1154
         * @param filterList
1155
         */
1156
        public void setFilterList(RasterFilterList filterList) {
1157
                this.filterList = filterList;
1158
        }
1159

    
1160
        /**
1161
         *Aplica la lista de filtros sobre el buffer
1162
         * @throws InterruptedException
1163
         */
1164
        public void applyFilters() throws InterruptedException {
1165
                if (filterList == null)
1166
                        return;
1167

    
1168
                filterList.setInitRasterBuf(rasterBuf);
1169

    
1170
                filterList.getEnv().put("GridExtent", getGridExtent());
1171
                filterList.getEnv().put("WindowExtent", getWindowExtent());
1172

    
1173
                filterList.execute();
1174
                rasterBuf = (RasterBuffer) filterList.getResult();
1175
                dataType = rasterBuf.getDataType();
1176
        }
1177

    
1178
        /**
1179
         * Obtiene el buffer de datos del grid
1180
         * @return RasterBuf
1181
         */
1182
        public IBuffer getRasterBuf() {
1183
                return rasterBuf;
1184
        }
1185

    
1186
        /**
1187
         * Asigna la banda sobre la que se realizan las operaciones. Por defecto es la banda 0
1188
         * con lo que para el uso de MDTs no habr? que modificar este valor.
1189
         * @param band Banda sobre la que se realizan las operaciones.
1190
         */
1191
        public void setBandToOperate(int band) {
1192
                if(writer != null)
1193
                        writer.setBandToOperate(band);
1194
                if(reader != null)
1195
                        reader.setBandToOperate(band);
1196
                if(statistic != null)
1197
                        statistic.setBandToOperate(band);
1198
        }
1199

    
1200
        /**
1201
         * Obtiene el extent de la ventana seleccionada para petici?n de datos
1202
         * @return GridExtent
1203
         */
1204
        public GridExtent getWindowExtent() {
1205
                return windowExtent;
1206
        }
1207

    
1208
        /**
1209
         * Obtiene el extent del grid para petici?n de datos
1210
         * @return GridExtent
1211
         */
1212
        public GridExtent getGridExtent() {
1213
                return layerExtent;
1214
        }
1215

    
1216
        /**
1217
         * Releases buffer resources
1218
         */
1219
        public void free() {
1220
                if (rasterBuf != null)
1221
                        rasterBuf.free();
1222
                if (filterList != null)
1223
                        filterList.free();
1224
                if (writer != null)
1225
                        writer.free();
1226
        }
1227
}