diff --git a/pdal/PointTable.cpp b/pdal/PointTable.cpp index e9e0214606..8b8a879ceb 100644 --- a/pdal/PointTable.cpp +++ b/pdal/PointTable.cpp @@ -47,6 +47,19 @@ MetadataNode BasePointTable::privateMetadata(const std::string& name) } +void BasePointTable::addSpatialReference(const SpatialReference& spatialRef) +{ + auto it = std::find(m_spatialRefs.begin(), m_spatialRefs.end(), spatialRef); + + // If not found, add to the beginning. + if (it == m_spatialRefs.end()) + m_spatialRefs.push_front(spatialRef); + // If not the first element, move the found element to the front. + else if (it != m_spatialRefs.begin()) + m_spatialRefs.splice(m_spatialRefs.begin(), m_spatialRefs, it); +} + + void SimplePointTable::setFieldInternal(Dimension::Id id, PointId idx, const void *value) { diff --git a/pdal/PointTable.hpp b/pdal/PointTable.hpp index 211a381103..e5b6b7b1ff 100644 --- a/pdal/PointTable.hpp +++ b/pdal/PointTable.hpp @@ -34,7 +34,7 @@ #pragma once -#include +#include #include #include "pdal/SpatialReference.hpp" @@ -48,6 +48,7 @@ namespace pdal class PDAL_DLL BasePointTable : public PointContainer { + FRIEND_TEST(PointTable, srs); friend class PointView; protected: @@ -75,8 +76,7 @@ class PDAL_DLL BasePointTable : public PointContainer } void clearSpatialReferences() { m_spatialRefs.clear(); } - void addSpatialReference(const SpatialReference& srs) - { m_spatialRefs.insert(srs); } + void addSpatialReference(const SpatialReference& srs); bool spatialReferenceUnique() const { return m_spatialRefs.size() == 1; } SpatialReference spatialReference() const @@ -103,7 +103,7 @@ class PDAL_DLL BasePointTable : public PointContainer protected: MetadataPtr m_metadata; - std::set m_spatialRefs; + std::list m_spatialRefs; PointLayout& m_layoutRef; }; typedef BasePointTable& PointTableRef; diff --git a/test/unit/PointTableTest.cpp b/test/unit/PointTableTest.cpp index aa4ebe1d4f..a8073a22ab 100644 --- a/test/unit/PointTableTest.cpp +++ b/test/unit/PointTableTest.cpp @@ -38,7 +38,8 @@ #include #include "Support.hpp" -using namespace pdal; +namespace pdal +{ TEST(PointTable, resolveType) { @@ -178,3 +179,28 @@ TEST(PointTable, userView) EXPECT_TRUE(called); } +TEST(PointTable, srs) +{ + SpatialReference srs1("GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4326\"]]"); + + SpatialReference srs2("PROJCS[\"WGS 84 / UTM zone 17N\",GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0],UNIT[\"degree\",0.0174532925199433],AUTHORITY[\"EPSG\",\"4326\"]],PROJECTION[\"Transverse_Mercator\"],PARAMETER[\"latitude_of_origin\",0],PARAMETER[\"central_meridian\",-81],PARAMETER[\"scale_factor\",0.9996],PARAMETER[\"false_easting\",500000],PARAMETER[\"false_northing\",0],UNIT[\"metre\",1,AUTHORITY[\"EPSG\",\"9001\"]],AUTHORITY[\"EPSG\",\"32617\"]]"); + + PointTable table; + + table.addSpatialReference(srs1); + table.addSpatialReference(srs1); + EXPECT_TRUE(table.spatialReferenceUnique()); + EXPECT_EQ(table.anySpatialReference(), srs1); + + table.addSpatialReference(srs2); + EXPECT_FALSE(table.spatialReferenceUnique()); + EXPECT_EQ(table.anySpatialReference(), srs2); + EXPECT_EQ(table.m_spatialRefs.size(), 2u); + + table.addSpatialReference(srs1); + EXPECT_FALSE(table.spatialReferenceUnique()); + EXPECT_EQ(table.anySpatialReference(), srs1); + EXPECT_EQ(table.m_spatialRefs.size(), 2u); +} + +} // namespace