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

History | View | Annotate | Download (19.5 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
                Buffer buf = null;
183
                
184
                if(q.isSupersamplingOptionActive() && q.isSupersamplingTheRequest()) {
185
                        //TODO: Sin probar
186
                        Supersampling supersampling = new Supersampling(q, this);
187
                        buf = supersampling.query(provider);
188
                } else if(q.requestHasShift()) {
189
                        //TODO: Sin probar
190
                        FramedBufferResampling framedBufferResampling = new FramedBufferResampling(q, this);
191
                        buf = framedBufferResampling.query(provider);                        
192
                } else {
193
                        q.calculateParameters(this);
194
                        try {
195
                                if(q.isReadOnly())
196
                                        buf = q.getBufferForProviders();
197
                                else
198
                                        buf = ((AbstractRasterProvider)provider).getDataSet(q);
199
                        } catch (RasterDriverException e) {
200
                                throw new QueryException("Error getting data", e);
201
                        }
202
                }
203
                
204
                result = new Object[]{buf};
205
                
206
                if(q.isStoredLastBuffer())
207
                        lastBuffer = buf;
208
                else
209
                        lastBuffer = null;
210
                q.storeLastBuffer(false);
211
                
212
                if(result != null && result.length > 0 && result[0] instanceof Buffer) {
213
                        ((Buffer)result[0]).setStore(this);
214
                        if(getNoDataValue() != null && getNoDataValue().isDefined())
215
                                ((Buffer)result[0]).setNoDataValue(getNoDataValue());
216
                        return result;
217
                }
218
                return null;
219
        }
220
        
221
        public Extent adjustToExtent(Extent e) {
222
                Extent currentExtent = this.getExtent();
223
        
224
                double minx = e.getMin().getX() < currentExtent.getMin().getX() ? currentExtent.getMin().getX() : e.getMin().getX();
225
                double maxx = e.getMax().getX() > currentExtent.getMax().getX() ? currentExtent.getMax().getX() : e.getMax().getX();
226
                double miny = e.getMin().getY() < currentExtent.getMin().getY() ? currentExtent.getMin().getY() : e.getMin().getY();
227
                double maxy = e.getMax().getY() > currentExtent.getMax().getY() ? currentExtent.getMax().getY() : e.getMax().getY();
228
                
229
                return new ExtentImpl(minx, maxy, maxx, miny);
230
        }
231
        
232
        public String getFullName() {
233
                return getName();
234
        }
235

    
236
        public Buffer getLastBuffer() {
237
                return lastBuffer;
238
        }
239
        
240
        public TimeSeries getTimeSerials() throws RmfSerializerException {
241
                if(getProvider() instanceof RasterProvider)
242
                        return ((RasterProvider)getProvider()).getTimeSerials();
243
                return null;
244
        }
245

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

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

    
311
        public int getSourceType() {
312
                return ((RasterProvider)getProvider()).getSourceType();
313
        }
314
        
315
    //****************************************************
316
        //****Implementing DataStoreImplementation methods****
317
        //****************************************************
318

    
319
        public void intializePhase1(DataManager dataManager,
320
                        DataStoreParameters parameters) throws InitializeException {
321
                DynObjectManager dynManager = ToolsLocator.getDynObjectManager();
322

    
323
                this.dynobj = (DelegatedDynObject) dynManager
324
                                .createDynObject( 
325
                                                MetadataLocator.getMetadataManager().getDefinition(DataStore.SPATIAL_METADATA_DEFINITION_NAME)
326
                                );
327
                this.dataManager = dataManager;
328
                this.parameters = parameters;
329

    
330
        }
331

    
332
        public void intializePhase2(DataStoreProvider provider)
333
                        throws InitializeException {
334
                setProvider((RasterProvider)provider);
335
        }
336
        
337
        public DataManager getManager() {
338
                return this.dataManager;
339
        }        
340

    
341
        //****************************************************
342
        //*********Implementing DataStore methods*************
343
        //****************************************************
344

    
345
        public String getProviderName() {
346
                return getProvider().getFullName();
347
        }
348
        
349
        public void refresh() throws DataException {
350
                return;
351
        }
352

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

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

    
381
        public void accept(Visitor visitor, DataQuery dataQuery)
382
                        throws BaseException {
383
        }
384

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

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

    
411
        public DataSet getSelection() throws DataException {
412
                return null;
413
        }
414

    
415
        public void setSelection(DataSet selection) throws DataException {
416
        }
417

    
418
        public DataSet createSelection() throws DataException {
419
                return null;
420
        }
421

    
422
        public Iterator<?> getChildren() {
423
                return null;
424
        }
425

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

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

    
474
    public void enableNotifications() {
475
                
476
        }
477

    
478
    public void beginComplexNotification() {
479
                
480
        }
481

    
482
    public void endComplexNotification() {
483
                
484
        }
485
    
486
    //****************************************************
487
        //********Implementing Observable methods*************
488
        //****************************************************
489
    
490
        public void addObserver(org.gvsig.tools.observer.Observer o) {
491
                
492
        }
493

    
494
        public void deleteObserver(org.gvsig.tools.observer.Observer o) {
495
                
496
        }
497

    
498
        public void deleteObservers() {
499
                
500
        }
501
        
502
    //****************************************************
503
        //********Implementing DynObject methods*************
504
        //****************************************************
505
        
506
        public DynClass getDynClass() {
507
                return this.dynobj.getDynClass();
508
        }
509

    
510
        public void implement(DynClass dynClass) {
511
                this.dynobj.implement(dynClass);
512
    }
513

    
514
        public void delegate(DynObject dynObject) {
515
                this.dynobj.delegate(dynObject);
516
    }
517

    
518
        public Object getDynValue(String name)  throws DynFieldNotFoundException {
519
                return this.dynobj.getDynValue(name);
520
        }
521

    
522
        public void setDynValue(String name, Object value)  throws DynFieldNotFoundException {
523
                this.dynobj.setDynValue(name, value);
524
    }
525

    
526
        public boolean hasDynValue(String name) {
527
                return this.dynobj.hasDynValue(name);
528
        }
529

    
530
        public Object invokeDynMethod(String name, DynObject context) throws DynMethodException {
531
                return this.dynobj.invokeDynMethod(this, name, context);
532
        }
533

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

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

    
552
        public CoverageSelection createDefaultCoverageSelection()
553
        throws DataException {
554
                return null;
555
        }
556

    
557
        public CoverageStore getCoverageStore() {
558
                return this;
559
        }
560

    
561
        public abstract CoverageStoreProvider getProvider();
562

    
563
        public void notifyChange(String notification) {
564
                delegateObservable
565
                .notifyObservers(new DefaultCoverageStoreNotification(
566
                                this, notification));
567
        }
568

    
569
        public void notifyChange(String arg0, Resource arg1) {
570
                /*delegateObservable.notifyObservers(new DefaultFeatureStoreNotification(
571
                                this, FeatureStoreNotification.RESOURCE_CHANGED));*/
572
        }
573

    
574
        public void notifyChange(String notification, Command command) {
575
                delegateObservable
576
                .notifyObservers(new DefaultCoverageStoreNotification(
577
                                this, notification, command));
578
        }
579

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