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 10740 nacho
/* 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 11076 nacho
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 10939 nacho
import org.gvsig.raster.dataset.IBuffer;
29 13328 nacho
import org.gvsig.raster.dataset.IRasterDataSource;
30 17686 nbrodin
import org.gvsig.raster.datastruct.Extent;
31 12383 nacho
import org.gvsig.raster.datastruct.Transparency;
32 10740 nacho
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 12166 bsanchez
 *
41 10740 nacho
 * 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 12166 bsanchez
 *
51 10740 nacho
 * @author Victor Olaya (volaya@ya.com)
52
 * @author Nacho Brodin (nachobrodin@gmail.com)
53
 */
54
public class Grid implements IQueryableGrid, IWritableGrid{
55
56 18322 nbrodin
        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 12166 bsanchez
62 18651 nbrodin
        /* neighbor's address*/                               /* N  NE   E  SE   S  SW   W  NW */
63 10740 nacho
        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 12166 bsanchez
66 10740 nacho
        private double                                         m_dDist[];
67
        public double                                         _2DX, _6DX, _DX_2, _4DX_2;
68 12166 bsanchez
69 10740 nacho
        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 12166 bsanchez
83 10740 nacho
        /**
84 11579 nacho
         * 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 12166 bsanchez
         *
87 11579 nacho
         * Cuando se construye el reader se carga el buffer con la extensi?n definida en windowExtent
88
         * y las bandas especificadas en bands.
89 12166 bsanchez
         *
90 13328 nacho
         * @param IRasterDataSource datasets que proporcionan los datos
91 10740 nacho
         * @param bands n?mero de bandas requeridas
92 12166 bsanchez
         * @param windowExtent Extensi?n de la ventana. Si este par?metro es null se usar? el
93 10740 nacho
         * mismo que el de la capa.
94
         */
95 13328 nacho
        public Grid(IRasterDataSource datasets, int[] bands, GridExtent windowExtent)
96 10740 nacho
                        throws RasterBufferInvalidException{
97 17686 nbrodin
                BufferFactory bufferFactory = new BufferFactory(datasets);
98 20871 dguerrero
                double cellSize[] = calcCellSize(bufferFactory.getDataSource(), bufferFactory.getSourceWidth(),
99
                                                                                        bufferFactory.getSourceHeight());
100 17686 nbrodin
                layerExtent = new GridExtent(        bufferFactory.getDataSource().getExtent(),
101 20871 dguerrero
                                                                                cellSize[0],
102
                                                                                cellSize[1]);
103 17686 nbrodin
                if(bufferFactory.getDataSource() != null && bufferFactory.getDataSource().getDataType() != null)
104
                        dataType = bufferFactory.getDataSource().getDataType()[0];
105 10740 nacho
                this.bands = bands;
106 12166 bsanchez
107 10740 nacho
                if(windowExtent == null)
108
                        this.windowExtent = layerExtent;
109
                else
110
                        this.windowExtent = windowExtent;
111 12166 bsanchez
112 22259 nbrodin
                if (this.windowExtent.fitsIn(layerExtent))
113
                        reader = new GridNotInterpolated(bufferFactory, layerExtent, this.windowExtent, bands);
114 21459 nbrodin
                else {
115 22259 nbrodin
                        reader = new GridInterpolated(bufferFactory, layerExtent, this.windowExtent, bands);
116 21459 nbrodin
                }
117 21387 nbrodin
                rasterBuf = (RasterBuffer)bufferFactory.getRasterBuf();
118 10740 nacho
                writer = new GridWriter(layerExtent, dataType, rasterBuf);
119 21459 nbrodin
120 17686 nbrodin
                init(bufferFactory);
121 10740 nacho
        }
122 21459 nbrodin
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 12166 bsanchez
172 10740 nacho
        /**
173 12166 bsanchez
         * Crea un grid vacio a partir de un extent y un tipo de datos. Se crea un buffer vacio en el
174 10740 nacho
         * 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 12166 bsanchez
         * escritura.
177 10740 nacho
         * @param layerExtent Tama?o completo de la capa
178
         * @param windowExtent Ventana de datos.
179
         * @param dataType Tipo de datos del buffer
180 12166 bsanchez
         * @param bands n?mero de bandas requeridas y orden de dibujado en el buffer
181 10740 nacho
         */
182
        public Grid(GridExtent layerExtent,
183
                                GridExtent windowExtent,
184 12166 bsanchez
                                int dataType,
185 10740 nacho
                                int[] bands) throws RasterBufferInvalidException{
186
                this.windowExtent = windowExtent;
187
                this.layerExtent = layerExtent;
188
                this.dataType = dataType;
189 12166 bsanchez
190 11396 nacho
                rasterBuf = RasterBuffer.getBuffer(dataType, layerExtent.getNX(), layerExtent.getNY(), bands.length, true);
191 12166 bsanchez
192 10740 nacho
                if (windowExtent.fitsIn(layerExtent))
193 12166 bsanchez
                        reader = new GridNotInterpolated(rasterBuf, layerExtent, windowExtent, bands);
194 10740 nacho
                else
195
                        reader = new GridInterpolated(rasterBuf, layerExtent, windowExtent, bands);
196
                writer = new GridWriter(layerExtent, dataType, rasterBuf);
197
                init(null);
198
        }
199 12166 bsanchez
200 10740 nacho
        /**
201 11076 nacho
         * Crea un grid a partir de un BufferFactory. El buffer debe estar cargado de datos y el extent
202 10740 nacho
         * de este viene definido en el par?metro windowExtent.
203 13328 nacho
         * @param bufferFactory Fuente de datos
204
         * @param windowExtent        Extent de los datos cargados en bufferFactory
205 10740 nacho
         * @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 13328 nacho
        public Grid(BufferFactory bufferFactory, boolean notInterp) {
209 20871 dguerrero
                double cellSize[] = calcCellSize(bufferFactory.getDataSource(), bufferFactory.getSourceWidth(),
210
                                bufferFactory.getSourceHeight());
211 17686 nbrodin
                this.layerExtent = new GridExtent(        bufferFactory.getDataSource().getExtent(),
212 20871 dguerrero
                                                                                        cellSize[0],
213
                                                                                        cellSize[1]);
214 17686 nbrodin
                if(bufferFactory.getDataSource() != null && bufferFactory.getDataSource().getDataType() != null)
215
                        dataType = bufferFactory.getDataSource().getDataType()[0];
216 13328 nacho
                bands = bufferFactory.getDrawableBands();
217 12166 bsanchez
218 17686 nbrodin
                this.windowExtent = new GridExtent(        bufferFactory.getDataExtent(),
219 20871 dguerrero
                                                                                        cellSize[0],
220
                                                                                        cellSize[1]);
221 12166 bsanchez
222 13328 nacho
                rasterBuf = (RasterBuffer)bufferFactory.getRasterBuf();
223 12166 bsanchez
224 11579 nacho
                if(notInterp) {
225 10740 nacho
                        reader = new GridNotInterpolated(rasterBuf, layerExtent, windowExtent, bands);
226 11579 nacho
                } else {
227 10740 nacho
                        if (windowExtent.fitsIn(layerExtent))
228 12166 bsanchez
                                reader = new GridNotInterpolated(rasterBuf, layerExtent, windowExtent, bands);
229 10740 nacho
                        else
230
                                reader = new GridInterpolated(rasterBuf, layerExtent, windowExtent, bands);
231
                }
232
                writer = new GridWriter(layerExtent, dataType, rasterBuf);
233 13328 nacho
                init(bufferFactory);
234 10740 nacho
        }
235 12166 bsanchez
236 10740 nacho
        /**
237 11076 nacho
         * Crea un grid a partir de un BufferFactory. Se har? una petici?n del extent completo de la fuente.
238 13328 nacho
         * bufferFactory tiene asociada una fuente de datos pero se ignorar? si tiene el buffer lleno. La instanciaci?n
239 12166 bsanchez
         * de GridNotInterpolated har? que se haga la petici?n para cargar raster completo.
240 13328 nacho
         * @param bufferFactory Fuente de datos
241 10740 nacho
         * @param bands n?mero de bandas requeridas y orden de dibujado en el buffer
242
         */
243 13328 nacho
        public Grid(BufferFactory bufferFactory, int[] bands)
244 10740 nacho
                        throws RasterBufferInvalidException{
245
                //La petici?n es del raster completo
246 20871 dguerrero
                double cellSize[] = calcCellSize(bufferFactory.getDataSource(), bufferFactory.getSourceWidth(),
247
                                bufferFactory.getSourceHeight());
248 17686 nbrodin
                windowExtent = layerExtent = new GridExtent(bufferFactory.getDataSource().getExtent(),
249 20871 dguerrero
                                                                                                        cellSize[0],
250
                                                                                                        cellSize[1]);
251 17686 nbrodin
                if(bufferFactory.getDataSource() != null && bufferFactory.getDataSource().getDataType() != null)
252
                        dataType = bufferFactory.getDataSource().getDataType()[0];
253 10740 nacho
                this.bands = bands;
254 12166 bsanchez
255 13328 nacho
                reader = new GridNotInterpolated(bufferFactory, layerExtent, windowExtent, bands);
256
                rasterBuf = (RasterBuffer)bufferFactory.getRasterBuf();
257 10740 nacho
                writer = new GridWriter(windowExtent, dataType, rasterBuf);
258 12166 bsanchez
259 13328 nacho
                init(bufferFactory);
260 10740 nacho
        }
261 12166 bsanchez
262 10740 nacho
        /**
263 11076 nacho
         * Crea un grid a partir de un BufferFactory. Se har? una petici?n del extent completo de la fuente.
264 13328 nacho
         * bufferFactory tiene asociada una fuente de datos pero se ignorar? si tiene el buffer lleno. La instanciaci?n
265 12166 bsanchez
         * de GridNotInterpolated har? que se haga la petici?n para cargar raster completo. El buffer se cargar?
266
         * con todas las bandas disponibles.
267 13328 nacho
         * @param bufferFactory Fuente de datos
268 10740 nacho
         */
269 13328 nacho
        public Grid(BufferFactory bufferFactory) throws RasterBufferInvalidException {
270 10740 nacho
                //La petici?n es del raster completo
271 20871 dguerrero
                double cellSize[] = calcCellSize(bufferFactory.getDataSource(), bufferFactory.getSourceWidth(),
272
                                bufferFactory.getSourceHeight());
273 17686 nbrodin
                windowExtent = layerExtent = new GridExtent(bufferFactory.getDataSource().getExtent(),
274 20871 dguerrero
                                                                                                        cellSize[0],
275
                                                                                                        cellSize[1]);
276 17686 nbrodin
                if(bufferFactory.getDataSource() != null && bufferFactory.getDataSource().getDataType() != null)
277
                        dataType = bufferFactory.getDataSource().getDataType()[0];
278 12166 bsanchez
279 13328 nacho
                bands = new int[bufferFactory.getBandCount()];
280
                for(int i = 0; i < bufferFactory.getBandCount(); i ++)
281 10740 nacho
                        bands[i] = i;
282 13328 nacho
                reader = new GridNotInterpolated(bufferFactory, layerExtent, windowExtent, bands);
283
                rasterBuf = (RasterBuffer)bufferFactory.getRasterBuf();
284 10740 nacho
                writer = new GridWriter(windowExtent, dataType, rasterBuf);
285 12166 bsanchez
286 13328 nacho
                init(bufferFactory);
287 10740 nacho
        }
288 17686 nbrodin
289
        /**
290 20871 dguerrero
         * Calcula el tama?o de celda a partir de un dataset, un ancho y un alto en pixeles
291 17686 nbrodin
         * @return
292
         */
293 20871 dguerrero
        private double[] calcCellSize(IRasterDataSource mDataset, double w, double h) {
294
                double dCellsize[] = new double[2];
295 17686 nbrodin
                try {
296
                        Extent e = mDataset.getExtent();
297 21459 nbrodin
                        dCellsize[0] = (e.getLRX() - e.getULX()) / w;
298
                        dCellsize[1] = (e.getLRY() - e.getULY()) / h;
299 20871 dguerrero
300 17686 nbrodin
                        return dCellsize;
301
                } catch (NullPointerException e) {
302 20871 dguerrero
                        dCellsize[0] = 1;
303
                        dCellsize[1] = 1;
304
                        return dCellsize;
305 17686 nbrodin
                }
306
        }
307 12166 bsanchez
308 10740 nacho
        /**
309
         * Selecciona la forma de obtener datos con o sin interpolaci?n. Si pasamos el par?metro
310 12166 bsanchez
         * false al aplicar un m?todo de consulta de datos este ser? aplicado sin interpolaci?n.
311 10740 nacho
         * @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 12166 bsanchez
322 10740 nacho
        /**
323
         * Inicializaci?n de constantes
324
         */
325 12066 nacho
        private void init(BufferFactory ds) {
326 10740 nacho
                int i;
327 16327 bsanchez
                GridTransparency transparency = null;
328
329 10740 nacho
                double dCellSize = getCellSize();
330 12166 bsanchez
331 10740 nacho
                statistic = new GridStatistic(this);
332 16327 bsanchez
                if (ds == null)
333 10740 nacho
                        transparency = new GridTransparency();
334 12066 nacho
                else {
335 13328 nacho
                        Transparency transp = ds.getDataSource().getTransparencyFilesStatus();
336 16327 bsanchez
                        if (transp != null)
337 10740 nacho
                                transparency = new GridTransparency(transp);
338 12218 nacho
                        this.palette = new GridPalette[ds.getColorTables().length];
339 16327 bsanchez
                        for (int iPal = 0; iPal < ds.getColorTables().length; iPal++) {
340 12567 bsanchez
                                if (ds.getColorTables()[iPal] != null)
341 12218 nacho
                                        this.palette[iPal] = new GridPalette(ds.getColorTables()[iPal]);
342 10740 nacho
                        }
343
                }
344
                filterList = new RasterFilterList();
345 16327 bsanchez
                if (ds != null) {
346 15778 nbrodin
                        filterList.addEnvParam("IStatistics", ds.getDataSource().getStatistics());
347
                        filterList.addEnvParam("MultiRasterDataset", ds.getDataSource());
348 11766 nacho
                }
349 15778 nbrodin
                filterList.addEnvParam("Transparency", transparency);
350 21387 nbrodin
                if(rasterBuf != null)
351
                        filterList.setInitRasterBuf(rasterBuf);
352 12166 bsanchez
353 10740 nacho
                m_dDist = new double[8];
354 12166 bsanchez
355 16327 bsanchez
                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 12166 bsanchez
360 16327 bsanchez
                _2DX = dCellSize * 2.0;
361
                _6DX = dCellSize * 6.0;
362 10740 nacho
                _DX_2 = dCellSize * dCellSize;
363
                _4DX_2 = 4.0 * _DX_2;
364
        }
365 12166 bsanchez
366 10740 nacho
        //************* Write Services *********************
367 12166 bsanchez
368 10740 nacho
        /*
369
         *  (non-Javadoc)
370
         * @see org.gvsig.fmap.grid.IWritableGrid#assign(byte)
371
         */
372 16590 nbrodin
        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 10740 nacho
        }
