Statistics
| Revision:

gvsig-raster / org.gvsig.raster / branches / org.gvsig.raster_dataaccess_refactoring / org.gvsig.raster.lib / org.gvsig.raster.lib.impl / src / main / java / org / gvsig / raster / impl / buffer / DefaultRasterQuery.java @ 2303

History | View | Annotate | Download (29 KB)

1
package org.gvsig.raster.impl.buffer;
2

    
3
import java.awt.Rectangle;
4
import java.awt.geom.AffineTransform;
5
import java.awt.geom.Point2D;
6

    
7
import org.gvsig.fmap.dal.coverage.RasterLocator;
8
import org.gvsig.fmap.dal.coverage.dataset.Buffer;
9
import org.gvsig.fmap.dal.coverage.datastruct.BandList;
10
import org.gvsig.fmap.dal.coverage.datastruct.Extent;
11
import org.gvsig.fmap.dal.coverage.datastruct.NoData;
12
import org.gvsig.fmap.dal.coverage.exception.FileNotExistsException;
13
import org.gvsig.fmap.dal.coverage.exception.InvalidSetViewException;
14
import org.gvsig.fmap.dal.coverage.exception.NotSupportedExtensionException;
15
import org.gvsig.fmap.dal.coverage.exception.QueryException;
16
import org.gvsig.fmap.dal.coverage.store.RasterDataStore;
17
import org.gvsig.fmap.dal.coverage.store.RasterQuery;
18
import org.gvsig.fmap.dal.coverage.util.RasterUtils;
19
import org.gvsig.raster.cache.tile.TileCacheLibrary;
20
import org.gvsig.raster.cache.tile.provider.CacheStruct;
21
import org.gvsig.raster.cache.tile.provider.TileListener;
22
import org.gvsig.raster.impl.DefaultRasterManager;
23
import org.gvsig.raster.impl.buffer.cache.RasterReadOnlyBuffer;
24
import org.gvsig.raster.impl.datastruct.ExtentImpl;
25
import org.gvsig.raster.impl.store.QueryableRaster;
26
import org.gvsig.timesupport.Time;
27
import org.gvsig.tools.persistence.PersistentState;
28
import org.gvsig.tools.persistence.exception.PersistenceException;
29
import org.gvsig.tools.task.TaskStatus;
30

    
31
/**
32
 * Default implementation for RasterQuery
33
 * 
34
 * @author Nacho Brodin (nachobrodin@gmail.com)
35
 */
