Skip to content

Commit

Permalink
Merge pull request #2091 from PROJ-BOT/backport-2090-to-6.3
Browse files Browse the repository at this point in the history
[Backport 6.3] EngineeringCRS: when exporting to WKT1_GDAL, output unit and axis (fixes OSGeo/gdal#2347)
  • Loading branch information
rouault committed Mar 25, 2020
2 parents 60ed38a + 2eb726f commit b3fe4a0
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 26 deletions.
34 changes: 12 additions & 22 deletions src/iso19111/crs.cpp
Expand Up @@ -310,17 +310,15 @@ CRSNNPtr CRS::alterCSLinearUnit(const common::UnitOfMeasure &unit) const {
auto cartCS = util::nn_dynamic_pointer_cast<cs::CartesianCS>(
engCRS->coordinateSystem());
if (cartCS) {
auto props = createPropertyMap(this);
props.set("FORCE_OUTPUT_CS", true);
return EngineeringCRS::create(props, engCRS->datum(),
return EngineeringCRS::create(createPropertyMap(this),
engCRS->datum(),
cartCS->alterUnit(unit));
} else {
auto vertCS = util::nn_dynamic_pointer_cast<cs::VerticalCS>(
engCRS->coordinateSystem());
if (vertCS) {
auto props = createPropertyMap(this);
props.set("FORCE_OUTPUT_CS", true);
return EngineeringCRS::create(props, engCRS->datum(),
return EngineeringCRS::create(createPropertyMap(this),
engCRS->datum(),
vertCS->alterUnit(unit));
}
}
Expand Down Expand Up @@ -5316,9 +5314,7 @@ bool TemporalCRS::_isEquivalentTo(
// ---------------------------------------------------------------------------

//! @cond Doxygen_Suppress
struct EngineeringCRS::Private {
bool forceOutputCS_ = false;
};
struct EngineeringCRS::Private {};
//! @endcond

// ---------------------------------------------------------------------------
Expand Down Expand Up @@ -5376,17 +5372,6 @@ EngineeringCRS::create(const util::PropertyMap &properties,
crs->assignSelf(crs);
crs->setProperties(properties);

const auto pVal = properties.get("FORCE_OUTPUT_CS");
if (pVal) {
if (const auto genVal =
dynamic_cast<const util::BoxedValue *>(pVal->get())) {
if (genVal->type() == util::BoxedValue::Type::BOOLEAN &&
genVal->booleanValue()) {
crs->d->forceOutputCS_ = true;
}
}
}

return crs;
}

Expand All @@ -5401,11 +5386,16 @@ void EngineeringCRS::_exportToWKT(io::WKTFormatter *formatter) const {
formatter->addQuotedString(nameStr());
if (isWKT2 || !datum()->nameStr().empty()) {
datum()->_exportToWKT(formatter);
coordinateSystem()->_exportToWKT(formatter);
}
if (!isWKT2 && d->forceOutputCS_) {
if (!isWKT2) {
coordinateSystem()->axisList()[0]->unit()._exportToWKT(formatter);
}

const auto oldAxisOutputRule = formatter->outputAxis();
formatter->setOutputAxis(io::WKTFormatter::OutputAxisRule::YES);
coordinateSystem()->_exportToWKT(formatter);
formatter->setOutputAxis(oldAxisOutputRule);

ObjectUsage::baseExportToWKT(formatter);
formatter->endNode();
}
Expand Down
7 changes: 6 additions & 1 deletion test/unit/test_c_api.cpp
Expand Up @@ -2738,7 +2738,12 @@ TEST_F(CApi, proj_create_engineering_crs) {
ASSERT_NE(crs, nullptr);
auto wkt = proj_as_wkt(m_ctxt, crs, PJ_WKT1_GDAL, nullptr);
ASSERT_NE(wkt, nullptr);
EXPECT_EQ(std::string(wkt), "LOCAL_CS[\"name\"]") << wkt;
EXPECT_EQ(std::string(wkt), "LOCAL_CS[\"name\",\n"
" UNIT[\"metre\",1,\n"
" AUTHORITY[\"EPSG\",\"9001\"]],\n"
" AXIS[\"Easting\",EAST],\n"
" AXIS[\"Northing\",NORTH]]")
<< wkt;
}

// ---------------------------------------------------------------------------
Expand Down
9 changes: 7 additions & 2 deletions test/unit/test_crs.cpp
Expand Up @@ -4813,7 +4813,11 @@ TEST(crs, engineeringCRS_WKT2) {
TEST(crs, engineeringCRS_WKT1) {

auto expected = "LOCAL_CS[\"Engineering CRS\",\n"
" LOCAL_DATUM[\"Engineering datum\",32767]]";
" LOCAL_DATUM[\"Engineering datum\",32767],\n"
" UNIT[\"metre\",1,\n"
" AUTHORITY[\"EPSG\",\"9001\"]],\n"
" AXIS[\"Easting\",EAST],\n"
" AXIS[\"Northing\",NORTH]]";
EXPECT_EQ(
createEngineeringCRS()->exportToWKT(
WKTFormatter::create(WKTFormatter::Convention::WKT1_GDAL).get()),
Expand Down Expand Up @@ -5399,7 +5403,8 @@ TEST(crs, crs_alterCSLinearUnit) {
auto wkt = alteredCRS->exportToWKT(
&(WKTFormatter::create(WKTFormatter::Convention::WKT1_GDAL)
->setMultiLine(false)));
EXPECT_EQ(wkt, "LOCAL_CS[\"foo\",UNIT[\"my unit\",2]]");
EXPECT_EQ(wkt, "LOCAL_CS[\"foo\",UNIT[\"my unit\",2],"
"AXIS[\"Easting\",EAST],AXIS[\"Northing\",NORTH]]");
}

{
Expand Down
7 changes: 6 additions & 1 deletion test/unit/test_io.cpp
Expand Up @@ -3901,10 +3901,15 @@ TEST(wkt_parse, LOCAL_CS_short) {
auto cs = crs->coordinateSystem();
ASSERT_EQ(cs->axisList().size(), 2U);

auto expected_wkt = "LOCAL_CS[\"Engineering CRS\",\n"
" UNIT[\"metre\",1,\n"
" AUTHORITY[\"EPSG\",\"9001\"]],\n"
" AXIS[\"Easting\",EAST],\n"
" AXIS[\"Northing\",NORTH]]";
EXPECT_EQ(
crs->exportToWKT(
WKTFormatter::create(WKTFormatter::Convention::WKT1_GDAL).get()),
wkt);
expected_wkt);
}

// ---------------------------------------------------------------------------
Expand Down

0 comments on commit b3fe4a0

Please sign in to comment.