379 12166 bsanchez
380 10740 nacho
        /*
381
         *  (non-Javadoc)
382
         * @see org.gvsig.fmap.grid.IWritableGrid#assign(short)
383
         */
384 16590 nbrodin
        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 10740 nacho
        }
391 12166 bsanchez
392 10740 nacho
        /*
393
         *  (non-Javadoc)
394
         * @see org.gvsig.fmap.grid.IWritableGrid#assign(int)
395 16590 nbrodin
         */
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 10740 nacho
        }
403 12166 bsanchez
404 10740 nacho
        /*
405
         *  (non-Javadoc)
406
         * @see org.gvsig.fmap.grid.IWritableGrid#assign(float)
407
         */
408 16590 nbrodin
        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 10740 nacho
        }
415 12166 bsanchez
416 10740 nacho
        /*
417
         *  (non-Javadoc)
418
         * @see org.gvsig.fmap.grid.IWritableGrid#assign(double)
419
         */
420 16590 nbrodin
        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 10740 nacho
        }
427 12166 bsanchez
428 10740 nacho
        /*
429
         *  (non-Javadoc)
430 11076 nacho
         * @see org.gvsig.fmap.grid.IWritableGrid#assign(org.gvsig.fmap.dataaccess.BufferFactory)
431 10740 nacho
         */
