Statistics
| Revision:

gvsig-raster / org.gvsig.raster / branches / org.gvsig.raster_dataaccess_refactoring / org.gvsig.raster.lib / org.gvsig.raster.lib.impl / src / main / java / org / gvsig / raster / impl / store / AbstractRasterDataStore.java @ 2328

History | View | Annotate | Download (19.7 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.io.File;
25
import java.util.Iterator;
26
import java.util.List;
27
import java.util.Set;
28

    
29
import org.cresques.cts.ICoordTrans;
30
import org.gvsig.fmap.dal.DataManager;
31
import org.gvsig.fmap.dal.DataQuery;
32
import org.gvsig.fmap.dal.DataServerExplorer;
33
import org.gvsig.fmap.dal.DataSet;
34
import org.gvsig.fmap.dal.DataStore;
35
import org.gvsig.fmap.dal.DataStoreParameters;
36
import org.gvsig.fmap.dal.coverage.RasterLocator;
37
import org.gvsig.fmap.dal.coverage.dataset.Buffer;
38
import org.gvsig.fmap.dal.coverage.datastruct.BandList;
39
import org.gvsig.fmap.dal.coverage.datastruct.DatasetBand;
40
import org.gvsig.fmap.dal.coverage.datastruct.Extent;
41
import org.gvsig.fmap.dal.coverage.datastruct.GeoPointList;
42
import org.gvsig.fmap.dal.coverage.exception.BandNotFoundInListException;
43
import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException;
44
import org.gvsig.fmap.dal.coverage.exception.QueryException;
45
import org.gvsig.fmap.dal.coverage.exception.RasterDriverException;
46
import org.gvsig.fmap.dal.coverage.exception.RmfSerializerException;
47
import org.gvsig.fmap.dal.coverage.grid.render.Render;
48
import org.gvsig.fmap.dal.coverage.process.vector.Vectorization;
49
import org.gvsig.fmap.dal.coverage.store.RasterDataStore;
50
import org.gvsig.fmap.dal.coverage.store.RasterQuery;
51
import org.gvsig.fmap.dal.coverage.store.parameter.RasterDataParameters;
52
import org.gvsig.fmap.dal.coverage.store.parameter.RasterFileStoreParameters;
53
import org.gvsig.fmap.dal.coverage.store.props.ColorTable;
54
import org.gvsig.fmap.dal.coverage.store.props.Metadata;
55
import org.gvsig.fmap.dal.coverage.store.props.TimeSeries;
56
import org.gvsig.fmap.dal.exception.DataException;
57
import org.gvsig.fmap.dal.exception.InitializeException;
58
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
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.DataStoreInitializer;
65
import org.gvsig.fmap.dal.spi.DataStoreProvider;
66
import org.gvsig.metadata.MetadataLocator;
67
import org.gvsig.metadata.MetadataManager;
68
import org.gvsig.metadata.exceptions.MetadataException;
69
import org.gvsig.raster.impl.DefaultRasterManager;
70
import org.gvsig.raster.impl.buffer.DefaultRasterQuery;
71
import org.gvsig.raster.impl.datastruct.BandListImpl;
72
import org.gvsig.raster.impl.datastruct.DatasetBandImpl;
73
import org.gvsig.raster.impl.datastruct.ExtentImpl;
74
import org.gvsig.raster.impl.grid.render.DefaultRender;
75
import org.gvsig.raster.impl.process.vector.PotraceVectorization;
76
import org.gvsig.raster.impl.provider.AbstractRasterProvider;
77
import org.gvsig.raster.impl.provider.RasterProvider;
78
import org.gvsig.tools.ToolsLocator;
79
import org.gvsig.tools.dispose.impl.AbstractDisposable;
80
import org.gvsig.tools.dynobject.DelegatedDynObject;
81
import org.gvsig.tools.dynobject.DynClass;
82
import org.gvsig.tools.dynobject.DynObject;
83
import org.gvsig.tools.dynobject.DynObjectManager;
84
import org.gvsig.tools.dynobject.DynStruct;
85
import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException;
86
import org.gvsig.tools.dynobject.exception.DynMethodException;
87
import org.gvsig.tools.exception.BaseException;
88
import org.gvsig.tools.observer.impl.DelegateWeakReferencingObservable;
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, QueryableRaster, DataStoreInitializer {
99
        public static final String         PERSISTENT_NAME        = "AbstractRasterDataStore_Persistent";
100
    public static final String         PERSISTENT_DESCRIPTION = "AbstractRasterDataStore Persistent";
101
   // private Logger                     log                    = LoggerFactory.getLogger(AbstractRasterDataStore.class);
102
        protected DefaultRasterQuery       currentQuery           = null;
103
        protected BandListImpl             storeBandList          = null;
104
        protected RasterProvider           provider               = null;
105
        protected Metadata                 metadata               = null;
106
        
107
        /**
108
         * Lista de paletas asociadas a las bandas cargadas en el DataSource. Estas son calculadas
109
         * en las funciones que asignan las bandas a dibujar (addDrawableBands)
110
         */
111
        private ColorTable[]                           palette                = null;
112

    
113
        /**
114
         * Ancho y alto en pixeles del ?ltimo buffer asignado
115
         */
116
        protected double                                nWidth                 = 0;
117
        protected double                                nHeight                = 0;
118
        private Buffer                     lastBuffer             = null;
119
        
120
        private DataManager                dataManager            = null;
121
        protected DataStoreParameters      parameters             = null;
122
        private DelegatedDynObject         dynobj                 = null;
123
        private DelegateWeakReferencingObservable 
124
                                           delegateObservable     = new DelegateWeakReferencingObservable(this);
125
        private Render                     render                 = null;
126
        protected ICoordTrans              coordTrans             = null;
127
        
128
        public BandList getDefaultBandList() {
129
                if(storeBandList == null) {
130
                        initializeBandList();
131
                }
132
                return storeBandList;
133
        }
134
        
135
        public void initializeBandList() {
136
                if(provider != null) {
137
                        storeBandList = new BandListImpl();
138
                        storeBandList.clear();
139
                        for(int i = 0; i < provider.getBandCount(); i++) {
140
                                try {
141
                                        int dataType = provider.getDataType()[i];
142
                                        DatasetBand band = new DatasetBandImpl(provider.getURIByBand(i), 
143
                                                        provider.getBandPositionByProvider(i), 
144
                                                        dataType, 
145
                                                        provider.getBandCount());
146
                                        storeBandList.addBand(band, i);
147
                                } catch(BandNotFoundInListException ex) {
148
                                        //No a?adimos la banda
149
                                }
150
                        }
151

    
152
                        int[] drawableBands = new int[storeBandList.getBandCount()];
153
                        for (int i = 0; i < storeBandList.getBandCount(); i++) {
154
                                drawableBands[i] = i;
155
                        }
156

    
157
                        storeBandList.setDrawableBands(drawableBands);
158
                }
159
        }
160
        
161
        public Buffer query(RasterQuery query) throws ProcessInterruptedException, QueryException {
162
                Object[] obj = queryArray(query);
163
                if(obj != null && obj.length > 0 && obj[0] instanceof Buffer)
164
                        return ((Buffer)obj[0]);
165
                return null;
166
        }
167
        
168
        /**
169
         * <UL>Tasks to do in the <code>Datastore</code>
170
         * <LI>Check parameters and adjust them to not exceed the window size</LI>
171
         * <LI>Build the band list to draw</LI>
172
         * <LI>Create the buffers (RO or RW) and set them in the <code>RasterQuery</code> object</LI>
173
         * <LI>Calculate steps when supersampling is applied</LI>
174
         * </UL>
175
         */
176
        public Object[] queryArray(RasterQuery query)
177
                        throws ProcessInterruptedException, QueryException {
178
                Object[] result = null;
179
                currentQuery = (DefaultRasterQuery)query;
180
                DefaultRasterQuery q = currentQuery;
181
                
182
                if(isTiled())
183
                        q.dontBuildBuffer();
184
                
185
                Buffer buf = null;
186
                
187
                if(q.requestHasShift()) {
188
                        //TODO: Sin probar
189
                        FramedBufferResampling framedBufferResampling = new FramedBufferResampling(q, this);
190
                        buf = framedBufferResampling.query(provider);                        
191
                } else {
192
                        q.calculateParameters(this);
193
                        try {
194
                                if(q.isReadOnly() && !isTiled())
195
                                        buf = q.getBufferForProviders();
196
                                else
197
                                        buf = ((AbstractRasterProvider)provider).getDataSet(q);
198
                        } catch (RasterDriverException e) {
199
                                throw new QueryException("Error getting data", e);
200
                        }
201
                        
202
                        if(q.isSupersamplingOptionActive() && q.isSupersamplingTheRequest()) {
203
                                buf = buf.getAdjustedWindow(q.getBufWidth(), q.getBufHeight(), Buffer.INTERPOLATION_NearestNeighbour);
204
                        }
205
                }
206
                if(buf != null)
207
                        buf.setDataExtent(q.getAdjustedRequestBoundingBox().toRectangle2D());
208
                result = new Object[]{buf};
209
                
210
                if(q.isStoredLastBuffer())
211
                        lastBuffer = buf;
212
                else
213
                        lastBuffer = null;
214
                q.storeLastBuffer(false);
215
                
216
                if(result != null && result.length > 0 && result[0] instanceof Buffer) {
217
                        ((Buffer)result[0]).setStore(this);
218
                        if(getNoDataValue() != null && getNoDataValue().isDefined())
219
                                ((Buffer)result[0]).setNoDataValue(getNoDataValue());
220
                        return result;
221
                }
222
                return null;
223
        }
224
        
225
        public Extent adjustToExtent(Extent e) {
226
                Extent currentExtent = this.getExtent();
227
        
228
                double minx = e.getMin().getX() < currentExtent.getMin().getX() ? currentExtent.getMin().getX() : e.getMin().getX();
229
                double maxx = e.getMax().getX() > currentExtent.getMax().getX() ? currentExtent.getMax().getX() : e.getMax().getX();
230
                double miny = e.getMin().getY() < currentExtent.getMin().getY() ? currentExtent.getMin().getY() : e.getMin().getY();
231
                double maxy = e.getMax().getY() > currentExtent.getMax().getY() ? currentExtent.getMax().getY() : e.getMax().getY();
232
                
233
                return new ExtentImpl(minx, maxy, maxx, miny);
234
        }
235
        
236
        public String getFullName() {
237
                return getName();
238
        }
239

    
240
        public Buffer getLastBuffer() {
241
                return lastBuffer;
242
        }
243
        
244
        public TimeSeries getTimeSerials() throws RmfSerializerException {
245
                if(getProvider() instanceof RasterProvider)
246
                        return ((RasterProvider)getProvider()).getTimeSerials();
247
                return null;
248
        }
249

    
250
        public void setTimeSerials(TimeSeries serialInfo) throws RmfSerializerException {
251
                if(getProvider() instanceof RasterProvider)
252
                        ((RasterProvider)getProvider()).setTimeSerials(serialInfo);                        
253
        }
254
        
255
        public abstract void saveColorTableToRmf(ColorTable table) throws RmfSerializerException;
256
        
257
        public abstract void saveGeoPointListToRmf(GeoPointList pointList) throws RmfSerializerException;
258
        
259
        public abstract void saveROIFileListToRmf(List<File> fileList) throws RmfSerializerException;
260
        
261
        /**
262
         * Saves information about serials
263
         * @param object to save
264
         * @throws RmfSerializerException
265
         */
266
        public abstract void saveSerialInfoToRmf(TimeSeries serialInfo) throws RmfSerializerException;
267
        
268
        /**
269
         * Loads information about serials
270
         * @param object to load
271
         * @throws RmfSerializerException
272
         */
273
        public abstract boolean loadSerialInfoFromRmf(TimeSeries serialInfo);
274
        
275
        public Vectorization createVectorizeObject() throws QueryException, ProcessInterruptedException {
276
                return new PotraceVectorization((RasterDataStore)this);
277
        }
278
        
279
        public Render getRender() {
280
                if(render == null)
281
                        render = new DefaultRender(this);
282
                return render;
283
        }
284
        
285
        public void setRender(Render render) {
286
                this.render = render;
287
        }
288

    
289
        public boolean isFileSupported(String fName) {
290
                return RasterLocator.getManager().getProviderServices().isExtensionSupported(fName);
291
        }
292
        
293
        /**
294
         * Registers metadata definition
295
         * @throws MetadataException
296
         */
297
        public static void registerMetadataDefinition() throws MetadataException {
298
                MetadataManager manager = MetadataLocator.getMetadataManager();
299
                
300
                if(manager == null)
301
                        return;
302
                
303
                if( manager.getDefinition(METADATA_DEFINITION_NAME) == null  ) {
304
                        DynStruct defnition = manager.addDefinition(
305
                                        METADATA_DEFINITION_NAME,
306
                                        METADATA_DEFINITION_NAME 
307
                        );
308
                        defnition.extend(
309
                                        MetadataManager.METADATA_NAMESPACE, 
310
                                        DataStore.METADATA_DEFINITION_NAME
311
                        );
312
                }
313
        }
314

    
315
        public int getSourceType() {
316
                return ((RasterProvider)getProvider()).getSourceType();
317
        }
318
        
319
    //****************************************************
320
        //****Implementing DataStoreImplementation methods****
321
        //****************************************************
322

    
323
        public void intializePhase1(DataManager dataManager,
324
                        DataStoreParameters parameters) throws InitializeException {
325
                DynObjectManager dynManager = ToolsLocator.getDynObjectManager();
326

    
327
                this.dynobj = (DelegatedDynObject) dynManager
328
                                .createDynObject( 
329
                                                MetadataLocator.getMetadataManager().getDefinition(DataStore.SPATIAL_METADATA_DEFINITION_NAME)
330
                                );
331
                this.dataManager = dataManager;
332
                this.parameters = parameters;
333

    
334
        }
335

    
336
        public void intializePhase2(DataStoreProvider provider)
337
                        throws InitializeException {
338
                setProvider((RasterProvider)provider);
339
        }
340
        
341
        public DataManager getManager() {
342
                return this.dataManager;
343
        }        
344

    
345
        //****************************************************
346
        //*********Implementing DataStore methods*************
347
        //****************************************************
348

    
349
        public String getProviderName() {
350
                return getProvider().getFullName();
351
        }
352
        
353
        public void refresh() throws DataException {
354
                return;
355
        }
356

    
357
        public DataSet getDataSet() throws DataException {
358
                RasterQuery query = DefaultRasterManager.getInstance().createQuery();
359
                query.setSupersamplingOption(false);
360
                query.setAreaOfInterest();
361
                int[] bands = new int[getBandCount()];
362
                for (int i = 0; i < bands.length; i++) {
363
                        bands[i] = i;
364
                }
365
                query.setDrawableBands(bands);
366
                return getDataSet(query);
367
        }
368

    
369
        public DataSet getDataSet(DataQuery dataQuery) throws DataException {
370
                if(dataQuery instanceof RasterQuery) {
371
                        Object[] obj = null;
372
                        try {
373
                                obj = queryArray((RasterQuery)dataQuery);
374
                        } catch (ProcessInterruptedException e) {
375
                                throw new RasterDriverException("", e);
376
                        } catch (QueryException e) {
377
                                throw new RasterDriverException(e.getMessage(), e);
378
                        }
379
                        if(obj != null && obj.length > 0 && obj[0] instanceof Buffer)
380
                                return (Buffer)obj[0];
381
                }
382
                return null;
383
        }
384

    
385
        public void accept(Visitor visitor, DataQuery dataQuery)
386
                        throws BaseException {
387
        }
388

    
389
        public void getDataSet(org.gvsig.tools.observer.Observer observer) throws DataException {
390
                RasterQuery query = DefaultRasterManager.getInstance().createQuery();
391
                query.setSupersamplingOption(false);
392
                query.setAreaOfInterest();
393
                int[] bands = new int[getBandCount()];
394
                for (int i = 0; i < bands.length; i++) {
395
                        bands[i] = i;
396
                }
397
                query.setDrawableBands(bands);
398
                getDataSet(query, observer);
399
        }
400

    
401
        public void getDataSet(DataQuery dataQuery, org.gvsig.tools.observer.Observer observer) throws DataException {
402
                if(dataQuery instanceof RasterQuery) {
403
                        Object[] obj = null;
404
                        try {
405
                                obj = queryArray((RasterQuery)dataQuery);
406
                        } catch (ProcessInterruptedException e) {
407
                        } catch (QueryException e) {
408
                                throw new RasterDriverException(e.getMessage(), e);
409
                        }
410
                        if(obj != null && obj.length > 0 && obj[0] instanceof Buffer)
411
                                observer.update(this, obj[0]);
412
                }
413
        }
414

    
415
        public DataSet getSelection() throws DataException {
416
                return null;
417
        }
418

    
419
        public void setSelection(DataSet selection) throws DataException {
420
        }
421

    
422
        public DataSet createSelection() throws DataException {
423
                return null;
424
        }
425

    
426
        public Iterator<?> getChildren() {
427
                return null;
428
        }
429

    
430
        public DataServerExplorer getExplorer() throws DataException, ValidateDataParametersException {
431
                return null;
432
        }
433
        
434
        public DataQuery createQuery() {
435
                return new DefaultRasterQuery();
436
        }
437
        
438
        //****************************************************
439
        //*********Implementing Metadata methods*************
440
        //****************************************************
441
        
442
    public Object getMetadataID() {
443
            if(parameters != null && parameters instanceof RasterFileStoreParameters)
444
                    return ((RasterFileStoreParameters)parameters).getFile();
445
            if(parameters != null && parameters instanceof RasterDataParameters)
446
                    return ((RasterDataParameters)parameters).getURI();
447
            return null;
448
    }
449

    
450
        public Set<?> getMetadataChildren() {
451
            return null;
452
    }
453
    
454
    //****************************************************
455
        //*********Implementing Disposable methods************
456
        //****************************************************
457
    
458
    public void doDispose() {
459
            
460
    }
461
    
462
    //****************************************************
463
        //*********Implementing Visitable methods*************
464
        //****************************************************
465
    
466
    public void accept(Visitor visitor) throws BaseException {
467
            
468
    }
469
    
470
    //****************************************************
471
        //****Implementing ComplexObservable methods**********
472
        //****************************************************
473
    
474
    public void disableNotifications() {
475
                
476
        }
477

    
478
    public void enableNotifications() {
479
                
480
        }
481

    
482
    public void beginComplexNotification() {
483
                
484
        }
485

    
486
    public void endComplexNotification() {
487
                
488
        }
489
    
490
    //****************************************************
491
        //********Implementing Observable methods*************
492
        //****************************************************
493
    
494
        public void addObserver(org.gvsig.tools.observer.Observer o) {
495
                
496
        }
497

    
498
        public void deleteObserver(org.gvsig.tools.observer.Observer o) {
499
                
500
        }
501

    
502
        public void deleteObservers() {
503
                
504
        }
505
        
506
    //****************************************************
507
        //********Implementing DynObject methods*************
508
        //****************************************************
509
        
510
        public DynClass getDynClass() {
511
                return this.dynobj.getDynClass();
512
        }
513

    
514
        public void implement(DynClass dynClass) {
515
                this.dynobj.implement(dynClass);
516
    }
517

    
518
        public void delegate(DynObject dynObject) {
519
                this.dynobj.delegate(dynObject);
520
    }
521

    
522
        public Object getDynValue(String name)  throws DynFieldNotFoundException {
523
                return this.dynobj.getDynValue(name);
524
        }
525

    
526
        public void setDynValue(String name, Object value)  throws DynFieldNotFoundException {
527
                this.dynobj.setDynValue(name, value);
528
    }
529

    
530
        public boolean hasDynValue(String name) {
531
                return this.dynobj.hasDynValue(name);
532
        }
533

    
534
        public Object invokeDynMethod(String name, DynObject context) throws DynMethodException {
535
                return this.dynobj.invokeDynMethod(this, name, context);
536
        }
537

    
538
        public Object invokeDynMethod(int code, DynObject context) throws DynMethodException {
539
                return this.dynobj.invokeDynMethod(this, code, context);
540
        }
541

    
542
        public void clear() {
543
                if (dynobj != null) {
544
                        dynobj.clear();
545
                }
546
    }
547
        
548
        public DataStoreParameters getParameters() {
549
                return parameters;
550
        }
551
        
552
        //***********************************************************************
553
        //********Implementing CoverageStoreProviderServices methods*************
554
        //***********************************************************************
555

    
556
        public CoverageSelection createDefaultCoverageSelection()
557
        throws DataException {
558
                return null;
559
        }
560

    
561
        public CoverageStore getCoverageStore() {
562
                return this;
563
        }
564

    
565
        public abstract CoverageStoreProvider getProvider();
566

    
567
        public void notifyChange(String notification) {
568
                delegateObservable
569
                .notifyObservers(new DefaultCoverageStoreNotification(
570
                                this, notification));
571
        }
572

    
573
        public void notifyChange(String arg0, Resource arg1) {
574
                /*delegateObservable.notifyObservers(new DefaultFeatureStoreNotification(
575
                                this, FeatureStoreNotification.RESOURCE_CHANGED));*/
576
        }
577

    
578
        public void notifyChange(String notification, Command command) {
579
                delegateObservable
580
                .notifyObservers(new DefaultCoverageStoreNotification(
581
                                this, notification, command));
582
        }
583

    
584
        public DataStore getStore() {
585
                return this;
586
        }
587
        
588
        public void setCoordTrans(ICoordTrans t) {
589
                this.coordTrans = t;
590
        }
591
        
592
        public void close() {
593
                if(lastBuffer != null) {
594
                        lastBuffer.dispose();
595
                }
596
        }
597
        
598
        protected void finalize() throws Throwable {
599
                currentQuery           = null;
600
                if(palette != null) {
601
                        for (int i = 0; i < palette.length; i++) {
602
                                palette[i] = null;
603
                        }
604
                        palette = null;
605
                }
606
                lastBuffer             = null;
607
                dataManager            = null;
608
                parameters             = null;
609
                metadata               = null;
610
                delegateObservable     = null;
611
                render                 = null;
612
                super.finalize();
613
        }
614
}