-
Notifications
You must be signed in to change notification settings - Fork 433
/
LasReader.cpp
803 lines (691 loc) · 24.8 KB
/
LasReader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
/******************************************************************************
* Copyright (c) 2011, Michael P. Gerlek (mpg@flaxen.com)
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following
* conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided
* with the distribution.
* * Neither the name of Hobu, Inc. or Flaxen Geo Consulting nor the
* names of its contributors may be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
****************************************************************************/
#include "LasReader.hpp"
#include <sstream>
#include <string.h>
#include <pdal/Extractor.hpp>
#include <pdal/util/FileUtils.hpp>
#include <pdal/IStream.hpp>
#include <pdal/QuickInfo.hpp>
#include <pdal/PointBuffer.hpp>
#include <pdal/Metadata.hpp>
#include "LasHeader.hpp"
#include "VariableLengthRecord.hpp"
#include "ZipPoint.hpp"
#include "GeotiffSupport.hpp"
namespace pdal
{
namespace
{
double toDouble(const Everything& e, Dimension::Type::Enum type)
{
using namespace Dimension::Type;
double d = 0;
switch (type)
{
case Unsigned8:
d = e.u8;
break;
case Unsigned16:
d = e.u16;
break;
case Unsigned32:
d = e.u32;
break;
case Unsigned64:
d = e.u64;
break;
case Signed8:
d = e.s8;
break;
case Signed16:
d = e.s16;
break;
case Signed32:
d = e.s32;
break;
case Signed64:
d = e.s64;
break;
case Float:
d = e.f;
break;
case Double:
d = e.d;
break;
default:
break;
}
return d;
}
} // unnamed namespace
void LasReader::processOptions(const Options& options)
{
StringList extraDims = options.getValueOrDefault<StringList>("extra_dims");
m_extraDims = LasUtils::parse(extraDims);
m_error.setFilename(m_filename);
}
QuickInfo LasReader::inspect()
{
QuickInfo qi;
PointContext ctx;
addDimensions(ctx);
initialize();
Dimension::IdList dims = ctx.dims();
for (auto di = dims.begin(); di != dims.end(); ++di)
qi.m_dimNames.push_back(ctx.dimName(*di));
qi.m_pointCount =
Utils::saturation_cast<point_count_t>(m_lasHeader.pointCount());
qi.m_bounds = m_lasHeader.getBounds();
qi.m_srs = getSrsFromVlrs();
return qi;
}
void LasReader::initialize()
{
m_streamFactory = createFactory();
m_istream = &(m_streamFactory->allocate());
m_istream->seekg(0);
ILeStream in(m_istream);
in >> m_lasHeader;
if (!m_lasHeader.pointFormatSupported())
{
std::ostringstream oss;
oss << "Unsupported LAS input point format: " <<
(int)m_lasHeader.pointFormat() << ".";
throw pdal_error(oss.str());
}
// We need to read the VLRs in initialize() because they may contain an
// extra-bytes VLR that is needed to determine dimensions.
m_istream->seekg(m_lasHeader.vlrOffset());
for (size_t i = 0; i < m_lasHeader.vlrCount(); ++i)
{
VariableLengthRecord r;
in >> r;
m_vlrs.push_back(std::move(r));
}
if (m_lasHeader.versionAtLeast(1, 4))
{
m_istream->seekg(m_lasHeader.eVlrOffset());
for (size_t i = 0; i < m_lasHeader.eVlrCount(); ++i)
{
ExtVariableLengthRecord r;
in >> r;
m_vlrs.push_back(std::move(r));
}
readExtraBytesVlr();
}
fixupVlrs();
}
void LasReader::ready(PointContext ctx, MetadataNode& m)
{
m_index = 0;
setSrsFromVlrs(m);
extractHeaderMetadata(m);
if (m_lasHeader.compressed())
{
#ifdef PDAL_HAVE_LASZIP
VariableLengthRecord *vlr = findVlr(LASZIP_USER_ID, LASZIP_RECORD_ID);
m_zipPoint.reset(new ZipPoint(vlr));
if (!m_unzipper)
{
m_unzipper.reset(new LASunzipper());
m_istream->seekg(m_lasHeader.pointOffset(), std::ios::beg);
// Once we open the zipper, don't touch the stream until the
// zipper is closed or bad things happen.
if (!m_unzipper->open(*m_istream, m_zipPoint->GetZipper()))
{
std::ostringstream oss;
const char* err = m_unzipper->get_error();
if (err == NULL)
err = "(unknown error)";
oss << "Failed to open LASzip stream: " << std::string(err);
throw pdal_error(oss.str());
}
}
#else
throw pdal_error("LASzip is not enabled. Can't read LAZ data.");
#endif
}
}
Options LasReader::getDefaultOptions()
{
Options options;
options.add("filename", "", "file to read from");
options.add("extra_dims", "", "Extra dimensions not part of the LAS "
"point format to be read from each point.");
return options;
}
void LasReader::extractHeaderMetadata(MetadataNode& m)
{
m.add<bool>("compressed", m_lasHeader.compressed(),
"true if this LAS file is compressed");
m.add<uint32_t>("major_version",
static_cast<uint32_t>(m_lasHeader.versionMajor()),
"The major LAS version for the file, always 1 for now");
m.add<uint32_t>("minor_version",
static_cast<uint32_t>(m_lasHeader.versionMinor()),
"The minor LAS version for the file");
if (m_lasHeader.versionAtLeast(1, 1))
m.add<uint32_t>("filesource_id",
static_cast<uint32_t>(m_lasHeader.fileSourceId()),
"File Source ID (Flight Line Number if this file was derived from "
"an original flight line).");
if (m_lasHeader.versionAtLeast(1, 2))
{
uint16_t globalEncoding = m_lasHeader.globalEncoding();
m.addEncoded("global_encoding", (uint8_t *)&globalEncoding,
sizeof(globalEncoding),
"Global Encoding: general property bit field.");
}
m.add<boost::uuids::uuid>("project_id",
m_lasHeader.projectId(), "Project ID.");
m.add<std::string>("system_id", m_lasHeader.systemId());
m.add<std::string>("software_id", m_lasHeader.softwareId(),
"Generating software description.");
m.add<uint32_t>("creation_doy",
static_cast<uint32_t>(m_lasHeader.creationDOY()),
"Day, expressed as an unsigned short, on which this file was created. "
"Day is computed as the Greenwich Mean Time (GMT) day. January 1 is "
"considered day 1.");
m.add<uint32_t>("creation_year",
static_cast<uint32_t>(m_lasHeader.creationYear()),
"The year, expressed as a four digit number, in which the file was "
"created.");
m.add<uint32_t>("header_size",
static_cast<uint32_t>(m_lasHeader.vlrOffset()),
"The size, in bytes, of the header block, including any extension "
"by specific software.");
m.add<uint32_t>("dataoffset",
static_cast<uint32_t>(m_lasHeader.pointOffset()),
"The actual number of bytes from the beginning of the file to the "
"first field of the first point record data field. This data offset "
"must be updated if any software adds data from the Public Header "
"Block or adds/removes data to/from the Variable Length Records.");
m.add<double>("scale_x", m_lasHeader.scaleX(),
"The scale factor for X values.");
m.add<double>("scale_y", m_lasHeader.scaleY(),
"The scale factor for Y values.");
m.add<double>("scale_z", m_lasHeader.scaleZ(),
"The scale factor for Z values.");
m.add<double>("offset_x", m_lasHeader.offsetX(),
"The offset for X values.");
m.add<double>("offset_y", m_lasHeader.offsetY(),
"The offset for Y values.");
m.add<double>("offset_z", m_lasHeader.offsetZ(),
"The offset for Z values.");
m.add<double>("minx", m_lasHeader.minX(),
"The max and min data fields are the actual unscaled extents of the "
"LAS point file data, specified in the coordinate system of the LAS "
"data.");
m.add<double>("miny", m_lasHeader.minY(),
"The max and min data fields are the actual unscaled extents of the "
"LAS point file data, specified in the coordinate system of the LAS "
"data.");
m.add<double>("minz", m_lasHeader.minZ(),
"The max and min data fields are the actual unscaled extents of the "
"LAS point file data, specified in the coordinate system of the LAS "
"data.");
m.add<double>("maxx", m_lasHeader.maxX(),
"The max and min data fields are the actual unscaled extents of the "
"LAS point file data, specified in the coordinate system of the LAS "
"data.");
m.add<double>("maxy", m_lasHeader.maxY(),
"The max and min data fields are the actual unscaled extents of the "
"LAS point file data, specified in the coordinate system of the LAS "
"data.");
m.add<double>("maxz", m_lasHeader.maxZ(),
"The max and min data fields are the actual unscaled extents of the "
"LAS point file data, specified in the coordinate system of the LAS "
"data.");
m.add<uint32_t>("count",
m_lasHeader.pointCount(), "This field contains the total "
"number of point records within the file.");
}
void LasReader::fixupVlrs()
{
const size_t KEY_SIZE = 8;
char zeros[8] = {};
// There is currently only one fixup - for the geotiff directory VLR.
VariableLengthRecord *vlr =
findVlr(TRANSFORM_USER_ID, GEOTIFF_DIRECTORY_RECORD_ID);
if (vlr)
{
while (vlr->dataLen() > 8)
{
// If the key at the end has a zero value, remove it
// by resizing the array and decrementing the size.
char *testPos = const_cast<char *>(
vlr->data() + vlr->dataLen() - KEY_SIZE);
if (memcmp(zeros, testPos, KEY_SIZE))
break;
uint16_t size;
vlr->setDataLen(vlr->dataLen() - 8);
// Reduce the size of the data by one. The size field is
// at offset 6 from the beginning of the data.
memcpy((void *)&size, vlr->data() + 6, 2);
size = le16toh(size);
size--;
size = htole16(size);
memcpy((void *)(vlr->data() + 6), &size, 2);
}
}
}
void LasReader::readExtraBytesVlr()
{
VariableLengthRecord *vlr = findVlr(SPEC_USER_ID, EXTRA_BYTES_RECORD_ID);
if (!vlr)
return;
const char *pos = vlr->data();
size_t size = vlr->dataLen();
if (size % sizeof(ExtraBytesSpec) != 0)
{
log()->get(LogLevel::Warning) << "Bad size for extra bytes VLR. "
"Ignoring.";
return;
}
size /= sizeof(ExtraBytesSpec);
std::vector<ExtraBytesIf> ebList;
while (size--)
{
ExtraBytesIf eb;
eb.readFrom(pos);
ebList.push_back(eb);
pos += sizeof(ExtraBytesSpec);
}
std::vector<ExtraDim> extraDims;
for (ExtraBytesIf& eb : ebList)
{
std::vector<ExtraDim> eds = eb.toExtraDims();
for (auto& ed : eds)
extraDims.push_back(std::move(ed));
}
if (m_extraDims.size() && m_extraDims != extraDims)
log()->get(LogLevel::Warning) << "Extra byte dimensions specified "
"in pineline and VLR don't match. Ignoring pipeline-specified "
"dimensions";
m_extraDims = extraDims;
}
void LasReader::setSrsFromVlrs(MetadataNode& m)
{
// If the user is already overriding this by setting it on the stage, we'll
// take their overridden value
SpatialReference srs = getSpatialReference();
if (srs.getWKT(pdal::SpatialReference::eCompoundOK).empty())
setSpatialReference(m, getSrsFromVlrs());
}
SpatialReference LasReader::getSrsFromVlrs()
{
SpatialReference srs = getSrsFromWktVlr();
if (srs.empty())
srs = getSrsFromGeotiffVlr();
return srs;
}
VariableLengthRecord *LasReader::findVlr(const std::string& userId,
uint16_t recordId)
{
for (auto vi = m_vlrs.begin(); vi != m_vlrs.end(); ++vi)
{
VariableLengthRecord& vlr = *vi;
if (vlr.matches(userId, recordId))
return &vlr;
}
return NULL;
}
SpatialReference LasReader::getSrsFromWktVlr()
{
SpatialReference srs;
VariableLengthRecord *vlr = findVlr(TRANSFORM_USER_ID, WKT_RECORD_ID);
if (!vlr)
vlr = findVlr(LIBLAS_USER_ID, WKT_RECORD_ID);
if (!vlr || vlr->dataLen() == 0)
return srs;
// There is supposed to be a NULL byte at the end of the data,
// but sometimes there isn't because some people don't follow the
// rules. If there is a NULL byte, don't stick it in the
// wkt string.
size_t len = vlr->dataLen();
const char *c = vlr->data() + len - 1;
if (*c == 0)
len--;
std::string wkt(vlr->data(), len);
srs.setWKT(wkt);
return srs;
}
SpatialReference LasReader::getSrsFromGeotiffVlr()
{
SpatialReference srs;
#ifdef PDAL_HAVE_LIBGEOTIFF
GeotiffSupport geotiff;
geotiff.resetTags();
VariableLengthRecord *vlr;
vlr = findVlr(TRANSFORM_USER_ID, GEOTIFF_DIRECTORY_RECORD_ID);
// We must have a directory entry.
if (!vlr)
return srs;
geotiff.setKey(vlr->recordId(), (void *)vlr->data(), vlr->dataLen(),
STT_SHORT);
vlr = findVlr(TRANSFORM_USER_ID, GEOTIFF_DOUBLES_RECORD_ID);
if (vlr)
geotiff.setKey(vlr->recordId(), (void *)vlr->data(), vlr->dataLen(),
STT_DOUBLE);
vlr = findVlr(TRANSFORM_USER_ID, GEOTIFF_ASCII_RECORD_ID);
if (vlr)
geotiff.setKey(vlr->recordId(), (void *)vlr->data(), vlr->dataLen(),
STT_ASCII);
geotiff.setTags();
std::string wkt(geotiff.getWkt(false, false));
if (wkt.size())
srs.setFromUserInput(geotiff.getWkt(false, false));
#endif
return srs;
}
void LasReader::extractVlrMetadata(MetadataNode& m)
{
static const size_t DATA_LEN_MAX = 1000000;
int i = 0;
for (auto vi = m_vlrs.begin(); vi != m_vlrs.end(); ++vi, ++i)
{
const VariableLengthRecord& vlr = *vi;
if (vlr.dataLen() > DATA_LEN_MAX)
continue;
std::ostringstream name;
name << "vlr_" << i;
MetadataNode vlrNode = m.addEncoded(name.str(),
(const uint8_t *)vlr.data(), vlr.dataLen(), vlr.description());
vlrNode.add("user_id", vlr.userId(),
"User ID of the record or pre-defined value from the "
"specification.");
vlrNode.add("record_id", vlr.recordId(),
"Record ID specified by the user.");
vlrNode.add("description", vlr.description());
}
}
void LasReader::addDimensions(PointContextRef ctx)
{
using namespace Dimension;
ctx.registerDim(Id::X, Type::Double);
ctx.registerDim(Id::Y, Type::Double);
ctx.registerDim(Id::Z, Type::Double);
ctx.registerDim(Id::Intensity, Type::Unsigned16);
ctx.registerDim(Id::ReturnNumber, Type::Unsigned8);
ctx.registerDim(Id::NumberOfReturns, Type::Unsigned8);
ctx.registerDim(Id::ScanDirectionFlag, Type::Unsigned8);
ctx.registerDim(Id::EdgeOfFlightLine, Type::Unsigned8);
ctx.registerDim(Id::Classification, Type::Unsigned8);
ctx.registerDim(Id::ScanAngleRank, Type::Signed8);
ctx.registerDim(Id::UserData, Type::Unsigned8);
ctx.registerDim(Id::PointSourceId, Type::Unsigned16);
if (m_lasHeader.hasTime())
ctx.registerDim(Id::GpsTime, Type::Double);
if (m_lasHeader.hasColor())
{
ctx.registerDim(Id::Red, Type::Unsigned16);
ctx.registerDim(Id::Green, Type::Unsigned16);
ctx.registerDim(Id::Blue, Type::Unsigned16);
}
if (m_lasHeader.hasInfrared())
ctx.registerDim(Id::Infrared);
if (m_lasHeader.versionAtLeast(1, 4))
ctx.registerDim(Id::ScanChannel);
for (auto& dim : m_extraDims)
{
Dimension::Type::Enum type = dim.m_dimType.m_type;
if (type == Dimension::Type::None)
continue;
if (dim.m_dimType.m_xform.nonstandard())
type = Dimension::Type::Double;
dim.m_dimType.m_id = ctx.assignDim(dim.m_name, type);
}
}
point_count_t LasReader::read(PointBuffer& data, point_count_t count)
{
size_t pointByteCount = m_lasHeader.pointLen();
count = std::min(count, getNumPoints() - m_index);
PointId i = 0;
if (m_zipPoint)
{
#ifdef PDAL_HAVE_LASZIP
for (i = 0; i < count; i++)
{
if (!m_unzipper->read(m_zipPoint->m_lz_point))
{
std::string error = "Error reading compressed point data: ";
const char* err = m_unzipper->get_error();
if (!err)
err = "(unknown error)";
error += err;
throw pdal_error(error);
}
loadPoint(data, (char *)m_zipPoint->m_lz_point_data.data(),
pointByteCount);
}
#else
throw pdal_error("LASzip is not enabled for this "
"LasReader::processBuffer");
#endif
}
else
{
m_istream->seekg(m_lasHeader.pointOffset());
point_count_t remaining = count;
// Make a buffer at most a meg.
size_t bufsize = std::min((point_count_t)1000000,
count * pointByteCount);
std::vector<char> buf(bufsize);
try
{
do
{
point_count_t blockPoints = readFileBlock(buf, remaining);
remaining -= blockPoints;
char *pos = buf.data();
while (blockPoints--)
{
loadPoint(data, pos, pointByteCount);
pos += pointByteCount;
i++;
}
} while (remaining);
}
catch (std::out_of_range&)
{}
catch (pdal::invalid_stream&)
{}
}
m_index += i;
return (point_count_t)i;
}
point_count_t LasReader::readFileBlock(std::vector<char>& buf,
point_count_t maxpoints)
{
size_t ptLen = m_lasHeader.pointLen();
point_count_t blockpoints = buf.size() / ptLen;
blockpoints = std::min(maxpoints, blockpoints);
m_istream->read(buf.data(), blockpoints * ptLen);
return blockpoints;
}
void LasReader::loadPoint(PointBuffer& data, char *buf, size_t bufsize)
{
if (m_lasHeader.has14Format())
loadPointV14(data, buf, bufsize);
else
loadPointV10(data, buf, bufsize);
}
void LasReader::loadPointV10(PointBuffer& data, char *buf, size_t bufsize)
{
LeExtractor istream(buf, bufsize);
PointId nextId = data.size();
int32_t xi, yi, zi;
istream >> xi >> yi >> zi;
const LasHeader& h = m_lasHeader;
double x = xi * h.scaleX() + h.offsetX();
double y = yi * h.scaleY() + h.offsetY();
double z = zi * h.scaleZ() + h.offsetZ();
uint16_t intensity;
uint8_t flags;
uint8_t classification;
int8_t scanAngleRank;
uint8_t user;
uint16_t pointSourceId;
istream >> intensity >> flags >> classification >> scanAngleRank >>
user >> pointSourceId;
uint8_t returnNum = flags & 0x07;
uint8_t numReturns = (flags >> 3) & 0x07;
uint8_t scanDirFlag = (flags >> 6) & 0x01;
uint8_t flight = (flags >> 7) & 0x01;
if (returnNum == 0 || returnNum > 5)
m_error.returnNumWarning(returnNum);
if (numReturns == 0 || numReturns > 5)
m_error.numReturnsWarning(numReturns);
data.setField(Dimension::Id::X, nextId, x);
data.setField(Dimension::Id::Y, nextId, y);
data.setField(Dimension::Id::Z, nextId, z);
data.setField(Dimension::Id::Intensity, nextId, intensity);
data.setField(Dimension::Id::ReturnNumber, nextId, returnNum);
data.setField(Dimension::Id::NumberOfReturns, nextId, numReturns);
data.setField(Dimension::Id::ScanDirectionFlag, nextId, scanDirFlag);
data.setField(Dimension::Id::EdgeOfFlightLine, nextId, flight);
data.setField(Dimension::Id::Classification, nextId, classification);
data.setField(Dimension::Id::ScanAngleRank, nextId, scanAngleRank);
data.setField(Dimension::Id::UserData, nextId, user);
data.setField(Dimension::Id::PointSourceId, nextId, pointSourceId);
if (h.hasTime())
{
double time;
istream >> time;
data.setField(Dimension::Id::GpsTime, nextId, time);
}
if (h.hasColor())
{
uint16_t red, green, blue;
istream >> red >> green >> blue;
data.setField(Dimension::Id::Red, nextId, red);
data.setField(Dimension::Id::Green, nextId, green);
data.setField(Dimension::Id::Blue, nextId, blue);
}
if (m_extraDims.size())
loadExtraDims(istream, data, nextId);
}
void LasReader::loadPointV14(PointBuffer& data, char *buf, size_t bufsize)
{
LeExtractor istream(buf, bufsize);
PointId nextId = data.size();
int32_t xi, yi, zi;
istream >> xi >> yi >> zi;
const LasHeader& h = m_lasHeader;
double x = xi * h.scaleX() + h.offsetX();
double y = yi * h.scaleY() + h.offsetY();
double z = zi * h.scaleZ() + h.offsetZ();
uint16_t intensity;
uint8_t returnInfo;
uint8_t flags;
uint8_t classification;
uint8_t user;
int16_t scanAngle;
uint16_t pointSourceId;
double gpsTime;
istream >> intensity >> returnInfo >> flags >> classification >> user >>
scanAngle >> pointSourceId >> gpsTime;
uint8_t returnNum = returnInfo & 0x0F;
uint8_t numReturns = (returnInfo >> 4) & 0x0F;
uint8_t classFlags = flags & 0x0F;
uint8_t scanChannel = (flags >> 4) & 0x03;
uint8_t scanDirFlag = (flags >> 6) & 0x01;
uint8_t flight = (flags >> 7) & 0x01;
//ABELL - Need to do something with the classFlags;
data.setField(Dimension::Id::X, nextId, x);
data.setField(Dimension::Id::Y, nextId, y);
data.setField(Dimension::Id::Z, nextId, z);
data.setField(Dimension::Id::Intensity, nextId, intensity);
data.setField(Dimension::Id::ReturnNumber, nextId, returnNum);
data.setField(Dimension::Id::NumberOfReturns, nextId, numReturns);
data.setField(Dimension::Id::ScanChannel, nextId, scanChannel);
data.setField(Dimension::Id::ScanDirectionFlag, nextId, scanDirFlag);
data.setField(Dimension::Id::EdgeOfFlightLine, nextId, flight);
data.setField(Dimension::Id::Classification, nextId, classification);
data.setField(Dimension::Id::ScanAngleRank, nextId, scanAngle * .006);
data.setField(Dimension::Id::UserData, nextId, user);
data.setField(Dimension::Id::PointSourceId, nextId, pointSourceId);
data.setField(Dimension::Id::GpsTime, nextId, gpsTime);
if (h.hasColor())
{
uint16_t red, green, blue;
istream >> red >> green >> blue;
data.setField(Dimension::Id::Red, nextId, red);
data.setField(Dimension::Id::Green, nextId, green);
data.setField(Dimension::Id::Blue, nextId, blue);
}
if (h.hasInfrared())
{
uint16_t nearInfraRed;
istream >> nearInfraRed;
data.setField(Dimension::Id::Infrared, nextId, nearInfraRed);
}
if (m_extraDims.size())
loadExtraDims(istream, data, nextId);
}
void LasReader::loadExtraDims(LeExtractor& istream, PointBuffer& data,
PointId nextId)
{
Everything e;
for (auto& dim : m_extraDims)
{
// Dimension type of None is undefined and unprocessed
if (dim.m_dimType.m_type == Dimension::Type::None)
{
istream.skip(dim.m_size);
continue;
}
istream.get(dim.m_dimType.m_type, e);
if (dim.m_dimType.m_xform.nonstandard())
{
double d = toDouble(e, dim.m_dimType.m_type);
d = d * dim.m_dimType.m_xform.m_scale +
dim.m_dimType.m_xform.m_offset;
data.setField(dim.m_dimType.m_id, nextId, d);
}
else
data.setField(dim.m_dimType.m_id, dim.m_dimType.m_type, nextId, &e);
}
}
void LasReader::done(PointContextRef ctx)
{
#ifdef PDAL_HAVE_LASZIP
m_zipPoint.reset();
m_unzipper.reset();
#endif
if (m_istream)
m_streamFactory->deallocate(*m_istream);
}
} // namespace pdal