Statistics
| Revision:

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

History | View | Annotate | Download (39.3 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2006 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 */
19
package org.gvsig.raster.grid;
20

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

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

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

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

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

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

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

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

    
81

    
82

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

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

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

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

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

    
192
                if (windowExtent.fitsIn(layerExtent))
193
                        reader = new GridNotInterpolated(rasterBuf, layerExtent, windowExtent, bands);
194
                else
195
                        reader = new GridInterpolated(rasterBuf, layerExtent, windowExtent, bands);
196
                writer = new GridWriter(layerExtent, dataType, rasterBuf);
197
                init(null);
198
        }
199

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

    
218
                this.windowExtent = new GridExtent(        bufferFactory.getDataExtent(), 
219
                                                                                        cellSize[0],
220
                                                                                        cellSize[1]);
221

    
222
                rasterBuf = (RasterBuffer)bufferFactory.getRasterBuf();
223

    
224
                if(notInterp) {
225
                        reader = new GridNotInterpolated(rasterBuf, layerExtent, windowExtent, bands);
226
                } else {
227
                        if (windowExtent.fitsIn(layerExtent))
228
                                reader = new GridNotInterpolated(rasterBuf, layerExtent, windowExtent, bands);
229
                        else
230
                                reader = new GridInterpolated(rasterBuf, layerExtent, windowExtent, bands);
231
                }
232
                writer = new GridWriter(layerExtent, dataType, rasterBuf);
233
                init(bufferFactory);
234
        }
235

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

    
255
                reader = new GridNotInterpolated(bufferFactory, layerExtent, windowExtent, bands);
256
                rasterBuf = (RasterBuffer)bufferFactory.getRasterBuf();
257
                writer = new GridWriter(windowExtent, dataType, rasterBuf);
258

    
259
                init(bufferFactory);
260
        }
261

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

    
279
                bands = new int[bufferFactory.getBandCount()];
280
                for(int i = 0; i < bufferFactory.getBandCount(); i ++)
281
                        bands[i] = i;
282
                reader = new GridNotInterpolated(bufferFactory, layerExtent, windowExtent, bands);
283
                rasterBuf = (RasterBuffer)bufferFactory.getRasterBuf();
284
                writer = new GridWriter(windowExtent, dataType, rasterBuf);
285

    
286
                init(bufferFactory);
287
        }
288
        
289
        /**
290
         * Calcula el tama?o de celda a partir de un dataset, un ancho y un alto en pixeles
291
         * @return
292
         */
293
        private double[] calcCellSize(IRasterDataSource mDataset, double w, double h) {
294
                double dCellsize[] = new double[2];
295
                try {
296
                        Extent e = mDataset.getExtent();
297
                        dCellsize[0] = (e.getLRX() - e.getULX()) / w;
298
                        dCellsize[1] = (e.getLRY() - e.getULY()) / h;
299
                        
300
                        return dCellsize;
301
                } catch (NullPointerException e) {
302
                        dCellsize[0] = 1;
303
                        dCellsize[1] = 1;
304
                        return dCellsize;
305
                }
306
        }
307

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

    
322
        /**
323
         * Inicializaci?n de constantes
324
         */
325
        private void init(BufferFactory ds) {
326
                int i;
327
                GridTransparency transparency = null;
328

    
329
                double dCellSize = getCellSize();
330

    
331
                statistic = new GridStatistic(this);
332
                if (ds == null)
333
                        transparency = new GridTransparency();
334
                else {
335
                        Transparency transp = ds.getDataSource().getTransparencyFilesStatus();
336
                        if (transp != null)
337
                                transparency = new GridTransparency(transp);
338
                        this.palette = new GridPalette[ds.getColorTables().length];
339
                        for (int iPal = 0; iPal < ds.getColorTables().length; iPal++) {
340
                                if (ds.getColorTables()[iPal] != null)
341
                                        this.palette[iPal] = new GridPalette(ds.getColorTables()[iPal]);
342
                        }
343
                }
344
                filterList = new RasterFilterList();
345
                if (ds != null) {
346
                        filterList.addEnvParam("IStatistics", ds.getDataSource().getStatistics());
347
                        filterList.addEnvParam("MultiRasterDataset", ds.getDataSource());
348
                }
349
                filterList.addEnvParam("Transparency", transparency);
350
                if(rasterBuf != null)
351
                        filterList.setInitRasterBuf(rasterBuf);
352

    
353
                m_dDist = new double[8];
354

    
355
                for (i = 0; i < 8; i++) {
356
                        m_dDist[i] = Math.sqrt ( m_iOffsetX[i] * dCellSize * m_iOffsetX[i] * dCellSize
357
                                                                        + m_iOffsetY[i] * dCellSize * m_iOffsetY[i] * dCellSize );
358
                }
359

    
360
                _2DX = dCellSize * 2.0;
361
                _6DX = dCellSize * 6.0;
362
                _DX_2 = dCellSize * dCellSize;
363
                _4DX_2 = 4.0 * _DX_2;
364
        }
