Statistics
| Revision:

root / trunk / libraries / libRaster / src / org / gvsig / raster / grid / GridNotInterpolated.java @ 20871

History | View | Annotate | Download (14.2 KB)

1
/*******************************************************************************
2
    GridWrapperNotInterpolated
3
    Copyright (C) Victor Olaya
4
    
5
    This program is free software; you can redistribute it and/or modify
6
    it under the terms of the GNU General Public License as published by
7
    the Free Software Foundation; either version 2 of the License, or
8
    (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

    
20
package org.gvsig.raster.grid;
21

    
22
import org.gvsig.raster.buffer.BufferFactory;
23
import org.gvsig.raster.buffer.RasterBuffer;
24
import org.gvsig.raster.buffer.RasterBufferInvalidAccessException;
25
import org.gvsig.raster.buffer.RasterBufferInvalidException;
26

    
27
/**
28
 * A grid wrapper that does not perform interpolation to 
29
 * calculate cell values. This should be used when the window 
30
 * extent 'fits' into the structure (coordinates and cellsize)
31
 * of the grid, so it is faster than using a grid wrapper with
32
 * interpolation
33
 * 
34
 * Upon construction, cellsizes are not checked, so they are assumed
35
 * to be equal. Use a QueryableGridWindow to safely create a GridWrapper
36
 * better than instantiating this class directly.
37
 * 
38
 * @author Victor Olaya (volaya@ya.com)
39
 */
