Skip to content

Commit

Permalink
Merge commit '5decb5400be5d8dd6a7256799e254d7efcce43b7' into 1.7-main…
Browse files Browse the repository at this point in the history
…tenance
  • Loading branch information
hobu committed Apr 9, 2018
2 parents fb002d5 + 5decb54 commit 8979e3a
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
47 changes: 44 additions & 3 deletions io/GeotiffSupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ PDAL_C_START

// These functions are available from GDAL, but they
// aren't exported.
char CPL_DLL * GTIFGetOGISDefn(GTIF*, GTIFDefn*);
int CPL_DLL GTIFSetFromOGISDefn(GTIF*, const char*);
char PDAL_DLL * GTIFGetOGISDefn(GTIF*, GTIFDefn*);
int PDAL_DLL GTIFSetFromOGISDefn(GTIF*, const char*);

PDAL_C_END

Expand Down Expand Up @@ -77,9 +77,20 @@ struct GeotiffCtx

}

#pragma pack(push)
#pragma pack(1)
struct Entry
{
uint16_t key;
uint16_t location;
uint16_t count;
uint16_t offset;
};
#pragma pack(pop)

GeotiffSrs::GeotiffSrs(const std::vector<uint8_t>& directoryRec,
const std::vector<uint8_t>& doublesRec,
const std::vector<uint8_t>& asciiRec)
const std::vector<uint8_t>& asciiRec, LogPtr log) : m_log(log)
{
GeotiffCtx ctx;

Expand All @@ -103,6 +114,9 @@ GeotiffSrs::GeotiffSrs(const std::vector<uint8_t>& directoryRec,
if (directoryRec.size() < declaredSize)
return;

validateDirectory((const Entry *)(header + 1), header->numKeys,
doublesRec.size() / sizeof(double), asciiRec.size());

uint8_t *dirData = const_cast<uint8_t *>(directoryRec.data());
ST_SetKey(ctx.tiff, GEOTIFF_DIRECTORY_RECORD_ID,
(1 + header->numKeys) * 4, STT_SHORT, (void *)dirData);
Expand Down Expand Up @@ -137,6 +151,33 @@ GeotiffSrs::GeotiffSrs(const std::vector<uint8_t>& directoryRec,
}


void GeotiffSrs::validateDirectory(const Entry *ent, size_t numEntries,
size_t numDoubles, size_t asciiSize)
{
for (size_t i = 0; i < numEntries; ++i)
{
if (ent->count == 0)
m_log->get(LogLevel::Warning) << "Geotiff directory contains " <<
"key " << ent->key << " with 0 count." << std::endl;
if (ent->location == 0 && ent->count != 1)
m_log->get(LogLevel::Error) << "Geotiff directory contains key " <<
ent->key << " with short entry and more than one value." <<
std::endl;
if (ent->location == GEOTIFF_DIRECTORY_RECORD_ID)
if (ent->offset + ent->count > numDoubles)
m_log->get(LogLevel::Error) << "Geotiff directory contains " <<
"key " << ent->key << " with count/offset outside of valid "
"range of doubles record." << std::endl;
if (ent->location == GEOTIFF_ASCII_RECORD_ID)
if (ent->offset + ent->count > asciiSize)
m_log->get(LogLevel::Error) << "Geotiff directory contains " <<
" key " << ent->key << " with count/offset outside of "
"valid range of ascii record." << std::endl;
ent++;
}
}


GeotiffTags::GeotiffTags(const SpatialReference& srs)
{
if (srs.empty())
Expand Down
9 changes: 8 additions & 1 deletion io/GeotiffSupport.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

#pragma once

#include <pdal/Log.hpp>
#include <pdal/SpatialReference.hpp>

namespace pdal
Expand All @@ -48,16 +49,22 @@ namespace Geotiff
};
}

struct Entry;

class GeotiffSrs
{
public:
GeotiffSrs(const std::vector<uint8_t>& directoryRec,
const std::vector<uint8_t>& doublesRec,
const std::vector<uint8_t>& asciiRec);
const std::vector<uint8_t>& asciiRec, LogPtr log);
SpatialReference srs() const
{ return m_srs; }
private:
SpatialReference m_srs;
LogPtr m_log;

void validateDirectory(const Entry *ent, size_t numEntries,
size_t numDoubles, size_t asciiSize);
};

class GeotiffTags
Expand Down
2 changes: 1 addition & 1 deletion io/LasHeader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ void LasHeader::setSrsFromGeotiff()
}
std::vector<uint8_t> asciiRec(data, data + dataLen);

GeotiffSrs geotiff(directoryRec, doublesRec, asciiRec);
GeotiffSrs geotiff(directoryRec, doublesRec, asciiRec, m_log);
SpatialReference gtiffSrs = geotiff.srs();
if (!gtiffSrs.empty())
m_srs = gtiffSrs;
Expand Down

0 comments on commit 8979e3a

Please sign in to comment.