svn-gvsig-desktop / branches / org.gvsig.desktop-2018a / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.file / org.gvsig.fmap.dal.file.bsq / src / main / java / org / gvsig / fmap / dal / file / bsq / BSQFileSystemServerProvider.java @ 43876
History | View | Annotate | Download (5.95 KB)
1 | 43861 | jjdelcerro | /* gvSIG. Desktop Geographic Information System.
|
---|---|---|---|
2 | *
|
||
3 | * Copyright ? 2007-2016 gvSIG Association
|
||
4 | *
|
||
5 | * This program is free software; you can redistribute it and/or
|
||
6 | * modify it under the terms of the GNU General Public License
|
||
7 | * as published by the Free Software Foundation; either version 2
|
||
8 | * of the License, or (at your option) any later version.
|
||
9 | *
|
||
10 | * This program is distributed in the hope that it will be useful,
|
||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
13 | * GNU General Public License for more details.
|
||
14 | *
|
||
15 | * You should have received a copy of the GNU General Public License
|
||
16 | * along with this program; if not, write to the Free Software
|
||
17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||
18 | * MA 02110-1301, USA.
|
||
19 | *
|
||
20 | * For any additional information, do not hesitate to contact us
|
||
21 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
22 | */
|
||
23 | 43867 | jjdelcerro | package org.gvsig.fmap.dal.file.bsq; |
24 | 43861 | jjdelcerro | |
25 | 43876 | jjdelcerro | import java.io.DataOutputStream; |
26 | 43861 | jjdelcerro | import java.io.File; |
27 | 43876 | jjdelcerro | import java.io.FileOutputStream; |
28 | import java.io.IOException; |
||
29 | import java.nio.ByteOrder; |
||
30 | 43861 | jjdelcerro | import org.apache.commons.io.FilenameUtils; |
31 | 43876 | jjdelcerro | import org.apache.commons.io.IOUtils; |
32 | import org.gvsig.basicformats.FormatsFile; |
||
33 | import org.gvsig.basicformats.HDRFile; |
||
34 | 43861 | jjdelcerro | |
35 | import org.gvsig.fmap.dal.DataServerExplorer; |
||
36 | 43867 | jjdelcerro | import org.gvsig.fmap.dal.NewDataStoreParameters; |
37 | import org.gvsig.fmap.dal.exception.CreateException; |
||
38 | import org.gvsig.fmap.dal.exception.DataException; |
||
39 | 43876 | jjdelcerro | import org.gvsig.fmap.dal.raster.NewRasterStoreParameters; |
40 | import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters; |
||
41 | 43861 | jjdelcerro | import org.gvsig.fmap.dal.serverexplorer.filesystem.impl.AbstractFilesystemServerExplorerProvider; |
42 | 43876 | jjdelcerro | import org.gvsig.raster.lib.buffer.api.Band; |
43 | import org.gvsig.raster.lib.buffer.api.Buffer; |
||
44 | import org.gvsig.raster.lib.buffer.api.BufferDimensions; |
||
45 | import org.gvsig.raster.lib.buffer.api.BufferManager; |
||
46 | 43861 | jjdelcerro | |
47 | 43867 | jjdelcerro | public class BSQFileSystemServerProvider extends AbstractFilesystemServerExplorerProvider { |
48 | 43861 | jjdelcerro | |
49 | protected String[] fileExtensions; |
||
50 | |||
51 | 43867 | jjdelcerro | public BSQFileSystemServerProvider(
|
52 | 43876 | jjdelcerro | String name,
|
53 | String description,
|
||
54 | 43861 | jjdelcerro | String[] fileExtensions |
55 | 43876 | jjdelcerro | ) { |
56 | 43861 | jjdelcerro | super(name, description);
|
57 | this.fileExtensions = fileExtensions;
|
||
58 | } |
||
59 | 43876 | jjdelcerro | |
60 | 43861 | jjdelcerro | @Override
|
61 | 43867 | jjdelcerro | public boolean canCreate() { |
62 | 43876 | jjdelcerro | return true; |
63 | 43867 | jjdelcerro | } |
64 | |||
65 | @Override
|
||
66 | public NewDataStoreParameters getCreateParameters() throws DataException { |
||
67 | throw new UnsupportedOperationException(); |
||
68 | } |
||
69 | |||
70 | @Override
|
||
71 | public void create(NewDataStoreParameters parameters, boolean overwrite) throws CreateException { |
||
72 | 43876 | jjdelcerro | FileOutputStream fos = null; |
73 | try {
|
||
74 | Buffer buffer = ((NewRasterStoreParameters) parameters).getBuffer();
|
||
75 | if (!buffer.areAllBandsOfTheSameType()) {
|
||
76 | throw new IllegalArgumentException("The BSQ format does not support bands of different types."); |
||
77 | } |
||
78 | BufferDimensions dimensions = buffer.getDimensions(); |
||
79 | File file = ((FilesystemStoreParameters) parameters).getFile();
|
||
80 | fos = new FileOutputStream(file); |
||
81 | DataOutputStream dos = new DataOutputStream(fos); |
||
82 | HDRFile hdr = FormatsFile.createHDRFile(); |
||
83 | hdr.setDataType(buffer.getBandTypes()[0]);
|
||
84 | hdr.setNBands(buffer.getBandCount()); |
||
85 | hdr.setByteOrder(ByteOrder.nativeOrder());
|
||
86 | hdr.setDimensions( |
||
87 | dimensions.getRows(), |
||
88 | dimensions.getColumns(), |
||
89 | dimensions.getEnvelope() |
||
90 | ); |
||
91 | for (int nband = 0; nband < buffer.getBandCount(); nband++) { |
||
92 | writeBand(dos, buffer.getBand(nband)); |
||
93 | } |
||
94 | hdr.write(file); |
||
95 | } catch (Exception ex) { |
||
96 | throw new CreateException(this.getName(), ex); |
||
97 | } finally {
|
||
98 | IOUtils.closeQuietly(fos); |
||
99 | } |
||
100 | 43867 | jjdelcerro | } |
101 | 43876 | jjdelcerro | |
102 | void writeBand(DataOutputStream dos, Band band) throws IOException { |
||
103 | int dataType = band.getDataType();
|
||
104 | Object rowBuffer = band.createRowBuffer();
|
||
105 | for (int row = 0; row < band.getRows(); row++) { |
||
106 | band.fetchRow(row, rowBuffer); |
||
107 | switch (dataType) {
|
||
108 | case BufferManager.TYPE_BYTE:
|
||
109 | byte[] byteBuffer = (byte[]) rowBuffer; |
||
110 | for (int n = 0; n < byteBuffer.length; n++) { |
||
111 | dos.writeByte(byteBuffer[n]); |
||
112 | } |
||
113 | break;
|
||
114 | case BufferManager.TYPE_SHORT:
|
||
115 | case BufferManager.TYPE_USHORT:
|
||
116 | short[] shortBuffer = (short[]) rowBuffer; |
||
117 | for (int n = 0; n < shortBuffer.length; n++) { |
||
118 | dos.writeShort(shortBuffer[n]); |
||
119 | } |
||
120 | break;
|
||
121 | case BufferManager.TYPE_INT:
|
||
122 | int[] intBuffer = (int[]) rowBuffer; |
||
123 | for (int n = 0; n < intBuffer.length; n++) { |
||
124 | dos.writeInt(intBuffer[n]); |
||
125 | } |
||
126 | break;
|
||
127 | case BufferManager.TYPE_FLOAT:
|
||
128 | float[] floatBuffer = (float[]) rowBuffer; |
||
129 | for (int n = 0; n < floatBuffer.length; n++) { |
||
130 | dos.writeFloat(floatBuffer[n]); |
||
131 | } |
||
132 | break;
|
||
133 | |||
134 | case BufferManager.TYPE_DOUBLE:
|
||
135 | double[] doubleBuffer = (double[]) rowBuffer; |
||
136 | for (int n = 0; n < doubleBuffer.length; n++) { |
||
137 | dos.writeDouble(doubleBuffer[n]); |
||
138 | } |
||
139 | break;
|
||
140 | |||
141 | default:
|
||
142 | throw new IllegalArgumentException("Data Type " + dataType + " not supported."); |
||
143 | } |
||
144 | } |
||
145 | } |
||
146 | |||
147 | 43867 | jjdelcerro | @Override
|
148 | 43861 | jjdelcerro | public int getMode() { |
149 | return DataServerExplorer.MODE_RASTER;
|
||
150 | } |
||
151 | |||
152 | @Override
|
||
153 | public boolean accept(File pathname) { |
||
154 | return FilenameUtils.isExtension(pathname.getName().toLowerCase(), this.fileExtensions); |
||
155 | } |
||
156 | } |