Statistics
| Revision:

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

History | View | Annotate | Download (38.6 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 30008 nbrodin
                                                                                        bufferFactory.getSourceHeight());
100
                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 30008 nbrodin
                else
115 22259 nbrodin
                        reader = new GridInterpolated(bufferFactory, layerExtent, this.windowExtent, bands);
116 21387 nbrodin
                rasterBuf = (RasterBuffer)bufferFactory.getRasterBuf();
117 10740 nacho
                writer = new GridWriter(layerExtent, dataType, rasterBuf);
118 30008 nbrodin
119 17686 nbrodin
                init(bufferFactory);
120 10740 nacho
        }
121 30008 nbrodin
122 21459 nbrodin
        /**
123
         * Carga el buffer de datos desde el reader para poder escribir sobre los datos de
124
         * la ventana original. Esto es ?til cuando el GridWriter se crea asociado a un raster
125
         * pero el buffer est? vacio, por ejemplo en el caso de crear un GridInterpolated. Un
126
         * GridInterpolated no carga ning?n buffer en memoria sino que accede a los datos en disco
127
         * a medida que se le van solicitando. Por ello, no es posible modificarlos sino cargamos
128
         * un buffer con los datos previamente.
129
         */
130
        public void loadWriterData() throws GridException {
131
                rasterBuf = RasterBuffer.getBuffer(dataType, reader.getNX(), reader.getNY(), bands.length, true);
132
                writer = new GridWriter(windowExtent, dataType, rasterBuf);
133
                int x = 0, y = 0;
134
                try{
135
                        switch(rasterBuf.getDataType()) {
136 30008 nbrodin
                        case IBuffer.TYPE_BYTE: for (x = 0; x < getNX(); x++)
137
                                for (y = 0; y < getNY(); y++)
138
                                        writer.setCellValue(x, y, (reader.getCellValueAsByte(x, y)));
139 21459 nbrodin
                                                                        break;
140 30008 nbrodin
                        case IBuffer.TYPE_SHORT:for (x = 0; x < getNX(); x++)
141
                                for (y = 0; y < getNY(); y++)
142
                                        writer.setCellValue(x, y, (reader.getCellValueAsShort(x, y)));
143 21459 nbrodin
                                                                        break;
144 30008 nbrodin
                        case IBuffer.TYPE_INT:         for (x = 0; x < getNX(); x++)
145
                                for (y = 0; y < getNY(); y++)
146
                                        writer.setCellValue(x, y, (reader.getCellValueAsInt(x, y)));
147 21459 nbrodin
                                                                        break;
148 30008 nbrodin
                        case IBuffer.TYPE_FLOAT:for (x = 0; x < getNX(); x++)
149
                                for (y = 0; y < getNY(); y++)
150
                                        writer.setCellValue(x, y, (reader.getCellValueAsFloat(x, y)));
151 21459 nbrodin
                                                                        break;
152 30008 nbrodin
                        case IBuffer.TYPE_DOUBLE:for (x = 0; x < getNX(); x++)
153
                                for (y = 0; y < getNY(); y++)
154
                                        writer.setCellValue(x, y, (reader.getCellValueAsDouble(x, y)));
155 21459 nbrodin
                                                                         break;
156
                        }
157
                } catch (RasterBufferInvalidException e) {
158
                        throw new GridException("Buffer de datos no v?lido " + x + " " + y, e);
159
                } catch (OutOfGridException e1) {
160
                        throw new GridException("Acceso fuera de los l?mites del Grid " + x + " " + y, e1);
161
                } catch (RasterBufferInvalidAccessException e) {
162
                        throw new GridException("Acceso al grid no v?lido " + x + " " + y, e);
163
                }
164
        }
165 12166 bsanchez
166 10740 nacho
        /**
167 12166 bsanchez
         * Crea un grid vacio a partir de un extent y un tipo de datos. Se crea un buffer vacio en el
168 10740 nacho
         * que puede escribirse a trav?s del writer. Los datos escritos pueden ser consultados con el
169
         * reader. No tiene una fuente de datos asociada ya que es un grid vacio basicamente para
170 12166 bsanchez
         * escritura.
171 10740 nacho
         * @param layerExtent Tama?o completo de la capa
172
         * @param windowExtent Ventana de datos.
173
         * @param dataType Tipo de datos del buffer
174 12166 bsanchez
         * @param bands n?mero de bandas requeridas y orden de dibujado en el buffer
175 10740 nacho
         */
176
        public Grid(GridExtent layerExtent,
177
                                GridExtent windowExtent,
178 12166 bsanchez
                                int dataType,
179 10740 nacho
                                int[] bands) throws RasterBufferInvalidException{
180
                this.windowExtent = windowExtent;
181
                this.layerExtent = layerExtent;
182
                this.dataType = dataType;
183 12166 bsanchez
184 11396 nacho
                rasterBuf = RasterBuffer.getBuffer(dataType, layerExtent.getNX(), layerExtent.getNY(), bands.length, true);
185 12166 bsanchez
186 10740 nacho
                if (windowExtent.fitsIn(layerExtent))
187 12166 bsanchez
                        reader = new GridNotInterpolated(rasterBuf, layerExtent, windowExtent, bands);
188 10740 nacho
                else
189
                        reader = new GridInterpolated(rasterBuf, layerExtent, windowExtent, bands);
190
                writer = new GridWriter(layerExtent, dataType, rasterBuf);
191
                init(null);
192
        }
193 12166 bsanchez
194 10740 nacho
        /**
195 11076 nacho
         * Crea un grid a partir de un BufferFactory. El buffer debe estar cargado de datos y el extent
196 10740 nacho
         * de este viene definido en el par?metro windowExtent.
197 13328 nacho
         * @param bufferFactory Fuente de datos
198
         * @param windowExtent        Extent de los datos cargados en bufferFactory
199 10740 nacho
         * @param notInterp Si es true fuerza a que el reader sea sin interpolaci?n. Si es false decide si
200
         * el reader es interpolado o no a partir de los extents de capa y la ventana seleccionada.
201
         */
202 13328 nacho
        public Grid(BufferFactory bufferFactory, boolean notInterp) {
203 20871 dguerrero
                double cellSize[] = calcCellSize(bufferFactory.getDataSource(), bufferFactory.getSourceWidth(),
204 30008 nbrodin
                                bufferFactory.getSourceHeight());
205
                this.layerExtent = new GridExtent(        bufferFactory.getDataSource().getExtent(),
206 20871 dguerrero
                                                                                        cellSize[0],
207
                                                                                        cellSize[1]);
208 17686 nbrodin
                if(bufferFactory.getDataSource() != null && bufferFactory.getDataSource().getDataType() != null)
209
                        dataType = bufferFactory.getDataSource().getDataType()[0];
210 13328 nacho
                bands = bufferFactory.getDrawableBands();
211 12166 bsanchez
212 30008 nbrodin
                this.windowExtent = new GridExtent(        bufferFactory.getDataExtent(),
213 20871 dguerrero
                                                                                        cellSize[0],
214
                                                                                        cellSize[1]);
215 12166 bsanchez
216 13328 nacho
                rasterBuf = (RasterBuffer)bufferFactory.getRasterBuf();
217 12166 bsanchez
218 30008 nbrodin
                if(notInterp)
219 10740 nacho
                        reader = new GridNotInterpolated(rasterBuf, layerExtent, windowExtent, bands);
220 30008 nbrodin
                else if (windowExtent.fitsIn(layerExtent))
221
                        reader = new GridNotInterpolated(rasterBuf, layerExtent, windowExtent, bands);
222
                else
223
                        reader = new GridInterpolated(rasterBuf, layerExtent, windowExtent, bands);
224 10740 nacho
                writer = new GridWriter(layerExtent, dataType, rasterBuf);
225 13328 nacho
                init(bufferFactory);
226 10740 nacho
        }
