Statistics
| Revision:

gvsig-geoprocess / org.gvsig.geoprocess / branches / refactor-2018 / org.gvsig.geoprocess / org.gvsig.geoprocess.lib / org.gvsig.geoprocess.lib.sextante / src / main / java / org / gvsig / geoprocess / lib / sextante / dataObjects / RasterDriver.java @ 1055

History | View | Annotate | Download (8.67 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.image.DataBuffer;
27
import java.io.File;
28

    
29
import es.unex.sextante.core.AnalysisExtent;
30
import es.unex.sextante.core.Sextante;
31

    
32
import org.cresques.cts.IProjection;
33

    
34
import org.gvsig.andami.Utilities;
35
import org.gvsig.fmap.dal.DALLocator;
36
import org.gvsig.fmap.dal.DataManager;
37
import org.gvsig.fmap.dal.DataServerExplorerParameters;
38
import org.gvsig.fmap.dal.raster.api.NewRasterStoreParameters;
39
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer;
40
import org.gvsig.raster.lib.buffer.api.Buffer;
41
import org.gvsig.raster.lib.buffer.api.BufferLocator;
42
import org.gvsig.raster.lib.buffer.api.BufferManager;
43
import org.gvsig.raster.lib.buffer.api.NoData;
44
import org.gvsig.raster.lib.buffer.api.exceptions.BufferException;
45

    
46

    
47
public class RasterDriver {
48

    
49
    public static final int RASTER_DATA_TYPE_FLOAT = DataBuffer.TYPE_FLOAT;
50
    public static final int RASTER_DATA_TYPE_DOUBLE = DataBuffer.TYPE_DOUBLE;
51
    public static final int RASTER_DATA_TYPE_INT = DataBuffer.TYPE_INT;
52
    public static final int RASTER_DATA_TYPE_SHORT = DataBuffer.TYPE_SHORT;
53
    public static final int RASTER_DATA_TYPE_BYTE = DataBuffer.TYPE_BYTE;
54

    
55
    private final AnalysisExtent m_GridExtent;
56
    private Buffer buf = null;
57
    private String name = null;
58

    
59
    public RasterDriver(final AnalysisExtent ae, final int iDataType,
60
        final int iNumBands) throws BufferException {
61

    
62
        super();
63
        IProjection projection = null; //TODO check create bufefr without projection
64
        int[] listDataType; //TODO check how listDataType is made.
65
        listDataType = new int[1];
66
        listDataType[0] = (iDataType);
67
        buf = BufferLocator.getBufferManager().createBuffer(ae.getNX(), ae.getNY(), listDataType, projection);
68
        m_GridExtent = ae;
69

    
70
    }
71

    
72
    public RasterDriver(final AnalysisExtent ae, final int iDataType) throws BufferException {
73
        this(ae, iDataType, 1);
74
    }
75

    
76
    public AnalysisExtent getGridExtent() {
77
        return m_GridExtent;
78
    }
79

    
80
    public Buffer getRasterBuf() {
81
        return this.buf;
82
    }
83

    
84
    public void reset() {
85
        free();
86
    }
87

    
88
    public void setCellValue(final int x, final int y, final double dValue) {
89
        setCellValue(x, y, 0, dValue);
90
    }
91

    
92
    public void setNoData(final int x, final int y) {
93
        setNoData(x, y, 0);
94
    }
95

    
96
    private void setNoData(final int x, final int y, final int iBand) {
97
        setCellValue(x, y, iBand, getNoDataValue());
98
    }
99

    
100
    public void setCellValue(final int x, final int y, final int iBand,
101
        final double dValue) {
102

    
103
        if (isInGrid(x, y) && (iBand < buf.getBandCount())) {
104
            switch (buf.getBand(iBand).getDataType()) {
105
            case BufferManager.TYPE_BYTE:
106
                buf.getBand(iBand).set(y,x, (byte) dValue);
107
                break;
108
            case BufferManager.TYPE_SHORT:
109
                buf.getBand(iBand).set(y, x,(short) dValue);
110
                break;
111
            case BufferManager.TYPE_INT:
112
                buf.getBand(iBand).set(y, x, (int) dValue);
113
                break;
114
            case BufferManager.TYPE_FLOAT:
115
                buf.getBand(iBand).set(y, x,(float) dValue);
116
                break;
117
            case BufferManager.TYPE_DOUBLE:
118
            default:
119
                buf.getBand(iBand).set(y, x, dValue);
120
                break;
121
            }
122
        }
123
    }
124

    
125
    public double getNoDataValue() {
126
        NoData nodata;
127
        nodata = buf.getBandNoData()[0];
128
        return nodata != null ? (nodata.getValue() != null ? nodata.getValue()
129
            .doubleValue() : 0.0d) : 0.0d;
130
    }
131

    
132
    public void setNoDataValue(final double dNoDataValue) {
133
        // TODO: hablar con NACHO
134
        for (int i = 0; i < buf.getBandCount(); i++) {
135
                if (buf.getBand(i).getNoData()== null) {
136
                    NoData nodata = BufferLocator.getBufferManager().createNoData(dNoDataValue, null);
137
                    buf.getBand(i).getNoData().setValue(nodata.getValue());
138
                } else {
139
                    buf.getBand(i).getNoData().setValue(dNoDataValue);
140
                }
141
        }
142
    }
143

    
144
    public double getCellValue(final int x, final int y) {
145
        return getCellValue(x, y, 0);
146
    }
147

    
148
    public double getCellValue(final int x, final int y, final int iBand) {
149

    
150
        if (isInGrid(x, y) && (iBand < buf.getBandCount())) {
151
            switch (buf.getBand(iBand).getDataType()) {
152
            case BufferManager.TYPE_BYTE:
153
                return buf.getBandByte(iBand).getValue(y, x);
154
            case BufferManager.TYPE_SHORT:
155
                return buf.getBandShort(iBand).getValue(y, x);
156
            case BufferManager.TYPE_INT:
157
                return buf.getBandInt(iBand).getValue(y,x);
158
            case BufferManager.TYPE_FLOAT:
159
                return buf.getBandFloat(iBand).getValue(y,x);
160
            case BufferManager.TYPE_DOUBLE:
161
                return buf.getBandDouble(iBand).getValue(y,x);
162
            default:
163
                return getNoDataValue();
164
            }
165
        } else {
166
            return getNoDataValue();
167
        }
168

    
169
    }
170

    
171
    public boolean isNoDataValue(final double dNoDataValue) {
172
        return (getNoDataValue() == dNoDataValue);
173
    }
174

    
175
    public boolean isInGrid(final int x, final int y) {
176

    
177
        if ((x < 0) || (y < 0)) {
178
            return false;
179
        }
180
        
181
        if ((x >= m_GridExtent.getNX()) || (y >= m_GridExtent.getNY())) {
182
            return false;
183
        }
184

    
185
        return true;
186

    
187
    }
188

    
189
    public double getCellSize() {
190

    
191
        return m_GridExtent.getCellSize();
192

    
193
    }
194

    
195
    public static String getFilename(String sRoot, final String sExtension) {
196

    
197
        String sFilename;
198
        int i = 1;
199

    
200
        sRoot = sRoot.toLowerCase();
201
        sRoot = sRoot.replaceAll(" ", "_");
202
        sRoot = sRoot.replaceAll("\\)", "");
203
        sRoot = sRoot.replaceAll("\\(", "_");
204
        sRoot = sRoot.replaceAll("\\[", "_");
205
        sRoot = sRoot.replaceAll("\\]", "");
206
        sRoot = sRoot.replaceAll("<", "_");
207
        sRoot = sRoot.replaceAll(">", "_");
208
        sRoot = sRoot.replaceAll("__", "_");
209

    
210
        while (true) {
211
            sFilename =
212
                Utilities.createTempDirectory() + File.separator + sRoot
213
                    + Integer.toString(i) + "." + sExtension;
214
            final File file = new File(sFilename);
215
            if (file.exists()) {
216
                i++;
217
            } else {
218
                return sFilename;
219
            }
220
        }
221

    
222
    }
223

    
224
    public boolean export(final String sFilename, final IProjection projection) {
225

    
226

    
227
        try {
228
            final DataManager dm = DALLocator.getDataManager();
229
            DataServerExplorerParameters serverParameters = dm.createServerExplorerParameters(FilesystemServerExplorer.NAME);
230
            FilesystemServerExplorer server = (FilesystemServerExplorer) dm.openServerExplorer(FilesystemServerExplorer.NAME, serverParameters);
231
            File file = new File(sFilename);
232
            NewRasterStoreParameters parameters = (NewRasterStoreParameters) server.getAddParameters(file);
233
            parameters.setDynValue("crs", projection);
234
            parameters.setBuffer(buf);
235
            server.add(parameters.getDataStoreName(), parameters, true);
236
            
237
            return true;
238
        } catch (Exception e) {
239
            Sextante.addErrorToLog(e);
240
        }
241
        return false;
242
    }
243

    
244
    public void setName(final String name) {
245
        this.name = name;
246
    }
247

    
248
    public String getName() {
249
        return this.name;
250
    }
251

    
252

    
253
    public void free() {
254
            if(buf != null) {
255
                    buf.dispose();
256
                    buf = null;
257
            }
258
    }
259

    
260
}