LCOV - code coverage report
Current view: top level - ogr - ogr_srs_dict.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 24 38 63.2 %
Date: 2024-05-04 12:52:34 Functions: 2 3 66.7 %

          Line data    Source code
       1             : /******************************************************************************
       2             :  *
       3             :  * Project:  OpenGIS Simple Features Reference Implementation
       4             :  * Purpose:  Implement importFromDict() method to read a WKT SRS from a
       5             :  *           coordinate system dictionary in a simple text format.
       6             :  * Author:   Frank Warmerdam, warmerdam@pobox.com
       7             :  *
       8             :  ******************************************************************************
       9             :  * Copyright (c) 2004, Frank Warmerdam
      10             :  *
      11             :  * Permission is hereby granted, free of charge, to any person obtaining a
      12             :  * copy of this software and associated documentation files (the "Software"),
      13             :  * to deal in the Software without restriction, including without limitation
      14             :  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
      15             :  * and/or sell copies of the Software, and to permit persons to whom the
      16             :  * Software is furnished to do so, subject to the following conditions:
      17             :  *
      18             :  * The above copyright notice and this permission notice shall be included
      19             :  * in all copies or substantial portions of the Software.
      20             :  *
      21             :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
      22             :  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      23             :  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
      24             :  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
      25             :  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
      26             :  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
      27             :  * DEALINGS IN THE SOFTWARE.
      28             :  ****************************************************************************/
      29             : 
      30             : #include "cpl_port.h"
      31             : #include "ogr_spatialref.h"
      32             : 
      33             : #include <cstring>
      34             : 
      35             : #include "cpl_conv.h"
      36             : #include "cpl_error.h"
      37             : #include "cpl_string.h"
      38             : #include "cpl_vsi.h"
      39             : #include "ogr_core.h"
      40             : #include "ogr_srs_api.h"
      41             : 
      42             : /************************************************************************/
      43             : /*                           importFromDict()                           */
      44             : /************************************************************************/
      45             : 
      46             : /**
      47             :  * Read SRS from WKT dictionary.
      48             :  *
      49             :  * This method will attempt to find the indicated coordinate system identity
      50             :  * in the indicated dictionary file.  If found, the WKT representation is
      51             :  * imported and used to initialize this OGRSpatialReference.
      52             :  *
      53             :  * More complete information on the format of the dictionary files can
      54             :  * be found in the epsg.wkt file in the GDAL data tree.  The dictionary
      55             :  * files are searched for in the "GDAL" domain using CPLFindFile().  Normally
      56             :  * this results in searching /usr/local/share/gdal or somewhere similar.
      57             :  *
      58             :  * This method is the same as the C function OSRImportFromDict().
      59             :  *
      60             :  * @param pszDictFile the name of the dictionary file to load.
      61             :  *
      62             :  * @param pszCode the code to lookup in the dictionary.
      63             :  *
      64             :  * @return OGRERR_NONE on success, or OGRERR_SRS_UNSUPPORTED if the code isn't
      65             :  * found, and OGRERR_SRS_FAILURE if something more dramatic goes wrong.
      66             :  */
      67             : 
      68           3 : OGRErr OGRSpatialReference::importFromDict(const char *pszDictFile,
      69             :                                            const char *pszCode)
      70             : 
      71             : {
      72           6 :     CPLString osWKT(lookupInDict(pszDictFile, pszCode));
      73           3 :     if (osWKT.empty())
      74           0 :         return OGRERR_UNSUPPORTED_SRS;
      75             : 
      76           3 :     OGRErr eErr = importFromWkt(osWKT);
      77           3 :     if (eErr == OGRERR_NONE && strstr(pszDictFile, "esri_") == nullptr)
      78             :     {
      79           0 :         morphFromESRI();
      80             :     }
      81             : 
      82           3 :     return eErr;
      83             : }
      84             : 
      85             : /************************************************************************/
      86             : /*                          lookupInDict()                              */
      87             : /************************************************************************/
      88             : 
      89         116 : CPLString OGRSpatialReference::lookupInDict(const char *pszDictFile,
      90             :                                             const char *pszCode)
      91             : 
      92             : {
      93             :     /* -------------------------------------------------------------------- */
      94             :     /*      Find and open file.                                             */
      95             :     /* -------------------------------------------------------------------- */
      96         232 :     CPLString osDictFile(pszDictFile);
      97         116 :     const char *pszFilename = CPLFindFile("gdal", pszDictFile);
      98         116 :     if (pszFilename == nullptr)
      99           0 :         return CPLString();
     100             : 
     101         116 :     VSILFILE *fp = VSIFOpenL(pszFilename, "rb");
     102         116 :     if (fp == nullptr)
     103           0 :         return CPLString();
     104             : 
     105             :     /* -------------------------------------------------------------------- */
     106             :     /*      Process lines.                                                  */
     107             :     /* -------------------------------------------------------------------- */
     108         232 :     CPLString osWKT;
     109         116 :     const char *pszLine = nullptr;
     110             : 
     111      136995 :     while ((pszLine = CPLReadLineL(fp)) != nullptr)
     112             : 
     113             :     {
     114      136940 :         if (pszLine[0] == '#')
     115           0 :             continue;
     116             : 
     117      136940 :         if (STARTS_WITH_CI(pszLine, "include "))
     118             :         {
     119           0 :             osWKT = lookupInDict(pszLine + 8, pszCode);
     120           0 :             if (!osWKT.empty())
     121           0 :                 break;
     122           0 :             continue;
     123             :         }
     124             : 
     125      136940 :         if (strstr(pszLine, ",") == nullptr)
     126           0 :             continue;
     127             : 
     128      136940 :         if (EQUALN(pszLine, pszCode, strlen(pszCode)) &&
     129          61 :             pszLine[strlen(pszCode)] == ',')
     130             :         {
     131          61 :             osWKT = pszLine + strlen(pszCode) + 1;
     132          61 :             break;
     133             :         }
     134             :     }
     135             : 
     136             :     /* -------------------------------------------------------------------- */
     137             :     /*      Cleanup                                                         */
     138             :     /* -------------------------------------------------------------------- */
     139         116 :     VSIFCloseL(fp);
     140             : 
     141         116 :     return osWKT;
     142             : }
     143             : 
     144             : /************************************************************************/
     145             : /*                         OSRImportFromDict()                          */
     146             : /************************************************************************/
     147             : 
     148             : /**
     149             :  * Read SRS from WKT dictionary.
     150             :  *
     151             :  * This method will attempt to find the indicated coordinate system identity
     152             :  * in the indicated dictionary file.  If found, the WKT representation is
     153             :  * imported and used to initialize this OGRSpatialReference.
     154             :  *
     155             :  * More complete information on the format of the dictionary files can
     156             :  * be found in the epsg.wkt file in the GDAL data tree.  The dictionary
     157             :  * files are searched for in the "GDAL" domain using CPLFindFile().  Normally
     158             :  * this results in searching /usr/local/share/gdal or somewhere similar.
     159             :  *
     160             :  * This method is the same as the C++ method
     161             :  * OGRSpatialReference::importFromDict().
     162             :  *
     163             :  * @param hSRS spatial reference system handle.
     164             :  *
     165             :  * @param pszDictFile the name of the dictionary file to load.
     166             :  *
     167             :  * @param pszCode the code to lookup in the dictionary.
     168             :  *
     169             :  * @return OGRERR_NONE on success, or OGRERR_SRS_UNSUPPORTED if the code isn't
     170             :  * found, and OGRERR_SRS_FAILURE if something more dramatic goes wrong.
     171             :  */
     172             : 
     173           0 : OGRErr OSRImportFromDict(OGRSpatialReferenceH hSRS, const char *pszDictFile,
     174             :                          const char *pszCode)
     175             : 
     176             : {
     177           0 :     VALIDATE_POINTER1(hSRS, "OSRImportFromDict", OGRERR_FAILURE);
     178             : 
     179           0 :     return reinterpret_cast<OGRSpatialReference *>(hSRS)->importFromDict(
     180           0 :         pszDictFile, pszCode);
     181             : }

Generated by: LCOV version 1.14