227 12166 bsanchez
228 10740 nacho
        /**
229 11076 nacho
         * Crea un grid a partir de un BufferFactory. Se har? una petici?n del extent completo de la fuente.
230 13328 nacho
         * bufferFactory tiene asociada una fuente de datos pero se ignorar? si tiene el buffer lleno. La instanciaci?n
231 12166 bsanchez
         * de GridNotInterpolated har? que se haga la petici?n para cargar raster completo.
232 13328 nacho
         * @param bufferFactory Fuente de datos
233 10740 nacho
         * @param bands n?mero de bandas requeridas y orden de dibujado en el buffer
234
         */
235 13328 nacho
        public Grid(BufferFactory bufferFactory, int[] bands)
236 10740 nacho
                        throws RasterBufferInvalidException{
237
                //La petici?n es del raster completo
238 20871 dguerrero
                double cellSize[] = calcCellSize(bufferFactory.getDataSource(), bufferFactory.getSourceWidth(),
239 30008 nbrodin
                                bufferFactory.getSourceHeight());
240
                windowExtent = layerExtent = new GridExtent(bufferFactory.getDataSource().getExtent(),
241 20871 dguerrero
                                                                                                        cellSize[0],
242
                                                                                                        cellSize[1]);
243 17686 nbrodin
                if(bufferFactory.getDataSource() != null && bufferFactory.getDataSource().getDataType() != null)
244
                        dataType = bufferFactory.getDataSource().getDataType()[0];
245 10740 nacho
                this.bands = bands;
246 12166 bsanchez
247 13328 nacho
                reader = new GridNotInterpolated(bufferFactory, layerExtent, windowExtent, bands);
248
                rasterBuf = (RasterBuffer)bufferFactory.getRasterBuf();
249 10740 nacho
                writer = new GridWriter(windowExtent, dataType, rasterBuf);
250 12166 bsanchez
251 13328 nacho
                init(bufferFactory);
252 10740 nacho
        }
253 12166 bsanchez
254 10740 nacho
        /**
255 11076 nacho
         * Crea un grid a partir de un BufferFactory. Se har? una petici?n del extent completo de la fuente.
256 13328 nacho
         * bufferFactory tiene asociada una fuente de datos pero se ignorar? si tiene el buffer lleno. La instanciaci?n
257 12166 bsanchez
         * de GridNotInterpolated har? que se haga la petici?n para cargar raster completo. El buffer se cargar?
258
         * con todas las bandas disponibles.
259 13328 nacho
         * @param bufferFactory Fuente de datos
260 10740 nacho
         */
261 13328 nacho
        public Grid(BufferFactory bufferFactory) throws RasterBufferInvalidException {
262 10740 nacho
                //La petici?n es del raster completo
263 20871 dguerrero
                double cellSize[] = calcCellSize(bufferFactory.getDataSource(), bufferFactory.getSourceWidth(),
264 30008 nbrodin
                                bufferFactory.getSourceHeight());
265
                windowExtent = layerExtent = new GridExtent(bufferFactory.getDataSource().getExtent(),
266 20871 dguerrero
                                                                                                        cellSize[0],
267
                                                                                                        cellSize[1]);
268 17686 nbrodin
                if(bufferFactory.getDataSource() != null && bufferFactory.getDataSource().getDataType() != null)
269
                        dataType = bufferFactory.getDataSource().getDataType()[0];
270 12166 bsanchez
271 13328 nacho
                bands = new int[bufferFactory.getBandCount()];
272
                for(int i = 0; i < bufferFactory.getBandCount(); i ++)
273 10740 nacho
                        bands[i] = i;
274 13328 nacho
                reader = new GridNotInterpolated(bufferFactory, layerExtent, windowExtent, bands);
275
                rasterBuf = (RasterBuffer)bufferFactory.getRasterBuf();
276 10740 nacho
                writer = new GridWriter(windowExtent, dataType, rasterBuf);
277 12166 bsanchez
278 13328 nacho
                init(bufferFactory);
279 10740 nacho
        }
280 30008 nbrodin
281 17686 nbrodin
        /**
282 20871 dguerrero
         * Calcula el tama?o de celda a partir de un dataset, un ancho y un alto en pixeles
283 17686 nbrodin
         * @return
284
         */
285 20871 dguerrero
        private double[] calcCellSize(IRasterDataSource mDataset, double w, double h) {
286
                double dCellsize[] = new double[2];
287 17686 nbrodin
                try {
288
                        Extent e = mDataset.getExtent();
289 21459 nbrodin
                        dCellsize[0] = (e.getLRX() - e.getULX()) / w;
290
                        dCellsize[1] = (e.getLRY() - e.getULY()) / h;
291 30008 nbrodin
292 17686 nbrodin
                        return dCellsize;
293
                } catch (NullPointerException e) {
294 20871 dguerrero
                        dCellsize[0] = 1;
295
                        dCellsize[1] = 1;
296
                        return dCellsize;
297 17686 nbrodin
                }
298
        }
299 12166 bsanchez
300 10740 nacho
        /**
301
         * Selecciona la forma de obtener datos con o sin interpolaci?n. Si pasamos el par?metro
302 12166 bsanchez
         * false al aplicar un m?todo de consulta de datos este ser? aplicado sin interpolaci?n.
303 10740 nacho
         * @param interpolation
304
         */
305
        public void switchToInterpolationMethod(boolean interpolation){
306
                //GridExtent layer = new GridExtent(0, 0, rasterBuf.getWidth(), rasterBuf.getHeight(), 1);
307
                if(interpolation)
308
                        reader = new GridInterpolated(rasterBuf, layerExtent, windowExtent, bands);
309
                else
310
                        reader = new GridNotInterpolated(rasterBuf, layerExtent, windowExtent, bands);
311
                init(null);
312
        }
313 12166 bsanchez
314 10740 nacho
        /**
315
         * Inicializaci?n de constantes
316
         */
