LCOV - code coverage report
Current view: top level - apps - gdalalg_crs_info.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 149 201 74.1 %
Date: 2026-11-30 21:24:02 Functions: 8 8 100.0 %

          Line data    Source code
       1             : /******************************************************************************
       2             :  *
       3             :  * Project:  GDAL
       4             :  * Purpose:  gdal "crs info" subcommand
       5             :  * Author:   Alessandro Pasotti <elpaso at itopen dot it>
       6             :  *
       7             :  ******************************************************************************
       8             :  * Copyright (c) 2026, Alessandro Pasotti <elpaso at itopen dot it>
       9             :  *
      10             :  * SPDX-License-Identifier: MIT
      11             :  ****************************************************************************/
      12             : 
      13             : //! @cond Doxygen_Suppress
      14             : 
      15             : #include "gdalalg_crs_info.h"
      16             : #include "gdal_priv.h"
      17             : #include "ogr_spatialref.h"
      18             : #include "cpl_port.h"
      19             : 
      20             : #ifndef _
      21             : #define _(x) (x)
      22             : #endif
      23             : 
      24             : /************************************************************************/
      25             : /*                         GDALCRSInfoAlgorithm                         */
      26             : /************************************************************************/
      27             : 
      28          54 : GDALCRSInfoAlgorithm::GDALCRSInfoAlgorithm()
      29             :     : GDALAlgorithm(NAME, DESCRIPTION, HELP_URL),
      30             :       m_outputTypeStr(OUTPUT_TYPE_DEFAULT_STR),
      31          54 :       m_outputType(OutputType::OUTPUT_TYPE_DEFAULT)
      32             : {
      33             : 
      34          54 :     AddProgressArg(/* hidden */ true);
      35          54 :     AddOutputStringArg(&m_output);
      36          54 :     AddStdoutArg(&m_stdout);
      37         108 :     AddArg(GDAL_ARG_NAME_INPUT_CRS, 's', _("Input CRS"), &m_crs)
      38         108 :         .SetIsCRSArg()
      39          54 :         .SetRequired()
      40          54 :         .SetPositional();
      41             : 
      42          54 :     AddArg("validate", 0, _("Validate the CRS"), &m_validate);
      43             :     AddArg("identify", 'i', _("Search for EPSG number(s) corresponding to CRS"),
      44          54 :            &m_identifyCRS);
      45             :     AddArg("single-line", 0, _("Print WKT or PROJJSON on a single line"),
      46          54 :            &m_singleLineOutput);
      47         108 :     AddArg("output-type", 'o', _("Output type"), &m_outputTypeStr)
      48          54 :         .SetDefault(OUTPUT_TYPE_DEFAULT_STR)
      49             :         .SetChoices({OUTPUT_TYPE_DEFAULT_STR, OUTPUT_TYPE_ALL_STR,
      50             :                      OUTPUT_TYPE_WKT_ALL_STR, OUTPUT_TYPE_WKT_LATEST_STR,
      51             :                      OUTPUT_TYPE_PROJJSON_STR, OUTPUT_TYPE_PROJ4_STR,
      52             :                      OUTPUT_TYPE_WKT1_STR, OUTPUT_TYPE_WKT_ESRI_STR,
      53             :                      OUTPUT_TYPE_WKT2_STR, OUTPUT_TYPE_MAPINFO_STR,
      54         702 :                      OUTPUT_TYPE_XML_STR})
      55             :         .AddAction(
      56          38 :             [this]()
      57             :             {
      58             :                 // Default is "proj4" and "wkt2"
      59           5 :                 if (m_outputTypeStr == OUTPUT_TYPE_DEFAULT_STR)
      60             :                 {
      61           0 :                     m_outputType = OutputType::OUTPUT_TYPE_DEFAULT;
      62             :                 }
      63             :                 // Latest and 2019
      64           5 :                 else if (m_outputTypeStr == OUTPUT_TYPE_WKT_LATEST_STR)
      65             :                 {
      66           0 :                     m_outputType = OutputType::OUTPUT_TYPE_WKT2;
      67             :                 }
      68           5 :                 else if (m_outputTypeStr == OUTPUT_TYPE_ALL_STR)
      69             :                 {
      70           0 :                     m_outputType = OutputType::OUTPUT_TYPE_ALL;
      71             :                 }
      72           5 :                 else if (m_outputTypeStr == OUTPUT_TYPE_WKT_ALL_STR)
      73             :                 {
      74           1 :                     m_outputType = OutputType::OUTPUT_TYPE_WKT_ALL;
      75             :                 }
      76           4 :                 else if (m_outputTypeStr == OUTPUT_TYPE_PROJJSON_STR)
      77             :                 {
      78           0 :                     m_outputType = OutputType::OUTPUT_TYPE_PROJJSON;
      79             :                 }
      80           4 :                 else if (m_outputTypeStr == OUTPUT_TYPE_PROJ4_STR)
      81             :                 {
      82           3 :                     m_outputType = OutputType::OUTPUT_TYPE_PROJ4;
      83             :                 }
      84           1 :                 else if (m_outputTypeStr == OUTPUT_TYPE_WKT1_STR)
      85             :                 {
      86           0 :                     m_outputType = OutputType::OUTPUT_TYPE_WKT1;
      87             :                 }
      88           1 :                 else if (m_outputTypeStr == OUTPUT_TYPE_WKT_ESRI_STR)
      89             :                 {
      90           0 :                     m_outputType = OutputType::OUTPUT_TYPE_WKT_ESRI;
      91             :                 }
      92           1 :                 else if (m_outputTypeStr == OUTPUT_TYPE_WKT2_STR)
      93             :                 {
      94           0 :                     m_outputType = OutputType::OUTPUT_TYPE_WKT2;
      95             :                 }
      96           1 :                 else if (m_outputTypeStr == OUTPUT_TYPE_MAPINFO_STR)
      97             :                 {
      98           0 :                     m_outputType = OutputType::OUTPUT_TYPE_MAPINFO;
      99             :                 }
     100           1 :                 else if (m_outputTypeStr == OUTPUT_TYPE_XML_STR)
     101             :                 {
     102           1 :                     m_outputType = OutputType::OUTPUT_TYPE_XML;
     103             :                 }
     104          54 :             });
     105          54 : }
     106             : 
     107          55 : void GDALCRSInfoAlgorithm::Print(const char *str) const
     108             : {
     109          55 :     if (m_stdout)
     110           0 :         fwrite(str, 1, strlen(str), stdout);
     111             :     else
     112          55 :         m_output += str;
     113          55 : }
     114             : 
     115           7 : bool GDALCRSInfoAlgorithm::RunImpl(CPL_UNUSED GDALProgressFunc pfnProgress,
     116             :                                    CPL_UNUSED void *pProgressData)
     117             : {
     118             : 
     119             :     struct CRSMatch
     120             :     {
     121           9 :         CRSMatch(std::unique_ptr<OGRSpatialReference> oCRSIn, int nConfidenceIn)
     122           9 :             : oCRS(std::move(oCRSIn)), nConfidence(nConfidenceIn)
     123             :         {
     124           9 :         }
     125             : 
     126             :         std::unique_ptr<OGRSpatialReference> oCRS;
     127             :         int nConfidence = 0;
     128             :     };
     129             : 
     130          14 :     std::vector<CRSMatch> aoCRS;
     131             : 
     132             :     std::unique_ptr<OGRSpatialReference> poCRS =
     133          14 :         std::make_unique<OGRSpatialReference>();
     134             : 
     135           7 :     if (poCRS->SetFromUserInput(m_crs.c_str()) != OGRERR_NONE)
     136             :     {
     137           0 :         CPLError(CE_Failure, CPLE_AppDefined, "Cannot parse CRS: %s",
     138             :                  m_crs.c_str());
     139           0 :         return false;
     140             :     }
     141             : 
     142           7 :     int nEntries = 0;
     143           7 :     CPLErr eErr = CE_None;
     144             : 
     145           7 :     if (m_identifyCRS)
     146             :     {
     147           2 :         int *panConfidence = nullptr;
     148             : 
     149             :         OGRSpatialReferenceH *pahCRS =
     150           2 :             poCRS->FindMatches(nullptr, &nEntries, &panConfidence);
     151             : 
     152           6 :         for (int i = 0; i < nEntries; i++)
     153             :         {
     154           4 :             OGRSpatialReferenceH hCRS = pahCRS[i];
     155             :             std::unique_ptr<OGRSpatialReference> crs(
     156           4 :                 OGRSpatialReference::FromHandle(hCRS));
     157           4 :             aoCRS.emplace_back(std::move(crs), panConfidence[i]);
     158           4 :             pahCRS[i] = nullptr;  // Ownership transferred to unique_ptr
     159             :         }
     160           2 :         CPLFree(panConfidence);
     161           2 :         CPLFree(pahCRS);
     162             :     }
     163             :     else
     164             :     {
     165           5 :         aoCRS.emplace_back(std::move(poCRS), 100);
     166             :     }
     167             : 
     168          16 :     for (const auto &crsMatch : aoCRS)
     169             :     {
     170          18 :         std::string osIdentifier;
     171           9 :         if (nEntries)
     172             :         {
     173           4 :             if (crsMatch.nConfidence != 100)
     174             :             {
     175           3 :                 Print(_("Confidence in this match: "));
     176           3 :                 Print(std::to_string(crsMatch.nConfidence).c_str());
     177           3 :                 Print("\n");
     178             :             }
     179             : 
     180           4 :             osIdentifier = crsMatch.oCRS->GetAuthorityName();
     181           8 :             if (const std::string osCode = crsMatch.oCRS->GetAuthorityCode();
     182           4 :                 !osCode.empty())
     183             :             {
     184           4 :                 if (!osIdentifier.empty())
     185           4 :                     osIdentifier += ":";
     186           4 :                 osIdentifier += osCode;
     187             :             }
     188             :         }
     189             : 
     190           9 :         if (m_validate)
     191             :         {
     192           0 :             const OGRErr retErr = crsMatch.oCRS->Validate();
     193           0 :             if (retErr != OGRERR_NONE)
     194             :             {
     195           0 :                 Print(_("\nValidation Failed"));
     196           0 :                 if (retErr == OGRERR_CORRUPT_DATA)
     197           0 :                     Print(_(" - CRS is not well formed"));
     198           0 :                 else if (retErr == OGRERR_UNSUPPORTED_SRS)
     199           0 :                     Print(_(" - contains non-standard PROJECTION[] values"));
     200           0 :                 Print("\n");
     201           0 :                 eErr = CE_Failure;
     202             :             }
     203             :             else
     204             :             {
     205           0 :                 Print(_("\nValidation Succeeded\n"));
     206             :             }
     207             :         }
     208             : 
     209           9 :         if (!m_singleLineOutput && m_identifyCRS)
     210             :         {
     211           4 :             Print("\n");
     212           4 :             Print(osIdentifier.c_str());
     213           4 :             Print("\n");
     214             :         }
     215             : 
     216           9 :         if (const auto retErr = PrintCRSOutputTypes(
     217           9 :                 crsMatch.oCRS.get(), m_outputType, !m_singleLineOutput);
     218             :             retErr != CE_None)
     219             :         {
     220           1 :             eErr = retErr;
     221             :         }
     222             :     }
     223             : 
     224           7 :     return eErr == CE_None;
     225             : }
     226             : 
     227          13 : CPLErr GDALCRSInfoAlgorithm::PrintCRSInfo(const OGRSpatialReference *crs,
     228             :                                           OutputType outputType,
     229             :                                           bool prettyPrint,
     230             :                                           bool printSeparator) const
     231             : {
     232          13 :     char *pszOutput = nullptr;
     233             : 
     234          13 :     CPLErr eErr = CE_None;
     235          26 :     CPLErrorStateBackuper oBackuper(CPLQuietErrorHandler);
     236             : 
     237           1 :     const auto PrintError = [&]()
     238             :     {
     239           1 :         eErr = CE_Failure;
     240           1 :         Print("(error) ");
     241           1 :         Print(CPLGetLastErrorMsg());
     242          14 :     };
     243             : 
     244          13 :     switch (outputType)
     245             :     {
     246           7 :         case OutputType::OUTPUT_TYPE_PROJ4:
     247             :         {
     248             :             // Note: this is inline (space instead of line break)
     249           7 :             if (printSeparator)
     250           2 :                 Print("PROJ.4 : ");
     251           7 :             crs->exportToProj4(&pszOutput);
     252           7 :             if (pszOutput)
     253             :             {
     254           7 :                 Print(pszOutput);
     255             :             }
     256             :             else
     257             :             {
     258           0 :                 PrintError();
     259             :             }
     260           7 :             Print("\n");
     261           7 :             break;
     262             :         }
     263           0 :         case OutputType::OUTPUT_TYPE_PROJJSON:
     264             :         {
     265           0 :             if (printSeparator)
     266           0 :                 Print("PROJJSON :\n");
     267           0 :             const char *const apszOptions[] = {
     268           0 :                 prettyPrint ? "MULTILINE=YES" : "MULTILINE=NO", nullptr};
     269           0 :             crs->exportToPROJJSON(&pszOutput, apszOptions);
     270           0 :             if (pszOutput)
     271             :             {
     272           0 :                 Print(pszOutput);
     273             :             }
     274             :             else
     275             :             {
     276           0 :                 PrintError();
     277             :             }
     278           0 :             Print("\n");
     279           0 :             break;
     280             :         }
     281           1 :         case OutputType::OUTPUT_TYPE_WKT1:
     282             :         {
     283           1 :             if (printSeparator)
     284           0 :                 Print("WKT1 :\n");
     285           1 :             const char *const apszOptions[] = {
     286           1 :                 "FORMAT=WKT1_GDAL", prettyPrint ? "MULTILINE=YES" : nullptr,
     287           1 :                 nullptr};
     288           1 :             crs->exportToWkt(&pszOutput, apszOptions);
     289           1 :             if (pszOutput)
     290             :             {
     291           1 :                 Print(pszOutput);
     292             :             }
     293             :             else
     294             :             {
     295           0 :                 PrintError();
     296             :             }
     297           1 :             Print("\n");
     298           1 :             break;
     299             :         }
     300           1 :         case OutputType::OUTPUT_TYPE_WKT_ESRI:
     301             :         {
     302           1 :             if (printSeparator)
     303           0 :                 Print("ESRI WKT1 :\n");
     304           1 :             const char *const apszOptions[] = {
     305           1 :                 "FORMAT=WKT1_ESRI", prettyPrint ? "MULTILINE=YES" : nullptr,
     306           1 :                 nullptr};
     307           1 :             crs->exportToWkt(&pszOutput, apszOptions);
     308           1 :             if (pszOutput)
     309             :             {
     310           1 :                 Print(pszOutput);
     311             :             }
     312             :             else
     313             :             {
     314           0 :                 PrintError();
     315             :             }
     316           1 :             break;
     317             :         }
     318           3 :         case OutputType::OUTPUT_TYPE_WKT2:
     319             :         {
     320           3 :             if (printSeparator)
     321           2 :                 Print("OGC WKT2:2019 :\n");
     322           3 :             const char *const apszOptions[] = {
     323           3 :                 "FORMAT=WKT2_2019", prettyPrint ? "MULTILINE=YES" : nullptr,
     324           3 :                 nullptr};
     325           3 :             crs->exportToWkt(&pszOutput, apszOptions);
     326           3 :             if (pszOutput)
     327             :             {
     328           3 :                 Print(pszOutput);
     329             :             }
     330             :             else
     331             :             {
     332           0 :                 PrintError();
     333             :             }
     334           3 :             Print("\n");
     335           3 :             break;
     336             :         }
     337           0 :         case OutputType::OUTPUT_TYPE_MAPINFO:
     338             :         {
     339           0 :             if (printSeparator)
     340           0 :                 Print("MapInfo :\n");
     341           0 :             crs->exportToMICoordSys(&pszOutput);
     342           0 :             if (pszOutput)
     343             :             {
     344           0 :                 Print(pszOutput);
     345             :             }
     346             :             else
     347             :             {
     348           0 :                 PrintError();
     349             :             }
     350           0 :             Print("\n");
     351           0 :             break;
     352             :         }
     353           1 :         case OutputType::OUTPUT_TYPE_XML:
     354             :         {
     355           1 :             if (printSeparator)
     356           0 :                 Print("XML :\n");
     357           1 :             crs->exportToXML(&pszOutput);
     358           1 :             if (pszOutput)
     359             :             {
     360           0 :                 Print(pszOutput);
     361             :             }
     362             :             else
     363             :             {
     364           1 :                 PrintError();
     365             :             }
     366           1 :             Print("\n");
     367           1 :             break;
     368             :         }
     369           0 :         default:
     370             :         {
     371           0 :             CPLError(CE_Failure, CPLE_AppDefined, "Unsupported output type");
     372           0 :             return CE_Failure;
     373             :         }
     374             :     }
     375             : 
     376          13 :     CPLFree(pszOutput);
     377          13 :     return eErr;
     378             : }
     379             : 
     380           9 : CPLErr GDALCRSInfoAlgorithm::PrintCRSOutputTypes(const OGRSpatialReference *crs,
     381             :                                                  OutputType outputTypes,
     382             :                                                  bool prettyPrint) const
     383             : {
     384             : 
     385           9 :     CPLErr eErr = CE_None;
     386             :     // Count requested formats
     387           9 :     std::vector<OutputType> requestedFormats;
     388          72 :     for (uint32_t i = 0; static_cast<uint32_t>(1 << i) <=
     389             :                          static_cast<uint32_t>(OutputType::OUTPUT_TYPE_LAST);
     390             :          i++)
     391             :     {
     392          63 :         if (static_cast<uint32_t>(outputTypes) & (1 << i))
     393             :         {
     394          13 :             requestedFormats.push_back(static_cast<OutputType>(1 << i));
     395             :         }
     396             :     }
     397             : 
     398           9 :     const size_t numFormats = requestedFormats.size();
     399             : 
     400          22 :     for (const auto &requestedFormat : requestedFormats)
     401             :     {
     402          13 :         if (numFormats > 1 && prettyPrint)
     403           4 :             Print("\n");
     404          13 :         if (const auto retErr = PrintCRSInfo(crs, requestedFormat, prettyPrint,
     405          13 :                                              numFormats > 1 && prettyPrint);
     406             :             retErr != CE_None)
     407             :         {
     408           1 :             eErr = retErr;
     409             :         }
     410             :     }
     411          18 :     return eErr;
     412             : }
     413             : 
     414             : //! @endcond

Generated by: LCOV version 1.14