Skip to content

Commit

Permalink
Remove boost::iostreams::restriction from Nitf (#2931)
Browse files Browse the repository at this point in the history
* Don't use boost for NITF.

* Define WIN32 for Nitro.

* Nitro needs bad WIN32 macro.

* Add GDAL for now.  Ugh.
  • Loading branch information
abellgithub committed Feb 12, 2020
1 parent 943b904 commit 6617727
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 57 deletions.
2 changes: 1 addition & 1 deletion io/LasReader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class PDAL_DLL LasReader : public Reader, public Streamable
LasStreamIf(const std::string& filename)
{ m_istream = Utils::openFile(filename); }

~LasStreamIf()
virtual ~LasStreamIf()
{
if (m_istream)
Utils::closeFile(m_istream);
Expand Down
36 changes: 8 additions & 28 deletions plugins/nitf/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,6 @@ if (NOT NITRO_FOUND)
message(FATAL_ERROR "Can't find NITRO support required by NITF.")
endif()

if (WIN32)
add_definitions("-DNOMINMAX")
endif()

#
# NITF Reader
#
PDAL_ADD_PLUGIN(reader_libname reader nitf
FILES
io/NitfFileReader.cpp
Expand All @@ -21,18 +14,13 @@ PDAL_ADD_PLUGIN(reader_libname reader nitf
io/NitfReader.cpp
LINK_WITH
${NITRO_LIBRARIES}
INCLUDES
${PDAL_VENDOR_DIR}/pdalboost
${NLOHMANN_INCLUDE_DIR}
${ROOT_DIR}
)
if (WIN32)
target_compile_definitions(${reader_libname} PRIVATE
NOMINMAX WIN32 _WIN32)
target_compile_definitions(${reader_libname}
PRIVATE
WIN32
)
endif()
#
# NITF Writer
#

PDAL_ADD_PLUGIN(writer_libname writer nitf
FILES
Expand All @@ -43,14 +31,12 @@ PDAL_ADD_PLUGIN(writer_libname writer nitf
LINK_WITH
${NITRO_LIBRARIES}
${GDAL_LIBRARY}
INCLUDES
${PDAL_VENDOR_DIR}/pdalboost
${NLOHMANN_INCLUDE_DIR}
${ROOT_DIR}
)
if (WIN32)
target_compile_definitions(${writer_libname} PRIVATE
NOMINMAX WIN32 _WIN32)
target_compile_definitions(${writer_libname}
PRIVATE
WIN32
)
endif()

if (WITH_TESTS)
Expand All @@ -59,18 +45,12 @@ if (WITH_TESTS)
test/NitfWriterTest.cpp
LINK_WITH
${writer_libname}
INCLUDES
${ROOT_DIR}
${NLOHMANN_INCLUDE_DIR}
)

PDAL_ADD_TEST(pdal_io_nitf_reader_test
FILES
test/NitfReaderTest.cpp
LINK_WITH
${reader_libname}
INCLUDES
${ROOT_DIR}
${NLOHMANN_INCLUDE_DIR}
)
endif()
2 changes: 1 addition & 1 deletion plugins/nitf/io/NitfFileReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
#include <pdal/Metadata.hpp>

#include "MetadataReader.hpp"
#include "tre_plugins.hpp"

#include "tre_plugins.hpp"

// Set to true if you want the metadata to contain
// NITF fields that are empty; if false, those fields
Expand Down
96 changes: 69 additions & 27 deletions plugins/nitf/io/NitfReader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@

#pragma once

#include <boost/iostreams/stream.hpp>
#include <boost/iostreams/restrict.hpp>
#include <fstream>

#include <io/LasReader.hpp>
#include <pdal/StageFactory.hpp>
Expand All @@ -47,59 +46,102 @@ namespace pdal

class PDAL_DLL NitfReader : public LasReader
{
typedef pdalboost::iostreams::restriction<std::istream> RDevice;
typedef pdalboost::iostreams::stream<RDevice> RStream;

class NitfStreamIf : public LasStreamIf
template <class CharT, class Traits = std::char_traits<CharT>>
class Shiftbuf : public std::basic_filebuf<CharT, Traits>
{
public:
NitfStreamIf(const std::string& filename, uint64_t offset, uint64_t len)
typedef typename Traits::off_type off_type;
typedef typename Traits::pos_type pos_type;
typedef typename std::basic_filebuf<CharT, Traits> Base;

Shiftbuf(Shiftbuf::off_type offset) : m_offset(offset)
{
seekpos(0);
}

protected:
virtual pos_type seekpos(Shiftbuf::pos_type sp,
std::ios_base::openmode which =
std::ios_base::in | std::ios_base::binary) override
{
return Base::seekpos(sp + m_offset, which);
}

virtual pos_type seekoff(off_type off, std::ios_base::seekdir dir,
std::ios_base::openmode which =
std::ios_base::in | std::ios_base::binary) override
{
m_baseStream = FileUtils::openFile(filename);
if (m_baseStream)
{
m_rdevice.reset(new RDevice(*m_baseStream, offset, len));
m_rstream.reset(new RStream(*m_rdevice));
m_istream = m_rstream.get();
}
if (dir == std::ios_base::beg)
off += m_offset;
return Base::seekoff(off, dir, which);
}

~NitfStreamIf()
private:
off_type m_offset;
};

template <class CharT, class Traits = std::char_traits<CharT>>
class basic_ShiftStream : public std::basic_istream<CharT, Traits>
{
public:
typedef typename Traits::off_type off_type;
typedef typename std::basic_istream<CharT, Traits> Base;

basic_ShiftStream(const std::string& filename, off_type offset) :
Base(&m_buf), m_buf(offset)
{
m_rstream.reset();
m_rdevice.reset();
if (m_baseStream)
FileUtils::closeFile(m_baseStream);
m_baseStream = NULL;
m_istream = NULL;
Base::init(&m_buf);
if (!m_buf.open(filename,
std::ios_base::in | std::ios_base::binary))
Base::setstate(std::ios_base::failbit);
}

~basic_ShiftStream()
{ m_buf.close(); }

private:
std::istream *m_baseStream;
std::unique_ptr<RDevice> m_rdevice;
std::unique_ptr<RStream> m_rstream;
Shiftbuf<CharT, Traits> m_buf;
};
using ShiftStream = basic_ShiftStream<char>;

class NitfStreamIf : public LasStreamIf
{
public:
NitfStreamIf(const std::string& filename, ShiftStream::off_type off)
{
m_istream = new ShiftStream(filename, off);
}

virtual ~NitfStreamIf()
{
delete m_istream;

// Important - Otherwise the base class will attempt to use in dtor.
m_istream = nullptr;
}
};

public:
NitfReader() : LasReader(), m_offset(0), m_length(0)
{}
NitfReader& operator=(const NitfReader&) = delete;
NitfReader(const NitfReader&) = delete;

std::string getName() const;

protected:
virtual void createStream()
{
if (m_streamIf)
std::cerr << "Attempt to create stream twice!\n";
m_streamIf.reset(new NitfStreamIf(m_filename, m_offset, m_length));
m_streamIf.reset(new NitfStreamIf(m_filename, m_offset));
}

private:
uint64_t m_offset;
uint64_t m_length;

virtual void initialize(PointTableRef table);
NitfReader& operator=(const NitfReader&); // not implemented
NitfReader(const NitfReader&); // not implemented
};

} // namespace pdal

0 comments on commit 6617727

Please sign in to comment.