svn-gvsig-desktop / trunk / org.gvsig.desktop / 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 @ 44831
History | View | Annotate | Download (6.12 KB)
1 |
/* 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 |
package org.gvsig.fmap.dal.file.bsq; |
24 |
|
25 |
import java.io.DataOutputStream; |
26 |
import java.io.File; |
27 |
import java.io.FileOutputStream; |
28 |
import java.io.IOException; |
29 |
import java.nio.ByteOrder; |
30 |
import org.apache.commons.io.FilenameUtils; |
31 |
import org.apache.commons.io.IOUtils; |
32 |
import org.gvsig.basicformats.FormatsFile; |
33 |
import org.gvsig.basicformats.HDRFile; |
34 |
|
35 |
import org.gvsig.fmap.dal.DataServerExplorer; |
36 |
import org.gvsig.fmap.dal.NewDataStoreParameters; |
37 |
import org.gvsig.fmap.dal.exception.CreateException; |
38 |
import org.gvsig.fmap.dal.exception.DataException; |
39 |
import org.gvsig.fmap.dal.raster.NewRasterStoreParameters; |
40 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters; |
41 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.impl.AbstractFilesystemServerExplorerProvider; |
42 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.spi.FilesystemServerExplorerProviderFactory; |
43 |
import org.gvsig.raster.lib.buffer.api.Band; |
44 |
import org.gvsig.raster.lib.buffer.api.Buffer; |
45 |
import org.gvsig.raster.lib.buffer.api.BufferDimensions; |
46 |
import org.gvsig.raster.lib.buffer.api.BufferManager; |
47 |
|
48 |
public class BSQFileSystemServerProvider extends AbstractFilesystemServerExplorerProvider { |
49 |
|
50 |
protected String[] fileExtensions; |
51 |
|
52 |
public BSQFileSystemServerProvider(
|
53 |
FilesystemServerExplorerProviderFactory factory, |
54 |
String name,
|
55 |
String description,
|
56 |
String[] fileExtensions |
57 |
) { |
58 |
super(factory, name, description);
|
59 |
this.fileExtensions = fileExtensions;
|
60 |
} |
61 |
|
62 |
@Override
|
63 |
public boolean canCreate() { |
64 |
return true; |
65 |
} |
66 |
|
67 |
@Override
|
68 |
public NewDataStoreParameters getCreateParameters() throws DataException { |
69 |
throw new UnsupportedOperationException(); |
70 |
} |
71 |
|
72 |
@Override
|
73 |
public void create(NewDataStoreParameters parameters, boolean overwrite) throws CreateException { |
74 |
FileOutputStream fos = null; |
75 |
try {
|
76 |
Buffer buffer = ((NewRasterStoreParameters) parameters).getBuffer();
|
77 |
if (!buffer.areAllBandsOfTheSameType()) {
|
78 |
throw new IllegalArgumentException("The BSQ format does not support bands of different types."); |
79 |
} |
80 |
BufferDimensions dimensions = buffer.getDimensions(); |
81 |
File file = ((FilesystemStoreParameters) parameters).getFile();
|
82 |
fos = new FileOutputStream(file); |
83 |
DataOutputStream dos = new DataOutputStream(fos); |
84 |
HDRFile hdr = FormatsFile.createHDRFile(); |
85 |
hdr.setDataType(buffer.getBandTypes()[0]);
|
86 |
hdr.setNBands(buffer.getBandCount()); |
87 |
hdr.setByteOrder(ByteOrder.nativeOrder());
|
88 |
hdr.setDimensions( |
89 |
dimensions.getRows(), |
90 |
dimensions.getColumns(), |
91 |
dimensions.getEnvelope() |
92 |
); |
93 |
for (int nband = 0; nband < buffer.getBandCount(); nband++) { |
94 |
writeBand(dos, buffer.getBand(nband)); |
95 |
} |
96 |
hdr.write(file); |
97 |
} catch (Exception ex) { |
98 |
throw new CreateException(this.getName(), ex); |
99 |
} finally {
|
100 |
IOUtils.closeQuietly(fos); |
101 |
} |
102 |
} |
103 |
|
104 |
void writeBand(DataOutputStream dos, Band band) throws IOException { |
105 |
int dataType = band.getDataType();
|
106 |
Object rowBuffer = band.createRowBuffer();
|
107 |
for (int row = 0; row < band.getRows(); row++) { |
108 |
band.fetchRow(row, rowBuffer); |
109 |
switch (dataType) {
|
110 |
case BufferManager.TYPE_BYTE:
|
111 |
byte[] byteBuffer = (byte[]) rowBuffer; |
112 |
for (int n = 0; n < byteBuffer.length; n++) { |
113 |
dos.writeByte(byteBuffer[n]); |
114 |
} |
115 |
break;
|
116 |
case BufferManager.TYPE_SHORT:
|
117 |
case BufferManager.TYPE_USHORT:
|
118 |
short[] shortBuffer = (short[]) rowBuffer; |
119 |
for (int n = 0; n < shortBuffer.length; n++) { |
120 |
dos.writeShort(shortBuffer[n]); |
121 |
} |
122 |
break;
|
123 |
case BufferManager.TYPE_INT:
|
124 |
int[] intBuffer = (int[]) rowBuffer; |
125 |
for (int n = 0; n < intBuffer.length; n++) { |
126 |
dos.writeInt(intBuffer[n]); |
127 |
} |
128 |
break;
|
129 |
case BufferManager.TYPE_FLOAT:
|
130 |
float[] floatBuffer = (float[]) rowBuffer; |
131 |
for (int n = 0; n < floatBuffer.length; n++) { |
132 |
dos.writeFloat(floatBuffer[n]); |
133 |
} |
134 |
break;
|
135 |
|
136 |
case BufferManager.TYPE_DOUBLE:
|
137 |
double[] doubleBuffer = (double[]) rowBuffer; |
138 |
for (int n = 0; n < doubleBuffer.length; n++) { |
139 |
dos.writeDouble(doubleBuffer[n]); |
140 |
} |
141 |
break;
|
142 |
|
143 |
default:
|
144 |
throw new IllegalArgumentException("Data Type " + dataType + " not supported."); |
145 |
} |
146 |
} |
147 |
} |
148 |
|
149 |
@Override
|
150 |
public int getMode() { |
151 |
return DataServerExplorer.MODE_RASTER;
|
152 |
} |
153 |
|
154 |
@Override
|
155 |
public boolean accept(File pathname) { |
156 |
return FilenameUtils.isExtension(pathname.getName().toLowerCase(), this.fileExtensions); |
157 |
} |
158 |
} |