Skip to content

Commit

Permalink
support setting data record lengths for LAS that are not just the fix…
Browse files Browse the repository at this point in the history
…ed set defined by the point formats
  • Loading branch information
hobu committed May 10, 2012
1 parent 1120a3b commit a5500a9
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 30 deletions.
3 changes: 2 additions & 1 deletion include/pdal/drivers/las/Header.hpp
Expand Up @@ -238,6 +238,7 @@ class PDAL_DLL LasHeader
/// available for the file in the form of a liblas.org VLR schema record,
/// These extra bytes are available via liblas::Point::GetExtraData().
boost::uint16_t GetDataRecordLength() const;
void SetDataRecordLength(boost::uint16_t v);

/// Get total number of point records stored in the LAS file.
boost::uint32_t GetPointRecordsCount() const;
Expand Down Expand Up @@ -380,7 +381,7 @@ class PDAL_DLL LasHeader
PointOffsets m_offsets;
bool m_isCompressed;
boost::uint32_t m_headerPadding;

boost::uint16_t m_dataRecordLength;
PointFormat m_pointFormat;

Bounds<double> m_bounds;
Expand Down
1 change: 1 addition & 0 deletions include/pdal/drivers/las/Writer.hpp
Expand Up @@ -113,6 +113,7 @@ class PDAL_DLL Writer : public pdal::Writer

bool m_headerInitialized;
boost::uint64_t m_streamOffset; // the first byte of the LAS file
void setOptions();

Writer& operator=(const Writer&); // not implemented
Writer(const Writer&); // not implemented
Expand Down
20 changes: 14 additions & 6 deletions src/drivers/las/Header.cpp
Expand Up @@ -260,13 +260,23 @@ pdal::drivers::las::PointFormat LasHeader::getPointFormat() const

void LasHeader::setPointFormat(pdal::drivers::las::PointFormat v)
{
pdal::drivers::las::PointFormat old = m_pointFormat;
boost::uint16_t oldpad = m_dataRecordLength - Support::getPointDataSize(old);
m_pointFormat = v;
m_dataRecordLength = Support::getPointDataSize(v) + oldpad;
}

void LasHeader::SetDataRecordLength(boost::uint16_t v)
{
boost::uint16_t requiredsize = Support::getPointDataSize(m_pointFormat);
if (requiredsize < v)
throw std::invalid_argument("record size too small to hold current point format required size");
m_dataRecordLength = v;
}
boost::uint16_t LasHeader::GetDataRecordLength() const
{
// No matter what the schema says, this must be a short in size.
return pdal::drivers::las::Support::getPointDataSize(m_pointFormat);
return m_dataRecordLength;
//return pdal::drivers::las::Support::getPointDataSize(m_pointFormat);
}

boost::uint32_t LasHeader::GetPointRecordsCount() const
Expand Down Expand Up @@ -406,24 +416,22 @@ void LasHeader::initialize()

std::memset(m_signature, 0, eFileSignatureSize);
std::strncpy(m_signature, FileSignature, eFileSignatureSize);
// m_signature = Header::FileSignature;

std::memset(m_systemId, 0, eSystemIdSize);
std::strncpy(m_systemId, SystemIdentifier, eSystemIdSize);
// m_systemId = Header::SystemIdentifier;

std::memset(m_softwareId, 0, eSoftwareIdSize);
std::strncpy(m_softwareId, SoftwareIdentifier, eSoftwareIdSize);
// m_softwareId = Header::SoftwareIdentifier;

m_pointRecordsByReturn.resize(ePointsByReturnSize);

// Zero scale value is useless, so we need to use a small value.
SetScale(0.01, 0.01, 0.01);
SetScale(1.0, 1.0, 1.0);

m_isCompressed = false;
m_headerPadding = 0;
m_compressionInfo = std::string("");
m_dataRecordLength = Support::getPointDataSize(m_pointFormat);
}