36
public class DefaultRasterQuery implements RasterQuery, SpiRasterQuery {
37
        public static final int  TYPE_UNDEFINED             = -1;
38
        public static final int  TYPE_ENTIRE                = 0;
39
        public static final int  TYPE_WCOORDS               = 1;
40
        public static final int  TYPE_WCOORDS_RESCALED      = 2;
41
        public static final int  TYPE_WCOORDS_SHIFT         = 3;
42
        public static final int  TYPE_PX                    = 4;
43
        public static final int  TYPE_PX_RESCALED           = 5;
44
        public static final int  TYPE_PX_SHIFT              = 6;
45
        public static final int  TYPE_PX_RESCALED_SHIFT     = 7;
46
        public static final int  TYPE_TILED                 = 8;
47
        public static final int  TYPE_ONE_TILE              = 9;
48

    
49
        private int              type                       = TYPE_ENTIRE;
50
        /*
51
         * Request in pixels
52
         */
53
        private Rectangle        pixelWindow                = null;
54
        /*
55
         * Request in pixels adjusted to the source. The provider will use this parameter. If the type is with
56
         * shift then adjustedPixelWindow will be the same to the pixelWindow
57
         */
58
        private Rectangle        adjustedPixelWindow        = null;
59
        /*
60
         * Request in world coordinates
61
         */
62
        private Extent           requestBoundingBox         = null;
63
        /*
64
         * Request in world coordinates adjusted to the source. The provider will use this parameter. If the type is with
65
         * shift then adjustedBoundingBox will be the same to the requestBoundingBox
66
         */
67
        private Extent           adjustedBoundingBox        = null;
68

    
69
        private int              widthForResampling         = -1;
70
        private int              heightForResampling        = -1;
71
        /*
72
         * If the request is adjusted to the input source this width is the same to widthForResampling else
73
         * the provider has to receive the right buffer size for the request, so this adjustedWidth will
74
         * contain a smaller buffer without frames. 
75
         */
76
        private int              adjustedBufferWidth        = 0;
77
        /*
78
         * If the request is adjusted to the input source this height is the same to heightForResampling else
79
         * the provider has to receive the right buffer size for the request, so this adjustedHeight will
80
         * contain a smaller buffer without frames. 
81
         */
82
        private int              adjustedBufferHeight       = 0;
83

    
84
        private double[]         step                       = null;
85

    
86
        private int              tileRow                    = 0;
87
        private int              tileCol                    = 0;
88
        private int              resolutionLevel            = -1;
89
        private boolean          adjustToSrcExtent          = true;
90

    
91
        private int[]            drawableBands              = new int[]{0};
92
        private boolean          readOnly                   = false;
93
        private boolean          memoryBuffer               = false;
94
        private boolean          storeLastBuffer            = false;
95
        private Time             time                       = null;
96

    
97
        
98
        private boolean                         supersamplingLoadingBuffer = false;
99
        /**
100
         * Valor NoData con el que se rellenan las celdas cuando adjustToExtent es false
101
         */
102
        private NoData           noDataValueToFill          = null;
103

    
104
        private TileListener     listener                   = null;
105
        private int              alphaBandNumber            = -1;
106
        private boolean          dontBuildBuffer            = false;
107
        private CacheStruct      cacheStruct                = null;   
108
        private TaskStatus       taskStatus                 = null;
109

    
110
        //Parameters only for providers
111
        private BandList         bandList                   = null;
112
        private Buffer           bufferForProviders         = null;
113
        //private Buffer           bufferWithoutAdjust        = null;
114

    
115

    
116
        //****************************************************
117
        //*************Request of data windows****************
118
        //****************************************************
119
        
120
        public void setAreaOfInterest() {
121
                this.type = TYPE_ENTIRE;
122
        }
123

    
124
        public void setAreaOfInterest(Rectangle pixelWindow) {
125
                this.pixelWindow = pixelWindow;
126
                this.type = TYPE_PX;
127
        }
128

    
129
        public void setAreaOfInterest(Rectangle pixelWindow, int bufWidth, int bufHeight) {
130
                this.pixelWindow = pixelWindow;
131
                this.widthForResampling = bufWidth;
132
                this.heightForResampling = bufHeight;
133
                this.type = TYPE_PX;
134
        }
135

    
136
        public void setAreaOfInterest(Extent requestBoundingBox, int bufWidth, int bufHeight) {
137
                this.widthForResampling = bufWidth;
138
                this.heightForResampling = bufHeight;
139
                this.requestBoundingBox = requestBoundingBox;
140
                this.type = TYPE_WCOORDS;
141
        }
142

    
143
        public void setAreaOfInterest(Extent boundingBox) {
144
                this.requestBoundingBox = boundingBox;
145
                this.type = TYPE_WCOORDS;
146
        }
147

    
148
        public void setAreaOfInterest(Extent requestBoundingBox, 
149
                        int bufWidth, int bufHeight, TileListener listener) {
150
                this.requestBoundingBox = requestBoundingBox;
151
                this.widthForResampling = bufWidth;
152
                this.heightForResampling = bufHeight;
153
                this.type = TYPE_TILED;
154
                this.listener = listener;
155
        }
156

    
157
        public void setTileParameters(int level, int tileCol, int tileRow) {
158
                this.type = TYPE_ONE_TILE;
159
                this.resolutionLevel = level;
160
                this.tileRow = tileRow;
161
                this.tileCol = tileCol;
162
        }
163

    
164
        /**
165
         * @deprecated Use setTileParameters(int level, int tileCol, int tileRow)
166
         */
167
        public void setTileParameters(int level, int tileCol, int tileRow, Extent bbox, CacheStruct cacheStruct) {
168
                this.type = TYPE_ONE_TILE;
169
                this.requestBoundingBox = bbox;
170
                this.resolutionLevel = level;
171
                this.tileRow = tileRow;
172
                this.tileCol = tileCol;
173
                int[] size = null;
174
                if(cacheStruct != null) {
175
                        this.cacheStruct = cacheStruct;
176
                        size = cacheStruct.getTileSizeByLevel(level);
177
                } else {
178
                        size = new int[]{TileCacheLibrary.DEFAULT_TILEWIDTH, TileCacheLibrary.DEFAULT_TILEHEIGHT};
179
                }
180
                this.widthForResampling = size[0];
181
                this.heightForResampling = size[1];
182
        }
183

    
184

    
185

    
186
        //****************************************************
187
        //****************Internal Services*******************
188
        //****************************************************
189
        
190
        public boolean requestIsTiled() {
191
                switch (type) {
192
                case TYPE_TILED:
193
                case TYPE_ONE_TILE:
194
                        return true;
195
                }
196
                return false;
197
        }
198

    
199
        public boolean requestHasShift() {
200
                switch (type) {
201
                case TYPE_PX_RESCALED_SHIFT:
202
                case TYPE_PX_SHIFT:
203
                case TYPE_WCOORDS_SHIFT:
204
                        return true;
205
                }
206
                return false;
207
        }
208

    
209
        public boolean requestIsPixelCoordinates() {
210
                switch (type) {
211
                case TYPE_PX:
212
                case TYPE_PX_SHIFT:
213
                case TYPE_PX_RESCALED:
214
                case TYPE_PX_RESCALED_SHIFT:
215
                case TYPE_ENTIRE:
216
                        return true;
217
                }
218
                return false;
219
        }
220

    
221
        public boolean requestIsInWorldCoordinates() {
222
                switch (type) {
223
                case TYPE_WCOORDS:
224
                case TYPE_WCOORDS_RESCALED:
225
                case TYPE_WCOORDS_SHIFT:
226
                case TYPE_TILED:
227
                        return true;
228
                }
229
                return false;
230
        }
231

    
232
        public boolean requestIsRescaled() {
233
                switch (type) {
234
                case TYPE_PX_RESCALED:
235
                case TYPE_PX_RESCALED_SHIFT:
236
                case TYPE_WCOORDS_RESCALED:
237
                case TYPE_TILED:
238
                        return true;
239
                }
240
                return false;
241
        }
242

    
243
        /**
244
         * Returns true if the request is trying apply supersampling, that is, the size of the result is
245
         * bigger that the window of maximum resolution of this source
246
         * @return
247
         */
248
        public boolean isSupersamplingTheRequest() {
249
                if(!requestHasShift()) {
250
                        return (getBufWidth() > getAdjustedWidth() || getBufHeight() > getAdjustedHeight());
251
                }
252
                return (getAdjustedBufWidth() > getAdjustedWidth() || getAdjustedBufHeight() > getAdjustedHeight());
253
        }
254

    
255

    
256
        public RasterQuery clone() {
257
                DefaultRasterQuery q = new DefaultRasterQuery();
258
                q.type = type;
259
                q.pixelWindow = (Rectangle)pixelWindow.clone();
260
                q.adjustToSrcExtent = adjustToSrcExtent;
261
                q.widthForResampling = widthForResampling;
262
                q.heightForResampling = heightForResampling;
263
                q.requestBoundingBox = requestBoundingBox != null ? requestBoundingBox.clone() : null;
264
                q.tileCol = tileCol;
265
                q.tileRow = tileRow;
266
                q.resolutionLevel = resolutionLevel;
267
                q.readOnly = readOnly;
268
                q.memoryBuffer = memoryBuffer;
269
                q.storeLastBuffer = storeLastBuffer;
270
                q.drawableBands = new int[drawableBands.length];
271
                for (int i = 0; i < q.drawableBands.length; i++) {
272
                        q.drawableBands[i] = drawableBands[i];
273
                }
274
                q.supersamplingLoadingBuffer = supersamplingLoadingBuffer;
275
                q.noDataValueToFill = noDataValueToFill != null ? (NoData)noDataValueToFill.clone() : null;
276
                q.listener = listener;
277
                q.alphaBandNumber = alphaBandNumber;
278
                q.bandList = bandList != null ? (BandList)bandList.clone() : null;
279
                q.bufferForProviders = bufferForProviders;
280
                return q;
281
        }
282

    
283
        //****************************************************
284
        //*************Parameters calculation*****************
285
        //****************************************************
286
        
287
        /**
288
         * Calculates the parameters using the base request. That is, bounding boxes, data windows,
289
         * output size, steps and parameters for supersampling, band lists and buffers. This parameters
290
         * are calculated adjusting to the source limits. For this reason, the request parameters are 
291
         * saved in other variables, to allow calculate the displacement and resampling whether it was 
292
         * necessary
293
         * @param store
294
         * @throws QueryException 
295
         */
296
        public void calculateParameters(RasterDataStore store) throws QueryException {
297
                calculateRequestType(store);
298

    
299
                //Parameters for requests in pixels
300
                if(requestIsPixelCoordinates()) {
301
                        calculateAdjustedPxSizeFromPxSize((int)store.getWidth(), (int)store.getHeight());
302
                        calculateBoundingBoxesFromPxRequest(store);
303
                } 
304

    
305
                //Parameters for requests in world coordinates
306
                if(requestIsInWorldCoordinates()) {
307
                        calculateAdjustedWCWindowFromWCWindow(store.getAffineTransform(), (int)store.getWidth(), (int)store.getHeight());
308
                        calculatePxWindowsFromBoundingBoxes(store);
309
                }
310

    
311
                //Output
312
                calculateOutputSizeFromPxWindow(store);
313

    
314
                //Supersampling parameters
315
                step = null;
316
                if(isSupersamplingTheRequest() && !store.isRasterEnclosed()) {
317
                        calculateStep(store);
318
                        supersampledBuffers(store);
319
                }
320

    
321
                //Bands and buffers
322
                buildDrawableBandList(store.getBands());
323
                if(!dontBuildBuffer)
324
                        createBuffer(store);
325
        }
326

    
327
        /**
328
         * Adjust the request in world coordinates to the data size
329
         * @param q
330
         */
331
        private void calculateAdjustedWCWindowFromWCWindow(AffineTransform at, int w, int h) {
332
                if(requestHasShift()) {
333
                        setAdjustedRequestBoundingBox(getRequestBoundingBox().clone());
334
                }
335
                RasterUtils util = RasterLocator.getManager().getRasterUtils();
336
                Extent adjustedDataExtent = util.calculateAdjustedView(getRequestBoundingBox(), at, w, h);
337
                setAdjustedRequestBoundingBox(adjustedDataExtent);
338
        }
339

    
340
        /**
341
         * Adjust the request in pixel coordinates to the data size
342
         * @param q
343
         */
344
        private void calculateAdjustedPxSizeFromPxSize(int sourceWidth, int sourceHeight) {
345
                adjustedPixelWindow = (Rectangle)pixelWindow.clone();
346

    
347
                if(requestHasShift()) 
348
                        return;
349

    
350
                if(getAdjustedX() < 0)
351
                        setAdjustedX(0);
352
                if(getAdjustedY() < 0)
353
                        setAdjustedY(0);
354
                if((getAdjustedX() + getAdjustedWidth()) > sourceWidth)
355
                        setAdjustedWidth(sourceWidth - getX());
356
                if((getAdjustedY() + getAdjustedHeight()) > sourceHeight)
357
                        setAdjustedHeight(sourceHeight - getAdjustedY());
358
        }
359

    
360

    
361
        private void calculateBoundingBoxesFromPxRequest(RasterDataStore store) {
362
                Extent extent = new ExtentImpl(
363
                                store.rasterToWorld(new Point2D.Double(getX(), getY())),
364
                                store.rasterToWorld(new Point2D.Double(getX() + getWidth(), getY() + getHeight())));
365
                setRequestBoundingBox(extent);
366

    
367
                Extent adjustedExtent = new ExtentImpl(
368
                                store.rasterToWorld(new Point2D.Double(getAdjustedX(), getAdjustedY())),
369
                                store.rasterToWorld(new Point2D.Double(getAdjustedX() + getAdjustedWidth(), getAdjustedY() + getAdjustedHeight())));
370
                setAdjustedRequestBoundingBox(adjustedExtent);
371
        }
372

    
373
        private double round(double value) {
374
                double a = (value - (int)value);
375
                return (a > 0.95 || a < 0.05) ? Math.round(value) : value;
376
        }
377

    
378
        private void calculateOutputSizeFromPxWindow(RasterDataStore store) {
379
                if(!requestIsRescaled()) {
380
                        setBufHeight(getHeight());
381
                        setBufWidth(getWidth());
382
                        setAdjustedBufWidth(getAdjustedWidth());
383
                        setAdjustedBufHeight(getAdjustedHeight());
384
                } else {
385
                        double scaleWidth = (double)getWidth() / getBufWidth();
386
                        double scaleHeight = (double)getHeight() / getBufHeight();
387
                        setAdjustedBufWidth((int)Math.ceil(round(getAdjustedWidth() / scaleWidth)));
388
                        setAdjustedBufHeight((int)Math.ceil(round(getAdjustedHeight() / scaleHeight)));
389
                }
390
        }
391

    
392
        private void calculatePxWindowsFromBoundingBoxes(RasterDataStore store) {
393
                Extent requestBBox = getRequestBoundingBox();
394
                Point2D ul = store.worldToRaster(requestBBox.getUL());
395
                Point2D lr = store.worldToRaster(requestBBox.getLR());
396
                int w = (int)Math.ceil(Math.abs(round(ul.getX()) - round(lr.getX())));
397
                int h = (int)Math.ceil(Math.abs(round(ul.getY()) - round(lr.getY())));
398
                Rectangle r = new Rectangle((int)ul.getX(), (int)ul.getY(), w, h);
399
                pixelWindow = r;
400

    
401
                Extent adjustedBBox = getAdjustedRequestBoundingBox();
402
                ul = store.worldToRaster(adjustedBBox.getUL());
403
                lr = store.worldToRaster(adjustedBBox.getLR());
404
                w = (int)Math.ceil(Math.abs(round(ul.getX()) - round(lr.getX())));
405
                h = (int)Math.ceil(Math.abs(round(ul.getY()) - round(lr.getY())));
406
                r = new Rectangle((int)ul.getX(), (int)ul.getY(), w, h);
407
                adjustedPixelWindow = r;
408
        }
409

    
410
        /**
411
         * <p>
412
         * If the request is being supersampled, then the buffer size will be bigger than the pixel window. 
413
         * In this case the provider will receive a buffer in 1:1 scale. The supersampling function will be 
414
         * in the client (supersampling deactivated) or in the store (supersampling activated). 
415
         * </p><p>
416
         * The buffer will have one pixel more because the resampling cuts the first pixel by the beginning and
417
         * shows the last pixel by the end.
418
         * </p>
419
         * @param store
420
         */
421
        private void supersampledBuffers(RasterDataStore store) {
422
                //Si se est? pidiendo supersampleo el provider tiene que recibir un buffer de tama?o igual al n?mero real de
423
                //pixels del ?rea, por ello habr? que cambiar el adjustedbufwidth y adjustedBufHeight. Adem?s habr? que variar
424
                //el extent del provider y la ventana en pixeles para que todo cuadre. El objetivo es que cuando el cliente (Render)
425
                //supersamplee no le queden huecos en blanco porque le falte el ?ltimo pixel.
426
                if((getAdjustedX() + getAdjustedWidth()) < store.getWidth())
427
                        setAdjustedWidth(getAdjustedWidth() + 1);
428
                if((getAdjustedY() + getAdjustedHeight()) < store.getHeight())
429
                        setAdjustedHeight(getAdjustedHeight() + 1);
430
                setAdjustedBufWidth(getAdjustedWidth());
431
                setAdjustedBufHeight(getAdjustedHeight());
432
                Point2D ul = store.rasterToWorld(new Point2D.Double(adjustedPixelWindow.getX(), adjustedPixelWindow.getY()));
433
                Point2D lr = store.rasterToWorld(new Point2D.Double(
434
                                adjustedPixelWindow.getX() + adjustedPixelWindow.getWidth(), 
435
                                adjustedPixelWindow.getY() + adjustedPixelWindow.getHeight()));
436
                setAdjustedRequestBoundingBox(new ExtentImpl(ul, lr));
437
        }
438

    
439
        /**
440
         * Giving a world coordinates, a buffer size and a original raster size. If the 
441
         * buffer is bigger than the window requested in the source raster (supersampling), 
442
         * then it means that each pixel of the raster will be written several times in 
443
         * the destination buffer.
444
         * 
445
         * This function calculates the shift in pixels en X and Y coordinate that 
446
         * correspond to the first pixel, due to this pixel won't be drawn entirely.
447
         * 
448
         * This operation is needed when the client wants to apply suppersampling on the 
449
         * result of a request.
450
         */
451
        private void calculateStep(RasterDataStore store) {
452
                Point2D tl = store.worldToRaster(getAdjustedRequestBoundingBox().getUL());
453
                Point2D br = store.worldToRaster(getAdjustedRequestBoundingBox().getLR());
454

    
455
                //Se obtiene el tama?o de la petici?n original pero ajustada ya que adjustedBufferWidth y adjustedBufferHeight
456
                //habr?n sido modificados para que no excedan el tama?o de la ventana, ya que el provider no debe supersamplear
457
                double scaleWidth = (double)getWidth() / getBufWidth();
458
                double scaleHeight = (double)getHeight() / getBufHeight();
459
                int srcOriginalAdjustedWidth = (int)Math.ceil(round(getAdjustedWidth() / scaleWidth));
460
                int srcOriginalAdjustedHeight = (int)Math.ceil(round(getAdjustedHeight() / scaleHeight));
461

    
462
                double wPx = (srcOriginalAdjustedWidth / Math.abs(br.getX() - tl.getX()));
463
                double hPx = (srcOriginalAdjustedHeight / Math.abs(br.getY() - tl.getY()));
464

    
465
                int x = (int)((tl.getX() > br.getX()) ? Math.floor(br.getX()) : Math.floor(tl.getX()));
466
                int y = (int)((tl.getY() > br.getY()) ? Math.floor(br.getY()) : Math.floor(tl.getY()));
467

    
468
                double a = (tl.getX() > br.getX()) ? (Math.abs(br.getX() - x)) : (Math.abs(tl.getX() - x));
469
                double b = (tl.getY() > br.getY()) ? (Math.abs(br.getY() - y)) : (Math.abs(tl.getY() - y));
470

    
471
                double stpX = (int)((a * srcOriginalAdjustedWidth) / Math.abs(br.getX() - tl.getX()));
472
                double stpY = (int)((b * srcOriginalAdjustedHeight) / Math.abs(br.getY() - tl.getY()));
473

    
474
                step = new double[]{stpX, stpY, wPx, hPx};
475
        }
476

    
477
        private Buffer createBuffer(RasterDataStore store) throws QueryException {
478
                if(isReadOnly()) {
479
                        bufferForProviders = DefaultRasterManager.getInstance().createReadOnlyBuffer(
480
                                        store.getDataType()[0], getAdjustedBufWidth(), getAdjustedBufHeight(), store.getBandCount());
481
                        try {
482
                                ((RasterReadOnlyBuffer)bufferForProviders).setBufferParams((QueryableRaster)store, 
483
                                                getAdjustedX(), 
484
                                                getAdjustedY(), 
485
                                                getAdjustedX() + getAdjustedWidth() - 1, 
486
                                                getAdjustedY() + getAdjustedHeight() - 1, 
487
                                                bandList);
488
                        } catch (FileNotExistsException e) {
489
                                throw new QueryException("Error setting buffer params in a RO buffer", e);
490
                        } catch (NotSupportedExtensionException e) {
491
                                throw new QueryException("Error setting buffer params in a RO buffer", e);
492
                        } catch (InvalidSetViewException e) {
493
                                throw new QueryException("Error setting buffer params in a RO buffer", e);
494
                        }
495
                } else {
496
                        BandList bandList = buildDrawableBandList(store.getBands());
497

    
498
                        if(isMemoryBuffer()) 
499
                                bufferForProviders = DefaultRasterManager.getInstance().createMemoryBuffer(
500
                                                store.getDataType()[0], getAdjustedBufWidth(), getAdjustedBufHeight(), bandList.getDrawableBandsCount(), true);
501
                        else
502
                                bufferForProviders = DefaultRasterManager.getInstance().createBuffer(
503
                                                store.getDataType()[0], getAdjustedBufWidth(), getAdjustedBufHeight(), bandList.getDrawableBandsCount(), true);
504
                }
505

    
506
                bufferForProviders.setDataExtent(getAdjustedRequestBoundingBox().toRectangle2D());
507
                return bufferForProviders;
508

    
509
        }
510

    
511
        /**
512
         * Builds a list o bands to draw from the store band list and
513
         * the <code>drawablebands</code> asigned by the user
514
         * @param drawableBands
515
         * @param storeBandList
516
         * @return
517
         */
518
        public BandList buildDrawableBandList(BandList storeBandList) {
519
                bandList = (BandList)storeBandList.clone();
520
                //If drawableBands is null then the bandList is the same that the source band list
521
                if(drawableBands != null)
522
                        bandList.setDrawableBands(drawableBands);
523
                return bandList;
524
        }
525
        
526
        private boolean isAdjustingPixelWindowToStore(RasterDataStore store) {
527
                return (pixelWindow.getX() >= 0 && 
528
                                pixelWindow.getY() >= 0 && 
529
                                pixelWindow.getWidth() <= store.getWidth() && 
530
                                pixelWindow.getHeight() <= store.getHeight());
531
        }
532
        
533
        private boolean isBufferSizeDefined() {
534
                return (widthForResampling != -1 && heightForResampling != -1);
535
        }
536
        
537
        private boolean isAdjustingRequestBBoxToStore(RasterDataStore store) {
538
                Extent intersection = requestBoundingBox.intersection(store.getExtent());
539
                return intersection.equals(store.getExtent());
540
        }
541
        
542
        private void calculateRequestType(RasterDataStore store) {
543
                if(type == TYPE_ENTIRE) {
544
                        pixelWindow = new Rectangle(0, 0, (int)store.getWidth(), (int)store.getHeight());
545
                        return;
546
                }
547
                
548
                if(!adjustToSrcExtent) {
549
                        if(type == TYPE_PX) {
550
                                if(isAdjustingPixelWindowToStore(store)) {
551
                                        if(isBufferSizeDefined())
552
                                                this.type = TYPE_PX_RESCALED;
553
                                        else
554
                                                this.type = TYPE_PX;
555
                                } else {
556
                                        if(isBufferSizeDefined())
557
                                                this.type = TYPE_PX_RESCALED_SHIFT;
558
                                        else
559
                                                this.type = TYPE_PX_SHIFT;
560
                                }
561
                        }
562
                        if(type == TYPE_WCOORDS) {
563
                                if(isBufferSizeDefined()) {
564
                                        if(isAdjustingRequestBBoxToStore(store))
565
                                                this.type = TYPE_WCOORDS_RESCALED;
566
                                        else
567
                                                this.type = TYPE_WCOORDS_SHIFT;                                                
568
                                } //else TYPE_WCOORDS
569
                        }
570
                } else {
571
                        if(type == TYPE_PX) {
572
                                if(isBufferSizeDefined())
573
                                        this.type = TYPE_PX_RESCALED;
574
                        }
575
                        if(type == TYPE_WCOORDS) {
576
                                if(isBufferSizeDefined())
577
                                        this.type = TYPE_WCOORDS_RESCALED;
578
                                //else TYPE_WCOORDS
579
                        }
580
                }
581
        }
582

    
583
        //****************************************************
584
        //*********Implementing DataQuery methods*************
585
        //****************************************************
586

    
587
        public Object getQueryParameter(String name) {
588
                return null;
589
        }
590

    
591
        public double getScale() {
592
                return 0;
593
        }
594

    
595
        public void setQueryParameter(String name, Object value) {
596

    
597
        }
598

    
599
        public void setScale(double scale) {
600

    
601
        }
602

    
603
        //****************************************************
604
        //*********Implementing Persistent methods************
605
        //****************************************************
606

    
607
        public void loadFromState(PersistentState state)
608
                        throws PersistenceException {
609

    
610
        }
611

    
612
        public void saveToState(PersistentState state) throws PersistenceException {
613

    
614
        }
615

    
616
        //****************************************************
617
        //**************Getters and Setters*******************
618
        //****************************************************
619
        
620
        public void dontBuildBuffer() {
621
                this.dontBuildBuffer = true;
622
        }
623

    
624
        public int getType() {
625
                return type;
626
        }
627

    
628
        public void storeLastBuffer(boolean store) {
629
                this.storeLastBuffer = store;
630
        }
631

    
632
        /**
633
         * The user can told if the buffer will be stored or not in the RasterDatasource.
634
         * @return store
635
         */
636
        public boolean isStoredLastBuffer() {
637
                return storeLastBuffer;
638
        }
639

    
640
        public void setAdjustToExtent(boolean adjustToExtent) {
641
                this.adjustToSrcExtent = adjustToExtent;
642
        }
643

    
644
        public boolean isAdjustToExtent() {
645
                return adjustToSrcExtent;
646
        }
647

    
648
        public int[] getDrawableBands() {
649
                return drawableBands;
650
        }
651

    
652
        public void setAllDrawableBands() {
653
                this.drawableBands = null;
654
        }
655

    
656
        public void setDrawableBands(int[] drawableBands) {
657
                this.drawableBands = drawableBands;
658
        }
659

    
660
        public void setReadOnly(boolean readOnly) {
661
                this.readOnly = readOnly;
662
        }
663

    
664
        /**
665
         * Returns true if the supersampling is enable and false if it is disable
666
         * Rendering from gvSIG, the resampling is always disable because if the request 
667
         * is greater than 1:1 scale the resampling will be done in the client <code>Render</code>.
668
         * The default value is disable.
669
         */
670
        public boolean isSupersamplingOptionActive() {
671
                return supersamplingLoadingBuffer;
672
        }
673

    
674
        /**
675
         * Enables or disables the supersampling loading the buffer. Rendering from gvSIG,
676
         * the resampling is always disable because if the request is greater than 1:1 scale
677
         * the resampling will be done in the client <code>Render</code>
678
         */
679
        public void setSupersamplingOption(boolean supersamplingLoadingBuffer) {
680
                this.supersamplingLoadingBuffer = supersamplingLoadingBuffer;
681
        }
682

    
683
        public void setMemoryBuffer(boolean memoryBuffer) {
684
                this.memoryBuffer = memoryBuffer;
685
        }
686

    
687
        /**
688
         * Obtiene el flag que dice si la carga del siguiente buffer es en memoria
689
         * @return memory true si la siguiente carga de buffer se hace en memoria y false se deja decidir al dataset 
690
         * el tipo de buffer
691
         */
692
        public boolean isMemoryBuffer() {
693
                return memoryBuffer;
694
        }
695

    
696
        public void setNoDataToFill(NoData noData) {
697
                this.noDataValueToFill = noData;
698
        }
699

    
700
        public void setAlphaBand(int bandNumber) {
701
                this.alphaBandNumber = bandNumber;
702
        }
703

    
704
        public int getAlphaBandNumber() {
705
                return alphaBandNumber;
706
        }
707

    
708
        public NoData getNoDataValueToFill() {
709
                return this.noDataValueToFill;
710
        }
711
        
712
        public void setRequestPixelWindow(Rectangle pixelWindow) {
713
                this.pixelWindow = pixelWindow;
714
        }
715

    
716
        public int getX() {
717
                return (int)pixelWindow.getX();
718
        }
719

    
720
        public int getY() {
721
                return (int)pixelWindow.getY();
722
        }
723

    
724
        public int getWidth() {
725
                return (int)pixelWindow.getWidth();
726
        }
727

    
728
        public int getHeight() {
729
                return (int)pixelWindow.getHeight();
730
        }
731

    
732
        public void setX(int x) {
733
                pixelWindow.setLocation(x, getY());
734
        }
735

    
736
        public void setY(int y) {
737
                pixelWindow.setLocation(getX(), y);
738
        }
739

    
740
        public void setWidth(int w) {
741
                pixelWindow.setSize(w, getWidth());
742
        }
743

    
744
        public void setHeight(int h) {
745
                pixelWindow.setSize(getHeight(), h);
746
        }
747

    
748
        public int getAdjustedX() {
749
                return (int)adjustedPixelWindow.getX();
750
        }
751

    
752
        public int getAdjustedY() {
753
                return (int)adjustedPixelWindow.getY();
754
        }
755

    
756
        public int getAdjustedWidth() {
757
                return (int)adjustedPixelWindow.getWidth();
758
        }
759

    
760
        public int getAdjustedHeight() {
761
                return (int)adjustedPixelWindow.getHeight();
762
        }
763

    
764
        public void setAdjustedX(int x) {
765
                adjustedPixelWindow.setLocation(x, getAdjustedY());
766
        }
767

    
768
        public void setAdjustedY(int y) {
769
                adjustedPixelWindow.setLocation(getAdjustedX(), y);
770
        }
771

    
772
        public void setAdjustedWidth(int w) {
773
                adjustedPixelWindow.setSize(w, getAdjustedHeight());
774
        }
775

    
776
        public void setAdjustedHeight(int h) {
777
                adjustedPixelWindow.setSize(getAdjustedWidth(), h);
778
        }
779

    
780
        /**
781
         * Gets the window of the request in pixel coordinates
782
         */
783
         public Rectangle getRequestPxWindow() {
784
                 return pixelWindow;
785
         }
786

    
787
         /**
788
          * Gets the adjusted window of the request in pixel coordinates
789
          */
790
         public Rectangle getAdjustedRequestPxWindow() {
791
                 return adjustedPixelWindow;
792
         }
793

    
794
         /**
795
          * Gets a bounding box of a request in world coordinates.
796
          */
797
         public Extent getRequestBoundingBox() {
798
                 return requestBoundingBox;
799
         }
800

    
801
         /**
802
          * Sets a bounding box of a request in world coordinates.
803
          * @param requestBoundingBox
804
          */
805
         public void setRequestBoundingBox(Extent requestBoundingBox) {
806
                 this.requestBoundingBox = requestBoundingBox;
807
         }
808

    
809
         /**
810
          * Gets a bounding box of a request in world coordinates adjusted to the bounding box of the source.
811
          */
812
         public Extent getAdjustedRequestBoundingBox() {
813
                 return adjustedBoundingBox;
814
         }
815

    
816
         /**
817
          * Sets a bounding box of a request in world coordinates adjusted to the bounding box of the source.
818
          * @param requestBoundingBox
819
          */
820
         public void setAdjustedRequestBoundingBox(Extent adjustedBoundingBox) {
821
                 this.adjustedBoundingBox = adjustedBoundingBox;
822
         }
823

    
824
         public int getBufWidth() {
825
                 return widthForResampling;
826
         }
827

    
828
         public int getBufHeight() {
829
                 return heightForResampling;
830
         }
831

    
832
         public void setBufHeight(int h) {
833
                 heightForResampling = h;
834
         }
835

    
836
         public void setBufWidth(int w) {
837
                 widthForResampling = w;
838
         }
839

    
840
         public int getAdjustedBufWidth() {
841
                 return adjustedBufferWidth;
842
         }
843

    
844
         public int getAdjustedBufHeight() {
845
                 return adjustedBufferHeight;
846
         }
847

    
848
         public void setAdjustedBufHeight(int h) {
849
                 adjustedBufferHeight = h;
850
         }
851

    
852
         public void setAdjustedBufWidth(int w) {
853
                 adjustedBufferWidth = w;
854
         }
855

    
856
         public boolean isReadOnly() {
857
                 return readOnly;
858
         }
859

    
860
         public TileListener getTileListener() {
861
                 return listener;
862
         }
863

    
864
         public void setTileListener(TileListener listener) {
865
                 this.listener = listener;
866
         }
867

    
868
         public Time getTime() {
869
                 return time;
870
         }
871

    
872
         public void setTime(Time time) {
873
                 this.time = time;
874
         }
875

    
876
         public int getTileRow() {
877
                 return tileRow;
878
         }
879

    
880
         public int getTileCol() {
881
                 return tileCol;
882
         }
883

    
884
         public CacheStruct getCacheStruct() {
885
                 return cacheStruct;
886
         }
887

    
888
         public void setCacheStruct(CacheStruct cacheStruct) {
889
                 this.cacheStruct = cacheStruct;
890
         }
891

    
892
         public int getResolutionLevel() {
893
                 return resolutionLevel;
894
         }
895

    
896
         public void setTaskStatus(TaskStatus taskStatus) {
897
                 this.taskStatus = taskStatus;
898
         }
899

    
900
         public BandList getBandList() {
901
                 return bandList;
902
         }
903

    
904
         public void setBandList(BandList bandList) {
905
                 this.bandList = bandList;
906
         }
907

    
908
         /**
909
          * Buffer loaded by the provider and created by the store
910
          * @return
911
          */
912
         public Buffer getBufferForProviders() {
913
                 return bufferForProviders;
914
         }
915
         
916
         public void setBufferResult(Buffer buffer) {
917
                 this.bufferForProviders = buffer;
918
         }
919

    
920
         public void setBufferForProviders(Buffer buffer) throws QueryException {
921
                 if(buffer.getWidth() != getAdjustedBufWidth() || buffer.getHeight() != getAdjustedBufHeight())
922
                         throw new QueryException("Error in buffer size");
923
                 this.bufferForProviders = buffer;
924
         }
925

    
926
         public double[] getStep() {
927
                 return step;
928
         }
929

    
930
         /**
931
          * Gets the task status
932
          */
933
         public TaskStatus getTaskStatus() {
934
                 return taskStatus;
935
         }
936
}