~ubuntu-branches/ubuntu/quantal/marble/quantal

« back to all changes in this revision

Viewing changes to src/lib/ServerLayout.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Philip Muškovac
  • Date: 2011-07-11 15:43:02 UTC
  • Revision ID: james.westby@ubuntu.com-20110711154302-lq69ftcx125g1jx5
Tags: upstream-4.6.90+repack
Import upstream version 4.6.90+repack

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// This file is part of the Marble Virtual Globe.
 
3
//
 
4
// This program is free software licensed under the GNU LGPL. You can
 
5
// find a copy of this license in LICENSE.txt in the top directory of
 
6
// the source code.
 
7
//
 
8
// Copyright 2010,2011 Bernhard Beschow <bbeschow@cs.tu-berlin.de>
 
9
//
 
10
 
 
11
// Own
 
12
#include "ServerLayout.h"
 
13
 
 
14
#include "GeoSceneTexture.h"
 
15
#include "global.h"
 
16
#include "TileId.h"
 
17
 
 
18
#include <math.h>
 
19
 
 
20
namespace Marble
 
21
{
 
22
 
 
23
ServerLayout::ServerLayout( GeoSceneTexture *textureLayer )
 
24
    : m_textureLayer( textureLayer )
 
25
{
 
26
}
 
27
 
 
28
ServerLayout::~ServerLayout()
 
29
{
 
30
}
 
31
 
 
32
qint64 ServerLayout::numTilesX( const Marble::TileId& tileId ) const
 
33
{
 
34
    return ( 1 << tileId.zoomLevel() ) * m_textureLayer->levelZeroColumns();
 
35
}
 
36
 
 
37
qint64 ServerLayout::numTilesY( const Marble::TileId& tileId ) const
 
38
{
 
39
    return ( 1 << tileId.zoomLevel() ) * m_textureLayer->levelZeroRows();
 
40
}
 
41
 
 
42
MarbleServerLayout::MarbleServerLayout( GeoSceneTexture *textureLayer )
 
43
    : ServerLayout( textureLayer )
 
44
{
 
45
}
 
46
 
 
47
QUrl MarbleServerLayout::downloadUrl( const QUrl &prototypeUrl, const TileId &id ) const
 
48
{
 
49
    QUrl url = prototypeUrl;
 
50
    url.setPath( url.path() + m_textureLayer->relativeTileFileName( id ) );
 
51
 
 
52
    return url;
 
53
}
 
54
 
 
55
QString MarbleServerLayout::name() const
 
56
{
 
57
    return "Marble";
 
58
}
 
59
 
 
60
 
 
61
OsmServerLayout::OsmServerLayout( GeoSceneTexture *textureLayer )
 
62
    : ServerLayout( textureLayer )
 
63
{
 
64
}
 
65
 
 
66
QUrl OsmServerLayout::downloadUrl( const QUrl &prototypeUrl, const TileId &id ) const
 
67
{
 
68
    const QString suffix = m_textureLayer->fileFormat().toLower();
 
69
    const QString path = QString( "%1/%2/%3.%4" ).arg( id.zoomLevel() )
 
70
                                                 .arg( id.x() )
 
71
                                                 .arg( id.y() )
 
72
                                                 .arg( suffix );
 
73
 
 
74
    QUrl url = prototypeUrl;
 
75
    url.setPath( url.path() + path );
 
76
 
 
77
    return url;
 
78
}
 
79
 
 
80
QString OsmServerLayout::name() const
 
81
{
 
82
    return "OpenStreetMap";
 
83
}
 
84
 
 
85
 
 
86
CustomServerLayout::CustomServerLayout( GeoSceneTexture *texture )
 
87
    : ServerLayout( texture )
 
88
{
 
89
}
 
90
 
 
91
QUrl CustomServerLayout::downloadUrl( const QUrl &prototypeUrl, const TileId &id ) const
 
92
{
 
93
    QString urlStr = prototypeUrl.toString();
 
94
 
 
95
    urlStr.replace( "{zoomLevel}", QString::number( id.zoomLevel() ) );
 
96
    urlStr.replace( "{x}", QString::number( id.x() ) );
 
97
    urlStr.replace( "{y}", QString::number( id.y() ) );
 
98
 
 
99
    return QUrl( urlStr );
 
100
}
 
101
 
 
102
QString CustomServerLayout::name() const
 
103
{
 
104
    return "Custom";
 
105
}
 
106
 
 
107
 
 
108
WmsServerLayout::WmsServerLayout( GeoSceneTexture *texture )
 
109
    : ServerLayout( texture )
 
110
{
 
111
}
 
112
 
 
113
QUrl WmsServerLayout::downloadUrl( const QUrl &prototypeUrl, const Marble::TileId &tileId ) const
 
114
{
 
115
    const qreal radius = numTilesX( tileId ) / 2.0;
 
116
    const qint64 x = tileId.x();
 
117
 
 
118
    const qreal lonLeft   = ( x - radius ) / radius * 180.0;
 
119
    const qreal lonRight  = ( x - radius + 1 ) / radius * 180.0;
 
120
 
 
121
    QUrl url = prototypeUrl;
 
122
    url.addQueryItem( "service", "WMS" );
 
123
    url.addQueryItem( "request", "GetMap" );
 
124
    url.addQueryItem( "version", "1.1.1" );
 
125
    if ( !url.hasQueryItem( "styles" ) )
 
126
        url.addQueryItem( "styles", "" );
 
127
    if ( !url.hasQueryItem( "format" ) ) {
 
128
        if ( m_textureLayer->fileFormat().toLower() == "jpg" )
 
129
            url.addQueryItem( "format", "image/jpeg" );
 
130
        else
 
131
            url.addQueryItem( "format", "image/" + m_textureLayer->fileFormat().toLower() );
 
132
    }
 
133
    if ( !url.hasQueryItem( "srs" ) ) {
 
134
        url.addQueryItem( "srs", epsgCode() );
 
135
    }
 
136
    if ( !url.hasQueryItem( "layers" ) )
 
137
        url.addQueryItem( "layers", m_textureLayer->name() );
 
138
    url.addQueryItem( "width", QString::number( m_textureLayer->tileSize().width() ) );
 
139
    url.addQueryItem( "height", QString::number( m_textureLayer->tileSize().height() ) );
 
140
    url.addQueryItem( "bbox", QString( "%1,%2,%3,%4" ).arg( QString::number( lonLeft, 'f', 12 ) )
 
141
                                                      .arg( QString::number( latBottom( tileId ), 'f', 12 ) )
 
142
                                                      .arg( QString::number( lonRight, 'f', 12 ) )
 
143
                                                      .arg( QString::number( latTop( tileId ), 'f', 12 ) ) );
 
144
 
 
145
    return url;
 
146
}
 
147
 
 
148
QString WmsServerLayout::name() const
 
149
{
 
150
    return "WebMapService";
 
151
}
 
152
 
 
153
qreal WmsServerLayout::latBottom( const Marble::TileId &tileId ) const
 
154
{
 
155
    const qreal radius = numTilesY( tileId ) / 2.0;
 
156
 
 
157
    switch( m_textureLayer->projection() )
 
158
    {
 
159
    case GeoSceneTexture::Equirectangular:
 
160
        return ( radius - tileId.y() - 1 ) / radius *  90.0;
 
161
    case GeoSceneTexture::Mercator:
 
162
        return atan( sinh( ( radius - tileId.y() - 1 ) / radius * M_PI ) ) * 180.0 / M_PI;
 
163
    }
 
164
 
 
165
    Q_ASSERT( false ); // not reached
 
166
    return 0.0;
 
167
}
 
168
 
 
169
qreal WmsServerLayout::latTop( const Marble::TileId &tileId ) const
 
170
{
 
171
    const qreal radius = numTilesY( tileId ) / 2.0;
 
172
 
 
173
    switch( m_textureLayer->projection() )
 
174
    {
 
175
    case GeoSceneTexture::Equirectangular:
 
176
        return ( radius - tileId.y() ) / radius *  90.0;
 
177
    case GeoSceneTexture::Mercator:
 
178
        return atan( sinh( ( radius - tileId.y() ) / radius * M_PI ) ) * 180.0 / M_PI;
 
179
    }
 
180
 
 
181
    Q_ASSERT( false ); // not reached
 
182
    return 0.0;
 
183
}
 
184
 
 
185
QString WmsServerLayout::epsgCode() const
 
186
{
 
187
    switch ( m_textureLayer->projection() ) {
 
188
        case GeoSceneTexture::Equirectangular:
 
189
            return "EPSG:4326";
 
190
        case GeoSceneTexture::Mercator:
 
191
            return "EPSG:3785";
 
192
    }
 
193
 
 
194
    Q_ASSERT( false ); // not reached
 
195
    return QString();
 
196
}
 
197
 
 
198
QuadTreeServerLayout::QuadTreeServerLayout( GeoSceneTexture *textureLayer )
 
199
    : ServerLayout( textureLayer )
 
200
{
 
201
}
 
202
 
 
203
QUrl QuadTreeServerLayout::downloadUrl( const QUrl &prototypeUrl, const Marble::TileId &id ) const
 
204
{
 
205
    QString urlStr = prototypeUrl.toString();
 
206
 
 
207
    urlStr.replace( "{quadIndex}", encodeQuadTree( id ) );
 
208
 
 
209
    return QUrl( urlStr );
 
210
}
 
211
 
 
212
QString QuadTreeServerLayout::name() const
 
213
{
 
214
    return "QuadTree";
 
215
}
 
216
 
 
217
QString QuadTreeServerLayout::encodeQuadTree( const Marble::TileId &id )
 
218
{
 
219
    QString tileNum;
 
220
 
 
221
    for ( int i = id.zoomLevel(); i >= 0; i-- ) {
 
222
        const int tileX = (id.x() >> i) % 2;
 
223
        const int tileY = (id.y() >> i) % 2;
 
224
        const int num = ( 2 * tileY ) + tileX;
 
225
 
 
226
        tileNum += QString::number( num );
 
227
    }
 
228
 
 
229
    return tileNum;
 
230
}
 
231
 
 
232
}