317 12066 nacho
        private void init(BufferFactory ds) {
318 10740 nacho
                int i;
319 16327 bsanchez
                GridTransparency transparency = null;
320
321 10740 nacho
                double dCellSize = getCellSize();
322 12166 bsanchez
323 10740 nacho
                statistic = new GridStatistic(this);
324 16327 bsanchez
                if (ds == null)
325 10740 nacho
                        transparency = new GridTransparency();
326 12066 nacho
                else {
327 13328 nacho
                        Transparency transp = ds.getDataSource().getTransparencyFilesStatus();
328 16327 bsanchez
                        if (transp != null)
329 10740 nacho
                                transparency = new GridTransparency(transp);
330 12218 nacho
                        this.palette = new GridPalette[ds.getColorTables().length];
331 30008 nbrodin
                        for (int iPal = 0; iPal < ds.getColorTables().length; iPal++)
332 12567 bsanchez
                                if (ds.getColorTables()[iPal] != null)
333 12218 nacho
                                        this.palette[iPal] = new GridPalette(ds.getColorTables()[iPal]);
334 10740 nacho
                }
335
                filterList = new RasterFilterList();
336 16327 bsanchez
                if (ds != null) {
337 15778 nbrodin
                        filterList.addEnvParam("IStatistics", ds.getDataSource().getStatistics());
338
                        filterList.addEnvParam("MultiRasterDataset", ds.getDataSource());
339 11766 nacho
                }
340 15778 nbrodin
                filterList.addEnvParam("Transparency", transparency);
341 21387 nbrodin
                if(rasterBuf != null)
342
                        filterList.setInitRasterBuf(rasterBuf);
343 12166 bsanchez
344 10740 nacho
                m_dDist = new double[8];
345 12166 bsanchez
346 30008 nbrodin
                for (i = 0; i < 8; i++)
347 16327 bsanchez
                        m_dDist[i] = Math.sqrt ( m_iOffsetX[i] * dCellSize * m_iOffsetX[i] * dCellSize
348
                                                                        + m_iOffsetY[i] * dCellSize * m_iOffsetY[i] * dCellSize );
349 12166 bsanchez
350 16327 bsanchez
                _2DX = dCellSize * 2.0;
351
                _6DX = dCellSize * 6.0;
352 10740 nacho
                _DX_2 = dCellSize * dCellSize;
353
                _4DX_2 = 4.0 * _DX_2;
354
        }
355 12166 bsanchez
356 10740 nacho
        //************* Write Services *********************
357 12166 bsanchez
358 10740 nacho
        /*
359
         *  (non-Javadoc)
360
         * @see org.gvsig.fmap.grid.IWritableGrid#assign(byte)
361
         */
362 16590 nbrodin
        public void assign(byte value)throws GridException {
363
                try {
364
                        writer.assign(value);
365
                } catch (RasterBufferInvalidAccessException e) {
366
                        throw new GridException("Error wrinting buffer");
367
                }
368 10740 nacho
        }
369 12166 bsanchez
370 10740 nacho
        /*
371
         *  (non-Javadoc)
372
         * @see org.gvsig.fmap.grid.IWritableGrid#assign(short)
373
         */
374 16590 nbrodin
        public void assign(short value)throws GridException {
375
                try {
376
                        writer.assign(value);
377
                } catch (RasterBufferInvalidAccessException e) {
378
                        throw new GridException("Error wrinting buffer");
379
                }
380 10740 nacho
        }
381 12166 bsanchez
382 10740 nacho
        /*
383
         *  (non-Javadoc)
384
         * @see org.gvsig.fmap.grid.IWritableGrid#assign(int)
385 30008 nbrodin
         */
386 16590 nbrodin
        public void assign(int value)throws GridException {
387
                try {
388
                        writer.assign(value);
389
                } catch (RasterBufferInvalidAccessException e) {
390
                        throw new GridException("Error wrinting buffer");
391
                }
392 10740 nacho
        }
393 12166 bsanchez
394 10740 nacho
        /*
395
         *  (non-Javadoc)
396
         * @see org.gvsig.fmap.grid.IWritableGrid#assign(float)
397
         */
398 16590 nbrodin
        public void assign(float value)throws GridException {
399
                try {
400
                        writer.assign(value);
401
                } catch (RasterBufferInvalidAccessException e) {
402
                        throw new GridException("Error wrinting buffer");
403
                }
404 10740 nacho
        }
405 12166 bsanchez
406 10740 nacho
        /*
407
         *  (non-Javadoc)
408
         * @see org.gvsig.fmap.grid.IWritableGrid#assign(double)
409
         */
410 16590 nbrodin
        public void assign(double value)throws GridException {
411
                try {
412
                        writer.assign(value);
413
                } catch (RasterBufferInvalidAccessException e) {
414
                        throw new GridException("Error wrinting buffer");
415
                }
416 10740 nacho
        }
417 12166 bsanchez
418 10740 nacho
        /*
419
         *  (non-Javadoc)
420 11076 nacho
         * @see org.gvsig.fmap.grid.IWritableGrid#assign(org.gvsig.fmap.dataaccess.BufferFactory)
421 10740 nacho
         */
422 16590 nbrodin
        public void assign(BufferFactory bufferFactory) throws GridException {
423
                Grid window;
424
                try {
425
                        window = new Grid(bufferFactory.getDataSource(), bands, windowExtent);
426
                        write(window);
427
                        writer.setNoDataValue(window.getNoDataValue());
428
                } catch (RasterBufferInvalidException e) {
429
                        throw new GridException("Error writing buffer");
430
                }
431 10740 nacho
        }
432 12166 bsanchez
433 10740 nacho
        /*
434
         *  (non-Javadoc)
435
         * @see org.gvsig.fmap.grid.IWritableGrid#assign(org.gvsig.fmap.grid.Grid)
436
         */
437 16590 nbrodin
        public void assign(Grid driver) throws GridException {
438 10740 nacho
                if (driver.getGridExtent().equals(layerExtent)){
439
                        write(driver);
440
                        writer.setNoDataValue(driver.getNoDataValue());
441 12166 bsanchez
                }
442
        }
443
444 10740 nacho
        /**
445
         * Sobreescribe los datos del grid actual con los datos del grid pasado por par?metro
446
         * @param g Grid desde donde se obtienen los datos
447 16327 bsanchez
         */
448 16590 nbrodin
        private void write(Grid g) throws GridException {
449 10740 nacho
                try{
450
                        switch(rasterBuf.getDataType()){
451 30008 nbrodin
                        case IBuffer.TYPE_BYTE: for (int x = 0; x < g.getNX(); x++)
452
                                for (int y = 0; y < g.getNY(); y++)
453
                                        writer.setCellValue(x, y, g.getCellValueAsByte(x, y));
454 10740 nacho
                                                                        break;
455 30008 nbrodin
                        case IBuffer.TYPE_SHORT:for (int x = 0; x < g.getNX(); x++)
456
                                for (int y = 0; y < g.getNY(); y++)
457
                                        writer.setCellValue(x, y, g.getCellValueAsShort(x, y));
458 10740 nacho
                                                                        break;
459 30008 nbrodin
                        case IBuffer.TYPE_INT:         for (int x = 0; x < g.getNX(); x++)
460
                                for (int y = 0; y < g.getNY(); y++)
461
                                        writer.setCellValue(x, y, g.getCellValueAsInt(x, y));
462 10740 nacho
                                                                        break;
463 30008 nbrodin
                        case IBuffer.TYPE_FLOAT:for (int x = 0; x < g.getNX(); x++)
464
                                for (int y = 0; y < g.getNY(); y++)
465
                                        writer.setCellValue(x, y, g.getCellValueAsFloat(x, y));
466 10740 nacho
                                                                        break;
467 30008 nbrodin
                        case IBuffer.TYPE_DOUBLE:for (int x = 0; x < g.getNX(); x++)
468
                                for (int y = 0; y < g.getNY(); y++)
469
                                        writer.setCellValue(x, y, g.getCellValueAsDouble(x, y));
470 10740 nacho
                                                                        break;
471
                        }
472
                } catch (OutOfGridException e) {
473 16590 nbrodin
                        throw new GridException("");
474 30008 nbrodin
                }
475 10740 nacho
        }
