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

History | View | Annotate | Download (9.38 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.DataStore;
39
import org.gvsig.fmap.dal.raster.api.NewRasterStoreParameters;
40
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer;
41
import org.gvsig.fmap.geom.GeometryLocator;
42
import org.gvsig.fmap.geom.exception.CreateEnvelopeException;
43
import org.gvsig.fmap.geom.primitive.Envelope;
44
import org.gvsig.raster.lib.buffer.api.Buffer;
45
import org.gvsig.raster.lib.buffer.api.BufferLocator;
46
import org.gvsig.raster.lib.buffer.api.BufferManager;
47
import org.gvsig.raster.lib.buffer.api.NoData;
48
import org.gvsig.raster.lib.buffer.api.exceptions.BufferException;
49

    
50
public class RasterDriver {
51

    
52
    public static final int RASTER_DATA_TYPE_FLOAT = DataBuffer.TYPE_FLOAT;
53
    public static final int RASTER_DATA_TYPE_DOUBLE = DataBuffer.TYPE_DOUBLE;
54
    public static final int RASTER_DATA_TYPE_INT = DataBuffer.TYPE_INT;
55
    public static final int RASTER_DATA_TYPE_SHORT = DataBuffer.TYPE_SHORT;
56
    public static final int RASTER_DATA_TYPE_BYTE = DataBuffer.TYPE_BYTE;
57

    
58
    private final AnalysisExtent m_GridExtent;
59
    private Buffer buf = null;
60
    private String name = null;
61

    
62
    public RasterDriver(final AnalysisExtent ae, final int iDataType,
63
        final int iNumBands) {
64

    
65
        super();
66
        IProjection projection = null; //TODO check create bufefr without projection
67
        int[] listDataType; //TODO check how listDataType is made.
68
        listDataType = new int[1];
69
        listDataType[0] = (iDataType);
70
        //buf = BufferLocator.getBufferManager().createBuffer(ae.getNY(),ae.getNX(), listDataType, projection);
71
        Envelope enve;
72
        try {
73
            enve = GeometryLocator.getGeometryManager().createEnvelope(ae.getXMin(), ae.getYMin(), ae.getXMax(), ae.getYMax(), 2);
74
        } catch (CreateEnvelopeException e) {
75
            Sextante.addErrorToLog(e);
76
            enve = null;
77
        }
78
        
79
        try {
80
            buf = BufferLocator.getBufferManager().createBuffer(ae.getNY(), ae.getNX(), listDataType, null, projection, enve);
81
        } catch (BufferException e) {
82
            Sextante.addErrorToLog(e);
83
            buf = null;
84
        }
85
        m_GridExtent = ae;
86

    
87
    }
88

    
89
    public RasterDriver(final AnalysisExtent ae, final int iDataType) throws BufferException {
90
        this(ae, iDataType, 1);
91
    }
92

    
93
    public AnalysisExtent getGridExtent() {
94
        return m_GridExtent;
95
    }
96

    
97
    public Buffer getRasterBuf() {
98
        return this.buf;
99
    }
100

    
101
    public void reset() {
102
        free();
103
    }
104

    
105
    public void setCellValue(final int x, final int y, final double dValue) {
106
        setCellValue(x, y, 0, dValue);
107
    }
108

    
109
    public void setNoData(final int x, final int y) {
110
        setNoData(x, y, 0);
111
    }
112

    
113
    private void setNoData(final int x, final int y, final int iBand) {
114
        setCellValue(x, y, iBand, getNoDataValue());
115
    }
116

    
117
    public void setCellValue(final int x, final int y, final int iBand,
118
        final double dValue) {
119

    
120
        if (isInGrid(x, y) && (iBand < buf.getBandCount())) {
121
            switch (buf.getBand(iBand).getDataType()) {
122
            case BufferManager.TYPE_BYTE:
123
                buf.getBand(iBand).set(y,x, (byte) dValue);
124
                break;
125
            case BufferManager.TYPE_SHORT:
126
                buf.getBand(iBand).set(y, x,(short) dValue);
127
                break;
128
            case BufferManager.TYPE_INT:
129
                buf.getBand(iBand).set(y, x, (int) dValue);
130
                break;
131
            case BufferManager.TYPE_FLOAT:
132
                buf.getBand(iBand).set(y, x,(float) dValue);
133
                break;
134
            case BufferManager.TYPE_DOUBLE:
135
            default:
136
                buf.getBand(iBand).set(y, x, dValue);
137
                break;
138
            }
139
        }
140
    }
141

    
142
    public double getNoDataValue() {
143
        NoData nodata;
144
        nodata = buf.getBandNoData()[0];
145
        return nodata != null ? (nodata.getValue() != null ? nodata.getValue()
146
            .doubleValue() : 0.0d) : 0.0d;
147
    }
148

    
149
    public void setNoDataValue(final double dNoDataValue) {
150
        for (int i = 0; i < buf.getBandCount(); i++) {
151
                if (buf.getBand(i).getNoData()== null) {
152
                    NoData nodata = BufferLocator.getBufferManager().createNoData(dNoDataValue, null);
153
                    buf.getBand(i).getNoData().setValue(nodata.getValue());
154
                } else {
155
                    buf.getBand(i).getNoData().setValue(dNoDataValue);
156
                }
157
        }
158
    }
159

    
160
    public double getCellValue(final int x, final int y) {
161
        return getCellValue(x, y, 0);
162
    }
163

    
164
    public double getCellValue(final int x, final int y, final int iBand) {
165

    
166
        if (isInGrid(x, y) && (iBand < buf.getBandCount())) {
167
            switch (buf.getBand(iBand).getDataType()) {
168
            case BufferManager.TYPE_BYTE:
169
                return buf.getBandByte(iBand).getValue(y, x);
170
            case BufferManager.TYPE_SHORT:
171
                return buf.getBandShort(iBand).getValue(y, x);
172
            case BufferManager.TYPE_INT:
173
                return buf.getBandInt(iBand).getValue(y,x);
174
            case BufferManager.TYPE_FLOAT:
175
                return buf.getBandFloat(iBand).getValue(y,x);
176
            case BufferManager.TYPE_DOUBLE:
177
                return buf.getBandDouble(iBand).getValue(y,x);
178
            default:
179
                return getNoDataValue();
180
            }
181
        } else {
182
            return getNoDataValue();
183
        }
184

    
185
    }
186

    
187
    public boolean isNoDataValue(final double dNoDataValue) {
188
        return (getNoDataValue() == dNoDataValue);
189
    }
190

    
191
    public boolean isInGrid(final int x, final int y) {
192

    
193
        if ((x < 0) || (y < 0)) {
194
            return false;
195
        }
196
        
197
        if ((x >= m_GridExtent.getNX()) || (y >= m_GridExtent.getNY())) {
198
            return false;
199
        }
200

    
201
        return true;
202

    
203
    }
204

    
205
    public double getCellSize() {
206

    
207
        return m_GridExtent.getCellSize();
208

    
209
    }
210

    
211
    public static String getFilename(String sRoot, final String sExtension) {
212

    
213
        String sFilename;
214
        int i = 1;
215

    
216
        sRoot = sRoot.toLowerCase();
217
        sRoot = sRoot.replaceAll(" ", "_");
218
        sRoot = sRoot.replaceAll("\\)", "");
219
        sRoot = sRoot.replaceAll("\\(", "_");
220
        sRoot = sRoot.replaceAll("\\[", "_");
221
        sRoot = sRoot.replaceAll("\\]", "");
222
        sRoot = sRoot.replaceAll("<", "_");
223
        sRoot = sRoot.replaceAll(">", "_");
224
        sRoot = sRoot.replaceAll("__", "_");
225

    
226
        while (true) {
227
            sFilename =
228
                Utilities.createTempDirectory() + File.separator + sRoot
229
                    + Integer.toString(i) + "." + sExtension;
230
            final File file = new File(sFilename);
231
            if (file.exists()) {
232
                i++;
233
            } else {
234
                return sFilename;
235
            }
236
        }
237

    
238
    }
239

    
240
    public boolean export(final String sFilename, final IProjection projection) {
241

    
242

    
243
        try {
244
            final DataManager dm = DALLocator.getDataManager();
245
            DataServerExplorerParameters serverParameters = dm.createServerExplorerParameters(FilesystemServerExplorer.NAME);
246
            FilesystemServerExplorer server = (FilesystemServerExplorer) dm.openServerExplorer(FilesystemServerExplorer.NAME, serverParameters);
247
            File file = new File(sFilename);
248
            NewRasterStoreParameters parameters = (NewRasterStoreParameters) server.getAddParameters(file);
249
            parameters.setDynValue(DataStore.METADATA_CRS, projection);
250
            parameters.setBuffer(buf);
251
            server.add(parameters.getDataStoreName(), parameters, true);
252
            
253
            
254
            return true;
255
        } catch (Exception e) {
256
            Sextante.addErrorToLog(e);
257
        }
258
        return false;
259
    }
260

    
261
    public void setName(final String name) {
262
        this.name = name;
263
    }
264

    
265
    public String getName() {
266
        return this.name;
267
    }
268

    
269

    
270
    public void free() {
271
            if(buf != null) {
272
                    buf.dispose();
273
                    buf = null;
274
            }
275
    }
276

    
277
}