Statistics
| Revision:

svn-gvsig-desktop / branches / v10 / libraries / libDielmoOpenLidar / src / com / dielmo / lidar / LASVariableLengthRecord_V11.java @ 26395

History | View | Annotate | Download (5.2 KB)

1
/* DielmoOpenLiDAR
2
 *
3
 * Copyright (C) 2008 DIELMO 3D S.L. (DIELMO) and Infrastructures  
4
 * and Transports Department 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
 * For more information, contact:
22
 *
23
 * DIELMO 3D S.L.
24
 * Plaza Vicente Andr?s Estell?s 1 Bajo E
25
 * 46950 Xirivella, Valencia
26
 * SPAIN
27
 *   
28
 * +34 963137212
29
 * dielmo@dielmo.com
30
 * www.dielmo.com
31
 * 
32
 * or
33
 * 
34
 * Generalitat Valenciana
35
 * Conselleria d'Infraestructures i Transport
36
 * Av. Blasco Ib??ez, 50
37
 * 46010 VALENCIA
38
 * SPAIN
39
 *
40
 * +34 963862235
41
 * gvsig@gva.es
42
 * www.gvsig.gva.es
43
 */
44

    
45
/*
46
 * AUTHORS (In addition to DIELMO and CIT):
47
 *  
48
 */
49

    
50
package com.dielmo.lidar;
51

    
52
import java.io.*;
53
import java.nio.ByteBuffer;
54

    
55
import javax.swing.JOptionPane;
56

    
57

    
58
public class LASVariableLengthRecord_V11 extends LASVariableLengthRecord_1X {        
59
        
60
        /**
61
         * a reserved field.
62
         */
63
        private int reserved;
64
        
65
        /**
66
         * Default constructor, without arguments.
67
         * Initializes all components to zero.
68
         */ 
69
        public LASVariableLengthRecord_V11() {
70
                super();
71
                reserved = 0;
72
        }
73
        
74
        /**
75
         * This field is reserved in LAS1.1
76
         * 
77
         * @return record signature
78
         */
79
        public int getReserved() {        
80
                return reserved;
81
        }
82
        
83
        /**
84
         * This field is reserved in LAS1.1
85
         * 
86
         * param r new record signature
87
         */
88
        public void setReserved(int r) {
89
                
90
                try{
91
                        if(r>=0 && r<=LidarHeader.UNSIGNED_SHORT_MAX)
92
                                reserved = r;
93
                        else
94
                                throw new OutOfRangeLidarException("Out of range of reserved field (Variable Length Record)");
95
                        
96
                } catch(OutOfRangeLidarException e) {
97
                        
98
                        e.printStackTrace();
99
                }
100
        }
101

    
102
        /**
103
         * Read the variable length record
104
         * 
105
         * @param input input file to read
106
         * @param VarLengthRecord buffer into which the data is read 
107
         * @return true if success else return false 
108
         */
109
        protected boolean readVarLegthRecordData(InputStream input) {
110
                if(RecordLengthAfterHeader <= 0) {
111
                        return false;
112
                }
113
                
114
                try {
115
                        
116
                        data = new byte[RecordLengthAfterHeader];
117
                        int offset=0,numRead=0;
118

    
119
                        while (offset < RecordLengthAfterHeader && (numRead = input.read(data, offset, RecordLengthAfterHeader-offset) ) >= 0) {
120
                             offset += numRead;
121
                        }
122
                        
123
                    if (offset < RecordLengthAfterHeader) {
124
                            JOptionPane.showMessageDialog(null, "Bad input format");
125
                            return false;
126
                        }
127
                } catch(IOException e) {
128
                        
129
                        e.printStackTrace();
130
                        return false;
131
                }
132
                
133
                return true;                        
134
        }
135
        
136
        /**
137
         * Read the Variable length record header of LAS file
138
         * 
139
         * @param input input file to read
140
         * @return true if success else return false 
141
         */
142
        public boolean readVarLegthRecord(InputStream input) {
143
                
144
                int offset=0,numRead=0, j; 
145
                byte[] VarLengthRecord = new byte[54];
146
                
147
                //read VarLenhRecord
148
              try {
149
                    
150
                        while (offset < 54 && (numRead = input.read(VarLengthRecord, offset, VarLengthRecord.length-offset) ) >= 0) {
151
                                
152
                             offset += numRead;
153
                        }
154
                        
155
                    if (offset < VarLengthRecord.length) {
156
                            JOptionPane.showMessageDialog(null, "Bad input format");
157
                            return false;
158
                        }
159
                    
160
                
161
                    reserved = ByteUtilities.arr2Unsignedshort(VarLengthRecord, 0);
162
              
163
                    for(j=0;j<16;j++)
164
                                userID[j] = (char)(VarLengthRecord[j+2] & 0xFF);
165
                        
166
                        recordID = ByteUtilities.arr2Unsignedshort(VarLengthRecord, 18);
167
                        RecordLengthAfterHeader = ByteUtilities.arr2Unsignedshort(VarLengthRecord, 20);
168
                        
169
                        for(j=0;j<32;j++)
170
                                description[j] = (char)(VarLengthRecord[j+22] & 0xFF);
171

    
172
                        // read data of VLR
173
                        readVarLegthRecordData(input);
174
                    
175
                } catch (IOException e) {
176
                        // TODO Auto-generated catch block
177
                        e.printStackTrace();
178
                        return false;
179
                }
180
                
181
                return true;
182
        }
183

    
184
        public boolean writeVarLegthRecord(ByteBuffer bb) {
185
                
186
                int i;
187
                int index=0;
188
                byte[] VL_Buffer = new byte[54];
189
            
190
                // reserved bytes 0-2
191
                ByteUtilities.unsignedShort2Arr(getReserved(), VL_Buffer, 0);
192
            
193
                // userID bytes 2-18
194
                i = 0;
195
                for(index=2;index<18;index++) {
196
                        VL_Buffer[index] = ((byte)(userID[i] & 0XFF));
197
                        i++;
198
                }
199
                
200
                // recordID bytes 18-20
201
                ByteUtilities.unsignedShort2Arr(getRecordID(), VL_Buffer, 18);
202
                
203
                // RecordLengthAfterHeader bytes 20-22
204
                ByteUtilities.unsignedShort2Arr(getRecordLengthAfterHeader(), VL_Buffer, 20);
205

    
206
                // description bytes 22-54
207
                i = 0;
208
                for(index=22;index<54;index++) {
209
                        VL_Buffer[index] = ((byte)(description[i] & 0XFF));
210
                        i++;
211
                }
212
                
213
                // write header VLR
214
                bb.put(VL_Buffer);
215
                // write data VLR
216
                bb.put(getData());
217
                
218
                return true;
219
        }
220
}