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

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.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 RasterDataServerExplorer explorer               = null;
131

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

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

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

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

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

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

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

    
196
                Buffer buf = null;
197

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
346
        }
347

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
478
    public void doDispose() {
479

    
480
    }
481

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

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

    
488
    }
489

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

    
494
    public void disableNotifications() {
495

    
496
        }
497

    
498
    public void enableNotifications() {
499

    
500
        }
501

    
502
    public void beginComplexNotification() {
503

    
504
        }
505

    
506
    public void endComplexNotification() {
507

    
508
        }
509

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

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

    
516
        }
517

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

    
520
        }
521

    
522
        public void deleteObservers() {
523

    
524
        }
525

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
585
        public abstract CoverageStoreProvider getProvider();
586

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

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

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

    
604
        public DataStore getStore() {
605
                return this;
606
        }
607
        
608
        public void close() {
609
                if(lastBuffer != null) {
610
                        lastBuffer.dispose();
611
                }
612
        }
613

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