432 16590 nbrodin
        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 10740 nacho
        }
442 12166 bsanchez
443 10740 nacho
        /*
444
         *  (non-Javadoc)
445
         * @see org.gvsig.fmap.grid.IWritableGrid#assign(org.gvsig.fmap.grid.Grid)
446
         */
447 16590 nbrodin
        public void assign(Grid driver) throws GridException {
448 10740 nacho
                if (driver.getGridExtent().equals(layerExtent)){
449
                        write(driver);
450
                        writer.setNoDataValue(driver.getNoDataValue());
451 12166 bsanchez
                }
452
        }
453
454 10740 nacho
        /**
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 16327 bsanchez
         */
458 16590 nbrodin
        private void write(Grid g) throws GridException {
459 10740 nacho
                try{
460
                        switch(rasterBuf.getDataType()){
461 13328 nacho
                        case IBuffer.TYPE_BYTE: for (int x = 0; x < g.getNX(); x++) {
462
                                                                                for (int y = 0; y < g.getNY(); y++) {
463 10740 nacho
                                                                                        writer.setCellValue(x, y, g.getCellValueAsByte(x, y));
464
                                                                                }
465 12166 bsanchez
                                                                        }
466 10740 nacho
                                                                        break;
467 13328 nacho
                        case IBuffer.TYPE_SHORT:for (int x = 0; x < g.getNX(); x++) {
468
                                                                                for (int y = 0; y < g.getNY(); y++) {
469 10740 nacho
                                                                                        writer.setCellValue(x, y, g.getCellValueAsShort(x, y));
470
                                                                                }
471 12166 bsanchez
                                                                        }
472 10740 nacho
                                                                        break;
473 13328 nacho
                        case IBuffer.TYPE_INT:         for (int x = 0; x < g.getNX(); x++) {
474
                                                                                for (int y = 0; y < g.getNY(); y++) {
475 10740 nacho
                                                                                        writer.setCellValue(x, y, g.getCellValueAsInt(x, y));
476
                                                                                }
477 12166 bsanchez
                                                                        }
478 10740 nacho
                                                                        break;
479 13328 nacho
                        case IBuffer.TYPE_FLOAT:for (int x = 0; x < g.getNX(); x++) {
480
                                                                                for (int y = 0; y < g.getNY(); y++) {
481 10740 nacho
                                                                                        writer.setCellValue(x, y, g.getCellValueAsFloat(x, y));
482
                                                                                }
483 12166 bsanchez
                                                                        }
484 10740 nacho
                                                                        break;
485 13328 nacho
                        case IBuffer.TYPE_DOUBLE:for (int x = 0; x < g.getNX(); x++) {
486
                                                                                for (int y = 0; y < g.getNY(); y++) {
487 10740 nacho
                                                                                        writer.setCellValue(x, y, g.getCellValueAsDouble(x, y));
488
                                                                                }
489 12166 bsanchez
                                                                        }
490 10740 nacho
                                                                        break;
491
                        }
492
                } catch (OutOfGridException e) {
493 16590 nbrodin
                        throw new GridException("");
494
                }
495 10740 nacho
        }
