Skip to content

Commit

Permalink
Catch EptSupport exceptions and rethrow with throwError utility in re…
Browse files Browse the repository at this point in the history
…ader/addon-writer.
  • Loading branch information
connormanning committed Mar 5, 2019
1 parent 4e906be commit 4cc05e2
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 76 deletions.
38 changes: 23 additions & 15 deletions io/EptAddonWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,24 +98,32 @@ void EptAddonWriter::ready(PointTableRef table)
if (meta.findChild("info").value().empty())
throwError("EPT writer must descend from EPT reader");

const auto info(parse(meta.findChild("info").value<std::string>()));
const auto keys(parse(meta.findChild("keys").value<std::string>()));
m_hierarchyStep = meta.findChild("step").value<uint64_t>();
m_info.reset(new EptInfo(info));

for (const std::string s : keys.getMemberNames())
try
{
m_hierarchy[Key(s)] = keys[s].asUInt64();
}
const auto info(parse(meta.findChild("info").value<std::string>()));
const auto keys(parse(meta.findChild("keys").value<std::string>()));

m_hierarchyStep = meta.findChild("step").value<uint64_t>();
m_info.reset(new EptInfo(info));

const PointLayout& layout(*table.layout());
for (const std::string path : m_addonsArg->getMemberNames())
for (const std::string s : keys.getMemberNames())
{
m_hierarchy[Key(s)] = keys[s].asUInt64();
}

const PointLayout& layout(*table.layout());
for (const std::string path : m_addonsArg->getMemberNames())
{
const auto endpoint(
m_arbiter->getEndpoint(arbiter::fs::expandTilde(path)));
const std::string dimName((*m_addonsArg)[path].asString());
const Dimension::Id id(layout.findDim(dimName));
m_addons.emplace_back(new Addon(layout, endpoint, id));
}
}
catch (std::exception& e)
{
const auto endpoint(
m_arbiter->getEndpoint(arbiter::fs::expandTilde(path)));
const std::string dimName((*m_addonsArg)[path].asString());
const Dimension::Id id(layout.findDim(dimName));
m_addons.emplace_back(new Addon(layout, endpoint, id));
throwError(e.what());
}
}

Expand Down
104 changes: 74 additions & 30 deletions io/EptReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,29 @@ void EptReader::initialize()
m_pool.reset(new Pool(threads));

debug << "Endpoint: " << m_ep->prefixedRoot() << std::endl;
m_info.reset(new EptInfo(parse(m_ep->get("ept.json"))));
try
{
m_info.reset(new EptInfo(parse(m_ep->get("ept.json"))));
}
catch (std::exception& e)
{
throwError(e.what());
}

debug << "Got EPT info" << std::endl;
debug << "SRS: " << m_info->srs() << std::endl;
setSpatialReference(m_info->srs());

// Figure out our query parameters.
m_queryBounds = m_args.bounds();
handleOriginQuery();
try
{
handleOriginQuery();
}
catch (std::exception& e)
{
throwError(e.what());
}

// Figure out our max depth.
const double queryResolution(m_args.resolution());
Expand Down Expand Up @@ -229,29 +244,43 @@ void EptReader::handleOriginQuery()
const Json::Value& found(sources[static_cast<Json::ArrayIndex>(
m_queryOriginId)]);

BOX3D q(toBox3d(found["bounds"]));
try
{
BOX3D q(toBox3d(found["bounds"]));

// Clip the bounds to the queried origin bounds. Don't just overwrite it -
// it's possible that both a bounds and an origin are specified.
m_queryBounds.clip(q);
// Clip the bounds to the queried origin bounds. Don't just overwrite
// it - it's possible that both a bounds and an origin are specified.
m_queryBounds.clip(q);

log()->get(LogLevel::Debug) << "Query origin " <<
m_queryOriginId << ": " << found["id"].asString() << std::endl;
log()->get(LogLevel::Debug) << "Query origin " <<
m_queryOriginId << ": " << found["id"].asString() << std::endl;
}
catch (std::exception& e)
{
throwError(e.what());
}
}

QuickInfo EptReader::inspect()
{
QuickInfo qi;

initialize();
try
{
initialize();

qi.m_bounds = toBox3d(m_info->json()["boundsConforming"]);
qi.m_srs = m_info->srs();
qi.m_pointCount = m_info->points();
qi.m_bounds = toBox3d(m_info->json()["boundsConforming"]);
qi.m_srs = m_info->srs();
qi.m_pointCount = m_info->points();

for (Json::ArrayIndex i(0); i < m_info->schema().size(); ++i)
for (Json::ArrayIndex i(0); i < m_info->schema().size(); ++i)
{
qi.m_dimNames.push_back(m_info->schema()[i]["name"].asString());
}
}
catch (std::exception& e)
{
qi.m_dimNames.push_back(m_info->schema()[i]["name"].asString());
throwError(e.what());
}

qi.m_valid = true;
Expand Down Expand Up @@ -320,26 +349,34 @@ void EptReader::addDimensions(PointLayoutPtr layout)
m_xyzTransforms[1] = m_dimTypes[dimIndex(D::Y)].m_xform;
m_xyzTransforms[2] = m_dimTypes[dimIndex(D::Z)].m_xform;

for (const std::string dimName : m_args.addons().getMemberNames())
try
{
std::string root(m_args.addons()[dimName].asString());
const std::string postfix("ept-addon.json");
if (Utils::endsWith(root, postfix))
for (const std::string dimName : m_args.addons().getMemberNames())
{
root = root.substr(0, root.size() - postfix.size());
}
root = arbiter::fs::expandTilde(root);
std::string root(m_args.addons()[dimName].asString());
const std::string postfix("ept-addon.json");
if (Utils::endsWith(root, postfix))
{
root = root.substr(0, root.size() - postfix.size());
}
root = arbiter::fs::expandTilde(root);

const arbiter::Endpoint ep(m_arbiter->getEndpoint(root));
const Json::Value addonInfo(parse(ep.get("ept-addon.json")));
const Dimension::Type type(getType(addonInfo));
const Dimension::Id id(layout->registerOrAssignDim(dimName, type));
const arbiter::Endpoint ep(m_arbiter->getEndpoint(root));
const Json::Value addonInfo(parse(ep.get("ept-addon.json")));
const Dimension::Type type(getType(addonInfo));
const Dimension::Id id(layout->registerOrAssignDim(dimName, type));

m_addons.emplace_back(new Addon(*layout, ep, id));
m_addons.emplace_back(new Addon(*layout, ep, id));

log()->get(LogLevel::Debug) << "Registering addon dim " << dimName <<
": " << Dimension::interpretationName(m_addons.back()->type()) <<
", from " << root << std::endl;
log()->get(LogLevel::Debug) << "Registering addon dim " <<
dimName << ": " <<
Dimension::interpretationName(m_addons.back()->type()) <<
", from " << root << std::endl;
}
}
catch (std::exception& e)
{
throwError(e.what());
}
}

