Statistics
| Revision:

gvsig-raster / org.gvsig.raster / branches / org.gvsig.raster.2.4 / org.gvsig.raster / org.gvsig.raster.lib / org.gvsig.raster.lib.buffer / org.gvsig.raster.lib.buffer.impl / src / main / java / org / gvsig / raster / lib / buffer / impl / DefaultBuffer.java @ 5518

History | View | Annotate | Download (9.87 KB)

1
package org.gvsig.raster.lib.buffer.impl;
2

    
3
import java.util.ArrayList;
4
import java.util.Iterator;
5
import java.util.List;
6

    
7
import org.cresques.cts.ICoordTrans;
8
import org.cresques.cts.IProjection;
9
import org.slf4j.Logger;
10
import org.slf4j.LoggerFactory;
11

    
12
import org.gvsig.fmap.geom.Geometry;
13
import org.gvsig.fmap.geom.GeometryLocator;
14
import org.gvsig.fmap.geom.operation.GeometryOperationException;
15
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
16
import org.gvsig.fmap.geom.primitive.Envelope;
17
import org.gvsig.fmap.geom.primitive.Point;
18
import org.gvsig.raster.lib.buffer.api.Band;
19
import org.gvsig.raster.lib.buffer.api.Band.BandByte;
20
import org.gvsig.raster.lib.buffer.api.Band.BandDouble;
21
import org.gvsig.raster.lib.buffer.api.Band.BandFloat;
22
import org.gvsig.raster.lib.buffer.api.Band.BandInt;
23
import org.gvsig.raster.lib.buffer.api.Band.BandShort;
24
import org.gvsig.raster.lib.buffer.api.Buffer;
25
import org.gvsig.raster.lib.buffer.api.BufferLocator;
26
import org.gvsig.raster.lib.buffer.api.BufferManager;
27
import org.gvsig.raster.lib.buffer.api.FilterList;
28
import org.gvsig.raster.lib.buffer.api.NoData;
29
import org.gvsig.raster.lib.buffer.api.PageManager;
30
import org.gvsig.raster.lib.buffer.api.exceptions.BandException;
31
import org.gvsig.raster.lib.buffer.api.statistics.Statistics;
32
import org.gvsig.raster.lib.buffer.impl.exceptions.CreateBufferException;
33
import org.gvsig.raster.lib.buffer.impl.statistics.DefaultStatistics;
34
import org.gvsig.tools.task.SimpleTaskStatus;
35

    
36
/**
37
 * @author fdiaz
38
 *
39
 */