476 12166 bsanchez
477 10740 nacho
        /*
478
         *  (non-Javadoc)
479
         * @see org.gvsig.fmap.grid.IWritableGrid#assignNoData()
480
         */
481 12166 bsanchez
        public void assignNoData(){
482 10740 nacho
                try {
483
                        switch(rasterBuf.getDataType()){
484
                        case IBuffer.TYPE_BYTE: writer.assign((byte)rasterBuf.getNoDataValue());break;
485
                        case IBuffer.TYPE_SHORT: writer.assign((short)rasterBuf.getNoDataValue());break;
486
                        case IBuffer.TYPE_INT: writer.assign((int)rasterBuf.getNoDataValue());break;
487
                        case IBuffer.TYPE_FLOAT: writer.assign((float)rasterBuf.getNoDataValue());break;
488 30008 nbrodin
                        case IBuffer.TYPE_DOUBLE: writer.assign(rasterBuf.getNoDataValue());break;
489 10740 nacho
                        }
490
                } catch (RasterBufferInvalidAccessException e) {
491
                        //No hacemos nada. El tipo de dato al que se accede no es controlado por el usuario por lo
492
                        //que no le mandamos la excepci?n hacia arriba.
493
                        e.printStackTrace();
494
                }
495
        }
496 12166 bsanchez
497 10740 nacho
        /*
498
         *  (non-Javadoc)
499
         * @see org.gvsig.fmap.grid.IWritableGrid#setCellValue(int, int, byte)
500
         */
501
        public void setCellValue(int x, int y, byte value)throws OutOfGridException{
502
                writer.setCellValue(x, y, value);
503
        }
504 12166 bsanchez
505 10740 nacho
        /*
506
         *  (non-Javadoc)
507
         * @see org.gvsig.fmap.grid.IWritableGrid#setCellValue(int, int, short)
508
         */
509
        public void setCellValue(int x, int y, short value)throws OutOfGridException{
510
                writer.setCellValue(x, y, value);
511
        }
512 12166 bsanchez
513 10740 nacho
        /*
514
         *  (non-Javadoc)
515
         * @see org.gvsig.fmap.grid.IWritableGrid#setCellValue(int, int, int)
516
         */
517
        public void setCellValue(int x, int y, int value)throws OutOfGridException{
518
                writer.setCellValue(x, y, value);
519
        }
520 12166 bsanchez
521 10740 nacho
        /*
522
         *  (non-Javadoc)
523
         * @see org.gvsig.fmap.grid.IWritableGrid#setCellValue(int, int, float)
524
         */
525
        public void setCellValue(int x, int y, float value)throws OutOfGridException{
526
                writer.setCellValue(x, y, value);
527
        }
528 12166 bsanchez
529 10740 nacho
        /*
530
         *  (non-Javadoc)
531
         * @see org.gvsig.fmap.grid.IWritableGrid#setCellValue(int, int, double)
532
         */
533
        public void setCellValue(int x, int y, double value)throws OutOfGridException{
534
                writer.setCellValue(x, y, value);
535
        }
536 12166 bsanchez
537 10740 nacho
        /*
538
         *  (non-Javadoc)
539
         * @see org.gvsig.fmap.grid.IWritableGrid#add(org.gvsig.fmap.grid.Grid)
540
         */
541 16590 nbrodin
        public void add(Grid g) throws GridException {
542
                if (g.getGridExtent().equals(getGridExtent())) {
543 10740 nacho
                        boolean interp = (reader instanceof GridInterpolated);
544
                        switchToInterpolationMethod(false);
545
                        try{
546
                                switch(rasterBuf.getDataType()){
547 30008 nbrodin
                                case IBuffer.TYPE_BYTE: for (int x = 0; x < g.getNX(); x++)
548
                                        for (int y = 0; y < g.getNY(); y++)
549
                                                writer.setCellValue(x, y, reader.getCellValueAsByte(x, y) + g.getCellValueAsByte(x,y));
550 10740 nacho
                                                                                break;
551 30008 nbrodin
                                case IBuffer.TYPE_SHORT:for (int x = 0; x < g.getNX(); x++)
552
                                        for (int y = 0; y < g.getNY(); y++)
553
                                                writer.setCellValue(x, y, reader.getCellValueAsShort(x, y) + g.getCellValueAsShort(x,y));
554 10740 nacho
                                                                                break;
555 30008 nbrodin
                                case IBuffer.TYPE_INT:         for (int x = 0; x < g.getNX(); x++)
556
                                        for (int y = 0; y < g.getNY(); y++)
557
                                                writer.setCellValue(x, y, reader.getCellValueAsInt(x, y) + g.getCellValueAsInt(x,y));
558 10740 nacho
                                                                                break;
559 30008 nbrodin
                                case IBuffer.TYPE_FLOAT:for (int x = 0; x < g.getNX(); x++)
560
                                        for (int y = 0; y < g.getNY(); y++)
561
                                                writer.setCellValue(x, y, reader.getCellValueAsFloat(x, y) + g.getCellValueAsFloat(x,y));
562 10740 nacho
                                                                                break;
563 30008 nbrodin
                                case IBuffer.TYPE_DOUBLE:for (int x = 0; x < g.getNX(); x++)
564
                                        for (int y = 0; y < g.getNY(); y++)
565
                                                writer.setCellValue(x, y, reader.getCellValueAsDouble(x, y) + g.getCellValueAsDouble(x,y));
566 10740 nacho
                                                                                break;
567
                                }
568
                        } catch (OutOfGridException e) {
569 16590 nbrodin
                                throw new GridException("");
570 10740 nacho
                        } catch (RasterBufferInvalidAccessException e1) {
571 16590 nbrodin
                                throw new GridException("");
572
                        } catch (RasterBufferInvalidException e) {
573
                                throw new GridException("");
574 10740 nacho
                        }
575
                        switchToInterpolationMethod(interp);
576
                }
577
        }
578 12166 bsanchez
579 10740 nacho
        /*
580
         *  (non-Javadoc)
581
         * @see org.gvsig.fmap.grid.IWritableGrid#addToCellValue(int, int, byte)
582
         */
583
        public void addToCellValue(int x, int y, byte value)
584 16590 nbrodin
                throws GridException {
585
                try {
586
                        writer.setCellValue(x, y, (byte)(reader.getCellValueAsByte(x, y) + value));
587
                } catch (OutOfGridException e) {
588
                        throw new GridException("");
589
                } catch (RasterBufferInvalidAccessException e) {
590
                        throw new GridException("");
591
                } catch (RasterBufferInvalidException e) {
592
                        throw new GridException("");
593
                }
594 10740 nacho
        }
595 12166 bsanchez
596 10740 nacho
        /*
597
         *  (non-Javadoc)
598
         * @see org.gvsig.fmap.grid.IWritableGrid#addToCellValue(int, int, short)
599
         */
600
        public void addToCellValue(int x, int y, short value)
