laswriter.cpp
Go to the documentation of this file.
1 /*
2 ===============================================================================
3 
4  FILE: laswriter.cpp
5 
6  CONTENTS:
7 
8  see corresponding header file
9 
10  PROGRAMMERS:
11 
12  martin.isenburg@gmail.com
13 
14  COPYRIGHT:
15 
16  (c) 2007-2011, Martin Isenburg, LASSO - tools to catch reality
17 
18  This is free software; you can redistribute and/or modify it under the
19  terms of the GNU Lesser General Licence as published by the Free Software
20  Foundation. See the COPYING file for more information.
21 
22  This software is distributed WITHOUT ANY WARRANTY and without even the
23  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
24 
25  CHANGE HISTORY:
26 
27  see corresponding header file
28 
29 ===============================================================================
30 */
31 #include "laswriter.hpp"
32 
33 #include "laswriter_las.hpp"
34 #include "laswriter_bin.hpp"
35 #include "laswriter_qfit.hpp"
36 #include "laswriter_txt.hpp"
37 
38 #include <stdlib.h>
39 #include <string.h>
40 
42 {
43  return ((file_name == 0) && use_stdout);
44 }
45 
47 {
48  if (use_nil)
49  {
50  LASwriterLAS* laswriterlas = new LASwriterLAS();
52  {
53  fprintf(stderr,"ERROR: cannot open laswriterlas to NULL\n");
54  delete laswriterlas;
55  return 0;
56  }
57  return laswriterlas;
58  }
59  else if (file_name)
60  {
62  {
63  LASwriterLAS* laswriterlas = new LASwriterLAS();
65  {
66  fprintf(stderr,"ERROR: cannot open laswriterlas with file name '%s'\n", file_name);
67  delete laswriterlas;
68  return 0;
69  }
70  return laswriterlas;
71  }
72  else if (format == LAS_TOOLS_FORMAT_TXT)
73  {
74  LASwriterTXT* laswritertxt = new LASwriterTXT();
75  if (!laswritertxt->open(file_name, header, parse_string, separator))
76  {
77  fprintf(stderr,"ERROR: cannot open laswritertxt with file name '%s'\n", file_name);
78  delete laswritertxt;
79  return 0;
80  }
81  return laswritertxt;
82  }
83  else if (format == LAS_TOOLS_FORMAT_BIN)
84  {
85  LASwriterBIN* laswriterbin = new LASwriterBIN();
86  if (!laswriterbin->open(file_name, header, "ts8"))
87  {
88  fprintf(stderr,"ERROR: cannot open laswriterbin with file name '%s'\n", file_name);
89  delete laswriterbin;
90  return 0;
91  }
92  return laswriterbin;
93  }
94  else if (format == LAS_TOOLS_FORMAT_QFIT)
95  {
96  LASwriterQFIT* laswriterqfit = new LASwriterQFIT();
97  if (!laswriterqfit->open(file_name, header, 40))
98  {
99  fprintf(stderr,"ERROR: cannot open laswriterqfit with file name '%s'\n", file_name);
100  delete laswriterqfit;
101  return 0;
102  }
103  return laswriterqfit;
104  }
105  else
106  {
107  fprintf(stderr,"ERROR: unknown format %d\n", format);
108  return 0;
109  }
110  }
111  else if (use_stdout)
112  {
114  {
115  LASwriterLAS* laswriterlas = new LASwriterLAS();
117  {
118  fprintf(stderr,"ERROR: cannot open laswriterlas to stdout\n");
119  delete laswriterlas;
120  return 0;
121  }
122  return laswriterlas;
123  }
124  else if (format == LAS_TOOLS_FORMAT_TXT)
125  {
126  LASwriterTXT* laswritertxt = new LASwriterTXT();
127  if (!laswritertxt->open(stdout, header, parse_string, separator))
128  {
129  fprintf(stderr,"ERROR: cannot open laswritertxt to stdout\n");
130  delete laswritertxt;
131  return 0;
132  }
133  return laswritertxt;
134  }
135  else if (format == LAS_TOOLS_FORMAT_BIN)
136  {
137  LASwriterBIN* laswriterbin = new LASwriterBIN();
138  if (!laswriterbin->open(stdout, header, "ts8"))
139  {
140  fprintf(stderr,"ERROR: cannot open laswriterbin to stdout\n");
141  delete laswriterbin;
142  return 0;
143  }
144  return laswriterbin;
145  }
146  else if (format == LAS_TOOLS_FORMAT_QFIT)
147  {
148  LASwriterQFIT* laswriterqfit = new LASwriterQFIT();
149  if (!laswriterqfit->open(stdout, header, 40))
150  {
151  fprintf(stderr,"ERROR: cannot open laswriterbin to stdout\n");
152  delete laswriterqfit;
153  return 0;
154  }
155  return laswriterqfit;
156  }
157  else
158  {
159  fprintf(stderr,"ERROR: unknown format %d\n", format);
160  return 0;
161  }
162  }
163  else
164  {
165  fprintf(stderr,"ERROR: no laswriter output specified\n");
166  return 0;
167  }
168 }
169 
171 {
172  if (lasheader->point_data_format < 4) return 0;
173  if (lasheader->vlr_wave_packet_descr == 0) return 0;
174  if (get_file_name() == 0) return 0;
175  LASwaveform13writer* waveform13writer = new LASwaveform13writer();
176  if (waveform13writer->open(get_file_name(), lasheader->vlr_wave_packet_descr))
177  {
178  return waveform13writer;
179  }
180  delete waveform13writer;
181  return 0;
182 }
183 
185 {
186  fprintf(stderr,"Supported LAS Outputs\n");
187  fprintf(stderr," -o lidar.las\n");
188  fprintf(stderr," -o lidar.laz\n");
189  fprintf(stderr," -o xyzta.txt -oparse xyzta (on-the-fly to ASCII)\n");
190  fprintf(stderr," -o terrasolid.bin\n");
191  fprintf(stderr," -o nasa.qi\n");
192  fprintf(stderr," -olas -olaz -otxt -obin -oqfit (specify format)\n");
193  fprintf(stderr," -stdout (pipe to stdout)\n");
194  fprintf(stderr," -nil (pipe to NULL)\n");
195 }
196 
198 {
199  int i;
200  for (i = 1; i < argc; i++)
201  {
202  if (argv[i][0] == '\0')
203  {
204  continue;
205  }
206  else if (strcmp(argv[i],"-h") == 0 || strcmp(argv[i],"-help") == 0)
207  {
208  usage();
209  return TRUE;
210  }
211  else if (strcmp(argv[i],"-o") == 0)
212  {
213  if ((i+1) >= argc)
214  {
215  fprintf(stderr,"ERROR: '%s' needs 1 argument: file_name\n", argv[i]);
216  return FALSE;
217  }
218  set_file_name(argv[i+1]);
219  *argv[i]='\0'; *argv[i+1]='\0'; i+=1;
220  }
221  else if (strcmp(argv[i],"-olas") == 0)
222  {
224  *argv[i]='\0';
225  }
226  else if (strcmp(argv[i],"-olaz") == 0)
227  {
229  *argv[i]='\0';
230  }
231  else if (strcmp(argv[i],"-otxt") == 0)
232  {
234  *argv[i]='\0';
235  }
236  else if (strcmp(argv[i],"-obin") == 0)
237  {
239  *argv[i]='\0';
240  }
241  else if (strcmp(argv[i],"-oqi") == 0 || strcmp(argv[i],"-oqfit") == 0)
242  {
244  *argv[i]='\0';
245  }
246  else if (strcmp(argv[i],"-stdout") == 0)
247  {
248  use_stdout = TRUE;
249  use_nil = FALSE;
250  *argv[i]='\0';
251  }
252  else if (strcmp(argv[i],"-nil") == 0)
253  {
254  use_nil = TRUE;
255  use_stdout = FALSE;
256  *argv[i]='\0';
257  }
258  else if (strcmp(argv[i],"-v1") == 0)
259  {
260  use_v1 = TRUE;
262  *argv[i]='\0';
263  }
264  else if (strcmp(argv[i],"-v2") == 0)
265  {
266  use_v1 = FALSE;
267  use_chunking = TRUE;
268  *argv[i]='\0';
269  }
270  else if (strcmp(argv[i],"-no_chunk") == 0)
271  {
273  *argv[i]='\0';
274  }
275  else if (strcmp(argv[i],"-chunk") == 0)
276  {
277  use_chunking = TRUE;
278  *argv[i]='\0';
279  }
280  else if (strcmp(argv[i],"-chunk_size") == 0)
281  {
282  if ((i+1) >= argc)
283  {
284  fprintf(stderr,"ERROR: '%s' needs 1 argument: number_points\n", argv[i]);
285  return FALSE;
286  }
287  use_chunking = TRUE;
288  chunk_size = atoi(argv[i+1]);
289  *argv[i]='\0'; *argv[i+1]='\0'; i+=1;
290  }
291  else if (strcmp(argv[i],"-oparse") == 0)
292  {
293  if ((i+1) >= argc)
294  {
295  fprintf(stderr,"ERROR: '%s' needs 1 argument: string\n", argv[i]);
296  return FALSE;
297  }
298  set_parse_string(argv[i+1]);
299  *argv[i]='\0'; *argv[i+1]='\0'; i+=1;
300  }
301  else if (strcmp(argv[i],"-sep") == 0)
302  {
303  if ((i+1) >= argc)
304  {
305  fprintf(stderr,"ERROR: '%s' needs 1 argument: separator\n", argv[i]);
306  return FALSE;
307  }
308  set_separator(argv[i+1]);
309  *argv[i]='\0'; *argv[i+1]='\0'; i+=1;
310  }
311  }
312  return TRUE;
313 }
314 
315 void LASwriteOpener::set_file_name(const char* file_name)
316 {
317  if (this->file_name) free(this->file_name);
318  if (file_name)
319  {
320  this->file_name = strdup(file_name);
321  if (strstr(file_name, ".laz") || strstr(file_name, ".LAZ"))
322  {
324  }
325  else if (strstr(file_name, ".las") || strstr(file_name, ".LAS"))
326  {
328  }
329  else if (strstr(file_name, ".bin") || strstr(file_name, ".BIN")) // terrasolid
330  {
332  }
333  else if (strstr(file_name, ".qi") || strstr(file_name, ".QI")) // QFIT
334  {
336  }
337  else // assume ascii output
338  {
340  }
341  }
342  else
343  {
344  this->file_name = 0;
345  }
346 }
347 
348 void LASwriteOpener::set_format(const char* format)
349 {
350  if (format)
351  {
352  if (strstr(format, "laz") || strstr(format, "LAZ"))
353  {
354  this->format = LAS_TOOLS_FORMAT_LAZ;
355  }
356  else if (strstr(format, "las") || strstr(format, "LAS"))
357  {
358  this->format = LAS_TOOLS_FORMAT_LAS;
359  }
360  else if (strstr(format, "bin") || strstr(format, "BIN")) // terrasolid
361  {
362  this->format = LAS_TOOLS_FORMAT_BIN;
363  }
364  else if (strstr(format, "qi") || strstr(format, "QI")) // QFIT
365  {
366  this->format = LAS_TOOLS_FORMAT_QFIT;
367  }
368  else // assume ascii output
369  {
370  this->format = LAS_TOOLS_FORMAT_TXT;
371  }
372  }
373  else
374  {
375  this->format = LAS_TOOLS_FORMAT_DEFAULT;
376  }
377 }
378 
379 void LASwriteOpener::make_file_name(const char* file_name, I32 file_number)
380 {
381  size_t len;
382  if (file_number > -1)
383  {
384  if (file_name)
385  {
386  if (this->file_name) free(this->file_name);
387  len = strlen(file_name);
388  this->file_name = (char*)malloc(len+10);
389  strcpy(this->file_name, file_name);
390  }
391  else
392  {
393  len = strlen(this->file_name);
394  }
395  while (len > 0 && this->file_name[len] != '.') len--;
396  len++;
397  int num = len - 2;
398  while (num > 0 && this->file_name[num] >= '0' && this->file_name[num] <= '9')
399  {
400  this->file_name[num] = '0' + (file_number%10);
401  file_number = file_number/10;
402  num--;
403  }
404  }
405  else
406  {
407  if (this->file_name) free(this->file_name);
408  if (file_name)
409  {
410  len = strlen(file_name);
411  this->file_name = (char*)malloc(len+10);
412  strcpy(this->file_name, file_name);
413  while (len > 0 && this->file_name[len] != '.') len--;
414  if (file_number == -1)
415  {
416  this->file_name[len] = '_';
417  this->file_name[len+1] = '1';
418  this->file_name[len+2] = '.';
419  len += 3;
420  }
421  else
422  {
423  len++;
424  }
425  }
426  else
427  {
428  len = 7;
429  this->file_name = strdup("output.xxx");
430  }
431  }
433  {
434  this->file_name[len] = 'l';
435  this->file_name[len+1] = 'a';
436  this->file_name[len+2] = 's';
437  }
438  else if (format == LAS_TOOLS_FORMAT_LAZ)
439  {
440  this->file_name[len] = 'l';
441  this->file_name[len+1] = 'a';
442  this->file_name[len+2] = 'z';
443  }
444  else if (format == LAS_TOOLS_FORMAT_BIN)
445  {
446  this->file_name[len] = 'b';
447  this->file_name[len+1] = 'i';
448  this->file_name[len+2] = 'n';
449  }
450  else // if (format == LAS_TOOLS_FORMAT_TXT)
451  {
452  this->file_name[len] = 't';
453  this->file_name[len+1] = 'x';
454  this->file_name[len+2] = 't';
455  }
456  this->file_name[len+3] = '\0';
457 }
458 
459 const char* LASwriteOpener::get_file_name() const
460 {
461  return file_name;
462 }
463 
465 {
466  return (format != LAS_TOOLS_FORMAT_DEFAULT);
467 }
468 
470 {
471  return format;
472 }
473 
474 void LASwriteOpener::set_parse_string(const char* parse_string)
475 {
476  if (this->parse_string) free(this->parse_string);
477  this->parse_string = strdup(parse_string);
478 }
479 
480 void LASwriteOpener::set_separator(const char* separator)
481 {
482  if (this->separator) free(this->separator);
483  this->separator = strdup(separator);
484 }
485 
487 {
488  return (file_name != 0 || use_stdout || use_nil);
489 }
490 
492 {
493  file_name = 0;
494  parse_string = 0;
495  separator = 0;
498  use_chunking = TRUE;
499  use_stdout = FALSE;
500  use_nil = FALSE;
501  use_v1 = FALSE;
502 }
503 
505 {
506  if (file_name) free(file_name);
507  if (parse_string) free(parse_string);
508  if (separator) free(separator);
509 }
LASheader
Definition: lasdefinitions.hpp:952
LASZIP_COMPRESSOR_CHUNKED
#define LASZIP_COMPRESSOR_CHUNKED
Definition: laszip.hpp:58
LASwriteOpener::separator
char * separator
Definition: laswriter.hpp:90
LASwriterLAS
Definition: laswriter_las.hpp:55
LASZIP_COMPRESSOR_NONE
#define LASZIP_COMPRESSOR_NONE
Definition: laszip.hpp:53
LASwriteOpener::file_name
char * file_name
Definition: laswriter.hpp:88
LAS_TOOLS_FORMAT_QFIT
#define LAS_TOOLS_FORMAT_QFIT
Definition: lasdefinitions.hpp:62
LASwriteOpener::LASwriteOpener
LASwriteOpener()
Definition: laswriter.cpp:491
LASwriteOpener::chunk_size
U32 chunk_size
Definition: laswriter.hpp:92
LASwriteOpener::piped
BOOL piped() const
Definition: laswriter.cpp:41
LASwriteOpener::use_chunking
BOOL use_chunking
Definition: laswriter.hpp:93
LAS_TOOLS_FORMAT_LAS
#define LAS_TOOLS_FORMAT_LAS
Definition: lasdefinitions.hpp:58
I32
int I32
Definition: mydefs.hpp:35
laswriter_txt.hpp
TRUE
#define TRUE
Definition: mydefs.hpp:137
LASwriterQFIT::open
BOOL open(const char *file_name, const LASheader *header, I32 version=48, U32 io_buffer_size=65536)
Definition: laswriter_qfit.cpp:50
LASwriter
Definition: laswriter.hpp:47
LAS_TOOLS_FORMAT_BIN
#define LAS_TOOLS_FORMAT_BIN
Definition: lasdefinitions.hpp:60
laswriter.hpp
LASwriteOpener::make_file_name
void make_file_name(const char *file_name, I32 file_number=-1)
Definition: laswriter.cpp:379
LASwriteOpener::parse
BOOL parse(int argc, char *argv[])
Definition: laswriter.cpp:197
LASheader::point_data_format
U8 point_data_format
Definition: lasdefinitions.hpp:971
LASwriterBIN
Definition: laswriter_bin.hpp:41
LASwriteOpener::~LASwriteOpener
~LASwriteOpener()
Definition: laswriter.cpp:504
LASwaveform13writer::open
BOOL open(const char *file_name, const LASvlr_wave_packet_descr *const *wave_packet_descr)
Definition: laswaveform13writer.cpp:72
LASwriteOpener::set_format
void set_format(const char *format)
Definition: laswriter.cpp:348
LASwriterQFIT
Definition: laswriter_qfit.hpp:40
LASwriteOpener::use_nil
BOOL use_nil
Definition: laswriter.hpp:95
LASwriteOpener::open_waveform13
LASwaveform13writer * open_waveform13(const LASheader *lasheader)
Definition: laswriter.cpp:170
LASwriteOpener::format_was_specified
BOOL format_was_specified() const
Definition: laswriter.cpp:464
LASwriteOpener::active
BOOL active() const
Definition: laswriter.cpp:486
laswriter_bin.hpp
LASwriteOpener::set_parse_string
void set_parse_string(const char *parse_string)
Definition: laswriter.cpp:474
LASwriteOpener::set_file_name
void set_file_name(const char *file_name)
Definition: laswriter.cpp:315
laswriter_qfit.hpp
BOOL
int BOOL
Definition: mydefs.hpp:57
LASheader::vlr_wave_packet_descr
LASvlr_wave_packet_descr ** vlr_wave_packet_descr
Definition: lasdefinitions.hpp:1000
LASwriterBIN::open
BOOL open(const char *file_name, const LASheader *header, const char *version, U32 io_buffer_size=65536)
Definition: laswriter_bin.cpp:88
LAS_TOOLS_FORMAT_DEFAULT
#define LAS_TOOLS_FORMAT_DEFAULT
Definition: lasdefinitions.hpp:57
LAS_TOOLS_FORMAT_TXT
#define LAS_TOOLS_FORMAT_TXT
Definition: lasdefinitions.hpp:63
argc
int argc
Definition: tests_high_five_parallel.cpp:27
FALSE
#define FALSE
Definition: mydefs.hpp:133
LASwriteOpener::format
U32 format
Definition: laswriter.hpp:91
LASwriteOpener::get_format
I32 get_format() const
Definition: laswriter.cpp:469
LASwaveform13writer
Definition: laswaveform13writer.hpp:42
LASwriterTXT
Definition: laswriter_txt.hpp:38
LASwriterLAS::open
BOOL open(const LASheader *header, U32 compressor=LASZIP_COMPRESSOR_NONE, I32 requested_version=0, I32 chunk_size=50000)
Definition: laswriter_las.cpp:53
LASwriteOpener::parse_string
char * parse_string
Definition: laswriter.hpp:89
LAS_TOOLS_FORMAT_LAZ
#define LAS_TOOLS_FORMAT_LAZ
Definition: lasdefinitions.hpp:59
LASZIP_CHUNK_SIZE_DEFAULT
#define LASZIP_CHUNK_SIZE_DEFAULT
Definition: laszip.hpp:66
laswriter_las.hpp
LASwriteOpener::set_separator
void set_separator(const char *separator)
Definition: laswriter.cpp:480
LASwriteOpener::usage
void usage() const
Definition: laswriter.cpp:184
LASwriteOpener::open
LASwriter * open(LASheader *header)
Definition: laswriter.cpp:46
LASwriterTXT::open
BOOL open(const char *file_name, const LASheader *header, const char *parse_string=0, const char *separator=0)
Definition: laswriter_txt.cpp:42
LASwriteOpener::get_file_name
const char * get_file_name() const
Definition: laswriter.cpp:459
argv
char ** argv
Definition: tests_high_five_parallel.cpp:28
LASwriteOpener::use_v1
BOOL use_v1
Definition: laswriter.hpp:96
LASZIP_COMPRESSOR_NOT_CHUNKED
#define LASZIP_COMPRESSOR_NOT_CHUNKED
Definition: laszip.hpp:59
LASwriteOpener::use_stdout
BOOL use_stdout
Definition: laswriter.hpp:94


lvr2
Author(s): Thomas Wiemann , Sebastian Pütz , Alexander Mock , Lars Kiesow , Lukas Kalbertodt , Tristan Igelbrink , Johan M. von Behren , Dominik Feldschnieders , Alexander Löhr
autogenerated on Wed Mar 2 2022 00:37:23