svn-gvsig-desktop / branches / v10 / libraries / libLidar / src / com / dielmo / gvsig / lidar / LASVariableLengthRecord_V10.java @ 23189
History | View | Annotate | Download (5.09 KB)
1 |
/*******************************************************************************
|
---|---|
2 |
LASVariableLengthRecord.java
|
3 |
Copyright (C) 2008 by DIELMO 3D S.L.
|
4 |
|
5 |
This program is free software; you can redistribute it and/or modify
|
6 |
it under the terms of the GNU General Public License as published by
|
7 |
the Free Software Foundation; either version 2 of the License, or
|
8 |
(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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
18 |
*******************************************************************************/
|
19 |
|
20 |
package com.dielmo.gvsig.lidar; |
21 |
|
22 |
import java.io.*; |
23 |
import java.nio.ByteBuffer; |
24 |
|
25 |
import javax.swing.JOptionPane; |
26 |
import com.iver.andami.PluginServices; |
27 |
import com.iver.cit.gvsig.fmap.drivers.dgn.ByteUtils; |
28 |
|
29 |
|
30 |
public class LASVariableLengthRecord_V10 extends LASVariableLengthRecord_1X { |
31 |
|
32 |
/**
|
33 |
* file Signature, must contain AABB in version LAS1.0, while in version LAS1.1
|
34 |
* it is a reserved field.
|
35 |
*/
|
36 |
private int recordSignature; |
37 |
|
38 |
/**
|
39 |
* Default constructor, without arguments.
|
40 |
* Initializes all components to zero.
|
41 |
*/
|
42 |
public LASVariableLengthRecord_V10() {
|
43 |
super();
|
44 |
recordSignature = 0;
|
45 |
} |
46 |
|
47 |
/**
|
48 |
* Return record signature. This must be always "AABB" in version LAS1.0
|
49 |
* This field is reserved in LAS1.1
|
50 |
*
|
51 |
* @return record signature
|
52 |
*/
|
53 |
public int getRecordSignature() { |
54 |
return recordSignature;
|
55 |
} |
56 |
|
57 |
/**
|
58 |
* Read the variable length record
|
59 |
*
|
60 |
* @param input input file to read
|
61 |
* @param VarLengthRecord buffer into which the data is read
|
62 |
* @return true if success else return false
|
63 |
*/
|
64 |
public boolean readVarLegthRecord(InputStream input, byte[] VarLengthRecord) { |
65 |
if(RecordLengthAfterHeader <= 0) { |
66 |
return false; |
67 |
} |
68 |
|
69 |
try {
|
70 |
|
71 |
VarLengthRecord = new byte[RecordLengthAfterHeader]; |
72 |
int offset=0,numRead=0; |
73 |
|
74 |
while (offset < RecordLengthAfterHeader && (numRead = input.read(VarLengthRecord, offset, RecordLengthAfterHeader-offset) ) >= 0) { |
75 |
offset += numRead; |
76 |
} |
77 |
|
78 |
if (offset < RecordLengthAfterHeader) {
|
79 |
JOptionPane.showMessageDialog(null, PluginServices.getText(this, "bad_input_format")); |
80 |
return false; |
81 |
} |
82 |
|
83 |
} catch(IOException e) { |
84 |
e.printStackTrace(); |
85 |
return false; |
86 |
} |
87 |
|
88 |
return true; |
89 |
} |
90 |
|
91 |
/**
|
92 |
* Read the Variable length record header of LAS file
|
93 |
*
|
94 |
* @param input input file to read
|
95 |
* @return true if success else return false
|
96 |
*/
|
97 |
public boolean readVarLegthRecordHeader(InputStream input) { |
98 |
|
99 |
int offset=0,numRead=0, j; |
100 |
byte[] VarLengthRecord = new byte[54]; |
101 |
|
102 |
//read VarLenhRecord
|
103 |
try {
|
104 |
|
105 |
while (offset < 54 && (numRead = input.read(VarLengthRecord, offset, VarLengthRecord.length-offset) ) >= 0) { |
106 |
offset += numRead; |
107 |
} |
108 |
|
109 |
if (offset < VarLengthRecord.length) {
|
110 |
JOptionPane.showMessageDialog(null, PluginServices.getText(this, "bad_input_format")); |
111 |
return false; |
112 |
} |
113 |
|
114 |
|
115 |
recordSignature = ByteUtilities.arr2Unsignedshort(VarLengthRecord, 0);
|
116 |
|
117 |
if(recordSignature != 43707) { |
118 |
return false; |
119 |
} |
120 |
|
121 |
|
122 |
for(j=0;j<16;j++) |
123 |
userID[j] = (char)(VarLengthRecord[j+2] & 0xFF); |
124 |
|
125 |
recordID = ByteUtilities.arr2Unsignedshort(VarLengthRecord, 18);
|
126 |
RecordLengthAfterHeader = ByteUtilities.arr2Unsignedshort(VarLengthRecord, 20);
|
127 |
|
128 |
for(j=0;j<32;j++) |
129 |
description[j] = (char)(VarLengthRecord[j+22] & 0xFF); |
130 |
|
131 |
|
132 |
} catch (IOException e) { |
133 |
// TODO Auto-generated catch block
|
134 |
e.printStackTrace(); |
135 |
return false; |
136 |
} |
137 |
|
138 |
return true; |
139 |
} |
140 |
|
141 |
public boolean writeVarLegthRecordHeader(ByteBuffer bb) { |
142 |
int i;
|
143 |
int index=0; |
144 |
byte[] VL_Buffer = new byte[54]; |
145 |
byte[] aux = new byte[8]; |
146 |
int[] begin = new int[1]; |
147 |
|
148 |
if(recordSignature != 43707) { |
149 |
return false; |
150 |
} |
151 |
|
152 |
// recordSignature bytes 0-2
|
153 |
begin[0] = 0; |
154 |
i=3;
|
155 |
ByteUtils.intToBytes(recordSignature, aux, begin); |
156 |
for(index=0;index<2;index++) { |
157 |
VL_Buffer[index] = aux[i]; |
158 |
i--; |
159 |
} |
160 |
|
161 |
// userID bytes 2-18
|
162 |
i = 0;
|
163 |
for(index=2;index<18;index++) { |
164 |
VL_Buffer[index] = ((byte)(userID[i] & 0XFF)); |
165 |
i++; |
166 |
} |
167 |
|
168 |
// recordID bytes 18-20
|
169 |
begin[0] = 0; |
170 |
i=3;
|
171 |
ByteUtils.intToBytes(recordID, aux, begin); |
172 |
for(index=18;index<20;index++) { |
173 |
VL_Buffer[index] = aux[i]; |
174 |
i--; |
175 |
} |
176 |
|
177 |
// RecordLengthAfterHeader bytes 20-22
|
178 |
begin[0] = 0; |
179 |
i=3;
|
180 |
ByteUtils.intToBytes(RecordLengthAfterHeader, aux, begin); |
181 |
for(index=20;index<22;index++) { |
182 |
VL_Buffer[index] = aux[i]; |
183 |
i--; |
184 |
} |
185 |
|
186 |
// description bytes 22-54
|
187 |
i = 0;
|
188 |
for(index=22;index<54;index++) { |
189 |
VL_Buffer[index] = ((byte)(description[i] & 0XFF)); |
190 |
i++; |
191 |
} |
192 |
|
193 |
bb.put(VL_Buffer); |
194 |
|
195 |
return true; |
196 |
} |
197 |
} |