PageRenderTime 60ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/Utilities/otbliblas/apps/las2las-old.c

https://bitbucket.org/olahlou/otb
C | 1163 lines | 1001 code | 132 blank | 30 comment | 351 complexity | 3df17769536cdb9e29cee7e792697970 MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0, LGPL-2.0, CC-BY-SA-3.0, BSD-3-Clause, LGPL-2.1
  1. /***************************************************************************
  2. * $Id$
  3. * $Date$
  4. *
  5. * Project: libLAS -- C/C++ read/write library for LAS LIDAR data
  6. * Purpose: LAS translation with optional configuration
  7. * Author: Martin Isenburg isenburg@cs.unc.edu
  8. ***************************************************************************
  9. * Copyright (c) 2007, Martin Isenburg isenburg@cs.unc.edu
  10. *
  11. * See LICENSE.txt in this source distribution for more information.
  12. **************************************************************************/
  13. #include "liblas.h"
  14. /* std */
  15. #include <assert.h>
  16. #include <time.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #ifdef _WIN32
  21. #define compare_no_case(a,b,n) _strnicmp( (a), (b), (n) )
  22. #else
  23. #define compare_no_case(a,b,n) strncasecmp( (a), (b), (n) )
  24. #endif
  25. LASPointSummary* SummarizePoints(LASReaderH reader);
  26. void print_point_summary(FILE *file, LASPointSummary* summary, LASHeaderH header);
  27. void print_point(FILE *file, LASPointH point);
  28. void print_header(FILE *file, LASHeaderH header, const char* file_name, int bSkipVLR);
  29. void repair_header(FILE *file, LASHeaderH header, LASPointSummary* summary) ;
  30. #define LAS_FORMAT_10 0
  31. #define LAS_FORMAT_11 1
  32. #define LAS_FORMAT_12 2
  33. void do_bulk_copy(const char* infile, size_t in_start_point, const char* outfile)
  34. {
  35. /* bulk copy assumes that the header has already been written to outfile
  36. as it is supposed to be, and that we're just going to copy all of the
  37. points in infile as they are.
  38. */
  39. FILE* file_out = 0;
  40. FILE* file_in = 0;
  41. size_t read = 0;
  42. size_t written = 0;
  43. size_t size = 1000;
  44. char *buffer = 0;
  45. buffer = (char*) malloc(size * sizeof(char));
  46. if (buffer == 0) {
  47. LASError_Print("unable to allocate buffer copy");
  48. exit(1);
  49. }
  50. file_in = fopen(infile, "rb");
  51. fseek(file_in, in_start_point, SEEK_SET);
  52. if (file_in == 0) {
  53. LASError_Print("input filename not valid for bulk copy");
  54. exit(1);
  55. }
  56. file_out = fopen(outfile, "ab+");
  57. if (file_out == 0) {
  58. LASError_Print("output filename not valid for bulk copy");
  59. exit(1);
  60. }
  61. while (feof(file_in) == 0) {
  62. read = fread(buffer, 1, size, file_in);
  63. written = fwrite(buffer, 1, read, file_out);
  64. if (read != written) {
  65. LASError_Print("unable to write data in bulk copy");
  66. exit(1);
  67. }
  68. }
  69. fclose(file_in);
  70. fclose(file_out);
  71. free(buffer);
  72. }
  73. void usage()
  74. {
  75. fprintf(stderr,"----------------------------------------------------------\n");
  76. fprintf(stderr," las2las-old (version %s) usage:\n", LAS_GetFullVersion());
  77. fprintf(stderr,"----------------------------------------------------------\n");
  78. fprintf(stderr,"\n");
  79. fprintf(stderr,"Clip las file to a bounding box, throwing out points that have invalid data:\n");
  80. fprintf(stderr," las2las-old -i in.las -clip 63000000 483450000 63050000 483500000 -o out.las --skip_invalid\n");
  81. fprintf(stderr,"\n");
  82. fprintf(stderr,"Read from stdin, eliminate intensities below 2000, and write to stdout:\n");
  83. fprintf(stderr," las2las-old --eliminate_intensity_below 2000 --stdin --stdout < in.las > out.las \n");
  84. fprintf(stderr,"\n");
  85. fprintf(stderr,"Eliminate return number two:\n");
  86. fprintf(stderr," las2las-old -i in.las -eliminate_return 2 -o out.las\n");
  87. fprintf(stderr,"\n");
  88. fprintf(stderr,"Eliminate scan angles above 15:\n");
  89. fprintf(stderr," las2las-old -i in.las -eliminate_scan_angle_above 15 -o out.las\n");
  90. fprintf(stderr,"\n");
  91. fprintf(stderr,"Eliminate intensities below 1000:\n");
  92. fprintf(stderr," las2las-old -i in.las -eliminate_intensity_below 1000 --stdout > out.las\n");
  93. fprintf(stderr,"\n");
  94. fprintf(stderr,"Eliminate intensities below 1000 and classifications that equal 2 (ground):\n");
  95. fprintf(stderr," las2las-old -i in.las --eliminate_class 2 --out out.las\n");
  96. fprintf(stderr,"\n");
  97. fprintf(stderr,"Capture only first returns and clip to bounding box:\n");
  98. fprintf(stderr," las2las-old -i in.las -first_only -clip 63000000 483450000 63050000 483500000 -o out.las\n");
  99. fprintf(stderr,"\n");
  100. fprintf(stderr,"Capture only last returns, eliminate intensities below 2000, and write to stdout:\n");
  101. fprintf(stderr," las2las-old --input in.las --last_only --eliminate_intensity_below 2000 --stdout > out.las\n");
  102. fprintf(stderr,"\n");
  103. fprintf(stderr,"Reproject the data (requires GDAL support):\n");
  104. fprintf(stderr," las2las-old -v in.las output.las -xyz_offset 0 0 0 "
  105. "-t_srs EPSG:4326 --xyz_scale 0.0000001 0.0000001 0.0000001"
  106. "-s_srs EPSG:26915\n");
  107. fprintf(stderr,"\n");
  108. fprintf(stderr, "\nFor more information, see the full documentation for las2las-old at:\n"
  109. " http://liblas.org/utilities/las2las-old.html\n");
  110. fprintf(stderr,"----------------------------------------------------------\n");
  111. }
  112. void ptime(const char *const msg)
  113. {
  114. float t= ((float)clock())/CLOCKS_PER_SEC;
  115. fprintf(stderr, "cumulative CPU time thru %s = %f\n", msg, t);
  116. }
  117. int main(int argc, char *argv[])
  118. {
  119. int i;
  120. int verbose = FALSE;
  121. int use_stdin = FALSE;
  122. int use_stdout = FALSE;
  123. char* file_name_in = 0;
  124. char* file_name_out = 0;
  125. char* proj4_text = NULL;
  126. double *clip_xy_min = NULL;
  127. double *clip_xy_max = NULL;
  128. int clip = FALSE;
  129. int remove_extra_header = FALSE;
  130. int elim_return = 0;
  131. int elim_scan_angle_above = 0;
  132. int elim_intensity_below = 0;
  133. int elim_class = 0;
  134. int clsidx = 0;
  135. int first_only = FALSE;
  136. int last_only = FALSE;
  137. int skip_invalid = FALSE;
  138. int format = LAS_FORMAT_12;
  139. int bulk_copy = FALSE;
  140. LASReaderH reader = NULL;
  141. LASHeaderH header = NULL;
  142. LASHeaderH surviving_header = NULL;
  143. LASPointH p = NULL;
  144. LASWriterH writer = NULL;
  145. LASSRSH in_srs = NULL;
  146. LASSRSH out_srs = NULL;
  147. int use_min_offset = FALSE;
  148. int do_reprojection = FALSE;
  149. int do_set_offset = FALSE;
  150. int do_set_scale = FALSE;
  151. int do_pad_header = FALSE;
  152. int header_pad = 0;
  153. int first_surviving_point = TRUE;
  154. unsigned int surviving_number_of_point_records = 0;
  155. unsigned int surviving_number_of_points_by_return[] = {0,0,0,0,0,0,0,0};
  156. LASPointH surviving_point_min = NULL;
  157. LASPointH surviving_point_max = NULL;
  158. double surviving_gps_time_min;
  159. double surviving_gps_time_max;
  160. int verticalCSType = -1, verticalDatum = -1, verticalUnits = 9001;
  161. const char *verticalCitation = "";
  162. int clipped = 0;
  163. int eliminated_return = 0;
  164. int eliminated_scan_angle = 0;
  165. int eliminated_intensity = 0;
  166. int eliminated_class = 0;
  167. int eliminated_first_only = 0;
  168. int eliminated_last_only = 0;
  169. int thinned = 0;
  170. int bThin = FALSE;
  171. int nThin = 0;
  172. double xyz_scale[3] = {0.0, 0.0, 0.0};
  173. double xyz_offset[3] = {0.0, 0.0, 0.0};
  174. double minx, maxx, miny, maxy, minz, maxz;
  175. LASPointSummary* summary = NULL;
  176. int ret = 0;
  177. for (i = 1; i < argc; i++) {
  178. if ( strcmp(argv[i],"-h") == 0 ||
  179. strcmp(argv[i],"--help") == 0
  180. )
  181. {
  182. usage();
  183. exit(0);
  184. }
  185. else if ( strcmp(argv[i],"-v") == 0 ||
  186. strcmp(argv[i],"--verbose") == 0
  187. )
  188. {
  189. verbose = TRUE;
  190. }
  191. else if ( strcmp(argv[i],"-s") == 0 ||
  192. strcmp(argv[i],"--skip_invalid") == 0
  193. )
  194. {
  195. skip_invalid = TRUE;
  196. }
  197. else if ( strcmp(argv[i],"-b") == 0 ||
  198. strcmp(argv[i],"--bulk") == 0
  199. )
  200. {
  201. bulk_copy = TRUE;
  202. }
  203. else if ( strcmp(argv[i],"--input") == 0 ||
  204. strcmp(argv[i],"-input") == 0 ||
  205. strcmp(argv[i],"-i") == 0 ||
  206. strcmp(argv[i],"-in") == 0
  207. )
  208. {
  209. i++;
  210. file_name_in = argv[i];
  211. }
  212. else if ( strcmp(argv[i],"--output") == 0 ||
  213. strcmp(argv[i],"--out") == 0 ||
  214. strcmp(argv[i],"-out") == 0 ||
  215. strcmp(argv[i],"-o") == 0
  216. )
  217. {
  218. i++;
  219. file_name_out = argv[i];
  220. }
  221. else if ( strcmp(argv[i],"--stdout") == 0 ||
  222. strcmp(argv[i],"-olas") == 0
  223. )
  224. {
  225. use_stdout = TRUE;
  226. file_name_out = "stdout";
  227. }
  228. else if ( strcmp(argv[i],"--clip") == 0 ||
  229. strcmp(argv[i],"-clip") == 0 ||
  230. strcmp(argv[i],"-clip_xy") == 0
  231. )
  232. {
  233. clip_xy_min = (double*) malloc (2 * sizeof(double));
  234. clip_xy_max = (double*) malloc( 2 * sizeof(double));
  235. i++;
  236. clip_xy_min[0] = atof(argv[i]);
  237. i++;
  238. clip_xy_min[1] = atof(argv[i]);
  239. i++;
  240. clip_xy_max[0] = atof(argv[i]);
  241. i++;
  242. clip_xy_max[1] = atof(argv[i]);
  243. clip = TRUE;
  244. }
  245. else if ( strcmp(argv[i],"--format") == 0 ||
  246. strcmp(argv[i],"-f") == 0 ||
  247. strcmp(argv[i],"-format") == 0
  248. )
  249. {
  250. i++;
  251. if (strcmp(argv[i], "1.0") == 0) {
  252. format = LAS_FORMAT_10;
  253. }
  254. else if (strcmp(argv[i], "1.1") == 0) {
  255. format = LAS_FORMAT_11;
  256. }
  257. else if (strcmp(argv[i], "1.2") == 0) {
  258. format = LAS_FORMAT_12;
  259. }
  260. else {
  261. LASError_Print("Format must be specified as 1.0, 1.1, or 1.2");
  262. }
  263. }
  264. else if ( strcmp(argv[i],"--eliminate_return") == 0 ||
  265. strcmp(argv[i],"-eliminate_return") == 0 ||
  266. strcmp(argv[i],"-elim_return") == 0 ||
  267. strcmp(argv[i],"-elim_ret") == 0
  268. )
  269. {
  270. i++;
  271. elim_return |= (1 << atoi(argv[i]));
  272. }
  273. else if ( strcmp(argv[i],"--eliminate_scan_angle_above") == 0 ||
  274. strcmp(argv[i],"-eliminate_scan_angle_above") == 0 ||
  275. strcmp(argv[i],"-elim_scan_angle_above") == 0 ||
  276. strcmp(argv[i],"--clip_scan_above") == 0
  277. )
  278. {
  279. i++;
  280. elim_scan_angle_above = atoi(argv[i]);
  281. }
  282. else if ( strcmp(argv[i],"--eliminate_class") == 0 ||
  283. strcmp(argv[i],"-eliminate_class") == 0 ||
  284. strcmp(argv[i],"-elim_class") == 0 ||
  285. strcmp(argv[i],"--class") == 0
  286. )
  287. {
  288. i++;
  289. elim_class = atoi(argv[i]);
  290. }
  291. else if ( strcmp(argv[i],"--eliminate_intensity_below") == 0 ||
  292. strcmp(argv[i],"-eliminate_intensity_below") == 0 ||
  293. strcmp(argv[i],"-elim_intensity_below") == 0 ||
  294. strcmp(argv[i],"--clip_intensity_below") == 0
  295. )
  296. {
  297. i++;
  298. elim_intensity_below = atoi(argv[i]);
  299. }
  300. else if ( strcmp(argv[i], "--stdin") == 0 ||
  301. strcmp(argv[i], "-ilas") == 0
  302. )
  303. {
  304. use_stdin = TRUE;
  305. }
  306. else if ( strcmp(argv[i], "--first_only") == 0 ||
  307. strcmp(argv[i], "-first_only") == 0
  308. )
  309. {
  310. first_only = TRUE;
  311. }
  312. else if ( strcmp(argv[i], "--last_only") == 0 ||
  313. strcmp(argv[i], "-last_only") == 0
  314. )
  315. {
  316. last_only = TRUE;
  317. }
  318. else if ( strcmp(argv[i], "--remove_extra_header") == 0 ||
  319. strcmp(argv[i], "-remove_extra_header") == 0
  320. )
  321. {
  322. remove_extra_header = TRUE;
  323. }
  324. else if ( strcmp(argv[i],"--s_srs") == 0 ||
  325. strcmp(argv[i],"-s_srs") == 0
  326. )
  327. {
  328. ++i;
  329. if (LAS_IsGDALEnabled()) {
  330. in_srs = LASSRS_Create();
  331. ret = LASSRS_SetFromUserInput(in_srs, argv[i]);
  332. if (ret) {
  333. LASError_Print("Unable to import assigned SRS");
  334. exit(1);
  335. }
  336. }
  337. }
  338. else if ( strcmp(argv[i],"--t_srs") == 0 ||
  339. strcmp(argv[i],"-t_srs") == 0 ||
  340. strcmp(argv[i],"-t") == 0
  341. )
  342. {
  343. ++i;
  344. if (LAS_IsGDALEnabled()) {
  345. out_srs = LASSRS_Create();
  346. ret = LASSRS_SetFromUserInput(out_srs, argv[i]);
  347. if (ret) {
  348. LASError_Print("Unable to import output SRS");
  349. exit(1);
  350. }
  351. do_reprojection = TRUE;
  352. }
  353. }
  354. else if ( strcmp(argv[i],"--a_srs") == 0 ||
  355. strcmp(argv[i],"-a_srs") == 0
  356. )
  357. {
  358. ++i;
  359. if (LAS_IsGDALEnabled()) {
  360. out_srs = LASSRS_Create();
  361. ret = LASSRS_SetFromUserInput(out_srs, argv[i]);
  362. if (ret) {
  363. LASError_Print("Unable to import output SRS");
  364. exit(1);
  365. }
  366. }
  367. }
  368. else if ( strcmp(argv[i],"--a_vertcs") == 0 ||
  369. strcmp(argv[i],"-a_vertcs") == 0
  370. )
  371. {
  372. ++i;
  373. verticalCSType = atoi(argv[i]);
  374. ++i;
  375. if( i < argc && argv[i][0] != '-' )
  376. {
  377. verticalCitation = argv[i];
  378. ++i;
  379. if( i < argc && argv[i][0] != '-' )
  380. {
  381. verticalDatum = atoi(argv[i]);
  382. ++i;
  383. if( i < argc && argv[i][0] != '-' )
  384. {
  385. verticalUnits = atoi(argv[i]);
  386. ++i;
  387. }
  388. }
  389. }
  390. }
  391. else if ( strcmp(argv[i],"--scale") == 0 ||
  392. strcmp(argv[i],"-scale") == 0
  393. )
  394. {
  395. i++;
  396. sscanf(argv[i], "%lf", &(xyz_scale[2]));
  397. xyz_scale[0] = xyz_scale[1] = xyz_scale[2];
  398. do_set_scale = TRUE;
  399. }
  400. else if ( strcmp(argv[i],"--xyz_scale") == 0 ||
  401. strcmp(argv[i],"-xyz_scale") == 0
  402. )
  403. {
  404. i++;
  405. sscanf(argv[i], "%lf", &(xyz_scale[0]));
  406. i++;
  407. sscanf(argv[i], "%lf", &(xyz_scale[1]));
  408. i++;
  409. sscanf(argv[i], "%lf", &(xyz_scale[2]));
  410. do_set_scale = TRUE;
  411. }
  412. else if ( strcmp(argv[i],"--xyz_offset") == 0 ||
  413. strcmp(argv[i],"-xyz_offset") == 0
  414. )
  415. {
  416. i++;
  417. if (!compare_no_case(argv[i], "min", 3)) {
  418. use_min_offset = TRUE;
  419. do_set_offset = TRUE;
  420. } else
  421. {
  422. sscanf(argv[i], "%lf", &(xyz_offset[0]));
  423. i++;
  424. sscanf(argv[i], "%lf", &(xyz_offset[1]));
  425. i++;
  426. sscanf(argv[i], "%lf", &(xyz_offset[2]));
  427. do_set_offset = TRUE;
  428. }
  429. }
  430. else if ( strcmp(argv[i],"--pad-header") == 0 ||
  431. strcmp(argv[i],"-pad-header") == 0 ||
  432. strcmp(argv[i],"-ph") == 0
  433. )
  434. {
  435. i++;
  436. do_pad_header = TRUE;
  437. header_pad = atoi(argv[i]);
  438. }
  439. else if ( strcmp(argv[i],"--thin") == 0 ||
  440. strcmp(argv[i],"-y") == 0
  441. )
  442. {
  443. i++;
  444. bThin = TRUE;
  445. nThin = atoi(argv[i]);
  446. }
  447. else if (file_name_in == NULL && file_name_out == NULL)
  448. {
  449. file_name_in = argv[i];
  450. }
  451. else if (file_name_in != NULL && file_name_out == NULL)
  452. {
  453. file_name_out = argv[i];
  454. }
  455. else
  456. {
  457. fprintf(stderr, "ERROR: unknown argument '%s'\n",argv[i]);
  458. usage();
  459. exit(1);
  460. }
  461. }
  462. if (use_stdin) file_name_in="stdin";
  463. if (file_name_in)
  464. {
  465. reader = LASReader_Create(file_name_in);
  466. if (!reader) {
  467. LASError_Print("Could not open file to read");
  468. exit(1);
  469. }
  470. }
  471. else
  472. {
  473. LASError_Print("no input specified");
  474. usage();
  475. exit(1);
  476. }
  477. header = LASReader_GetHeader(reader);
  478. if (!header) {
  479. LASError_Print("Could not fetch header");
  480. exit(1);
  481. }
  482. if (verbose) {
  483. ptime("start.");
  484. fprintf(stderr, "first pass reading %d points ...\n", LASHeader_GetPointRecordsCount(header));
  485. }
  486. p = LASReader_GetNextPoint(reader);
  487. if (!p) {
  488. if (LASError_GetLastErrorNum())
  489. LASError_Print("Not able to fetch a point. LASReader is invalid");
  490. else
  491. LASError_Print("File does not contain any points to read.");
  492. exit(1);
  493. }
  494. i = 0;
  495. while (p)
  496. {
  497. if (bThin && ((i % nThin) != 0)) {
  498. thinned++;
  499. i++;
  500. p = LASReader_GetNextPoint(reader);
  501. continue;
  502. }
  503. if (last_only && LASPoint_GetReturnNumber(p) != LASPoint_GetNumberOfReturns(p))
  504. {
  505. eliminated_last_only++;
  506. p = LASReader_GetNextPoint(reader);
  507. continue;
  508. }
  509. if (first_only && LASPoint_GetReturnNumber(p) != 1)
  510. {
  511. eliminated_first_only++;
  512. p = LASReader_GetNextPoint(reader);
  513. continue;
  514. }
  515. if (clip && (LASPoint_GetX(p) < clip_xy_min[0] || LASPoint_GetY(p) < clip_xy_min[1]))
  516. {
  517. clipped++;
  518. p = LASReader_GetNextPoint(reader);
  519. continue;
  520. }
  521. if (clip && (LASPoint_GetX(p) > clip_xy_max[0] || LASPoint_GetY(p) > clip_xy_max[1]))
  522. {
  523. clipped++;
  524. p = LASReader_GetNextPoint(reader);
  525. continue;
  526. }
  527. if (elim_return && (elim_return & (1 << LASPoint_GetReturnNumber(p))))
  528. {
  529. eliminated_return++;
  530. p = LASReader_GetNextPoint(reader);
  531. continue;
  532. }
  533. if (elim_scan_angle_above && (LASPoint_GetScanAngleRank(p) > elim_scan_angle_above || LASPoint_GetScanAngleRank(p) < -elim_scan_angle_above))
  534. {
  535. eliminated_scan_angle++;
  536. p = LASReader_GetNextPoint(reader);
  537. continue;
  538. }
  539. clsidx = LASPoint_GetClassification(p);
  540. clsidx = (clsidx & 31); /* 31 is max index in classification lookup table */
  541. assert(clsidx <= 31);
  542. if (elim_class && (elim_class == clsidx))
  543. {
  544. eliminated_class++;
  545. p = LASReader_GetNextPoint(reader);
  546. continue;
  547. }
  548. if (elim_intensity_below && LASPoint_GetIntensity(p) < elim_intensity_below)
  549. {
  550. eliminated_intensity++;
  551. p = LASReader_GetNextPoint(reader);
  552. continue;
  553. }
  554. surviving_number_of_point_records++;
  555. if (LASPoint_GetReturnNumber(p))
  556. surviving_number_of_points_by_return[LASPoint_GetReturnNumber(p)-1]++;
  557. else
  558. surviving_number_of_points_by_return[LASPoint_GetReturnNumber(p)]++;
  559. if (first_surviving_point)
  560. {
  561. surviving_point_min = LASPoint_Copy(p);
  562. surviving_point_max = LASPoint_Copy(p);
  563. surviving_gps_time_min = LASPoint_GetTime(p);
  564. surviving_gps_time_max = LASPoint_GetTime(p);
  565. first_surviving_point = FALSE;
  566. }
  567. else
  568. {
  569. if (LASPoint_GetX(p) < LASPoint_GetX(surviving_point_min))
  570. LASPoint_SetX(surviving_point_min,LASPoint_GetX(p));
  571. else if (LASPoint_GetX(p) > LASPoint_GetX(surviving_point_max))
  572. LASPoint_SetX(surviving_point_max,LASPoint_GetX(p));
  573. if (LASPoint_GetY(p) < LASPoint_GetY(surviving_point_min))
  574. LASPoint_SetY(surviving_point_min,LASPoint_GetY(p));
  575. else if (LASPoint_GetY(p) > LASPoint_GetY(surviving_point_max))
  576. LASPoint_SetY(surviving_point_max,LASPoint_GetY(p));
  577. if (LASPoint_GetZ(p) < LASPoint_GetZ(surviving_point_min))
  578. LASPoint_SetZ(surviving_point_min,LASPoint_GetZ(p));
  579. else if (LASPoint_GetZ(p) > LASPoint_GetZ(surviving_point_max))
  580. LASPoint_SetZ(surviving_point_max,LASPoint_GetZ(p));
  581. if (LASPoint_GetIntensity(p) < LASPoint_GetIntensity(surviving_point_min))
  582. LASPoint_SetIntensity(surviving_point_min,LASPoint_GetIntensity(p));
  583. else if (LASPoint_GetIntensity(p) > LASPoint_GetIntensity(surviving_point_max))
  584. LASPoint_SetIntensity(surviving_point_max,LASPoint_GetIntensity(p));
  585. if (LASPoint_GetFlightLineEdge(p) < LASPoint_GetFlightLineEdge(surviving_point_min))
  586. LASPoint_SetFlightLineEdge(surviving_point_min,LASPoint_GetFlightLineEdge(p));
  587. else if (LASPoint_GetFlightLineEdge(p) > LASPoint_GetFlightLineEdge(surviving_point_max))
  588. LASPoint_SetFlightLineEdge(surviving_point_max,LASPoint_GetFlightLineEdge(p));
  589. if (LASPoint_GetScanDirection(p) < LASPoint_GetScanDirection(surviving_point_min))
  590. LASPoint_SetScanDirection(surviving_point_min,LASPoint_GetScanDirection(p));
  591. else if (LASPoint_GetScanDirection(p) > LASPoint_GetScanDirection(surviving_point_max))
  592. LASPoint_SetScanDirection(surviving_point_max,LASPoint_GetScanDirection(p));
  593. if (LASPoint_GetNumberOfReturns(p) < LASPoint_GetNumberOfReturns(surviving_point_min))
  594. LASPoint_SetNumberOfReturns(surviving_point_min,LASPoint_GetNumberOfReturns(p));
  595. else if (LASPoint_GetNumberOfReturns(p) > LASPoint_GetNumberOfReturns(surviving_point_max))
  596. LASPoint_SetNumberOfReturns(surviving_point_max,LASPoint_GetNumberOfReturns(p));
  597. if (LASPoint_GetReturnNumber(p) < LASPoint_GetReturnNumber(surviving_point_min))
  598. LASPoint_SetReturnNumber(surviving_point_min,LASPoint_GetReturnNumber(p));
  599. else if (LASPoint_GetReturnNumber(p) > LASPoint_GetReturnNumber(surviving_point_max))
  600. LASPoint_SetReturnNumber(surviving_point_max,LASPoint_GetReturnNumber(p));
  601. if (LASPoint_GetClassification(p) < LASPoint_GetClassification(surviving_point_min))
  602. LASPoint_SetClassification(surviving_point_min,LASPoint_GetClassification(p));
  603. else if (LASPoint_GetReturnNumber(p) > LASPoint_GetClassification(surviving_point_max))
  604. LASPoint_SetClassification(surviving_point_max,LASPoint_GetClassification(p));
  605. if (LASPoint_GetScanAngleRank(p) < LASPoint_GetScanAngleRank(surviving_point_min))
  606. LASPoint_SetScanAngleRank(surviving_point_min,LASPoint_GetScanAngleRank(p));
  607. else if (LASPoint_GetScanAngleRank(p) > LASPoint_GetScanAngleRank(surviving_point_max))
  608. LASPoint_SetScanAngleRank(surviving_point_max,LASPoint_GetScanAngleRank(p));
  609. if (LASPoint_GetUserData(p) < LASPoint_GetUserData(surviving_point_min))
  610. LASPoint_SetUserData(surviving_point_min,LASPoint_GetUserData(p));
  611. else if (LASPoint_GetUserData(p) > LASPoint_GetUserData(surviving_point_max))
  612. LASPoint_SetUserData(surviving_point_max,LASPoint_GetUserData(p));
  613. if (LASPoint_GetTime(p) < LASPoint_GetTime(surviving_point_min))
  614. LASPoint_SetTime(surviving_point_min,LASPoint_GetTime(p));
  615. else if (LASPoint_GetTime(p) > LASPoint_GetTime(surviving_point_max))
  616. LASPoint_SetTime(surviving_point_max,LASPoint_GetTime(p));
  617. }
  618. p = LASReader_GetNextPoint(reader);
  619. i++;
  620. }
  621. if (eliminated_first_only)
  622. fprintf(stderr,
  623. "eliminated based on first returns only: %d\n",
  624. eliminated_first_only);
  625. if (eliminated_last_only)
  626. fprintf(stderr,
  627. "eliminated based on last returns only: %d\n",
  628. eliminated_last_only);
  629. if (eliminated_class)
  630. fprintf(stderr,
  631. "eliminated classification: %d\n",
  632. eliminated_class);
  633. if (clipped)
  634. fprintf(stderr,
  635. "clipped: %d\n",
  636. clipped);
  637. if (eliminated_return)
  638. fprintf(stderr,
  639. "eliminated based on return number: %d\n",
  640. eliminated_return);
  641. if (eliminated_scan_angle)
  642. fprintf(stderr,
  643. "eliminated based on scan angle: %d\n",
  644. eliminated_scan_angle);
  645. if (eliminated_intensity)
  646. fprintf(stderr,
  647. "eliminated based on intensity: %d\n",
  648. eliminated_intensity);
  649. if (bThin)
  650. fprintf(stderr,
  651. "thinned: %d\n",
  652. thinned);
  653. if (surviving_number_of_point_records == 0) {
  654. fprintf(stderr, "All points were eliminated!\n");
  655. exit(0);
  656. }
  657. LASReader_Destroy(reader);
  658. LASHeader_Destroy(header);
  659. if (verbose)
  660. {
  661. fprintf(stderr,
  662. "x %.3f %.3f %.3f\n",
  663. LASPoint_GetX(surviving_point_min),
  664. LASPoint_GetX(surviving_point_max),
  665. LASPoint_GetX(surviving_point_max) - LASPoint_GetX(surviving_point_min)
  666. );
  667. fprintf(stderr,
  668. "y %.3f %.3f %.3f\n",
  669. LASPoint_GetY(surviving_point_min),
  670. LASPoint_GetY(surviving_point_max),
  671. LASPoint_GetY(surviving_point_max) - LASPoint_GetY(surviving_point_min)
  672. );
  673. fprintf(stderr,
  674. "z %.3f %.3f %.3f\n",
  675. LASPoint_GetZ(surviving_point_min),
  676. LASPoint_GetZ(surviving_point_max),
  677. LASPoint_GetZ(surviving_point_max) - LASPoint_GetZ(surviving_point_min)
  678. );
  679. fprintf(stderr,
  680. "intensity %d %d %d\n",
  681. LASPoint_GetIntensity(surviving_point_min),
  682. LASPoint_GetIntensity(surviving_point_max),
  683. LASPoint_GetIntensity(surviving_point_max) - LASPoint_GetIntensity(surviving_point_min)
  684. );
  685. fprintf(stderr,
  686. "edge_of_flight_line %d %d %d\n",
  687. LASPoint_GetFlightLineEdge(surviving_point_min),
  688. LASPoint_GetFlightLineEdge(surviving_point_max),
  689. LASPoint_GetFlightLineEdge(surviving_point_max) - LASPoint_GetFlightLineEdge(surviving_point_min)
  690. );
  691. fprintf(stderr,
  692. "scan_direction_flag %d %d %d\n",
  693. LASPoint_GetScanDirection(surviving_point_min),
  694. LASPoint_GetScanDirection(surviving_point_max),
  695. LASPoint_GetScanDirection(surviving_point_max) - LASPoint_GetScanDirection(surviving_point_min)
  696. );
  697. fprintf(stderr,
  698. "number_of_returns_of_given_pulse %d %d %d\n",
  699. LASPoint_GetNumberOfReturns(surviving_point_min),
  700. LASPoint_GetNumberOfReturns(surviving_point_max),
  701. LASPoint_GetNumberOfReturns(surviving_point_max) - LASPoint_GetNumberOfReturns(surviving_point_min)
  702. );
  703. fprintf(stderr,
  704. "return_number %d %d %d\n",
  705. LASPoint_GetReturnNumber(surviving_point_min),
  706. LASPoint_GetReturnNumber(surviving_point_max),
  707. LASPoint_GetReturnNumber(surviving_point_max) - LASPoint_GetReturnNumber(surviving_point_min)
  708. );
  709. fprintf(stderr,
  710. "classification %d %d %d\n",
  711. LASPoint_GetClassification(surviving_point_min),
  712. LASPoint_GetClassification(surviving_point_max),
  713. LASPoint_GetClassification(surviving_point_max) - LASPoint_GetClassification(surviving_point_min)
  714. );
  715. fprintf(stderr,
  716. "scan_angle_rank %d %d %d\n",
  717. LASPoint_GetScanAngleRank(surviving_point_min),
  718. LASPoint_GetScanAngleRank(surviving_point_max),
  719. LASPoint_GetScanAngleRank(surviving_point_max) - LASPoint_GetScanAngleRank(surviving_point_min)
  720. );
  721. fprintf(stderr,
  722. "user_data %d %d %d\n",
  723. LASPoint_GetUserData(surviving_point_min),
  724. LASPoint_GetUserData(surviving_point_max),
  725. LASPoint_GetUserData(surviving_point_max) - LASPoint_GetUserData(surviving_point_min)
  726. );
  727. fprintf(stderr,
  728. "gps_time %.8f %.8f %.8f\n",
  729. LASPoint_GetTime(surviving_point_min),
  730. LASPoint_GetTime(surviving_point_max),
  731. LASPoint_GetTime(surviving_point_max) - LASPoint_GetTime(surviving_point_min)
  732. );
  733. }
  734. if (file_name_out == NULL && !use_stdout)
  735. {
  736. LASError_Print("no output specified. exiting...");
  737. exit(1);
  738. }
  739. fprintf(stderr, "Creating another reader...\n");
  740. if (file_name_in)
  741. {
  742. reader = LASReader_Create(file_name_in);
  743. if (!reader) {
  744. LASError_Print("Could not open file to read");
  745. exit(1);
  746. }
  747. }
  748. else
  749. {
  750. LASError_Print("no input specified");
  751. usage();
  752. exit(1);
  753. }
  754. header = LASReader_GetHeader(reader);
  755. if (!header) {
  756. LASError_Print("Could not read header");
  757. exit(1);
  758. }
  759. surviving_header = LASHeader_Copy(header);
  760. LASHeader_SetPointRecordsCount(surviving_header, surviving_number_of_point_records);
  761. LASHeader_SetSystemId(surviving_header, "MODIFICATION");
  762. for (i = 0; i < 5; i++) LASHeader_SetPointRecordsByReturnCount(surviving_header, i, surviving_number_of_points_by_return[i]);
  763. minx = LASPoint_GetX(surviving_point_min) * \
  764. LASHeader_GetScaleX(surviving_header) + \
  765. LASHeader_GetOffsetX(surviving_header);
  766. maxx = LASPoint_GetX(surviving_point_max) * LASHeader_GetScaleX(surviving_header) + LASHeader_GetOffsetX(surviving_header);
  767. miny = LASPoint_GetY(surviving_point_min) * LASHeader_GetScaleY(surviving_header) + LASHeader_GetOffsetY(surviving_header);
  768. maxy = LASPoint_GetY(surviving_point_max) * LASHeader_GetScaleY(surviving_header) + LASHeader_GetOffsetY(surviving_header);
  769. minz = LASPoint_GetZ(surviving_point_min) * LASHeader_GetScaleZ(surviving_header) + LASHeader_GetOffsetZ(surviving_header);
  770. maxz = LASPoint_GetZ(surviving_point_max) * LASHeader_GetScaleZ(surviving_header) + LASHeader_GetOffsetZ(surviving_header);
  771. if (format == LAS_FORMAT_10) {
  772. LASHeader_SetVersionMinor(surviving_header, 0);
  773. } else if (format == LAS_FORMAT_11){
  774. LASHeader_SetVersionMinor(surviving_header, 1);
  775. } else if (format == LAS_FORMAT_12) {
  776. LASHeader_SetVersionMinor(surviving_header, 2);
  777. }
  778. if (do_set_offset) {
  779. if (use_min_offset) {
  780. if (verbose) {
  781. fprintf(stderr,
  782. "Setting xyz offset to minimums...\n");
  783. }
  784. LASHeader_SetOffset(surviving_header,
  785. LASPoint_GetX(surviving_point_min),
  786. LASPoint_GetY(surviving_point_min),
  787. LASPoint_GetZ(surviving_point_min));
  788. } else {
  789. if (verbose) {
  790. fprintf(stderr,
  791. "Setting xyz offset to %.8f %.8f %.8f...\n", xyz_offset[0], xyz_offset[1], xyz_offset[2]);
  792. }
  793. LASHeader_SetOffset(surviving_header,
  794. xyz_offset[0],
  795. xyz_offset[1],
  796. xyz_offset[2]);
  797. }
  798. }
  799. if (do_set_scale) {
  800. if (verbose) {
  801. fprintf(stderr,
  802. "Setting xyz scale to %.8f %.8f %.8f...\n", xyz_scale[0], xyz_scale[1], xyz_scale[2]);
  803. }
  804. LASHeader_SetScale( surviving_header,
  805. xyz_scale[0],
  806. xyz_scale[1],
  807. xyz_scale[2]);
  808. }
  809. if (do_pad_header){
  810. if (verbose) {
  811. fprintf(stderr,
  812. "Padding header by %d bytes. New header will be %d bytes long instead of %d bytes ...\n",
  813. header_pad,
  814. LASHeader_GetDataOffset(surviving_header)+abs(header_pad),
  815. LASHeader_GetDataOffset(surviving_header));
  816. }
  817. LASHeader_SetDataOffset(surviving_header, LASHeader_GetDataOffset(surviving_header)+abs(header_pad));
  818. }
  819. /* Do we have vertical cs info to set? */
  820. if( verticalCSType > 0 )
  821. {
  822. if( out_srs == NULL )
  823. out_srs = LASHeader_GetSRS(surviving_header);
  824. if( out_srs == NULL )
  825. out_srs = LASSRS_Create();
  826. LASSRS_SetVerticalCS( out_srs,
  827. verticalCSType,
  828. verticalCitation,
  829. verticalDatum,
  830. verticalUnits );
  831. }
  832. if (do_reprojection) {
  833. if (verbose) {
  834. proj4_text = LASSRS_GetProj4(out_srs);
  835. fprintf(stderr,
  836. "Setting new coordinate system to %s", proj4_text);
  837. LASString_Free(proj4_text);
  838. }
  839. /* keep around the header's SRS if we don't have one set by the user */
  840. if (in_srs == NULL) {
  841. in_srs = LASHeader_GetSRS(surviving_header);
  842. }
  843. LASHeader_SetSRS(surviving_header, out_srs);
  844. }
  845. /* Are we just assigning an override SRS? (-a_srs) */
  846. else if( out_srs != NULL )
  847. {
  848. LASHeader_SetSRS(surviving_header, out_srs);
  849. }
  850. if (verbose) {
  851. fprintf(stderr,
  852. "second pass reading %d and writing %d points ...\n",
  853. LASHeader_GetPointRecordsCount(surviving_header),
  854. surviving_number_of_point_records);
  855. }
  856. if (use_stdout) file_name_out = "stdout";
  857. writer = LASWriter_Create( file_name_out,
  858. surviving_header,
  859. LAS_MODE_WRITE);
  860. if (!writer) {
  861. LASError_Print("Could not open file to write");
  862. exit(1);
  863. }
  864. if (do_reprojection) {
  865. if (in_srs != NULL) {
  866. char* in_wkt = LASSRS_GetWKT(in_srs);
  867. if (strlen(in_wkt) == 0) {
  868. LASString_Free(in_wkt);
  869. LASError_Print("Input SRS is empty, please specify with -s_srs");
  870. exit(1);
  871. }
  872. LASString_Free(in_wkt);
  873. LASWriter_SetInputSRS(writer, in_srs);
  874. } else {
  875. }
  876. if (out_srs != NULL) {
  877. if (verbose) {
  878. fprintf(stderr,
  879. "Setting LASWriter_SetOutputSRS to %s", LASSRS_GetProj4(out_srs));
  880. }
  881. LASWriter_SetOutputSRS(writer, out_srs);
  882. }
  883. }
  884. /*
  885. if (!remove_extra_header)
  886. {
  887. for (unsigned int u = lasreader->header.header_size; u < lasreader->header.offset_to_point_data; u++)
  888. {
  889. fputc(fgetc(file_in),file_out);
  890. }
  891. }
  892. */
  893. LASReader_Destroy(reader);
  894. reader = NULL;
  895. if (file_name_in)
  896. {
  897. reader = LASReader_Create(file_name_in);
  898. if (!reader) {
  899. LASError_Print("Could not open file to read");
  900. exit(1);
  901. }
  902. }
  903. else
  904. {
  905. LASError_Print("no input specified");
  906. usage();
  907. exit(1);
  908. }
  909. p = LASReader_GetNextPoint(reader);
  910. if (!p) {
  911. if (LASError_GetLastErrorNum())
  912. LASError_Print("Not able to fetch a point. LASReader is invalid");
  913. else
  914. LASError_Print("File does not contain any points to read.");
  915. exit(1);
  916. }
  917. i = 0;
  918. while (p) {
  919. if (bThin && ((i % nThin) != 0)) {
  920. i++;
  921. p = LASReader_GetNextPoint(reader);
  922. continue;
  923. }
  924. if (skip_invalid && !LASPoint_IsValid(p)) {
  925. p = LASReader_GetNextPoint(reader);
  926. continue;
  927. }
  928. if (last_only && LASPoint_GetReturnNumber(p) != LASPoint_GetNumberOfReturns(p))
  929. {
  930. p = LASReader_GetNextPoint(reader);
  931. continue;
  932. }
  933. if (first_only && LASPoint_GetReturnNumber(p) != 1)
  934. {
  935. p = LASReader_GetNextPoint(reader);
  936. continue;
  937. }
  938. if (clip_xy_min && (LASPoint_GetX(p) < clip_xy_min[0] || LASPoint_GetY(p) < clip_xy_min[1]))
  939. {
  940. p = LASReader_GetNextPoint(reader);
  941. continue;
  942. }
  943. if (clip_xy_max && (LASPoint_GetX(p) > clip_xy_max[0] || LASPoint_GetY(p) > clip_xy_max[1]))
  944. {
  945. p = LASReader_GetNextPoint(reader);
  946. continue;
  947. }
  948. if (elim_return && (elim_return & (1 << LASPoint_GetReturnNumber(p))))
  949. {
  950. p = LASReader_GetNextPoint(reader);
  951. continue;
  952. }
  953. if (elim_scan_angle_above && (LASPoint_GetScanAngleRank(p) > elim_scan_angle_above || LASPoint_GetScanAngleRank(p) < -elim_scan_angle_above))
  954. {
  955. p = LASReader_GetNextPoint(reader);
  956. continue;
  957. }
  958. if (elim_intensity_below && LASPoint_GetIntensity(p) < elim_intensity_below)
  959. {
  960. p = LASReader_GetNextPoint(reader);
  961. continue;
  962. }
  963. clsidx = LASPoint_GetClassification(p);
  964. clsidx = (clsidx & 31); /* 31 is max index in classification lookup table */
  965. assert(clsidx <= 31);
  966. if (elim_class && (elim_class == clsidx))
  967. {
  968. p = LASReader_GetNextPoint(reader);
  969. continue;
  970. }
  971. ret = LASWriter_WritePoint(writer,p);
  972. if (ret == LE_Warning) {
  973. LASError_Print("Unable to write invalid point. Use --skip_invalid to avoid this problem (you will lose data in the translation)");
  974. exit(1);
  975. }
  976. p = LASReader_GetNextPoint(reader);
  977. i++;
  978. }
  979. LASWriter_Destroy(writer);
  980. LASReader_Destroy(reader);
  981. LASHeader_Destroy(header);
  982. LASHeader_Destroy(surviving_header);
  983. if (surviving_point_max != NULL)
  984. LASPoint_Destroy(surviving_point_max);
  985. if (surviving_point_min != NULL)
  986. LASPoint_Destroy(surviving_point_min);
  987. reader = LASReader_Create(file_name_out);
  988. if (!reader) {
  989. LASError_Print("Could not open file to read");
  990. exit(1);
  991. }
  992. header = LASReader_GetHeader(reader);
  993. if (!header) {
  994. LASError_Print("Could not read header");
  995. exit(1);
  996. }
  997. summary = SummarizePoints(reader);
  998. if (verbose) {
  999. print_point_summary(stderr, summary, header);
  1000. }
  1001. repair_header(stderr, header, summary) ;
  1002. if (summary != NULL) {
  1003. if (summary->pmin != NULL)
  1004. LASPoint_Destroy(summary->pmin);
  1005. if (summary->pmax != NULL)
  1006. LASPoint_Destroy(summary->pmax);
  1007. free(summary);
  1008. }
  1009. if (reader != NULL) {
  1010. LASReader_Destroy(reader);
  1011. reader = NULL;
  1012. }
  1013. writer = LASWriter_Create(file_name_out, header, LAS_MODE_APPEND);
  1014. if (!writer) {
  1015. LASError_Print("Problem creating LASWriterH object for append");
  1016. LASHeader_Destroy(header);
  1017. header = NULL;
  1018. exit(1);
  1019. }
  1020. LASWriter_Destroy(writer);
  1021. writer = NULL;
  1022. LASHeader_Destroy(header);
  1023. header = NULL;
  1024. if (out_srs != NULL)
  1025. LASSRS_Destroy(out_srs);
  1026. if (in_srs != NULL)
  1027. LASSRS_Destroy(in_srs);
  1028. if (verbose) ptime("done.");
  1029. return 0;
  1030. }