365

    
366
        //************* Write Services *********************
367

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

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

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

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

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

    
428
        /*
429
         *  (non-Javadoc)
430
         * @see org.gvsig.fmap.grid.IWritableGrid#assign(org.gvsig.fmap.dataaccess.BufferFactory)
431
         */
432
        public void assign(BufferFactory bufferFactory) throws GridException {
433
                Grid window;
434
                try {
435
                        window = new Grid(bufferFactory.getDataSource(), bands, windowExtent);
436
                        write(window);
437
                        writer.setNoDataValue(window.getNoDataValue());
438
                } catch (RasterBufferInvalidException e) {
439
                        throw new GridException("Error writing buffer");
440
                }
441
        }
442

    
443
        /*
444
         *  (non-Javadoc)
445
         * @see org.gvsig.fmap.grid.IWritableGrid#assign(org.gvsig.fmap.grid.Grid)
446
         */
447
        public void assign(Grid driver) throws GridException {
448
                if (driver.getGridExtent().equals(layerExtent)){
449
                        write(driver);
450
                        writer.setNoDataValue(driver.getNoDataValue());
451
                }
452
        }
453

    
454
        /**
455
         * Sobreescribe los datos del grid actual con los datos del grid pasado por par?metro
456
         * @param g Grid desde donde se obtienen los datos
457
         */
458
        private void write(Grid g) throws GridException {
459
                try{
460
                        switch(rasterBuf.getDataType()){
461
                        case IBuffer.TYPE_BYTE: for (int x = 0; x < g.getNX(); x++) {
462
                                                                                for (int y = 0; y < g.getNY(); y++) {
463
                                                                                        writer.setCellValue(x, y, g.getCellValueAsByte(x, y));
464
                                                                                }
465
                                                                        }
466
                                                                        break;
467
                        case IBuffer.TYPE_SHORT:for (int x = 0; x < g.getNX(); x++) {
468
                                                                                for (int y = 0; y < g.getNY(); y++) {
469
                                                                                        writer.setCellValue(x, y, g.getCellValueAsShort(x, y));
470
                                                                                }
471
                                                                        }
472
                                                                        break;
473
                        case IBuffer.TYPE_INT:         for (int x = 0; x < g.getNX(); x++) {
474
                                                                                for (int y = 0; y < g.getNY(); y++) {
475
                                                                                        writer.setCellValue(x, y, g.getCellValueAsInt(x, y));
476
                                                                                }
477
                                                                        }
478
                                                                        break;
479
                        case IBuffer.TYPE_FLOAT:for (int x = 0; x < g.getNX(); x++) {
480
                                                                                for (int y = 0; y < g.getNY(); y++) {
481
                                                                                        writer.setCellValue(x, y, g.getCellValueAsFloat(x, y));
482
                                                                                }
483
                                                                        }
484
                                                                        break;
485
                        case IBuffer.TYPE_DOUBLE:for (int x = 0; x < g.getNX(); x++) {
486
                                                                                for (int y = 0; y < g.getNY(); y++) {
487
                                                                                        writer.setCellValue(x, y, g.getCellValueAsDouble(x, y));
488
                                                                                }
489
                                                                        }
490
                                                                        break;
491
                        }
492
                } catch (OutOfGridException e) {
493
                        throw new GridException("");
494
                } 
495
        }
496

    
497
        /*
498
         *  (non-Javadoc)
499
         * @see org.gvsig.fmap.grid.IWritableGrid#assignNoData()
500
         */
