Statistics
| Revision:

gvsig-raster / org.gvsig.raster / branches / org.gvsig.raster_dataaccess_refactoring / org.gvsig.raster.lib / org.gvsig.raster.lib.impl / src / main / java / org / gvsig / raster / impl / grid / GridImpl.java @ 2308

History | View | Annotate | Download (39 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22
package org.gvsig.raster.impl.grid;
23

    
24
import java.util.Arrays;
25

    
26
import org.gvsig.fmap.dal.coverage.RasterLibrary;
27
import org.gvsig.fmap.dal.coverage.dataset.Buffer;
28
import org.gvsig.fmap.dal.coverage.datastruct.Extent;
29
import org.gvsig.fmap.dal.coverage.datastruct.GridCell;
30
import org.gvsig.fmap.dal.coverage.datastruct.GridExtent;
31
import org.gvsig.fmap.dal.coverage.datastruct.NoData;
32
import org.gvsig.fmap.dal.coverage.exception.FilterAddException;
33
import org.gvsig.fmap.dal.coverage.exception.GridException;
34
import org.gvsig.fmap.dal.coverage.exception.OutOfGridException;
35
import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException;
36
import org.gvsig.fmap.dal.coverage.exception.RasterBufferInvalidAccessException;
37
import org.gvsig.fmap.dal.coverage.exception.RasterBufferInvalidException;
38
import org.gvsig.fmap.dal.coverage.grid.Grid;
39
import org.gvsig.fmap.dal.coverage.grid.RasterFilterList;
40
import org.gvsig.fmap.dal.coverage.store.RasterDataStore;
41
import org.gvsig.fmap.dal.coverage.store.props.BasicStats;
42
import org.gvsig.fmap.dal.coverage.store.props.ColorInterpretation;
43
import org.gvsig.fmap.dal.coverage.store.props.ColorTable;
44
import org.gvsig.fmap.dal.coverage.store.props.Transparency;
45
import org.gvsig.raster.impl.DefaultRasterManager;
46
import org.gvsig.raster.impl.buffer.RasterBuffer;
47
import org.gvsig.raster.impl.datastruct.ExtentImpl;
48
import org.gvsig.raster.impl.grid.filter.DefaultRasterFilterList;
49
import org.gvsig.raster.impl.store.QueryableRaster;
50
import org.gvsig.raster.impl.store.properties.DataStoreColorInterpretation;
51
import org.gvsig.raster.impl.store.properties.DataStoreColorTable;
52
import org.gvsig.raster.impl.store.properties.DataStoreTransparency;
53

    
54
/**
55
 * Clase que representa una rejilla de datos raster. Este tipo de grid tiene el interfaz necesario
56
 * de acceso a la informaci?n raster para aplicaciones de analisis raster. Contiene m?todos de acceso al
57
 * dato tanto en lectura como en escritura y encapsula operaciones entre grids. Adem?s contiene
58
 * objetos que contienen las caracter?sticas asociadas a ese grid. Sobre un grid pueden
59
 * aplicarse tambi?n operaciones de filtrado.
60
 *
61
 * Podemos crear un Grid de cinco formas distitas:
62
 * <UL>
63
 * <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>
64
 * <LI>A partir de un extensi?n de capa, una extensi?n de vista, un tipo de dato y un n?mero de datos
65
 * crea un Grid vacio util para escritura.</LI>
66
 * <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>
67
 * <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>
68
 * <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>
69
 * </UL>
70
 *
71
 * @author Victor Olaya (volaya@ya.com)
72
 * @author Nacho Brodin (nachobrodin@gmail.com)
73
 */
74
public class GridImpl implements Grid {
75

    
76
        public final static double                 DEG_45_IN_RAD = Math.PI / 180. * 45.;
77
        public final static double                 DEG_90_IN_RAD = Math.PI / 180. * 90.;
78
        public final static double                 DEG_180_IN_RAD = Math.PI ;
79
        public final static double                 DEG_270_IN_RAD = Math.PI / 180. * 270.;
80
        public final static double                 DEG_360_IN_RAD = Math.PI * 2.;
81

    
82
        /* neighbor's address*/                               /* N  NE   E  SE   S  SW   W  NW */
83
        private final static int                 m_iOffsetX []=        {  0,  1,  1,  1,  0, -1, -1, -1};
84
        private final static int                m_iOffsetY []=        {  1,  1,  0, -1, -1, -1,  0,  1};
85

    
86
        private double                                         m_dDist[];
87
        public double                                         _2DX, _6DX, _DX_2, _4DX_2;
88

    
89
        private int[]                                         bands = null;
90
        private int                                         dataType = Buffer.TYPE_UNDEFINED;
91

    
92
        private Buffer                                rasterBuf = null;
93
        private GridReader                                 reader = null;
94
        private GridWriter                                 writer = null;
95
        private GridExtent                                 windowExtent = null;
96
        private GridExtent                                 layerExtent = null;
97
        private GridStatistic                        statistic = null;
98
        private ColorTable                          palette = null;
99
        private RasterFilterList                filterList = null;
100

    
101
        /**
102
         * Crea un grid a partir de un RasterDataset. Usa el extent de la fuente de datos como
103
         * extent completo y el extent pasado como par?metro como extensi?n de ventana.
104
         *
105
         * Cuando se construye el reader se carga el buffer con la extensi?n definida en windowExtent
106
         * y las bandas especificadas en bands.
107
         *
108
         * @param IRasterDataSource datasets que proporcionan los datos
109
         * @param bands n?mero de bandas requeridas
110
         * @param windowExtent Extensi?n de la ventana. Si este par?metro es null se usar? el
111
         * mismo que el de la capa.
112
         */
113
        public GridImpl(RasterDataStore datasource, int[] bands, GridExtent windowExtent)
114
                        throws RasterBufferInvalidException {
115
                double cellSize[] = calcCellSize(datasource, 
116
                                                                                        datasource.getWidth(),
117
                                                                                        datasource.getHeight());
118
                layerExtent = new GridExtentImpl(datasource.getExtent(),
119
                                                                                        cellSize[0],
120
                                                                                        cellSize[1]);
121
                if(datasource.getDataType() != null)
122
                        dataType = datasource.getDataType()[0];
123
                this.bands = bands;
124

    
125
                if(windowExtent == null)
126
                        this.windowExtent = layerExtent;
127
                else
128
                        this.windowExtent = windowExtent;
129

    
130
                if (this.windowExtent.fitsIn(layerExtent))
131
                        reader = new GridNotInterpolated(datasource, layerExtent, this.windowExtent, bands);
132
                else
133
                        reader = new GridInterpolated(datasource, layerExtent, this.windowExtent, bands);
134
                rasterBuf = datasource.getLastBuffer();
135
                writer = new GridWriter(layerExtent, dataType, rasterBuf);
136

    
137
                init(datasource);
138
        }
139

    
140
        /**
141
         * Carga el buffer de datos desde el reader para poder escribir sobre los datos de
142
         * la ventana original. Esto es ?til cuando el GridWriter se crea asociado a un raster
143
         * pero el buffer est? vacio, por ejemplo en el caso de crear un GridInterpolated. Un
144
         * GridInterpolated no carga ning?n buffer en memoria sino que accede a los datos en disco
145
         * a medida que se le van solicitando. Por ello, no es posible modificarlos sino cargamos
146
         * un buffer con los datos previamente.
147
         */
148
        public void loadWriterData() throws GridException {
149
                rasterBuf = DefaultRasterManager.getInstance().createBuffer(dataType, reader.getNX(), reader.getNY(), bands.length, true);
150
                writer = new GridWriter(windowExtent, dataType, rasterBuf);
151
                int x = 0, y = 0;
152
                try{
153
                        switch(rasterBuf.getDataType()) {
154
                        case Buffer.TYPE_BYTE: for (x = 0; x < getNX(); x++)
155
                                for (y = 0; y < getNY(); y++)
156
                                        writer.setCellValue(x, y, (reader.getCellValueAsByte(x, y)));
157
                                                                        break;
158
                        case Buffer.TYPE_SHORT:for (x = 0; x < getNX(); x++)
159
                                for (y = 0; y < getNY(); y++)
160
                                        writer.setCellValue(x, y, (reader.getCellValueAsShort(x, y)));
161
                                                                        break;
162
                        case Buffer.TYPE_INT:         for (x = 0; x < getNX(); x++)
163
                                for (y = 0; y < getNY(); y++)
164
                                        writer.setCellValue(x, y, (reader.getCellValueAsInt(x, y)));
165
                                                                        break;
166
                        case Buffer.TYPE_FLOAT:for (x = 0; x < getNX(); x++)
167
                                for (y = 0; y < getNY(); y++)
168
                                        writer.setCellValue(x, y, (reader.getCellValueAsFloat(x, y)));
169
                                                                        break;
170
                        case Buffer.TYPE_DOUBLE:for (x = 0; x < getNX(); x++)
171
                                for (y = 0; y < getNY(); y++)
172
                                        writer.setCellValue(x, y, (reader.getCellValueAsDouble(x, y)));
173
                                                                         break;
174
                        }
175
                } catch (RasterBufferInvalidException e) {
176
                        throw new GridException("Buffer de datos no v?lido " + x + " " + y, e);
177
                } catch (OutOfGridException e1) {
178
                        throw new GridException("Acceso fuera de los l?mites del Grid " + x + " " + y, e1);
179
                } catch (RasterBufferInvalidAccessException e) {
180
                        throw new GridException("Acceso al grid no v?lido " + x + " " + y, e);
181
                }
182
        }
183

    
184
        /**
185
         * Crea un grid vacio a partir de un extent y un tipo de datos. Se crea un buffer vacio en el
186
         * que puede escribirse a trav?s del writer. Los datos escritos pueden ser consultados con el
187
         * reader. No tiene una fuente de datos asociada ya que es un grid vacio basicamente para
188
         * escritura.
189
         * @param layerExtent Tama?o completo de la capa
190
         * @param windowExtent Ventana de datos.
191
         * @param dataType Tipo de datos del buffer
192
         * @param bands n?mero de bandas requeridas y orden de dibujado en el buffer
193
         */
194
        public GridImpl(GridExtent layerExtent,
195
                                        GridExtent windowExtent,
196
                                        int dataType,
197
                                        int[] bands) throws RasterBufferInvalidException {
198
                this.windowExtent = windowExtent;
199
                this.layerExtent = layerExtent;
200
                this.dataType = dataType;
201

    
202
                rasterBuf = DefaultRasterManager.getInstance().createBuffer(dataType, layerExtent.getNX(), layerExtent.getNY(), bands.length, true);
203

    
204
                if (windowExtent.fitsIn(layerExtent))
205
                        reader = new GridNotInterpolated(rasterBuf, layerExtent, windowExtent, bands);
206
                else
207
                        reader = new GridInterpolated(rasterBuf, layerExtent, windowExtent, bands);
208
                writer = new GridWriter(layerExtent, dataType, rasterBuf);
209
                init(null);
210
        }
211

    
212
        /**
213
         * Crea un grid a partir de un BufferFactory. El buffer debe estar cargado de datos y el extent
214
         * de este viene definido en el par?metro windowExtent.
215
         * @param bufferFactory Fuente de datos
216
         * @param windowExtent        Extent de los datos cargados en bufferFactory
217
         * @param notInterp Si es true fuerza a que el reader sea sin interpolaci?n. Si es false decide si
218
         * el reader es interpolado o no a partir de los extents de capa y la ventana seleccionada.
219
         */
220
        public GridImpl(Buffer buf, RasterDataStore datasource, boolean notInterp) {
221
                double cellSize[] = calcCellSize(datasource, 
222
                                                                                datasource.getWidth(),
223
                                                                                datasource.getHeight());
224
                this.layerExtent = new GridExtentImpl(datasource.getExtent(),
225
                                                                                        cellSize[0],
226
                                                                                        cellSize[1]);
227
                if(datasource.getDataType() != null)
228
                        dataType = datasource.getDataType()[0];
229
                
230
                if(datasource instanceof QueryableRaster)
231
                        bands = ((QueryableRaster)datasource).getDrawableBands();
232
                else {
233
                        bands = new int[datasource.getBandCount()];
234
                        for (int i = 0; i < bands.length; i++) 
235
                                bands[i] = i;
236
                }
237
                        
238

    
239
                this.windowExtent = new GridExtentImpl(        new ExtentImpl(buf.getDataExtent()),
240
                                                                                        cellSize[0],
241
                                                                                        cellSize[1]);
242

    
243
                rasterBuf = buf;
244

    
245
                if(notInterp)
246
                        reader = new GridNotInterpolated(rasterBuf, layerExtent, windowExtent, bands);
247
                else if (windowExtent.fitsIn(layerExtent))
248
                        reader = new GridNotInterpolated(rasterBuf, layerExtent, windowExtent, bands);
249
                else
250
                        reader = new GridInterpolated(rasterBuf, layerExtent, windowExtent, bands);
251
                writer = new GridWriter(layerExtent, dataType, rasterBuf);
252
                init(datasource);
253
        }
254

    
255
        /**
256
         * Crea un grid a partir de un BufferFactory. Se har? una petici?n del extent completo de la fuente.
257
         * bufferFactory tiene asociada una fuente de datos pero se ignorar? si tiene el buffer lleno. La instanciaci?n
258
         * de GridNotInterpolated har? que se haga la petici?n para cargar raster completo.
259
         * @param bufferFactory Fuente de datos
260
         * @param bands n?mero de bandas requeridas y orden de dibujado en el buffer
261
         */
262
        public GridImpl(RasterDataStore datasource, int[] bands)
263
                        throws RasterBufferInvalidException{
264
                //La petici?n es del raster completo
265
                double cellSize[] = calcCellSize(datasource, 
266
                                                                                        datasource.getWidth(),
267
                                                                                        datasource.getHeight());
268
                windowExtent = layerExtent = new GridExtentImpl(datasource.getExtent(),
269
                                                                                                        cellSize[0],
270
                                                                                                        cellSize[1]);
271
                if(datasource.getDataType() != null)
272
                        dataType = datasource.getDataType()[0];
273
                this.bands = bands;
274

    
275
                reader = new GridNotInterpolated(datasource, layerExtent, windowExtent, bands);
276
                rasterBuf = datasource.getLastBuffer();
277
                writer = new GridWriter(windowExtent, dataType, rasterBuf);
278

    
279
                init(datasource);
280
        }
281

    
282
        /**
283
         * Crea un grid a partir de un BufferFactory. Se har? una petici?n del extent completo de la fuente.
284
         * bufferFactory tiene asociada una fuente de datos pero se ignorar? si tiene el buffer lleno. La instanciaci?n
285
         * de GridNotInterpolated har? que se haga la petici?n para cargar raster completo. El buffer se cargar?
286
         * con todas las bandas disponibles.
287
         * @param datasource Fuente de datos
288
         */
289
        public GridImpl(RasterDataStore datasource) throws RasterBufferInvalidException {
290
                //La petici?n es del raster completo
291
                double cellSize[] = calcCellSize(        datasource, 
292
                                                                                        datasource.getWidth(),
293
                                                                                        datasource.getHeight());
294
                windowExtent = layerExtent = new GridExtentImpl(datasource.getExtent(),
295
                                                                                                        cellSize[0],
296
                                                                                                        cellSize[1]);
297
                if(datasource.getDataType() != null)
298
                        dataType = datasource.getDataType()[0];
299

    
300
                bands = new int[datasource.getBandCount()];
301
                for(int i = 0; i < datasource.getBandCount(); i ++)
302
                        bands[i] = i;
303
                reader = new GridNotInterpolated(datasource, layerExtent, windowExtent, bands);
304
                rasterBuf = datasource.getLastBuffer();
305
                writer = new GridWriter(windowExtent, dataType, rasterBuf);
306

    
307
                init(datasource);
308
        }
309

    
310
        /**
311
         * Calcula el tama?o de celda a partir de un dataset, un ancho y un alto en pixeles
312
         * @return
313
         */
314
        private double[] calcCellSize(RasterDataStore mDataset, double w, double h) {
315
                double dCellsize[] = new double[2];
316
                try {
317
                        Extent e = mDataset.getExtent();
318
                        dCellsize[0] = (e.getLRX() - e.getULX()) / w;
319
                        dCellsize[1] = (e.getLRY() - e.getULY()) / h;
320

    
321
                        return dCellsize;
322
                } catch (NullPointerException e) {
323
                        dCellsize[0] = 1;
324
                        dCellsize[1] = 1;
325
                        return dCellsize;
326
                }
327
        }
328

    
329
        /**
330
         * Selecciona la forma de obtener datos con o sin interpolaci?n. Si pasamos el par?metro
331
         * false al aplicar un m?todo de consulta de datos este ser? aplicado sin interpolaci?n.
332
         * @param interpolation
333
         */
334
        public void switchToInterpolationMethod(boolean interpolation){
335
                //GridExtent layer = new GridExtent(0, 0, rasterBuf.getWidth(), rasterBuf.getHeight(), 1);
336
                if(interpolation)
337
                        reader = new GridInterpolated(rasterBuf, layerExtent, windowExtent, bands);
338
                else
339
                        reader = new GridNotInterpolated(rasterBuf, layerExtent, windowExtent, bands);
340
                init(null);
341
        }
342

    
343
        /**
344
         * Inicializaci?n de constantes
345
         */
346
        private void init(RasterDataStore ds) {
347
                int i;
348
                Transparency transparency = null;
349

    
350
                double dCellSize = getCellSize();
351

    
352
                statistic = new GridStatistic(this);
353
                if (ds == null) {
354
                        ColorInterpretation ci = new DataStoreColorInterpretation(rasterBuf.getBandCount());
355
                        transparency = new DataStoreTransparency(ci);
356
                }
357
                else {
358
                        transparency = ds.getTransparency().cloneTransparency();
359
                        this.palette = new DataStoreColorTable(ds.getColorTable());
360
                }
361
                filterList = new DefaultRasterFilterList();
362
                if (ds != null) {
363
                        filterList.addEnvParam("SrcStatistics", ds.getStatistics());
364
                }
365
                filterList.addEnvParam("Transparency", transparency);
366
                if(rasterBuf != null)
367
                        filterList.setInitRasterBuf(rasterBuf);
368

    
369
                m_dDist = new double[8];
370

    
371
                for (i = 0; i < 8; i++)
372
                        m_dDist[i] = Math.sqrt ( m_iOffsetX[i] * dCellSize * m_iOffsetX[i] * dCellSize
373
                                                                        + m_iOffsetY[i] * dCellSize * m_iOffsetY[i] * dCellSize );
374

    
375
                _2DX = dCellSize * 2.0;
376
                _6DX = dCellSize * 6.0;
377
                _DX_2 = dCellSize * dCellSize;
378
                _4DX_2 = 4.0 * _DX_2;
379
        }
380
        
381
        /*
382
         * (non-Javadoc)
383
         * @see org.gvsig.fmap.dal.coverage.grid.Grid#getBandCount()
384
         */
385
        public int getBandCount() {
386
                if(rasterBuf != null)
387
                        return rasterBuf.getBandCount();
388
                return 0;
389
        }
390

    
391
        //************* Write Services *********************
392

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

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

    
417
        /*
418
         *  (non-Javadoc)
419
         * @see org.gvsig.fmap.grid.IWritableGrid#assign(int)
420
         */
421
        public void assign(int value)throws GridException {
422
                try {
423
                        writer.assign(value);
424
                } catch (RasterBufferInvalidAccessException e) {
425
                        throw new GridException("Error wrinting buffer");
426
                }
427
        }
428

    
429
        /*
430
         *  (non-Javadoc)
431
         * @see org.gvsig.fmap.grid.IWritableGrid#assign(float)
432
         */
433
        public void assign(float value)throws GridException {
434
                try {
435
                        writer.assign(value);
436
                } catch (RasterBufferInvalidAccessException e) {
437
                        throw new GridException("Error wrinting buffer");
438
                }
439
        }
440

    
441
        /*
442
         *  (non-Javadoc)
443
         * @see org.gvsig.fmap.grid.IWritableGrid#assign(double)
444
         */
445
        public void assign(double value)throws GridException {
446
                try {
447
                        writer.assign(value);
448
                } catch (RasterBufferInvalidAccessException e) {
449
                        throw new GridException("Error wrinting buffer");
450
                }
451
        }
452

    
453
        /*
454
         *  (non-Javadoc)
455
         * @see org.gvsig.fmap.grid.IWritableGrid#assign(org.gvsig.fmap.dataaccess.BufferFactory)
456
         */
457
        public void assign(RasterDataStore dSource) throws GridException {
458
                Grid window = null;
459
                try {
460
                        window = new GridImpl(dSource, bands, windowExtent);
461
                        write(window);
462
                        writer.setNoDataValue(window.getNoDataValue());
463
                } catch (RasterBufferInvalidException e) {
464
                        throw new GridException("Error writing buffer");
465
                }
466
        }
467

    
468
        /*
469
         *  (non-Javadoc)
470
         * @see org.gvsig.fmap.grid.IWritableGrid#assign(org.gvsig.fmap.grid.Grid)
471
         */
472
        public void assign(Grid driver) throws GridException {
473
                if (driver.getGridExtent().equals(layerExtent)) {
474
                        write(driver);
475
                        writer.setNoDataValue(driver.getNoDataValue());
476
                }
477
        }
478

    
479
        /**
480
         * Sobreescribe los datos del grid actual con los datos del grid pasado por par?metro
481
         * @param g Grid desde donde se obtienen los datos
482
         */
483
        private void write(Grid g) throws GridException {
484
                try{
485
                        switch(rasterBuf.getDataType()){
486
                        case Buffer.TYPE_BYTE: for (int x = 0; x < g.getNX(); x++)
487
                                for (int y = 0; y < g.getNY(); y++)
488
                                        writer.setCellValue(x, y, g.getCellValueAsByte(x, y));
489
                                                                        break;
490
                        case Buffer.TYPE_SHORT:for (int x = 0; x < g.getNX(); x++)
491
                                for (int y = 0; y < g.getNY(); y++)
492
                                        writer.setCellValue(x, y, g.getCellValueAsShort(x, y));
493
                                                                        break;
494
                        case Buffer.TYPE_INT:         for (int x = 0; x < g.getNX(); x++)
495
                                for (int y = 0; y < g.getNY(); y++)
496
                                        writer.setCellValue(x, y, g.getCellValueAsInt(x, y));
497
                                                                        break;
498
                        case Buffer.TYPE_FLOAT:for (int x = 0; x < g.getNX(); x++)
499
                                for (int y = 0; y < g.getNY(); y++)
500
                                        writer.setCellValue(x, y, g.getCellValueAsFloat(x, y));
501
                                                                        break;
502
                        case Buffer.TYPE_DOUBLE:for (int x = 0; x < g.getNX(); x++)
503
                                for (int y = 0; y < g.getNY(); y++)
504
                                        writer.setCellValue(x, y, g.getCellValueAsDouble(x, y));
505
                                                                        break;
506
                        }
507
                } catch (OutOfGridException e) {
508
                        throw new GridException("");
509
                }
510
        }
511

    
512
        /*
513
         *  (non-Javadoc)
514
         * @see org.gvsig.fmap.grid.IWritableGrid#assignNoData()
515
         */
516
        public void assignNoData(){
517
                try {
518
                        if(!rasterBuf.getNoDataValue().isDefined())
519
                                return;
520
                        
521
                        switch(rasterBuf.getDataType()){
522
                        case Buffer.TYPE_BYTE: writer.assign((byte)rasterBuf.getNoDataValue().getValue().byteValue());break;
523
                        case Buffer.TYPE_SHORT: writer.assign((short)rasterBuf.getNoDataValue().getValue().shortValue());break;
524
                        case Buffer.TYPE_INT: writer.assign((int)rasterBuf.getNoDataValue().getValue().intValue());break;
525
                        case Buffer.TYPE_FLOAT: writer.assign((float)rasterBuf.getNoDataValue().getValue().floatValue());break;
526
                        case Buffer.TYPE_DOUBLE: writer.assign(rasterBuf.getNoDataValue().getValue().doubleValue());break;
527
                        }
528
                } catch (RasterBufferInvalidAccessException e) {
529
                        //No hacemos nada. El tipo de dato al que se accede no es controlado por el usuario por lo
530
                        //que no le mandamos la excepci?n hacia arriba.
531
                        e.printStackTrace();
532
                }
533
        }
534

    
535
        /*
536
         *  (non-Javadoc)
537
         * @see org.gvsig.fmap.grid.IWritableGrid#setCellValue(int, int, byte)
538
         */
539
        public void setCellValue(int x, int y, byte value)throws OutOfGridException{
540
                writer.setCellValue(x, y, value);
541
        }
542

    
543
        /*
544
         *  (non-Javadoc)
545
         * @see org.gvsig.fmap.grid.IWritableGrid#setCellValue(int, int, short)
546
         */
547
        public void setCellValue(int x, int y, short value)throws OutOfGridException{
548
                writer.setCellValue(x, y, value);
549
        }
550

    
551
        /*
552
         *  (non-Javadoc)
553
         * @see org.gvsig.fmap.grid.IWritableGrid#setCellValue(int, int, int)
554
         */
555
        public void setCellValue(int x, int y, int value)throws OutOfGridException{
556
                writer.setCellValue(x, y, value);
557
        }
558

    
559
        /*
560
         *  (non-Javadoc)
561
         * @see org.gvsig.fmap.grid.IWritableGrid#setCellValue(int, int, float)
562
         */
563
        public void setCellValue(int x, int y, float value)throws OutOfGridException{
564
                writer.setCellValue(x, y, value);
565
        }
566

    
567
        /*
568
         *  (non-Javadoc)
569
         * @see org.gvsig.fmap.grid.IWritableGrid#setCellValue(int, int, double)
570
         */
571
        public void setCellValue(int x, int y, double value)throws OutOfGridException{
572
                writer.setCellValue(x, y, value);
573
        }
574

    
575
        /*
576
         *  (non-Javadoc)
577
         * @see org.gvsig.fmap.grid.IWritableGrid#add(org.gvsig.fmap.grid.Grid)
578
         */
579
        public void add(Grid g) throws GridException {
580
                if (g.getGridExtent().equals(getGridExtent())) {
581
                        boolean interp = (reader instanceof GridInterpolated);
582
                        switchToInterpolationMethod(false);
583
                        try{
584
                                switch(rasterBuf.getDataType()){
585
                                case Buffer.TYPE_BYTE: for (int x = 0; x < g.getNX(); x++)
586
                                        for (int y = 0; y < g.getNY(); y++)
587
                                                writer.setCellValue(x, y, reader.getCellValueAsByte(x, y) + g.getCellValueAsByte(x,y));
588
                                                                                break;
589
                                case Buffer.TYPE_SHORT:for (int x = 0; x < g.getNX(); x++)
590
                                        for (int y = 0; y < g.getNY(); y++)
591
                                                writer.setCellValue(x, y, reader.getCellValueAsShort(x, y) + g.getCellValueAsShort(x,y));
592
                                                                                break;
593
                                case Buffer.TYPE_INT:         for (int x = 0; x < g.getNX(); x++)
594
                                        for (int y = 0; y < g.getNY(); y++)
595
                                                writer.setCellValue(x, y, reader.getCellValueAsInt(x, y) + g.getCellValueAsInt(x,y));
596
                                                                                break;
597
                                case Buffer.TYPE_FLOAT:for (int x = 0; x < g.getNX(); x++)
598
                                        for (int y = 0; y < g.getNY(); y++)
599
                                                writer.setCellValue(x, y, reader.getCellValueAsFloat(x, y) + g.getCellValueAsFloat(x,y));
600
                                                                                break;
601
                                case Buffer.TYPE_DOUBLE:for (int x = 0; x < g.getNX(); x++)
602
                                        for (int y = 0; y < g.getNY(); y++)
603
                                                writer.setCellValue(x, y, reader.getCellValueAsDouble(x, y) + g.getCellValueAsDouble(x,y));
604
                                                                                break;
605
                                }
606
                        } catch (OutOfGridException e) {
607
                                throw new GridException("");
608
                        } catch (RasterBufferInvalidAccessException e1) {
609
                                throw new GridException("");
610
                        } catch (RasterBufferInvalidException e) {
611
                                throw new GridException("");
612
                        }
613
                        switchToInterpolationMethod(interp);
614
                }
615
        }
616

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

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

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

    
668
        /*
669
         *  (non-Javadoc)
670
         * @see org.gvsig.fmap.grid.IWritableGrid#addToCellValue(int, int, float)
671
         */
672
        public void addToCellValue(int x, int y, float value)
673
                throws GridException {
674
                try {
675
                        writer.setCellValue(x, y, (reader.getCellValueAsFloat(x, y) + value));
676
                } catch (OutOfGridException e) {
677
                        throw new GridException("");
678
                } catch (RasterBufferInvalidAccessException e) {
679
                        throw new GridException("");
680
                } catch (RasterBufferInvalidException e) {
681
                        throw new GridException("");
682
                }
683
        }
684

    
685
        /*
686
         *  (non-Javadoc)
687
         * @see org.gvsig.fmap.grid.IWritableGrid#addToCellValue(int, int, double)
688
         */
689
        public void addToCellValue(int x, int y, double value)
690
                throws GridException {
691
                try {
692
                        writer.setCellValue(x, y, (reader.getCellValueAsDouble(x, y) + value));
693
                } catch (OutOfGridException e) {
694
                        throw new GridException("");
695
                } catch (RasterBufferInvalidAccessException e) {
696
                        throw new GridException("");
697
                } catch (RasterBufferInvalidException e) {
698
                        throw new GridException("");
699
                }
700
        }
701

    
702
        /*
703
         *  (non-Javadoc)
704
         * @see org.gvsig.fmap.grid.IWritableGrid#multiply(double)
705
         */
706
        public void multiply(double value) throws GridException {
707
                boolean interp = (reader instanceof GridInterpolated);
708
                switchToInterpolationMethod(false);
709
                try{
710
                        switch(rasterBuf.getDataType()){
711
                        case Buffer.TYPE_BYTE: for (int x = 0; x < getNX(); x++)
712
                                for (int y = 0; y < getNY(); y++)
713
                                        writer.setCellValue(x, y, (byte)(reader.getCellValueAsByte(x, y) * value));
714
                                                                        break;
715
                        case Buffer.TYPE_SHORT:for (int x = 0; x < getNX(); x++)
716
                                for (int y = 0; y < getNY(); y++)
717
                                        writer.setCellValue(x, y, (short)(reader.getCellValueAsShort(x, y) * value));
718
                                                                        break;
719
                        case Buffer.TYPE_INT:         for (int x = 0; x < getNX(); x++)
720
                                for (int y = 0; y < getNY(); y++)
721
                                        writer.setCellValue(x, y, (int)(reader.getCellValueAsInt(x, y) * value));
722
                                                                        break;
723
                        case Buffer.TYPE_FLOAT:for (int x = 0; x < getNX(); x++)
724
                                for (int y = 0; y < getNY(); y++)
725
                                        writer.setCellValue(x, y, (float)(reader.getCellValueAsFloat(x, y) * value));
726
                                                                        break;
727
                        case Buffer.TYPE_DOUBLE:for (int x = 0; x < getNX(); x++)
728
                                for (int y = 0; y < getNY(); y++)
729
                                        writer.setCellValue(x, y, (reader.getCellValueAsDouble(x, y) * value));
730
                                                                        break;
731
                        }
732
                } catch (RasterBufferInvalidException e) {
733
                        throw new GridException("Buffer de datos no v?lido", e);
734
                } catch (OutOfGridException e1) {
735
                        throw new GridException("Acceso fuera de los l?mites del Grid", e1);
736
                } catch (RasterBufferInvalidAccessException e) {
737
                        throw new GridException("Acceso al grid no v?lido", e);
738
                }
739
                //Restauramos el reader que hab?a
740
                switchToInterpolationMethod(interp);
741
        }
742

    
743
        /*
744
         *  (non-Javadoc)
745
         * @see org.gvsig.fmap.grid.IWritableGrid#setNoDataValue(double)
746
         */
747
        public void setNoDataValue(NoData dNoDataValue) {
748
                writer.setNoDataValue(dNoDataValue);
749
        }
750

    
751
        /*
752
         *  (non-Javadoc)
753
         * @see org.gvsig.fmap.grid.IWritableGrid#setNoData(int, int)
754
         */
755
        public void setNoData(int x, int y){
756
                writer.setNoData(x, y);
757
        }
758

    
759
        //************* Query Services *********************
760

    
761
        /*
762
         *  (non-Javadoc)
763
         * @see org.gvsig.fmap.grid.IQueryableGrid#isNoDataValue(double)
764
         */
765
        public boolean isNoDataValue(double noDataValue){
766
                return (reader.getNoDataValue() == noDataValue);
767
        }
768

    
769
        /*
770
         *  (non-Javadoc)
771
         * @see org.gvsig.fmap.grid.IQueryableGrid#isInGrid(int, int)
772
         */
773
        public boolean isInGrid(int x, int y){
774
                return reader.isCellInGrid(x, y);
775
        }
776

    
777
        /*
778
         *  (non-Javadoc)
779
         * @see org.gvsig.fmap.grid.IQueryableGrid#getCellSize()
780
         */
781
        public double getCellSize() {
782
                return reader.getCellSize();
783
        }
784

    
785
        /*
786
         *  (non-Javadoc)
787
         * @see org.gvsig.fmap.grid.IQueryableGrid#getCellValueAsByte(int, int)
788
         */
789
        public byte getCellValueAsByte(int x, int y)throws GridException {
790
                try {
791
                        return  reader.getCellValueAsByte(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#getCellValueAsShort(int, int)
802
         */
803
        public short getCellValueAsShort(int x, int y)throws GridException {
804
                try {
805
                        return reader.getCellValueAsShort(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#getCellValueAsInt(int, int)
816
         */
817
        public int getCellValueAsInt(int x, int y)throws GridException {
818
                try {
819
                        return reader.getCellValueAsInt(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#getCellValueAsFloat(int, int)
830
         */
831
        public float getCellValueAsFloat(int x, int y)throws GridException {
832
                try {
833
                        return reader.getCellValueAsFloat(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#getCellValueAsDouble(int, int)
844
         */
845
        public double getCellValueAsDouble(int x, int y)throws GridException {
846
                try {
847
                        return reader.getCellValueAsDouble(x, y);
848
                } catch (RasterBufferInvalidAccessException e) {
849
                        throw new GridException("Position: (" + x + "," + y + ") not valid");
850
                } catch (RasterBufferInvalidException e) {
851
                        throw new GridException("Buffer not valid");
852
                }
853
        }
854

    
855
        /*
856
         *  (non-Javadoc)
857
         * @see org.gvsig.fmap.grid.IQueryableGrid#getCellValueAsDouble(int, int)
858
         */
859
        public double getCellValue(int x, int y)throws GridException {
860
                try {
861
                        return reader.getCellValue(x, y);
862
                } catch (RasterBufferInvalidAccessException e) {
863
                        throw new GridException("Position: (" + x + "," + y + ") not valid");
864
                } catch (RasterBufferInvalidException e) {
865
                        throw new GridException("Buffer not valid");
866
                }
867
        }
868

    
869
        /*
870
         *  (non-Javadoc)
871
         * @see org.gvsig.fmap.grid.IQueryableGrid#getNoDataValue()
872
         */
873
        public NoData getNoDataValue() {
874
                return rasterBuf.getNoDataValue();
875
        }
876

    
877
        /*
878
         *  (non-Javadoc)
879
         * @see org.gvsig.fmap.grid.IQueryableGrid#setInterpolationMethod(int)
880
         */
881
        public void setInterpolationMethod(int iMethod){
882
                if (reader instanceof GridInterpolated)
883
                        ((GridInterpolated) reader).setInterpolationMethod(iMethod);
884
                else{
885
                        this.switchToInterpolationMethod(true);
886
                        ((GridInterpolated) reader).setInterpolationMethod(iMethod);
887
                }
888
        }
889

    
890
        /*
891
         *  (non-Javadoc)
892
         * @see org.gvsig.fmap.grid.IQueryableGrid#getNX()
893
         */
894
        public int getNX(){
895
                return reader.getNX();
896
        }
897

    
898
        /*
899
         *  (non-Javadoc)
900
         * @see org.gvsig.fmap.grid.IQueryableGrid#getNY()
901
         */
902
        public int getNY(){
903
                return reader.getNY();
904
        }
905

    
906
        /*
907
         *  (non-Javadoc)
908
         * @see org.gvsig.fmap.grid.IQueryableGrid#getLayerNX()
909
         */
910
        public int getLayerNX(){
911
                return layerExtent.getNX();
912
        }
913

    
914
        /*
915
         *  (non-Javadoc)
916
         * @see org.gvsig.fmap.grid.IQueryableGrid#getLayerNY()
917
         */
918
        public int getLayerNY(){
919
                return layerExtent.getNY();
920
        }
921

    
922
        public int getDataType(){
923
                return dataType;
924
        }
925

    
926
        private boolean getSubMatrix3x3(int x, int y, double subMatrix[])
927
                throws GridException {
928
                int        i;
929
                int iDir;
930
                double        z, z2;
931

    
932
                z = getCellValueAsDouble(x, y);
933

    
934
                if(isNoDataValue(z))
935
                        return false;
936
                else {
937
                        //SubMatrix[4]        = 0.0;
938
                        for(i = 0; i < 4; i++) {
939
                                iDir = 2 * i;
940
                                z2 = getCellValueAsDouble(x + m_iOffsetX[iDir], y + m_iOffsetY[iDir]);
941
                                if( !isNoDataValue(z2))
942
                                        subMatrix[i] =  z2 - z;
943
                                else {
944
                                        z2 = getCellValueAsDouble(x + m_iOffsetX[(iDir + 4) % 8], y + m_iOffsetY[(iDir  + 4) % 8]);
945
                                        if( !isNoDataValue(z2))
946
                                                subMatrix[i] = z - z2;
947
                                        else
948
                                                subMatrix[i] = 0.0;
949
                                }
950
                        }
951

    
952
                        return true;
953
                }
954
        }
955

    
956
        /*
957
         * (non-Javadoc)
958
         * @see org.gvsig.raster.grid.IQueryableGrid#getSlope(int, int)
959
         */
960
        public double getSlope(int x, int y)throws GridException {
961
                double        zm[], G, H;
962

    
963
                zm = new double[4];
964

    
965
                try {
966
                        if( getSubMatrix3x3(x, y, zm) ) {
967
                                G = (zm[0] - zm[2]) / _2DX;
968
                                H = (zm[1] - zm[3]) / _2DX;
969
                                return Math.atan(Math.sqrt(G * G + H * H));
970
                        } else
971
                                return rasterBuf.getNoDataValue().isDefined() ? rasterBuf.getNoDataValue().getValue().doubleValue() : RasterLibrary.defaultDoubleNoDataValue;
972
                } catch (GridException e) {
973
                        throw new GridException("Problems accesing 3x3 submatrix");
974
                }
975
        }
976

    
977
        /*
978
         * (non-Javadoc)
979
         * @see org.gvsig.raster.grid.IQueryableGrid#getAspect(int, int)
980
         */
981
        public double getAspect(int x, int y)throws GridException {
982
                double        zm[], G, H, dAspect;
983
                zm = new double[4];
984

    
985
                try {
986
                        if( getSubMatrix3x3(x, y, zm) ) {
987
                                G = (zm[0] - zm[2]) / _2DX;
988
                                H = (zm[1] - zm[3]) / _2DX;
989
                                if(G != 0.0)
990
                                        dAspect = DEG_180_IN_RAD + Math.atan2(H, G);
991
                                else
992
                                        dAspect = H > 0.0 ? DEG_270_IN_RAD : (H < 0.0 ? DEG_90_IN_RAD : -1.0);
993
                                return dAspect;
994
                        }
995
                        else
996
                                return rasterBuf.getNoDataValue().isDefined() ? rasterBuf.getNoDataValue().getValue().doubleValue() : RasterLibrary.defaultDoubleNoDataValue;
997
                } catch (GridException e) {
998
                        throw new GridException("Problems accesing 3x3 submatrix");
999
                }
1000
        }
1001

    
1002
        public static int getXOffsetInDir(int iDir){
1003
                return m_iOffsetX[iDir];
1004
        }
1005

    
1006
        public static int getYOffsetInDir(int iDir){
1007
                return m_iOffsetY[iDir];
1008
        }
1009

    
1010
        public double getDistToNeighborInDir(int iDir){
1011
                return m_dDist[iDir];
1012
        }
1013

    
1014
        public static double getUnitDistToNeighborInDir(int iDir){
1015
                return( (iDir % 2 != 0) ? Math.sqrt(2.0)  : 1.0 );
1016
        }
1017

    
1018
        /*
1019
         *  (non-Javadoc)
1020
         * @see org.gvsig.fmap.grid.IQueryableGrid#getDirToNextDownslopeCell(int, int)
1021
         */
1022
        public int getDirToNextDownslopeCell(int x, int y)throws GridException{
1023
                return getDirToNextDownslopeCell(x, y, true);
1024
        }
1025

    
1026
        /*
1027
         *  (non-Javadoc)
1028
         * @see org.gvsig.fmap.grid.IQueryableGrid#getDirToNextDownslopeCell(int, int, boolean)
1029
         */
1030
        public int getDirToNextDownslopeCell(int x, int y, boolean bForceDirToNoDataCell)
1031
                throws GridException{
1032

    
1033
                int                i, iDir;
1034
                double        z, z2, dSlope, dMaxSlope;
1035

    
1036
                z = getCellValueAsDouble(x, y);
1037

    
1038
                if(isNoDataValue(z))
1039
                        return -1;
1040

    
1041
                dMaxSlope = 0.0;
1042
                for(iDir=-1, i=0; i<8; i++) {
1043
                        z2 = getCellValueAsDouble(x + m_iOffsetX[i], y + m_iOffsetY[i]);
1044
                        if(isNoDataValue(z2)) {
1045
                                if (bForceDirToNoDataCell)
1046
                                        return i;
1047
                                else
1048
                                        return -1;
1049
                        }
1050
                        else {
1051
                                dSlope        = (z - z2) / getDistToNeighborInDir(i);
1052
                                if( dSlope > dMaxSlope ) {
1053
                                        iDir = i;
1054
                                        dMaxSlope = dSlope;
1055
                                }
1056
                        }
1057
                }
1058
                return iDir;
1059
        }
1060

    
1061
        public GridCell[] getSortedArrayOfCells()throws GridException {
1062
                int i;
1063
                int iX,iY;
1064
                int iNX =  getNX();
1065
                int iCells = getNX() * getNY();
1066
                GridCell [] cells = null;
1067

    
1068
                cells = new GridCellImpl[iCells];
1069

    
1070
                for (i = 0; i < iCells; i++) {
1071
                        iX = i % iNX;
1072
                        iY = i / iNX;
1073
                        cells[i] = getGridCell(iX, iY);
1074
                }
1075

    
1076
                Arrays.sort(cells);
1077
                return cells;
1078
        }
1079
        
1080
        /*
1081
         * (non-Javadoc)
1082
         * @see org.gvsig.fmap.dal.coverage.grid.Grid#getGridCell(int, int)
1083
         */
1084
        public GridCell getGridCell(int iX, int iY) throws GridException {
1085
                switch(getDataType()){
1086
                case Buffer.TYPE_BYTE: return new GridCellImpl(iX, iY, getCellValueAsByte(iX, iY));
1087
                case Buffer.TYPE_SHORT: return new GridCellImpl(iX, iY, getCellValueAsShort(iX, iY));
1088
                case Buffer.TYPE_INT: return new GridCellImpl(iX, iY, getCellValueAsInt(iX, iY)); 
1089
                case Buffer.TYPE_FLOAT: return new GridCellImpl(iX, iY, getCellValueAsFloat(iX, iY));
1090
                case Buffer.TYPE_DOUBLE: return new GridCellImpl(iX, iY, getCellValueAsDouble(iX, iY));
1091
                }
1092
                return null;
1093
        }
1094

    
1095
        /**
1096
         * Obtiene la lista de paletas asociadas al grid
1097
         * @return Lista de objetos GridPalette
1098
         */
1099
        public ColorTable getPalette() {
1100
                return palette;
1101
        }
1102

    
1103
        /**
1104
         * Obtiene el n?mero de bandas del grid o 0 si no tiene buffer de
1105
         * datos asociado.
1106
         * @return N?mero de bandas
1107
         */
1108
        public int calculateStatistics() {
1109
                if(rasterBuf != null)
1110
                        return rasterBuf.getBandCount();
1111
                return 0;
1112
        }
1113

    
1114
        /**
1115
         * Asigna la lista de paletas asociadas al grid
1116
         * @param palette Lista de objetos GridPalette
1117
         */
1118
        public void setPalette(ColorTable palette) {
1119
                this.palette = palette;
1120
        }
1121

    
1122
        /**
1123
         * Obtiene la lista de filtros.
1124
         * @return Lista de filtros.
1125
         */
1126
        public RasterFilterList getFilterList(){
1127
                return filterList;
1128
        }
1129

    
1130
        /**
1131
         * Asigna la lista de filtros
1132
         * @param filterList
1133
         */
1134
        public void setFilterList(RasterFilterList filterList) {
1135
                this.filterList = filterList;
1136
        }
1137

    
1138
        /**
1139
         *Aplica la lista de filtros sobre el buffer
1140
         * @throws FilterAddException 
1141
         * @throws InterruptedException
1142
         */
1143
        public void applyFilters() throws ProcessInterruptedException, FilterAddException {
1144
                if (filterList == null)
1145
                        return;
1146

    
1147
                filterList.setInitRasterBuf(rasterBuf);
1148

    
1149
                filterList.addEnvParam("GridExtent", getGridExtent());
1150
                filterList.addEnvParam("WindowExtent", getWindowExtent());
1151

    
1152
                filterList.execute();
1153
                if(filterList.getResult() != null)
1154
                        rasterBuf = (RasterBuffer) filterList.getResult();
1155
                dataType = rasterBuf.getDataType();
1156
        }
1157

    
1158
        /*
1159
         * (non-Javadoc)
1160
         * @see org.gvsig.fmap.dal.coverage.grid.Grid#getRasterBuf()
1161
         */
1162
        public Buffer getRasterBuf() {
1163
                return rasterBuf;
1164
        }
1165
        
1166
        /**
1167
         * Obtiene el objeto que calcula las estad?sticas del Grid
1168
         * @return IBasicStats
1169
         */
1170
        public BasicStats getStatistics() {
1171
                return statistic;
1172
        }
1173

    
1174
        /**
1175
         * Asigna la banda sobre la que se realizan las operaciones. Por defecto es la banda 0
1176
         * con lo que para el uso de MDTs no habr? que modificar este valor.
1177
         * @param band Banda sobre la que se realizan las operaciones.
1178
         */
1179
        public void setBandToOperate(int band) {
1180
                if(writer != null)
1181
                        writer.setBandToOperate(band);
1182
                if(reader != null)
1183
                        reader.setBandToOperate(band);
1184
                if(statistic != null)
1185
                        statistic.setBandToOperate(band);
1186
        }
1187

    
1188
        /**
1189
         * Obtiene el extent de la ventana seleccionada para petici?n de datos
1190
         * @return GridExtent
1191
         */
1192
        public GridExtent getWindowExtent() {
1193
                return windowExtent;
1194
        }
1195

    
1196
        /*
1197
         * (non-Javadoc)
1198
         * @see org.gvsig.fmap.dal.coverage.grid.Grid#getGridExtent()
1199
         */
1200
        public GridExtent getGridExtent() {
1201
                return layerExtent;
1202
        }
1203

    
1204
        /*
1205
         * (non-Javadoc)
1206
         * @see org.gvsig.tools.dispose.Disposable#dispose()
1207
         */
1208
        public void dispose() {
1209
                if (rasterBuf != null)
1210
                        rasterBuf.dispose();
1211
                if(reader != null)
1212
                        reader.dispose();
1213
                if(writer != null)
1214
                        writer.dispose();
1215
                if(statistic != null)
1216
                        statistic.dispose();
1217
                //if (filterList != null)
1218
                        //filterList.free();
1219
                if (writer != null)
1220
                        writer.dispose();
1221
                try {
1222
                        finalize();
1223
                } catch (Throwable e) {
1224
                }
1225
        }
1226
        
1227
        /*
1228
         * (non-Javadoc)
1229
         * @see java.lang.Object#finalize()
1230
         */
1231
        protected void finalize() throws Throwable {
1232
                bands = null;
1233
                rasterBuf = null;
1234
                reader = null;
1235
                writer = null;
1236
                windowExtent = null;
1237
                layerExtent = null;
1238
                statistic = null;
1239
                palette = null;
1240
                filterList = null;
1241
                super.finalize();
1242
        }
1243
}