Statistics
| Revision:

gvsig-raster / org.gvsig.raster / branches / org.gvsig.raster_proyeccion_al_vuelo / org.gvsig.raster / org.gvsig.raster.lib / org.gvsig.raster.lib.impl / src / main / java / org / gvsig / raster / impl / grid / GridNotInterpolated.java @ 5458

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 java.awt.Rectangle;
23

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

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

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

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

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

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

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

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

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