PageRenderTime 27ms CodeModel.GetById 7ms app.highlight 18ms RepoModel.GetById 1ms app.codeStats 0ms

/MapView/Map/RMWMSSource.m

http://github.com/route-me/route-me
Objective C | 99 lines | 51 code | 17 blank | 31 comment | 1 complexity | a9eca615f17895a82d998b9d8ffe78ba MD5 | raw file
 1//
 2//  RMWMSSource.m
 3//
 4// Copyright (c) 2008-2009, Route-Me Contributors
 5// All rights reserved.
 6//
 7// Redistribution and use in source and binary forms, with or without
 8// modification, are permitted provided that the following conditions are met:
 9//
10// * Redistributions of source code must retain the above copyright notice, this
11//   list of conditions and the following disclaimer.
12// * Redistributions in binary form must reproduce the above copyright notice,
13//   this list of conditions and the following disclaimer in the documentation
14//   and/or other materials provided with the distribution.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26// POSSIBILITY OF SUCH DAMAGE.
27
28#import "RMWMSSource.h"
29
30
31@implementation RMWMSSource
32
33@synthesize minZoom;
34@synthesize maxZoom;
35@synthesize name;
36@synthesize uniqueTilecacheKey;
37@synthesize wms;
38
39-(id) init 
40{ 
41    if (![super init]) 
42        return nil; 
43    
44    // The code below is based on the followin URL, but fixed for this use
45    // http://groups.google.com/group/route-me-map/browse_thread/thread/b6aa3757d46055aa/c93e7b0c861973e5?lnk=gst&q=900913#c93e7b0c861973e5
46    
47    initialResolution = 2 * M_PI * 6378137 / kDefaultTileSize; 
48    originShift = 2 * M_PI * 6378137 / 2.0; 
49    
50    [self setMinZoom:1.0];
51    [self setMaxZoom:18.0];
52
53    // some default values
54    [self setName:@"wms"];
55    [self setUniqueTilecacheKey:@"wms"];
56    
57    return self; 
58} 
59
60-(NSString*) bboxForTile: (RMTile) tile
61{
62    float resolution = [self resolutionAtZoom: tile.zoom];
63    CGPoint min = [self pixelsToMetersAtZoom: (tile.x     * kDefaultTileSize) PixelY:((tile.y+1) * kDefaultTileSize) atResolution:resolution];
64    CGPoint max = [self pixelsToMetersAtZoom: ((tile.x+1) * kDefaultTileSize) PixelY:((tile.y)   * kDefaultTileSize) atResolution:resolution];
65    return [NSString stringWithFormat:@"%f,%f,%f,%f", 
66            min.x, min.y, max.x, max.y];
67}
68
69-(NSString*) tileURL: (RMTile) tile
70{
71    NSString *bbox = [self bboxForTile:tile];
72    return [wms createGetMapForBbox:bbox size:CGSizeMake(kDefaultTileSize, kDefaultTileSize)];
73}
74
75//Resolution (meters/pixel) for given zoom level (measured at Equator) 
76-(float) resolutionAtZoom : (int) zoom 
77{ 
78    return initialResolution /pow (2,zoom); 
79} 
80
81// Converts pixel coordinates in given resolution to EPSG: 900913 
82-(CGPoint) pixelsToMetersAtZoom: (int) px PixelY:(int)py atResolution:(float) resolution 
83{ 
84    CGPoint meters; 
85    meters.x = (px * resolution) - originShift; 
86    meters.y = originShift - (py * resolution); 
87    return meters; 
88} 
89
90- (void) dealloc
91{
92    [self setName:nil];
93    [self setUniqueTilecacheKey:nil];
94    [self setWms:nil];
95    
96    [super dealloc];
97}
98
99@end