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 / store / AbstractRasterDataStore.java @ 210

History | View | Annotate | Download (36.8 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22
package org.gvsig.raster.impl.store;
23

    
24
import java.awt.Dimension;
25
import java.awt.geom.Point2D;
26
import java.awt.geom.Rectangle2D;
27
import java.util.Iterator;
28
import java.util.Set;
29

    
30
import org.gvsig.fmap.dal.DataQuery;
31
import org.gvsig.fmap.dal.DataServerExplorer;
32
import org.gvsig.fmap.dal.DataSet;
33
import org.gvsig.fmap.dal.DataStore;
34
import org.gvsig.fmap.dal.DataStoreParameters;
35
import org.gvsig.fmap.dal.coverage.RasterLocator;
36
import org.gvsig.fmap.dal.coverage.dataset.Buffer;
37
import org.gvsig.fmap.dal.coverage.datastruct.Extent;
38
import org.gvsig.fmap.dal.coverage.exception.InvalidSetViewException;
39
import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException;
40
import org.gvsig.fmap.dal.coverage.exception.RasterDriverException;
41
import org.gvsig.fmap.dal.coverage.exception.RmfSerializerException;
42
import org.gvsig.fmap.dal.coverage.grid.render.Render;
43
import org.gvsig.fmap.dal.coverage.grid.render.TileListener;
44
import org.gvsig.fmap.dal.coverage.process.vector.Vectorization;
45
import org.gvsig.fmap.dal.coverage.store.RasterDataStore;
46
import org.gvsig.fmap.dal.coverage.store.RasterFileStoreParameters;
47
import org.gvsig.fmap.dal.coverage.store.RasterQuery;
48
import org.gvsig.fmap.dal.coverage.store.RasterStoreParameters;
49
import org.gvsig.fmap.dal.coverage.store.props.ColorTable;
50
import org.gvsig.fmap.dal.coverage.store.props.SerialInfo;
51
import org.gvsig.fmap.dal.coverage.util.RasterUtils;
52
import org.gvsig.fmap.dal.exception.DataException;
53
import org.gvsig.fmap.dal.exception.InitializeException;
54
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
55
import org.gvsig.fmap.dal.feature.FeatureStoreNotification;
56
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureStoreNotification;
57
import org.gvsig.fmap.dal.impl.DataStoreImplementation;
58
import org.gvsig.fmap.dal.impl.DefaultDataManager;
59
import org.gvsig.fmap.dal.raster.CoverageSelection;
60
import org.gvsig.fmap.dal.raster.CoverageStore;
61
import org.gvsig.fmap.dal.raster.spi.CoverageStoreProvider;
62
import org.gvsig.fmap.dal.raster.spi.CoverageStoreProviderServices;
63
import org.gvsig.fmap.dal.resource.Resource;
64
import org.gvsig.fmap.dal.spi.DataStoreProvider;
65
import org.gvsig.metadata.MetadataLocator;
66
import org.gvsig.metadata.MetadataManager;
67
import org.gvsig.metadata.exceptions.MetadataException;
68
import org.gvsig.raster.impl.DefaultRasterManager;
69
import org.gvsig.raster.impl.buffer.DefaultRasterQuery;
70
import org.gvsig.raster.impl.datastruct.ExtentImpl;
71
import org.gvsig.raster.impl.grid.render.DefaultRender;
72
import org.gvsig.raster.impl.process.vector.PotraceVectorization;
73
import org.gvsig.raster.impl.store.properties.DataStoreSerialInfo;
74
import org.gvsig.tools.ToolsLocator;
75
import org.gvsig.tools.dispose.impl.AbstractDisposable;
76
import org.gvsig.tools.dynobject.DelegatedDynObject;
77
import org.gvsig.tools.dynobject.DynClass;
78
import org.gvsig.tools.dynobject.DynObject;
79
import org.gvsig.tools.dynobject.DynObjectManager;
80
import org.gvsig.tools.dynobject.DynStruct;
81
import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException;
82
import org.gvsig.tools.dynobject.exception.DynMethodException;
83
import org.gvsig.tools.exception.BaseException;
84
import org.gvsig.tools.extensionpoint.ExtensionPoint;
85
import org.gvsig.tools.extensionpoint.ExtensionPointManager;
86
import org.gvsig.tools.observer.impl.DelegateWeakReferencingObservable;
87
import org.gvsig.tools.persistence.PersistentState;
88
import org.gvsig.tools.persistence.exception.PersistenceException;
89
import org.gvsig.tools.undo.command.Command;
90
import org.gvsig.tools.visitor.Visitor;
91

    
92
/**
93
 * Default implementation for RasterDataSource
94
 * 
95
 * @author Nacho Brodin (nachobrodin@gmail.com)
96
 */
97
public abstract class AbstractRasterDataStore extends AbstractDisposable 
98
        implements RasterDataStore, CoverageStoreProviderServices, DataStoreImplementation, QueryableRaster  {
99
        protected DefaultRasterQuery       currentQuery      = null;
100
        /**
101
         * Lista de paletas asociadas a las bandas cargadas en el DataSource. Estas son calculadas
102
         * en las funciones que asignan las bandas a dibujar (addDrawableBands)
103
         */
104
        private ColorTable[]                           palette           = null;
105
        /**
106
         * Extensi?n de los datos del buffer
107
         */
108
        private Extent                                           dataExtent        = null;
109
        /**
110
         * Ancho y alto en pixeles del ?ltimo buffer asignado
111
         */
112
        protected double                                nWidth            = 0;
113
        protected double                                nHeight           = 0;
114
        private double[]                   step              = null;
115
        private Buffer                     lastBuffer        = null;
116
        
117
        private DefaultDataManager         dataManager       = null;
118
        protected DataStoreParameters      parameters        = null;
119
        private DelegatedDynObject         metadata          = null;
120
        private RasterUtils                util              = null;
121
        private SerialInfo                 serialInfo        = null;
122
        private DelegateWeakReferencingObservable 
123
                                           delegateObservable = new DelegateWeakReferencingObservable(this);
124
        private Render                     render             = null;
125
        
126
        public AbstractRasterDataStore() {
127
                util = RasterLocator.getManager().getRasterUtils();
128
        }
129
        
130
        /*
131
         * (non-Javadoc)
132
         * @see org.gvsig.fmap.dal.coverage.buffer.RasterDataSource#query(org.gvsig.fmap.dal.coverage.buffer.RasterQuery)
133
         */
134
        public Buffer query(RasterQuery query)
135
                        throws ProcessInterruptedException, RasterDriverException,
136
                        InvalidSetViewException {
137
                currentQuery = (DefaultRasterQuery)query;
138
                
139
                if(currentQuery.getDrawableBands() != null)
140
                        setQueryDrawableBands(currentQuery.getDrawableBands());
141
                else 
142
                        setQueryAllDrawableBands();
143
                
144
                setMemoryBuffer(currentQuery.isMemoryBuffer());
145
                
146
                Buffer buf = null;
147
                if(currentQuery.getType() == DefaultRasterQuery.TYPE_ENTIRE) {
148
                        buf = setAreaOfInterest();
149
                        Extent e = getExtent();
150
                        buf.setDataExtent(new Rectangle2D.Double(e.getULX(), e.getULY(), e.getMax().getX() - e.getMin().getX(), e.getMax().getY() - e.getMin().getY()));
151
                }
152
                
153
                if(currentQuery.getType() == DefaultRasterQuery.TYPE_COORDS) {
154
                        buf = setAreaOfInterest(currentQuery.getX(), currentQuery.getY(), currentQuery.getW(), currentQuery.getH());
155
                        buf.setDataExtent(new Rectangle2D.Double(currentQuery.getX(), currentQuery.getY(), currentQuery.getW(), currentQuery.getH()));
156
                }
157
                
158
                if(currentQuery.getType() == DefaultRasterQuery.TYPE_COORDS_SIZE) {
159
                        buf = setAreaOfInterest(currentQuery.getUlx(), currentQuery.getUly(), currentQuery.getLrx(), currentQuery.getLry(), currentQuery.getBufWidth(), currentQuery.getBufHeight());
160
                        buf.setDataExtent(new Rectangle2D.Double(currentQuery.getUlx(), currentQuery.getUly(), currentQuery.getLrx() - currentQuery.getUlx(), currentQuery.getLry() - currentQuery.getUly()));
161
                }
162
                
163
                if(currentQuery.getType() == DefaultRasterQuery.TYPE_COORDS_SIZE_TILED) {
164
                        setAreaOfInterest(currentQuery.getUlx(), currentQuery.getUly(), currentQuery.getLrx(), currentQuery.getLry(), currentQuery.getBufWidth(), currentQuery.getBufHeight(), currentQuery.getTileListener(), currentQuery.getFrameWidthPx());
165
                }
166
                
167
                if(currentQuery.getType() == DefaultRasterQuery.TYPE_PX)
168
                        buf = setAreaOfInterest(currentQuery.getPixelX(), currentQuery.getPixelY(), currentQuery.getPixelW(), currentQuery.getPixelH());
169
                
170
                if(currentQuery.getType() == DefaultRasterQuery.TYPE_PX_SIZE)
171
                        buf = setAreaOfInterest(currentQuery.getPixelX(), currentQuery.getPixelY(), currentQuery.getPixelW(), currentQuery.getPixelH(), currentQuery.getBufWidth(), currentQuery.getBufHeight());
172
                
173
                if(currentQuery.isStoredLastBuffer())
174
                        lastBuffer = buf;
175
                else
176
                        lastBuffer = null;
177
                currentQuery.storeLastBuffer(false);
178
                
179
                if(buf != null) {
180
                        buf.setStore(this);
181
                        buf.setNoDataValue(getNoDataValue());
182
                        return buf;
183
                }
184
                return null;
185
        }
186
        
187
        /*
188
         * (non-Javadoc)
189
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#getAdjustedToExtent(org.gvsig.fmap.dal.coverage.datastruct.Extent)
190
         */
191
        public Extent adjustToExtent(Extent e) {
192
                Extent currentExtent = this.getExtent();
193
        
194
                double minx = e.getMin().getX() < currentExtent.getMin().getX() ? currentExtent.getMin().getX() : e.getMin().getX();
195
                double maxx = e.getMax().getX() > currentExtent.getMax().getX() ? currentExtent.getMax().getX() : e.getMax().getX();
196
                double miny = e.getMin().getY() < currentExtent.getMin().getY() ? currentExtent.getMin().getY() : e.getMin().getY();
197
                double maxy = e.getMax().getY() > currentExtent.getMax().getY() ? currentExtent.getMax().getY() : e.getMax().getY();
198
                
199
                return new ExtentImpl(minx, maxy, maxx, miny);
200
        }
201
        
202
        public String getFullName() {
203
                return getName();
204
        }
205
        
206
        /*
207
         * (non-Javadoc)
208
         * @see org.gvsig.fmap.dal.coverage.buffer.RasterDataSource#getLastBuffer()
209
         */
210
        public Buffer getLastBuffer() {
211
                return lastBuffer;
212
        }
213
        
214
        /*
215
         * (non-Javadoc)
216
         * @see org.gvsig.fmap.dal.coverage.buffer.RasterDataSource#getStep()
217
         */
218
        public double[] getStep() {
219
                return step;
220
        }
221
        
222
        /**
223
         * Asigna el ?rea de inter?s a toda la extensi?n del raster.
224
         */
225
        protected Buffer setAreaOfInterest() 
226
                throws ProcessInterruptedException, InvalidSetViewException, RasterDriverException {
227
                step = null;
228
                dataExtent = getExtent();
229
                return getWindowRaster(0, 0, (int)getWidth(), (int)getHeight());
230
        }
231
        
232
        /**
233
         * Asigna el ?rea de interes en coordenadas del mundo real. Si las coordenadas exceden del tama?o de la imagen
234
         * estas coordenadas son ajustadas el extent.
235
         * @param x Coordenada X, esquina superior izquierda
236
         * @param y Coordenada Y, esquina superior izquierda
237
         * @param w Ancho del ?rea
238
         * @param h Alto del ?rea
239
         * @throws ArrayIndexOutOfBoundsException
240
         * @throws InvalidSetViewException 
241
         */
242
        protected Buffer setAreaOfInterest(double x, double y, double w, double h)
243
                throws RasterDriverException, InvalidSetViewException, ProcessInterruptedException {
244
                step = null;
245
                dataExtent = new ExtentImpl(x, y, x + w, y - h);
246

    
247
                Extent adjustedDataExtent = util.calculateAdjustedView(dataExtent, getAffineTransform(), new Dimension((int)getWidth(), (int)getHeight()));
248
                
249
                return getWindowRaster(adjustedDataExtent.getMin().getX(), adjustedDataExtent.getMax().getY(), adjustedDataExtent.width(), adjustedDataExtent.height(), currentQuery.isAdjustToExtent());
250
        }
251
        
252
        public void setAreaOfInterest(double ulx, double uly, double lrx, double lry, 
253
                        int bufWidth, int bufHeight, TileListener listener, int frameWidthPx) throws RasterDriverException, InvalidSetViewException, ProcessInterruptedException { 
254
                dataExtent = new ExtentImpl(ulx, uly, lrx, lry);
255
                Extent adjustedDataExtent = util.calculateAdjustedView(dataExtent, getAffineTransform(), new Dimension((int)getWidth(), (int)getHeight()));        
256
                getWindowRaster(adjustedDataExtent.getULX(), adjustedDataExtent.getULY(), adjustedDataExtent.getLRX(), adjustedDataExtent.getLRY(), bufWidth, bufHeight, listener, frameWidthPx);
257
        }
258
        
259
        /**
260
         * Asigna el ?rea de interes en coordenadas del mundo real. Si las coordenadas exceden del tama?o de la imagen
261
         * estas coordenadas son ajustadas el extent.
262
         * @param x Coordenada X, esquina superior izquierda
263
         * @param y Coordenada Y, esquina superior izquierda
264
         * @param w Ancho del ?rea
265
         * @param h Alto del ?rea
266
         * @param bufWidth Ancho del buffer
267
         * @param bufHeight Alto del buffer
268
         * @return En caso de que el buffer sea mayor que el tama?o seleccionado de raster se produce supersampleo. La funci?n devuelve
269
         * un array de dos elementos que representan el desplazamiento en pixels de X e Y de la esquina superior izquierda.
270
         * @throws ArrayIndexOutOfBoundsException
271
         * @throws InvalidSetViewException 
272
         */
273
        protected Buffer setAreaOfInterest(double ulx, double uly, double lrx, double lry, int bufWidth, int bufHeight)
274
                throws RasterDriverException, InvalidSetViewException, ProcessInterruptedException {
275
                step = null;
276
                dataExtent = new ExtentImpl(ulx, uly, lrx, lry);
277
                Extent adjustedDataExtent = util.calculateAdjustedView(dataExtent, getAffineTransform(), new Dimension((int)getWidth(), (int)getHeight()));
278

    
279
                //Caso 3D: La petici?n no se ajusta al ?rea y se rellena el exterior con NoData
280
                if(!currentQuery.isAdjustToExtent() && !util.isInside(dataExtent, getExtent())) { 
281
                        return requestFillingWithNoData(dataExtent, adjustedDataExtent, bufWidth, bufHeight);
282
                }
283
                
284
                //Esta secci?n es para que no supersamplee el driver y pueda hacerse en el cliente
285
                if(!currentQuery.isSupersamplingLoadingBuffer()) {
286
                        //nWidth = ((adjustedDataExtent.width() * mDataset.getDataset(0).getWidth()) / mDataset.getExtentForRequest().width());
287
                        //nHeight = ((adjustedDataExtent.height() * mDataset.getDataset(0).getHeight()) / mDataset.getExtentForRequest().height());
288
                        Point2D p1 = worldToRaster(new Point2D.Double(adjustedDataExtent.getULX(), adjustedDataExtent.getULY()));
289
                        Point2D p2 = worldToRaster(new Point2D.Double(adjustedDataExtent.getLRX(), adjustedDataExtent.getLRY()));
290
                        nWidth = Math.abs(p1.getX() - p2.getX());
291
                        nHeight = Math.abs(p1.getY() - p2.getY());
292

    
293
                        if(bufWidth > Math.ceil(nWidth) && bufHeight > Math.ceil(nHeight)) {
294
                                step = calcSteps(adjustedDataExtent.getULX(), adjustedDataExtent.getULY(), adjustedDataExtent.getLRX(), adjustedDataExtent.getLRY(), nWidth, nHeight, bufWidth, bufHeight);
295
                                return  getWindowRaster(adjustedDataExtent.getULX(), adjustedDataExtent.getULY(), adjustedDataExtent.getLRX(), adjustedDataExtent.getLRY());
296
                        }
297
                }
298
                return getWindowRaster(adjustedDataExtent.getULX(), adjustedDataExtent.getULY(), adjustedDataExtent.getLRX(), adjustedDataExtent.getLRY(), bufWidth, bufHeight, true /*Siempre ajustado*/);
299
        }
300
        
301
        /**
302
         * Asigna el ?rea de interes en coordenadas pixel. Si las coordenadas exceden del tama?o de la imagen
303
         * lanza una excepci?n.
304
         * @param x Coordenada X, esquina superior izquierda
305
         * @param y Coordenada Y, esquina superior izquierda
306
         * @param w Ancho del ?rea
307
         * @param h Alto del ?rea
308
         * @throws InvalidSetViewException  
309
         * @throws ArrayIndexOutOfBoundsException
310
         */
311
        protected Buffer setAreaOfInterest(int x, int y, int w, int h) 
312
                throws InvalidSetViewException, ProcessInterruptedException, RasterDriverException {
313
                step = null;
314
                if(x > getSourceWidth() || y > getSourceHeight())
315
                        throw new InvalidSetViewException("Par?metros incorrectos en setAreaOfInterest");
316
                x = (x < 0) ? 0 : x;
317
                y = (y < 0) ? 0 : y;
318
                w = (w > getSourceWidth()) ? getSourceWidth() : w;
319
                h = (h > getSourceHeight()) ? getSourceHeight() : h;
320

    
321
                dataExtent = new ExtentImpl(rasterToWorld(new Point2D.Double(x, y)),
322
                                rasterToWorld(new Point2D.Double(x + w, y + h)));
323
                return getWindowRaster(x, y, w, h);
324
        }
325
        
326
        /**
327
         * Asigna el ?rea de interes en coordenadas pixel. Esta operaci?n cargar? un RasterBuffer con los datos solicitados por
328
         * lo que, si al acabar hacemos getRasterBuf obtendremos la matriz de datos. Si las coordenadas exceden del tama?o
329
         * de la imagen lanza una excepci?n.
330
         * @param x Coordenada X, esquina superior izquierda
331
         * @param y Coordenada Y, esquina superior izquierda
332
         * @param w Ancho del ?rea
333
         * @param h Alto del ?rea
334
         * @param bufWidth Ancho del buffer
335
         * @param bufHeight Alto del buffer
336
         * @throws ArrayIndexOutOfBoundsException
337
         */
338
        protected Buffer setAreaOfInterest(int x, int y, int w, int h, int bufWidth, int bufHeight)
339
                throws InvalidSetViewException, ProcessInterruptedException, RasterDriverException {
340
                step = null;
341
                if(x > getSourceWidth() || y > getSourceHeight())
342
                        throw new InvalidSetViewException("Par?metros incorrectos en setAreaOfInterest");
343
                
344
                x = (x < 0) ? 0 : x;
345
                y = (y < 0) ? 0 : y;
346
                w = (w > getSourceWidth()) ? getSourceWidth() : w;
347
                h = (h > getSourceHeight()) ? getSourceHeight() : h;
348

    
349
                dataExtent = new ExtentImpl(rasterToWorld(new Point2D.Double(x, y)),
350
                                                                rasterToWorld(new Point2D.Double(x + w, y + h)));
351

    
352
                return getWindowRaster(x, y, w, h, bufWidth, bufHeight);
353
        }
354

    
355
        
356
        /**
357
         * Para este GeoRasterFile asigna que bandas se pintaran
358
         * sobre el RasterBuf cuando se haga un update. Cada posici?n del vector es una banda
359
         * del rasterBuf y el contenido de esa posici?n es la banda de la imagen que se dibujar?
360
         * sobre ese RasterBuf.
361
         * @param drawableBands        Array con las bandas a dibujar.
362
         * @return array con tantos elementos como bandas a dibujar. El valor contenido es el fichero del
363
         * dataset multifichero al que corresponde la banda.
364
         */
365
        private void setQueryDrawableBands(int[] drawableBands) {
366
                clearDrawableBands();
367
                setDrawableBands(drawableBands);
368

    
369
                int[] files = new int[drawableBands.length];
370
                palette = new ColorTable[drawableBands.length];
371

    
372
                for(int i = 0; i< drawableBands.length; i++) {
373
                        if(drawableBands[i] < 0 || drawableBands[i] >= getBandCount())
374
                                continue;
375
                        addDrawableBand(i, drawableBands[i]);
376
                        String fileName = getBands().getBand(drawableBands[i]).getFileName();
377
                        files[i] = getBands().getFileNumber(fileName);
378
                        palette[i] = getColorTable(fileName);
379
                }
380
        }
381
        
382
        /**
383
         * Para este GeoRasterFile asigna que bandas se pintaran
384
         * sobre el RasterBuf cuando se haga un update. Cada posici?n del vector es una banda
385
         * del rasterBuf y el contenido de esa posici?n es la banda de la imagen que se dibujar?
386
         * sobre ese RasterBuf. Esta llamada asigna todas las bandas dibujables en su orden natural.
387
         * @return array con tantos elementos como bandas a dibujar. El valor contenido es el fichero del
388
         * dataset multifichero al que corresponde la banda.
389
         */
390
        public int[] setQueryAllDrawableBands() {
391
                clearDrawableBands();
392
                
393
                int[] list = new int[getBandCount()];
394
                for(int i = 0; i< getBandCount(); i++)
395
                        list[i] = i;
396
                setDrawableBands(list);
397
                
398
                int[] files = new int[getBandCount()];
399
                palette = new ColorTable[getBandCount()];
400

    
401
                for(int i = 0; i< getBandCount(); i++) {
402
                        addDrawableBand(i, i);
403
                        String fileName = getBands().getBand(i).getFileName();
404
                        files[i] = getBands().getFileNumber(fileName);
405
                        palette[i] = getColorTable(fileName);
406
                }
407
                return files;
408
        }
409
        
410
        /**
411
         * M?todo que crea un buffer con la extensi?n que se ha pedido completa y sin ajustar
412
         * a la extensi?n del raster. La zona que tenga informaci?n del raster se rellenara con 
413
         * esta y la que quede vacia se rellenar? con valores NoData. El ancho y alto del buffer corresponden
414
         * a toda la zona solicitada, tanto la que cae dentro como la rellena con Nodata
415
         * 
416
         * @param rasterBuf Buffer de salida.
417
         * @throws InvalidSetViewException 
418
         */
419
        private Buffer requestFillingWithNoData(Extent requestExtent, Extent fitExtent, int bufWidth, int bufHeight) 
420
                throws InvalidSetViewException, ProcessInterruptedException, RasterDriverException {
421
                double error = 0.01;
422
                //Upper Left
423
                double distWcX = Math.abs(fitExtent.getMin().getX() - dataExtent.getMin().getX());
424
                distWcX = (distWcX > error) ? distWcX : 0;
425
                double distWcY = Math.abs(fitExtent.getMax().getY() - dataExtent.getMax().getY());
426
                distWcY = (distWcY > error) ? distWcY : 0;
427
                //Pixel inicial del buffer donde se empieza a dibujar. Redondeamos por arriba pq lo que sobra se pone NoData
428
                double initPxX = Math.ceil((distWcX * bufWidth) / requestExtent.width()); 
429
                double initPxY = Math.ceil((distWcY * bufHeight) / requestExtent.height());
430

    
431
                //Lower Right
432
                distWcX = Math.abs(fitExtent.getMax().getX() - dataExtent.getMin().getX());
433
                distWcX = (distWcX > error) ? distWcX : 0;
434
                distWcY = Math.abs(fitExtent.getMin().getY() - dataExtent.getMax().getY());
435
                distWcY = (distWcY > error) ? distWcY : 0;
436
                //Pixel final del buffer donde se dibuja. Redondeamos por abajo pq lo que sobra se pone NoData
437
                double endPxX = Math.floor((distWcX * bufWidth) / requestExtent.width()); 
438
                double endPxY = Math.floor((distWcY * bufHeight) / requestExtent.height());
439

    
440
                int copyX = (int)Math.abs(endPxX - initPxX);
441
                int copyY = (int)Math.abs(endPxY - initPxY);
442

    
443
                Buffer rasterBuf = getWindowRaster(fitExtent.getULX(), fitExtent.getULY(), fitExtent.getLRX(), fitExtent.getLRY(), copyX, copyY, true);
444
                Buffer buf = DefaultRasterManager.getInstance().createBuffer(getDataType()[0], bufWidth, bufHeight, rasterBuf.getBandCount(), true);
445
                buf.setNoDataValue(currentQuery.getNoDataValueToFill());
446
                for(int i = 0; i < buf.getBandCount(); i++) {
447
                        switch(buf.getDataType()) {
448
                        case Buffer.TYPE_BYTE:buf.assign(i, buf.getByteNoDataValue());break;
449
                        case Buffer.TYPE_SHORT:buf.assign(i, buf.getShortNoDataValue());break;
450
                        case Buffer.TYPE_INT:buf.assign(i, buf.getIntNoDataValue());break;
451
                        case Buffer.TYPE_FLOAT:buf.assign(i, buf.getFloatNoDataValue());break;
452
                        case Buffer.TYPE_DOUBLE:buf.assign(i, buf.getNoDataValue());break;
453
                        }
454
                }        
455

    
456
                switch(rasterBuf.getDataType()) {
457
                case Buffer.TYPE_BYTE:
458
                        for (int iBand = 0; iBand < rasterBuf.getBandCount(); iBand++)        
459
                                for (int row = 0; row < copyY; row++) 
460
                                        for (int col = 0; col < copyX; col++) 
461
                                                buf.setElem((int)(row + initPxY), (int)(col + initPxX), 
462
                                                                iBand, 
463
                                                                rasterBuf.getElemByte(row, col, iBand));
464
                        break;
465
                case Buffer.TYPE_SHORT:
466
                case Buffer.TYPE_USHORT:
467
                        for (int iBand = 0; iBand < rasterBuf.getBandCount(); iBand++)        
468
                                for (int row = 0; row < copyY; row++) 
469
                                        for (int col = 0; col < copyX; col++) 
470
                                                buf.setElem((int)(row + initPxY), (int)(col + initPxX), 
471
                                                                iBand, 
472
                                                                rasterBuf.getElemShort(row, col, iBand));
473
                        break;
474
                case Buffer.TYPE_INT:
475
                        for (int iBand = 0; iBand < rasterBuf.getBandCount(); iBand++)        
476
                                for (int row = 0; row < copyY; row++) 
477
                                        for (int col = 0; col < copyX; col++) 
478
                                                buf.setElem((int)(row + initPxY), (int)(col + initPxX), 
479
                                                                iBand, 
480
                                                                rasterBuf.getElemInt(row, col, iBand));
481
                        break;
482
                case Buffer.TYPE_FLOAT:
483
                        for (int iBand = 0; iBand < rasterBuf.getBandCount(); iBand++)        
484
                                for (int row = 0; row < copyY; row++) 
485
                                        for (int col = 0; col < copyX; col++) 
486
                                                buf.setElem((int)(row + initPxY), (int)(col + initPxX), 
487
                                                                iBand, 
488
                                                                rasterBuf.getElemFloat(row, col, iBand));
489
                        break;
490
                case Buffer.TYPE_DOUBLE:
491
                        for (int iBand = 0; iBand < rasterBuf.getBandCount(); iBand++)        
492
                                for (int row = 0; row < copyY; row++) 
493
                                        for (int col = 0; col < copyX; col++) 
494
                                                buf.setElem((int)(row + initPxY), (int)(col + initPxX), 
495
                                                                iBand, 
496
                                                                rasterBuf.getElemDouble(row, col, iBand));
497
                        break;
498
                }
499
                return buf;
500
        }
501
        
502
        /*
503
         * (non-Javadoc)
504
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#getSerialInfo()
505
         */
506
        public SerialInfo getSerialInfo() throws RmfSerializerException {
507
                if(serialInfo == null) {
508
                        serialInfo =  new DataStoreSerialInfo();
509
                        loadSerialInfoFromRmf(serialInfo);
510
                        //Seleccionamos la primera serie por defecto. El usuario seleccionar? otra si la necesita
511
                        serialInfo.selectSerial(0);
512
                }
513
                return serialInfo;
514
        }
515
        
516
        /*
517
         * (non-Javadoc)
518
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#setSerialInfo()
519
         */
520
        public void setSerialInfo(SerialInfo serialInfo) throws RmfSerializerException {
521
                this.serialInfo = serialInfo;
522
                saveSerialInfoToRmf(serialInfo);
523
        }
524
        
525
        /*
526
         * (non-Javadoc)
527
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#saveColorTableToRmf(org.gvsig.fmap.dal.coverage.store.props.ColorTable)
528
         */
529
        public abstract void saveColorTableToRmf(ColorTable table) throws RmfSerializerException;
530
        
531
        
532
        /**
533
         * Saves information about serials
534
         * @param object to save
535
         * @throws RmfSerializerException
536
         */
537
        public abstract void saveSerialInfoToRmf(SerialInfo serialInfo) throws RmfSerializerException;
538
        
539
        /**
540
         * Loads information about serials
541
         * @param object to load
542
         * @throws RmfSerializerException
543
         */
544
        public abstract boolean loadSerialInfoFromRmf(SerialInfo serialInfo);
545
        
546
        /*
547
         * (non-Javadoc)
548
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#createVectorizeObject(org.gvsig.fmap.dal.coverage.store.RasterDataStore)
549
         */
550
        public Vectorization createVectorizeObject() throws RasterDriverException, ProcessInterruptedException {
551
                return new PotraceVectorization((RasterDataStore)this);
552
        }
553
        
554
        /*
555
         * (non-Javadoc)
556
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#getRender()
557
         */
558
        public Render getRender() {
559
                if(render == null)
560
                        render = new DefaultRender(this);
561
                return render;
562
        }
563
        
564
        /*
565
         * (non-Javadoc)
566
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#getRender()
567
         */
568
        public void setRender(Render render) {
569
                this.render = render;
570
        }
571
        
572
        /**
573
         * Obtiene la altura de la fuente de datos.
574
         * @return altura en celdas del grid.
575
         */
576
        private int getSourceHeight() {
577
                return (int)getHeight();
578
        }
579

    
580
        /**
581
         * Obtiene la anchura de la fuente de datos.
582
         * @return anchura en celdas del grid.
583
         */
584
        private int getSourceWidth() {
585
                return (int)getWidth();
586
        }
587
        
588
        /*
589
         * (non-Javadoc)
590
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#isFileSupported(java.lang.String)
591
         */
592
        public boolean isFileSupported(String fName) {
593
                ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager();
594
                ExtensionPoint point = extensionPoints.get("RasterReader");
595
                return point.has(RasterLocator.getManager().getFileUtils().getExtensionFromFileName(fName));
596
        }
597
        
598
        /**
599
         * Registers metadata definition
600
         * @throws MetadataException
601
         */
602
        public static void registerMetadataDefinition() throws MetadataException {
603
                MetadataManager manager = MetadataLocator.getMetadataManager();
604
                
605
                if(manager == null)
606
                        return;
607
                
608
                if( manager.getDefinition(METADATA_DEFINITION_NAME) == null  ) {
609
                        DynStruct defnition = manager.addDefinition(
610
                                        METADATA_DEFINITION_NAME,
611
                                        METADATA_DEFINITION_NAME 
612
                        );
613
                        defnition.extend(
614
                                        MetadataManager.METADATA_NAMESPACE, 
615
                                        DataStore.METADATA_DEFINITION_NAME
616
                        );
617
                }
618
        }
619
        
620
    //****************************************************
621
        //****Implementing DataStoreImplementation methods****
622
        //****************************************************
623

    
624
        /*
625
         * (non-Javadoc)
626
         * @see org.gvsig.fmap.dal.impl.DataStoreImplementation#intializePhase1(org.gvsig.fmap.dal.impl.DefaultDataManager, org.gvsig.fmap.dal.DataStoreParameters)
627
         */
628
        public void intializePhase1(DefaultDataManager dataManager,
629
                        DataStoreParameters parameters) throws InitializeException {
630
                DynObjectManager dynManager = ToolsLocator.getDynObjectManager();
631

    
632
                this.metadata = (DelegatedDynObject) dynManager
633
                                .createDynObject( 
634
                                                MetadataLocator.getMetadataManager().getDefinition(DataStore.SPATIAL_METADATA_DEFINITION_NAME)
635
                                );
636
                this.dataManager = dataManager;
637
                this.parameters = parameters;
638

    
639
        }
640

    
641
        /*
642
         * (non-Javadoc)
643
         * @see org.gvsig.fmap.dal.impl.DataStoreImplementation#intializePhase2(org.gvsig.fmap.dal.spi.DataStoreProvider)
644
         */
645
        public void intializePhase2(DataStoreProvider provider)
646
                        throws InitializeException {
647
        }
648
        
649
        /* (non-Javadoc)
650
         * @see org.gvsig.fmap.dal.raster.spi.CoverageStoreProviderServices#getManager()
651
         */
652
        public DefaultDataManager getManager() {
653
                return this.dataManager;
654
        }        
655

    
656
        //****************************************************
657
        //*********Implementing DataStore methods*************
658
        //****************************************************
659

    
660
        /*
661
         * (non-Javadoc)
662
         * @see org.gvsig.fmap.dal.DataStore#getProviderName()
663
         */
664
        public String getProviderName() {
665
                return null;
666
        }
667
        
668
        /*
669
         * (non-Javadoc)
670
         * @see org.gvsig.fmap.dal.DataStore#refresh()
671
         */
672
        public void refresh() throws DataException {
673
                return;
674
        }
675

    
676
        /*
677
         * (non-Javadoc)
678
         * @see org.gvsig.fmap.dal.DataStore#getDataSet()
679
         */
680
        public DataSet getDataSet() throws DataException {
681
                return null;
682
        }
683

    
684
        /*
685
         * (non-Javadoc)
686
         * @see org.gvsig.fmap.dal.DataStore#getDataSet(org.gvsig.fmap.dal.DataQuery)
687
         */
688
        public DataSet getDataSet(DataQuery dataQuery) throws DataException {
689
                return null;
690
        }
691

    
692
        /*
693
         * (non-Javadoc)
694
         * @see org.gvsig.fmap.dal.DataStore#accept(org.gvsig.tools.visitor.Visitor, org.gvsig.fmap.dal.DataQuery)
695
         */
696
        public void accept(Visitor visitor, DataQuery dataQuery)
697
                        throws BaseException {
698
        }
699

    
700
        /*
701
         * (non-Javadoc)
702
         * @see org.gvsig.fmap.dal.DataStore#getDataSet(org.gvsig.tools.observer.Observer)
703
         */
704
        public void getDataSet(org.gvsig.tools.observer.Observer observer) throws DataException {
705
        }
706

    
707
        /*
708
         * (non-Javadoc)
709
         * @see org.gvsig.fmap.dal.DataStore#getDataSet(org.gvsig.fmap.dal.DataQuery, org.gvsig.tools.observer.Observer)
710
         */
711
        public void getDataSet(DataQuery dataQuery, org.gvsig.tools.observer.Observer observer) throws DataException {
712
        }
713

    
714
        /*
715
         * (non-Javadoc)
716
         * @see org.gvsig.fmap.dal.DataStore#getSelection()
717
         */
718
        public DataSet getSelection() throws DataException {
719
                return null;
720
        }
721

    
722
        /*
723
         * (non-Javadoc)
724
         * @see org.gvsig.fmap.dal.DataStore#setSelection(org.gvsig.fmap.dal.DataSet)
725
         */
726
        public void setSelection(DataSet selection) throws DataException {
727
        }
728

    
729
        /*
730
         * (non-Javadoc)
731
         * @see org.gvsig.fmap.dal.DataStore#createSelection()
732
         */
733
        public DataSet createSelection() throws DataException {
734
                return null;
735
        }
736

    
737
        /*
738
         * (non-Javadoc)
739
         * @see org.gvsig.fmap.dal.DataStore#getChildren()
740
         */
741
        @SuppressWarnings("unchecked")
742
        public Iterator getChildren() {
743
                return null;
744
        }
745

    
746
        /*
747
         * (non-Javadoc)
748
         * @see org.gvsig.fmap.dal.DataStore#getExplorer()
749
         */
750
        public DataServerExplorer getExplorer() throws DataException, ValidateDataParametersException {
751
                return null;
752
        }
753
        
754
        /*
755
         * (non-Javadoc)
756
         * @see org.gvsig.fmap.dal.DataStore#createQuery()
757
         */
758
        public DataQuery createQuery() {
759
                return new DefaultRasterQuery();
760
        }
761
        
762
        //****************************************************
763
        //*********Implementing Persistent methods*************
764
        //****************************************************
765
        
766
        /*
767
         * (non-Javadoc)
768
         * @see org.gvsig.tools.persistence.Persistent#saveToState(org.gvsig.tools.persistence.PersistentState)
769
         */
770
        public void saveToState(PersistentState state) throws PersistenceException {
771
                
772
        }
773

    
774
        /*
775
         * (non-Javadoc)
776
         * @see org.gvsig.tools.persistence.Persistent#loadFromState(org.gvsig.tools.persistence.PersistentState)
777
         */
778
        public void loadFromState(PersistentState state) throws PersistenceException {
779
                
780
        }
781
        
782
        //****************************************************
783
        //*********Implementing Metadata methods*************
784
        //****************************************************
785
        
786
    /*
787
     * (non-Javadoc)
788
     * @see org.gvsig.metadata.Metadata#getMetadataID()
789
     */
790
    public Object getMetadataID() {
791
            if(parameters != null && parameters instanceof RasterFileStoreParameters)
792
                    return ((RasterFileStoreParameters)parameters).getFile();
793
            if(parameters != null && parameters instanceof RasterStoreParameters)
794
                    return ((RasterStoreParameters)parameters).getURI();
795
            return null;
796
    }
797

    
798
   /*
799
    * (non-Javadoc)
800
    * @see org.gvsig.metadata.Metadata#getMetadataChildren()
801
    */
802
    @SuppressWarnings("unchecked")
803
        public Set getMetadataChildren() {
804
            return null;
805
    }
806
    
807
    //****************************************************
808
        //*********Implementing Disposable methods************
809
        //****************************************************
810
    
811
    /*
812
     * (non-Javadoc)
813
     * @see org.gvsig.tools.dispose.impl.AbstractDisposable#doDispose()
814
     */
815
    public void doDispose() {
816
            
817
    }
818
    
819
    //****************************************************
820
        //*********Implementing Visitable methods*************
821
        //****************************************************
822
    
823
    /*
824
     * (non-Javadoc)
825
     * @see org.gvsig.tools.visitor.Visitable#accept(org.gvsig.tools.visitor.Visitor)
826
     */
827
    public void accept(Visitor visitor) throws BaseException {
828
            
829
    }
830
    
831
    //****************************************************
832
        //****Implementing ComplexObservable methods**********
833
        //****************************************************
834
    
835
   /*
836
    * (non-Javadoc)
837
    * @see org.gvsig.tools.observer.ComplexObservable#disableNotifications()
838
    */
839
    public void disableNotifications() {
840
                
841
        }
842

    
843
        /*
844
         * (non-Javadoc)
845
         * @see org.gvsig.tools.observer.ComplexObservable#enableNotifications()
846
         */
847
    public void enableNotifications() {
848
                
849
        }
850

    
851
        /*
852
         * (non-Javadoc)
853
         * @see org.gvsig.tools.observer.ComplexObservable#beginComplexNotification()
854
         */
855
    public void beginComplexNotification() {
856
                
857
        }
858

    
859
        /*
860
         * (non-Javadoc)
861
         * @see org.gvsig.tools.observer.ComplexObservable#endComplexNotification()
862
         */
863
    public void endComplexNotification() {
864
                
865
        }
866
    
867
    //****************************************************
868
        //********Implementing Observable methods*************
869
        //****************************************************
870
    
871
    /*
872
     * (non-Javadoc)
873
     * @see org.gvsig.tools.observer.Observable#addObserver(org.gvsig.tools.observer.Observer)
874
     */
875
        public void addObserver(org.gvsig.tools.observer.Observer o) {
876
                
877
        }
878

    
879
        /*
880
         * (non-Javadoc)
881
         * @see org.gvsig.tools.observer.Observable#deleteObserver(org.gvsig.tools.observer.Observer)
882
         */
883
        public void deleteObserver(org.gvsig.tools.observer.Observer o) {
884
                
885
        }
886

    
887
        /*
888
         * (non-Javadoc)
889
         * @see org.gvsig.tools.observer.Observable#deleteObservers()
890
         */
891
        public void deleteObservers() {
892
                
893
        }
894
        
895
    //****************************************************
896
        //********Implementing DynObject methods*************
897
        //****************************************************
898
        
899
        /*
900
         * (non-Javadoc)
901
         * @see org.gvsig.tools.dynobject.DynObject#getDynClass()
902
         */
903
        public DynClass getDynClass() {
904
                return this.metadata.getDynClass();
905
        }
906

    
907
        /*
908
         * (non-Javadoc)
909
         * @see org.gvsig.tools.dynobject.DynObject#implement(org.gvsig.tools.dynobject.DynClass)
910
         */
911
        public void implement(DynClass dynClass) {
912
                this.metadata.implement(dynClass);
913
    }
914

    
915
        /*
916
         * (non-Javadoc)
917
         * @see org.gvsig.tools.dynobject.DynObject#delegate(org.gvsig.tools.dynobject.DynObject)
918
         */
919
        public void delegate(DynObject dynObject) {
920
                this.metadata.delegate(dynObject);
921
    }
922

    
923
        /*
924
         * (non-Javadoc)
925
         * @see org.gvsig.tools.dynobject.DynObject#getDynValue(java.lang.String)
926
         */
927
        public Object getDynValue(String name)  throws DynFieldNotFoundException {
928
                return this.metadata.getDynValue(name);
929
        }
930

    
931
        /*
932
         * (non-Javadoc)
933
         * @see org.gvsig.tools.dynobject.DynObject#setDynValue(java.lang.String, java.lang.Object)
934
         */
935
        public void setDynValue(String name, Object value)  throws DynFieldNotFoundException {
936
                this.metadata.setDynValue(name, value);
937
    }
938

    
939
        /*
940
         * (non-Javadoc)
941
         * @see org.gvsig.tools.dynobject.DynObject#hasDynValue(java.lang.String)
942
         */
943
        public boolean hasDynValue(String name) {
944
                return this.metadata.hasDynValue(name);
945
        }
946

    
947
        /*
948
         * (non-Javadoc)
949
         * @see org.gvsig.tools.dynobject.DynObject#invokeDynMethod(java.lang.String, org.gvsig.tools.dynobject.DynObject)
950
         */
951
        public Object invokeDynMethod(String name, DynObject context) throws DynMethodException {
952
                return this.metadata.invokeDynMethod(this, name, context);
953
        }
954

    
955
        /*
956
         * (non-Javadoc)
957
         * @see org.gvsig.tools.dynobject.DynObject#invokeDynMethod(int, org.gvsig.tools.dynobject.DynObject)
958
         */
959
        public Object invokeDynMethod(int code, DynObject context) throws DynMethodException {
960
                return this.metadata.invokeDynMethod(this, code, context);
961
        }
962

    
963
        /*
964
         * (non-Javadoc)
965
         * @see org.gvsig.tools.dynobject.DynObject#clear()
966
         */
967
        public void clear() {
968
                if (metadata != null) {
969
                        metadata.clear();
970
                }
971
    }
972
        
973
        /*
974
         * (non-Javadoc)
975
         * @see org.gvsig.fmap.dal.DataStore#getParameters()
976
         */
977
        public DataStoreParameters getParameters() {
978
                return parameters;
979
        }
980

    
981
        //***********************************************************************
982
        //********Implementing CoverageStoreProviderServices methods*************
983
        //***********************************************************************
984

    
985
        public CoverageSelection createDefaultCoverageSelection()
986
        throws DataException {
987
                return null;
988
        }
989

    
990
        /*
991
         * (non-Javadoc)
992
         * @see org.gvsig.fmap.dal.raster.spi.CoverageStoreProviderServices#getCoverageStore()
993
         */
994
        public CoverageStore getCoverageStore() {
995
                return this;
996
        }
997

    
998
        /*
999
         * (non-Javadoc)
1000
         * @see org.gvsig.fmap.dal.raster.spi.CoverageStoreProviderServices#getProvider()
1001
         */
1002
        public abstract CoverageStoreProvider getProvider();
1003

    
1004
        /*
1005
         * (non-Javadoc)
1006
         * @see org.gvsig.fmap.dal.raster.spi.CoverageStoreProviderServices#notifyChange(java.lang.String)
1007
         */
1008
        public void notifyChange(String notification) {
1009
                delegateObservable
1010
                .notifyObservers(new DefaultCoverageStoreNotification(
1011
                                this, notification));
1012
        }
1013

    
1014
        /*
1015
         * (non-Javadoc)
1016
         * @see org.gvsig.fmap.dal.raster.spi.CoverageStoreProviderServices#notifyChange(java.lang.String, org.gvsig.fmap.dal.resource.Resource)
1017
         */
1018
        public void notifyChange(String arg0, Resource arg1) {
1019
                delegateObservable.notifyObservers(new DefaultFeatureStoreNotification(
1020
                                this, FeatureStoreNotification.RESOURCE_CHANGED));
1021
        }
1022

    
1023
        /*
1024
         * (non-Javadoc)
1025
         * @see org.gvsig.fmap.dal.raster.spi.CoverageStoreProviderServices#notifyChange(java.lang.String, org.gvsig.tools.undo.command.Command)
1026
         */
1027
        public void notifyChange(String notification, Command command) {
1028
                delegateObservable
1029
                .notifyObservers(new DefaultCoverageStoreNotification(
1030
                                this, notification, command));
1031
        }
1032

    
1033
        /*
1034
         * (non-Javadoc)
1035
         * @see org.gvsig.fmap.dal.spi.DataStoreProviderServices#getStore()
1036
         */
1037
        public DataStore getStore() {
1038
                return this;
1039
        }
1040
}