Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libRaster / src / org / gvsig / raster / grid / GridNotInterpolated.java @ 11076

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

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

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

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

    
244
        /*
245
         *  (non-Javadoc)
246
         * @see org.gvsig.fmap.grid.GridReader#getBandsValuesAsByte(int, int)
247
         */
248
        public byte[] getBandsValuesAsByte(int x, int y)throws RasterBufferInvalidAccessException {
249
                byte[] b = new byte[rasterBuf.getBandCount()];
250
                try{
251
                        if (isInRasterBuf(x - m_iOffsetX, y -  m_iOffsetY)){
252
                                try{
253
                                        rasterBuf.getElemByte(y -  m_iOffsetY, x - m_iOffsetX, b);
254
                                }catch(ArrayIndexOutOfBoundsException e){
255
                                        throw new RasterBufferInvalidAccessException("Access to data type " + dataType + "invalid");
256
                                }catch(NullPointerException e){
257
                                        throw new RasterBufferInvalidAccessException("Access to data type " + dataType + "invalid");
258
                                }
259
                                return b;
260
                        }else{
261
                                for(int i = 0; i < rasterBuf.getBandCount(); i++)
262
                                b[i] = (byte)rasterBuf.getNoDataValue();
263
                                return b;
264
                        }
265
                }catch(Exception e){
266
                        for(int i = 0; i < rasterBuf.getBandCount(); i++)
267
                        b[i] = (byte)rasterBuf.getNoDataValue();
268
                        return b;
269
                }        
270
        }
271

    
272
        /*
273
         *  (non-Javadoc)
274
         * @see org.gvsig.fmap.grid.GridReader#getBandsValuesAsShort(int, int)
275
         */
276
        public short[] getBandsValuesAsShort(int x, int y)throws RasterBufferInvalidAccessException {
277
                short[] b = new short[rasterBuf.getBandCount()];
278
                try{
279
                        if (isInRasterBuf(x - m_iOffsetX, y -  m_iOffsetY)){
280
                                try{
281
                                        rasterBuf.getElemShort(y -  m_iOffsetY, x - m_iOffsetX, b);
282
                                }catch(ArrayIndexOutOfBoundsException e){
283
                                        throw new RasterBufferInvalidAccessException("Access to data type " + dataType + "invalid");
284
                                }catch(NullPointerException e){
285
                                        throw new RasterBufferInvalidAccessException("Access to data type " + dataType + "invalid");
286
                                }
287
                                return b;
288
                        }else{
289
                                for(int i = 0; i < rasterBuf.getBandCount(); i++)
290
                                b[i] = (short)rasterBuf.getNoDataValue();
291
                                return b;
292
                        }
293
                }catch(Exception e){
294
                        for(int i = 0; i < rasterBuf.getBandCount(); i++)
295
                        b[i] = (short)rasterBuf.getNoDataValue();
296
                        return b;
297
                }
298
        }
299

    
300
        /*
301
         *  (non-Javadoc)
302
         * @see org.gvsig.fmap.grid.GridReader#getBandsValuesAsInt(int, int)
303
         */
304
        public int[] getBandsValuesAsInt(int x, int y)throws RasterBufferInvalidAccessException {
305
                int[] b = new int[rasterBuf.getBandCount()];
306
                try{
307
                        if (isInRasterBuf(x - m_iOffsetX, y -  m_iOffsetY)){
308
                                try{
309
                                        rasterBuf.getElemInt(y -  m_iOffsetY, x - m_iOffsetX, b);
310
                                }catch(ArrayIndexOutOfBoundsException e){
311
                                        throw new RasterBufferInvalidAccessException("Access to data type " + dataType + "invalid");
312
                                }catch(NullPointerException e){
313
                                        throw new RasterBufferInvalidAccessException("Access to data type " + dataType + "invalid");
314
                                }
315
                                return b;
316
                        }else{
317
                                for(int i = 0; i < rasterBuf.getBandCount(); i++)
318
                                b[i] = (int)rasterBuf.getNoDataValue();
319
                                return b;
320
                        }
321
                }catch(Exception e){
322
                        for(int i = 0; i < rasterBuf.getBandCount(); i++)
323
                        b[i] = (int)rasterBuf.getNoDataValue();
324
                        return b;
325
                }
326
        }
327

    
328
        /*
329
         *  (non-Javadoc)
330
         * @see org.gvsig.fmap.grid.GridReader#getBandsValuesAsFloat(int, int)
331
         */
332
        public float[] getBandsValuesAsFloat(int x, int y) throws RasterBufferInvalidAccessException{
333
                float[] b = new float[rasterBuf.getBandCount()];
334
                try{
335
                        if (isInRasterBuf(x - m_iOffsetX, y -  m_iOffsetY)){
336
                                try{
337
                                        rasterBuf.getElemFloat(y -  m_iOffsetY, x - m_iOffsetX, b);
338
                                }catch(ArrayIndexOutOfBoundsException e){
339
                                        throw new RasterBufferInvalidAccessException("Access to data type " + dataType + "invalid");
340
                                }catch(NullPointerException e){
341
                                        throw new RasterBufferInvalidAccessException("Access to data type " + dataType + "invalid");
342
                                }
343
                                return b;
344
                        }else{
345
                                for(int i = 0; i < rasterBuf.getBandCount(); i++)
346
                                b[i] = (float)rasterBuf.getNoDataValue();
347
                                return b;
348
                        }
349
                }catch(Exception e){
350
                        for(int i = 0; i < rasterBuf.getBandCount(); i++)
351
                        b[i] = (float)rasterBuf.getNoDataValue();
352
                        return b;
353
                }
354
        }
355

    
356
        /*
357
         *  (non-Javadoc)
358
         * @see org.gvsig.fmap.grid.GridReader#getBandsValuesAsDouble(int, int)
359
         */
360
        public double[] getBandsValuesAsDouble(int x, int y) throws RasterBufferInvalidAccessException{
361
                double[] b = new double[rasterBuf.getBandCount()];
362
                try{
363
                        if (isInRasterBuf(x - m_iOffsetX, y -  m_iOffsetY)){
364
                                try{
365
                                        rasterBuf.getElemDouble(y -  m_iOffsetY, x - m_iOffsetX, b);
366
                                }catch(ArrayIndexOutOfBoundsException e){
367
                                        throw new RasterBufferInvalidAccessException("Access to data type " + dataType + "invalid");
368
                                }catch(NullPointerException e){
369
                                        throw new RasterBufferInvalidAccessException("Access to data type " + dataType + "invalid");
370
                                }
371
                                return b;
372
                        }else{
373
                                for(int i = 0; i < rasterBuf.getBandCount(); i++)
374
                                b[i] = rasterBuf.getNoDataValue();
375
                                return b;
376
                        }
377
                }catch(Exception e){
378
                        for(int i = 0; i < rasterBuf.getBandCount(); i++)
379
                        b[i] = rasterBuf.getNoDataValue();
380
                        return b;
381
                }
382
        }
383
}