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 @ 186

History | View | Annotate | Download (35.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.process.vector.Vectorization;
44
import org.gvsig.fmap.dal.coverage.store.RasterDataStore;
45
import org.gvsig.fmap.dal.coverage.store.RasterFileStoreParameters;
46
import org.gvsig.fmap.dal.coverage.store.RasterQuery;
47
import org.gvsig.fmap.dal.coverage.store.RasterStoreParameters;
48
import org.gvsig.fmap.dal.coverage.store.props.ColorTable;
49
import org.gvsig.fmap.dal.coverage.store.props.SerialInfo;
50
import org.gvsig.fmap.dal.coverage.util.RasterUtils;
51
import org.gvsig.fmap.dal.exception.DataException;
52
import org.gvsig.fmap.dal.exception.InitializeException;
53
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
54
import org.gvsig.fmap.dal.feature.FeatureStoreNotification;
55
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureStoreNotification;
56
import org.gvsig.fmap.dal.impl.DataStoreImplementation;
57
import org.gvsig.fmap.dal.impl.DefaultDataManager;
58
import org.gvsig.fmap.dal.raster.CoverageSelection;
59
import org.gvsig.fmap.dal.raster.CoverageStore;
60
import org.gvsig.fmap.dal.raster.spi.CoverageStoreProvider;
61
import org.gvsig.fmap.dal.raster.spi.CoverageStoreProviderServices;
62
import org.gvsig.fmap.dal.resource.Resource;
63
import org.gvsig.fmap.dal.spi.DataStoreProvider;
64
import org.gvsig.metadata.MetadataLocator;
65
import org.gvsig.metadata.MetadataManager;
66
import org.gvsig.metadata.exceptions.MetadataException;
67
import org.gvsig.raster.impl.DefaultRasterManager;
68
import org.gvsig.raster.impl.buffer.DefaultRasterQuery;
69
import org.gvsig.raster.impl.datastruct.ExtentImpl;
70
import org.gvsig.raster.impl.grid.render.DefaultRender;
71
import org.gvsig.raster.impl.process.vector.PotraceVectorization;
72
import org.gvsig.raster.impl.store.properties.DataStoreSerialInfo;
73
import org.gvsig.tools.ToolsLocator;
74
import org.gvsig.tools.dispose.impl.AbstractDisposable;
75
import org.gvsig.tools.dynobject.DelegatedDynObject;
76
import org.gvsig.tools.dynobject.DynClass;
77
import org.gvsig.tools.dynobject.DynObject;
78
import org.gvsig.tools.dynobject.DynObjectManager;
79
import org.gvsig.tools.dynobject.DynStruct;
80
import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException;
81
import org.gvsig.tools.dynobject.exception.DynMethodException;
82
import org.gvsig.tools.exception.BaseException;
83
import org.gvsig.tools.extensionpoint.ExtensionPoint;
84
import org.gvsig.tools.extensionpoint.ExtensionPointManager;
85
import org.gvsig.tools.observer.impl.DelegateWeakReferencingObservable;
86
import org.gvsig.tools.persistence.PersistentState;
87
import org.gvsig.tools.persistence.exception.PersistenceException;
88
import org.gvsig.tools.undo.command.Command;
89
import org.gvsig.tools.visitor.Visitor;
90

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

    
238
                Extent adjustedDataExtent = util.calculateAdjustedView(dataExtent, getAffineTransform(), new Dimension((int)getWidth(), (int)getHeight()));
239
                
240
                return getWindowRaster(adjustedDataExtent.getMin().getX(), adjustedDataExtent.getMax().getY(), adjustedDataExtent.width(), adjustedDataExtent.height(), currentQuery.isAdjustToExtent());
241
        }
242
        
243
        /**
244
         * Asigna el ?rea de interes en coordenadas del mundo real. Si las coordenadas exceden del tama?o de la imagen
245
         * estas coordenadas son ajustadas el extent.
246
         * @param x Coordenada X, esquina superior izquierda
247
         * @param y Coordenada Y, esquina superior izquierda
248
         * @param w Ancho del ?rea
249
         * @param h Alto del ?rea
250
         * @param bufWidth Ancho del buffer
251
         * @param bufHeight Alto del buffer
252
         * @return En caso de que el buffer sea mayor que el tama?o seleccionado de raster se produce supersampleo. La funci?n devuelve
253
         * un array de dos elementos que representan el desplazamiento en pixels de X e Y de la esquina superior izquierda.
254
         * @throws ArrayIndexOutOfBoundsException
255
         * @throws InvalidSetViewException 
256
         */