496 12166 bsanchez
497 10740 nacho
        /*
498
         *  (non-Javadoc)
499
         * @see org.gvsig.fmap.grid.IWritableGrid#assignNoData()
500
         */
501 12166 bsanchez
        public void assignNoData(){
502 10740 nacho
                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 12166 bsanchez
517 10740 nacho
        /*
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 12166 bsanchez
525 10740 nacho
        /*
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 12166 bsanchez
533 10740 nacho
        /*
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 12166 bsanchez
541 10740 nacho
        /*
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 12166 bsanchez
549 10740 nacho
        /*
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 12166 bsanchez
557 10740 nacho
        /*
558
         *  (non-Javadoc)
559
         * @see org.gvsig.fmap.grid.IWritableGrid#add(org.gvsig.fmap.grid.Grid)
560
         */
561 16590 nbrodin
        public void add(Grid g) throws GridException {
562
                if (g.getGridExtent().equals(getGridExtent())) {
563 10740 nacho
                        boolean interp = (reader instanceof GridInterpolated);
564
                        switchToInterpolationMethod(false);
565
                        try{
566
                                switch(rasterBuf.getDataType()){
567 16590 nbrodin
                                case IBuffer.TYPE_BYTE: for (int x = 0; x < g.getNX(); x++) {
568
                                                                                        for (int y = 0; y < g.getNY(); y++) {
569 10740 nacho
                                                                                                writer.setCellValue(x, y, reader.getCellValueAsByte(x, y) + g.getCellValueAsByte(x,y));
570
                                                                                        }
571 12166 bsanchez
                                                                                }
572 10740 nacho
                                                                                break;
573 16590 nbrodin
                                case IBuffer.TYPE_SHORT:for (int x = 0; x < g.getNX(); x++) {
574
                                                                                        for (int y = 0; y < g.getNY(); y++) {
575 10740 nacho
                                                                                                writer.setCellValue(x, y, reader.getCellValueAsShort(x, y) + g.getCellValueAsShort(x,y));
576
                                                                                        }
577 12166 bsanchez
                                                                                }
578 10740 nacho
                                                                                break;
579 16590 nbrodin
                                case IBuffer.TYPE_INT:         for (int x = 0; x < g.getNX(); x++) {
580
                                                                                        for (int y = 0; y < g.getNY(); y++) {
581 10740 nacho
                                                                                                writer.setCellValue(x, y, reader.getCellValueAsInt(x, y) + g.getCellValueAsInt(x,y));
582
                                                                                        }
583 12166 bsanchez
                                                                                }
584 10740 nacho
                                                                                break;
585 16590 nbrodin
                                case IBuffer.TYPE_FLOAT:for (int x = 0; x < g.getNX(); x++) {
586
                                                                                        for (int y = 0; y < g.getNY(); y++) {
587 10740 nacho
                                                                                                writer.setCellValue(x, y, reader.getCellValueAsFloat(x, y) + g.getCellValueAsFloat(x,y));
588
                                                                                        }
589 12166 bsanchez
                                                                                }
590 10740 nacho
                                                                                break;
591 16590 nbrodin
                                case IBuffer.TYPE_DOUBLE:for (int x = 0; x < g.getNX(); x++) {
592
                                                                                        for (int y = 0; y < g.getNY(); y++) {
593 10740 nacho
                                                                                                writer.setCellValue(x, y, reader.getCellValueAsDouble(x, y) + g.getCellValueAsDouble(x,y));
594
                                                                                        }
595 12166 bsanchez
                                                                                }
596 10740 nacho
                                                                                break;
597
                                }
598
                        } catch (OutOfGridException e) {
599 16590 nbrodin
                                throw new GridException("");
600 10740 nacho
                        } catch (RasterBufferInvalidAccessException e1) {
601 16590 nbrodin
                                throw new GridException("");
602
                        } catch (RasterBufferInvalidException e) {
603
                                throw new GridException("");
604 10740 nacho
                        }
605
                        switchToInterpolationMethod(interp);
606
                }
607
        }
608 12166 bsanchez
609 10740 nacho
        /*
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 16590 nbrodin
                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 10740 nacho
        }
625 12166 bsanchez
626 10740 nacho
        /*
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 16590 nbrodin
                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 10740 nacho
        }
642 12166 bsanchez
643 10740 nacho
        /*
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 16590 nbrodin
                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 10740 nacho
        }
659 12166 bsanchez
660 10740 nacho
        /*
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 16590 nbrodin
                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 10740 nacho
        }
676 12166 bsanchez
677 10740 nacho
        /*
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 16590 nbrodin
                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 12166 bsanchez
        }
693
694 10740 nacho
        /*
695
         *  (non-Javadoc)
696
         * @see org.gvsig.fmap.grid.IWritableGrid#multiply(double)
697
         */
698 16590 nbrodin
        public void multiply(double value) throws GridException {
699 10740 nacho
                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 12166 bsanchez
                                                                        }
707 10740 nacho
                                                                        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 12166 bsanchez
                                                                        }
