From ad42ef9e826a7864885c929632630b218796d9d3 Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Tue, 4 Dec 2018 18:29:35 +0100 Subject: WKT1 export of LOCAL_CS: add UNIT if explicitly set --- src/crs.cpp | 47 ++++++++++++++++++++++++++++++++++++++++++++--- test/unit/test_crs.cpp | 10 ++++++++++ 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/src/crs.cpp b/src/crs.cpp index 9f1f53c7..546cfb0a 100644 --- a/src/crs.cpp +++ b/src/crs.cpp @@ -301,6 +301,29 @@ CRSNNPtr CRS::alterCSLinearUnit(const common::UnitOfMeasure &unit) const { } } + { + auto engCRS = dynamic_cast(this); + if (engCRS) { + auto cartCS = util::nn_dynamic_pointer_cast( + engCRS->coordinateSystem()); + if (cartCS) { + auto props = createPropertyMap(this); + props.set("FORCE_OUTPUT_CS", true); + return EngineeringCRS::create(props, engCRS->datum(), + cartCS->alterUnit(unit)); + } else { + auto vertCS = util::nn_dynamic_pointer_cast( + engCRS->coordinateSystem()); + if (vertCS) { + auto props = createPropertyMap(this); + props.set("FORCE_OUTPUT_CS", true); + return EngineeringCRS::create(props, engCRS->datum(), + vertCS->alterUnit(unit)); + } + } + } + } + return NN_NO_CHECK( std::dynamic_pointer_cast(shared_from_this().as_nullable())); } @@ -4376,7 +4399,9 @@ bool TemporalCRS::_isEquivalentTo( // --------------------------------------------------------------------------- //! @cond Doxygen_Suppress -struct EngineeringCRS::Private {}; +struct EngineeringCRS::Private { + bool forceOutputCS_ = false; +}; //! @endcond // --------------------------------------------------------------------------- @@ -4389,12 +4414,13 @@ EngineeringCRS::~EngineeringCRS() = default; EngineeringCRS::EngineeringCRS(const datum::EngineeringDatumNNPtr &datumIn, const cs::CoordinateSystemNNPtr &csIn) - : SingleCRS(datumIn.as_nullable(), nullptr, csIn), d(nullptr) {} + : SingleCRS(datumIn.as_nullable(), nullptr, csIn), + d(internal::make_unique()) {} // --------------------------------------------------------------------------- EngineeringCRS::EngineeringCRS(const EngineeringCRS &other) - : SingleCRS(other), d(nullptr) {} + : SingleCRS(other), d(internal::make_unique(*(other.d))) {} // --------------------------------------------------------------------------- @@ -4432,6 +4458,18 @@ EngineeringCRS::create(const util::PropertyMap &properties, auto crs(EngineeringCRS::nn_make_shared(datumIn, csIn)); crs->assignSelf(crs); crs->setProperties(properties); + + auto oIter = properties.find("FORCE_OUTPUT_CS"); + if (oIter != properties.end()) { + if (auto genVal = util::nn_dynamic_pointer_cast( + oIter->second)) { + if (genVal->type() == util::BoxedValue::Type::BOOLEAN && + genVal->booleanValue()) { + crs->d->forceOutputCS_ = true; + } + } + } + return crs; } @@ -4448,6 +4486,9 @@ void EngineeringCRS::_exportToWKT(io::WKTFormatter *formatter) const { datum()->_exportToWKT(formatter); coordinateSystem()->_exportToWKT(formatter); } + if (!isWKT2 && d->forceOutputCS_) { + coordinateSystem()->axisList()[0]->unit()._exportToWKT(formatter); + } ObjectUsage::baseExportToWKT(formatter); formatter->endNode(); } diff --git a/test/unit/test_crs.cpp b/test/unit/test_crs.cpp index e0505fea..44b84401 100644 --- a/test/unit/test_crs.cpp +++ b/test/unit/test_crs.cpp @@ -4976,6 +4976,16 @@ TEST(crs, crs_alterCSLinearUnit) { EXPECT_EQ(cs->axisList()[0]->unit().conversionToSI(), 2); } + { + auto obj = WKTParser().createFromWKT("LOCAL_CS[\"foo\"]"); + auto crs = nn_dynamic_pointer_cast(obj); + auto alteredCRS = crs->alterCSLinearUnit(UnitOfMeasure("my unit", 2)); + auto wkt = alteredCRS->exportToWKT( + &(WKTFormatter::create(WKTFormatter::Convention::WKT1_GDAL) + ->setMultiLine(false))); + EXPECT_EQ(wkt, "LOCAL_CS[\"foo\",UNIT[\"my unit\",2]]"); + } + { // Not implemented on compoundCRS auto crs = -- cgit v1.2.3