601 16590 nbrodin
                throws GridException {
602
                try {
603
                        writer.setCellValue(x, y, (short)(reader.getCellValueAsShort(x, y) + value));
604
                } catch (OutOfGridException e) {
605
                        throw new GridException("");
606
                } catch (RasterBufferInvalidAccessException e) {
607
                        throw new GridException("");
608
                } catch (RasterBufferInvalidException e) {
609
                        throw new GridException("");
610
                }
611 10740 nacho
        }
612 12166 bsanchez
613 10740 nacho
        /*
614
         *  (non-Javadoc)
615
         * @see org.gvsig.fmap.grid.IWritableGrid#addToCellValue(int, int, int)
616
         */
617
        public void addToCellValue(int x, int y, int value)
618 16590 nbrodin
                throws GridException {
619
                try {
620 30008 nbrodin
                        writer.setCellValue(x, y, (reader.getCellValueAsInt(x, y) + value));
621 16590 nbrodin
                } catch (OutOfGridException e) {
622
                        throw new GridException("");
623
                } catch (RasterBufferInvalidAccessException e) {
624
                        throw new GridException("");
625
                } catch (RasterBufferInvalidException e) {
626
                        throw new GridException("");
627
                }
628 10740 nacho
        }
629 12166 bsanchez
630 10740 nacho
        /*
631
         *  (non-Javadoc)
632
         * @see org.gvsig.fmap.grid.IWritableGrid#addToCellValue(int, int, float)
633
         */
634
        public void addToCellValue(int x, int y, float value)
635 16590 nbrodin
                throws GridException {
636
                try {
637 30008 nbrodin
                        writer.setCellValue(x, y, (reader.getCellValueAsFloat(x, y) + value));
638 16590 nbrodin
                } catch (OutOfGridException e) {
639
                        throw new GridException("");
640
                } catch (RasterBufferInvalidAccessException e) {
641
                        throw new GridException("");
642
                } catch (RasterBufferInvalidException e) {
643
                        throw new GridException("");
644
                }
645 10740 nacho
        }
646 12166 bsanchez
647 10740 nacho
        /*
648
         *  (non-Javadoc)
649
         * @see org.gvsig.fmap.grid.IWritableGrid#addToCellValue(int, int, double)
650
         */
651
        public void addToCellValue(int x, int y, double value)
652 16590 nbrodin
                throws GridException {
653
                try {
654 30008 nbrodin
                        writer.setCellValue(x, y, (reader.getCellValueAsDouble(x, y) + value));
655 16590 nbrodin
                } catch (OutOfGridException e) {
656
                        throw new GridException("");
657
                } catch (RasterBufferInvalidAccessException e) {
658
                        throw new GridException("");
659
                } catch (RasterBufferInvalidException e) {
660
                        throw new GridException("");
661
                }
662 12166 bsanchez
        }
663
664 10740 nacho
        /*
665
         *  (non-Javadoc)
666
         * @see org.gvsig.fmap.grid.IWritableGrid#multiply(double)
667
         */
668 16590 nbrodin
        public void multiply(double value) throws GridException {
669 10740 nacho
                boolean interp = (reader instanceof GridInterpolated);
670
                switchToInterpolationMethod(false);
671
                try{
672
                        switch(rasterBuf.getDataType()){
673 30008 nbrodin
                        case IBuffer.TYPE_BYTE: for (int x = 0; x < getNX(); x++)
674
                                for (int y = 0; y < getNY(); y++)
675
                                        writer.setCellValue(x, y, (byte)(reader.getCellValueAsByte(x, y) * value));
676 10740 nacho
                                                                        break;
677 30008 nbrodin
                        case IBuffer.TYPE_SHORT:for (int x = 0; x < getNX(); x++)
678
                                for (int y = 0; y < getNY(); y++)
679
                                        writer.setCellValue(x, y, (short)(reader.getCellValueAsShort(x, y) * value));
680 10740 nacho
                                                                        break;
681 30008 nbrodin
                        case IBuffer.TYPE_INT:         for (int x = 0; x < getNX(); x++)
682
                                for (int y = 0; y < getNY(); y++)
683
                                        writer.setCellValue(x, y, (int)(reader.getCellValueAsInt(x, y) * value));
684 10740 nacho
                                                                        break;
685 30008 nbrodin
                        case IBuffer.TYPE_FLOAT:for (int x = 0; x < getNX(); x++)
686
                                for (int y = 0; y < getNY(); y++)
687
                                        writer.setCellValue(x, y, (float)(reader.getCellValueAsFloat(x, y) * value));
688 10740 nacho
                                                                        break;
689 30008 nbrodin
                        case IBuffer.TYPE_DOUBLE:for (int x = 0; x < getNX(); x++)
690
                                for (int y = 0; y < getNY(); y++)
691
                                        writer.setCellValue(x, y, (reader.getCellValueAsDouble(x, y) * value));
692 10740 nacho
                                                                        break;
693
                        }
694 16590 nbrodin
                } catch (RasterBufferInvalidException e) {
695 21459 nbrodin
                        throw new GridException("Buffer de datos no v?lido", e);
696 16590 nbrodin
                } catch (OutOfGridException e1) {
697 21459 nbrodin
                        throw new GridException("Acceso fuera de los l?mites del Grid", e1);
698 16590 nbrodin
                } catch (RasterBufferInvalidAccessException e) {
699 21459 nbrodin
                        throw new GridException("Acceso al grid no v?lido", e);
700 10740 nacho
                }
701
                //Restauramos el reader que hab?a
702
                switchToInterpolationMethod(interp);
703
        }
704 12166 bsanchez
705 10740 nacho
        /*
706
         *  (non-Javadoc)
707
         * @see org.gvsig.fmap.grid.IWritableGrid#setNoDataValue(double)
708
         */
709
        public void setNoDataValue(double dNoDataValue){
710
                writer.setNoDataValue((float) dNoDataValue);
711
        }
712 12166 bsanchez
713 10740 nacho
        /*
714
         *  (non-Javadoc)
715
         * @see org.gvsig.fmap.grid.IWritableGrid#setNoData(int, int)
716
         */
717
        public void setNoData(int x, int y){
718
                writer.setNoData(x,y);
719
        }
720 12166 bsanchez
721
        public void ExportToArcViewASCIIFile(String sFilename){
722 10740 nacho
                try {
723
                        writer.ExportToArcViewASCIIFile(sFilename);
724
                } catch (NumberFormatException e) {
725
                        e.printStackTrace();
726
                } catch (IOException e) {
727
                        e.printStackTrace();
728
                }
729
        }
730 12166 bsanchez
731 10740 nacho
        //************* Query Services *********************
732 12166 bsanchez
733 10740 nacho
        /*
734
         *  (non-Javadoc)
735
         * @see org.gvsig.fmap.grid.IQueryableGrid#isNoDataValue(double)
736
         */
737
        public boolean isNoDataValue(double noDataValue){
738
                return (reader.getNoDataValue() == noDataValue);
739
        }
740 12166 bsanchez
741 10740 nacho
        /*
742
         *  (non-Javadoc)
743
         * @see org.gvsig.fmap.grid.IQueryableGrid#isInGrid(int, int)
744
         */
745
        public boolean isInGrid(int x, int y){
746
                return reader.isCellInGrid(x, y);
747
        }
748 12166 bsanchez
749 10740 nacho
        /*
750
         *  (non-Javadoc)
751
         * @see org.gvsig.fmap.grid.IQueryableGrid#getCellSize()
752
         */
