Skip to content

Commit

Permalink
Merge pull request #1276 from PDAL/pts-reader
Browse files Browse the repository at this point in the history
add readers.pts for reading PTS files directly
  • Loading branch information
hobu committed Aug 5, 2016
2 parents f3946ad + c10c703 commit 3412611
Show file tree
Hide file tree
Showing 9 changed files with 426 additions and 0 deletions.
32 changes: 32 additions & 0 deletions doc/stages/readers.pts.rst
@@ -0,0 +1,32 @@
.. _readers.pts:

readers.pts
============

The **PTS reader** reads data from PTS files.


Example Pipeline
----------------

.. code-block:: json
{
"pipeline":[
{
"type":"readers.pts",
"filename":"test.pts"
},
{
"type":"writers.text",
"filename":"outputfile.txt"
}
]
}
Options
-------

filename
text file to read [Required]

1 change: 1 addition & 0 deletions io/CMakeLists.txt
Expand Up @@ -8,6 +8,7 @@ add_subdirectory(gdal)
add_subdirectory(null)
add_subdirectory(optech)
add_subdirectory(ply)
add_subdirectory(pts)
add_subdirectory(qfit)
add_subdirectory(sbet)
add_subdirectory(text)
Expand Down
14 changes: 14 additions & 0 deletions io/pts/CMakeLists.txt
@@ -0,0 +1,14 @@
#
# Text driver CMake configuration
#

set(srcs
PtsReader.cpp
)

set(incs
PtsReader.hpp
)

PDAL_ADD_DRIVER(reader pts "${srcs}" "${incs}" objects)
set(PDAL_TARGET_OBJECTS ${PDAL_TARGET_OBJECTS} ${objects} PARENT_SCOPE)
165 changes: 165 additions & 0 deletions io/pts/PtsReader.cpp
@@ -0,0 +1,165 @@
/******************************************************************************
* Copyright (c) 2016, Hobu Inc., info@hobu.co
*
* 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. 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 <pdal/PDALUtils.hpp>
#include <pdal/util/Algorithm.hpp>

#include "PtsReader.hpp"

#include <pdal/pdal_macros.hpp>

namespace pdal
{

static PluginInfo const s_info = PluginInfo(
"readers.pts",
"Pts Reader",
"http://pdal.io/stages/readers.pts.html" );

CREATE_STATIC_PLUGIN(1, 0, PtsReader, Reader, s_info)

std::string PtsReader::getName() const { return s_info.name; }

void PtsReader::initialize(PointTableRef table)
{
m_istream = Utils::openFile(m_filename);
if (!m_istream)
{
std::ostringstream oss;
oss << getName() << ": Unable to open pts file '" <<
m_filename << "'.";
throw pdal_error(oss.str());
}

std::string buf;
std::getline(*m_istream, buf);

// Very first line is point count
Utils::fromString(buf, m_PointCount);
Utils::closeFile(m_istream);
}


void PtsReader::addDimensions(PointLayoutPtr layout)
{
// Dimensions are fixed in PTS

m_dims.push_back(Dimension::Id::X);
m_dims.push_back(Dimension::Id::Y);
m_dims.push_back(Dimension::Id::Z);
m_dims.push_back(Dimension::Id::Reflectance);
m_dims.push_back(Dimension::Id::Red);
m_dims.push_back(Dimension::Id::Green);
m_dims.push_back(Dimension::Id::Blue);

for (auto d: m_dims)
{
layout->registerDim(d);
}

}


void PtsReader::ready(PointTableRef table)
{
m_istream = Utils::openFile(m_filename);
if (!m_istream)
{
std::ostringstream oss;
oss << getName() << ": Unable to open text file '" <<
m_filename << "'.";
throw pdal_error(oss.str());
}

// Skip header line.
std::string buf;
std::getline(*m_istream, buf);
}


point_count_t PtsReader::read(PointViewPtr view, point_count_t numPts)
{
PointId idx = view->size();

point_count_t cnt = 0;
size_t line = 1;

while (m_istream->good() && cnt < numPts)
{
std::string buf;
StringList fields;

std::getline(*m_istream, buf);
line++;
if (buf.empty())
continue;

fields = Utils::split2(buf, m_separator);
if (fields.size() != m_dims.size())
{
log()->get(LogLevel::Error) << "Line " << line <<
" in '" << m_filename << "' contains " << fields.size() <<
" fields when " << m_dims.size() << " were expected. "
"Ignoring." << std::endl;
continue;
}

double d;
for (size_t i = 0; i < fields.size(); ++i)
{
if (!Utils::fromString(fields[i], d))
{
log()->get(LogLevel::Error) << "Can't convert "
"field '" << fields[i] << "' to numeric value on line " <<
line << " in '" << m_filename << "'. Setting to 0." <<
std::endl;
d = 0;
}
view->setField(m_dims[i], idx, d);
}
cnt++;
idx++;
}
return cnt;
}


void PtsReader::done(PointTableRef table)
{
Utils::closeFile(m_istream);
}


} // namespace pdal

107 changes: 107 additions & 0 deletions io/pts/PtsReader.hpp
@@ -0,0 +1,107 @@
/******************************************************************************
* Copyright (c) 2016, Hobu Inc. (info@hobu.co)
*
* 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. 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.
****************************************************************************/

