1 : /******************************************************************************
2 : * $Id: minidriver_tms.cpp 22021 2011-03-23 21:51:19Z rouault $
3 : *
4 : * Project: WMS Client Driver
5 : * Purpose: Implementation of Dataset and RasterBand classes for WMS
6 : * and other similar services.
7 : * Author: Chris Schmidt
8 : *
9 : ******************************************************************************
10 : * Copyright (c) 2007, Chris Schmidt
11 : *
12 : * Permission is hereby granted, free of charge, to any person obtaining a
13 : * copy of this software and associated documentation files (the "Software"),
14 : * to deal in the Software without restriction, including without limitation
15 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 : * and/or sell copies of the Software, and to permit persons to whom the
17 : * Software is furnished to do so, subject to the following conditions:
18 : *
19 : * The above copyright notice and this permission notice shall be included
20 : * in all copies or substantial portions of the Software.
21 : *
22 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28 : * DEALINGS IN THE SOFTWARE.
29 : ****************************************************************************/
30 :
31 : #include "stdinc.h"
32 :
33 1061 : CPP_GDALWMSMiniDriverFactory(TMS)
34 :
35 2 : GDALWMSMiniDriver_TMS::GDALWMSMiniDriver_TMS() {
36 2 : }
37 :
38 2 : GDALWMSMiniDriver_TMS::~GDALWMSMiniDriver_TMS() {
39 2 : }
40 :
41 2 : CPLErr GDALWMSMiniDriver_TMS::Initialize(CPLXMLNode *config) {
42 2 : CPLErr ret = CE_None;
43 :
44 2 : if (ret == CE_None) {
45 2 : const char *base_url = CPLGetXMLValue(config, "ServerURL", "");
46 2 : if (base_url[0] != '\0') {
47 2 : m_base_url = base_url;
48 2 : if (m_base_url.find("${") == std::string::npos) {
49 0 : if (m_base_url[m_base_url.size()-1] != '/') {
50 0 : m_base_url += "/";
51 : }
52 0 : m_base_url += "${version}/${layer}/${z}/${x}/${y}.${format}";
53 : }
54 : } else {
55 0 : CPLError(CE_Failure, CPLE_AppDefined, "GDALWMS, TMS mini-driver: ServerURL missing.");
56 0 : ret = CE_Failure;
57 : }
58 : }
59 :
60 2 : m_dataset = CPLGetXMLValue(config, "Layer", "");
61 4 : m_version = CPLGetXMLValue(config, "Version", "1.0.0");
62 4 : m_format = CPLGetXMLValue(config, "Format", "jpg");
63 :
64 2 : return ret;
65 : }
66 :
67 2 : void GDALWMSMiniDriver_TMS::GetCapabilities(GDALWMSMiniDriverCapabilities *caps) {
68 2 : caps->m_capabilities_version = 1;
69 2 : caps->m_has_arb_overviews = 0;
70 2 : caps->m_has_image_request = 0;
71 2 : caps->m_has_tiled_image_requeset = 1;
72 2 : caps->m_max_overview_count = 32;
73 2 : }
74 :
75 0 : void GDALWMSMiniDriver_TMS::ImageRequest(CPLString *url, const GDALWMSImageRequestInfo &iri) {
76 0 : }
77 :
78 0 : void GDALWMSMiniDriver_TMS::TiledImageRequest(CPLString *url, const GDALWMSImageRequestInfo &iri, const GDALWMSTiledImageRequestInfo &tiri) {
79 0 : const GDALWMSDataWindow *data_window = m_parent_dataset->WMSGetDataWindow();
80 : int tms_y;
81 :
82 0 : if (data_window->m_y_origin != GDALWMSDataWindow::TOP) {
83 : tms_y = static_cast<int>(floor(((data_window->m_y1 - data_window->m_y0)
84 0 : / (iri.m_y1 - iri.m_y0)) + 0.5)) - tiri.m_y - 1;
85 : } else {
86 0 : tms_y = tiri.m_y;
87 : }
88 : // http://tms25.arc.nasa.gov/tile/tile.aspx?T=geocover2000&L=0&X=86&Y=39
89 0 : *url = m_base_url;
90 :
91 0 : URLSearchAndReplace(url, "${version}", "%s", m_version.c_str());
92 0 : URLSearchAndReplace(url, "${layer}", "%s", m_dataset.c_str());
93 0 : URLSearchAndReplace(url, "${format}", "%s", m_format.c_str());
94 0 : URLSearchAndReplace(url, "${x}", "%d", tiri.m_x);
95 0 : URLSearchAndReplace(url, "${y}", "%d", tms_y);
96 0 : 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 0 : URLSearchAndReplace(url, "${xxx}", "%03d/%03d/%03d", tiri.m_x / 1000000, (tiri.m_x / 1000) % 1000, tiri.m_x % 1000);
101 0 : URLSearchAndReplace(url, "${yyy}", "%03d/%03d/%03d", tms_y / 1000000, (tms_y / 1000) % 1000, tms_y % 1000);
102 :
103 0 : }
|