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 @ 859

History | View | Annotate | Download (38.1 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

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

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

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

    
136
                init(datasource);
137
        }
138

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

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

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

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

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

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

    
242
                rasterBuf = buf;
243

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

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

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

    
278
                init(datasource);
279
        }
280

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

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

    
306
                init(datasource);
307
        }
308

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

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

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

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

    
349
                double dCellSize = getCellSize();
350

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

    
367
                m_dDist = new double[8];
368

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

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

    
389
        //************* Write Services *********************
390

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
757
        //************* Query Services *********************
758

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
920
        public int getDataType(){
921
                return dataType;
922
        }
923

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

    
930
                z = getCellValueAsDouble(x, y);
931

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

    
950
                        return true;
951
                }
952
        }
953

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

    
961
                zm = new double[4];
962

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

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

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

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

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

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

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

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

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

    
1031
                int                i, iDir;
1032
                double        z, z2, dSlope, dMaxSlope;
1033

    
1034
                z = getCellValueAsDouble(x, y);
1035

    
1036
                if(isNoDataValue(z))
1037
                        return -1;
1038

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

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

    
1066
                cells = new GridCellImpl[iCells];
1067

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

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

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

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

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

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

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

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

    
1144
                filterList.setInitRasterBuf(rasterBuf);
1145

    
1146
                filterList.getEnv().put("GridExtent", getGridExtent());
1147
                filterList.getEnv().put("WindowExtent", getWindowExtent());
1148

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

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

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

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

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

    
1200
        /**
1201
         * Releases buffer resources
1202
         */
1203
        public void free() {
1204
                if (rasterBuf != null)
1205
                        rasterBuf.free();
1206
                //if (filterList != null)
1207
                        //filterList.free();
1208
                if (writer != null)
1209
                        writer.free();
1210
        }
1211
}