257
        private Buffer setAreaOfInterest(double ulx, double uly, double lrx, double lry, int bufWidth, int bufHeight)
258
                throws RasterDriverException, InvalidSetViewException, ProcessInterruptedException {
259
                step = null;
260
                dataExtent = new ExtentImpl(ulx, uly, lrx, lry);
261
                Extent adjustedDataExtent = util.calculateAdjustedView(dataExtent, getAffineTransform(), new Dimension((int)getWidth(), (int)getHeight()));
262

    
263
                //Caso 3D: La petici?n no se ajusta al ?rea y se rellena el exterior con NoData
264
                if(!currentQuery.isAdjustToExtent() && !util.isInside(dataExtent, getExtent())) { 
265
                        return requestFillingWithNoData(dataExtent, adjustedDataExtent, bufWidth, bufHeight);
266
                }
267
                
268
                //Esta secci?n es para que no supersamplee el driver y pueda hacerse en el cliente
269
                if(!currentQuery.isSupersamplingLoadingBuffer()) {
270
                        //nWidth = ((adjustedDataExtent.width() * mDataset.getDataset(0).getWidth()) / mDataset.getExtentForRequest().width());
271
                        //nHeight = ((adjustedDataExtent.height() * mDataset.getDataset(0).getHeight()) / mDataset.getExtentForRequest().height());
272
                        Point2D p1 = worldToRaster(new Point2D.Double(adjustedDataExtent.getULX(), adjustedDataExtent.getULY()));
273
                        Point2D p2 = worldToRaster(new Point2D.Double(adjustedDataExtent.getLRX(), adjustedDataExtent.getLRY()));
274
                        nWidth = Math.abs(p1.getX() - p2.getX());
275
                        nHeight = Math.abs(p1.getY() - p2.getY());
276

    
277
                        if(bufWidth > Math.ceil(nWidth) && bufHeight > Math.ceil(nHeight)) {
278
                                step = calcSteps(adjustedDataExtent.getULX(), adjustedDataExtent.getULY(), adjustedDataExtent.getLRX(), adjustedDataExtent.getLRY(), nWidth, nHeight, bufWidth, bufHeight);
279
                                return  getWindowRaster(adjustedDataExtent.getULX(), adjustedDataExtent.getULY(), adjustedDataExtent.getLRX(), adjustedDataExtent.getLRY());
280
                        }
281
                }
282
                return getWindowRaster(adjustedDataExtent.getULX(), adjustedDataExtent.getULY(), adjustedDataExtent.getLRX(), adjustedDataExtent.getLRY(), bufWidth, bufHeight, true /*Siempre ajustado*/);
283
        }
284
        
285
        /**
286
         * Asigna el ?rea de interes en coordenadas pixel. Si las coordenadas exceden del tama?o de la imagen
287
         * lanza una excepci?n.
288
         * @param x Coordenada X, esquina superior izquierda
289
         * @param y Coordenada Y, esquina superior izquierda
290
         * @param w Ancho del ?rea
291
         * @param h Alto del ?rea
292
         * @throws InvalidSetViewException  
293
         * @throws ArrayIndexOutOfBoundsException
294
         */
295
        private Buffer setAreaOfInterest(int x, int y, int w, int h) 
296
                throws InvalidSetViewException, ProcessInterruptedException, RasterDriverException {
297
                step = null;
298
                if(x > getSourceWidth() || y > getSourceHeight())
299
                        throw new InvalidSetViewException("Par?metros incorrectos en setAreaOfInterest");
300
                x = (x < 0) ? 0 : x;
301
                y = (y < 0) ? 0 : y;
302
                w = (w > getSourceWidth()) ? getSourceWidth() : w;
303
                h = (h > getSourceHeight()) ? getSourceHeight() : h;
304

    
305
                dataExtent = new ExtentImpl(rasterToWorld(new Point2D.Double(x, y)),
306
                                rasterToWorld(new Point2D.Double(x + w, y + h)));
307
                return getWindowRaster(x, y, w, h);
308
        }
