Skip to content

Commit

Permalink
Simplify logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
abellgithub committed Mar 9, 2020
1 parent 8790296 commit 068326a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 27 deletions.
3 changes: 1 addition & 2 deletions io/LasWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -503,12 +503,11 @@ void LasWriter::addGeotiffVlrs()
/// \return Whether the VLR was added.
bool LasWriter::addWktVlr()
{
std::string wkt = m_srs.getWKT();
std::string wkt = m_srs.getWKT1();
if (wkt.empty())
return false;

// LAS 1.4 requires WKTv1
wkt = SpatialReference::convertToWKT1(wkt);
std::vector<uint8_t> wktBytes(wkt.begin(), wkt.end());
// This tacks a NULL to the end of the data, which is required by the spec.
wktBytes.resize(wktBytes.size() + 1, 0);
Expand Down
41 changes: 17 additions & 24 deletions pdal/SpatialReference.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -438,35 +438,28 @@ std::string SpatialReference::prettyWkt(const std::string& wkt)
return outWkt;
}

std::string SpatialReference::convertToWKT1(const std::string& wkt)
std::string SpatialReference::getWKT1() const
{
std::string outWkt(wkt);

// WKT flavors only exist in GDAL 3.x+
#if GDAL_VERSION_MAJOR >= 3
#if GDAL_VERSION_MAJOR < 3
return getWkt();
#endif

std::string wkt = getWKT();
OGRScopedSpatialReference srs = ogrCreateSrs(wkt);
if (!srs)
return outWkt;

char *buf = nullptr;

const char* apszOptions[] = { "FORMAT=WKT1_GDAL", nullptr };
srs->exportToWkt(&buf, apszOptions);

// If we couldn't convert to WKT1, return empty
if (buf == nullptr)
if (srs)
{
outWkt = "";
}
else
{
outWkt = buf;
CPLFree(buf);
}
char *buf = nullptr;
const char* apszOptions[] = { "FORMAT=WKT1_GDAL", nullptr };
srs->exportToWkt(&buf, apszOptions);

#endif
return outWkt;
// If we couldn't convert to WKT1, return empty
if (buf)
{
wkt = buf;
CPLFree(buf);
}
}
return wkt;
}


Expand Down
2 changes: 1 addition & 1 deletion pdal/SpatialReference.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ class PDAL_DLL SpatialReference
bool valid() const;

std::string getWKT() const;
std::string getWKT1() const;

/// Parse the string starting at position `pos` as a spatial reference.
/// \param s String to parse.
Expand Down Expand Up @@ -164,7 +165,6 @@ class PDAL_DLL SpatialReference
static int calculateZone(double lon, double lat);
static bool isWKT(const std::string& wkt);
static std::string prettyWkt(const std::string& wkt);
static std::string convertToWKT1(const std::string& wkt);

private:
std::string m_wkt;
Expand Down

0 comments on commit 068326a

Please sign in to comment.