Skip to content

Instantly share code, notes, and snippets.

@connormanning
Created September 3, 2015 00:15
Show Gist options
  • Save connormanning/3c1205b7602ae6250ba0 to your computer and use it in GitHub Desktop.
Save connormanning/3c1205b7602ae6250ba0 to your computer and use it in GitHub Desktop.
PDAL chunked reader-only pipeline - API sample
#include <chrono>
#include <iostream>
#include <memory>
#include <pdal/BufferReader.hpp>
#include <pdal/PointView.hpp>
#include <pdal/StageFactory.hpp>
#include <pdal/StageWrapper.hpp>
#include <pdal/StatsFilter.hpp>
// Common stuff.
std::unique_ptr<pdal::Reader> createReader(std::string path)
{
pdal::StageFactory factory;
const std::string driver(factory.inferReaderDriver(path));
if (driver.empty())
{
throw std::runtime_error("No driver for " + path);
}
std::unique_ptr<pdal::Reader> reader(
static_cast<pdal::Reader*>(factory.createStage(driver)));
return reader;
}
std::chrono::high_resolution_clock::time_point now()
{
return std::chrono::high_resolution_clock::now();
}
int msSince(const std::chrono::high_resolution_clock::time_point start)
{
std::chrono::duration<double> d(now() - start);
return std::chrono::duration_cast<std::chrono::milliseconds>(d).count();
}
std::size_t points(0);
double val(0);
///////////////////////////////////////////////////////////////////////////////
// Read callback functionality - default way.
auto readCb([](pdal::PointView& view, pdal::PointId id)
{
++points;
val = view.getFieldAs<double>(pdal::Dimension::Id::X, id);
});
void readWithCb(std::string path)
{
auto reader(createReader(path));
pdal::Options options;
options.add(pdal::Option("filename", path));
reader->setOptions(options);
reader->setReadCb(readCb);
pdal::PointTable table;
reader->prepare(table);
reader->execute(table);
}
///////////////////////////////////////////////////////////////////////////////
// Chunked read functionality.
auto onInit([](pdal::BasePointTable&) {
// If you need the table for anything, now's the time. For example,
// initializing a reprojection filter for use during onData().
});
auto onData([](pdal::PointView& view)
{
for (std::size_t i(0); i < view.size(); ++i)
{
++points;
val = view.getFieldAs<double>(pdal::Dimension::Id::X, i);
}
});
void readChunked(std::string path)
{
// Optionally, customize the chunkBytes, pass your own pdal::Options, or
// use a custom pre-populated pdal::Layout derivative.
//
// For point-at-a-time functionality, use a chunkBytes value of 1, since
// this value will be rounded up to the nearest full point size.
auto reader(createReader(path));
reader->read(path, onInit, onData);
}
///////////////////////////////////////////////////////////////////////////////
int main()
{
bool chunked(true);
std::string path("/Users/connor/data/nepal/nepal.las");
auto start(now());
if (!chunked) readWithCb(path);
else readChunked(path);
int ms(msSince(start));
std::cout << "Used " << (chunked ? "CHUNKED" : "current") << " method\n";
std::cout << points << " points\n";
std::cout << "Done in " << ms << " ms." << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment