Statistics
| Revision:

gvsig-geoprocess / org.gvsig.geoprocess / trunk / org.gvsig.geoprocess / org.gvsig.geoprocess.lib / org.gvsig.geoprocess.lib.sextante / src / main / java / org / gvsig / geoprocess / lib / sextante / dataObjects / FLyrRasterReadOnlyIRasterLayer.java @ 382

History | View | Annotate | Download (7.6 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2012 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.geoprocess.lib.sextante.dataObjects;
25

    
26
import java.awt.geom.Rectangle2D;
27

    
28
import org.gvsig.fmap.dal.coverage.RasterLocator;
29
import org.gvsig.fmap.dal.coverage.RasterManager;
30
import org.gvsig.fmap.dal.coverage.dataset.Buffer;
31
import org.gvsig.fmap.dal.coverage.datastruct.Extent;
32
import org.gvsig.fmap.dal.coverage.datastruct.NoData;
33
import org.gvsig.fmap.dal.coverage.exception.GridException;
34
import org.gvsig.fmap.dal.coverage.exception.InvalidSetViewException;
35
import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException;
36
import org.gvsig.fmap.dal.coverage.exception.RasterDriverException;
37
import org.gvsig.fmap.dal.coverage.grid.Grid;
38
import org.gvsig.fmap.dal.coverage.store.RasterDataStore;
39
import org.gvsig.fmap.dal.coverage.store.RasterQuery;
40
import org.gvsig.raster.fmap.layers.FLyrRaster;
41

    
42
import es.unex.sextante.core.AnalysisExtent;
43
import es.unex.sextante.core.Sextante;
44
import es.unex.sextante.dataObjects.AbstractRasterLayer;
45
import es.unex.sextante.outputs.IOutputChannel;
46

    
47
/**
48
 * A wrapper for a gvSIG Raster layer. Allows only reading, but not writing to
49
 * it
50
 * 
51
 * @author volaya, nacho brodin (nachobrodin@gmail.com)
52
 * 
53
 */
54
public class FLyrRasterReadOnlyIRasterLayer extends AbstractRasterLayer {
55

    
56
    private Grid           m_LayerGrid    = null;
57
    private FLyrRaster     m_Layer        = null;
58
    private Buffer         m_Buffer       = null;
59
    private int            m_iDataType    = 0;
60
    private double         m_dNoDataValue = 0;
61
    private int            xTranslate     = 0;
62
    private int            yTranslate     = 0;
63
    private AnalysisExtent layerExtent    = null;
64

    
65
    public void create(final FLyrRaster obj) {
66
        m_Layer = obj;
67
    }
68

    
69
    public int getDataType() {
70
        return m_Layer.getDataStore().getDataType()[0];
71
    }
72

    
73
    public void setCellValue(final int x, final int y, final int iBand,
74
        final double dValue) {
75
    }
76

    
77
    public void setNoDataValue(final double dNoDataValue) {
78
        m_Layer.getNoDataValue().setValue(Double.valueOf(dNoDataValue));
79
    }
80

    
81
    @Override
82
    public void setNoData(final int x, final int y) {
83

    
84
    }
85

    
86
    public double getNoDataValue() {
87
        return m_dNoDataValue;
88
    }
89

    
90
    public double getCellValueInLayerCoords(final int x, final int y,
91
        final int iBand) {
92
            int newx = x - xTranslate;
93
            int newy = y - yTranslate;
94
        if (m_Buffer.isInside(newx, newy)) {
95
            switch (m_iDataType) {
96
            case Buffer.TYPE_BYTE:
97
                return m_Buffer.getElemByte(newy, newx, iBand);
98
            case Buffer.TYPE_SHORT:
99
                return m_Buffer.getElemShort(newy, newx, iBand);
100
            case Buffer.TYPE_INT:
101
                return m_Buffer.getElemInt(newy, newx, iBand);
102
            case Buffer.TYPE_FLOAT:
103
                return m_Buffer.getElemFloat(newy, newx, iBand);
104
            case Buffer.TYPE_DOUBLE:
105
            default:
106
                return m_Buffer.getElemDouble(newy, newx, iBand);
107
            }
108
        } else {
109
            return m_dNoDataValue;
110
        }
111

    
112
    }
113

    
114
    public int getBandsCount() {
115
        return m_Layer.getDataStore().getBandCount();
116
    }
117

    
118
    public String getName() {
119
        return m_Layer.getName();
120
    }
121

    
122
    public void postProcess() {
123

    
124
    }
125

    
126
    public void open() {
127
        try {
128
            m_LayerGrid = m_Layer.getReadOnlyFullGrid(false);
129
            m_Buffer = m_LayerGrid.getRasterBuf();
130
            m_iDataType = m_LayerGrid.getDataType();
131
            NoData nodata = m_LayerGrid.getNoDataValue();
132
            m_dNoDataValue = nodata != null ? 
133
                            (nodata.getValue() != null ? nodata.getValue().doubleValue() : 0.0d) : 
134
                            0.0d;
135
        } catch (GridException e) {
136
            Sextante.addErrorToLog(e);
137
        } catch (InterruptedException e) {
138
            Sextante.addErrorToLog(e);
139
        }
140
    }
141

    
142
    public void close() {
143
        m_LayerGrid = null;
144
    }
145

    
146
    public Rectangle2D getFullExtent() {
147
        return m_Layer.getFullRasterExtent().toRectangle2D();
148
    }
149

    
150
    public AnalysisExtent getLayerGridExtent() {
151
            if(layerExtent == null) {
152
                    try {
153
                            layerExtent = new AnalysisExtent();
154
                            layerExtent.setCellSize(m_Layer.getDataStore().getCellSize());
155
                            layerExtent.setXRange(m_Layer.getMinX(), m_Layer.getMaxX(), true);
156
                            layerExtent.setYRange(m_Layer.getMinY(), m_Layer.getMaxY(), true);
157
                    } catch (final Exception e) {
158
                            return null;
159
                    }
160
            }
161
            return layerExtent;
162
    }
163
    
164
    public void setWindowExtent(final AnalysisExtent extent) {
165
            super.setWindowExtent(extent);
166

    
167
            RasterManager rManager = RasterLocator.getManager();
168
        RasterDataStore store = m_Layer.getDataStore();
169
        RasterQuery query = rManager.createQuery();
170
        query.setReadOnly(true);
171
        int[] bands = new int[store.getBandCount()];
172
        for (int i = 0; i < store.getBandCount(); i++) {
173
                        bands[i] = i;
174
                }
175
        query.setDrawableBands(bands);
176
        Extent ext = RasterLocator.getManager().getDataStructFactory().createExtent(
177
                        extent.getXMin(), 
178
                        extent.getYMax(), 
179
                        extent.getXMax(), 
180
                        extent.getYMin());
181
        query.setAreaOfInterest(ext, extent.getNX(), extent.getNY());
182
        Buffer entireBuf = m_Buffer;
183
        try {
184
                        m_Buffer = store.query(query);
185
                        //Calculamos la traslaci?n respecto a la ventana recortada pq Sextante pedir? 
186
                        //p?xeles en relaci?n a la imagen completa
187
                        double distx = extent.getXMin() - m_Layer.getMinX();
188
                        double disty = m_Layer.getMaxY() - extent.getYMax();
189
                        xTranslate = (int)Math.round((distx * extent.getNX()) / (extent.getXMax() - extent.getXMin()));
190
                        yTranslate = (int)Math.round((disty * extent.getNY()) / (extent.getYMax() - extent.getYMin()));
191
                } catch (RasterDriverException e) {
192
                        m_Buffer = entireBuf;
193
                } catch (ProcessInterruptedException e) {
194
                } catch (InvalidSetViewException e) {
195
                        m_Buffer = entireBuf;
196
                }
197
     }
198

    
199
    public double getLayerCellSize() {
200
        return m_Layer.getDataStore().getCellSize();
201
    }
202

    
203
    public Object getCRS() {
204
        return m_Layer.getProjection();
205
    }
206

    
207
    public void setName(final String name) {
208
        m_Layer.setName(name);
209
    }
210

    
211
    public void free() {
212
        m_Layer.dispose();
213
        m_Layer = null;
214
        m_LayerGrid = null;
215
    }
216

    
217
    public Object getBaseDataObject() {
218
        return m_Layer;
219
    }
220

    
221
    public IOutputChannel getOutputChannel() {
222
        return new IOutputChannel() {
223
            public String getAsCommandLineParameter() {
224
                return m_Layer.getName();
225
            }
226
        };
227
    }
228

    
229
}