501
        public void assignNoData(){
502
                try {
503
                        switch(rasterBuf.getDataType()){
504
                        case IBuffer.TYPE_BYTE: writer.assign((byte)rasterBuf.getNoDataValue());break;
505
                        case IBuffer.TYPE_SHORT: writer.assign((short)rasterBuf.getNoDataValue());break;
506
                        case IBuffer.TYPE_INT: writer.assign((int)rasterBuf.getNoDataValue());break;
507
                        case IBuffer.TYPE_FLOAT: writer.assign((float)rasterBuf.getNoDataValue());break;
508
                        case IBuffer.TYPE_DOUBLE: writer.assign((double)rasterBuf.getNoDataValue());break;
509
                        }
510
                } catch (RasterBufferInvalidAccessException e) {
511
                        //No hacemos nada. El tipo de dato al que se accede no es controlado por el usuario por lo
512
                        //que no le mandamos la excepci?n hacia arriba.
513
                        e.printStackTrace();
514
                }
515
        }
516

    
517
        /*
518
         *  (non-Javadoc)
519
         * @see org.gvsig.fmap.grid.IWritableGrid#setCellValue(int, int, byte)
520
         */
521
        public void setCellValue(int x, int y, byte value)throws OutOfGridException{
522
                writer.setCellValue(x, y, value);
523
        }
524

    
525
        /*
526
         *  (non-Javadoc)
527
         * @see org.gvsig.fmap.grid.IWritableGrid#setCellValue(int, int, short)
528
         */
529
        public void setCellValue(int x, int y, short value)throws OutOfGridException{
530
                writer.setCellValue(x, y, value);
531
        }
532

    
533
        /*
534
         *  (non-Javadoc)
535
         * @see org.gvsig.fmap.grid.IWritableGrid#setCellValue(int, int, int)
536
         */
537
        public void setCellValue(int x, int y, int 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, float)
544
         */
545
        public void setCellValue(int x, int y, float 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, double)
552
         */
553
        public void setCellValue(int x, int y, double value)throws OutOfGridException{
554
                writer.setCellValue(x, y, value);
555
        }
556

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

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

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

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

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

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

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

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

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

    
756
        public void ExportToArcViewASCIIFile(String sFilename){
757
                try {
758
                        writer.ExportToArcViewASCIIFile(sFilename);
759
                } catch (NumberFormatException e) {
760
                        e.printStackTrace();
761
                } catch (IOException e) {
762
                        e.printStackTrace();
763
                }
764
        }
765

    
766
        //************* Query Services *********************
767

    
768
        /*
769
         *  (non-Javadoc)
770
         * @see org.gvsig.fmap.grid.IQueryableGrid#isNoDataValue(double)
771
         */
772
        public boolean isNoDataValue(double noDataValue){
773
                return (reader.getNoDataValue() == noDataValue);
774
        }
775

    
776
        /*
777
         *  (non-Javadoc)
778
         * @see org.gvsig.fmap.grid.IQueryableGrid#isInGrid(int, int)
779
         */
780
        public boolean isInGrid(int x, int y){
781
                return reader.isCellInGrid(x, y);
782
        }
783

    
784
        /*
785
         *  (non-Javadoc)
786
         * @see org.gvsig.fmap.grid.IQueryableGrid#getCellSize()
787
         */
788
        public double getCellSize() {
789
                return reader.getCellSize();
790
        }
791

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

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

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

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

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

    
876
        /*
877
         *  (non-Javadoc)
878
         * @see org.gvsig.fmap.grid.IQueryableGrid#getNoDataValue()
879
         */
880
        public double getNoDataValue() {
881
                return (double) rasterBuf.getNoDataValue();
882
        }
883

    
884
        /*
885
         *  (non-Javadoc)
886
         * @see org.gvsig.fmap.grid.IQueryableGrid#getMinValue()
887
         */
888
        public double getMinValue() throws GridException {
889
                if (!statistic.isStatisticsCalculated())
890
                        statistic.calculateStatistics();
891
                return statistic.getMin();
892
        }
893

    
894
        /*
895
         *  (non-Javadoc)
896
         * @see org.gvsig.fmap.grid.IQueryableGrid#getMaxValue()
897
         */
898
        public double getMaxValue() throws GridException {
899
                if (!statistic.isStatisticsCalculated())
900
                        statistic.calculateStatistics();
901
                return statistic.getMax();
902
        }
903

    
904
        /*
905
         *  (non-Javadoc)
906
         * @see org.gvsig.fmap.grid.IQueryableGrid#getMeanValue()
907
         */
