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

History | View | Annotate | Download (7.61 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.operation.GeometryOperationException;
13
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
14
import org.gvsig.fmap.geom.primitive.Envelope;
15
import org.gvsig.fmap.geom.primitive.Point;
16
import org.gvsig.raster.lib.buffer.api.Band;
17
import org.gvsig.raster.lib.buffer.api.Band.BandByte;
18
import org.gvsig.raster.lib.buffer.api.Band.BandDouble;
19
import org.gvsig.raster.lib.buffer.api.Band.BandFloat;
20
import org.gvsig.raster.lib.buffer.api.Band.BandInt;
21
import org.gvsig.raster.lib.buffer.api.Band.BandShort;
22
import org.gvsig.raster.lib.buffer.api.BandPageManager;
23
import org.gvsig.raster.lib.buffer.api.Buffer;
24
import org.gvsig.raster.lib.buffer.api.BufferManager;
25
import org.gvsig.raster.lib.buffer.api.FilterList;
26
import org.gvsig.raster.lib.buffer.api.RasterLocator;
27
import org.gvsig.raster.lib.buffer.api.exceptions.BandException;
28
import org.gvsig.raster.lib.buffer.api.statistics.Histogram;
29
import org.gvsig.raster.lib.buffer.api.statistics.Statistics;
30
import org.gvsig.raster.lib.buffer.impl.exceptions.CreateBufferException;
31
import org.gvsig.tools.task.SimpleTaskStatus;
32

    
33

    
34
/**
35
 * @author fdiaz
36
 *
37
 */
38
public class DefaultBuffer implements Buffer {
39

    
40
    protected static final Logger logger = LoggerFactory.getLogger(DefaultBuffer.class);
41

    
42

    
43
    private List<Band> bands;
44
    private int rows;
45
    private int columns;
46
    private FilterList filters;
47
    private IProjection projection;
48
    private BandPageManager bandPageManager;
49

    
50

    
51

    
52

    
53
    /**
54
     * @author fdiaz
55
     *
56
     */
57
    public class BandsIterator implements Iterator<Band> {
58

    
59
        private Band band;
60
        private int current;
61

    
62
        /**
63
         *
64
         */
65
        public BandsIterator() {
66
            this.current = 0;
67
            if( getBandCount()>0 ) {
68
                this.band = (Band) getBand(0);
69
            } else {
70
                this.band = null;
71
            }
72
        }
73

    
74
        @Override
75
        public boolean hasNext() {
76
            return this.current < getBandCount() ;
77
        }
78

    
79
        @Override
80
        public Band next() {
81
            this.current++;
82
            return this.band;
83
        }
84

    
85
        @Override
86
        public void remove() {
87
            throw new UnsupportedOperationException("Not supported yet.");
88
        }
89
    }
90

    
91

    
92
    /**
93
     * @param rows
94
     * @param columns
95
     * @param bandDataTypes
96
     * @param projection
97
     * @param bandPageManager
98
     * @throws CreateBufferException
99
     */
100
    public DefaultBuffer(int rows, int columns, int[] bandDataTypes, IProjection projection, BandPageManager bandPageManager) throws CreateBufferException {
101
        this.rows = rows;
102
        this.columns = columns;
103
        this.projection = projection;
104
        this.bandPageManager = bandPageManager;
105
        try {
106
            createBands(bandDataTypes);
107
        } catch (BandException e) {
108
            throw new CreateBufferException(e);
109
        }
110
    }
111

    
112
    private void createBands(int[] bandDataTypes) throws BandException {
113

    
114
        BufferManager bufferManager = RasterLocator.getBufferManager();
115

    
116
        for (int i = 0; i < bandDataTypes.length; i++) {
117
            bands.add(bufferManager.createBand(bandDataTypes[i], this.rows, this.columns, null, this.bandPageManager));
118
        }
119

    
120
    }
121

    
122
    /**
123
     * @param rows
124
     * @param columns
125
     * @param projection
126
     * @throws CreateBufferException
127
     */
128
    public DefaultBuffer(int rows, int columns, int[] bandDataTypes, IProjection projection) throws CreateBufferException {
129
        this(rows, columns, bandDataTypes, projection, null);
130
    }
131

    
132
    @Override
133
    public Histogram getHistogram(SimpleTaskStatus status) {
134
        // TODO Auto-generated method stub
135
        return null;
136
    }
137

    
138
    @Override
139
    public Statistics getStatistics(SimpleTaskStatus status) {
140
        // TODO Auto-generated method stub
141
        return null;
142
    }
143

    
144
    @Override
145
    public Iterator<Band> iterator() {
146
        return new BandsIterator();
147
    }
148

    
149
    @Override
150
    public void filter(FilterList filterList) {
151
        this.filters = filterList;
152
    }
153

    
154
    @Override
155
    public int getBandCount() {
156
        return bands.size();
157
    }
158

    
159
    @Override
160
    public Band[] getBands() {
161
        return (Band[]) bands.toArray();
162
    }
163

    
164
    @Override
165
    public int getColumns() {
166
        return this.columns;
167
    }
168

    
169
    @Override
170
    public int getRows() {
171
        return this.rows;
172
    }
173

    
174
    @Override
175
    public Envelope getEnvelope() {
176
        // TODO Auto-generated method stub
177
        //FIXME: ?De d?nde sacamos este envelope?
178
        return null;
179
    }
180

    
181
    @Override
182
    public IProjection getProjection() {
183
        return this.projection;
184
    }
185

    
186
    @Override
187
    public boolean isInside(int cellX, int cellY) {
188
        return (cellX>=0 && cellX < this.columns && cellY >= 0 && cellY < this.rows);
189
    }
190

    
191
    @Override
192
    public boolean isInside(Point point) {
193
        try {
194
            return getEnvelope().getGeometry().contains(point);
195
        } catch (GeometryOperationNotSupportedException | GeometryOperationException e) {
196
            logger.warn("It could not determine if the point is on the envelope", e);
197
            return false;
198
        }
199
    }
200

    
201
    @Override
202
    public void addBand(Band band) {
203
        this.bands.add(band);
204
    }
205

    
206
    @Override
207
    public void setBand(int pos, Band band) throws BandException {
208
        this.bands.get(pos).copyFrom(band);
209
    }
210

    
211
    @Override
212
    public void removeBand(int pos) {
213
        this.bands.remove(pos);
214
    }
215

    
216
    @Override
217
    public Band getBand(int pos) {
218
        return this.bands.get(pos);
219
    }
220

    
221
    @Override
222
    public BandByte getBandByte(int pos) {
223
        return (BandByte) this.bands.get(pos);
224
    }
225

    
226
    @Override
227
    public BandShort getBandShort(int pos) {
228
        return (BandShort) this.bands.get(pos);
229
    }
230

    
231
    @Override
232
    public BandInt getBandInt(int pos) {
233
        return (BandInt) this.bands.get(pos);
234

    
235
    }
236

    
237
    @Override
238
    public BandFloat getBandFloat(int pos) {
239
        return (BandFloat) this.bands.get(pos);
240

    
241
    }
242

    
243
    @Override
244
    public BandDouble getBandDouble(int pos) {
245
        return (BandDouble) this.bands.get(pos);
246

    
247
    }
248

    
249
    @Override
250
    public void switchBands(int[] positions) {
251
        List<Integer> visited = new ArrayList<Integer>();
252
        if(positions.length != this.getBandCount()){
253
            return;
254
        }
255
        for(int i = 0; i < positions.length; i++) {
256
            if(positions[i] >=  positions.length || positions[i] < 0) {
257
                return;
258
            }
259
            Integer position = new Integer(positions[i]);
260
            if(visited.contains(position)){
261
                return;
262
            }
263
            visited.add(position);
264
        }
265

    
266
        List<Band> auxBands = new ArrayList<Band>(bands.size());
267
        for(int i = 0; i < positions.length; i++) {
268
            auxBands.set(i, bands.get(positions[i]));
269
        }
270
        bands = auxBands;
271
    }
272

    
273
    @Override
274
    public void switchBands(int pos1, int pos2) {
275
        Band auxBand = bands.get(pos1);
276
        bands.set(pos1, bands.get(pos2));
277
        bands.set(pos2, auxBand);
278
    }
279

    
280
    @Override
281
    public Buffer createInterpolated(int rows, int columns, int interpolationMode, SimpleTaskStatus status) {
282
        // TODO Auto-generated method stub
283
        return null;
284
    }
285

    
286
    @Override
287
    public Buffer convert(ICoordTrans ct, SimpleTaskStatus status) {
288
        // TODO Auto-generated method stub
289
        return null;
290
    }
291

    
292
    @Override
293
    public boolean isPaginated() {
294
        return bandPageManager!=null;
295
    }
296

    
297
}