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

History | View | Annotate | Download (20.1 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.RasterDataServerExplorer;
50
import org.gvsig.fmap.dal.coverage.store.RasterDataStore;
51
import org.gvsig.fmap.dal.coverage.store.RasterQuery;
52
import org.gvsig.fmap.dal.coverage.store.parameter.RasterDataParameters;
53
import org.gvsig.fmap.dal.coverage.store.parameter.RasterFileStoreParameters;
54
import org.gvsig.fmap.dal.coverage.store.props.ColorTable;
55
import org.gvsig.fmap.dal.coverage.store.props.Metadata;
56
import org.gvsig.fmap.dal.coverage.store.props.TimeSeries;
57
import org.gvsig.fmap.dal.exception.DataException;
58
import org.gvsig.fmap.dal.exception.InitializeException;
59
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
60
import org.gvsig.fmap.dal.raster.CoverageSelection;
61
import org.gvsig.fmap.dal.raster.CoverageStore;
62
import org.gvsig.fmap.dal.raster.spi.CoverageStoreProvider;
63
import org.gvsig.fmap.dal.raster.spi.CoverageStoreProviderServices;
64
import org.gvsig.fmap.dal.resource.Resource;
65
import org.gvsig.fmap.dal.spi.DataStoreInitializer;
66
import org.gvsig.fmap.dal.spi.DataStoreProvider;
67
import org.gvsig.metadata.MetadataLocator;
68
import org.gvsig.metadata.MetadataManager;
69
import org.gvsig.metadata.exceptions.MetadataException;
70
import org.gvsig.raster.impl.DefaultRasterManager;
71
import org.gvsig.raster.impl.buffer.DefaultRasterQuery;
72
import org.gvsig.raster.impl.datastruct.BandListImpl;
73
import org.gvsig.raster.impl.datastruct.DatasetBandImpl;
74
import org.gvsig.raster.impl.datastruct.ExtentImpl;
75
import org.gvsig.raster.impl.grid.render.DefaultRender;
76
import org.gvsig.raster.impl.process.vector.PotraceVectorization;
77
import org.gvsig.raster.impl.provider.AbstractRasterProvider;
78
import org.gvsig.raster.impl.provider.RasterProvider;
79
import org.gvsig.raster.impl.provider.RemoteRasterProvider;
80
import org.gvsig.tools.ToolsLocator;
81
import org.gvsig.tools.dispose.impl.AbstractDisposable;
82
import org.gvsig.tools.dynobject.DelegatedDynObject;
83
import org.gvsig.tools.dynobject.DynClass;
84
import org.gvsig.tools.dynobject.DynObject;
85
import org.gvsig.tools.dynobject.DynObjectManager;
86
import org.gvsig.tools.dynobject.DynStruct;
87
import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException;
88
import org.gvsig.tools.dynobject.exception.DynMethodException;
89
import org.gvsig.tools.exception.BaseException;
90
import org.gvsig.tools.observer.impl.DelegateWeakReferencingObservable;
91
import org.gvsig.tools.undo.command.Command;
92
import org.gvsig.tools.visitor.Visitor;
93

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

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

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

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

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

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

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

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

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

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

    
337
        }
338

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

    
348
        //****************************************************
349
        //*********Implementing DataStore methods*************
350
        //****************************************************
351

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

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

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

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

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

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

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

    
422
        public void setSelection(DataSet selection) throws DataException {
423
        }
424

    
425
        public DataSet createSelection() throws DataException {
426
                return null;
427
        }
428

    
429
        public Iterator<?> getChildren() {
430
                return null;
431
        }
432

    
433
        public DataServerExplorer getExplorer() throws DataException, ValidateDataParametersException {
434
                return explorer;
435
        }
436
        
437
        /**
438
     * Sets the explorer
439
     * @param explorer
440
     */
441
    public void setExplorer(RasterDataServerExplorer explorer) {
442
            this.explorer = explorer;
443
    }
444
        
445
        public DataQuery createQuery() {
446
                return new DefaultRasterQuery();
447
        }
448
        
449
        //****************************************************
450
        //*********Implementing Metadata methods*************
451
        //****************************************************
452
        
453
    public Object getMetadataID() {
454
            if(parameters != null && parameters instanceof RasterFileStoreParameters)
455
                    return ((RasterFileStoreParameters)parameters).getFile();
456
            if(parameters != null && parameters instanceof RasterDataParameters)
457
                    return ((RasterDataParameters)parameters).getURI();
458
            return null;
459
    }
