Statistics
| Revision:

gvsig-geoprocess / org.gvsig.geoprocess / trunk / org.gvsig.geoprocess / org.gvsig.geoprocess.app / org.gvsig.geoprocess.app.mainplugin / src / main / java / org / gvsig / geoprocess / core / gvRasterLayerRead.java @ 189

History | View | Annotate | Download (4.54 KB)

1
package org.gvsig.geoprocess.core;
2

    
3
import java.awt.geom.Rectangle2D;
4

    
5
import es.unex.sextante.core.AnalysisExtent;
6
import es.unex.sextante.core.Sextante;
7
import es.unex.sextante.dataObjects.AbstractRasterLayer;
8
import es.unex.sextante.outputs.IOutputChannel;
9

    
10
import org.gvsig.fmap.dal.coverage.dataset.Buffer;
11
import org.gvsig.fmap.dal.coverage.datastruct.NoData;
12
import org.gvsig.fmap.dal.coverage.exception.GridException;
13
import org.gvsig.fmap.dal.coverage.grid.Grid;
14
import org.gvsig.raster.fmap.layers.FLyrRaster;
15

    
16
/**
17
 * A wrapper for a gvSIG Raster layer. Allows only reading, but not writing to
18
 * it
19
 * 
20
 * @author volaya, nacho brodin (nachobrodin@gmail.com)
21
 * 
22
 */
23
public class gvRasterLayerRead extends AbstractRasterLayer {
24

    
25
    private Grid m_LayerGrid = null;
26
    private FLyrRaster m_Layer;
27
    private Buffer m_Buffer;
28
    private int m_iDataType;
29
    private double m_dNoDataValue;
30

    
31
    public void create(final FLyrRaster obj) {
32

    
33
        m_Layer = obj;
34
    }
35

    
36
    public int getDataType() {
37

    
38
        return m_Layer.getDataStore().getDataType()[0];
39

    
40
    }
41

    
42
    public void setCellValue(final int x, final int y, final int iBand,
43
        final double dValue) {
44

    
45
    }
46

    
47
    public void setNoDataValue(final double dNoDataValue) {
48

    
49
        m_Layer.getNoDataValue().setValue(Double.valueOf(dNoDataValue));
50

    
51
    }
52

    
53
    @Override
54
    public void setNoData(final int x, final int y) {
55

    
56
    }
57

    
58
    public double getNoDataValue() {
59
        return m_dNoDataValue;
60
    }
61

    
62
    public double getCellValueInLayerCoords(final int x, final int y,
63
        final int iBand) {
64

    
65
        if (m_Buffer.isInside(x, y)) {
66
            switch (m_iDataType) {
67
            case Buffer.TYPE_BYTE:
68
                return m_Buffer.getElemByte(y, x, iBand);
69
            case Buffer.TYPE_SHORT:
70
                return m_Buffer.getElemShort(y, x, iBand);
71
            case Buffer.TYPE_INT:
72
                return m_Buffer.getElemInt(y, x, iBand);
73
            case Buffer.TYPE_FLOAT:
74
                return m_Buffer.getElemFloat(y, x, iBand);
75
            case Buffer.TYPE_DOUBLE:
76
            default:
77
                return m_Buffer.getElemDouble(y, x, iBand);
78
            }
79
        } else {
80
            return m_dNoDataValue;
81
        }
82

    
83
    }
84

    
85
    public int getBandsCount() {
86

    
87
        return m_Layer.getDataStore().getBandCount();
88

    
89
    }
90

    
91
    public String getName() {
92

    
93
        return m_Layer.getName();
94

    
95
    }
96

    
97
    public void postProcess() {
98

    
99
    }
100

    
101
    public void open() {
102

    
103
        try {
104
            m_LayerGrid = m_Layer.getReadOnlyFullGrid(false);
105
            m_Buffer = m_LayerGrid.getRasterBuf();
106
            m_iDataType = m_LayerGrid.getDataType();
107
            // TODO: Hablar con NACHO
108
            NoData nodata = m_LayerGrid.getNoDataValue();
109
            m_dNoDataValue =
110
                nodata != null ? (nodata.getValue() != null ? nodata.getValue()
111
                    .doubleValue() : 0.0d) : 0.0d;
112
        } catch (final GridException e) {
113
            Sextante.addErrorToLog(e);
114
        } catch (final InterruptedException e) {
115
            Sextante.addErrorToLog(e);
116
        }
117

    
118
    }
119

    
120
    public void close() {
121

    
122
        // if (m_LayerGrid != null) {
123
        // m_LayerGrid.free();
124
        // }
125
        m_LayerGrid = null;
126

    
127
    }
128

    
129
    public Rectangle2D getFullExtent() {
130

    
131
        return m_Layer.getFullRasterExtent().toRectangle2D();
132

    
133
    }
134

    
135
    public AnalysisExtent getLayerGridExtent() {
136

    
137
        try {
138
            final AnalysisExtent extent = new AnalysisExtent();
139
            extent.setCellSize(m_Layer.getDataStore().getCellSize());
140
            extent.setXRange(m_Layer.getMinX(), m_Layer.getMaxX(), true);
141
            extent.setYRange(m_Layer.getMinY(), m_Layer.getMaxY(), true);
142
            return extent;
143
        } catch (final Exception e) {
144
            return null;
145
        }
146
    }
147

    
148
    public double getLayerCellSize() {
149

    
150
        return m_Layer.getDataStore().getCellSize();
151

    
152
    }
153

    
154
    public Object getCRS() {
155

    
156
        return m_Layer.getProjection();
157

    
158
    }
159

    
160
    public void setName(final String name) {
161

    
162
        m_Layer.setName(name);
163

    
164
    }
165

    
166
    public void free() {
167
        m_Layer.dispose();
168
        m_Layer = null;
169
        m_LayerGrid = null;
170
    }
171

    
172
    public Object getBaseDataObject() {
173
        return m_Layer;
174
    }
175

    
176
    public IOutputChannel getOutputChannel() {
177
        return new IOutputChannel() {
178

    
179
            public String getAsCommandLineParameter() {
180
                return m_Layer.getName();
181
            }
182
        };
183
    }
184

    
185
}