712 10740 nacho
                                                                        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 12166 bsanchez
                                                                        }
717 10740 nacho
                                                                        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 12166 bsanchez
                                                                        }
722 10740 nacho
                                                                        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 12166 bsanchez
                                                                        }
727 10740 nacho
                                                                        break;
728
                        }
729 16590 nbrodin
                } catch (RasterBufferInvalidException e) {
730 21459 nbrodin
                        throw new GridException("Buffer de datos no v?lido", e);
731 16590 nbrodin
                } catch (OutOfGridException e1) {
732 21459 nbrodin
                        throw new GridException("Acceso fuera de los l?mites del Grid", e1);
733 16590 nbrodin
                } catch (RasterBufferInvalidAccessException e) {
734 21459 nbrodin
                        throw new GridException("Acceso al grid no v?lido", e);
735 10740 nacho
                }
736
                //Restauramos el reader que hab?a
737
                switchToInterpolationMethod(interp);
738
        }
739 12166 bsanchez
740 10740 nacho
        /*
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 12166 bsanchez
748 10740 nacho
        /*
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 12166 bsanchez
756
        public void ExportToArcViewASCIIFile(String sFilename){
757 10740 nacho
                try {
758
                        writer.ExportToArcViewASCIIFile(sFilename);
759
                } catch (NumberFormatException e) {
760
                        e.printStackTrace();
761
                } catch (IOException e) {
762
                        e.printStackTrace();
763
                }
764
        }
765 12166 bsanchez
766 10740 nacho
        //************* Query Services *********************
767 12166 bsanchez
768 10740 nacho
        /*
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 12166 bsanchez
776 10740 nacho
        /*
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 12166 bsanchez
784 10740 nacho
        /*
785
         *  (non-Javadoc)
786
         * @see org.gvsig.fmap.grid.IQueryableGrid#getCellSize()
787
         */