908
        public double getMeanValue() throws GridException {
909
                if (!statistic.isStatisticsCalculated())
910
                        statistic.calculateStatistics();
911
                return statistic.getMean();
912
        }
913

    
914
        /*
915
         *  (non-Javadoc)
916
         * @see org.gvsig.fmap.grid.IQueryableGrid#getVariance()
917
         */
918
        public double getVariance() throws GridException {
919
                if (!statistic.isStatisticsCalculated())
920
                        statistic.calculateStatistics();
921
                return statistic.getVariance();
922
        }
923

    
924
        /*
925
         *  (non-Javadoc)
926
         * @see org.gvsig.fmap.grid.IQueryableGrid#setInterpolationMethod(int)
927
         */
928
        public void setInterpolationMethod(int iMethod){
929
                if (reader instanceof GridInterpolated)
930
                        ((GridInterpolated) reader).setInterpolationMethod(iMethod);
931
                else{
932
                        this.switchToInterpolationMethod(true);
933
                        ((GridInterpolated) reader).setInterpolationMethod(iMethod);
934
                }
935
        }
936

    
937
        /*
938
         *  (non-Javadoc)
939
         * @see org.gvsig.fmap.grid.IQueryableGrid#getNX()
940
         */
941
        public int getNX(){
942
                return reader.getNX();
943
        }
944

    
945
        /*
946
         *  (non-Javadoc)
947
         * @see org.gvsig.fmap.grid.IQueryableGrid#getNY()
948
         */
949
        public int getNY(){
950
                return reader.getNY();
951
        }
952

    
953
        /*
954
         *  (non-Javadoc)
955
         * @see org.gvsig.fmap.grid.IQueryableGrid#getLayerNX()
956
         */
957
        public int getLayerNX(){
958
                return layerExtent.getNX();
959
        }
960

    
961
        /*
962
         *  (non-Javadoc)
963
         * @see org.gvsig.fmap.grid.IQueryableGrid#getLayerNY()
964
         */
965
        public int getLayerNY(){
966
                return layerExtent.getNY();
967
        }
968

    
969
        public int getDataType(){
970
                return dataType;
971
        }
972

    
973
        private boolean getSubMatrix3x3(int x, int y, double subMatrix[])
974
                throws GridException {
975
                int        i;
976
                int iDir;
977
                double        z, z2;
978

    
979
                z = getCellValueAsDouble(x, y);
980

    
981
                if(isNoDataValue(z)) {
982
                        return false;
983
                } else {
984
                        //SubMatrix[4]        = 0.0;
985
                        for(i = 0; i < 4; i++) {
986
                                iDir = 2 * i;
987
                                z2 = getCellValueAsDouble(x + m_iOffsetX[iDir], y + m_iOffsetY[iDir]);
988
                                if( !isNoDataValue(z2)){
989
                                        subMatrix[i] =  z2 - z;
990
                                } else {
991
                                        z2 = getCellValueAsDouble(x + m_iOffsetX[(iDir + 4) % 8], y + m_iOffsetY[(iDir  + 4) % 8]);
992
                                        if( !isNoDataValue(z2)) {
993
                                                subMatrix[i] = z - z2;
994
                                        } else {
995
                                                subMatrix[i] = 0.0;
996
                                        }
997
                                }
998
                        }
999

    
1000
                        return true;
1001
                }
1002
        }
1003

    
1004
        /*
1005
         * (non-Javadoc)
1006
         * @see org.gvsig.raster.grid.IQueryableGrid#getSlope(int, int)
1007
         */
1008
        public double getSlope(int x, int y)throws GridException {
1009
                double        zm[], G, H;
1010

    
1011
                zm = new double[4];
1012

    
1013
                try {
1014
                        if( getSubMatrix3x3(x, y, zm) ) {
1015
                                G = (zm[0] - zm[2]) / _2DX;
1016
                                H = (zm[1] - zm[3]) / _2DX;
1017
                                return Math.atan(Math.sqrt(G * G + H * H));
1018
                        }
1019
                        else {
1020
                                return rasterBuf.getNoDataValue();
1021
                        }
1022
                } catch (GridException e) {
1023
                        throw new GridException("Problems accesing 3x3 submatrix");
1024
                }
1025
        }
1026

    
1027
        /*
1028
         * (non-Javadoc)
1029
         * @see org.gvsig.raster.grid.IQueryableGrid#getAspect(int, int)
1030
         */
