LCOV - code coverage report
Current view: top level - frmts/wms - minidriver_tms.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 37 44 84.1 %
Date: 2024-11-21 22:18:42 Functions: 5 5 100.0 %

          Line data    Source code
       1             : /******************************************************************************
       2             :  *
       3             :  * Project:  WMS Client Driver
       4             :  * Purpose:  Implementation of Dataset and RasterBand classes for WMS
       5             :  *           and other similar services.
       6             :  * Author:   Chris Schmidt
       7             :  *
       8             :  ******************************************************************************
       9             :  * Copyright (c) 2007, Chris Schmidt
      10             :  *
      11             :  * SPDX-License-Identifier: MIT
      12             :  ****************************************************************************/
      13             : 
      14             : #include "wmsdriver.h"
      15             : #include "minidriver_tms.h"
      16             : 
      17         333 : WMSMiniDriver_TMS::WMSMiniDriver_TMS()
      18             : {
      19         333 : }
      20             : 
      21         666 : WMSMiniDriver_TMS::~WMSMiniDriver_TMS()
      22             : {
      23         666 : }
      24             : 
      25         333 : CPLErr WMSMiniDriver_TMS::Initialize(CPLXMLNode *config,
      26             :                                      CPL_UNUSED char **papszOpenOptions)
      27             : {
      28         333 :     CPLErr ret = CE_None;
      29             : 
      30             :     {
      31         333 :         const char *base_url = CPLGetXMLValue(config, "ServerURL", "");
      32         333 :         if (base_url[0] != '\0')
      33             :         {
      34         333 :             m_base_url = base_url;
      35         333 :             if (m_base_url.find("${") == std::string::npos)
      36             :             {
      37           0 :                 if (m_base_url.back() != '/')
      38             :                 {
      39           0 :                     m_base_url += "/";
      40             :                 }
      41           0 :                 m_base_url += "${version}/${layer}/${z}/${x}/${y}.${format}";
      42             :             }
      43             :         }
      44             :         else
      45             :         {
      46           0 :             CPLError(CE_Failure, CPLE_AppDefined,
      47             :                      "GDALWMS, TMS mini-driver: ServerURL missing.");
      48           0 :             ret = CE_Failure;
      49             :         }
      50             :     }
      51             : 
      52             :     // These never change
      53         333 :     const char *dataset = CPLGetXMLValue(config, "Layer", "");
      54         333 :     URLSearchAndReplace(&m_base_url, "${layer}", "%s", dataset);
      55         333 :     const char *version = CPLGetXMLValue(config, "Version", "1.0.0");
      56         333 :     URLSearchAndReplace(&m_base_url, "${version}", "%s", version);
      57         333 :     const char *format = CPLGetXMLValue(config, "Format", "jpg");
      58         333 :     URLSearchAndReplace(&m_base_url, "${format}", "%s", format);
      59             : 
      60         333 :     m_nTileXMultiplier = atoi(CPLGetXMLValue(config, "TileXMultiplier", "1"));
      61             : 
      62         333 :     return ret;
      63             : }
      64             : 
      65             : CPLErr
      66          48 : WMSMiniDriver_TMS::TiledImageRequest(WMSHTTPRequest &request,
      67             :                                      const GDALWMSImageRequestInfo &iri,
      68             :                                      const GDALWMSTiledImageRequestInfo &tiri)
      69             : {
      70          48 :     CPLString &url = request.URL;
      71          48 :     const GDALWMSDataWindow *data_window = m_parent_dataset->WMSGetDataWindow();
      72             :     int tms_y;
      73             : 
      74          48 :     if (data_window->m_y_origin != GDALWMSDataWindow::TOP)
      75             :     {
      76           3 :         if (iri.m_y0 == iri.m_y1)
      77           0 :             return CE_Failure;
      78           3 :         const double dfTmp = floor(
      79           3 :             ((data_window->m_y1 - data_window->m_y0) / (iri.m_y1 - iri.m_y0)) +
      80             :             0.5);
      81           3 :         if (!(dfTmp >= 0 && dfTmp < INT_MAX))
      82           0 :             return CE_Failure;
      83           3 :         tms_y = static_cast<int>(dfTmp) - tiri.m_y - 1;
      84             :     }
      85             :     else
      86             :     {
      87          45 :         tms_y = tiri.m_y;
      88             :     }
      89             :     // http://tms25.arc.nasa.gov/tile/tile.aspx?T=geocover2000&L=0&X=86&Y=39
      90          48 :     url = m_base_url;
      91             : 
      92          48 :     URLSearchAndReplace(&url, "${x}", "%d", tiri.m_x * m_nTileXMultiplier);
      93          48 :     URLSearchAndReplace(&url, "${y}", "%d", tms_y);
      94          48 :     URLSearchAndReplace(&url, "${z}", "%d", tiri.m_level);
      95             : 
      96             :     /* Hack for some TMS like servers that require tile numbers split into 3
      97             :      * groups of */
      98             :     /* 3 digits, like
      99             :      * http://tile8.geo.admin.ch/geoadmin/ch.swisstopo.pixelkarte-farbe */
     100          48 :     URLSearchAndReplace(&url, "${xxx}", "%03d/%03d/%03d", tiri.m_x / 1000000,
     101          48 :                         (tiri.m_x / 1000) % 1000, tiri.m_x % 1000);
     102          48 :     URLSearchAndReplace(&url, "${yyy}", "%03d/%03d/%03d", tms_y / 1000000,
     103          48 :                         (tms_y / 1000) % 1000, tms_y % 1000);
     104             : 
     105          48 :     return CE_None;
     106             : }

Generated by: LCOV version 1.14