753
        public double getCellSize() {
754
                return reader.getCellSize();
755
        }
756 12166 bsanchez
757 10740 nacho
        /*
758
         *  (non-Javadoc)
759
         * @see org.gvsig.fmap.grid.IQueryableGrid#getCellValueAsByte(int, int)
760
         */
761 16590 nbrodin
        public byte getCellValueAsByte(int x, int y)throws GridException {
762
                try {
763
                        return  reader.getCellValueAsByte(x, y);
764
                } catch (RasterBufferInvalidAccessException e) {
765
                        throw new GridException("Position: (" + x + "," + y + ") not valid");
766
                } catch (RasterBufferInvalidException e) {
767
                        throw new GridException("Buffer not valid");
768
                }
769 10740 nacho
        }
770 12166 bsanchez
771 10740 nacho
        /*
772
         *  (non-Javadoc)
773
         * @see org.gvsig.fmap.grid.IQueryableGrid#getCellValueAsShort(int, int)
774
         */
775 16590 nbrodin
        public short getCellValueAsShort(int x, int y)throws GridException {
776
                try {
777 30008 nbrodin
                        return reader.getCellValueAsShort(x, y);
778 16590 nbrodin
                } catch (RasterBufferInvalidAccessException e) {
779
                        throw new GridException("Position: (" + x + "," + y + ") not valid");
780
                } catch (RasterBufferInvalidException e) {
781
                        throw new GridException("Buffer not valid");
782
                }
783 10740 nacho
        }
784 12166 bsanchez
785 10740 nacho
        /*
786
         *  (non-Javadoc)
787
         * @see org.gvsig.fmap.grid.IQueryableGrid#getCellValueAsInt(int, int)
788
         */
789 16590 nbrodin
        public int getCellValueAsInt(int x, int y)throws GridException {
790
                try {
791 30008 nbrodin
                        return reader.getCellValueAsInt(x, y);
792 16590 nbrodin
                } catch (RasterBufferInvalidAccessException e) {
793
                        throw new GridException("Position: (" + x + "," + y + ") not valid");
794
                } catch (RasterBufferInvalidException e) {
795
                        throw new GridException("Buffer not valid");
796
                }
797 10740 nacho
        }
798
799
        /*
800
         *  (non-Javadoc)
801
         * @see org.gvsig.fmap.grid.IQueryableGrid#getCellValueAsFloat(int, int)
802
         */
803 16590 nbrodin
        public float getCellValueAsFloat(int x, int y)throws GridException {
804
                try {
805
                        return reader.getCellValueAsFloat(x, y);
806
                } catch (RasterBufferInvalidAccessException e) {
807
                        throw new GridException("Position: (" + x + "," + y + ") not valid");
808
                } catch (RasterBufferInvalidException e) {
809
                        throw new GridException("Buffer not valid");
810
                }
811 10740 nacho
        }
812 12166 bsanchez
813 10740 nacho
        /*
814
         *  (non-Javadoc)
815
         * @see org.gvsig.fmap.grid.IQueryableGrid#getCellValueAsDouble(int, int)
816
         */
817 16590 nbrodin
        public double getCellValueAsDouble(int x, int y)throws GridException {
818
                try {
819 30008 nbrodin
                        return reader.getCellValueAsDouble(x, y);
820 16590 nbrodin
                } catch (RasterBufferInvalidAccessException e) {
821
                        throw new GridException("Position: (" + x + "," + y + ") not valid");
822
                } catch (RasterBufferInvalidException e) {
823
                        throw new GridException("Buffer not valid");
824
                }
825 10740 nacho
        }
826 30008 nbrodin
827 22235 nbrodin
        /*
828
         *  (non-Javadoc)
829
         * @see org.gvsig.fmap.grid.IQueryableGrid#getCellValueAsDouble(int, int)
830
         */
831
        public double getCellValue(int x, int y)throws GridException {
832
                try {
833 30008 nbrodin
                        return reader.getCellValue(x, y);
834 22235 nbrodin
                } catch (RasterBufferInvalidAccessException e) {
835
                        throw new GridException("Position: (" + x + "," + y + ") not valid");
836
                } catch (RasterBufferInvalidException e) {
837
                        throw new GridException("Buffer not valid");
838
                }
839
        }
840 12166 bsanchez
841 10740 nacho
        /*
842
         *  (non-Javadoc)
843
         * @see org.gvsig.fmap.grid.IQueryableGrid#getNoDataValue()
844
         */
845 16590 nbrodin
        public double getNoDataValue() {
846 30008 nbrodin
                return rasterBuf.getNoDataValue();
847 10740 nacho
        }
848 12166 bsanchez
849 10740 nacho
        /*
850
         *  (non-Javadoc)
851
         * @see org.gvsig.fmap.grid.IQueryableGrid#getMinValue()
852
         */
853 16590 nbrodin
        public double getMinValue() throws GridException {
854 10740 nacho
                if (!statistic.isStatisticsCalculated())
855 16590 nbrodin
                        statistic.calculateStatistics();
856 10740 nacho
                return statistic.getMin();
857
        }
858 12166 bsanchez
859 10740 nacho
        /*
860
         *  (non-Javadoc)
861
         * @see org.gvsig.fmap.grid.IQueryableGrid#getMaxValue()
862
         */
863 16590 nbrodin
        public double getMaxValue() throws GridException {
864 10740 nacho
                if (!statistic.isStatisticsCalculated())
865 16590 nbrodin
                        statistic.calculateStatistics();
866 11837 nacho
                return statistic.getMax();
867 10740 nacho
        }
868 12166 bsanchez
869 10740 nacho
        /*
870
         *  (non-Javadoc)
871
         * @see org.gvsig.fmap.grid.IQueryableGrid#getMeanValue()
872
         */
873 16590 nbrodin
        public double getMeanValue() throws GridException {
874 10740 nacho
                if (!statistic.isStatisticsCalculated())
875 16590 nbrodin
                        statistic.calculateStatistics();
876 11837 nacho
                return statistic.getMean();
877 10740 nacho
        }
878 12166 bsanchez
879 10740 nacho
        /*
880
         *  (non-Javadoc)
881
         * @see org.gvsig.fmap.grid.IQueryableGrid#getVariance()
882
         */
883 16590 nbrodin
        public double getVariance() throws GridException {
884 10740 nacho
                if (!statistic.isStatisticsCalculated())
885 16590 nbrodin
                        statistic.calculateStatistics();
886 10740 nacho
                return statistic.getVariance();
887
        }
888 12166 bsanchez
889 10740 nacho
        /*
890
         *  (non-Javadoc)
891
         * @see org.gvsig.fmap.grid.IQueryableGrid#setInterpolationMethod(int)
892
         */
893
        public void setInterpolationMethod(int iMethod){
894
                if (reader instanceof GridInterpolated)
895
                        ((GridInterpolated) reader).setInterpolationMethod(iMethod);
896
                else{
897
                        this.switchToInterpolationMethod(true);
898
                        ((GridInterpolated) reader).setInterpolationMethod(iMethod);
899
                }
900
        }
901 12166 bsanchez
902 10740 nacho
        /*
903
         *  (non-Javadoc)
904
         * @see org.gvsig.fmap.grid.IQueryableGrid#getNX()
905
         */
906
        public int getNX(){
907
                return reader.getNX();
908
        }