Expand Down
56 changes: 33 additions & 23 deletions src/drivers/las/Writer.cpp
Expand Up @@ -63,30 +63,11 @@ Writer::Writer(Stage& prevStage, const Options& options)
, m_headerInitialized(false)
, m_streamOffset(0)
{
setGeneratingSoftware(options.getValueOrDefault<std::string>("software_id", LasHeader::SoftwareIdentifier));
setDate((boost::uint16_t)options.getValueOrDefault<boost::uint32_t>("creation_year", 0),
(boost::uint16_t)options.getValueOrDefault<boost::uint32_t>("creation_doy", 0));
setPointFormat(static_cast<PointFormat>(options.getValueOrDefault<boost::uint32_t>("format", 3)));
setSystemIdentifier(options.getValueOrDefault<std::string>("system_id", LasHeader::SystemIdentifier));

setHeaderPadding(options.getValueOrDefault<boost::uint32_t>("header_padding", 0));

if (options.hasOption("a_srs"))
{
setSpatialReference(options.getValueOrDefault<std::string>("a_srs",""));
}
setCompressed(options.getValueOrDefault("compression", false));

return;
setOptions();
return;
}


Writer::Writer(Stage& prevStage, std::ostream* ostream)
: pdal::Writer(prevStage, Options::none())
, m_streamManager(ostream)
, m_numPointsWritten(0)
, m_headerInitialized(false)
, m_streamOffset(0)
void Writer::setOptions()
{
setGeneratingSoftware(getOptions().getValueOrDefault<std::string>("software_id", LasHeader::SoftwareIdentifier));
setDate((boost::uint16_t)getOptions().getValueOrDefault<boost::uint32_t>("year", 0),
Expand All @@ -100,7 +81,24 @@ Writer::Writer(Stage& prevStage, std::ostream* ostream)
setSpatialReference(getOptions().getValueOrDefault<std::string>("a_srs",""));
}
setCompressed(getOptions().getValueOrDefault("compression", false));
return;
m_lasHeader.SetFileSourceId(getOptions().getValueOrDefault<boost::uint16_t>("filesourceid", 0));
try
{
boost::uint16_t record_length = getOptions().getValueOrThrow<boost::uint16_t>("datarecordlength");
m_lasHeader.SetDataRecordLength(record_length);
} catch (pdal::option_not_found&) {};


}
Writer::Writer(Stage& prevStage, std::ostream* ostream)
: pdal::Writer(prevStage, Options::none())
, m_streamManager(ostream)
, m_numPointsWritten(0)
, m_headerInitialized(false)
, m_streamOffset(0)
{
setOptions();
return;
}


Expand Down Expand Up @@ -138,6 +136,7 @@ const Options Writer::getDefaultOptions() const
Option year("creation_year", 2011, "4-digit year value for file");
Option system_id("system_id", LasHeader::SystemIdentifier, "System ID for this file");
Option software_id("software_id", LasHeader::SoftwareIdentifier, "Software ID for this file");
Option filesourceid("filesourceid", 0, "File Source ID for this file");
Option header_padding("header_padding", 0, "Header padding (space between end of VLRs and beginning of point data)");
Option set_metadata("forward_metadata", false, "forward metadata into the file as necessary");

Expand Down Expand Up @@ -315,6 +314,17 @@ void Writer::writeBufferBegin(PointBuffer const& data)
<< "from metadata" << std::endl;
} catch (std::bad_cast&) {}
}
boost::optional<pdal::metadata::Entry const&> filesourceid = metadata.getEntryOptional("filesourceid");
if (filesourceid && useMetadata)
{
try
{
m_lasHeader.SetFileSourceId(filesourceid->cast<boost::uint16_t>());
log()->get(logDEBUG) << "Setting file source id to "
<< filesourceid->cast<boost::uint16_t>()
<< "from metadata" << std::endl;
} catch (std::bad_cast&) {}
}

LasHeaderWriter lasHeaderWriter(m_lasHeader, m_streamManager.ostream(), m_streamOffset);
lasHeaderWriter.write();
Expand Down

0 comments on commit a5500a9

Please sign in to comment.