1031
        public double getAspect(int x, int y)throws GridException {
1032
                double        zm[], G, H, dAspect;
1033
                zm = new double[4];
1034

    
1035
                try {
1036
                        if( getSubMatrix3x3(x, y, zm) ) {
1037
                                G = (zm[0] - zm[2]) / _2DX;
1038
                                H = (zm[1] - zm[3]) / _2DX;
1039
                                if(G != 0.0)
1040
                                        dAspect = DEG_180_IN_RAD + Math.atan2(H, G);
1041
                                else
1042
                                        dAspect = H > 0.0 ? DEG_270_IN_RAD : (H < 0.0 ? DEG_90_IN_RAD : -1.0);
1043
                                return dAspect;
1044
                        }
1045
                        else
1046
                                return rasterBuf.getNoDataValue();
1047
                } catch (GridException e) {
1048
                        throw new GridException("Problems accesing 3x3 submatrix");
1049
                }
1050
        }
1051

    
1052
        public static int getXOffsetInDir(int iDir){
1053
                return m_iOffsetX[iDir];
1054
        }
1055

    
1056
        public static int getYOffsetInDir(int iDir){
1057
                return m_iOffsetY[iDir];
1058
        }
1059

    
1060
        public double getDistToNeighborInDir(int iDir){
1061
                return m_dDist[iDir];
1062
        }
1063

    
1064
        public static double getUnitDistToNeighborInDir(int iDir){
1065
                return( (iDir % 2 != 0) ? Math.sqrt(2.0)  : 1.0 );
1066
        }
1067

    
1068
        /*
1069
         *  (non-Javadoc)
1070
         * @see org.gvsig.fmap.grid.IQueryableGrid#getDirToNextDownslopeCell(int, int)
1071
         */
1072
        public int getDirToNextDownslopeCell(int x, int y)throws GridException{
1073
                return getDirToNextDownslopeCell(x, y, true);
1074
        }
1075

    
1076
        /*
1077
         *  (non-Javadoc)
1078
         * @see org.gvsig.fmap.grid.IQueryableGrid#getDirToNextDownslopeCell(int, int, boolean)
1079
         */
1080
        public int getDirToNextDownslopeCell(int x, int y, boolean bForceDirToNoDataCell)
1081
                throws GridException{
1082

    
1083
                int                i, iDir;
1084
                double        z, z2, dSlope, dMaxSlope;
1085

    
1086
                z = getCellValueAsDouble(x, y);
1087

    
1088
                if(isNoDataValue(z)){
1089
                        return -1;
1090
                }
1091

    
1092
                dMaxSlope = 0.0;
1093
                for(iDir=-1, i=0; i<8; i++) {
1094
                        z2 = getCellValueAsDouble(x + m_iOffsetX[i], y + m_iOffsetY[i]);
1095
                        if(isNoDataValue(z2)) {
1096
                                if (bForceDirToNoDataCell) {
1097
                                        return i;
1098
                                }
1099
                                else {
1100
                                        return -1;
1101
                                }
1102
                        }
1103
                        else {
1104
                                dSlope        = (z - z2) / getDistToNeighborInDir(i);
1105
                                if( dSlope > dMaxSlope ) {
1106
                                        iDir = i;
1107
                                        dMaxSlope = dSlope;
1108
                                }
1109
                        }
1110
                }
1111
                return iDir;
1112
        }
1113

    
1114
        public GridCell[] getSortedArrayOfCells()throws GridException{
1115
                int i;
1116
                int iX,iY;
1117
                int iNX =  getNX();
1118
                int iCells = getNX() * getNY();
1119
                GridCell [] cells = null;
1120
                GridCell cell = null;
1121

    
1122
                cells = new GridCell[iCells];
1123

    
1124
                for (i = 0; i < iCells; i++) {
1125
                        iX = i % iNX;
1126
                        iY = i / iNX;
1127
                        switch(getDataType()){
1128
                        case IBuffer.TYPE_BYTE: cell = new GridCell(iX, iY, getCellValueAsByte(iX, iY)); break;
1129
                        case IBuffer.TYPE_SHORT: cell = new GridCell(iX, iY, getCellValueAsShort(iX, iY)); break;
1130
                        case IBuffer.TYPE_INT: cell = new GridCell(iX, iY, getCellValueAsInt(iX, iY)); break;
1131
                        case IBuffer.TYPE_FLOAT: cell = new GridCell(iX, iY, getCellValueAsFloat(iX, iY)); break;
1132
                        case IBuffer.TYPE_DOUBLE: cell = new GridCell(iX, iY, getCellValueAsDouble(iX, iY)); break;
1133
                        }
1134

    
1135
                        cells[i] = cell;
1136
                }
1137

    
1138
                Arrays.sort(cells);
1139

    
1140
                return cells;
1141
        }
