diff --git a/io/EptReader.cpp b/io/EptReader.cpp index e527d2a167..6d369e0481 100644 --- a/io/EptReader.cpp +++ b/io/EptReader.cpp @@ -250,22 +250,9 @@ void EptReader::initialize() if (!m_args->m_ogr.is_null()) { - std::vector ogr_geoms = pdal::gdal::fetchOGRGeometries(m_args->m_ogr); - - debug << "Number of OGR filter geometries: " - << ogr_geoms.size() - << std::endl; - - for (auto g: ogr_geoms) - { - OGRSpatialReference* srs = g->getSpatialReference(); - char *poWKT = 0; - srs->exportToWkt(&poWKT); - Polygon p = pdal::Polygon(g, std::string(poWKT)); - p.transform(getSpatialReference()); - m_args->m_polys.emplace_back(p); - CPLFree(poWKT); - } + auto& plist = m_args->m_polys; + std::vector ogrPolys = gdal::getPolygons(m_args->m_ogr); + plist.insert(plist.end(), ogrPolys.begin(), ogrPolys.end()); } // Transform query bounds to match point source SRS. diff --git a/pdal/GDALUtils.cpp b/pdal/GDALUtils.cpp index d22415c051..87615730a0 100644 --- a/pdal/GDALUtils.cpp +++ b/pdal/GDALUtils.cpp @@ -32,6 +32,7 @@ * OF SUCH DAMAGE. ****************************************************************************/ +#include #include #include #include @@ -778,7 +779,7 @@ OGRGeometry *createFromGeoJson(const std::string& s, std::string& srs) } -std::vector fetchOGRGeometries(const NL::json ogr) +std::vector getPolygons(const NL::json& ogr) { registerDrivers(); const NL::json& datasource = ogr.at("datasource"); @@ -826,8 +827,6 @@ std::vector fetchOGRGeometries(const NL::json ogr) throw pdal_error("Unable to read layer in fetchOGRGeometries " + layer.dump() ); } - std::vector output; - OGRFeature *poFeature (nullptr); OGRGeometry* filterGeometry(nullptr); std::string dialect("OGRSQL"); @@ -891,14 +890,13 @@ std::vector fetchOGRGeometries(const NL::json ogr) throw pdal_error("unable to execute sql query!"); } - while( (poFeature = poLayer->GetNextFeature()) != NULL ) + std::vector polys; + while ((poFeature = poLayer->GetNextFeature()) != NULL) { - OGRGeometry* poGeometry = poFeature->GetGeometryRef(); - OGRGeometry* clone = poGeometry->clone(); - output.emplace_back(clone); + polys.emplace_back(poFeature->GetGeometryRef()); OGRFeature::DestroyFeature( poFeature ); } - return output; + return polys; } } // namespace gdal diff --git a/pdal/GDALUtils.hpp b/pdal/GDALUtils.hpp index d5e7811158..14878282f6 100644 --- a/pdal/GDALUtils.hpp +++ b/pdal/GDALUtils.hpp @@ -43,8 +43,8 @@ #include #include #include -#include #include +#include #include #include @@ -59,6 +59,8 @@ class OGRGeometry; namespace pdal { +class Polygon; + namespace gdal { @@ -867,7 +869,7 @@ OGRGeometry *createFromGeoJson(const char *s); OGRGeometry *createFromWkt(const std::string& s, std::string& srs); OGRGeometry *createFromGeoJson(const std::string& s, std::string& srs); -std::vector fetchOGRGeometries(const NL::json json); +std::vector getPolygons(const NL::json& ogr); inline OGRGeometry *fromHandle(OGRGeometryH geom) { return reinterpret_cast(geom); } diff --git a/pdal/Geometry.cpp b/pdal/Geometry.cpp index 9f10738b8c..7f76bea0b2 100644 --- a/pdal/Geometry.cpp +++ b/pdal/Geometry.cpp @@ -67,6 +67,11 @@ Geometry::Geometry(Geometry&& input) : m_geom(std::move(input.m_geom)) {} +Geometry::Geometry(OGRGeometryH g) : + m_geom((reinterpret_cast(g))->clone()) +{} + + Geometry::Geometry(OGRGeometryH g, const SpatialReference& srs) : m_geom((reinterpret_cast(g))->clone()) { diff --git a/pdal/Geometry.hpp b/pdal/Geometry.hpp index 41f672f04f..7e5fae3cdb 100644 --- a/pdal/Geometry.hpp +++ b/pdal/Geometry.hpp @@ -56,6 +56,7 @@ class PDAL_DLL Geometry Geometry(Geometry&&); Geometry(const std::string& wkt_or_json, SpatialReference ref = SpatialReference()); + Geometry(OGRGeometryH g); Geometry(OGRGeometryH g, const SpatialReference& srs); public: diff --git a/pdal/Polygon.cpp b/pdal/Polygon.cpp index 530dd46e98..f63ab197ba 100644 --- a/pdal/Polygon.cpp +++ b/pdal/Polygon.cpp @@ -38,7 +38,19 @@ namespace pdal { +Polygon::Polygon(OGRGeometryH g) : Geometry(g) +{ + init(); +} + + Polygon::Polygon(OGRGeometryH g, const SpatialReference& srs) : Geometry(g, srs) +{ + init(); +} + + +void Polygon::init() { // If the handle was null, we need to create an empty polygon. if (!m_geom) diff --git a/pdal/Polygon.hpp b/pdal/Polygon.hpp index e1f9d44816..ebae18320d 100644 --- a/pdal/Polygon.hpp +++ b/pdal/Polygon.hpp @@ -58,6 +58,7 @@ class PDAL_DLL Polygon : public Geometry Polygon(const BOX2D&); Polygon(const BOX3D&); + Polygon(OGRGeometryH g); Polygon(OGRGeometryH g, const SpatialReference& srs); OGRGeometryH getOGRHandle(); @@ -78,6 +79,9 @@ class PDAL_DLL Polygon : public Geometry bool crosses(const Polygon& p) const; Ring exteriorRing() const; std::vector interiorRings() const; + +private: + void init(); }; } // namespace pdal