Statistics
| Revision:

gvsig-raster / org.gvsig.raster.tools / trunk / org.gvsig.raster.tools / org.gvsig.raster.tools.app.basic / src / main / java / org / gvsig / raster / tools / app / basic / tool / raw / tool / VRTFileCreator.java @ 2480

History | View | Annotate | Download (8.59 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
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
*/
22
package org.gvsig.raster.tools.app.basic.tool.raw.tool;
23

    
24
import java.io.File;
25
import java.io.FileWriter;
26
import java.io.IOException;
27
import java.io.Writer;
28
/**
29
 * This class is user to create a VRT file. It writes the file when the
30
 * "writeFile" method is called. If some params are not seted the deafult values
31
 * will bi used.
32
 * 
33
 * @author Jorge Piera Llodr? (piera_jor@gva.es)
34
 */
35
public class VRTFileCreator {
36
        private Writer writer             = null;
37
        private File   m_File             = null;
38
        private int    width              = 0;
39
        private int    height             = 0;
40
        private int    bands              = 0;
41
        private int    headerSize         = 0;
42
        private int    fileSize           = 0;
43
        private String rawFile            = null;
44
        private String dataType           = null;
45
        private int    dataSize           = 0;
46
        private String byteOrder          = null;
47
        private String interleaving       = null;
48
        private String outputHeaderFormat = null;
49
        
50
        public VRTFileCreator(File m_File) {
51
                super();                
52
                this.m_File = m_File;
53
        }
54
        
55
        public VRTFileCreator(String file) {
56
                super();                
57
                this.m_File = new File(file);
58
        }
59
        
60
        /**
61
         * It writes the VRT file
62
         * @throws IOException
63
         */
64
        public void writeFile() throws IOException{
65
                if (!m_File.exists()){
66
                        m_File.createNewFile();
67
                }
68
                writer = new FileWriter(m_File);
69
                createVRTFile();
70
                writer.close();        
71
        }
72
        
73
        /**
74
         * It creates the VRT File
75
         * @throws IOException
76
         */
77
        private void createVRTFile() throws IOException{
78
                writer.write(getXMLVRTDatasetHeaderTag());
79
                
80
                for (int i=0 ; i<getBands() ; i++){
81
                        createOneBand(i+1);
82
                }
83
                
84
                writer.write(getXMLVRTDatasetEndTag());
85
        }
86
        
87
        /**
88
         * IT creates all the XML tags for one band
89
         * @param band Band number
90
         * @throws IOException
91
         */
92
        private void createOneBand(int band) throws IOException {
93
                writer.write(getXMLVRTRasterBandHeaderTag(band));
94
                writer.write(getXMLSourceFilenameTag());
95
                writer.write(getXMLByteOrderTag());
96
                writer.write(getXMLImageOffsetTag(band));
97
                writer.write(getXMLPixelOffsetTag());
98
                writer.write(getXMLLineOffsetTag());
99
                writer.write(getXMLVRTRasterBandEndTag());
100
        }
101

    
102
        /**
103
         * Creates the header of the VRTDataset XML tag. It contains the width and the
104
         * height.
105
         * @return The VRTDataset XML tag
106
         */
107
        private String getXMLVRTDatasetHeaderTag() {
108
                StringBuffer buffer = new StringBuffer();
109
                buffer.append("<VRTDataset rasterXSize=\"" + getImageWidth() + "\" ");
110
                buffer.append("rasterYSize=\"" + getImageHeight() + "\">\n");
111
                return buffer.toString();
112
        }        
113
        
114
        /**
115
         * Creates the end of the VRTDataset XML tag
116
         * @return The VRTDataset XML tag
117
         */
118
        private String getXMLVRTDatasetEndTag() {
119
                StringBuffer buffer = new StringBuffer();
120
                buffer.append("</VRTDataset>\n");
121
                return buffer.toString();
122
        }
123
        
124
        /**
125
         * Creates the header of the VRTRasterBand XML tag. It contains the image data
126
         * type.
127
         * @return The VRTRasterBand XML tag
128
         */
129
        private String getXMLVRTRasterBandHeaderTag(int band) {
130
                StringBuffer buffer = new StringBuffer();
131
                buffer.append("\t<VRTRasterBand dataType=\"" + getDataType() + "\" ");
132
                buffer.append("band=\"" + band + "\" ");
133
                buffer.append("subClass=\"VRTRawRasterBand\">\n");
134
                return buffer.toString();
135
        }
136

    
137
        /**
138
         * Creates the end of the VRTRasterBand XML tag
139
         * @return The VRTRasterBand XML tag
140
         */
141
        private String getXMLVRTRasterBandEndTag() {
142
                StringBuffer buffer = new StringBuffer();
143
                buffer.append("\t</VRTRasterBand>\n");
144
                return buffer.toString();
145
        }                
146
        
147
        
148
        /**
149
         * Creates the SourceFilename XML tag: The raw file path
150
         * @return The SourceFilename XML tag
151
         */
152
        private String getXMLSourceFilenameTag() {
153
                StringBuffer buffer = new StringBuffer();
154
                buffer.append("\t\t<SourceFilename relativetoVRT=\"1\">");
155
                buffer.append(getRawFile());
156
                buffer.append("</SourceFilename>\n");
157
                return buffer.toString();
158
        }
159

    
160
        /**
161
         * Creates the ByteOrder XML tag. Possible values are specified in
162
         * VRTFileOptions class.
163
         * @return The ByteOrder XML tag
164
         */
165
        private String getXMLByteOrderTag() {
166
                StringBuffer buffer = new StringBuffer();
167
                buffer.append("\t\t<ByteOrder>");
168
                buffer.append(getByteOrder());
169
                buffer.append("</ByteOrder>\n");
170
                return buffer.toString();
171
        }        
172
        
173
        /**
174
         * Creates the ImageOffset XML tag: The image data starts from the byte number
175
         * specified by this value
176
         * @return The ImageOffset XML tag
177
         */
178
        private String getXMLImageOffsetTag(int band) {
179
                StringBuffer buffer = new StringBuffer();
180
                buffer.append("\t\t<ImageOffset>");
181
                buffer.append(VRTFormatUtils.getImageOffset(this, band));
182
                buffer.append("</ImageOffset>\n");
183
                return buffer.toString();
184
        }
185

    
186
        /**
187
         * Creates the PixelOffset XML tag: The byte offeset between pixels
188
         * @return The PixelOffset XML tag
189
         */
190
        private String getXMLPixelOffsetTag() {
191
                StringBuffer buffer = new StringBuffer();
192
                buffer.append("\t\t<PixelOffset>");
193
                buffer.append(VRTFormatUtils.getPixelOffset(this));
194
                buffer.append("</PixelOffset>\n");
195
                return buffer.toString();
196
        }
197

    
198
        /**
199
         * Creates the LineOffset XML tag.
200
         * @return The LineOffset XML tag
201
         */
202
        private String getXMLLineOffsetTag() {
203
                StringBuffer buffer = new StringBuffer();
204
                buffer.append("\t\t<LineOffset>");
205
                buffer.append(VRTFormatUtils.getLineOffset(this));
206
                buffer.append("</LineOffset>\n");
207
                return buffer.toString();
208
        }
209

    
210
        /**
211
         * @return Returns the bands.
212
         */
213
        public int getBands() {
214
                return bands;
215
        }
216

    
217
        /**
218
         * @param bands The bands to set.
219
         */
220
        public void setBands(int bands) {
221
                this.bands = bands;
222
        }
223

    
224
        /**
225
         * @return Returns the byteOrder.
226
         */
227
        public String getByteOrder() {
228
                return byteOrder;
229
        }
230

    
231
        /**
232
         * @param byteOrder The byteOrder to set.
233
         */
234
        public void setByteOrder(String byteOrder) {
235
                this.byteOrder = byteOrder;
236
        }
237

    
238
        /**
239
         * @return Returns the dataType.
240
         */
241
        public String getDataType() {
242
                return dataType;
243
        }
244

    
245
        /**
246
         * @param dataType The dataType to set.
247
         */
248
        public void setDataType(String dataType) {
249
                this.dataType = dataType;
250
        }
251

    
252
        /**
253
         * @return Returns the fileSize.
254
         */
255
        public int getFileSize() {
256
                return fileSize;
257
        }
258

    
259
        /**
260
         * @param fileSize The fileSize to set.
261
         */
262
        public void setFileSize(int fileSize) {
263
                this.fileSize = fileSize;
264
        }
265

    
266
        /**
267
         * @return Returns the headerSize.
268
         */
269
        public int getHeaderSize() {
270
                return headerSize;
271
        }
272

    
273
        /**
274
         * @param headerSize The headerSize to set.
275
         */
276
        public void setHeaderSize(int headerSize) {
277
                this.headerSize = headerSize;
278
        }
279

    
280
        /**
281
         * @return Returns the height.
282
         */
283
        public int getImageHeight() {
284
                return height;
285
        }
286

    
287
        /**
288
         * @param height The height to set.
289
         */
290
        public void setImageHeight(int height) {
291
                this.height = height;
292
        }
293

    
294
        /**
295
         * @return Returns the interleaving.
296
         */
297
        public String getInterleaving() {
298
                return interleaving;
299
        }
300

    
301
        /**
302
         * @param interleaving The interleaving to set.
303
         */
304
        public void setInterleaving(String interleaving) {
305
                this.interleaving = interleaving;
306
        }
307

    
308
        /**
309
         * @return Returns the outputHeaderFormat.
310
         */
311
        public String getOutputHeaderFormat() {
312
                return outputHeaderFormat;
313
        }
314

    
315
        /**
316
         * @param outputHeaderFormat The outputHeaderFormat to set.
317
         */
318
        public void setOutputHeaderFormat(String outputHeaderFormat) {
319
                this.outputHeaderFormat = outputHeaderFormat;
320
        }
321

    
322
        /**
323
         * @return Returns the width.
324
         */
325
        public int getImageWidth() {
326
                return width;
327
        }
328

    
329
        /**
330
         * @param width The width to set.
331
         */
332
        public void setImageWidth(int width) {
333
                this.width = width;
334
        }
335

    
336
        /**
337
         * @return Returns the rawFile.
338
         */
339
        public String getRawFile() {
340
                return rawFile;
341
        }
342

    
343
        /**
344
         * @param rawFile The rawFile to set.
345
         */
346
        public void setRawFile(String rawFile) {
347
                this.rawFile = rawFile;
348
        }
349

    
350
        /**
351
         * @return Returns the m_File.
352
         */
353
        public File getM_File() {
354
                return m_File;
355
        }
356

    
357
        /**
358
         * @return Returns the dataSize.
359
         */
360
        public int getDataSize() {
361
                return dataSize;
362
        }
363

    
364
        /**
365
         * @param dataSize The dataSize to set.
366
         */
367
        public void setDataSize(int dataSize) {
368
                this.dataSize = dataSize;
369
        }
370
}