1142

    
1143
        /**
1144
         * Obtiene la paleta para el caso de un MDT. Esta funci?n evita el tener que obtener
1145
         * un array de paletas y buscar en el cuando se trata de un caso simple de MDT de una
1146
         * sola banda. Comprueba que se trata de un raster monobanda con paleta asociada antes
1147
         * de devolverla.
1148
         * @return GridPalette asociada al Grid
1149
         */
1150
        public GridPalette        getMDTPalette() {
1151
                if(        rasterBuf != null &&
1152
                        rasterBuf.getBandCount() == 1 &&
1153
                        palette != null)
1154
                        return palette[0];
1155
                return null;
1156
        }
1157

    
1158
        /**
1159
         * Obtiene la lista de paletas asociadas al grid
1160
         * @return Lista de objetos GridPalette
1161
         */
1162
        public GridPalette[] getPalettes() {
1163
                return palette;
1164
        }
1165

    
1166
        /**
1167
         * Obtiene el n?mero de bandas del grid o 0 si no tiene buffer de
1168
         * datos asociado.
1169
         * @return N?mero de bandas
1170
         */
1171
        public int getBandCount() {
1172
                if(rasterBuf != null)
1173
                        return rasterBuf.getBandCount();
1174
                return 0;
1175
        }
1176

    
1177
        /**
1178
         * Asigna la lista de paletas asociadas al grid
1179
         * @param palette Lista de objetos GridPalette
1180
         */
1181
        public void setPalettes(GridPalette[] palette) {
1182
                this.palette = palette;
1183
        }
1184

    
1185
        /**
1186
         * Obtiene la lista de filtros.
1187
         * @return Lista de filtros.
1188
         */
1189
        public RasterFilterList getFilterList(){
1190
                return filterList;
1191
        }
1192

    
1193
        /**
1194
         * Asigna la lista de filtros
1195
         * @param filterList
1196
         */
1197
        public void setFilterList(RasterFilterList filterList) {
1198
                this.filterList = filterList;
1199
        }
1200

    
1201
        /**
1202
         *Aplica la lista de filtros sobre el buffer
1203
         * @throws InterruptedException
1204
         */
1205
        public void applyFilters() throws InterruptedException {
1206
                if (filterList == null)
1207
                        return;
1208

    
1209
                filterList.setInitRasterBuf(rasterBuf);
1210
                
1211
                filterList.getEnv().put("GridExtent", getGridExtent());
1212
                filterList.getEnv().put("WindowExtent", getWindowExtent());
1213
                
1214
                filterList.execute();
1215
                rasterBuf = (RasterBuffer) filterList.getResult();
1216
                dataType = rasterBuf.getDataType();
1217
        }
1218

    
1219
        /**
1220
         * Obtiene el buffer de datos del grid
1221
         * @return RasterBuf
1222
         */
1223
        public IBuffer getRasterBuf() {
1224
                return rasterBuf;
1225
        }
1226

    
1227
        /**
1228
         * Asigna la banda sobre la que se realizan las operaciones. Por defecto es la banda 0
1229
         * con lo que para el uso de MDTs no habr? que modificar este valor.
1230
         * @param band Banda sobre la que se realizan las operaciones.
1231
         */
1232
        public void setBandToOperate(int band) {
1233
                if(writer != null)
1234
                        writer.setBandToOperate(band);
1235
                if(reader != null)
1236
                        reader.setBandToOperate(band);
1237
                if(statistic != null)
1238
                        statistic.setBandToOperate(band);
1239
        }
1240

    
1241
        /**
1242
         * Obtiene el extent de la ventana seleccionada para petici?n de datos
1243
         * @return GridExtent
1244
         */
1245
        public GridExtent getWindowExtent() {
1246
                return windowExtent;
1247
        }
1248

    
1249
        /**
1250
         * Obtiene el extent del grid para petici?n de datos
1251
         * @return GridExtent
1252
         */
1253
        public GridExtent getGridExtent() {
1254
                return layerExtent;
1255
        }
1256
}