40
public class DefaultBuffer implements Buffer {
41

    
42
    protected static final Logger logger = LoggerFactory.getLogger(DefaultBuffer.class);
43

    
44
    private List<Band> bands;
45
    private int rows;
46
    private int columns;
47
    private FilterList filters;
48
    private Envelope envelope;
49
    private IProjection projection;
50
    private PageManager bandPageManager;
51
    private Statistics statistics;
52

    
53
    /**
54
     * Default constructor of buffer.
55
     *
56
     * @param rows
57
     *            Rows of buffer and buffer bands.
58
     * @param columns
59
     *            Columns of buffer bands.
60
     * @param bandDataTypes
61
     *            Type of buffer bands. The band types also indicates the number
62
     *            of buffer bands.
63
     * @param bandNoData
64
     *            Band noData. If there are bans without NoData, bands will have
65
     *            undefined NoData value.
66
     * @param projection
67
     *            Projection of buffer
68
     * @param envelope
69
     *            Envelope of buffer
70
     * @param pageManager
71
     *            Page manager to paginate this buffer. If page manager is null,
72
     *            buffer will be loaded in memory.
73
     * @throws CreateBufferException
74
     *             If there are some problems creating buffer.
75
     */
76
    public DefaultBuffer(int rows, int columns, int[] bandDataTypes, NoData[] bandNoData,
77
        IProjection projection, Envelope envelope, PageManager pageManager)
78
        throws CreateBufferException {
79
        this.bands = new ArrayList<Band>();
80
        this.rows = rows;
81
        this.columns = columns;
82
        this.projection = projection;
83
        this.envelope = envelope;
84
        try {
85
            if (envelope == null) {
86
                this.envelope =
87
                    GeometryLocator.getGeometryManager().createEnvelope(0, 0, columns, rows, Geometry.SUBTYPES.GEOM2D);
88
            }
89
            this.bandPageManager = pageManager;
90
            createBands(bandDataTypes, bandNoData);
91
        } catch (Exception e) {
92
            throw new CreateBufferException(e);
93
        }
94
    }
95

    
96
    private void createBands(int[] bandDataTypes, NoData[] bandNoData) throws BandException {
97

    
98
        BufferManager bufferManager = BufferLocator.getBufferManager();
99

    
100
        for (int i = 0; i < bandDataTypes.length; i++) {
101

    
102
            NoData noDataBand = null;
103
            if (bandNoData != null && i < bandNoData.length) {
104
                noDataBand = bandNoData[i];
105
            }
106

    
107
            bands.add(bufferManager.createBand(bandDataTypes[i], this.rows, this.columns,
108
                noDataBand, this.bandPageManager));
109
        }
110
    }
111

    
112
    @Override
113
    public Statistics getStatistics(SimpleTaskStatus status) {
114
        if (statistics == null) {
115
            statistics = new DefaultStatistics(bands);
116
        }
117
        if (!statistics.isCalculated()) {
118
            statistics.calculate(status); // scale ???
119
        }
120
        return statistics;
121
    }
122

    
123
    @Override
124
    public Iterator<Band> iterator() {
125
        return bands.iterator();
126
    }
127

    
128
    @Override
129
    public void filter(FilterList filterList) {
130
        this.filters = filterList;
131
    }
132

    
133
    @Override
134
    public int getBandCount() {
135
        return bands.size();
136
    }
137

    
138
    @Override
139
    public Band[] getBands() {
140
        Band[] arrayBands = new Band[bands.size()];
141
        return bands.toArray(arrayBands);
142
    }
143

    
144
    @Override
145
    public int getColumns() {
146
        return this.columns;
147
    }
148

    
149
    @Override
150
    public int getRows() {
151
        return this.rows;
152
    }
153

    
154
    @Override
155
    public Envelope getEnvelope() {
156
        return this.envelope;
157
    }
158

    
159
    @Override
160
    public IProjection getProjection() {
161
        return this.projection;
162
    }
163

    
164
    @Override
165
    public boolean isInside(int cellX, int cellY) {
166
        return (cellX >= 0 && cellX < this.columns && cellY >= 0 && cellY < this.rows);
167
    }
168

    
169
    @Override
170
    public boolean isInside(Point point) {
171

    
172
        if (getEnvelope() == null) {
173
            return false;
174
        }
175

    
176
        try {
177
            return getEnvelope().getGeometry().contains(point);
178
        } catch (GeometryOperationNotSupportedException | GeometryOperationException e) {
179
            logger.warn("It could not determine if the point is on the envelope", e);
180
            return false;
181
        }
182
    }
183

    
184
    @Override
185
    public void addBand(Band band) {
186

    
187
        if(band.getColumns() != this.getColumns() || band.getRows() != this.getRows()){
188
            throw new IllegalArgumentException(
189
                "Can not add band to buffer. Band must have the same rows and columns as buffer.");
190
        }
191

    
192
        this.bands.add(band);
193
    }
194

    
195
    @Override
196
    public void setBand(int pos, Band band) throws BandException {
197
        this.bands.get(pos).copyFrom(band);
198
    }
199

    
200
    @Override
201
    public void removeBand(int pos) {
202
        this.bands.remove(pos);
203
    }
204

    
205
    @Override
206
    public Band getBand(int pos) {
207
        return this.bands.get(pos);
208
    }
209

    
210
    @Override
211
    public BandByte getBandByte(int pos) {
212
        if (this.bands.get(pos).getDataType() != BufferManager.TYPE_BYTE) {
213
            throw new IllegalArgumentException(String.format("Band in position %1s is type of %2s",
214
                pos, this.bands.get(pos).getDataType()));
215
        }
216
        return (BandByte) this.bands.get(pos);
217
    }
218

    
219
    @Override
220
    public BandShort getBandShort(int pos) {
221
        if (this.bands.get(pos).getDataType() != BufferManager.TYPE_SHORT) {
222
            throw new IllegalArgumentException(String.format("Band in position %1s is type of %2s",
223
                pos, this.bands.get(pos).getDataType()));
224
        }
225
        return (BandShort) this.bands.get(pos);
226
    }
227

    
228
    @Override
229
    public BandInt getBandInt(int pos) {
230
        if (this.bands.get(pos).getDataType() != BufferManager.TYPE_INT) {
231
            throw new IllegalArgumentException(String.format("Band in position %1s is type of %2s",
232
                pos, this.bands.get(pos).getDataType()));
233
        }
234
        return (BandInt) this.bands.get(pos);
235

    
236
    }
237

    
238
    @Override
239
    public BandFloat getBandFloat(int pos) {
240
        if (this.bands.get(pos).getDataType() != BufferManager.TYPE_FLOAT) {
241
            throw new IllegalArgumentException(String.format("Band in position %1s is type of %2s",
242
                pos, this.bands.get(pos).getDataType()));
243
        }
244
        return (BandFloat) this.bands.get(pos);
245

    
246
    }
247

    
248
    @Override
249
    public BandDouble getBandDouble(int pos) {
250
        if (this.bands.get(pos).getDataType() != BufferManager.TYPE_DOUBLE) {
251
            throw new IllegalArgumentException(String.format("Band in position %1s is type of %2s",
252
                pos, this.bands.get(pos).getDataType()));
253
        }
254
        return (BandDouble) this.bands.get(pos);
255

    
256
    }
257

    
258
    @Override
259
    public void switchBands(int[] positions) {
260
        List<Integer> visited = new ArrayList<Integer>();
261
        if (positions.length != this.getBandCount()) {
262
            throw new IllegalArgumentException(
263
                "Position array length has to be the same as band count");
264
        }
265
        for (int i = 0; i < positions.length; i++) {
266
            Integer position = new Integer(positions[i]);
267
            if (visited.contains(position)) {
268
                throw new IllegalArgumentException(
269
                    String.format(
270
                        "Position array can not have duplicated bands. Duplicated value: %1s",
271
                        position));
272
            }
273
            visited.add(position);
274
        }
275

    
276
        List<Band> auxBands = new ArrayList<Band>(bands.size());
277
        for(int i=0; i < visited.size(); i++){
278
            auxBands.add(bands.get(visited.get(i)));
279
        }
280
        bands = auxBands;
281
    }
282

    
283
    @Override
284
    public void switchBands(int pos1, int pos2) {
285
        Band auxBand = bands.get(pos1);
286
        bands.set(pos1, bands.get(pos2));
287
        bands.set(pos2, auxBand);
288
    }
289

    
290
    @Override
291
    public Buffer createInterpolated(int rows, int columns, int interpolationMode,
292
        SimpleTaskStatus status) {
293
        // TODO Auto-generated method stub
294
        return null;
295
    }
296

    
297
    @Override
298
    public Buffer convert(ICoordTrans ct, SimpleTaskStatus status) {
299

    
300
//        for (Band band : bands) {
301
//            band.convert(ct);
302
//
303
//        }
304
        // TODO Auto-generated method stub
305
        return null;
306
    }
307

    
308
    @Override
309
    public boolean isPaginated() {
310
        return bandPageManager != null;
311
    }
312

    
313
    @Override
314
    public boolean isReadOnly() {
315
        if(this.bandPageManager==null){
316
            return false;
317
        }
318
        return !this.bandPageManager.isSupportedSave();
319
    }
320
}