#pragma once

#include <istream>

#include <pdal/Reader.hpp>
#include <pdal/plugin.hpp>

extern "C" int32_t PtsReader_ExitFunc();
extern "C" PF_ExitFunc PtsReader_InitPlugin();

namespace pdal
{

class PDAL_DLL PtsReader : public Reader
{
public:
static void * create();
static int32_t destroy(void *);
std::string getName() const;

PtsReader() : m_separator(' '), m_PointCount(0), m_istream(NULL)
{}

private:
/**
Initialize the reader by opening the file and reading the header line
for the point count.
Closes the file on completion.
\param table Point table being initialized.
*/
virtual void initialize(PointTableRef table);

/**
Add dimensions fixed dimensions for PTS format
http://w3.leica-geosystems.com/kb/?guid=5532D590-114C-43CD-A55F-FE79E5937CB2
\param layout Layout to which the dimenions are added.
*/
virtual void addDimensions(PointLayoutPtr layout);

/**
Reopen the file in preparation for reading.
\param table Point table to make ready.
*/
virtual void ready(PointTableRef table);

/**
Read up to numPts points into the \ref view.
\param view PointView in which to insert point data.
\param numPts Maximum number of points to read.
\return Number of points read.
*/
virtual point_count_t read(PointViewPtr view, point_count_t numPts);

/**
Close input file.
\param table PointTable we're done with.
*/
virtual void done(PointTableRef table);

private:
char m_separator;
point_count_t m_PointCount;
std::istream *m_istream;
StringList m_dimNames;
Dimension::IdList m_dims;
};

} // namespace pdal
4 changes: 4 additions & 0 deletions src/StageFactory.cpp
Expand Up @@ -70,6 +70,7 @@
#include <optech/OptechReader.hpp>
#include <buffer/BufferReader.hpp>
#include <ply/PlyReader.hpp>
#include <pts/PtsReader.hpp>
#include <qfit/QfitReader.hpp>
#include <sbet/SbetReader.hpp>
#include <terrasolid/TerrasolidReader.hpp>
Expand Down Expand Up @@ -105,6 +106,7 @@ StringList StageFactory::extensions(const std::string& driver)
{ "readers.nitf", { "nitf", "nsf", "ntf" } },
{ "readers.pcd", { "pcd" } },
{ "readers.ply", { "ply" } },
{ "readers.pts", { "pts" } },
{ "readers.qfit", { "qi" } },
{ "readers.rxp", { "rxp" } },
{ "readers.sbet", { "sbet" } },
Expand Down Expand Up @@ -147,6 +149,7 @@ std::string StageFactory::inferReaderDriver(const std::string& filename)
{ "ntf", "readers.nitf" },
{ "pcd", "readers.pcd" },
{ "ply", "readers.ply" },
{ "pts", "readers.pts" },
{ "qi", "readers.qfit" },
{ "rxp", "readers.rxp" },
{ "sbet", "readers.sbet" },
Expand Down Expand Up @@ -250,6 +253,7 @@ StageFactory::StageFactory(bool no_plugins)
PluginManager::initializePlugin(LasReader_InitPlugin);
PluginManager::initializePlugin(OptechReader_InitPlugin);
PluginManager::initializePlugin(PlyReader_InitPlugin);
PluginManager::initializePlugin(PtsReader_InitPlugin);
PluginManager::initializePlugin(QfitReader_InitPlugin);
PluginManager::initializePlugin(SbetReader_InitPlugin);
PluginManager::initializePlugin(TerrasolidReader_InitPlugin);
Expand Down
20 changes: 20 additions & 0 deletions test/data/pts/test.pts
@@ -0,0 +1,20 @@
19
3.980972 -2.006119 -0.010086 -1035 97 59 38
-25.050461 -2.689682 -0.124985 -1164 32 19 11
-21.257339 -16.137466 -2.000961 -1397 15 10 10
-17.998978 -24.000290 -0.014267 -935 38 25 19
-19.475418 -24.485733 -0.103165 -1212 17 13 12
-19.483017 -24.488449 -0.121323 -1218 16 12 11
-19.377609 -24.499924 -1.078964 -1311 13 9 8
-19.504745 -24.508835 -1.603256 -1249 19 14 13
-19.503983 -24.513992 -1.596115 -1292 15 12 12
-19.501144 -24.502396 -1.659653 -1261 20 15 12
-19.500870 -24.506485 -1.639847 -1275 19 14 11
-19.500046 -24.518112 -1.600815 -1275 19 15 11
-19.500992 -24.505173 -1.619644 -1230 20 15 12
-19.505936 -24.500900 -1.617966 -1248 21 16 13
-19.502487 -24.524185 -1.573502 -1348 17 13 13
-19.503708 -24.515854 -1.588211 -1331 17 14 13
-19.503311 -24.518600 -1.580551 -1326 17 12 12
-19.502060 -24.526901 -1.565781 -1323 14 12 11
-19.501053 -24.533554 -1.533951 -1307 17 13 12
2 changes: 2 additions & 0 deletions test/unit/CMakeLists.txt
Expand Up @@ -26,6 +26,7 @@ include_directories(
${PROJECT_SOURCE_DIR}/io/las
${PROJECT_SOURCE_DIR}/io/optech
${PROJECT_SOURCE_DIR}/io/ply
${PROJECT_SOURCE_DIR}/io/pts
${PROJECT_SOURCE_DIR}/io/qfit
${PROJECT_SOURCE_DIR}/io/sbet
${PROJECT_SOURCE_DIR}/io/text
Expand Down Expand Up @@ -92,6 +93,7 @@ PDAL_ADD_TEST(pdal_io_las_writer_test FILES io/las/LasWriterTest.cpp)
PDAL_ADD_TEST(pdal_io_optech_test FILES io/optech/OptechReaderTest.cpp)
PDAL_ADD_TEST(pdal_io_ply_reader_test FILES io/ply/PlyReaderTest.cpp)
PDAL_ADD_TEST(pdal_io_ply_writer_test FILES io/ply/PlyWriterTest.cpp)
PDAL_ADD_TEST(pdal_io_pts_reader_test FILES io/pts/PtsReaderTest.cpp)
PDAL_ADD_TEST(pdal_io_qfit_test FILES io/qfit/QFITReaderTest.cpp)
PDAL_ADD_TEST(pdal_io_sbet_reader_test FILES io/sbet/SbetReaderTest.cpp)
PDAL_ADD_TEST(pdal_io_sbet_writer_test FILES io/sbet/SbetWriterTest.cpp)
Expand Down

0 comments on commit 3412611

Please sign in to comment.