Statistics
| Revision:

gvsig-raster / org.gvsig.raster / branches / org.gvsig.raster.2.4 / org.gvsig.raster / org.gvsig.fmap.dal.raster / org.gvsig.fmap.dal.raster.impl / src / main / java / org / gvsig / fmap / dal / raster / impl / DefaultRasterSet.java @ 6701

History | View | Annotate | Download (12.6 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.impl;
24

    
25
import java.awt.image.BufferedImage;
26
import java.util.Iterator;
27

    
28
import org.cresques.cts.ICoordTrans;
29
import org.cresques.cts.IProjection;
30
import org.slf4j.Logger;
31
import org.slf4j.LoggerFactory;
32

    
33
import org.gvsig.fmap.dal.DataStore;
34
import org.gvsig.fmap.dal.exception.DataException;
35
import org.gvsig.fmap.dal.raster.api.RasterQuery;
36
import org.gvsig.fmap.dal.raster.api.RasterSet;
37
import org.gvsig.fmap.dal.raster.api.RasterStore;
38
import org.gvsig.fmap.dal.raster.api.exceptions.RasterSetInitializeException;
39
import org.gvsig.fmap.dal.raster.spi.RasterStoreProvider;
40
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
41
import org.gvsig.fmap.geom.GeometryLocator;
42
import org.gvsig.fmap.geom.exception.CreateEnvelopeException;
43
import org.gvsig.fmap.geom.primitive.Envelope;
44
import org.gvsig.fmap.geom.primitive.Point;
45
import org.gvsig.raster.lib.buffer.api.Band;
46
import org.gvsig.raster.lib.buffer.api.Band.BandByte;
47
import org.gvsig.raster.lib.buffer.api.Band.BandDouble;
48
import org.gvsig.raster.lib.buffer.api.Band.BandFloat;
49
import org.gvsig.raster.lib.buffer.api.Band.BandInt;
50
import org.gvsig.raster.lib.buffer.api.Band.BandShort;
51
import org.gvsig.raster.lib.buffer.api.BandInfo;
52
import org.gvsig.raster.lib.buffer.api.Buffer;
53
import org.gvsig.raster.lib.buffer.api.BufferDimensions;
54
import org.gvsig.raster.lib.buffer.api.FilterList;
55
import org.gvsig.raster.lib.buffer.api.NoData;
56
import org.gvsig.raster.lib.buffer.api.exceptions.BandException;
57
import org.gvsig.raster.lib.buffer.api.exceptions.BufferException;
58
import org.gvsig.raster.lib.buffer.api.statistics.Statistics;
59
import org.gvsig.tools.ToolsLocator;
60
import org.gvsig.tools.dispose.DisposableManager;
61
import org.gvsig.tools.dispose.DisposeUtils;
62
import org.gvsig.tools.exception.BaseException;
63
import org.gvsig.tools.locator.LocatorException;
64
import org.gvsig.tools.observer.Observable;
65
import org.gvsig.tools.observer.Observer;
66
import org.gvsig.tools.task.SimpleTaskStatus;
67
import org.gvsig.tools.visitor.VisitCanceledException;
68
import org.gvsig.tools.visitor.Visitor;
69
import org.gvsig.tools.visitor.impl.AbstractIndexedVisitable;
70

    
71
/**
72
 * Implements RasterSet
73
 *
74
 * @author dmartinezizquierdo
75
 *
76
 */
77
public class DefaultRasterSet extends AbstractIndexedVisitable implements RasterSet, Observer {
78

    
79
    protected static final Logger logger = LoggerFactory.getLogger(DefaultRasterSet.class);
80

    
81
    private DefaultRasterStore store;
82
    protected RasterQuery query;
83
    protected Buffer buffer;
84

    
85
    // to make it disposable
86
    private final Object lock = new Object();
87
    private boolean disposed = false;
88

    
89
    /**
90
     * Creates a RasterSet with the raster filtered by the query
91
     *
92
     * @param store
93
     * @param query
94
     * @throws DataException
95
     */
96
    public DefaultRasterSet(RasterStore store, RasterQuery query) throws DataException {
97
        if (ToolsLocator.getDisposableManager() != null) {
98
            ToolsLocator.getDisposableManager().bind(this);
99
        } else {
100
            logger.warn("Can't retrieve the disposable manager,");
101
        }
102

    
103
        DisposableManager disposableManager = ToolsLocator.getDisposableManager();
104
        disposableManager.bind(store);
105
        this.store = (DefaultRasterStore) store;
106
        this.query = query;
107
        RasterStoreProvider provider = this.store.getProvider();
108

    
109
        try {
110
            buffer = provider.createBuffer(query);
111
            // Filter buffer by raster query
112
            if (buffer!=null && query != null) {
113
                if (query.getClip() != null) {
114
                    Buffer clip = buffer.clip(query.getClip());
115
                    DisposeUtils.dispose(buffer);
116
                    buffer = clip;
117
                }
118
            }
119
        } catch (BufferException e) {
120
            throw new RasterSetInitializeException(e);
121
        }
122

    
123
        this.store.addObserver(this);
124
    }
125

    
126
    @Override
127
    public void filter(FilterList filterList) {
128
        if (this.buffer != null) {
129
            this.buffer.filter(filterList);
130
        }
131

    
132
    }
133

    
134
    @Override
135
    public int getBandCount() {
136
        if (this.buffer != null) {
137
            return this.buffer.getBandCount();
138
        }
139
        return 0;
140
    }
141

    
142
    @Override
143
    public Band[] getBands() {
144
        if (this.buffer != null) {
145
            return this.buffer.getBands();
146
        }
147
        return null;
148
    }
149

    
150
    @Override
151
    public int getColumns() {
152
        if (this.buffer != null) {
153
            return this.buffer.getColumns();
154
        }
155
        return 0;
156
    }
157

    
158
    @Override
159
    public int getRows() {
160
        if (this.buffer != null) {
161
            return this.buffer.getRows();
162
        }
163
        return 0;
164
    }
165

    
166
    @Override
167
    public Envelope getEnvelope() throws LocatorException, CreateEnvelopeException {
168
        if (this.buffer != null) {
169
            return this.buffer.getEnvelope();
170
        }
171
        return GeometryLocator.getGeometryManager().createEnvelope(SUBTYPES.GEOM2D);
172
    }
173

    
174
    @Override
175
    public IProjection getProjection() {
176
        if (this.buffer != null) {
177
            return this.buffer.getProjection();
178
        }
179
        return null; // /OJO
180
    }
181

    
182
    @Override
183
    public boolean isInside(int cellX, int cellY) {
184
        if (this.buffer != null) {
185
            return this.buffer.isInside(cellX, cellY);
186
        }
187
        return false;
188
    }
189

    
190
    @Override
191
    public boolean isInside(Point point) {
192
        if (this.buffer != null) {
193
            return this.buffer.isInside(point);
194
        }
195
        return false;
196
    }
197

    
198
    @Override
199
    public void addBand(Band band) {
200
        if (this.buffer != null) {
201
            this.buffer.addBand(band);
202
        }
203
    }
204

    
205
    @Override
206
    public void setBand(int pos, Band band) throws BandException {
207
        if (this.buffer != null) {
208
            this.buffer.setBand(pos, band);
209
        }
210
    }
211

    
212
    @Override
213
    public void removeBand(int pos) {
214
        if (this.buffer != null) {
215
            this.buffer.removeBand(pos);
216
        }
217
    }
218

    
219
    @Override
220
    public Band getBand(int pos) {
221
        if (this.buffer != null) {
222
            return this.buffer.getBand(pos);
223
        }
224
        return null;
225
    }
226

    
227
    @Override
228
    public BandByte getBandByte(int pos) {
229
        if (this.buffer != null) {
230
            return this.buffer.getBandByte(pos);
231
        }
232
        return null;
233
    }
234

    
235
    @Override
236
    public BandShort getBandShort(int pos) {
237
        if (this.buffer != null) {
238
            return this.buffer.getBandShort(pos);
239
        }
240
        return null;
241
    }
242

    
243
    @Override
244
    public BandInt getBandInt(int pos) {
245
        if (this.buffer != null) {
246
            return this.buffer.getBandInt(pos);
247
        }
248
        return null;
249
    }
250

    
251
    @Override
252
    public BandFloat getBandFloat(int pos) {
253
        if (this.buffer != null) {
254
            return this.buffer.getBandFloat(pos);
255
        }
256
        return null;
257
    }
258

    
259
    @Override
260
    public BandDouble getBandDouble(int pos) {
261
        if (this.buffer != null) {
262
            return this.buffer.getBandDouble(pos);
263
        }
264
        return null;
265
    }
266

    
267
    @Override
268
    public void switchBands(int[] positions) {
269
        if (this.buffer != null) {
270
            this.buffer.switchBands(positions);
271
        }
272
    }
273

    
274
    @Override
275
    public void switchBands(int pos1, int pos2) {
276
        if (this.buffer != null) {
277
            this.buffer.switchBands(pos1, pos2);
278
        }
279
    }
280

    
281
    @Override
282
    public Buffer createInterpolated(int rows, int columns, int interpolationMode, SimpleTaskStatus status)
283
        throws LocatorException, BufferException {
284
        if (this.buffer != null) {
285
            return this.buffer.createInterpolated(rows, columns, interpolationMode, status);
286
        }
287
        return null;
288
    }
289

    
290
    @Override
291
    public Buffer convert(ICoordTrans ct, SimpleTaskStatus status) throws BufferException {
292
        if (this.buffer != null) {
293
            return this.buffer.convert(ct, status);
294
        }
295
        return null;
296
    }
297

    
298
    @Override
299
    public int[] getBandTypes() {
300
        if (this.buffer != null) {
301
            return this.buffer.getBandTypes();
302
        }
303
        return null;
304
    }
305

    
306
    @Override
307
    public NoData[] getBandNoData() {
308
        if (this.buffer != null) {
309
            return this.buffer.getBandNoData();
310
        }
311
        return null;
312

    
313
    }
314

    
315
    @Override
316
    public Buffer clip(Envelope envelope) throws BufferException {
317
        if (this.buffer != null) {
318
            return this.buffer.clip(envelope);
319
        }
320
        return null;
321
    }
322

    
323
    @Override
324
    public double getPixelSizeX() {
325
        if (this.buffer != null) {
326
            return this.buffer.getPixelSizeX();
327
        }
328
        return Double.NaN;
329
    }
330

    
331
    @Override
332
    public double getPixelSizeY() {
333
        if (this.buffer != null) {
334
            return this.buffer.getPixelSizeY();
335
        }
336
        return Double.NaN;
337
    }
338

    
339
    @Override
340
    public Statistics getStatistics(SimpleTaskStatus status) {
341
        if (this.buffer != null) {
342
            return this.buffer.getStatistics(status);
343
        }
344
        return null;
345
    }
346

    
347
    @Override
348
    public void addObserver(Observer o) {
349
        if (this.buffer != null) {
350
            this.buffer.addObserver(o);
351
        }
352
    }
353

    
354
    @Override
355
    public void deleteObserver(Observer o) {
356
        if (this.buffer != null) {
357
            this.buffer.deleteObserver(o);
358
        }
359
    }
360

    
361
    @Override
362
    public void deleteObservers() {
363
        if (this.buffer != null) {
364
            this.buffer.deleteObservers();
365
        }
366
    }
367

    
368
    @Override
369
    public Iterator<Band> iterator() {
370
        if (this.buffer != null) {
371
            return this.buffer.iterator();
372
        }
373
        return null;
374
    }
375

    
376
    @Override
377
    public boolean isFromStore(DataStore store) {
378
        return this.store.equals(store);
379
    }
380

    
381
    @Override
382
    public void update(Observable observable, Object notification) {
383
        // Do nothing
384
    }
385

    
386
    @Override
387
    protected void doAccept(Visitor visitor, long firstValueIndex) throws VisitCanceledException, BaseException {
388
        if (this.buffer != null) {
389

    
390
            DefaultRasterKernel kernel = new DefaultRasterKernel(this.buffer);
391
            for (int row = 0; row < this.buffer.getRows(); row++) {
392
                for (int column = 0; column < this.buffer.getColumns(); column++) {
393
                    kernel.set(row, column);
394
                    visitor.visit(kernel);
395
                }
396
            }
397
        }
398
    }
399

    
400
    public final void dispose() {
401
        synchronized (lock) {
402
            // Check if we have already been disposed, and don't do it again
403
            if (!disposed) {
404
                if (ToolsLocator.getDisposableManager().release(this)) {
405
                    try {
406
                        doDispose();
407
                    } catch (BaseException ex) {
408
                        logger.error("Error performing dispose", ex);
409
                    }
410
                    disposed = true;
411
                }
412
            }
413
        }
414
    }
415

    
416
    /**
417
     * Internal implementation for the {@link #dispose()} method.
418
     *
419
     * @see #dispose()
420
     */
421
    public void doDispose() throws BaseException {
422
        DisposableManager disposableManager = ToolsLocator.getDisposableManager();
423
        DisposeUtils.dispose(store);
424
        store = null;
425
        DisposeUtils.dispose(buffer);
426
        buffer = null;
427
        query = null;
428
    }
429

    
430
    @Override
431
    protected void finalize() throws Throwable {
432
        super.finalize();
433
    }
434

    
435
    public BufferedImage getBufferedImage() {
436
        if (this.buffer != null) {
437
            return buffer.getBufferedImage();
438
        }
439
        return null;
440
    }
441

    
442
    @Override
443
    public boolean isEmpty() {
444
        return buffer==null;
445
    }
446

    
447
    @Override
448
    public BufferDimensions getDimensions() {
449
        return buffer.getDimensions();
450
    }
451

    
452
    @Override
453
    public BandInfo[] getBandsInfo() {
454
        if (this.buffer != null) {
455
            return this.buffer.getBandsInfo();
456
        }
457
        return null;
458
    }
459

    
460
}