LCOV - code coverage report
Current view: top level - autotest/cpp - test_osr.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 351 351 100.0 %
Date: 2024-11-21 22:18:42 Functions: 59 59 100.0 %

          Line data    Source code
       1             : ///////////////////////////////////////////////////////////////////////////////
       2             : //
       3             : // Project:  C++ Test Suite for GDAL/OGR
       4             : // Purpose:  OGR Spatial Reference general features test.
       5             : // Author:   Mateusz Loskot <mateusz@loskot.net>
       6             : //
       7             : ///////////////////////////////////////////////////////////////////////////////
       8             : // Copyright (c) 2006, Mateusz Loskot <mateusz@loskot.net>
       9             : /*
      10             :  * SPDX-License-Identifier: MIT
      11             :  ****************************************************************************/
      12             : 
      13             : #include "gdal_unit_test.h"
      14             : 
      15             : #include "cpl_string.h"
      16             : #include "ogr_srs_api.h"
      17             : #include "ogr_spatialref.h"
      18             : 
      19             : #include <algorithm>
      20             : #include <cmath>
      21             : #include <string>
      22             : 
      23             : #include "gtest_include.h"
      24             : 
      25             : namespace
      26             : {
      27             : 
      28             : // Common fixture with test data
      29             : struct test_osr : public ::testing::Test
      30             : {
      31             :     OGRErr err_ = OGRERR_NONE;
      32             :     OGRSpatialReferenceH srs_ = nullptr;
      33             : 
      34          14 :     void SetUp() override
      35             :     {
      36          14 :         srs_ = OSRNewSpatialReference(nullptr);
      37          14 :         ASSERT_TRUE(nullptr != srs_);
      38             :     }
      39             : 
      40          14 :     void TearDown() override
      41             :     {
      42          14 :         OSRDestroySpatialReference(srs_);
      43          14 :         srs_ = nullptr;
      44          14 :     }
      45             : };
      46             : 
      47             : // Test UTM WGS84 coordinate system and its various items
      48           4 : TEST_F(test_osr, UTM_WGS84)
      49             : {
      50           1 :     err_ = OSRSetUTM(srs_, 11, TRUE);
      51           1 :     ASSERT_EQ(err_, OGRERR_NONE);
      52             : 
      53           1 :     err_ = OSRSetWellKnownGeogCS(srs_, "WGS84");
      54           1 :     ASSERT_EQ(err_, OGRERR_NONE);
      55             : 
      56           1 :     double val = 0;
      57             : 
      58           1 :     val = OSRGetProjParm(srs_, SRS_PP_CENTRAL_MERIDIAN, -1111, &err_);
      59           1 :     EXPECT_NEAR(val, -117.0, .00000000000010);
      60             : 
      61           1 :     val = OSRGetProjParm(srs_, SRS_PP_LATITUDE_OF_ORIGIN, -1111, &err_);
      62           1 :     EXPECT_NEAR(val, 0.0, .00000000000010);
      63             : 
      64           1 :     val = OSRGetProjParm(srs_, SRS_PP_SCALE_FACTOR, -1111, &err_);
      65           1 :     EXPECT_NEAR(val, 0.9996, .00000000000010);
      66             : 
      67           1 :     val = OSRGetProjParm(srs_, SRS_PP_FALSE_EASTING, -1111, &err_);
      68           1 :     EXPECT_NEAR(val, 500000, .00000000000010);
      69             : 
      70           1 :     val = OSRGetProjParm(srs_, SRS_PP_FALSE_NORTHING, -1111, &err_);
      71           1 :     EXPECT_NEAR(val, 0.0, .00000000000010);
      72             : 
      73           1 :     EXPECT_STREQ(OSRGetAuthorityName(srs_, "GEOGCS"), "EPSG");
      74           1 :     EXPECT_STREQ(OSRGetAuthorityCode(srs_, "GEOGCS"), "4326");
      75             : 
      76           1 :     EXPECT_STREQ(OSRGetAuthorityName(srs_, "DATUM"), "EPSG");
      77           1 :     EXPECT_STREQ(OSRGetAuthorityCode(srs_, "DATUM"), "6326");
      78             : }
      79             : 
      80             : // Simple default NAD83 State Plane zone
      81           4 : TEST_F(test_osr, NAD83_State_Plane)
      82             : {
      83             :     // California III NAD83
      84           1 :     OSRSetStatePlane(srs_, 403, 1);
      85             : 
      86           1 :     double val = 0;
      87             : 
      88           1 :     val = OSRGetProjParm(srs_, SRS_PP_STANDARD_PARALLEL_1, -1111, &err_);
      89           1 :     EXPECT_NEAR(val, 38.43333333333333, 1e-12);
      90             : 
      91           1 :     val = OSRGetProjParm(srs_, SRS_PP_STANDARD_PARALLEL_2, -1111, &err_);
      92           1 :     EXPECT_NEAR(val, 37.06666666666667, 1e-12);
      93             : 
      94           1 :     val = OSRGetProjParm(srs_, SRS_PP_LATITUDE_OF_ORIGIN, -1111, &err_);
      95           1 :     EXPECT_NEAR(val, 36.5, 1e-12);
      96             : 
      97           1 :     val = OSRGetProjParm(srs_, SRS_PP_CENTRAL_MERIDIAN, -1111, &err_);
      98           1 :     EXPECT_NEAR(val, -120.5, 1e-12);
      99             : 
     100           1 :     val = OSRGetProjParm(srs_, SRS_PP_FALSE_EASTING, -1111, &err_);
     101           1 :     EXPECT_NEAR(val, 2000000.0, 1e-12);
     102             : 
     103           1 :     val = OSRGetProjParm(srs_, SRS_PP_FALSE_NORTHING, -1111, &err_);
     104           1 :     EXPECT_NEAR(val, 500000.0, 1e-12);
     105             : 
     106           1 :     EXPECT_STREQ(OSRGetAuthorityName(srs_, "GEOGCS"), "EPSG");
     107           1 :     EXPECT_STREQ(OSRGetAuthorityCode(srs_, "GEOGCS"), "4269");
     108             : 
     109           1 :     EXPECT_STREQ(OSRGetAuthorityName(srs_, "DATUM"), "EPSG");
     110           1 :     EXPECT_STREQ(OSRGetAuthorityCode(srs_, "DATUM"), "6269");
     111             : 
     112           1 :     EXPECT_STREQ(OSRGetAuthorityName(srs_, "PROJCS"), "EPSG");
     113           1 :     EXPECT_STREQ(OSRGetAuthorityCode(srs_, "PROJCS"), "26943");
     114             : 
     115           1 :     EXPECT_STREQ(OSRGetAuthorityName(srs_, "PROJCS|UNIT"), "EPSG");
     116           1 :     EXPECT_STREQ(OSRGetAuthorityCode(srs_, "PROJCS|UNIT"), "9001");
     117           1 : }
     118             : 
     119             : // NAD83 State Plane zone, but overridden to be in Feet
     120           4 : TEST_F(test_osr, NAD83_State_Plane_Feet)
     121             : {
     122             :     // California III NAD83 (feet)
     123           1 :     OSRSetStatePlaneWithUnits(srs_, 403, 1, "Foot", 0.3048006096012192);
     124             : 
     125           1 :     double val = 0;
     126             : 
     127           1 :     val = OSRGetProjParm(srs_, SRS_PP_STANDARD_PARALLEL_1, -1111, &err_);
     128           1 :     EXPECT_NEAR(val, 38.43333333333333, 1e-12);
     129             : 
     130           1 :     val = OSRGetProjParm(srs_, SRS_PP_STANDARD_PARALLEL_2, -1111, &err_);
     131           1 :     EXPECT_NEAR(val, 37.06666666666667, 1e-12);
     132             : 
     133           1 :     val = OSRGetProjParm(srs_, SRS_PP_LATITUDE_OF_ORIGIN, -1111, &err_);
     134           1 :     EXPECT_NEAR(val, 36.5, 1e-12);
     135             : 
     136           1 :     val = OSRGetProjParm(srs_, SRS_PP_CENTRAL_MERIDIAN, -1111, &err_);
     137           1 :     EXPECT_NEAR(val, -120.5, 1e-12);
     138             : 
     139           1 :     val = OSRGetProjParm(srs_, SRS_PP_FALSE_EASTING, -1111, &err_);
     140           1 :     EXPECT_NEAR(val, 6561666.666666667, 1e-12);
     141             : 
     142           1 :     val = OSRGetProjParm(srs_, SRS_PP_FALSE_NORTHING, -1111, &err_);
     143           1 :     EXPECT_NEAR(val, 1640416.666666667, 1e-12);
     144             : 
     145           1 :     EXPECT_STREQ(OSRGetAuthorityName(srs_, "GEOGCS"), "EPSG");
     146           1 :     EXPECT_STREQ(OSRGetAuthorityCode(srs_, "GEOGCS"), "4269");
     147             : 
     148           1 :     EXPECT_STREQ(OSRGetAuthorityName(srs_, "DATUM"), "EPSG");
     149           1 :     EXPECT_STREQ(OSRGetAuthorityCode(srs_, "DATUM"), "6269");
     150             : 
     151           1 :     EXPECT_TRUE(nullptr == OSRGetAuthorityName(srs_, "PROJCS"));
     152           1 :     EXPECT_TRUE(nullptr == OSRGetAuthorityCode(srs_, "PROJCS|UNIT"));
     153             : 
     154           1 :     char *unitsName = nullptr;
     155           1 :     val = OSRGetLinearUnits(srs_, &unitsName);
     156           1 :     ASSERT_TRUE(nullptr != unitsName);
     157           1 :     EXPECT_STREQ(unitsName, "Foot");
     158             : }
     159             : 
     160             : // Translate a coordinate system with NAD shift into to PROJ.4 and back.
     161             : // Also, verify that the TOWGS84 parameters are preserved.
     162           4 : TEST_F(test_osr, NAD_shift)
     163             : {
     164           1 :     err_ = OSRSetGS(srs_, -117.0, 100000.0, 100000);
     165           1 :     EXPECT_EQ(err_, OGRERR_NONE);
     166             : 
     167           1 :     err_ = OSRSetGeogCS(srs_, "Test GCS", "Test Datum", "WGS84",
     168             :                         SRS_WGS84_SEMIMAJOR, SRS_WGS84_INVFLATTENING, nullptr,
     169             :                         0, nullptr, 0);
     170           1 :     EXPECT_EQ(err_, OGRERR_NONE);
     171             : 
     172           1 :     err_ = OSRSetTOWGS84(srs_, 1, 2, 3, 0, 0, 0, 0);
     173           1 :     EXPECT_EQ(err_, OGRERR_NONE);
     174             : 
     175           1 :     const int coeffSize = 7;
     176           1 :     double coeff[coeffSize] = {0};
     177           1 :     const double expect[coeffSize] = {1, 2, 3, 0, 0, 0, 0};
     178             : 
     179           1 :     err_ = OSRGetTOWGS84(srs_, coeff, 7);
     180           1 :     EXPECT_EQ(err_, OGRERR_NONE);
     181           1 :     EXPECT_TRUE(std::equal(coeff, coeff + coeffSize, expect));
     182           1 :     OSRSetLinearUnits(srs_, "Metre", 1);
     183             : 
     184           1 :     char *proj4 = nullptr;
     185           1 :     err_ = OSRExportToProj4(srs_, &proj4);
     186           1 :     EXPECT_EQ(err_, OGRERR_NONE);
     187             : 
     188           1 :     OGRSpatialReferenceH srs2 = nullptr;
     189           1 :     srs2 = OSRNewSpatialReference(nullptr);
     190             : 
     191           1 :     err_ = OSRImportFromProj4(srs2, proj4);
     192           1 :     EXPECT_EQ(err_, OGRERR_NONE);
     193             : 
     194           1 :     err_ = OSRGetTOWGS84(srs2, coeff, 7);
     195           1 :     EXPECT_EQ(err_, OGRERR_NONE);
     196           1 :     EXPECT_TRUE(std::equal(coeff, coeff + coeffSize, expect));
     197             : 
     198           1 :     OSRDestroySpatialReference(srs2);
     199           1 :     CPLFree(proj4);
     200           1 : }
     201             : 
     202             : // Test URN support for OGC:CRS84
     203           4 : TEST_F(test_osr, URN_OGC_CRS84)
     204             : {
     205           1 :     err_ = OSRSetFromUserInput(srs_, "urn:ogc:def:crs:OGC:1.3:CRS84");
     206           1 :     ASSERT_EQ(err_, OGRERR_NONE);
     207             : 
     208           1 :     char *wkt1 = nullptr;
     209           1 :     err_ = OSRExportToWkt(srs_, &wkt1);
     210           1 :     EXPECT_EQ(err_, OGRERR_NONE);
     211           1 :     EXPECT_TRUE(nullptr != wkt1);
     212             : 
     213           1 :     CPLFree(wkt1);
     214             : }
     215             : 
     216             : // Test URN support for EPSG
     217           4 : TEST_F(test_osr, URN_EPSG)
     218             : {
     219           1 :     err_ = OSRSetFromUserInput(srs_, "urn:ogc:def:crs:EPSG::4326");
     220           1 :     ASSERT_EQ(err_, OGRERR_NONE);
     221             : 
     222           1 :     char *wkt1 = nullptr;
     223           1 :     err_ = OSRExportToWkt(srs_, &wkt1);
     224           1 :     EXPECT_EQ(err_, OGRERR_NONE);
     225           1 :     EXPECT_TRUE(nullptr != wkt1);
     226             : 
     227           1 :     err_ = OSRSetFromUserInput(srs_, "EPSGA:4326");
     228           1 :     EXPECT_EQ(err_, OGRERR_NONE);
     229             : 
     230           1 :     char *wkt2 = nullptr;
     231           1 :     err_ = OSRExportToWkt(srs_, &wkt2);
     232           1 :     EXPECT_EQ(err_, OGRERR_NONE);
     233           1 :     EXPECT_TRUE(nullptr != wkt2);
     234             : 
     235           1 :     EXPECT_STREQ(wkt1, wkt2);
     236           1 :     CPLFree(wkt1);
     237           1 :     CPLFree(wkt2);
     238             : }
     239             : 
     240             : // Test URN support for auto projection
     241           4 : TEST_F(test_osr, URN_AUTO)
     242             : {
     243           1 :     err_ = OSRSetFromUserInput(srs_, "urn:ogc:def:crs:OGC::AUTO42001:-117:33");
     244           1 :     ASSERT_EQ(err_, OGRERR_NONE);
     245             : 
     246           2 :     OGRSpatialReference oSRS;
     247           1 :     oSRS.importFromEPSG(32611);
     248             : 
     249           1 :     EXPECT_TRUE(oSRS.IsSame(OGRSpatialReference::FromHandle(srs_)));
     250             : }
     251             : 
     252             : // Test StripTOWGS84IfKnownDatum
     253           4 : TEST_F(test_osr, StripTOWGS84IfKnownDatum)
     254             : {
     255             :     // Not a boundCRS
     256             :     {
     257           2 :         OGRSpatialReference oSRS;
     258           1 :         oSRS.importFromEPSG(4326);
     259           1 :         EXPECT_TRUE(!oSRS.StripTOWGS84IfKnownDatum());
     260             :     }
     261             :     // Custom boundCRS --> do not strip TOWGS84
     262             :     {
     263           2 :         OGRSpatialReference oSRS;
     264           1 :         oSRS.SetFromUserInput(
     265             :             "+proj=longlat +ellps=GRS80 +towgs84=1,2,3,4,5,6,7");
     266           1 :         EXPECT_TRUE(!oSRS.StripTOWGS84IfKnownDatum());
     267           1 :         double vals[7] = {0};
     268           1 :         EXPECT_TRUE(oSRS.GetTOWGS84(vals, 7) == OGRERR_NONE);
     269             :     }
     270             :     // BoundCRS whose base CRS has a known code --> strip TOWGS84
     271             :     {
     272           2 :         OGRSpatialReference oSRS;
     273           1 :         oSRS.importFromEPSG(4326);
     274           1 :         oSRS.SetTOWGS84(1, 2, 3, 4, 5, 6, 7);
     275           1 :         EXPECT_TRUE(oSRS.StripTOWGS84IfKnownDatum());
     276           1 :         double vals[7] = {0};
     277           1 :         EXPECT_TRUE(oSRS.GetTOWGS84(vals, 7) != OGRERR_NONE);
     278             :     }
     279             :     // BoundCRS whose datum code is known --> strip TOWGS84
     280             :     {
     281           2 :         OGRSpatialReference oSRS;
     282           1 :         oSRS.SetFromUserInput("GEOGCS[\"bar\","
     283             :                               "DATUM[\"foo\","
     284             :                               "SPHEROID[\"WGS 84\",6378137,298.257223563],"
     285             :                               "TOWGS84[1,2,3,4,5,6,7],"
     286             :                               "AUTHORITY[\"FOO\",\"1\"]],"
     287             :                               "PRIMEM[\"Greenwich\",0],"
     288             :                               "UNIT[\"degree\",0.0174532925199433]]");
     289           1 :         EXPECT_TRUE(oSRS.StripTOWGS84IfKnownDatum());
     290           1 :         double vals[7] = {0};
     291           1 :         EXPECT_TRUE(oSRS.GetTOWGS84(vals, 7) != OGRERR_NONE);
     292             :     }
     293             :     // BoundCRS whose datum name is known --> strip TOWGS84
     294             :     {
     295           2 :         OGRSpatialReference oSRS;
     296           1 :         oSRS.SetFromUserInput("GEOGCS[\"WGS 84\","
     297             :                               "DATUM[\"WGS_1984\","
     298             :                               "SPHEROID[\"WGS 84\",6378137,298.257223563],"
     299             :                               "TOWGS84[1,2,3,4,5,6,7]],"
     300             :                               "PRIMEM[\"Greenwich\",0],"
     301             :                               "UNIT[\"degree\",0.0174532925199433]]");
     302           1 :         EXPECT_TRUE(oSRS.StripTOWGS84IfKnownDatum());
     303           1 :         double vals[7] = {0};
     304           1 :         EXPECT_TRUE(oSRS.GetTOWGS84(vals, 7) != OGRERR_NONE);
     305             :     }
     306             :     // BoundCRS whose datum name is unknown --> do not strip TOWGS84
     307             :     {
     308           2 :         OGRSpatialReference oSRS;
     309           1 :         oSRS.SetFromUserInput("GEOGCS[\"WGS 84\","
     310             :                               "DATUM[\"i am unknown\","
     311             :                               "SPHEROID[\"WGS 84\",6378137,298.257223563],"
     312             :                               "TOWGS84[1,2,3,4,5,6,7]],"
     313             :                               "PRIMEM[\"Greenwich\",0],"
     314             :                               "UNIT[\"degree\",0.0174532925199433]]");
     315           1 :         EXPECT_TRUE(!oSRS.StripTOWGS84IfKnownDatum());
     316           1 :         double vals[7] = {0};
     317           1 :         EXPECT_TRUE(oSRS.GetTOWGS84(vals, 7) == OGRERR_NONE);
     318             :     }
     319           1 : }
     320             : 
     321             : // Test GetEPSGGeogCS
     322           4 : TEST_F(test_osr, GetEPSGGeogCS)
     323             : {
     324             :     // When export to WKT1 is not possible
     325           2 :     OGRSpatialReference oSRS;
     326           1 :     oSRS.SetFromUserInput(
     327             :         "PROJCRS[\"World_Vertical_Perspective\",\n"
     328             :         "    BASEGEOGCRS[\"WGS 84\",\n"
     329             :         "        DATUM[\"World Geodetic System 1984\",\n"
     330             :         "            ELLIPSOID[\"WGS 84\",6378137,298.257223563,\n"
     331             :         "                LENGTHUNIT[\"metre\",1]]],\n"
     332             :         "        PRIMEM[\"Greenwich\",0,\n"
     333             :         "            ANGLEUNIT[\"Degree\",0.0174532925199433]]],\n"
     334             :         "    CONVERSION[\"World_Vertical_Perspective\",\n"
     335             :         "        METHOD[\"Vertical Perspective\",\n"
     336             :         "            ID[\"EPSG\",9838]],\n"
     337             :         "        PARAMETER[\"Latitude of topocentric origin\",0,\n"
     338             :         "            ANGLEUNIT[\"Degree\",0.0174532925199433],\n"
     339             :         "            ID[\"EPSG\",8834]],\n"
     340             :         "        PARAMETER[\"Longitude of topocentric origin\",0,\n"
     341             :         "            ANGLEUNIT[\"Degree\",0.0174532925199433],\n"
     342             :         "            ID[\"EPSG\",8835]],\n"
     343             :         "        PARAMETER[\"Viewpoint height\",35800000,\n"
     344             :         "            LENGTHUNIT[\"metre\",1],\n"
     345             :         "            ID[\"EPSG\",8840]]],\n"
     346             :         "    CS[Cartesian,2],\n"
     347             :         "        AXIS[\"(E)\",east,\n"
     348             :         "            ORDER[1],\n"
     349             :         "            LENGTHUNIT[\"metre\",1]],\n"
     350             :         "        AXIS[\"(N)\",north,\n"
     351             :         "            ORDER[2],\n"
     352             :         "            LENGTHUNIT[\"metre\",1]],\n"
     353             :         "    USAGE[\n"
     354             :         "        SCOPE[\"Not known.\"],\n"
     355             :         "        AREA[\"World.\"],\n"
     356             :         "        BBOX[-90,-180,90,180]],\n"
     357             :         "    ID[\"ESRI\",54049]]");
     358           1 :     EXPECT_EQ(oSRS.GetEPSGGeogCS(), 4326);
     359           1 : }
     360             : 
     361             : // Test GetOGCURN
     362           4 : TEST_F(test_osr, GetOGCURN)
     363             : {
     364             :     {
     365           2 :         OGRSpatialReference oSRS;
     366           1 :         char *pszRet = oSRS.GetOGCURN();
     367           1 :         EXPECT_TRUE(pszRet == nullptr);
     368           1 :         CPLFree(pszRet);
     369             :     }
     370             :     {
     371           2 :         OGRSpatialReference oSRS;
     372           1 :         oSRS.SetFromUserInput("+proj=longlat");
     373           1 :         char *pszRet = oSRS.GetOGCURN();
     374           1 :         EXPECT_TRUE(pszRet == nullptr);
     375           1 :         CPLFree(pszRet);
     376             :     }
     377             : 
     378             :     {
     379           2 :         OGRSpatialReference oSRS;
     380           1 :         oSRS.importFromEPSG(32631);
     381           1 :         char *pszRet = oSRS.GetOGCURN();
     382           1 :         EXPECT_TRUE(pszRet != nullptr);
     383           1 :         if (pszRet)
     384             :         {
     385           1 :             EXPECT_STREQ(pszRet, "urn:ogc:def:crs:EPSG::32631");
     386             :         }
     387           1 :         CPLFree(pszRet);
     388             :     }
     389             : 
     390             :     {
     391           2 :         OGRSpatialReference oSRS;
     392           1 :         oSRS.SetFromUserInput("EPSG:32631+5773");
     393           1 :         char *pszRet = oSRS.GetOGCURN();
     394           1 :         EXPECT_TRUE(pszRet != nullptr);
     395           1 :         if (pszRet)
     396             :         {
     397           1 :             EXPECT_STREQ(pszRet,
     398             :                          "urn:ogc:def:crs,crs:EPSG::32631,crs:EPSG::5773");
     399             :         }
     400           1 :         CPLFree(pszRet);
     401             :     }
     402           1 : }
     403             : 
     404             : // Test constructors and assignment operators
     405           4 : TEST_F(test_osr, constructors_assignment_operators)
     406             : {
     407           1 :     OGRSpatialReference oSRS;
     408           1 :     oSRS.importFromEPSG(32631);
     409             : 
     410           1 :     OGRSpatialReference oSRS2(oSRS);
     411           1 :     ASSERT_TRUE(oSRS2.GetAuthorityCode(nullptr) != nullptr);
     412             : 
     413           1 :     OGRSpatialReference oSRS3;
     414           1 :     OGRSpatialReference &oSRSRef(oSRS);
     415           1 :     oSRS = oSRSRef;
     416           1 :     ASSERT_TRUE(oSRS.GetAuthorityCode(nullptr) != nullptr);
     417           1 :     oSRS3 = oSRS;
     418           1 :     ASSERT_TRUE(oSRS3.GetAuthorityCode(nullptr) != nullptr);
     419             : 
     420           1 :     OGRSpatialReference oSRS4(std::move(oSRS));
     421           1 :     ASSERT_TRUE(oSRS4.GetAuthorityCode(nullptr) != nullptr);
     422             : 
     423           1 :     OGRSpatialReference oSRS5;
     424           1 :     OGRSpatialReference &oSRS4Ref(oSRS4);
     425           1 :     oSRS4 = std::move(oSRS4Ref);
     426           1 :     ASSERT_TRUE(oSRS4.GetAuthorityCode(nullptr) != nullptr);
     427           1 :     oSRS5 = std::move(oSRS4);
     428           1 :     ASSERT_TRUE(oSRS5.GetAuthorityCode(nullptr) != nullptr);
     429             : }
     430             : 
     431          18 : static int GetEPSGCode(OGRSpatialReference &oSRS)
     432             : {
     433          18 :     int nEPSG = 0;
     434          18 :     auto pszEPSG = oSRS.GetAuthorityCode("PROJCS");
     435          18 :     if (pszEPSG == nullptr)
     436             :     {
     437           2 :         pszEPSG = oSRS.GetAuthorityCode("GEOGCS");
     438             :     }
     439          18 :     if (pszEPSG == nullptr)
     440             :     {
     441           2 :         pszEPSG = oSRS.GetAuthorityCode("VERT_CS");
     442             :     }
     443          18 :     if (pszEPSG != nullptr)
     444             :     {
     445          17 :         nEPSG = atoi(pszEPSG);
     446             :     }
     447          18 :     return nEPSG;
     448             : }
     449             : 
     450             : // Test exportVertCSToPanorama
     451           4 : TEST_F(test_osr, exportVertCSToPanorama)
     452             : {
     453           2 :     OGRSpatialReference oSRS;
     454           1 :     oSRS.importFromEPSG(28407);
     455             : 
     456           2 :     OGRSpatialReference oVertSRS;
     457           1 :     oVertSRS.importFromEPSG(5705);
     458           1 :     EXPECT_TRUE(oVertSRS.IsVertical() == TRUE);
     459           1 :     EXPECT_STRNE(oVertSRS.GetAttrValue("VERT_CS"), "");
     460           1 :     EXPECT_STRNE(oVertSRS.GetAttrValue("VERT_DATUM"), "");
     461           1 :     EXPECT_EQ(GetEPSGCode(oVertSRS), 5705);
     462             : 
     463           1 :     oSRS.SetVertCS(oVertSRS.GetAttrValue("VERT_CS"),
     464             :                    oVertSRS.GetAttrValue("VERT_DATUM"));
     465             : 
     466           1 :     int nVertID = 0;
     467           1 :     oSRS.exportVertCSToPanorama(&nVertID);
     468           1 :     EXPECT_EQ(nVertID, 25);
     469           1 : }
     470             : 
     471             : // Test importFromPanorama
     472           4 : TEST_F(test_osr, importFromPanorama)
     473             : {
     474           2 :     OGRSpatialReference oSRS;
     475           1 :     oSRS.importFromPanorama(35, 0, 45, nullptr);
     476           1 :     EXPECT_EQ(GetEPSGCode(oSRS), 3857);
     477             : 
     478           1 :     oSRS.importFromPanorama(35, 0, 9, nullptr);
     479           1 :     EXPECT_EQ(GetEPSGCode(oSRS), 3395);
     480             : 
     481           1 :     constexpr double TO_RADIANS = 0.017453292519943295769;
     482             :     {
     483             :         // WGS 84 / UTM zone 1
     484           1 :         double adfPrjParams[8] = {0.0,    0.0,      0.0, -177 * TO_RADIANS,
     485             :                                   0.9996, 500000.0, 0.0, 0.0};
     486           1 :         oSRS.importFromPanorama(17, 2, 9, adfPrjParams);
     487           1 :         EXPECT_EQ(GetEPSGCode(oSRS), 32601);
     488             : 
     489           1 :         oSRS.importFromPanorama(17, 2, 9, adfPrjParams, FALSE);
     490           1 :         EXPECT_EQ(GetEPSGCode(oSRS), 32701);
     491             :     }
     492             :     {
     493             :         // WGS 84 / UTM zone 37
     494           1 :         double adfPrjParams[8] = {0.0,    0.0,      0.0, 39 * TO_RADIANS,
     495             :                                   0.9996, 500000.0, 0.0, 0.0};
     496           1 :         oSRS.importFromPanorama(17, 2, 9, adfPrjParams);
     497           1 :         EXPECT_EQ(GetEPSGCode(oSRS), 32637);
     498             : 
     499           1 :         oSRS.importFromPanorama(17, 2, 9, adfPrjParams, FALSE);
     500           1 :         EXPECT_EQ(GetEPSGCode(oSRS), 32737);
     501             :     }
     502             :     {
     503             :         // Pulkovo 1942 / Gauss-Kruger zone 4
     504           1 :         double adfPrjParams[8] = {0.0, 0.0,       0.0, 21 * TO_RADIANS,
     505             :                                   1.0, 4500000.0, 0.0, 0.0};
     506           1 :         oSRS.importFromPanorama(1, 1, 1, adfPrjParams);
     507           1 :         EXPECT_EQ(GetEPSGCode(oSRS), 28404);
     508           1 :         oSRS.importFromPanorama(1, 0, 0, adfPrjParams);
     509           1 :         EXPECT_EQ(GetEPSGCode(oSRS), 28404);
     510           1 :         adfPrjParams[7] = 4;
     511           1 :         oSRS.importFromPanorama(1, 1, 1, adfPrjParams);
     512           1 :         EXPECT_EQ(GetEPSGCode(oSRS), 28404);
     513             :     }
     514             :     {
     515             :         // Pulkovo 1942 / Gauss-Kruger zone 31
     516           1 :         double adfPrjParams[8] = {0.0, 0.0,        0.0, -177 * TO_RADIANS,
     517             :                                   1.0, 31500000.0, 0.0, 0.0};
     518           1 :         oSRS.importFromPanorama(1, 1, 1, adfPrjParams);
     519           1 :         EXPECT_EQ(GetEPSGCode(oSRS), 28431);
     520           1 :         oSRS.importFromPanorama(1, 0, 0, adfPrjParams);
     521           1 :         EXPECT_EQ(GetEPSGCode(oSRS), 28431);
     522           1 :         adfPrjParams[7] = 31;
     523           1 :         oSRS.importFromPanorama(1, 1, 1, adfPrjParams);
     524           1 :         EXPECT_EQ(GetEPSGCode(oSRS), 28431);
     525             :     }
     526             :     {
     527             :         // Invalid data
     528           1 :         double adfPrjParams[8] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
     529           1 :         oSRS.importFromPanorama(0, 0, 0, adfPrjParams);
     530           1 :         EXPECT_EQ(oSRS.IsLocal(), true);
     531           1 :         EXPECT_EQ(GetEPSGCode(oSRS), 0);
     532             :     }
     533           1 : }
     534             : 
     535             : // Test exportToPanorama
     536           4 : TEST_F(test_osr, exportToPanorama)
     537             : {
     538           1 :     constexpr double RAD = 0.017453292519943295769;
     539           1 :     constexpr double EPS = 1e-12;
     540             : 
     541             :     {
     542           2 :         OGRSpatialReference oSRS;
     543           1 :         oSRS.importFromEPSG(32601);  // WGS 84 / UTM zone 1N
     544           1 :         EXPECT_EQ(GetEPSGCode(oSRS), 32601);
     545             : 
     546           1 :         long iProjSys = 0;
     547           1 :         long iDatum = 0;
     548           1 :         long iEllips = 0;
     549           1 :         long iZone = 0;
     550           1 :         double adfParams[7] = {0};
     551           1 :         oSRS.exportToPanorama(&iProjSys, &iDatum, &iEllips, &iZone, adfParams);
     552           1 :         EXPECT_EQ(iProjSys, 17);
     553           1 :         EXPECT_EQ(iDatum, 6);
     554           1 :         EXPECT_EQ(iEllips, 9);
     555           1 :         EXPECT_EQ(iZone, 1);
     556           1 :         EXPECT_NEAR(adfParams[2], 0.0, EPS);         //latitude_of_origin
     557           1 :         EXPECT_NEAR(adfParams[3], -177 * RAD, EPS);  //central_meridian
     558           1 :         EXPECT_NEAR(adfParams[4], 0.9996, EPS);      //scale_factor
     559           1 :         EXPECT_NEAR(adfParams[5], 500000, EPS);      //false_easting
     560           1 :         EXPECT_NEAR(adfParams[6], 0, EPS);           //false_northing
     561             :     }
     562             : 
     563             :     {
     564           2 :         OGRSpatialReference oSRS;
     565           1 :         oSRS.importFromEPSG(32660);  // WGS 84 / UTM zone 1N
     566           1 :         EXPECT_EQ(GetEPSGCode(oSRS), 32660);
     567             : 
     568           1 :         long iProjSys = 0;
     569           1 :         long iDatum = 0;
     570           1 :         long iEllips = 0;
     571           1 :         long iZone = 0;
     572           1 :         double adfParams[7] = {0};
     573           1 :         oSRS.exportToPanorama(&iProjSys, &iDatum, &iEllips, &iZone, adfParams);
     574           1 :         EXPECT_EQ(iProjSys, 17);
     575           1 :         EXPECT_EQ(iDatum, 6);
     576           1 :         EXPECT_EQ(iEllips, 9);
     577           1 :         EXPECT_EQ(iZone, 60);
     578           1 :         EXPECT_NEAR(adfParams[2], 0.0, EPS);        //latitude_of_origin
     579           1 :         EXPECT_NEAR(adfParams[3], 177 * RAD, EPS);  //central_meridian
     580           1 :         EXPECT_NEAR(adfParams[4], 0.9996, EPS);     //scale_factor
     581           1 :         EXPECT_NEAR(adfParams[5], 500000, EPS);     //false_easting
     582           1 :         EXPECT_NEAR(adfParams[6], 0, EPS);          //false_northing
     583             :     }
     584             : 
     585             :     {
     586           2 :         OGRSpatialReference oSRS;
     587           1 :         oSRS.importFromEPSG(28404);  // Pulkovo 1942 / Gauss-Kruger zone 4
     588           1 :         EXPECT_EQ(GetEPSGCode(oSRS), 28404);
     589             : 
     590           1 :         long iProjSys = 0;
     591           1 :         long iDatum = 0;
     592           1 :         long iEllips = 0;
     593           1 :         long iZone = 0;
     594           1 :         double adfParams[7] = {0};
     595           1 :         oSRS.exportToPanorama(&iProjSys, &iDatum, &iEllips, &iZone, adfParams);
     596           1 :         EXPECT_EQ(iProjSys, 1);
     597           1 :         EXPECT_EQ(iDatum, 1);
     598           1 :         EXPECT_EQ(iEllips, 1);
     599           1 :         EXPECT_EQ(iZone, 4);
     600           1 :         EXPECT_NEAR(adfParams[2], 0.0, EPS);       //latitude_of_origin
     601           1 :         EXPECT_NEAR(adfParams[3], 21 * RAD, EPS);  //central_meridian
     602           1 :         EXPECT_NEAR(adfParams[4], 1.0, EPS);       //scale_factor
     603           1 :         EXPECT_NEAR(adfParams[5], 4500000, EPS);   //false_easting
     604           1 :         EXPECT_NEAR(adfParams[6], 0, EPS);         //false_northing
     605             :     }
     606             :     {
     607           2 :         OGRSpatialReference oSRS;
     608           1 :         oSRS.importFromEPSG(28431);  // Pulkovo 1942 / Gauss-Kruger zone 31
     609           1 :         EXPECT_EQ(GetEPSGCode(oSRS), 28431);
     610             : 
     611           1 :         long iProjSys = 0;
     612           1 :         long iDatum = 0;
     613           1 :         long iEllips = 0;
     614           1 :         long iZone = 0;
     615           1 :         double adfParams[7] = {0};
     616           1 :         oSRS.exportToPanorama(&iProjSys, &iDatum, &iEllips, &iZone, adfParams);
     617           1 :         EXPECT_EQ(iProjSys, 1);
     618           1 :         EXPECT_EQ(iDatum, 1);
     619           1 :         EXPECT_EQ(iEllips, 1);
     620           1 :         EXPECT_EQ(iZone, 31);
     621           1 :         EXPECT_NEAR(adfParams[2], 0.0, EPS);         //latitude_of_origin
     622           1 :         EXPECT_NEAR(adfParams[3], -177 * RAD, EPS);  //central_meridian
     623           1 :         EXPECT_NEAR(adfParams[4], 1.0, EPS);         //scale_factor
     624           1 :         EXPECT_NEAR(adfParams[5], 31500000, EPS);    //false_easting
     625           1 :         EXPECT_NEAR(adfParams[6], 0, EPS);           //false_northing
     626             :     }
     627           1 : }
     628             : }  // namespace

Generated by: LCOV version 1.14