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  * 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 "wmsdriver.h"
31 #include "minidriver_tms.h"
32 
33 CPL_CVSID("$Id: minidriver_tms.cpp b22c0525633697f8dc25afb2b6b848c3ad372686 2020-09-24 14:51:49 +0200 Even Rouault $")
34 
WMSMiniDriver_TMS()35 WMSMiniDriver_TMS::WMSMiniDriver_TMS() {}
36 
~WMSMiniDriver_TMS()37 WMSMiniDriver_TMS::~WMSMiniDriver_TMS() {}
38 
Initialize(CPLXMLNode * config,CPL_UNUSED char ** papszOpenOptions)39 CPLErr WMSMiniDriver_TMS::Initialize(CPLXMLNode *config, CPL_UNUSED char **papszOpenOptions) {
40     CPLErr ret = CE_None;
41 
42     {
43         const char *base_url = CPLGetXMLValue(config, "ServerURL", "");
44         if (base_url[0] != '\0') {
45             m_base_url = base_url;
46             if (m_base_url.find("${") == std::string::npos) {
47                 if (m_base_url.back() != '/') {
48                     m_base_url += "/";
49                 }
50                 m_base_url += "${version}/${layer}/${z}/${x}/${y}.${format}";
51             }
52         } else {
53             CPLError(CE_Failure, CPLE_AppDefined, "GDALWMS, TMS mini-driver: ServerURL missing.");
54             ret = CE_Failure;
55         }
56     }
57 
58     // These never change
59     const char *dataset = CPLGetXMLValue(config, "Layer", "");
60     URLSearchAndReplace(&m_base_url, "${layer}", "%s", dataset);
61     const char *version = CPLGetXMLValue(config, "Version", "1.0.0");
62     URLSearchAndReplace(&m_base_url, "${version}", "%s", version);
63     const char *format = CPLGetXMLValue(config, "Format", "jpg");
64     URLSearchAndReplace(&m_base_url, "${format}", "%s", format);
65 
66     m_nTileXMultiplier = atoi(
67         CPLGetXMLValue(config, "TileXMultiplier", "1"));
68 
69     return ret;
70 }
71 
TiledImageRequest(WMSHTTPRequest & request,const GDALWMSImageRequestInfo & iri,const GDALWMSTiledImageRequestInfo & tiri)72 CPLErr WMSMiniDriver_TMS::TiledImageRequest(WMSHTTPRequest &request,
73                                             const GDALWMSImageRequestInfo &iri,
74                                             const GDALWMSTiledImageRequestInfo &tiri)
75 {
76     CPLString &url = request.URL;
77     const GDALWMSDataWindow *data_window = m_parent_dataset->WMSGetDataWindow();
78     int tms_y;
79 
80     if (data_window->m_y_origin != GDALWMSDataWindow::TOP) {
81         if( iri.m_y0 == iri.m_y1 )
82             return CE_Failure;
83         const double dfTmp = floor(((data_window->m_y1 - data_window->m_y0)
84                                       / (iri.m_y1 - iri.m_y0)) + 0.5);
85         if( !(dfTmp >= 0 && dfTmp < INT_MAX) )
86             return CE_Failure;
87         tms_y = static_cast<int>(dfTmp) - tiri.m_y - 1;
88     } else {
89         tms_y = tiri.m_y;
90     }
91     // http://tms25.arc.nasa.gov/tile/tile.aspx?T=geocover2000&L=0&X=86&Y=39
92     url = m_base_url;
93 
94     URLSearchAndReplace(&url, "${x}", "%d", tiri.m_x * m_nTileXMultiplier);
95     URLSearchAndReplace(&url, "${y}", "%d", tms_y);
96     URLSearchAndReplace(&url, "${z}", "%d", tiri.m_level);
97 
98     /* Hack for some TMS like servers that require tile numbers split into 3 groups of */
99     /* 3 digits, like http://tile8.geo.admin.ch/geoadmin/ch.swisstopo.pixelkarte-farbe */
100     URLSearchAndReplace(&url, "${xxx}", "%03d/%03d/%03d", tiri.m_x / 1000000, (tiri.m_x / 1000) % 1000, tiri.m_x % 1000);
101     URLSearchAndReplace(&url, "${yyy}", "%03d/%03d/%03d", tms_y / 1000000, (tms_y / 1000) % 1000, tms_y % 1000);
102 
103     return CE_None;
104 }
105