Expand All @@ -353,7 +390,14 @@ void EptReader::ready(PointTableRef table)
m_overlaps.clear();

// Determine all overlapping data files we'll need to fetch.
overlaps();
try
{
overlaps();
}
catch (std::exception& e)
{
throwError(e.what());
}

uint64_t overlapPoints(0);
Json::Value json;
Expand Down
45 changes: 14 additions & 31 deletions io/private/EptSupport.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@
namespace pdal
{

class ept_error : public pdal_error
{
public:
ept_error(std::string msg) : pdal_error(msg) { }
};

inline Dimension::Type getType(const Json::Value& dim)
{
if (dim.isMember("scale"))
Expand Down Expand Up @@ -118,7 +124,7 @@ inline Json::Value parse(const std::string& data)
const std::string jsonError(reader.getFormattedErrorMessages());
if (!jsonError.empty())
{
throw pdal_error("Error during parsing: " + jsonError);
throw ept_error("Error during parsing: " + jsonError);
}
}

Expand All @@ -129,36 +135,13 @@ inline BOX3D toBox3d(const Json::Value& b)
{
if (!b.isArray() || b.size() != 6)
{
throw pdal_error("Invalid bounds specification: " + b.toStyledString());
throw ept_error("Invalid bounds specification: " + b.toStyledString());
}

return BOX3D(b[0].asDouble(), b[1].asDouble(), b[2].asDouble(),
b[3].asDouble(), b[4].asDouble(), b[5].asDouble());
}

inline std::array<double, 3> toArray3(const Json::Value& json)
{
std::array<double, 3> result;

if (json.isArray() && json.size() == 3)
{
result[0] = json[0].asDouble();
result[1] = json[1].asDouble();
result[2] = json[2].asDouble();
}
else if (json.isNumeric())
{
result[0] = result[1] = result[2] = json.asDouble();
}
else
{
throw pdal_error("Invalid scale specification: " +
json.toStyledString());
}

return result;
}

class PDAL_DLL Key
{
// An EPT key representation (see https://git.io/fAiBh). A depth/X/Y/Z key
Expand All @@ -169,7 +152,7 @@ class PDAL_DLL Key
{
const std::vector<std::string> tokens(Utils::split(s, '-'));
if (tokens.size() != 4)
throw pdal_error("Invalid EPT KEY: " + s);
throw ept_error("Invalid EPT KEY: " + s);
d = std::stoull(tokens[0]);
x = std::stoull(tokens[1]);
y = std::stoull(tokens[2]);
Expand Down Expand Up @@ -198,7 +181,7 @@ class PDAL_DLL Key
case 3: return b.maxx;
case 4: return b.maxy;
case 5: return b.maxz;
default: throw pdal_error("Invalid Key[] index");
default: throw ept_error("Invalid Key[] index");
}
}

Expand All @@ -209,7 +192,7 @@ class PDAL_DLL Key
case 0: return x;
case 1: return y;
case 2: return z;
default: throw pdal_error("Invalid Key::idAt index");
default: throw ept_error("Invalid Key::idAt index");
}
}

Expand Down Expand Up @@ -316,7 +299,7 @@ class PDAL_DLL EptInfo
else if (dt == "binary")
m_dataType = DataType::Binary;
else
throw pdal_error("Unrecognized EPT dataType: " + dt);
throw ept_error("Unrecognized EPT dataType: " + dt);
}

const BOX3D& bounds() const { return m_bounds; }
Expand Down Expand Up @@ -432,7 +415,7 @@ class PDAL_DLL ShallowPointTable : public BasePointTable
protected:
virtual PointId addPoint() override
{
throw pdal_error("Cannot add points to ShallowPointTable");
throw ept_error("Cannot add points to ShallowPointTable");
}

virtual char* getPoint(PointId i) override
Expand Down Expand Up @@ -553,7 +536,7 @@ class PDAL_DLL Pool
std::unique_lock<std::mutex> lock(m_mutex);
if (!m_running)
{
throw pdal_error("Attempted to add a task to a stopped Pool");
throw ept_error("Attempted to add a task to a stopped Pool");
}

m_produceCv.wait(lock, [this]()
Expand Down

0 comments on commit 4cc05e2

Please sign in to comment.