Statistics
| Revision:

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

History | View | Annotate | Download (6.69 KB)

1
/*******************************************************************************
2
InicializeLidar.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.File;
23
import java.io.FileInputStream;
24
import java.io.FileNotFoundException;
25
import java.io.IOException;
26
import java.io.InputStream;
27

    
28
import javax.swing.JOptionPane;
29
import com.iver.andami.PluginServices;
30

    
31

    
32
/**
33
 * Inicizliza Lidar points and header
34
 *
35
 * @author Oscar Garcia
36
 */
37
public class InicializeLidar {
38

    
39
        public static int testLASFormat(File name) {
40
                
41
                File f = name;
42
                InputStream input;
43
                int offset = 0,numRead = 0;
44
                char[] FileSignatureLASF = new char[4];
45
                char  versionMajor, versionMinor, format;
46
                byte[] cabecera = new byte[105];
47
                
48
                try {
49
                      
50
                    input = new FileInputStream(f);
51
                        while (offset < cabecera.length && (numRead = input.read(cabecera, offset, cabecera.length-offset) ) >= 0) {
52
                             offset += numRead;
53
                        }
54
                        
55
                    if (offset < cabecera.length) {
56
                            JOptionPane.showMessageDialog(null, PluginServices.getText(null, "bad_input_format"));
57
                        }
58
                   
59
                    FileSignatureLASF[0] = (char)(cabecera[0] & 0xFF);
60
                    FileSignatureLASF[1] = (char)(cabecera[1] & 0xFF);
61
                    FileSignatureLASF[2] = (char)(cabecera[2] & 0xFF);
62
                    FileSignatureLASF[3] = (char)(cabecera[3] & 0xFF);
63
                    
64
                    if(FileSignatureLASF[0] == 'L'&& FileSignatureLASF[1] == 'A'&& FileSignatureLASF[2] == 'S' && FileSignatureLASF[3] == 'F') {
65
                            
66
                            if (offset < cabecera.length) {
67
                                    JOptionPane.showMessageDialog(null, PluginServices.getText(null, "bad_input_format"));
68
                                }
69
                            
70
                            versionMajor = (char)(cabecera[24] & 0xFF);
71
                            versionMinor = (char)(cabecera[25] & 0xFF);
72
                            format = (char)(cabecera[104] & 0xFF);
73
                        
74
                            if(versionMajor == 1 && versionMinor == 0) {
75
                                    
76
                                    if(format == 0)
77
                                            return LidarHeader.LAS10F0;
78
                                    else if(format == 1)
79
                                            return LidarHeader.LAS10F1;
80
                            } else if(versionMajor == 1 && versionMinor == 1) {
81
                                    
82
                                    if(format == 0)
83
                                            return LidarHeader.LAS11F0;
84
                                    else if(format == 1)
85
                                            return LidarHeader.LAS11F1;
86
                            } else {
87
                                    throw new UnexpectedLidarHeaderException(PluginServices.getText(null, "Cabecera_inesperada"));
88
                            }
89
                    }
90
                    
91
                } catch(UnexpectedLidarHeaderException e) {
92
                        e.printStackTrace();                        
93
                } catch (FileNotFoundException e) {
94
                        // TODO Auto-generated catch block
95
                        e.printStackTrace();
96
                } catch (IOException e) {
97
                        // TODO Auto-generated catch block
98
                        e.printStackTrace();
99
                }
100
                
101
                return LidarHeader.UNEXPECTED;
102
        }
103
        
104
        public static int testBINFormat(File name) {
105
                
106
                File f = name;
107
                InputStream input;
108
                int offset = 0,numRead = 0;
109
                byte[] cabecera = new byte[8];
110
                
111
                try {
112
                      
113
                    input = new FileInputStream(f);
114
                        while (offset < cabecera.length && (numRead = input.read(cabecera, offset, cabecera.length-offset) ) >= 0) {
115
                             offset += numRead;
116
                        }
117
                        
118
                    if (offset < cabecera.length) {
119
                            JOptionPane.showMessageDialog(null, PluginServices.getText(null, "bad_input_format"));
120
                        }
121
                 
122
                    
123
                    int HdrVersionBin; 
124
                    HdrVersionBin = ByteUtilities.arr2int(cabecera, 4);
125
                    if(HdrVersionBin == 20020715) {
126
                            
127
                            return LidarHeader.BIN20020715;
128
                    }
129
                    else if( HdrVersionBin == 20010712) {
130
                            
131
                            return LidarHeader.BIN20010712;
132
                    } //  ( HdrVersionBin == 20010129 || HdrVersionBin == 970404)        
133
                
134
                } catch (FileNotFoundException e) {
135
                        // TODO Auto-generated catch block
136
                        e.printStackTrace();
137
                } catch (IOException e) {
138
                        // TODO Auto-generated catch block
139
                        e.printStackTrace();
140
                }
141
                
142
                return LidarHeader.UNEXPECTED;
143
        }
144
        
145
        public static int testFormat(File name) {
146
                
147
                int f = testLASFormat(name);
148
                if(f != LidarHeader.UNEXPECTED) {
149
                        return f;
150
                }
151
                
152
                f = testBINFormat(name);
153
                if(f != LidarHeader.UNEXPECTED) {
154
                        return f;
155
                }
156
                
157
                return LidarHeader.UNEXPECTED;
158
                
159
        }
160
        
161
        public static LidarPoint InizializeLidarPoint(File file) {
162
                boolean binColor, binTime;
163
                
164
                int type = testFormat(file);
165
                switch(type) {
166
                
167
                        case LidarHeader.LAS10F0:
168
                                return new LASPoint10F0();
169
                                
170
                        case LidarHeader.LAS10F1:
171
                                return new LASPoint10F1();
172
                                
173
                        case LidarHeader.LAS11F0:
174
                                return new LASPoint11F0();
175
                                
176
                        case LidarHeader.LAS11F1:
177
                                return new LASPoint11F1();
178
                                
179
                        case LidarHeader.BIN20010712:
180
                                
181
                                BINHeader headerBin2001 = new BINHeader(file);
182
                                headerBin2001.readLidarHeader();
183
                                
184
                                binColor = false;
185
                                binTime = false;
186

    
187
                                
188
                                if(headerBin2001.getColor()>0)
189
                                        binColor = true;
190
                                        
191
                                if(headerBin2001.getTime()>0)
192
                                        binTime = true;
193
                                
194
                                return new BINPoint2001(binColor, binTime);
195
                                
196
                        case LidarHeader.BIN20020715:
197
                                BINHeader headerBin2002 = new BINHeader(file);
198
                                headerBin2002.readLidarHeader();
199
                                
200
                                binColor = false;
201
                                binTime = false;
202

    
203
                                
204
                                if(headerBin2002.getColor()>0)
205
                                        binColor = true;
206
                                        
207
                                if(headerBin2002.getTime()>0)
208
                                        binTime = true;
209
                                
210
                                return new BINPoint2002(binColor, binTime);
211
                }
212
                
213
                return null;
214
        }
215
        
216
        
217
        public static LidarHeader InizializeLidarHeader(File file) {
218
                int type = testFormat(file);
219
                switch(type) {
220
                
221
                        case LidarHeader.LAS10F0:
222
                        case LidarHeader.LAS10F1:
223
                                LASHeader_V10 header10 = new LASHeader_V10(file);
224
                                header10.readLidarHeader();
225
                                return header10;
226
                                
227
                        case LidarHeader.LAS11F0:
228
                        case LidarHeader.LAS11F1:
229
                                LASHeader_V11 header11 = new LASHeader_V11(file);
230
                                header11.readLidarHeader();
231
                                return header11;
232
                                
233
                        case LidarHeader.BIN20010712:
234
                                
235
                                BINHeader headerBin2001 = new BINHeader(file);
236
                                headerBin2001.readLidarHeader();
237
                                return headerBin2001;
238
                                
239
                        case LidarHeader.BIN20020715:
240
                                BINHeader headerBin2002 = new BINHeader(file);
241
                                headerBin2002.readLidarHeader();        
242
                                return headerBin2002;
243
                }
244
                
245
                return null;
246
        }
247
}