460

    
461
        public Set<?> getMetadataChildren() {
462
            return null;
463
    }
464
    
465
    //****************************************************
466
        //*********Implementing Disposable methods************
467
        //****************************************************
468
    
469
    public void doDispose() {
470
            
471
    }
472
    
473
    //****************************************************
474
        //*********Implementing Visitable methods*************
475
        //****************************************************
476
    
477
    public void accept(Visitor visitor) throws BaseException {
478
            
479
    }
480
    
481
    //****************************************************
482
        //****Implementing ComplexObservable methods**********
483
        //****************************************************
484
    
485
    public void disableNotifications() {
486
                
487
        }
488

    
489
    public void enableNotifications() {
490
                
491
        }
492

    
493
    public void beginComplexNotification() {
494
                
495
        }
496

    
497
    public void endComplexNotification() {
498
                
499
        }
500
    
501
    //****************************************************
502
        //********Implementing Observable methods*************
503
        //****************************************************
504
    
505
        public void addObserver(org.gvsig.tools.observer.Observer o) {
506
                
507
        }
508

    
509
        public void deleteObserver(org.gvsig.tools.observer.Observer o) {
510
                
511
        }
512

    
513
        public void deleteObservers() {
514
                
515
        }
516
        
517
    //****************************************************
518
        //********Implementing DynObject methods*************
519
        //****************************************************
520
        
521
        public DynClass getDynClass() {
522
                return this.dynobj.getDynClass();
523
        }
524

    
525
        public void implement(DynClass dynClass) {
526
                this.dynobj.implement(dynClass);
527
    }
528

    
529
        public void delegate(DynObject dynObject) {
530
                this.dynobj.delegate(dynObject);
531
    }
532

    
533
        public Object getDynValue(String name)  throws DynFieldNotFoundException {
534
                return this.dynobj.getDynValue(name);
535
        }
536

    
537
        public void setDynValue(String name, Object value)  throws DynFieldNotFoundException {
538
                this.dynobj.setDynValue(name, value);
539
    }
540

    
541
        public boolean hasDynValue(String name) {
542
                return this.dynobj.hasDynValue(name);
543
        }
544

    
545
        public Object invokeDynMethod(String name, DynObject context) throws DynMethodException {
546
                return this.dynobj.invokeDynMethod(this, name, context);
547
        }
548

    
549
        public Object invokeDynMethod(int code, DynObject context) throws DynMethodException {
550
                return this.dynobj.invokeDynMethod(this, code, context);
551
        }
552

    
553
        public void clear() {
554
                if (dynobj != null) {
555
                        dynobj.clear();
556
                }
557
    }
558
        
559
        public DataStoreParameters getParameters() {
560
                return parameters;
561
        }
562
        
563
        //***********************************************************************
564
        //********Implementing CoverageStoreProviderServices methods*************
565
        //***********************************************************************
566

    
567
        public CoverageSelection createDefaultCoverageSelection()
568
        throws DataException {
569
                return null;
570
        }
571

    
572
        public CoverageStore getCoverageStore() {
573
                return this;
574
        }
575

    
576
        public abstract CoverageStoreProvider getProvider();
577

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

    
584
        public void notifyChange(String arg0, Resource arg1) {
585
                /*delegateObservable.notifyObservers(new DefaultFeatureStoreNotification(
586
                                this, FeatureStoreNotification.RESOURCE_CHANGED));*/
587
        }
588

    
589
        public void notifyChange(String notification, Command command) {
590
                delegateObservable
591
                .notifyObservers(new DefaultCoverageStoreNotification(
592
                                this, notification, command));
593
        }
594

    
595
        public DataStore getStore() {
596
                return this;
597
        }
598
        
599
        public void setCoordTrans(ICoordTrans t) {
600
                this.coordTrans = t;
601
        }
602
        
603
        public void close() {
604
                if(lastBuffer != null) {
605
                        lastBuffer.dispose();
606
                }
607
        }
608
        
609
        protected void finalize() throws Throwable {
610
                currentQuery           = null;
611
                if(palette != null) {
612
                        for (int i = 0; i < palette.length; i++) {
613
                                palette[i] = null;
614
                        }
615
                        palette = null;
616
                }
617
                lastBuffer             = null;
618
                dataManager            = null;
619
                parameters             = null;
620
                metadata               = null;
621
                delegateObservable     = null;
622
                render                 = null;
623
                super.finalize();
624
        }
625
}