Statistics
| Revision:

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

History | View | Annotate | Download (7.26 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.File;
53
import java.io.FileInputStream;
54
import java.io.IOException;
55
import java.io.InputStream;
56

    
57
import javax.swing.JOptionPane;
58

    
59

    
60
/**
61
 * Inicizliza Lidar points and header
62
 *
63
 * @author Oscar Garcia
64
 */
65
public class InicializeLidar {
66

    
67
        public static int testLASFormat(File name) throws IOException,
68
                        UnexpectedLidarHeaderException {
69

    
70
                File f = name;
71
                InputStream input;
72
                int offset = 0,numRead = 0;
73
                char[] FileSignatureLASF = new char[4];
74
                char  versionMajor, versionMinor, format;
75
                byte[] cabecera = new byte[105]; // lleemos hasta el byte que nos indica la version de LAS en la que estamos
76

    
77

    
78
                    input = new FileInputStream(f);
79
                        while (offset < cabecera.length && (numRead = input.read(cabecera, offset, cabecera.length-offset) ) >= 0) {
80
                             offset += numRead;
81
                        }
82

    
83
                    FileSignatureLASF[0] = (char)(cabecera[0] & 0xFF);
84
                    FileSignatureLASF[1] = (char)(cabecera[1] & 0xFF);
85
                    FileSignatureLASF[2] = (char)(cabecera[2] & 0xFF);
86
                    FileSignatureLASF[3] = (char)(cabecera[3] & 0xFF);
87

    
88
                    if(FileSignatureLASF[0] == 'L'&& FileSignatureLASF[1] == 'A'&& FileSignatureLASF[2] == 'S' && FileSignatureLASF[3] == 'F') {
89

    
90
                            if (offset < cabecera.length) {
91
                                    JOptionPane.showMessageDialog(null, "Bad input format");
92
                                }
93

    
94
                            versionMajor = (char)(cabecera[24] & 0xFF);
95
                            versionMinor = (char)(cabecera[25] & 0xFF);
96
                            format = (char)(cabecera[104] & 0xFF);
97

    
98
                            if(versionMajor == 1 && versionMinor == 0) {
99

    
100
                                    if(format == 0) {
101
                                                return LidarHeader.LAS10F0;
102
                                        } else if(format == 1) {
103
                                                return LidarHeader.LAS10F1;
104
                                        }
105
                            } else if(versionMajor == 1 && versionMinor == 1) {
106

    
107
                                    if(format == 0) {
108
                                                return LidarHeader.LAS11F0;
109
                                        } else if(format == 1) {
110
                                                return LidarHeader.LAS11F1;
111
                                        }
112
                            } else if (versionMajor == 1 && versionMinor == 2){
113

    
114
                                    if(format == 0) {
115
                                                return LidarHeader.LAS12F0;
116
                                        } else if(format == 1) {
117
                                                return LidarHeader.LAS12F1;
118
                                        } else if(format == 2) {
119
                                                return LidarHeader.LAS12F2;
120
                                        } else if(format == 3) {
121
                                                return LidarHeader.LAS12F3;
122
                                        }
123

    
124
                            } else {
125
                                    throw new UnexpectedLidarHeaderException("Header not expected");
126
                            }
127
                    }
128

    
129

    
130
                return LidarHeader.UNEXPECTED;
131
        }
132

    
133
        public static int testBINFormat(File name) throws IOException {
134

    
135
                File f = name;
136
                InputStream input;
137
                int offset = 0,numRead = 0;
138
                byte[] cabecera = new byte[8];
139

    
140

    
141
                input = new FileInputStream(f);
142
                while (offset < cabecera.length
143
                                && (numRead = input.read(cabecera, offset, cabecera.length
144
                                                - offset)) >= 0) {
145
                        offset += numRead;
146
                }
147

    
148
                int HdrVersionBin;
149
                HdrVersionBin = ByteUtilities.arr2Int(cabecera, 4);
150
                if (HdrVersionBin == 20020715) {
151

    
152
                        return LidarHeader.BIN20020715;
153
                } else if (HdrVersionBin == 20010712) {
154

    
155
                        return LidarHeader.BIN20010712;
156
                } // ( HdrVersionBin == 20010129 || HdrVersionBin == 970404)
157

    
158

    
159

    
160
                return LidarHeader.UNEXPECTED;
161
        }
162

    
163
        public static int testFormat(File name) throws IOException,
164
                        UnexpectedLidarHeaderException {
165

    
166
                int f = testLASFormat(name);
167
                if(f != LidarHeader.UNEXPECTED) {
168
                        return f;
169
                }
170

    
171
                f = testBINFormat(name);
172
                if(f != LidarHeader.UNEXPECTED) {
173
                        return f;
174
                }
175

    
176
                if (f == LidarHeader.UNEXPECTED) {
177
                    JOptionPane.showMessageDialog(null, "Unsupported format");
178
                }
179

    
180
                return LidarHeader.UNEXPECTED;
181

    
182
        }
183

    
184
        public static LidarPoint InizializeLidarPoint(File file)
185
                        throws IOException, UnexpectedLidarHeaderException {
186
                boolean binColor, binTime;
187

    
188
                int type = testFormat(file);
189
                switch(type) {
190

    
191
                        case LidarHeader.LAS10F0:
192
                                return new LASPoint10F0();
193

    
194
                        case LidarHeader.LAS10F1:
195
                                return new LASPoint10F1();
196

    
197
                        case LidarHeader.LAS11F0:
198
                                return new LASPoint11F0();
199

    
200
                        case LidarHeader.LAS11F1:
201
                                return new LASPoint11F1();
202

    
203
                        case LidarHeader.LAS12F0:
204
                                return new LASPoint12F0();
205

    
206
                        case LidarHeader.LAS12F1:
207
                                return new LASPoint12F1();
208

    
209
                        case LidarHeader.LAS12F2:
210
                                return new LASPoint12F2();
211

    
212
                        case LidarHeader.LAS12F3:
213
                                return new LASPoint12F3();
214

    
215
                        case LidarHeader.BIN20010712:
216

    
217
                                BINHeader headerBin2001 = new BINHeader(file);
218
                                headerBin2001.readLidarHeader();
219

    
220
                                binColor = false;
221
                                binTime = false;
222

    
223

    
224
                                if(headerBin2001.getColor()>0) {
225
                                        binColor = true;
226
                                }
227

    
228
                                if(headerBin2001.getTime()>0) {
229
                                        binTime = true;
230
                                }
231

    
232
                                return new BINPoint2001(binColor, binTime);
233

    
234
                        case LidarHeader.BIN20020715:
235
                                BINHeader headerBin2002 = new BINHeader(file);
236
                                headerBin2002.readLidarHeader();
237

    
238
                                binColor = false;
239
                                binTime = false;
240

    
241

    
242
                                if(headerBin2002.getColor()>0) {
243
                                        binColor = true;
244
                                }
245

    
246
                                if(headerBin2002.getTime()>0) {
247
                                        binTime = true;
248
                                }
249

    
250
                                return new BINPoint2002(binColor, binTime);
251
                }
252

    
253
                return null;
254
        }
255

    
256

    
257
        public static LidarHeader InizializeLidarHeader(File file)
258
                        throws IOException, UnexpectedLidarHeaderException {
259
                int type = testFormat(file);
260
                switch(type) {
261

    
262
                        case LidarHeader.LAS10F0:
263
                        case LidarHeader.LAS10F1:
264
                                LASHeader_V10 header10 = new LASHeader_V10(file);
265
                                header10.readLidarHeader();
266
                                return header10;
267

    
268
                        case LidarHeader.LAS11F0:
269
                        case LidarHeader.LAS11F1:
270
                                LASHeader_V11 header11 = new LASHeader_V11(file);
271
                                header11.readLidarHeader();
272
                                return header11;
273

    
274
                        case LidarHeader.LAS12F0:
275
                        case LidarHeader.LAS12F1:
276
                        case LidarHeader.LAS12F2:
277
                        case LidarHeader.LAS12F3:
278
                                LASHeader_V12 header12 = new LASHeader_V12(file);
279
                                header12.readLidarHeader();
280
                                return header12;
281

    
282
                        case LidarHeader.BIN20010712:
283

    
284
                                BINHeader headerBin2001 = new BINHeader(file);
285
                                headerBin2001.readLidarHeader();
286
                                return headerBin2001;
287

    
288
                        case LidarHeader.BIN20020715:
289
                                BINHeader headerBin2002 = new BINHeader(file);
290
                                headerBin2002.readLidarHeader();
291
                                return headerBin2002;
292
                }
293

    
294
                return null;
295
        }
296
}