788
        public double getCellSize() {
789
                return reader.getCellSize();
790
        }
791 12166 bsanchez
792 10740 nacho
        /*
793
         *  (non-Javadoc)
794
         * @see org.gvsig.fmap.grid.IQueryableGrid#getCellValueAsByte(int, int)
795
         */
796 16590 nbrodin
        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 10740 nacho
        }
805 12166 bsanchez
806 10740 nacho
        /*
807
         *  (non-Javadoc)
808
         * @see org.gvsig.fmap.grid.IQueryableGrid#getCellValueAsShort(int, int)
809
         */
810 16590 nbrodin
        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 10740 nacho
        }
819 12166 bsanchez
820 10740 nacho
        /*
821
         *  (non-Javadoc)
822
         * @see org.gvsig.fmap.grid.IQueryableGrid#getCellValueAsInt(int, int)
823
         */
824 16590 nbrodin
        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 10740 nacho
        }
833
834
        /*
835
         *  (non-Javadoc)
836
         * @see org.gvsig.fmap.grid.IQueryableGrid#getCellValueAsFloat(int, int)
837
         */
838 16590 nbrodin
        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 10740 nacho
        }
847 12166 bsanchez
848 10740 nacho
        /*
849
         *  (non-Javadoc)
850
         * @see org.gvsig.fmap.grid.IQueryableGrid#getCellValueAsDouble(int, int)
851
         */
852 16590 nbrodin
        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 10740 nacho
        }
861 22235 nbrodin
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 12166 bsanchez
876 10740 nacho
        /*
877
         *  (non-Javadoc)
878
         * @see org.gvsig.fmap.grid.IQueryableGrid#getNoDataValue()
879
         */
880 16590 nbrodin
        public double getNoDataValue() {
881 10740 nacho
                return (double) rasterBuf.getNoDataValue();
882
        }
883 12166 bsanchez
884 10740 nacho
        /*
885
         *  (non-Javadoc)
886
         * @see org.gvsig.fmap.grid.IQueryableGrid#getMinValue()
887
         */
888 16590 nbrodin
        public double getMinValue() throws GridException {
889 10740 nacho
                if (!statistic.isStatisticsCalculated())
890 16590 nbrodin
                        statistic.calculateStatistics();
891 10740 nacho
                return statistic.getMin();
892
        }
893 12166 bsanchez
894 10740 nacho
        /*
895
         *  (non-Javadoc)
896
         * @see org.gvsig.fmap.grid.IQueryableGrid#getMaxValue()
897
         */
898 16590 nbrodin
        public double getMaxValue() throws GridException {
899 10740 nacho
                if (!statistic.isStatisticsCalculated())
900 16590 nbrodin
                        statistic.calculateStatistics();
901 11837 nacho
                return statistic.getMax();
902 10740 nacho
        }
903 12166 bsanchez
904 10740 nacho
        /*
905
         *  (non-Javadoc)
906
         * @see org.gvsig.fmap.grid.IQueryableGrid#getMeanValue()
907
         */
908 16590 nbrodin
        public double getMeanValue() throws GridException {
909 10740 nacho
                if (!statistic.isStatisticsCalculated())
910 16590 nbrodin
                        statistic.calculateStatistics();
911 11837 nacho
                return statistic.getMean();
912 10740 nacho
        }
913 12166 bsanchez
914 10740 nacho
        /*
915
         *  (non-Javadoc)
916
         * @see org.gvsig.fmap.grid.IQueryableGrid#getVariance()
917
         */
918 16590 nbrodin
        public double getVariance() throws GridException {
919 10740 nacho
                if (!statistic.isStatisticsCalculated())
920 16590 nbrodin
                        statistic.calculateStatistics();
921 10740 nacho
                return statistic.getVariance();
922
        }
923 12166 bsanchez
924 10740 nacho
        /*
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 12166 bsanchez
937 10740 nacho
        /*
938
         *  (non-Javadoc)
939
         * @see org.gvsig.fmap.grid.IQueryableGrid#getNX()
940
         */
941
        public int getNX(){
942
                return reader.getNX();
943
        }
944 12166 bsanchez
945 10740 nacho
        /*
946
         *  (non-Javadoc)
947
         * @see org.gvsig.fmap.grid.IQueryableGrid#getNY()
948
         */
949
        public int getNY(){
950
                return reader.getNY();
951
        }