909 12166 bsanchez
910 10740 nacho
        /*
911
         *  (non-Javadoc)
912
         * @see org.gvsig.fmap.grid.IQueryableGrid#getNY()
913
         */
914
        public int getNY(){
915
                return reader.getNY();
916
        }
917 12166 bsanchez
918 10740 nacho
        /*
919
         *  (non-Javadoc)
920
         * @see org.gvsig.fmap.grid.IQueryableGrid#getLayerNX()
921
         */
922
        public int getLayerNX(){
923
                return layerExtent.getNX();
924
        }
925 12166 bsanchez
926 10740 nacho
        /*
927
         *  (non-Javadoc)
928
         * @see org.gvsig.fmap.grid.IQueryableGrid#getLayerNY()
929
         */
930
        public int getLayerNY(){
931
                return layerExtent.getNY();
932
        }
933 12166 bsanchez
934 10740 nacho
        public int getDataType(){
935
                return dataType;
936
        }
937 12166 bsanchez
938 18651 nbrodin
        private boolean getSubMatrix3x3(int x, int y, double subMatrix[])
939 16590 nbrodin
                throws GridException {
940 10740 nacho
                int        i;
941
                int iDir;
942
                double        z, z2;
943
944
                z = getCellValueAsDouble(x, y);
945
946 30008 nbrodin
                if(isNoDataValue(z))
947 10740 nacho
                        return false;
948 30008 nbrodin
                else {
949 10740 nacho
                        //SubMatrix[4]        = 0.0;
950 18651 nbrodin
                        for(i = 0; i < 4; i++) {
951 10740 nacho
                                iDir = 2 * i;
952
                                z2 = getCellValueAsDouble(x + m_iOffsetX[iDir], y + m_iOffsetY[iDir]);
953 30008 nbrodin
                                if( !isNoDataValue(z2))
954 18651 nbrodin
                                        subMatrix[i] =  z2 - z;
955 30008 nbrodin
                                else {
956 10740 nacho
                                        z2 = getCellValueAsDouble(x + m_iOffsetX[(iDir + 4) % 8], y + m_iOffsetY[(iDir  + 4) % 8]);
957 30008 nbrodin
                                        if( !isNoDataValue(z2))
958 18651 nbrodin
                                                subMatrix[i] = z - z2;
959 30008 nbrodin
                                        else
960 18651 nbrodin
                                                subMatrix[i] = 0.0;
961 10740 nacho
                                }
962
                        }
963
964
                        return true;
965
                }
966
        }
967 12166 bsanchez
968 18651 nbrodin
        /*
969
         * (non-Javadoc)
970
         * @see org.gvsig.raster.grid.IQueryableGrid#getSlope(int, int)
971
         */
972 16590 nbrodin
        public double getSlope(int x, int y)throws GridException {
973 10740 nacho
                double        zm[], G, H;
974
975
                zm = new double[4];
976 12166 bsanchez
977 16590 nbrodin
                try {
978 18651 nbrodin
                        if( getSubMatrix3x3(x, y, zm) ) {
979
                                G = (zm[0] - zm[2]) / _2DX;
980
                                H = (zm[1] - zm[3]) / _2DX;
981
                                return Math.atan(Math.sqrt(G * G + H * H));
982 30008 nbrodin
                        } else
983 16590 nbrodin
                                return rasterBuf.getNoDataValue();
984
                } catch (GridException e) {
985
                        throw new GridException("Problems accesing 3x3 submatrix");
986 10740 nacho
                }
987
        }
988 12166 bsanchez
989 18651 nbrodin
        /*
990
         * (non-Javadoc)
991
         * @see org.gvsig.raster.grid.IQueryableGrid#getAspect(int, int)
992
         */
993 16590 nbrodin
        public double getAspect(int x, int y)throws GridException {
994 10740 nacho
                double        zm[], G, H, dAspect;
995
                zm = new double[4];
996 12166 bsanchez
997 16590 nbrodin
                try {
998 18651 nbrodin
                        if( getSubMatrix3x3(x, y, zm) ) {
999
                                G = (zm[0] - zm[2]) / _2DX;
1000
                                H = (zm[1] - zm[3]) / _2DX;
1001
                                if(G != 0.0)
1002 16590 nbrodin
                                        dAspect = DEG_180_IN_RAD + Math.atan2(H, G);
1003
                                else
1004
                                        dAspect = H > 0.0 ? DEG_270_IN_RAD : (H < 0.0 ? DEG_90_IN_RAD : -1.0);
1005
                                return dAspect;
1006
                        }
1007 10740 nacho
                        else
1008 16590 nbrodin
                                return rasterBuf.getNoDataValue();
1009
                } catch (GridException e) {
1010
                        throw new GridException("Problems accesing 3x3 submatrix");
1011 10740 nacho
                }
1012
        }
1013 12166 bsanchez
1014 10740 nacho
        public static int getXOffsetInDir(int iDir){
1015
                return m_iOffsetX[iDir];
1016
        }
1017 12166 bsanchez
1018 10740 nacho
        public static int getYOffsetInDir(int iDir){
1019
                return m_iOffsetY[iDir];
1020
        }
1021 12166 bsanchez
1022 10740 nacho
        public double getDistToNeighborInDir(int iDir){
1023
                return m_dDist[iDir];
1024
        }
1025 12166 bsanchez
1026 10740 nacho
        public static double getUnitDistToNeighborInDir(int iDir){
1027
                return( (iDir % 2 != 0) ? Math.sqrt(2.0)  : 1.0 );
1028
        }
1029 12166 bsanchez
1030 10740 nacho
        /*
1031
         *  (non-Javadoc)
1032
         * @see org.gvsig.fmap.grid.IQueryableGrid#getDirToNextDownslopeCell(int, int)
1033
         */
1034 16590 nbrodin
        public int getDirToNextDownslopeCell(int x, int y)throws GridException{
1035 10740 nacho
                return getDirToNextDownslopeCell(x, y, true);
1036
        }
1037 12166 bsanchez
1038 10740 nacho
        /*
1039
         *  (non-Javadoc)
1040
         * @see org.gvsig.fmap.grid.IQueryableGrid#getDirToNextDownslopeCell(int, int, boolean)
1041
         */
1042
        public int getDirToNextDownslopeCell(int x, int y, boolean bForceDirToNoDataCell)
1043 16590 nbrodin
                throws GridException{
1044 12166 bsanchez
1045 10740 nacho
                int                i, iDir;
1046
                double        z, z2, dSlope, dMaxSlope;
1047
1048
                z = getCellValueAsDouble(x, y);
1049
1050 30008 nbrodin
                if(isNoDataValue(z))
1051 10740 nacho
                        return -1;
1052
1053
                dMaxSlope = 0.0;
1054 16590 nbrodin
                for(iDir=-1, i=0; i<8; i++) {
1055 10740 nacho
                        z2 = getCellValueAsDouble(x + m_iOffsetX[i], y + m_iOffsetY[i]);
1056 16590 nbrodin
                        if(isNoDataValue(z2)) {
1057 30008 nbrodin
                                if (bForceDirToNoDataCell)
1058 10740 nacho
                                        return i;
1059 30008 nbrodin
                                else
1060 10740 nacho
                                        return -1;
1061
                        }
1062 16590 nbrodin
                        else {
1063 10740 nacho
                                dSlope        = (z - z2) / getDistToNeighborInDir(i);
1064 16590 nbrodin
                                if( dSlope > dMaxSlope ) {
1065 10740 nacho
                                        iDir = i;
1066
                                        dMaxSlope = dSlope;
1067
                                }
1068
                        }
1069
                }
1070
                return iDir;
1071
        }
