From 89130c37646b5a785cc364ee3cd8f0ce63c82f26 Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Thu, 4 Aug 2022 16:20:07 +0200 Subject: [PATCH] WKT parser: fix issue when parsing some WKT1 with Hotine_Oblique_Mercator_Azimuth_Center and ignoring rectified_grid_angle (fixes #3279) --- src/iso19111/io.cpp | 15 +++++++++++---- test/unit/test_io.cpp | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/src/iso19111/io.cpp b/src/iso19111/io.cpp index 6c5bc85b17..d58854e999 100644 --- a/src/iso19111/io.cpp +++ b/src/iso19111/io.cpp @@ -7604,10 +7604,17 @@ WKTParser::guessDialect(const std::string &wkt) noexcept { for (const auto &pointerKeyword : wkt1_keywords) { if (ci_starts_with(wkt, *pointerKeyword)) { - if (ci_find(wkt, "GEOGCS[\"GCS_") != std::string::npos || - (!ci_starts_with(wkt, WKTConstants::LOCAL_CS) && - ci_find(wkt, "AXIS[") == std::string::npos && - ci_find(wkt, "AUTHORITY[") == std::string::npos)) { + if ((ci_find(wkt, "GEOGCS[\"GCS_") != std::string::npos || + (!ci_starts_with(wkt, WKTConstants::LOCAL_CS) && + ci_find(wkt, "AXIS[") == std::string::npos && + ci_find(wkt, "AUTHORITY[") == std::string::npos)) && + // WKT1:GDAL and WKT1:ESRI have both a + // Hotine_Oblique_Mercator_Azimuth_Center If providing a + // WKT1:GDAL without AXIS, we may wrongly detect it as WKT1:ESRI + // and skip the rectified_grid_angle parameter cf + // /~https://github.com/OSGeo/PROJ/issues/3279 + ci_find(wkt, "PARAMETER[\"rectified_grid_angle") == + std::string::npos) { return WKTGuessedDialect::WKT1_ESRI; } diff --git a/test/unit/test_io.cpp b/test/unit/test_io.cpp index 6135991661..6af58507d2 100644 --- a/test/unit/test_io.cpp +++ b/test/unit/test_io.cpp @@ -1926,6 +1926,38 @@ TEST(wkt_parse, wkt1_hotine_oblique_mercator_with_rectified_grid_angle) { // --------------------------------------------------------------------------- +TEST(wkt_parse, + wkt1_hotine_oblique_mercator_azimuth_center_with_rectified_grid_angle) { + auto wkt = "PROJCS[\"unknown\"," + "GEOGCS[\"unknown\"," + " DATUM[\"WGS_1984\"," + " SPHEROID[\"WGS 84\",6378137,298.257223563]]," + " PRIMEM[\"Greenwich\",0]," + " UNIT[\"degree\",0.0174532925199433]]," + "PROJECTION[\"Hotine_Oblique_Mercator_Azimuth_Center\"]," + "PARAMETER[\"latitude_of_center\",0]," + "PARAMETER[\"longitude_of_center\",0]," + "PARAMETER[\"azimuth\",30]," + "PARAMETER[\"rectified_grid_angle\",0]," + "PARAMETER[\"scale_factor\",1]," + "PARAMETER[\"false_easting\",0]," + "PARAMETER[\"false_northing\",0]," + "UNIT[\"metre\",1]]"; + + auto obj = WKTParser().createFromWKT(wkt); + auto crs = nn_dynamic_pointer_cast(obj); + ASSERT_TRUE(crs != nullptr); + + // Check that we have not overriden rectified_grid_angle + auto got_wkt = crs->exportToWKT( + WKTFormatter::create(WKTFormatter::Convention::WKT1_GDAL).get()); + EXPECT_TRUE(got_wkt.find("PARAMETER[\"rectified_grid_angle\",0]") != + std::string::npos) + << got_wkt; +} + +// --------------------------------------------------------------------------- + TEST(proj_export, wkt2_hotine_oblique_mercator_without_rectified_grid_angle) { auto wkt = "PROJCRS[\"NAD_1983_Michigan_GeoRef_Meters\",\n" " BASEGEOGCRS[\"NAD83(1986)\",\n"