952 12166 bsanchez
953 10740 nacho
        /*
954
         *  (non-Javadoc)
955
         * @see org.gvsig.fmap.grid.IQueryableGrid#getLayerNX()
956
         */
957
        public int getLayerNX(){
958
                return layerExtent.getNX();
959
        }
960 12166 bsanchez
961 10740 nacho
        /*
962
         *  (non-Javadoc)
963
         * @see org.gvsig.fmap.grid.IQueryableGrid#getLayerNY()
964
         */
965
        public int getLayerNY(){
966
                return layerExtent.getNY();
967
        }
968 12166 bsanchez
969 10740 nacho
        public int getDataType(){
970
                return dataType;
971
        }
972 12166 bsanchez
973 18651 nbrodin
        private boolean getSubMatrix3x3(int x, int y, double subMatrix[])
974 16590 nbrodin
                throws GridException {
975 10740 nacho
                int        i;
976
                int iDir;
977
                double        z, z2;
978
979
                z = getCellValueAsDouble(x, y);
980
981 18651 nbrodin
                if(isNoDataValue(z)) {
982 10740 nacho
                        return false;
983 18651 nbrodin
                } else {
984 10740 nacho
                        //SubMatrix[4]        = 0.0;
985 18651 nbrodin
                        for(i = 0; i < 4; i++) {
986 10740 nacho
                                iDir = 2 * i;
987
                                z2 = getCellValueAsDouble(x + m_iOffsetX[iDir], y + m_iOffsetY[iDir]);
988
                                if( !isNoDataValue(z2)){
989 18651 nbrodin
                                        subMatrix[i] =  z2 - z;
990
                                } else {
991 10740 nacho
                                        z2 = getCellValueAsDouble(x + m_iOffsetX[(iDir + 4) % 8], y + m_iOffsetY[(iDir  + 4) % 8]);
992 18651 nbrodin
                                        if( !isNoDataValue(z2)) {
993
                                                subMatrix[i] = z - z2;
994
                                        } else {
995
                                                subMatrix[i] = 0.0;
996 10740 nacho
                                        }
997
                                }
998
                        }
999
1000
                        return true;
1001
                }
1002
        }
1003 12166 bsanchez
1004 18651 nbrodin
        /*
1005
         * (non-Javadoc)
1006
         * @see org.gvsig.raster.grid.IQueryableGrid#getSlope(int, int)
1007
         */
1008 16590 nbrodin
        public double getSlope(int x, int y)throws GridException {
1009 10740 nacho
                double        zm[], G, H;
1010
1011
                zm = new double[4];
1012 12166 bsanchez
1013 16590 nbrodin
                try {
1014 18651 nbrodin
                        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 16590 nbrodin
                        }
1019 18651 nbrodin
                        else {
1020 16590 nbrodin
                                return rasterBuf.getNoDataValue();
1021
                        }
1022
                } catch (GridException e) {
1023
                        throw new GridException("Problems accesing 3x3 submatrix");
1024 10740 nacho
                }
1025
        }
1026 12166 bsanchez
1027 18651 nbrodin
        /*
1028
         * (non-Javadoc)
1029
         * @see org.gvsig.raster.grid.IQueryableGrid#getAspect(int, int)
1030
         */
1031 16590 nbrodin
        public double getAspect(int x, int y)throws GridException {
1032 10740 nacho
                double        zm[], G, H, dAspect;
1033
                zm = new double[4];
1034 12166 bsanchez
1035 16590 nbrodin
                try {
1036 18651 nbrodin
                        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 16590 nbrodin
                                        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 10740 nacho
                        else
1046 16590 nbrodin
                                return rasterBuf.getNoDataValue();
1047
                } catch (GridException e) {
1048
                        throw new GridException("Problems accesing 3x3 submatrix");
1049 10740 nacho
                }
1050
        }
1051 12166 bsanchez
1052 10740 nacho
        public static int getXOffsetInDir(int iDir){
1053
                return m_iOffsetX[iDir];
1054
        }
1055 12166 bsanchez
1056 10740 nacho
        public static int getYOffsetInDir(int iDir){
1057
                return m_iOffsetY[iDir];
1058
        }
1059 12166 bsanchez
1060 10740 nacho
        public double getDistToNeighborInDir(int iDir){
1061
                return m_dDist[iDir];
1062
        }
1063 12166 bsanchez
1064 10740 nacho
        public static double getUnitDistToNeighborInDir(int iDir){
1065
                return( (iDir % 2 != 0) ? Math.sqrt(2.0)  : 1.0 );
1066
        }
1067 12166 bsanchez
1068 10740 nacho
        /*
1069
         *  (non-Javadoc)
1070
         * @see org.gvsig.fmap.grid.IQueryableGrid#getDirToNextDownslopeCell(int, int)
1071
         */
1072 16590 nbrodin
        public int getDirToNextDownslopeCell(int x, int y)throws GridException{
1073 10740 nacho
                return getDirToNextDownslopeCell(x, y, true);
1074
        }
