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

History | View | Annotate | Download (9.45 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
import java.util.logging.Level;
32
import java.util.logging.Logger;
33

    
34
import org.cresques.cts.IProjection;
35

    
36
import org.gvsig.andami.Utilities;
37
import org.gvsig.fmap.dal.DALLocator;
38
import org.gvsig.fmap.dal.DataManager;
39
import org.gvsig.fmap.dal.DataServerExplorerParameters;
40
import org.gvsig.fmap.dal.DataStore;
41
import org.gvsig.fmap.dal.raster.api.NewRasterStoreParameters;
42
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer;
43
import org.gvsig.fmap.geom.GeometryLocator;
44
import org.gvsig.fmap.geom.exception.CreateEnvelopeException;
45
import org.gvsig.fmap.geom.primitive.Envelope;
46
import org.gvsig.raster.lib.buffer.api.Buffer;
47
import org.gvsig.raster.lib.buffer.api.BufferLocator;
48
import org.gvsig.raster.lib.buffer.api.BufferManager;
49
import org.gvsig.raster.lib.buffer.api.NoData;
50
import org.gvsig.raster.lib.buffer.api.exceptions.BufferException;
51

    
52
public class RasterDriver {
53

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

    
60
    private final AnalysisExtent m_GridExtent;
61
    private Buffer buf = null;
62
    private String name = null;
63

    
64
    public RasterDriver(final AnalysisExtent ae, final int iDataType,
65
        final int iNumBands) {
66

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

    
89
    }
90

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

    
95
    public AnalysisExtent getGridExtent() {
96
        return m_GridExtent;
97
    }
98

    
99
    public Buffer getRasterBuf() {
100
        return this.buf;
101
    }
102

    
103
    public void reset() {
104
        free();
105
    }
106

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

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

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

    
119
    public void setCellValue(final int x, final int y, final int iBand,
120
        final double dValue) {
121

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

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

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

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

    
166
    public double getCellValue(final int x, final int y, final int iBand) {
167

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

    
187
    }
188

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

    
193
    public boolean isInGrid(final int x, final int y) {
194

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

    
203
        return true;
204

    
205
    }
206

    
207
    public double getCellSize() {
208

    
209
        return m_GridExtent.getCellSize();
210

    
211
    }
212

    
213
    public static String getFilename(String sRoot, final String sExtension) {
214

    
215
        String sFilename;
216
        int i = 1;
217

    
218
        sRoot = sRoot.toLowerCase();
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
        sRoot = sRoot.replaceAll(">", "_");
226
        sRoot = sRoot.replaceAll("__", "_");
227

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

    
240
    }
241

    
242
    public boolean export(final String sFilename, final IProjection projection) {
243

    
244

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

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

    
267
    public String getName() {
268
        return this.name;
269
    }
270

    
271

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

    
279
}