Statistics
| Revision:

svn-gvsig-desktop / branches / org.gvsig.desktop-2018a / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.spi / src / main / java / org / gvsig / fmap / dal / raster / spi / AbstractRasterStoreProvider.java @ 43876

History | View | Annotate | Download (10 KB)

1
/* gvSIG. Desktop Geographic Information System.
2
 *
3
 * Copyright ? 2007-2016 gvSIG Association
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18
 * MA  02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us
21
 * at info AT gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.fmap.dal.raster.spi;
24

    
25
import java.awt.Image;
26
import java.util.ArrayList;
27
import java.util.Collection;
28
import java.util.Iterator;
29
import java.util.List;
30

    
31
import org.slf4j.Logger;
32
import org.slf4j.LoggerFactory;
33

    
34
import org.gvsig.fmap.dal.DALLocator;
35
import org.gvsig.fmap.dal.DataStoreParameters;
36
import org.gvsig.fmap.dal.exception.CloseException;
37
import org.gvsig.fmap.dal.exception.InitializeException;
38
import org.gvsig.fmap.dal.exception.OpenException;
39
import org.gvsig.fmap.dal.raster.BandAttributeDescriptor;
40
import org.gvsig.fmap.dal.raster.BandDescriptor;
41
import org.gvsig.fmap.dal.raster.BandQuery;
42
import org.gvsig.fmap.dal.raster.RasterQuery;
43
import org.gvsig.fmap.dal.resource.ResourceManager;
44
import org.gvsig.fmap.dal.resource.spi.ResourceManagerProviderServices;
45
import org.gvsig.fmap.dal.resource.spi.ResourceProvider;
46
import org.gvsig.fmap.dal.spi.DataStoreProviderServices;
47
import org.gvsig.raster.lib.buffer.api.BufferDimensions;
48
import org.gvsig.raster.lib.buffer.api.NoData;
49
import org.gvsig.raster.lib.buffer.api.TileStruct;
50
import org.gvsig.timesupport.Interval;
51
import org.gvsig.tools.dispose.Disposable;
52
import org.gvsig.tools.dispose.DisposeUtils;
53
import org.gvsig.tools.dispose.impl.AbstractDisposable;
54
import org.gvsig.tools.dynobject.DelegatedDynObject;
55
import org.gvsig.tools.dynobject.DynClass;
56
import org.gvsig.tools.dynobject.DynObject;
57
import org.gvsig.tools.dynobject.DynObject_v2;
58
import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException;
59
import org.gvsig.tools.dynobject.exception.DynMethodException;
60
import org.gvsig.tools.exception.BaseException;
61

    
62
/**
63
 * Class to be extended by the providers to be implemented
64
 * @author dmartinezizquierdo
65
 *
66
 */
