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

History | View | Annotate | Download (20.2 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.net.URI;
26
import java.util.Iterator;
27
import java.util.List;
28
import java.util.Set;
29

    
30
import org.cresques.cts.ICoordTrans;
31

    
32
import org.gvsig.fmap.dal.DataManager;
33
import org.gvsig.fmap.dal.DataQuery;
34
import org.gvsig.fmap.dal.DataServerExplorer;
35
import org.gvsig.fmap.dal.DataSet;
36
import org.gvsig.fmap.dal.DataStore;
37
import org.gvsig.fmap.dal.DataStoreParameters;
38
import org.gvsig.fmap.dal.coverage.RasterLocator;
39
import org.gvsig.fmap.dal.coverage.dataset.Buffer;
40
import org.gvsig.fmap.dal.coverage.datastruct.BandList;
41
import org.gvsig.fmap.dal.coverage.datastruct.DatasetBand;
42
import org.gvsig.fmap.dal.coverage.datastruct.Extent;
43
import org.gvsig.fmap.dal.coverage.datastruct.GeoPointList;
44
import org.gvsig.fmap.dal.coverage.exception.BandNotFoundInListException;
45
import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException;
46
import org.gvsig.fmap.dal.coverage.exception.QueryException;
47
import org.gvsig.fmap.dal.coverage.exception.RasterDriverException;
48
import org.gvsig.fmap.dal.coverage.exception.RmfSerializerException;
49
import org.gvsig.fmap.dal.coverage.grid.render.Render;
50
import org.gvsig.fmap.dal.coverage.process.vector.Vectorization;
51
import org.gvsig.fmap.dal.coverage.store.RasterDataServerExplorer;
52
import org.gvsig.fmap.dal.coverage.store.RasterDataStore;
53
import org.gvsig.fmap.dal.coverage.store.RasterQuery;
54
import org.gvsig.fmap.dal.coverage.store.parameter.RasterDataParameters;
55
import org.gvsig.fmap.dal.coverage.store.parameter.RasterFileStoreParameters;
56
import org.gvsig.fmap.dal.coverage.store.props.ColorTable;
57
import org.gvsig.fmap.dal.coverage.store.props.Metadata;
58
import org.gvsig.fmap.dal.coverage.store.props.TimeSeries;
59
import org.gvsig.fmap.dal.exception.DataException;
60
import org.gvsig.fmap.dal.exception.InitializeException;
61
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
62
import org.gvsig.fmap.dal.raster.CoverageSelection;
63
import org.gvsig.fmap.dal.raster.CoverageStore;
64
import org.gvsig.fmap.dal.raster.spi.CoverageStoreProvider;
65
import org.gvsig.fmap.dal.raster.spi.CoverageStoreProviderServices;
66
import org.gvsig.fmap.dal.resource.Resource;
67
import org.gvsig.fmap.dal.spi.DataStoreInitializer;
68
import org.gvsig.fmap.dal.spi.DataStoreProvider;
69
import org.gvsig.metadata.MetadataLocator;
70
import org.gvsig.metadata.MetadataManager;
71
import org.gvsig.metadata.exceptions.MetadataException;
72
import org.gvsig.raster.impl.DefaultRasterManager;
73
import org.gvsig.raster.impl.buffer.DefaultRasterQuery;
74
import org.gvsig.raster.impl.datastruct.BandListImpl;
75
import org.gvsig.raster.impl.datastruct.DatasetBandImpl;
76
import org.gvsig.raster.impl.datastruct.ExtentImpl;
77
import org.gvsig.raster.impl.grid.render.DefaultRender;
78
import org.gvsig.raster.impl.process.vector.PotraceVectorization;
79
import org.gvsig.raster.impl.provider.AbstractRasterProvider;
80
import org.gvsig.raster.impl.provider.RasterProvider;
81
import org.gvsig.raster.impl.provider.RemoteRasterProvider;
82
import org.gvsig.tools.ToolsLocator;
83
import org.gvsig.tools.dispose.impl.AbstractDisposable;
84
import org.gvsig.tools.dynobject.DelegatedDynObject;
85
import org.gvsig.tools.dynobject.DynClass;
86
import org.gvsig.tools.dynobject.DynObject;
87
import org.gvsig.tools.dynobject.DynObjectManager;
88
import org.gvsig.tools.dynobject.DynStruct;
89
import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException;
90
import org.gvsig.tools.dynobject.exception.DynMethodException;
91
import org.gvsig.tools.exception.BaseException;
92
import org.gvsig.tools.observer.impl.DelegateWeakReferencingObservable;
93
import org.gvsig.tools.undo.command.Command;
94
import org.gvsig.tools.visitor.Visitor;
95

    
96
/**
97
 * Default implementation for RasterDataSource
98
 *
99
 * @author Nacho Brodin (nachobrodin@gmail.com)
100
 */
101
public abstract class AbstractRasterDataStore extends AbstractDisposable
102
        implements RasterDataStore, CoverageStoreProviderServices, QueryableRaster, DataStoreInitializer {
103
        public static final String         PERSISTENT_NAME        = "AbstractRasterDataStore_Persistent";
104
    public static final String         PERSISTENT_DESCRIPTION = "AbstractRasterDataStore Persistent";
105
   // private Logger                     log                    = LoggerFactory.getLogger(AbstractRasterDataStore.class);
106
        protected DefaultRasterQuery       currentQuery           = null;
107
        protected BandListImpl             storeBandList          = null;
108
        protected RasterProvider           provider               = null;
109
        protected Metadata                 metadata               = null;
110

    
111
        /**
112
         * Lista de paletas asociadas a las bandas cargadas en el DataSource. Estas son calculadas
113
         * en las funciones que asignan las bandas a dibujar (addDrawableBands)
114
         */
115
        private ColorTable[]                           palette                = null;
116

    
117
        /**
118
         * Ancho y alto en pixeles del ?ltimo buffer asignado
119
         */
120
        protected double                                nWidth                 = 0;
121
        protected double                                nHeight                = 0;
122
        private Buffer                     lastBuffer             = null;
123

    
124
        private DataManager                dataManager            = null;
125
        protected DataStoreParameters      parameters             = null;
126
        private DelegatedDynObject         dynobj                 = null;
127
        private DelegateWeakReferencingObservable
128
                                           delegateObservable     = new DelegateWeakReferencingObservable(this);
129
        private Render                     render                 = null;
130
        protected ICoordTrans              coordTrans             = null;
131
        protected RasterDataServerExplorer explorer               = null;
132

    
133
        public BandList getDefaultBandList() {
134
                if(storeBandList == null) {
135
                        initializeBandList();
136
                }
137
                return storeBandList;
138
        }
139

    
140
        public void initializeBandList() {
141
                if(provider != null) {
142
                        storeBandList = new BandListImpl();
143
                        storeBandList.clear();
144
                        for(int i = 0; i < provider.getBandCount(); i++) {
145
                                try {
146
                                        int dataType = provider.getDataType()[i];
147
                                        String filename;
148
                                        URI uriByBand = provider.getURIByBand(i);
149
                                        if("FILE".equalsIgnoreCase(uriByBand.getScheme())){
150
                                            filename = uriByBand.getPath();
151
                                        } else {
152
                        filename = uriByBand.toString();
153
                                        }
154
                                        DatasetBand band = new DatasetBandImpl(filename,
155
                                                        provider.getBandPositionByProvider(i),
156
                                                        dataType,
157
                                                        provider.getBandCount());
158
                                        storeBandList.addBand(band, i);
159
                                } catch(BandNotFoundInListException ex) {
160
                                        //No a?adimos la banda
161
                                }
162
                        }
163

    
164
                        int[] drawableBands = new int[storeBandList.getBandCount()];
165
                        for (int i = 0; i < storeBandList.getBandCount(); i++) {
166
                                drawableBands[i] = i;
167
                        }
168

    
169
                        storeBandList.setDrawableBands(drawableBands);
170
                }
171
        }
172

    
173
        public Buffer query(RasterQuery query) throws ProcessInterruptedException, QueryException {
174
                Object[] obj = queryArray(query);
175
                if(obj != null && obj.length > 0 && obj[0] instanceof Buffer)
176
                        return ((Buffer)obj[0]);
177
                return null;
178
        }
179

    
180
        /**
181
         * <UL>Tasks to do in the <code>Datastore</code>
182
         * <LI>Check parameters and adjust them to not exceed the window size</LI>
183
         * <LI>Build the band list to draw</LI>
184
         * <LI>Create the buffers (RO or RW) and set them in the <code>RasterQuery</code> object</LI>
185
         * <LI>Calculate steps when supersampling is applied</LI>
186
         * </UL>
187
         */
188
        public Object[] queryArray(RasterQuery query)
189
                        throws ProcessInterruptedException, QueryException {
190
                Object[] result = null;
191
                currentQuery = (DefaultRasterQuery)query;
192
                DefaultRasterQuery q = currentQuery;
193

    
194
                if(isTiled() || provider instanceof RemoteRasterProvider)
195
                        q.dontBuildBuffer();
196

    
197
                Buffer buf = null;
198

    
199
                if(q.requestHasShift()) {
200
                        //TODO: Sin probar
201
                        FramedBufferResampling framedBufferResampling = new FramedBufferResampling(q, this);
202
                        buf = framedBufferResampling.query(provider);
203
                } else {
204
                        q.calculateParameters(this);
205
                        try {
206
                                if(q.isReadOnly() && !isTiled())
207
                                        buf = q.getBufferForProviders();
208
                                else
209
                                        buf = ((AbstractRasterProvider)provider).getDataSet(q);
210
                        } catch (RasterDriverException e) {
211
                                throw new QueryException("Error getting data", e);
212
                        }
213

    
214
                        if(q.isSupersamplingOptionActive() && q.isSupersamplingTheRequest()) {
215
                                buf = buf.getAdjustedWindow(q.getBufWidth(), q.getBufHeight(), Buffer.INTERPOLATION_NearestNeighbour);
216
                        }
217
                }
218
                if(buf != null)
219
                        buf.setDataExtent(q.getAdjustedRequestBoundingBox().toRectangle2D());
220
                result = new Object[]{buf};
221

    
222
                if(q.isStoredLastBuffer())
223
                        lastBuffer = buf;
224
                else
225
                        lastBuffer = null;
226
                q.storeLastBuffer(false);
227

    
228
                if(result != null && result.length > 0 && result[0] instanceof Buffer) {
229
                        ((Buffer)result[0]).setStore(this);
230
                        if(getNoDataValue() != null && getNoDataValue().isDefined())
231
                                ((Buffer)result[0]).setNoDataValue(getNoDataValue());
232
                        return result;
233
                }
234
                return null;
235
        }
236

    
237
        public Extent adjustToExtent(Extent e) {
238
                Extent currentExtent = this.getExtent();
239

    
240
                double minx = e.getMin().getX() < currentExtent.getMin().getX() ? currentExtent.getMin().getX() : e.getMin().getX();
241
                double maxx = e.getMax().getX() > currentExtent.getMax().getX() ? currentExtent.getMax().getX() : e.getMax().getX();
242
                double miny = e.getMin().getY() < currentExtent.getMin().getY() ? currentExtent.getMin().getY() : e.getMin().getY();
243
                double maxy = e.getMax().getY() > currentExtent.getMax().getY() ? currentExtent.getMax().getY() : e.getMax().getY();
244

    
245
                return new ExtentImpl(minx, maxy, maxx, miny);
246
        }
247

    
248
        public String getFullName() {
249
                return getName();
250
        }
251

    
252
        public Buffer getLastBuffer() {
253
                return lastBuffer;
254
        }
255

    
256
        public TimeSeries getTimeSerials() throws RmfSerializerException {
257
                if(getProvider() instanceof RasterProvider)
258
                        return ((RasterProvider)getProvider()).getTimeSerials();
259
                return null;
260
        }
261

    
262
        public void setTimeSerials(TimeSeries serialInfo) throws RmfSerializerException {
263
                if(getProvider() instanceof RasterProvider)
264
                        ((RasterProvider)getProvider()).setTimeSerials(serialInfo);
265
        }
266

    
267
        public abstract void saveColorTableToRmf(ColorTable table) throws RmfSerializerException;
268

    
269
        public abstract void saveGeoPointListToRmf(GeoPointList pointList) throws RmfSerializerException;
270

    
271
        public abstract void saveROIFileListToRmf(List<File> fileList) throws RmfSerializerException;
272

    
273
        /**
274
         * Saves information about serials
275
         * @param serialInfo object to save
276
         * @throws RmfSerializerException
277
         */
278
        public abstract void saveSerialInfoToRmf(TimeSeries serialInfo) throws RmfSerializerException;
279

    
280
        /**
281
         * Loads information about serials
282
         * @param serialInfo
283
         * @return true if work correctly
284
         * @throws RmfSerializerException
285
         */
286
        public abstract boolean loadSerialInfoFromRmf(TimeSeries serialInfo);
287

    
288
        public Vectorization createVectorizeObject() throws QueryException, ProcessInterruptedException {
289
                return new PotraceVectorization((RasterDataStore)this);
290
        }
291

    
292
        public Render getRender() {
293
                if(render == null)
294
                        render = new DefaultRender(this);
295
                return render;
296
        }
297

    
298
        public void setRender(Render render) {
299
                this.render = render;
300
        }
301

    
302
        public boolean isFileSupported(String fName) {
303
                return RasterLocator.getManager().getProviderServices().isExtensionSupported(fName);
304
        }
305

    
306
        /**
307
         * Registers metadata definition
308
         * @throws MetadataException
309
         */
310
        public static void registerMetadataDefinition() throws MetadataException {
311
                MetadataManager manager = MetadataLocator.getMetadataManager();
312

    
313
                if(manager == null)
314
                        return;
315

    
316
                if( manager.getDefinition(METADATA_DEFINITION_NAME) == null  ) {
317
                        DynStruct defnition = manager.addDefinition(
318
                                        METADATA_DEFINITION_NAME,
319
                                        METADATA_DEFINITION_NAME
320
                        );
321
                        defnition.extend(
322
                                        MetadataManager.METADATA_NAMESPACE,
323
                                        DataStore.METADATA_DEFINITION_NAME
324
                        );
325
                }
326
        }
327

    
328
        public int getSourceType() {
329
                return ((RasterProvider)getProvider()).getSourceType();
330
        }
331

    
332
    //****************************************************
333
        //****Implementing DataStoreImplementation methods****
334
        //****************************************************
335

    
336
        public void intializePhase1(DataManager dataManager,
337
                        DataStoreParameters parameters) throws InitializeException {
338
                DynObjectManager dynManager = ToolsLocator.getDynObjectManager();
339

    
340
                this.dynobj = (DelegatedDynObject) dynManager
341
                                .createDynObject(
342
                                                MetadataLocator.getMetadataManager().getDefinition(DataStore.SPATIAL_METADATA_DEFINITION_NAME)
343
                                );
344
                this.dataManager = dataManager;
345
                this.parameters = parameters;
346

    
347
        }
348

    
349
        public void intializePhase2(DataStoreProvider provider)
350
                        throws InitializeException {
351
                setProvider((RasterProvider)provider);
352
        }
353

    
354
        public DataManager getManager() {
355
                return this.dataManager;
356
        }
357

    
358
        //****************************************************
359
        //*********Implementing DataStore methods*************
360
        //****************************************************
361

    
362
        public String getProviderName() {
363
                return getProvider().getFullName();
364
        }
365

    
366
        public void refresh() throws DataException {
367
                return;
368
        }
369

    
370
        public DataSet getDataSet() throws DataException {
371
                RasterQuery query = DefaultRasterManager.getInstance().createQuery();
372
                query.setSupersamplingOption(false);
373
                query.setAreaOfInterest();
374
                int[] bands = new int[getBandCount()];
375
                for (int i = 0; i < bands.length; i++) {
376
                        bands[i] = i;
377
                }
378
                query.setDrawableBands(bands);
379
                return getDataSet(query);
380
        }
381

    
382
        public DataSet getDataSet(DataQuery dataQuery) throws DataException {
383
                if(dataQuery instanceof RasterQuery) {
384
                        Object[] obj = null;
385
                        try {
386
                                obj = queryArray((RasterQuery)dataQuery);
387
                        } catch (ProcessInterruptedException e) {
388
                                throw new RasterDriverException("", e);
389
                        } catch (QueryException e) {
390
                                throw new RasterDriverException(e.getMessage(), e);
391
                        }
392
                        if(obj != null && obj.length > 0 && obj[0] instanceof Buffer)
393
                                return (Buffer)obj[0];
394
                }
395
                return null;
396
        }
397

    
398
        public void accept(Visitor visitor, DataQuery dataQuery)
399
                        throws BaseException {
400
        }
401

    
402
        public void getDataSet(org.gvsig.tools.observer.Observer observer) throws DataException {
403
                RasterQuery query = DefaultRasterManager.getInstance().createQuery();
404
                query.setSupersamplingOption(false);
405
                query.setAreaOfInterest();
406
                int[] bands = new int[getBandCount()];
407
                for (int i = 0; i < bands.length; i++) {
408
                        bands[i] = i;
409
                }
410
                query.setDrawableBands(bands);
411
                getDataSet(query, observer);
412
        }
413

    
414
        public void getDataSet(DataQuery dataQuery, org.gvsig.tools.observer.Observer observer) throws DataException {
415
                if(dataQuery instanceof RasterQuery) {
416
                        Object[] obj = null;
417
                        try {
418
                                obj = queryArray((RasterQuery)dataQuery);
419
                        } catch (ProcessInterruptedException e) {
420
                        } catch (QueryException e) {
421
                                throw new RasterDriverException(e.getMessage(), e);
422
                        }
423
                        if(obj != null && obj.length > 0 && obj[0] instanceof Buffer)
424
                                observer.update(this, obj[0]);
425
                }
426
        }
427

    
428
        public DataSet getSelection() throws DataException {
429
                return null;
430
        }
431

    
432
        public void setSelection(DataSet selection) throws DataException {
433
        }
434

    
435
        public DataSet createSelection() throws DataException {
436
                return null;
437
        }
438

    
439
        public Iterator<?> getChildren() {
440
                return null;
441
        }
442

    
443
        public DataServerExplorer getExplorer() throws DataException, ValidateDataParametersException {
444
                return explorer;
445
        }
446

    
447
        /**
448
     * Sets the explorer
449
     * @param explorer
450
     */
451
    public void setExplorer(RasterDataServerExplorer explorer) {
452
            this.explorer = explorer;
453
    }
454

    
455
        public DataQuery createQuery() {
456
                return new DefaultRasterQuery();
457
        }
458

    
459
        //****************************************************
460
        //*********Implementing Metadata methods*************
461
        //****************************************************
462

    
463
    public Object getMetadataID() {
464
            if(parameters != null && parameters instanceof RasterFileStoreParameters)
465
                    return ((RasterFileStoreParameters)parameters).getFile();
466
            if(parameters != null && parameters instanceof RasterDataParameters)
467
                    return ((RasterDataParameters)parameters).getURI();
468
            return null;
469
    }
470

    
471
        public Set<?> getMetadataChildren() {
472
            return null;
473
    }
474

    
475
    //****************************************************
476
        //*********Implementing Disposable methods************
477
        //****************************************************
478

    
479
    public void doDispose() {
480

    
481
    }
482

    
483
    //****************************************************
484
        //*********Implementing Visitable methods*************
485
        //****************************************************
486

    
487
    public void accept(Visitor visitor) throws BaseException {
488

    
489
    }
490

    
491
    //****************************************************
492
        //****Implementing ComplexObservable methods**********
493
        //****************************************************
494

    
495
    public void disableNotifications() {
496

    
497
        }
498

    
499
    public void enableNotifications() {
500

    
501
        }
502

    
503
    public void beginComplexNotification() {
504

    
505
        }
506

    
507
    public void endComplexNotification() {
508

    
509
        }
510

    
511
    //****************************************************
512
        //********Implementing Observable methods*************
513
        //****************************************************
514

    
515
        public void addObserver(org.gvsig.tools.observer.Observer o) {
516

    
517
        }
518

    
519
        public void deleteObserver(org.gvsig.tools.observer.Observer o) {
520

    
521
        }
522

    
523
        public void deleteObservers() {
524

    
525
        }
526

    
527
    //****************************************************
528
        //********Implementing DynObject methods*************
529
        //****************************************************
530

    
531
        public DynClass getDynClass() {
532
                return this.dynobj.getDynClass();
533
        }
534

    
535
        public void implement(DynClass dynClass) {
536
                this.dynobj.implement(dynClass);
537
    }
538

    
539
        public void delegate(DynObject dynObject) {
540
                this.dynobj.delegate(dynObject);
541
    }
542

    
543
        public Object getDynValue(String name)  throws DynFieldNotFoundException {
544
                return this.dynobj.getDynValue(name);
545
        }
546

    
547
        public void setDynValue(String name, Object value)  throws DynFieldNotFoundException {
548
                this.dynobj.setDynValue(name, value);
549
    }
550

    
551
        public boolean hasDynValue(String name) {
552
                return this.dynobj.hasDynValue(name);
553
        }
554

    
555
        public Object invokeDynMethod(String name, DynObject context) throws DynMethodException {
556
                return this.dynobj.invokeDynMethod(this, name, context);
557
        }
558

    
559
        public Object invokeDynMethod(int code, DynObject context) throws DynMethodException {
560
                return this.dynobj.invokeDynMethod(this, code, context);
561
        }
562

    
563
        public void clear() {
564
                if (dynobj != null) {
565
                        dynobj.clear();
566
                }
567
    }
568

    
569
        public DataStoreParameters getParameters() {
570
                return parameters;
571
        }
572

    
573
        //***********************************************************************
574
        //********Implementing CoverageStoreProviderServices methods*************
575
        //***********************************************************************
576

    
577
        public CoverageSelection createDefaultCoverageSelection()
578
        throws DataException {
579
                return null;
580
        }
581

    
582
        public CoverageStore getCoverageStore() {
583
                return this;
584
        }
585

    
586
        public abstract CoverageStoreProvider getProvider();
587

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

    
594
        public void notifyChange(String arg0, Resource arg1) {
595
                /*delegateObservable.notifyObservers(new DefaultFeatureStoreNotification(
596
                                this, FeatureStoreNotification.RESOURCE_CHANGED));*/
597
        }
598

    
599
        public void notifyChange(String notification, Command command) {
600
                delegateObservable
601
                .notifyObservers(new DefaultCoverageStoreNotification(
602
                                this, notification, command));
603
        }
604

    
605
        public DataStore getStore() {
606
                return this;
607
        }
608

    
609
        public void setCoordTrans(ICoordTrans t) {
610
                this.coordTrans = t;
611
        }
612

    
613
        public void close() {
614
                if(lastBuffer != null) {
615
                        lastBuffer.dispose();
616
                }
617
        }
618

    
619
        protected void finalize() throws Throwable {
620
                currentQuery           = null;
621
                if(palette != null) {
622
                        for (int i = 0; i < palette.length; i++) {
623
                                palette[i] = null;
624
                        }
625
                        palette = null;
626
                }
627
                lastBuffer             = null;
628
                dataManager            = null;
629
                parameters             = null;
630
                metadata               = null;
631
                delegateObservable     = null;
632
                render                 = null;
633
                super.finalize();
634
        }
635
}