Statistics
| Revision:

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

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.RasterQuery;
36
import org.gvsig.fmap.dal.raster.RasterSet;
37
import org.gvsig.fmap.dal.raster.RasterStore;
38
import org.gvsig.fmap.dal.raster.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.NoData;
55
import org.gvsig.raster.lib.buffer.api.exceptions.BandException;
56
import org.gvsig.raster.lib.buffer.api.exceptions.BufferException;
57
import org.gvsig.raster.lib.buffer.api.statistics.Statistics;
58
import org.gvsig.tools.ToolsLocator;
59
import org.gvsig.tools.dispose.DisposableManager;
60
import org.gvsig.tools.dispose.DisposeUtils;
61
import org.gvsig.tools.exception.BaseException;
62
import org.gvsig.tools.locator.LocatorException;
63
import org.gvsig.tools.observer.Observable;
64
import org.gvsig.tools.observer.Observer;
65
import org.gvsig.tools.task.SimpleTaskStatus;
66
import org.gvsig.tools.visitor.VisitCanceledException;
67
import org.gvsig.tools.visitor.Visitor;
68
import org.gvsig.tools.visitor.impl.AbstractIndexedVisitable;
69

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

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

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

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

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

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

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

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

    
125
    @Override
126
    public int getBandCount() {
127
        if (this.buffer != null) {
128
            return this.buffer.getBandCount();
129
        }
130
        return 0;
131
    }
132

    
133
    @Override
134
    public Band[] getBands() {
135
        if (this.buffer != null) {
136
            return this.buffer.getBands();
137
        }
138
        return null;
139
    }
140

    
141
    @Override
142
    public int getColumns() {
143
        if (this.buffer != null) {
144
            return this.buffer.getColumns();
145
        }
146
        return 0;
147
    }
148

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

    
157
    @Override
158
    public boolean areAllBandsOfTheSameType() {
159
        if (this.buffer != null) {
160
            return this.buffer.areAllBandsOfTheSameType();
161
        }
162
        return true;
163
    }
164

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
312
    }
313

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

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

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

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

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

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

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

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

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

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

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

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

    
399
    @Override
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
     * @throws org.gvsig.tools.exception.BaseException
420
     * @see #dispose()
421
     */
422
    public void doDispose() throws BaseException {
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
    @Override
436
    public BufferedImage getBufferedImage() {
437
        if (this.buffer != null) {
438
            return buffer.getBufferedImage();
439
        }
440
        return null;
441
    }
442

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

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

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

    
461
}