309
        
310
        /**
311
         * Asigna el ?rea de interes en coordenadas pixel. Esta operaci?n cargar? un RasterBuffer con los datos solicitados por
312
         * lo que, si al acabar hacemos getRasterBuf obtendremos la matriz de datos. Si las coordenadas exceden del tama?o
313
         * de la imagen lanza una excepci?n.
314
         * @param x Coordenada X, esquina superior izquierda
315
         * @param y Coordenada Y, esquina superior izquierda
316
         * @param w Ancho del ?rea
317
         * @param h Alto del ?rea
318
         * @param bufWidth Ancho del buffer
319
         * @param bufHeight Alto del buffer
320
         * @throws ArrayIndexOutOfBoundsException
321
         */
322
        private Buffer setAreaOfInterest(int x, int y, int w, int h, int bufWidth, int bufHeight)
323
                throws InvalidSetViewException, ProcessInterruptedException, RasterDriverException {
324
                step = null;
325
                if(x > getSourceWidth() || y > getSourceHeight())
326
                        throw new InvalidSetViewException("Par?metros incorrectos en setAreaOfInterest");
327
                
328
                x = (x < 0) ? 0 : x;
329
                y = (y < 0) ? 0 : y;
330
                w = (w > getSourceWidth()) ? getSourceWidth() : w;
331
                h = (h > getSourceHeight()) ? getSourceHeight() : h;
332

    
333
                dataExtent = new ExtentImpl(rasterToWorld(new Point2D.Double(x, y)),
334
                                                                rasterToWorld(new Point2D.Double(x + w, y + h)));
335

    
336
                return getWindowRaster(x, y, w, h, bufWidth, bufHeight);
337
        }
338

    
339
        
340
        /**
341
         * Para este GeoRasterFile asigna que bandas se pintaran
342
         * sobre el RasterBuf cuando se haga un update. Cada posici?n del vector es una banda
343
         * del rasterBuf y el contenido de esa posici?n es la banda de la imagen que se dibujar?
344
         * sobre ese RasterBuf.
345
         * @param drawableBands        Array con las bandas a dibujar.
346
         * @return array con tantos elementos como bandas a dibujar. El valor contenido es el fichero del
347
         * dataset multifichero al que corresponde la banda.
348
         */
349
        private void setQueryDrawableBands(int[] drawableBands) {
350
                clearDrawableBands();
351
                setDrawableBands(drawableBands);
352

    
353
                int[] files = new int[drawableBands.length];
354
                palette = new ColorTable[drawableBands.length];
355

    
356
                for(int i = 0; i< drawableBands.length; i++) {
357
                        if(drawableBands[i] < 0 || drawableBands[i] >= getBandCount())
358
                                continue;
359
                        addDrawableBand(i, drawableBands[i]);
360
                        String fileName = getBands().getBand(drawableBands[i]).getFileName();
361
                        files[i] = getBands().getFileNumber(fileName);
362
                        palette[i] = getColorTable(fileName);
363
                }
364
        }
365
        
366
        /**
367
         * Para este GeoRasterFile asigna que bandas se pintaran
368
         * sobre el RasterBuf cuando se haga un update. Cada posici?n del vector es una banda
369
         * del rasterBuf y el contenido de esa posici?n es la banda de la imagen que se dibujar?
370
         * sobre ese RasterBuf. Esta llamada asigna todas las bandas dibujables en su orden natural.
371
         * @return array con tantos elementos como bandas a dibujar. El valor contenido es el fichero del
372
         * dataset multifichero al que corresponde la banda.
373
         */