67
public abstract class AbstractRasterStoreProvider extends AbstractDisposable implements RasterStoreProvider{
68

    
69
    protected static final Logger LOG =
70
        LoggerFactory.getLogger(AbstractRasterStoreProvider.class);
71

    
72
    private RasterStoreProviderServices store;
73
    private DelegatedDynObject metadata;
74
    private DataStoreParameters parameters;
75
    private ArrayList<BandDescriptor> bandDescriptors;
76
    protected BufferDimensions dimensions;
77

    
78
    protected AbstractRasterStoreProvider(DataStoreParameters params,
79
        DataStoreProviderServices storeServices, DynObject metadata) {
80
        this.store=(RasterStoreProviderServices)storeServices;
81
        this.metadata=(DelegatedDynObject)metadata;
82
        this.parameters=params;
83
    }
84

    
85
    /**
86
     * @return the parameters
87
     */
88
    public DataStoreParameters getParameters() {
89
        return parameters;
90
    }
91

    
92
    /**
93
     * Create or get a resource of <code>type</code> for <code>params</code> in
94
     * {@link ResourceManager}
95
     *
96
     * @param type
97
     * @param params
98
     * @return
99
     * @throws InitializeException
100
     */
101
    protected ResourceProvider createResource(String type, Object[] params)
102
            throws InitializeException {
103
        ResourceManagerProviderServices manager = (ResourceManagerProviderServices) DALLocator
104
                .getResourceManager();
105
        ResourceProvider resource = manager.createAddResource(type, params);
106
        return resource;
107
    }
108

    
109
    /**
110
     * unsupported by default (return null), override this otherwise
111
     *
112
     * @see org.gvsig.fmap.dal.spi.DataStoreProvider#getChilds()
113
     */
114
    public Iterator<?> getChilds() {
115
        return null;
116
    }
117

    
118
    /**
119
     * (non-Javadoc)
120
     * @return RasterStoreProviderServices
121
     *
122
     * @see
123
     * org.gvsig.fmap.dal.feature.spi.RasterStoreProvider#getStoreServices()
124
     */
125
    public RasterStoreProviderServices getStoreServices() {
126
        return this.store;
127
    }
128

    
129
    /**
130
     * do nothing by default, override this otherwise
131
     *
132
     * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#close()
133
     */
134
    public void close() throws CloseException {
135
        // Do nothing by default
136
    }
137

    
138
    /**
139
     * do nothing by default, override this otherwise
140
     *
141
     * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#refresh()
142
     */
143
    public void refresh() throws OpenException {
144
        // Do nothing by default
145
    }
146

    
147
    public Interval getInterval() {
148
        // Do nothing
149
        return null;
150
    }
151

    
152
    public Collection<?> getTimes() {
153
        // Do nothing
154
        return null;
155
    }
156

    
157
    public Collection<?> getTimes(Interval interval) {
158
        // Do nothing
159
        return null;
160
    }
161

    
162
    // --- Metadata methods ---
163

    
164
    public void delegate(DynObject dynObject) {
165
        if (this.metadata == null) {
166
            return;
167
        }
168
        this.metadata.delegate(dynObject);
169
    }
170

    
171
    public DynClass getDynClass() {
172
        if (this.metadata == null) {
173
            return null;
174
        }
175
        return this.metadata.getDynClass();
176
    }
177

    
178
    public void implement(DynClass dynClass) {
179
        if (this.metadata == null) {
180
            return;
181
        }
182
        this.metadata.implement(dynClass);
183
    }
184

    
185
    public Object getDynValue(String name) throws DynFieldNotFoundException {
186
        if (this.metadata == null) {
187
            return null;
188
        }
189
        // TODO this.open??
190
        return this.metadata.getDynValue(name);
191
    }
192

    
193
    public void setDynValue(String name, Object value)
194
        throws DynFieldNotFoundException {
195
        if (this.metadata == null) {
196
            return;
197
        }
198
        // TODO this.open??
199
        this.metadata.setDynValue(name, value);
200
    }
201

    
202
    public boolean hasDynValue(String name) {
203
        if (this.metadata == null) {
204
            return false;
205
        }
206
        // TODO this.open??
207
        return this.metadata.hasDynValue(name);
208
    }
209

    
210
    public Object invokeDynMethod(int code, Object[] args)
211
        throws DynMethodException {
212
        if (this.metadata == null) {
213
            return null;
214
        }
215
        // TODO this.open??
216
        return this.metadata.invokeDynMethod(this, code, args);
217
    }
218

    
219
    public Object invokeDynMethod(String name, Object[] args)
220
            throws DynMethodException {
221
        if (this.metadata == null) {
222
            return null;
223
        }
224
        // TODO this.open??
225
        return this.metadata.invokeDynMethod(this, name, args);
226
    }
227

    
228
    public void clear() {
229
        if (metadata != null) {
230
            metadata.clear();
231
        }
232
    }
233

    
234
    protected void doDispose() throws BaseException {
235
        LOG.debug("Dispose AbstractRasterStoreProvider '"+this.getFullName()+"'");
236

    
237
        this.metadata = null;
238
        this.parameters = null;
239
        if (bandDescriptors != null) {
240
            for (Iterator iterator = bandDescriptors.iterator(); iterator.hasNext();) {
241
                BandDescriptor bandDescriptor = (BandDescriptor) iterator.next();
242
                DisposeUtils.dispose(bandDescriptor);
243
                iterator.remove();
244
            }
245
            this.bandDescriptors = null;
246
        }
247

    
248
    }
249

    
250

    
251
    @Override
252
    public TileStruct getTileStruct() {
253
        //Subclasses should implement this method
254
        throw new UnsupportedOperationException();
255
    }
256

    
257
    protected void autoOpen(){
258
        try {
259
            open();
260
        } catch (Exception e) {
261
            throw new RuntimeException(e);
262
        }
263
    }
264

    
265
    @Override
266
    public Image getImageLegend() {
267
        return null;
268
    }
269

    
270
    /**
271
     * @return
272
     */
273
    public List<BandDescriptor> getBandsDescriptor() {
274
        if(bandDescriptors==null){
275
            bandDescriptors = new ArrayList<BandDescriptor>();
276
        }
277
        if(bandDescriptors.isEmpty()){
278
            for (int i = 0; i < this.getBands(); i++) {
279
                bandDescriptors.add(getStoreServices().createBandDescriptor(i,
280
                    new ArrayList<BandAttributeDescriptor>()));
281

    
282
            }
283
        }
284
        return this.bandDescriptors;
285
    }
286

    
287

    
288
    @Override
289
    public BandDescriptorServices getBandDescriptor(int band) {
290
        if(band > this.getBands()){
291
            throw new IllegalArgumentException("Invalid band");
292
        }
293
        return (BandDescriptorServices) this.getBandsDescriptor().get(band);
294
    }
295

    
296

    
297
    public NoData getNoData(RasterQuery rasterQuery, int band){
298
        List<BandQuery> bands = rasterQuery.getBands();
299
        for (BandQuery bandQuery : bands) {
300
            if(bandQuery.getDescriptor().getBand()==band){
301
                NoData noData = bandQuery.getDescriptor().getNoData();
302
                if(noData == null) {
303
                    break;
304
                }
305
                return noData;
306
            }
307
        }
308
        return this.getBandDescriptor(band).getNoData();
309
    }
310

    
311
    public NoData[] getNoDatas(){
312
        NoData[] noDatas = new NoData[this.getBands()];
313
        for (int i = 0; i < this.getBands(); i++) {
314
            noDatas[i] = this.getBandDescriptor(i).getNoData();
315
        }
316
        return noDatas;
317
    }
318

    
319
    public int[] getDataTypes(){
320
        int[] dataTypes = new int[this.getBands()];
321
        for (int i = 0; i < this.getBands(); i++) {
322
            dataTypes[i] = this.getBandDescriptor(i).getDataType();
323
        }
324
        return dataTypes;
325
    }
326

    
327
    @Override
328
    public boolean isTiled() {
329
        return false;
330
    }
331

    
332
    @Override
333
    public boolean hasDynMethod(String name) {
334
        if( metadata instanceof DynObject_v2 ) {
335
            return ((DynObject_v2)this.metadata).hasDynMethod(name);
336
        }
337
        return false;
338
    }
339

    
340
}