1075 12166 bsanchez
1076 10740 nacho
        /*
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 16590 nbrodin
                throws GridException{
1082 12166 bsanchez
1083 10740 nacho
                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 16590 nbrodin
                for(iDir=-1, i=0; i<8; i++) {
1094 10740 nacho
                        z2 = getCellValueAsDouble(x + m_iOffsetX[i], y + m_iOffsetY[i]);
1095 16590 nbrodin
                        if(isNoDataValue(z2)) {
1096
                                if (bForceDirToNoDataCell) {
1097 10740 nacho
                                        return i;
1098
                                }
1099 16590 nbrodin
                                else {
1100 10740 nacho
                                        return -1;
1101
                                }
1102
                        }
1103 16590 nbrodin
                        else {
1104 10740 nacho
                                dSlope        = (z - z2) / getDistToNeighborInDir(i);
1105 16590 nbrodin
                                if( dSlope > dMaxSlope ) {
1106 10740 nacho
                                        iDir = i;
1107
                                        dMaxSlope = dSlope;
1108
                                }
1109
                        }
1110
                }
1111
                return iDir;
1112
        }
1113 12166 bsanchez
1114 16590 nbrodin
        public GridCell[] getSortedArrayOfCells()throws GridException{
1115 10740 nacho
                int i;
1116
                int iX,iY;
1117
                int iNX =  getNX();
1118
                int iCells = getNX() * getNY();
1119
                GridCell [] cells = null;
1120
                GridCell cell = null;
1121 12166 bsanchez
1122 10740 nacho
                cells = new GridCell[iCells];
1123 12166 bsanchez
1124 16590 nbrodin
                for (i = 0; i < iCells; i++) {
1125 10740 nacho
                        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 12166 bsanchez
1135 10740 nacho
                        cells[i] = cell;
1136
                }
1137
1138
                Arrays.sort(cells);
1139 12166 bsanchez
1140 10740 nacho
                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 16590 nbrodin
        public GridPalette        getMDTPalette() {
1151 12166 bsanchez
                if(        rasterBuf != null &&
1152 10740 nacho
                        rasterBuf.getBandCount() == 1 &&
1153
                        palette != null)
1154
                        return palette[0];
1155
                return null;
1156
        }
1157 12166 bsanchez
1158 10740 nacho
        /**
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 12166 bsanchez
         * Obtiene el n?mero de bandas del grid o 0 si no tiene buffer de
1168 11961 nacho
         * 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 12166 bsanchez
1177 11961 nacho
        /**
1178 10740 nacho
         * 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 12166 bsanchez
1185 10740 nacho
        /**
1186
         * Obtiene la lista de filtros.
1187
         * @return Lista de filtros.
1188
         */
1189
        public RasterFilterList getFilterList(){
1190
                return filterList;
1191
        }
1192 12166 bsanchez
1193 10740 nacho
        /**
1194 12166 bsanchez
         * Asigna la lista de filtros
1195 10740 nacho
         * @param filterList
1196
         */
1197 16590 nbrodin
        public void setFilterList(RasterFilterList filterList) {
1198 10740 nacho
                this.filterList = filterList;
1199
        }
1200 12166 bsanchez
1201 10740 nacho
        /**
1202
         *Aplica la lista de filtros sobre el buffer
1203 12325 bsanchez
         * @throws InterruptedException
1204 10740 nacho
         */
1205 13409 nacho
        public void applyFilters() throws InterruptedException {
1206 12180 bsanchez
                if (filterList == null)
1207 10740 nacho
                        return;
1208 12180 bsanchez
1209 10740 nacho
                filterList.setInitRasterBuf(rasterBuf);
1210 18358 bsanchez
1211
                filterList.getEnv().put("GridExtent", getGridExtent());
1212
                filterList.getEnv().put("WindowExtent", getWindowExtent());
1213
1214 10740 nacho
                filterList.execute();
1215 12180 bsanchez
                rasterBuf = (RasterBuffer) filterList.getResult();
1216 10740 nacho
                dataType = rasterBuf.getDataType();
1217
        }
1218 12166 bsanchez
1219 10740 nacho
        /**
1220
         * Obtiene el buffer de datos del grid
1221
         * @return RasterBuf
1222
         */
1223
        public IBuffer getRasterBuf() {
1224
                return rasterBuf;
1225
        }
1226 12166 bsanchez
1227 10740 nacho
        /**
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 16590 nbrodin
        public void setBandToOperate(int band) {
1233 10740 nacho
                if(writer != null)
1234
                        writer.setBandToOperate(band);
1235
                if(reader != null)
1236
                        reader.setBandToOperate(band);
1237 11961 nacho
                if(statistic != null)
1238
                        statistic.setBandToOperate(band);
1239 10740 nacho
        }
1240 12166 bsanchez
1241 10740 nacho
        /**
1242
         * Obtiene el extent de la ventana seleccionada para petici?n de datos
1243
         * @return GridExtent
1244
         */
1245 16590 nbrodin
        public GridExtent getWindowExtent() {
1246 10740 nacho
                return windowExtent;
1247
        }
1248 12166 bsanchez
1249 10740 nacho
        /**
1250
         * Obtiene el extent del grid para petici?n de datos
1251
         * @return GridExtent
1252
         */
1253 16590 nbrodin
        public GridExtent getGridExtent() {
1254 10740 nacho
                return layerExtent;
1255
        }
1256
}