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

History | View | Annotate | Download (19.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.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.raster.impl.provider.RemoteRasterProvider;
79
import org.gvsig.tools.ToolsLocator;
80
import org.gvsig.tools.dispose.impl.AbstractDisposable;
81
import org.gvsig.tools.dynobject.DelegatedDynObject;
82
import org.gvsig.tools.dynobject.DynClass;
83
import org.gvsig.tools.dynobject.DynObject;
84
import org.gvsig.tools.dynobject.DynObjectManager;
85
import org.gvsig.tools.dynobject.DynStruct;
86
import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException;
87
import org.gvsig.tools.dynobject.exception.DynMethodException;
88
import org.gvsig.tools.exception.BaseException;
89
import org.gvsig.tools.observer.impl.DelegateWeakReferencingObservable;
90
import org.gvsig.tools.undo.command.Command;
91
import org.gvsig.tools.visitor.Visitor;
92

    
93
/**
94
 * Default implementation for RasterDataSource
95
 * 
96
 * @author Nacho Brodin (nachobrodin@gmail.com)
97
 */
98
public abstract class AbstractRasterDataStore extends AbstractDisposable 
99
        implements RasterDataStore, CoverageStoreProviderServices, QueryableRaster, DataStoreInitializer {
100
        public static final String         PERSISTENT_NAME        = "AbstractRasterDataStore_Persistent";
101
    public static final String         PERSISTENT_DESCRIPTION = "AbstractRasterDataStore Persistent";
102
   // private Logger                     log                    = LoggerFactory.getLogger(AbstractRasterDataStore.class);
103
        protected DefaultRasterQuery       currentQuery           = null;
104
        protected BandListImpl             storeBandList          = null;
105
        protected RasterProvider           provider               = null;
106
        protected Metadata                 metadata               = null;
107
        
108
        /**
109
         * Lista de paletas asociadas a las bandas cargadas en el DataSource. Estas son calculadas
110
         * en las funciones que asignan las bandas a dibujar (addDrawableBands)
111
         */
112
        private ColorTable[]                           palette                = null;
113

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

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

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

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

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

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

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

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

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

    
335
        }
336

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
479
    public void enableNotifications() {
480
                
481
        }
482

    
483
    public void beginComplexNotification() {
484
                
485
        }
486

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

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

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

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

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

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

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

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

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

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

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

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

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

    
566
        public abstract CoverageStoreProvider getProvider();
567

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

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

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

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