Skip to content

Commit

Permalink
use streamFactory to avoid temp files
Browse files Browse the repository at this point in the history
  • Loading branch information
mpgerlek committed Mar 27, 2012
1 parent 681e62a commit f4cb8a0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 39 deletions.
3 changes: 2 additions & 1 deletion include/pdal/drivers/nitf/Reader.hpp
Expand Up @@ -44,6 +44,7 @@
namespace pdal
{
class PointBuffer;
class StreamFactory;

namespace drivers
{
Expand Down Expand Up @@ -87,7 +88,7 @@ class PDAL_DLL Reader : public pdal::Reader

private:
std::string m_nitfFilename;
std::string m_lasFilename; // temp file
StreamFactory* m_streamFactory;
pdal::drivers::las::Reader* m_lasReader;

Reader& operator=(const Reader&); // not implemented
Expand Down
50 changes: 12 additions & 38 deletions src/drivers/nitf/Reader.cpp
Expand Up @@ -36,6 +36,7 @@
#ifdef PDAL_HAVE_GDAL

#include <pdal/PointBuffer.hpp>
#include <pdal/StreamManager.hpp>
#include <pdal/drivers/las/Reader.hpp>

#include "nitflib.h"
Expand All @@ -49,7 +50,7 @@

namespace pdal { namespace drivers { namespace nitf {

static int extractLASFromNITF(NITFDES* psDES, const char* pszLASName)
static int extractLASFromNITF(NITFDES* psDES, boost::uint64_t& offset, boost::uint64_t& length)
{
NITFSegmentInfo* psSegInfo;

Expand All @@ -58,32 +59,8 @@ static int extractLASFromNITF(NITFDES* psDES, const char* pszLASName)

psSegInfo = psDES->psFile->pasSegmentInfo + psDES->iSegment;

GByte* pabyBuffer;

pabyBuffer = (GByte*) VSIMalloc((size_t)psSegInfo->nSegmentSize);
if (pabyBuffer == NULL)
{
throw pdal_error("Unable to allocate buffer large enough to extract NITF data!");
}

VSIFSeekL(psDES->psFile->fp, psSegInfo->nSegmentStart, SEEK_SET);
if (VSIFReadL(pabyBuffer, 1, (size_t)psSegInfo->nSegmentSize, psDES->psFile->fp) != psSegInfo->nSegmentSize)
{
VSIFree(pabyBuffer);
throw pdal_error("Unable to allocate extract NITF data!");
}

VSILFILE* fp = NULL;
fp = VSIFOpenL(pszLASName, "wb");
if (fp == NULL)
{
VSIFree(pabyBuffer);
throw pdal_error("Unable to open filename to write!");
}

VSIFWriteL(pabyBuffer, 1, (size_t)psSegInfo->nSegmentSize, fp);
VSIFCloseL(fp);
VSIFree(pabyBuffer);
offset = psSegInfo->nSegmentStart;
length = psSegInfo->nSegmentSize;

return TRUE;
}
Expand Down Expand Up @@ -168,7 +145,7 @@ static int findLIDARASegment(NITFFile* psFile)
}


static void extractNITF(const std::string& nitf_filename, std::string& las_filename)
static void extractNITF(const std::string& nitf_filename, boost::uint64_t& offset, boost::uint64_t& length)
{
NITFFile *psFile;
psFile = NITFOpen( nitf_filename.c_str(), FALSE );
Expand Down Expand Up @@ -196,10 +173,7 @@ static void extractNITF(const std::string& nitf_filename, std::string& las_filen
std::string tempfile = Utils::generate_tempfile();
//log()->get(logDEBUG) << "Using " << tempfile << " for NITF->LAS filename" << std::endl;

if (extractLASFromNITF(psDES, tempfile.c_str()))
{
las_filename = std::string(tempfile);
}
extractLASFromNITF(psDES, offset, length);

NITFDESDeaccess(psDES);

Expand All @@ -215,7 +189,7 @@ static void extractNITF(const std::string& nitf_filename, std::string& las_filen
Reader::Reader(const Options& options)
: pdal::Reader(options)
, m_nitfFilename(options.getValueOrThrow<std::string>("filename"))
, m_lasFilename("")
, m_streamFactory(NULL)
, m_lasReader(NULL)
{
addDefaultDimensions();
Expand All @@ -225,6 +199,7 @@ Reader::Reader(const Options& options)

Reader::~Reader()
{
delete m_streamFactory;
delete m_lasReader;
}

Expand All @@ -251,13 +226,12 @@ void Reader::initialize()
{
pdal::Reader::initialize();

extractNITF(m_nitfFilename, m_lasFilename);
boost::uint64_t offset, length;
extractNITF(m_nitfFilename, offset, length);

Options opts;
Option opt("filename",m_lasFilename);
opts.add(opt);
m_streamFactory = new FilenameSubsetStreamFactory(m_nitfFilename, offset, length);

m_lasReader = new pdal::drivers::las::Reader(opts);
m_lasReader = new pdal::drivers::las::Reader(m_streamFactory);
m_lasReader->initialize();

setCoreProperties(*m_lasReader);
Expand Down

0 comments on commit f4cb8a0

Please sign in to comment.