40

    
41
public class GridNotInterpolated extends GridReader {
42
        
43
        //this offsets are in cells, not in map units.
44
        int m_iOffsetX;
45
        int m_iOffsetY;
46
        
47
        //width of the valid area (the RasterBuffer)
48
        int m_iWidth;
49
        int m_iHeight;
50
        
51
        /**
52
         * Crea un objeto lector a partir de un buffer de datos y el extent de la extensi?n
53
         * completa y de la ventana accedida.
54
         * @param rb Buffer de datos
55
         * @param layerExtent extent de la capa completa
56
         * @param windowExtent Extent
57
         * @param bands N?mero de bandas del origen
58
         */
59
        public GridNotInterpolated(        RasterBuffer rb, 
60
                                                                GridExtent layerExtent,
61
                                                                GridExtent windowExtent,
62
                                                                int[] bands) {
63
                super(rb, layerExtent, windowExtent, bands);
64
                m_iWidth = rb.getWidth();
65
                m_iHeight = rb.getHeight();
66
        }
67
        
68
        /**
69
         * Crea un objeto lector a partir de una fuente de datos y el extent de la extensi?n
70
         * completa y de la ventana accedida. La fuente de datos no tiene bandas asignadas ni ?rea
71
         * de inter?s. Estas son calculadas a partir de los extent pasados por par?metro.
72
         * @param ds Fuente de datos
73
         * @param layerExtent extent de la capa completa
74
         * @param windowExtent Extent
75
         * @param bands N?mero de bandas del origen
76
         */
77
        public GridNotInterpolated(        BufferFactory ds, 
78
                                                                GridExtent layerExtent,
79
                                                                GridExtent windowExtent,
80
                                                                int[] bands) {
81
                super(ds, layerExtent, windowExtent, bands);
82
                init();
83
        }
84
        
85
        /**
86
         * Inicializaci?n de la fuente de datos y carga del buffer.
87
         */
88
        private void init() {
89
                double dMinX, dMaxX, dMinY, dMaxY;
90
                int iWindowMinX, iWindowMinY;        
91
                int iBufMinX, iBufMaxX, iBufMinY, iBufMaxY;        
92
                
93
                iWindowMinX = (int) ((windowExtent.minX() - layerExtent.minX() ) 
94
                                / windowExtent.getCellSizeX());
95
                iWindowMinY = (int) ((layerExtent.maxY() - windowExtent.maxY() ) 
96
                                 / windowExtent.getCellSizeY());
97
                
98
                dMinX = Math.min(Math.max(windowExtent.minX(), layerExtent.minX()), layerExtent.maxX());
99
                dMinY = Math.min(Math.max(windowExtent.minY(), layerExtent.minY()), layerExtent.maxY());
100
                dMaxX = Math.max(Math.min(windowExtent.maxX(), layerExtent.maxX()), layerExtent.minX());
101
                dMaxY = Math.max(Math.min(windowExtent.maxY(), layerExtent.maxY()), layerExtent.minY());
102
                
103
                iBufMinX = (int) Math.floor((dMinX - windowExtent.minX()) / windowExtent.getCellSizeX())
104
                                        + iWindowMinX;
105
                iBufMinY = (int) Math.floor((windowExtent.maxY() - dMaxY) / windowExtent.getCellSizeY())
106
                                        + iWindowMinY;;
107
                iBufMaxX = (int) Math.floor((dMaxX - windowExtent.minX()) / windowExtent.getCellSizeX())
108
                                        + iWindowMinX;
109
                iBufMaxY = (int) Math.floor((windowExtent.maxY() - dMinY) / windowExtent.getCellSizeY())
110
                                        + iWindowMinY;
111
                
112
                m_iOffsetX = iBufMinX - iWindowMinX;
113
                m_iOffsetY = iBufMinY - iWindowMinY;
114
                
115
                m_iWidth = iBufMaxX - iBufMinX ;
116
                m_iHeight = iBufMaxY - iBufMinY ;
117
                        
118
                try {
119
                        bufferFactory.clearDrawableBand();
120
                        bufferFactory.setDrawableBands(bands);
121
                        bufferFactory.setAreaOfInterest(iBufMinX, iBufMinY, m_iWidth, m_iHeight);
122
                        rasterBuf = (RasterBuffer)bufferFactory.getRasterBuf();
123
                } catch (Exception e){
124
                        rasterBuf = null;
125
                }
126
        }        
127
        
128
        /**
129
         * Comprueba si una coordenada pixel est? dentro del grid
130
         * @param x Coordenada X a comprobar
131
         * @param y Coordenada Y a comprobar
132
         * @return true si la coordenada X e Y pasada por par?metro cae dentro del grid
133
         * y false si cae fuera.
134
         */
135
        private boolean isInRasterBuf(int x, int y){
136
                if (rasterBuf != null)
137
                        return x >= 0 && y >= 0 && x <  m_iWidth && y < m_iHeight;
138
                else
139
                        return false;
140
        }
141
        
142
        /*
143
         *  (non-Javadoc)
144
         * @see org.gvsig.fmap.grid.GridReader#getCellValueAsByte(int, int)
145
         */
146
        public byte getCellValueAsByte(int x, int y) throws RasterBufferInvalidAccessException, RasterBufferInvalidException {
147
                try{
148
                        if (isInRasterBuf(x - m_iOffsetX, y -  m_iOffsetY))
149
                                return rasterBuf.getElemByte(y -  m_iOffsetY, x - m_iOffsetX, bandToOperate);
150
                        else
151
                                return (byte) rasterBuf.getNoDataValue(); 
152
                }catch(ArrayIndexOutOfBoundsException e){
153
                        throw new RasterBufferInvalidAccessException("Access to data type " + dataType + "invalid");
154
                }catch(NullPointerException e){
155
                        throw new RasterBufferInvalidException("Null Buffer");
156
                }                
157
        }
158
        
159
        /*
160
         *  (non-Javadoc)
161
         * @see org.gvsig.fmap.grid.GridReader#getCellValueAsShort(int, int)
162
         */
163
        public short getCellValueAsShort(int x, int y) throws RasterBufferInvalidAccessException, RasterBufferInvalidException {
164
                try{
165
                        if (isInRasterBuf(x - m_iOffsetX, y -  m_iOffsetY))
166
                                return rasterBuf.getElemShort(y -  m_iOffsetY, x - m_iOffsetX, bandToOperate);
167
                        else
168
                                return (short)rasterBuf.getNoDataValue();
169
                }catch(ArrayIndexOutOfBoundsException e){
170
                        throw new RasterBufferInvalidAccessException("Access to data type " + dataType + "invalid");
171
                }catch(NullPointerException e){
172
                        throw new RasterBufferInvalidException("Null Buffer");
173
                }
174
        }
175
        
176
        /*
177
         *  (non-Javadoc)
178
         * @see org.gvsig.fmap.grid.GridReader#getCellValueAsInt(int, int)
179
         */
180
        public int getCellValueAsInt(int x, int y) throws RasterBufferInvalidAccessException, RasterBufferInvalidException {
181
                try{
182
                        if (isInRasterBuf(x - m_iOffsetX, y -  m_iOffsetY))
183
                                return rasterBuf.getElemInt(y -  m_iOffsetY, x - m_iOffsetX, bandToOperate);
184
                        else
185
                                return (int) rasterBuf.getNoDataValue();
186
                }catch(ArrayIndexOutOfBoundsException e){
187
                        throw new RasterBufferInvalidAccessException("Access to data type " + dataType + "invalid");
188
                }catch(NullPointerException e){
189
                        throw new RasterBufferInvalidException("Null Buffer");
190
                }                
191
        }
192

    
193
        /*
194
         *  (non-Javadoc)
195
         * @see org.gvsig.fmap.grid.GridReader#getCellValueAsFloat(int, int)
196
         */
197
        public float getCellValueAsFloat(int x, int y) throws RasterBufferInvalidAccessException, RasterBufferInvalidException {
198
                try{
199
                        if (isInRasterBuf(x - m_iOffsetX, y -  m_iOffsetY))
200
                                return rasterBuf.getElemFloat(y -  m_iOffsetY, x - m_iOffsetX, bandToOperate);
201
                        else
202
                                return (float) rasterBuf.getNoDataValue();
203
                }catch(ArrayIndexOutOfBoundsException e){
204
                        throw new RasterBufferInvalidAccessException("Access to data type " + dataType + "invalid");
205
                }catch(NullPointerException e){
206
                        throw new RasterBufferInvalidException("Null Buffer");
207
                }
208
        }
209
        
210
        /*
211
         *  (non-Javadoc)
212
         * @see org.gvsig.fmap.grid.GridReader#getCellValueAsDouble(int, int)
213
         */
214
        public double getCellValueAsDouble(int x, int y) throws RasterBufferInvalidAccessException, RasterBufferInvalidException {
215
                try{
216
                        if (isInRasterBuf(x - m_iOffsetX, y -  m_iOffsetY))
217
                                return rasterBuf.getElemDouble(y -  m_iOffsetY, x - m_iOffsetX, bandToOperate);
218
                        else
219
                                return rasterBuf.getNoDataValue();
220
                }catch(ArrayIndexOutOfBoundsException e){
221
                        throw new RasterBufferInvalidAccessException("Access to data type " + dataType + "invalid");
222
                }catch(NullPointerException e){
223
                        throw new RasterBufferInvalidException("Null Buffer");
224
                }
225
        }
226
        
227
        /**
228
         * Obtiene el valor de una celda en double.
229
         * @param x Posici?n X del valor que queremos recuperar
230
         * @param y Posici?n Y del valor que queremos recuperar
231
         * @return Valor de tipo double contenido en la posici?n especificada
232
         */
233
        /*private double _getCellValue(int x, int yRasterBufferInvalidAccessException)throws RasterBufferInvalidAccessException{
234
                try{
235
                        if (dataType == RasterBuffer.TYPE_DOUBLE) {
236
                        return rasterBuf.getElemDouble(x, y, 0);
237
                } else if (dataType == RasterBuffer.TYPE_INT) {
238
                        return (double) rasterBuf.getElemInt(x, y, 0);
239
                } else if (dataType == RasterBuffer.TYPE_FLOAT) {
240
                        return (double) rasterBuf.getElemFloat(x, y, 0);
241
                } else if (dataType == RasterBuffer.TYPE_BYTE) {
242
                        return (double) rasterBuf.getElemByte(x, y, 0);
243
                } else if ((dataType == RasterBuffer.TYPE_SHORT) | (dataType == RasterBuffer.TYPE_USHORT)) {
244
                        return (double) rasterBuf.getElemShort(x, y, 0);
245
                }
246
                }catch(ArrayIndexOutOfBoundsException e){
247
                        throw new RasterBufferInvalidAccessException("Access to data type " + dataType + "invalid");
248
                }catch(NullPointerException e){
249
                        throw new RasterBufferInvalidAccessException("Access to data type " + dataType + "invalid");
250
                }
251
                return rasterBuf.getNoDataValue();
252
        }*/
253

    
254
        /*
255
         *  (non-Javadoc)
256
         * @see org.gvsig.fmap.grid.GridReader#getBandsValuesAsByte(int, int)
257
         */
258
        public byte[] getBandsValuesAsByte(int x, int y) throws RasterBufferInvalidAccessException, RasterBufferInvalidException {
259
                byte[] b = new byte[rasterBuf.getBandCount()];
260
                try{
261
                        if (isInRasterBuf(x - m_iOffsetX, y -  m_iOffsetY)){
262
                                try{
263
                                        rasterBuf.getElemByte(y -  m_iOffsetY, x - m_iOffsetX, b);
264
                                }catch(ArrayIndexOutOfBoundsException e){
265
                                        throw new RasterBufferInvalidAccessException("Access to data type " + dataType + "invalid");
266
                                }catch(NullPointerException e){
267
                                        throw new RasterBufferInvalidException("Null Buffer");
268
                                }
269
                                return b;
270
                        }else{
271
                                for(int i = 0; i < rasterBuf.getBandCount(); i++)
272
                                b[i] = (byte)rasterBuf.getNoDataValue();
273
                                return b;
274
                        }
275
                }catch(Exception e){
276
                        for(int i = 0; i < rasterBuf.getBandCount(); i++)
277
                        b[i] = (byte)rasterBuf.getNoDataValue();
278
                        return b;
279
                }        
280
        }
281

    
282
        /*
283
         *  (non-Javadoc)
284
         * @see org.gvsig.fmap.grid.GridReader#getBandsValuesAsShort(int, int)
285
         */
286
        public short[] getBandsValuesAsShort(int x, int y) throws RasterBufferInvalidAccessException, RasterBufferInvalidException {
287
                short[] b = new short[rasterBuf.getBandCount()];
288
                try{
289
                        if (isInRasterBuf(x - m_iOffsetX, y -  m_iOffsetY)){
290
                                try{
291
                                        rasterBuf.getElemShort(y -  m_iOffsetY, x - m_iOffsetX, b);
292
                                }catch(ArrayIndexOutOfBoundsException e){
293
                                        throw new RasterBufferInvalidAccessException("Access to data type " + dataType + "invalid");
294
                                }catch(NullPointerException e){
295
                                        throw new RasterBufferInvalidException("Null Buffer");
296
                                }
297
                                return b;
298
                        }else{
299
                                for(int i = 0; i < rasterBuf.getBandCount(); i++)
300
                                b[i] = (short)rasterBuf.getNoDataValue();
301
                                return b;
302
                        }
303
                }catch(Exception e){
304
                        for(int i = 0; i < rasterBuf.getBandCount(); i++)
305
                        b[i] = (short)rasterBuf.getNoDataValue();
306
                        return b;
307
                }
308
        }
309

    
310
        /*
311
         *  (non-Javadoc)
312
         * @see org.gvsig.fmap.grid.GridReader#getBandsValuesAsInt(int, int)
313
         */
314
        public int[] getBandsValuesAsInt(int x, int y) throws RasterBufferInvalidAccessException, RasterBufferInvalidException {
315
                int[] b = new int[rasterBuf.getBandCount()];
316
                try{
317
                        if (isInRasterBuf(x - m_iOffsetX, y -  m_iOffsetY)){
318
                                try{
319
                                        rasterBuf.getElemInt(y -  m_iOffsetY, x - m_iOffsetX, b);
320
                                }catch(ArrayIndexOutOfBoundsException e){
321
                                        throw new RasterBufferInvalidAccessException("Access to data type " + dataType + "invalid");
322
                                }catch(NullPointerException e){
323
                                        throw new RasterBufferInvalidException("Null Buffer");
324
                                }
325
                                return b;
326
                        }else{
327
                                for(int i = 0; i < rasterBuf.getBandCount(); i++)
328
                                b[i] = (int)rasterBuf.getNoDataValue();
329
                                return b;
330
                        }
331
                }catch(Exception e){
332
                        for(int i = 0; i < rasterBuf.getBandCount(); i++)
333
                        b[i] = (int)rasterBuf.getNoDataValue();
334
                        return b;
335
                }
336
        }
337

    
338
        /*
339
         *  (non-Javadoc)
340
         * @see org.gvsig.fmap.grid.GridReader#getBandsValuesAsFloat(int, int)
341
         */
342
        public float[] getBandsValuesAsFloat(int x, int y)  throws RasterBufferInvalidAccessException, RasterBufferInvalidException {
343
                float[] b = new float[rasterBuf.getBandCount()];
344
                try{
345
                        if (isInRasterBuf(x - m_iOffsetX, y -  m_iOffsetY)){
346
                                try{
347
                                        rasterBuf.getElemFloat(y -  m_iOffsetY, x - m_iOffsetX, b);
348
                                }catch(ArrayIndexOutOfBoundsException e){
349
                                        throw new RasterBufferInvalidAccessException("Access to data type " + dataType + "invalid");
350
                                }catch(NullPointerException e){
351
                                        throw new RasterBufferInvalidException("Null Buffer");
352
                                }
353
                                return b;
354
                        }else{
355
                                for(int i = 0; i < rasterBuf.getBandCount(); i++)
356
                                b[i] = (float)rasterBuf.getNoDataValue();
357
                                return b;
358
                        }
359
                }catch(Exception e){
360
                        for(int i = 0; i < rasterBuf.getBandCount(); i++)
361
                        b[i] = (float)rasterBuf.getNoDataValue();
362
                        return b;
363
                }
364
        }
365

    
366
        /*
367
         *  (non-Javadoc)
368
         * @see org.gvsig.fmap.grid.GridReader#getBandsValuesAsDouble(int, int)
369
         */
370
        public double[] getBandsValuesAsDouble(int x, int y) throws RasterBufferInvalidAccessException, RasterBufferInvalidException {
371
                double[] b = new double[rasterBuf.getBandCount()];
372
                try{
373
                        if (isInRasterBuf(x - m_iOffsetX, y -  m_iOffsetY)){
374
                                try{
375
                                        rasterBuf.getElemDouble(y -  m_iOffsetY, x - m_iOffsetX, b);
376
                                }catch(ArrayIndexOutOfBoundsException e){
377
                                        throw new RasterBufferInvalidAccessException("Access to data type " + dataType + "invalid");
378
                                }catch(NullPointerException e){
379
                                        throw new RasterBufferInvalidException("Null Buffer");
380
                                }
381
                                return b;
382
                        }else{
383
                                for(int i = 0; i < rasterBuf.getBandCount(); i++)
384
                                b[i] = rasterBuf.getNoDataValue();
385
                                return b;
386
                        }
387
                }catch(Exception e){
388
                        for(int i = 0; i < rasterBuf.getBandCount(); i++)
389
                        b[i] = rasterBuf.getNoDataValue();
390
                        return b;
391
                }
392
        }
393
}