374
        public int[] setQueryAllDrawableBands() {
375
                clearDrawableBands();
376
                
377
                int[] list = new int[getBandCount()];
378
                for(int i = 0; i< getBandCount(); i++)
379
                        list[i] = i;
380
                setDrawableBands(list);
381
                
382
                int[] files = new int[getBandCount()];
383
                palette = new ColorTable[getBandCount()];
384

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

    
415
                //Lower Right
416
                distWcX = Math.abs(fitExtent.getMax().getX() - dataExtent.getMin().getX());
417
                distWcX = (distWcX > error) ? distWcX : 0;
418
                distWcY = Math.abs(fitExtent.getMin().getY() - dataExtent.getMax().getY());
419
                distWcY = (distWcY > error) ? distWcY : 0;
420
                //Pixel final del buffer donde se dibuja. Redondeamos por abajo pq lo que sobra se pone NoData
421
                double endPxX = Math.floor((distWcX * bufWidth) / requestExtent.width()); 
422
                double endPxY = Math.floor((distWcY * bufHeight) / requestExtent.height());
423

    
424
                int copyX = (int)Math.abs(endPxX - initPxX);
425
                int copyY = (int)Math.abs(endPxY - initPxY);
426

    
427
                Buffer rasterBuf = getWindowRaster(fitExtent.getULX(), fitExtent.getULY(), fitExtent.getLRX(), fitExtent.getLRY(), copyX, copyY, true);
428
                Buffer buf = DefaultRasterManager.getInstance().createBuffer(getDataType()[0], bufWidth, bufHeight, rasterBuf.getBandCount(), true);
429
                buf.setNoDataValue(currentQuery.getNoDataValueToFill());
430
                for(int i = 0; i < buf.getBandCount(); i++) {
431
                        switch(buf.getDataType()) {
432
                        case Buffer.TYPE_BYTE:buf.assign(i, buf.getByteNoDataValue());break;
433
                        case Buffer.TYPE_SHORT:buf.assign(i, buf.getShortNoDataValue());break;
434
                        case Buffer.TYPE_INT:buf.assign(i, buf.getIntNoDataValue());break;
435
                        case Buffer.TYPE_FLOAT:buf.assign(i, buf.getFloatNoDataValue());break;
436
                        case Buffer.TYPE_DOUBLE:buf.assign(i, buf.getNoDataValue());break;
437
                        }
438
                }        
439

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

    
564
        /**
565
         * Obtiene la anchura de la fuente de datos.
566
         * @return anchura en celdas del grid.
567
         */
568
        private int getSourceWidth() {
569
                return (int)getWidth();
570
        }
571
        
572
        /*
573
         * (non-Javadoc)
574
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#isFileSupported(java.lang.String)
575
         */
576
        public boolean isFileSupported(String fName) {
577
                ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager();
578
                ExtensionPoint point = extensionPoints.get("RasterReader");
579
                return point.has(RasterLocator.getManager().getFileUtils().getExtensionFromFileName(fName));
580
        }
581
        
582
        /**
583
         * Registers metadata definition
584
         * @throws MetadataException
585
         */
586
        public static void registerMetadataDefinition() throws MetadataException {
587
                MetadataManager manager = MetadataLocator.getMetadataManager();
588
                
589
                if(manager == null)
590
                        return;
591
                
592
                if( manager.getDefinition(METADATA_DEFINITION_NAME) == null  ) {
593
                        DynStruct defnition = manager.addDefinition(
594
                                        METADATA_DEFINITION_NAME,
595
                                        METADATA_DEFINITION_NAME 
596
                        );
597
                        defnition.extend(
598
                                        MetadataManager.METADATA_NAMESPACE, 
599
                                        DataStore.METADATA_DEFINITION_NAME
600
                        );
601
                }
602
        }
603
        
604
    //****************************************************
605
        //****Implementing DataStoreImplementation methods****
606
        //****************************************************
607

    
608
        /*
609
         * (non-Javadoc)
610
         * @see org.gvsig.fmap.dal.impl.DataStoreImplementation#intializePhase1(org.gvsig.fmap.dal.impl.DefaultDataManager, org.gvsig.fmap.dal.DataStoreParameters)
611
         */
612
        public void intializePhase1(DefaultDataManager dataManager,
613
                        DataStoreParameters parameters) throws InitializeException {
614
                DynObjectManager dynManager = ToolsLocator.getDynObjectManager();
615

    
616
                this.metadata = (DelegatedDynObject) dynManager
617
                                .createDynObject( 
618
                                                MetadataLocator.getMetadataManager().getDefinition(DataStore.SPATIAL_METADATA_DEFINITION_NAME)
619
                                );
620
                this.dataManager = dataManager;
621
                this.parameters = parameters;
622

    
623
        }
624

    
625
        /*
626
         * (non-Javadoc)
627
         * @see org.gvsig.fmap.dal.impl.DataStoreImplementation#intializePhase2(org.gvsig.fmap.dal.spi.DataStoreProvider)
628
         */
629
        public void intializePhase2(DataStoreProvider provider)
630
                        throws InitializeException {
631
        }
632
        
633
        /* (non-Javadoc)
634
         * @see org.gvsig.fmap.dal.raster.spi.CoverageStoreProviderServices#getManager()
635
         */
636
        public DefaultDataManager getManager() {
637
                return this.dataManager;
638
        }        
639

    
640
        //****************************************************
641
        //*********Implementing DataStore methods*************
642
        //****************************************************
643

    
644
        /*
645
         * (non-Javadoc)
646
         * @see org.gvsig.fmap.dal.DataStore#getProviderName()
647
         */
648
        public String getProviderName() {
649
                return null;
650
        }
651
        
652
        /*
653
         * (non-Javadoc)
654
         * @see org.gvsig.fmap.dal.DataStore#refresh()
655
         */
656
        public void refresh() throws DataException {
657
                return;
658
        }
659

    
660
        /*
661
         * (non-Javadoc)
662
         * @see org.gvsig.fmap.dal.DataStore#getDataSet()
663
         */
664
        public DataSet getDataSet() throws DataException {
665
                return null;
666
        }
667

    
668
        /*
669
         * (non-Javadoc)
670
         * @see org.gvsig.fmap.dal.DataStore#getDataSet(org.gvsig.fmap.dal.DataQuery)
671
         */
672
        public DataSet getDataSet(DataQuery dataQuery) throws DataException {
673
                return null;
674
        }
675

    
676
        /*
677
         * (non-Javadoc)
678
         * @see org.gvsig.fmap.dal.DataStore#accept(org.gvsig.tools.visitor.Visitor, org.gvsig.fmap.dal.DataQuery)
679
         */
680
        public void accept(Visitor visitor, DataQuery dataQuery)
681
                        throws BaseException {
682
        }
683

    
684
        /*
685
         * (non-Javadoc)
686
         * @see org.gvsig.fmap.dal.DataStore#getDataSet(org.gvsig.tools.observer.Observer)
687
         */
688
        public void getDataSet(org.gvsig.tools.observer.Observer observer) throws DataException {
689
        }
690

    
691
        /*
692
         * (non-Javadoc)
693
         * @see org.gvsig.fmap.dal.DataStore#getDataSet(org.gvsig.fmap.dal.DataQuery, org.gvsig.tools.observer.Observer)
694
         */
695
        public void getDataSet(DataQuery dataQuery, org.gvsig.tools.observer.Observer observer) throws DataException {
696
        }
697

    
698
        /*
699
         * (non-Javadoc)
700
         * @see org.gvsig.fmap.dal.DataStore#getSelection()
701
         */
702
        public DataSet getSelection() throws DataException {
703
                return null;
704
        }
705

    
706
        /*
707
         * (non-Javadoc)
708
         * @see org.gvsig.fmap.dal.DataStore#setSelection(org.gvsig.fmap.dal.DataSet)
709
         */
710
        public void setSelection(DataSet selection) throws DataException {
711
        }
712

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

    
721
        /*
722
         * (non-Javadoc)
723
         * @see org.gvsig.fmap.dal.DataStore#getChildren()
724
         */
725
        @SuppressWarnings("unchecked")
726
        public Iterator getChildren() {
727
                return null;
728
        }
729

    
730
        /*
731
         * (non-Javadoc)
732
         * @see org.gvsig.fmap.dal.DataStore#getExplorer()
733
         */
734
        public DataServerExplorer getExplorer() throws DataException, ValidateDataParametersException {
735
                return null;
736
        }
737
        
738
        /*
739
         * (non-Javadoc)
740
         * @see org.gvsig.fmap.dal.DataStore#createQuery()
741
         */
742
        public DataQuery createQuery() {
743
                return new DefaultRasterQuery();
744
        }
745
        
746
        //****************************************************
747
        //*********Implementing Persistent methods*************
748
        //****************************************************
749
        
750
        /*
751
         * (non-Javadoc)
752
         * @see org.gvsig.tools.persistence.Persistent#saveToState(org.gvsig.tools.persistence.PersistentState)
753
         */
754
        public void saveToState(PersistentState state) throws PersistenceException {
755
                
756
        }
757

    
758
        /*
759
         * (non-Javadoc)
760
         * @see org.gvsig.tools.persistence.Persistent#loadFromState(org.gvsig.tools.persistence.PersistentState)
761
         */
762
        public void loadFromState(PersistentState state) throws PersistenceException {
763
                
764
        }
765
        
766
        //****************************************************
767
        //*********Implementing Metadata methods*************
768
        //****************************************************
769
        
770
    /*
771
     * (non-Javadoc)
772
     * @see org.gvsig.metadata.Metadata#getMetadataID()
773
     */
774
    public Object getMetadataID() {
775
            if(parameters != null && parameters instanceof RasterFileStoreParameters)
776
                    return ((RasterFileStoreParameters)parameters).getFile();
777
            if(parameters != null && parameters instanceof RasterStoreParameters)
778
                    return ((RasterStoreParameters)parameters).getURI();
779
            return null;
780
    }
781

    
782
   /*
783
    * (non-Javadoc)
784
    * @see org.gvsig.metadata.Metadata#getMetadataChildren()
785
    */
786
    @SuppressWarnings("unchecked")
787
        public Set getMetadataChildren() {
788
            return null;
789
    }
790
    
791
    //****************************************************
792
        //*********Implementing Disposable methods************
793
        //****************************************************
794
    
795
    /*
796
     * (non-Javadoc)
797
     * @see org.gvsig.tools.dispose.impl.AbstractDisposable#doDispose()
798
     */
799
    public void doDispose() {
800
            
801
    }
802
    
803
    //****************************************************
804
        //*********Implementing Visitable methods*************
805
        //****************************************************
806
    
807
    /*
808
     * (non-Javadoc)
809
     * @see org.gvsig.tools.visitor.Visitable#accept(org.gvsig.tools.visitor.Visitor)
810
     */
811
    public void accept(Visitor visitor) throws BaseException {
812
            
813
    }
814
    
815
    //****************************************************
816
        //****Implementing ComplexObservable methods**********
817
        //****************************************************
818
    
819
   /*
820
    * (non-Javadoc)
821
    * @see org.gvsig.tools.observer.ComplexObservable#disableNotifications()
822
    */
823
    public void disableNotifications() {
824
                
825
        }
826

    
827
        /*
828
         * (non-Javadoc)
829
         * @see org.gvsig.tools.observer.ComplexObservable#enableNotifications()
830
         */
831
    public void enableNotifications() {
832
                
833
        }
834

    
835
        /*
836
         * (non-Javadoc)
837
         * @see org.gvsig.tools.observer.ComplexObservable#beginComplexNotification()
838
         */
839
    public void beginComplexNotification() {
840
                
841
        }
842

    
843
        /*
844
         * (non-Javadoc)
845
         * @see org.gvsig.tools.observer.ComplexObservable#endComplexNotification()
846
         */
847
    public void endComplexNotification() {
848
                
849
        }
850
    
851
    //****************************************************
852
        //********Implementing Observable methods*************
853
        //****************************************************
854
    
855
    /*
856
     * (non-Javadoc)
857
     * @see org.gvsig.tools.observer.Observable#addObserver(org.gvsig.tools.observer.Observer)
858
     */
859
        public void addObserver(org.gvsig.tools.observer.Observer o) {
860
                
861
        }
862

    
863
        /*
864
         * (non-Javadoc)
865
         * @see org.gvsig.tools.observer.Observable#deleteObserver(org.gvsig.tools.observer.Observer)
866
         */
867
        public void deleteObserver(org.gvsig.tools.observer.Observer o) {
868
                
869
        }
870

    
871
        /*
872
         * (non-Javadoc)
873
         * @see org.gvsig.tools.observer.Observable#deleteObservers()
874
         */
875
        public void deleteObservers() {
876
                
877
        }
878
        
879
    //****************************************************
880
        //********Implementing DynObject methods*************
881
        //****************************************************
882
        
883
        /*
884
         * (non-Javadoc)
885
         * @see org.gvsig.tools.dynobject.DynObject#getDynClass()
886
         */
887
        public DynClass getDynClass() {
888
                return this.metadata.getDynClass();
889
        }
890

    
891
        /*
892
         * (non-Javadoc)
893
         * @see org.gvsig.tools.dynobject.DynObject#implement(org.gvsig.tools.dynobject.DynClass)
894
         */
895
        public void implement(DynClass dynClass) {
896
                this.metadata.implement(dynClass);
897
    }
898

    
899
        /*
900
         * (non-Javadoc)
901
         * @see org.gvsig.tools.dynobject.DynObject#delegate(org.gvsig.tools.dynobject.DynObject)
902
         */
903
        public void delegate(DynObject dynObject) {
904
                this.metadata.delegate(dynObject);
905
    }
906

    
907
        /*
908
         * (non-Javadoc)
909
         * @see org.gvsig.tools.dynobject.DynObject#getDynValue(java.lang.String)
910
         */
911
        public Object getDynValue(String name)  throws DynFieldNotFoundException {
912
                return this.metadata.getDynValue(name);
913
        }
914

    
915
        /*
916
         * (non-Javadoc)
917
         * @see org.gvsig.tools.dynobject.DynObject#setDynValue(java.lang.String, java.lang.Object)
918
         */
919
        public void setDynValue(String name, Object value)  throws DynFieldNotFoundException {
920
                this.metadata.setDynValue(name, value);
921
    }
922

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

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

    
939
        /*
940
         * (non-Javadoc)
941
         * @see org.gvsig.tools.dynobject.DynObject#invokeDynMethod(int, org.gvsig.tools.dynobject.DynObject)
942
         */
943
        public Object invokeDynMethod(int code, DynObject context) throws DynMethodException {
944
                return this.metadata.invokeDynMethod(this, code, context);
945
        }
946

    
947
        /*
948
         * (non-Javadoc)
949
         * @see org.gvsig.tools.dynobject.DynObject#clear()
950
         */
951
        public void clear() {
952
                if (metadata != null) {
953
                        metadata.clear();
954
                }
955
    }
956
        
957
        /*
958
         * (non-Javadoc)
959
         * @see org.gvsig.fmap.dal.DataStore#getParameters()
960
         */
961
        public DataStoreParameters getParameters() {
962
                return parameters;
963
        }
964

    
965
        //***********************************************************************
966
        //********Implementing CoverageStoreProviderServices methods*************
967
        //***********************************************************************
968

    
969
        public CoverageSelection createDefaultCoverageSelection()
970
        throws DataException {
971
                return null;
972
        }
973

    
974
        /*
975
         * (non-Javadoc)
976
         * @see org.gvsig.fmap.dal.raster.spi.CoverageStoreProviderServices#getCoverageStore()
977
         */
978
        public CoverageStore getCoverageStore() {
979
                return this;
980
        }
981

    
982
        /*
983
         * (non-Javadoc)
984
         * @see org.gvsig.fmap.dal.raster.spi.CoverageStoreProviderServices#getProvider()
985
         */
986
        public abstract CoverageStoreProvider getProvider();
987

    
988
        /*
989
         * (non-Javadoc)
990
         * @see org.gvsig.fmap.dal.raster.spi.CoverageStoreProviderServices#notifyChange(java.lang.String)
991
         */
992
        public void notifyChange(String notification) {
993
                delegateObservable
994
                .notifyObservers(new DefaultCoverageStoreNotification(
995
                                this, notification));
996
        }
997

    
998
        /*
999
         * (non-Javadoc)
1000
         * @see org.gvsig.fmap.dal.raster.spi.CoverageStoreProviderServices#notifyChange(java.lang.String, org.gvsig.fmap.dal.resource.Resource)
1001
         */
1002
        public void notifyChange(String arg0, Resource arg1) {
1003
                delegateObservable.notifyObservers(new DefaultFeatureStoreNotification(
1004
                                this, FeatureStoreNotification.RESOURCE_CHANGED));
1005
        }
1006

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

    
1017
        /*
1018
         * (non-Javadoc)
1019
         * @see org.gvsig.fmap.dal.spi.DataStoreProviderServices#getStore()
1020
         */
1021
        public DataStore getStore() {
1022
                return this;
1023
        }
1024
}