Statistics
| Revision:

gvsig-raster / org.gvsig.raster / trunk / org.gvsig.raster / org.gvsig.raster.lib / org.gvsig.raster.lib.impl / src / main / java / org / gvsig / raster / impl / grid / GridImpl.java @ 1315

History | View | Annotate | Download (38.6 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.GridException;
33
import org.gvsig.fmap.dal.coverage.exception.OutOfGridException;
34
import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException;
35
import org.gvsig.fmap.dal.coverage.exception.RasterBufferInvalidAccessException;
36
import org.gvsig.fmap.dal.coverage.exception.RasterBufferInvalidException;
37
import org.gvsig.fmap.dal.coverage.grid.Grid;
38
import org.gvsig.fmap.dal.coverage.grid.RasterFilterList;
39
import org.gvsig.fmap.dal.coverage.store.RasterDataStore;
40
import org.gvsig.fmap.dal.coverage.store.props.BasicStats;
41
import org.gvsig.fmap.dal.coverage.store.props.ColorTable;
42
import org.gvsig.fmap.dal.coverage.store.props.Transparency;
43
import org.gvsig.raster.impl.DefaultRasterManager;
44
import org.gvsig.raster.impl.buffer.RasterBuffer;
45
import org.gvsig.raster.impl.datastruct.ExtentImpl;
46
import org.gvsig.raster.impl.grid.filter.DefaultRasterFilterList;
47
import org.gvsig.raster.impl.store.QueryableRaster;
48
import org.gvsig.raster.impl.store.properties.DataStoreColorTable;
49
import org.gvsig.raster.impl.store.properties.DataStoreTransparency;
50

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

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

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

    
83
        private double                                         m_dDist[];
84
        public double                                         _2DX, _6DX, _DX_2, _4DX_2;
85

    
86
        private int[]                                         bands = null;
87
        private int                                         dataType = Buffer.TYPE_UNDEFINED;
88

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

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

    
122
                if(windowExtent == null)
123
                        this.windowExtent = layerExtent;
124
                else
125
                        this.windowExtent = windowExtent;
126

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

    
134
                init(datasource);
135
        }
136

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

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

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

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

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

    
236
                this.windowExtent = new GridExtentImpl(        new ExtentImpl(buf.getDataExtent()),
237
                                                                                        cellSize[0],
238
                                                                                        cellSize[1]);
239

    
240
                rasterBuf = buf;
241

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

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

    
272
                reader = new GridNotInterpolated(datasource, layerExtent, windowExtent, bands);
273
                rasterBuf = datasource.getLastBuffer();
274
                writer = new GridWriter(windowExtent, dataType, rasterBuf);
275

    
276
                init(datasource);
277
        }
278

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

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

    
304
                init(datasource);
305
        }
306

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

    
318
                        return dCellsize;
319
                } catch (NullPointerException e) {
320
                        dCellsize[0] = 1;
321
                        dCellsize[1] = 1;
322
                        return dCellsize;
323
                }
324
        }
325

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

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

    
347
                double dCellSize = getCellSize();
348

    
349
                statistic = new GridStatistic(this);
350
                if (ds == null)
351
                        transparency = new DataStoreTransparency();
352
                else {
353
                        transparency = ds.getTransparency().cloneTransparency();
354
                        this.palette = new DataStoreColorTable(ds.getColorTable());
355
                }
356
                filterList = new DefaultRasterFilterList();
357
                if (ds != null) {
358
                        filterList.addEnvParam("IStatistics", ds.getStatistics());
359
                        filterList.addEnvParam("MultiRasterDataset", ds);
360
                }
361
                filterList.addEnvParam("Transparency", transparency);
362
                if(rasterBuf != null)
363
                        filterList.setInitRasterBuf(rasterBuf);
364

    
365
                m_dDist = new double[8];
366

    
367
                for (i = 0; i < 8; i++)
