Statistics
| Revision:

gvsig-raster / org.gvsig.raster / trunk / org.gvsig.raster / org.gvsig.raster.lib / org.gvsig.raster.lib.impl / src / main / java / org / gvsig / raster / impl / grid / GridNotInterpolated.java @ 859

History | View | Annotate | Download (16.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.impl.grid;
21

    
22
import org.gvsig.fmap.dal.coverage.RasterLibrary;
23
import org.gvsig.fmap.dal.coverage.dataset.Buffer;
24
import org.gvsig.fmap.dal.coverage.datastruct.GridExtent;
25
import org.gvsig.fmap.dal.coverage.exception.RasterBufferInvalidAccessException;
26
import org.gvsig.fmap.dal.coverage.exception.RasterBufferInvalidException;
27
import org.gvsig.fmap.dal.coverage.store.RasterDataStore;
28
import org.gvsig.fmap.dal.coverage.store.RasterQuery;
29
import org.gvsig.raster.impl.DefaultRasterManager;
30
import org.gvsig.raster.impl.buffer.RasterBuffer;
31

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

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

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

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

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

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

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

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