1072 12166 bsanchez
1073 16590 nbrodin
        public GridCell[] getSortedArrayOfCells()throws GridException{
1074 10740 nacho
                int i;
1075
                int iX,iY;
1076
                int iNX =  getNX();
1077
                int iCells = getNX() * getNY();
1078
                GridCell [] cells = null;
1079
                GridCell cell = null;
1080 12166 bsanchez
1081 10740 nacho
                cells = new GridCell[iCells];
1082 12166 bsanchez
1083 16590 nbrodin
                for (i = 0; i < iCells; i++) {
1084 10740 nacho
                        iX = i % iNX;
1085
                        iY = i / iNX;
1086
                        switch(getDataType()){
1087
                        case IBuffer.TYPE_BYTE: cell = new GridCell(iX, iY, getCellValueAsByte(iX, iY)); break;
1088
                        case IBuffer.TYPE_SHORT: cell = new GridCell(iX, iY, getCellValueAsShort(iX, iY)); break;
1089
                        case IBuffer.TYPE_INT: cell = new GridCell(iX, iY, getCellValueAsInt(iX, iY)); break;
1090
                        case IBuffer.TYPE_FLOAT: cell = new GridCell(iX, iY, getCellValueAsFloat(iX, iY)); break;
1091
                        case IBuffer.TYPE_DOUBLE: cell = new GridCell(iX, iY, getCellValueAsDouble(iX, iY)); break;
1092
                        }
1093 12166 bsanchez
1094 10740 nacho
                        cells[i] = cell;
1095
                }
1096
1097
                Arrays.sort(cells);
1098 12166 bsanchez
1099 10740 nacho
                return cells;
1100
        }
1101
1102
        /**
1103
         * Obtiene la paleta para el caso de un MDT. Esta funci?n evita el tener que obtener
1104
         * un array de paletas y buscar en el cuando se trata de un caso simple de MDT de una
1105
         * sola banda. Comprueba que se trata de un raster monobanda con paleta asociada antes
1106
         * de devolverla.
1107
         * @return GridPalette asociada al Grid
1108
         */
1109 16590 nbrodin
        public GridPalette        getMDTPalette() {
1110 12166 bsanchez
                if(        rasterBuf != null &&
1111 10740 nacho
                        rasterBuf.getBandCount() == 1 &&
1112
                        palette != null)
1113
                        return palette[0];
1114
                return null;
1115
        }
1116 12166 bsanchez
1117 10740 nacho
        /**
1118
         * Obtiene la lista de paletas asociadas al grid
1119
         * @return Lista de objetos GridPalette
1120
         */
1121
        public GridPalette[] getPalettes() {
1122
                return palette;
1123
        }
1124
1125
        /**
1126 12166 bsanchez
         * Obtiene el n?mero de bandas del grid o 0 si no tiene buffer de
1127 11961 nacho
         * datos asociado.
1128
         * @return N?mero de bandas
1129
         */
1130
        public int getBandCount() {
1131
                if(rasterBuf != null)
1132
                        return rasterBuf.getBandCount();
1133
                return 0;
1134
        }
1135 12166 bsanchez
1136 11961 nacho
        /**
1137 10740 nacho
         * Asigna la lista de paletas asociadas al grid
1138
         * @param palette Lista de objetos GridPalette
1139
         */
1140
        public void setPalettes(GridPalette[] palette) {
1141
                this.palette = palette;
1142
        }
1143 12166 bsanchez
1144 10740 nacho
        /**
1145
         * Obtiene la lista de filtros.
1146
         * @return Lista de filtros.
1147
         */
1148
        public RasterFilterList getFilterList(){
1149
                return filterList;
1150
        }
1151 12166 bsanchez
1152 10740 nacho
        /**
1153 12166 bsanchez
         * Asigna la lista de filtros
1154 10740 nacho
         * @param filterList
1155
         */
1156 16590 nbrodin
        public void setFilterList(RasterFilterList filterList) {
1157 10740 nacho
                this.filterList = filterList;
1158
        }
1159 12166 bsanchez
1160 10740 nacho
        /**
1161
         *Aplica la lista de filtros sobre el buffer
1162 12325 bsanchez
         * @throws InterruptedException
1163 10740 nacho
         */
1164 13409 nacho
        public void applyFilters() throws InterruptedException {
1165 12180 bsanchez
                if (filterList == null)
1166 10740 nacho
                        return;
1167 12180 bsanchez
1168 10740 nacho
                filterList.setInitRasterBuf(rasterBuf);
1169 30008 nbrodin
1170 18358 bsanchez
                filterList.getEnv().put("GridExtent", getGridExtent());
1171
                filterList.getEnv().put("WindowExtent", getWindowExtent());
1172 30008 nbrodin
1173 10740 nacho
                filterList.execute();
1174 12180 bsanchez
                rasterBuf = (RasterBuffer) filterList.getResult();
1175 10740 nacho
                dataType = rasterBuf.getDataType();
1176
        }
1177 12166 bsanchez
1178 10740 nacho
        /**
1179
         * Obtiene el buffer de datos del grid
1180
         * @return RasterBuf
1181
         */
1182
        public IBuffer getRasterBuf() {
1183
                return rasterBuf;
1184
        }
1185 12166 bsanchez
1186 10740 nacho
        /**
1187
         * Asigna la banda sobre la que se realizan las operaciones. Por defecto es la banda 0
1188
         * con lo que para el uso de MDTs no habr? que modificar este valor.
1189
         * @param band Banda sobre la que se realizan las operaciones.
1190
         */
1191 16590 nbrodin
        public void setBandToOperate(int band) {
1192 10740 nacho
                if(writer != null)
1193
                        writer.setBandToOperate(band);
1194
                if(reader != null)
1195
                        reader.setBandToOperate(band);
1196 11961 nacho
                if(statistic != null)
1197
                        statistic.setBandToOperate(band);
1198 10740 nacho
        }
1199 12166 bsanchez
1200 10740 nacho
        /**
1201
         * Obtiene el extent de la ventana seleccionada para petici?n de datos
1202
         * @return GridExtent
1203
         */
1204 16590 nbrodin
        public GridExtent getWindowExtent() {
1205 10740 nacho
                return windowExtent;
1206
        }
1207 12166 bsanchez
1208 10740 nacho
        /**
1209
         * Obtiene el extent del grid para petici?n de datos
1210
         * @return GridExtent
1211
         */
1212 16590 nbrodin
        public GridExtent getGridExtent() {
1213 10740 nacho
                return layerExtent;
1214
        }
1215 30008 nbrodin
1216
        /**
1217
         * Releases buffer resources
1218
         */
1219
        public void free() {
1220
                if (rasterBuf != null)
1221
                        rasterBuf.free();
1222
                if (filterList != null)
1223
                        filterList.free();
1224
                if (writer != null)
1225
                        writer.free();
1226
        }
1227 10740 nacho
}