368
                        m_dDist[i] = Math.sqrt ( m_iOffsetX[i] * dCellSize * m_iOffsetX[i] * dCellSize
369
                                                                        + m_iOffsetY[i] * dCellSize * m_iOffsetY[i] * dCellSize );
370

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

    
387
        //************* Write Services *********************
388

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
613
        /*
614
         *  (non-Javadoc)
615
         * @see org.gvsig.fmap.grid.IWritableGrid#addToCellValue(int, int, byte)
616
         */
617
        public void addToCellValue(int x, int y, byte value)
618
                throws GridException {
619
                try {
620
                        writer.setCellValue(x, y, (byte)(reader.getCellValueAsByte(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, short)
633
         */
634
        public void addToCellValue(int x, int y, short value)
635
                throws GridException {
636
                try {
637
                        writer.setCellValue(x, y, (short)(reader.getCellValueAsShort(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, int)
650
         */
651
        public void addToCellValue(int x, int y, int value)
652
                throws GridException {
653
                try {
654
                        writer.setCellValue(x, y, (reader.getCellValueAsInt(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#addToCellValue(int, int, float)
667
         */
668
        public void addToCellValue(int x, int y, float value)
669
                throws GridException {
670
                try {
671
                        writer.setCellValue(x, y, (reader.getCellValueAsFloat(x, y) + value));
672
                } catch (OutOfGridException e) {
673
                        throw new GridException("");
674
                } catch (RasterBufferInvalidAccessException e) {
675
                        throw new GridException("");
676
                } catch (RasterBufferInvalidException e) {
677
                        throw new GridException("");
678
                }
679
        }
680

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

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

    
739
        /*
740
         *  (non-Javadoc)
741
         * @see org.gvsig.fmap.grid.IWritableGrid#setNoDataValue(double)
742
         */
743
        public void setNoDataValue(NoData dNoDataValue) {
744
                writer.setNoDataValue(dNoDataValue);
745
        }
746

    
747
        /*
748
         *  (non-Javadoc)
749
         * @see org.gvsig.fmap.grid.IWritableGrid#setNoData(int, int)
750
         */
751
        public void setNoData(int x, int y){
752
                writer.setNoData(x, y);
753
        }
754

    
755
        //************* Query Services *********************
756

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

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

    
773
        /*
774
         *  (non-Javadoc)
775
         * @see org.gvsig.fmap.grid.IQueryableGrid#getCellSize()
776
         */
777
        public double getCellSize() {
778
                return reader.getCellSize();
779
        }
780

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

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

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

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

    
837
        /*
838
         *  (non-Javadoc)
839
         * @see org.gvsig.fmap.grid.IQueryableGrid#getCellValueAsDouble(int, int)
840
         */
841
        public double getCellValueAsDouble(int x, int y)throws GridException {
842
                try {
843
                        return reader.getCellValueAsDouble(x, y);
844
                } catch (RasterBufferInvalidAccessException e) {
845
                        throw new GridException("Position: (" + x + "," + y + ") not valid");
846
                } catch (RasterBufferInvalidException e) {
847
                        throw new GridException("Buffer not valid");
848
                }
849
        }
850

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

    
865
        /*
866
         *  (non-Javadoc)
867
         * @see org.gvsig.fmap.grid.IQueryableGrid#getNoDataValue()
868
         */
869
        public NoData getNoDataValue() {
870
                return rasterBuf.getNoDataValue();
871
        }
872

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

    
886
        /*
887
         *  (non-Javadoc)
888
         * @see org.gvsig.fmap.grid.IQueryableGrid#getNX()
889
         */
890
        public int getNX(){
891
                return reader.getNX();
892
        }
893

    
894
        /*
895
         *  (non-Javadoc)
896
         * @see org.gvsig.fmap.grid.IQueryableGrid#getNY()
897
         */
898
        public int getNY(){
899
                return reader.getNY();
900
        }
901

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

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

    
918
        public int getDataType(){
919
                return dataType;
920
        }
921

    
922
        private boolean getSubMatrix3x3(int x, int y, double subMatrix[])
923
                throws GridException {
924
                int        i;
925
                int iDir;
926
                double        z, z2;
927

    
928
                z = getCellValueAsDouble(x, y);
929

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

    
948
                        return true;
949
                }
950
        }
951

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

    
959
                zm = new double[4];
960

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

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

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

    
998
        public static int getXOffsetInDir(int iDir){
999
                return m_iOffsetX[iDir];
1000
        }
1001

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

    
1006
        public double getDistToNeighborInDir(int iDir){
1007
                return m_dDist[iDir];
1008
        }
1009

    
1010
        public static double getUnitDistToNeighborInDir(int iDir){
1011
                return( (iDir % 2 != 0) ? Math.sqrt(2.0)  : 1.0 );
1012
        }
1013

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

    
1022
        /*
1023
         *  (non-Javadoc)
1024
         * @see org.gvsig.fmap.grid.IQueryableGrid#getDirToNextDownslopeCell(int, int, boolean)
1025
         */
1026
        public int getDirToNextDownslopeCell(int x, int y, boolean bForceDirToNoDataCell)
1027
                throws GridException{
1028

    
1029
                int                i, iDir;
1030
                double        z, z2, dSlope, dMaxSlope;
1031

    
1032
                z = getCellValueAsDouble(x, y);
1033

    
1034
                if(isNoDataValue(z))
1035
                        return -1;
1036

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

    
1057
        public GridCell[] getSortedArrayOfCells()throws GridException {
1058
                int i;
1059
                int iX,iY;
1060
                int iNX =  getNX();
1061
                int iCells = getNX() * getNY();
1062
                GridCell [] cells = null;
1063

    
1064
                cells = new GridCellImpl[iCells];
1065

    
1066
                for (i = 0; i < iCells; i++) {
1067
                        iX = i % iNX;
1068
                        iY = i / iNX;
1069
                        cells[i] = getGridCell(iX, iY);
1070
                }
1071

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

    
1091
        /**
1092
         * Obtiene la lista de paletas asociadas al grid
1093
         * @return Lista de objetos GridPalette
1094
         */
1095
        public ColorTable getPalette() {
1096
                return palette;
1097
        }
1098

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

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

    
1118
        /**
1119
         * Obtiene la lista de filtros.
1120
         * @return Lista de filtros.
1121
         */
1122
        public RasterFilterList getFilterList(){
1123
                return filterList;
1124
        }
1125

    
1126
        /**
1127
         * Asigna la lista de filtros
1128
         * @param filterList
1129
         */
1130
        public void setFilterList(RasterFilterList filterList) {
1131
                this.filterList = filterList;
1132
        }
1133

    
1134
        /**
1135
         *Aplica la lista de filtros sobre el buffer
1136
         * @throws InterruptedException
1137
         */
1138
        public void applyFilters() throws ProcessInterruptedException {
1139
                if (filterList == null)
1140
                        return;
1141

    
1142
                filterList.setInitRasterBuf(rasterBuf);
1143

    
1144
                filterList.addEnvParam("GridExtent", getGridExtent());
1145
                filterList.addEnvParam("WindowExtent", getWindowExtent());
1146

    
1147
                filterList.execute();
1148
                rasterBuf = (RasterBuffer) filterList.getResult();
1149
                dataType = rasterBuf.getDataType();
1150
        }
1151

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

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

    
1182
        /**
1183
         * Obtiene el extent de la ventana seleccionada para petici?n de datos
1184
         * @return GridExtent
1185
         */
1186
        public GridExtent getWindowExtent() {
1187
                return windowExtent;
1188
        }
1189

    
1190
        /*
1191
         * (non-Javadoc)
1192
         * @see org.gvsig.fmap.dal.coverage.grid.Grid#getGridExtent()
1193
         */
1194
        public GridExtent getGridExtent() {
1195
                return layerExtent;
1196
        }
1197

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