PageRenderTime 65ms CodeModel.GetById 18ms app.highlight 43ms RepoModel.GetById 2ms app.codeStats 0ms

/MapView/Map/RMTileStreamSource.m

http://github.com/route-me/route-me
Objective C | 181 lines | 108 code | 39 blank | 34 comment | 6 complexity | f11652086fa3970dd5bd47bc59645129 MD5 | raw file
  1//
  2//  RMTileStreamSource.h
  3//
  4//  Created by Justin R. Miller on 5/17/11.
  5//  Copyright 2011, Development Seed, Inc.
  6//  All rights reserved.
  7//  
  8//  Redistribution and use in source and binary forms, with or without
  9//  modification, are permitted provided that the following conditions are met:
 10//  
 11//      * Redistributions of source code must retain the above copyright
 12//        notice, this list of conditions and the following disclaimer.
 13//  
 14//      * Redistributions in binary form must reproduce the above copyright
 15//        notice, this list of conditions and the following disclaimer in the
 16//        documentation and/or other materials provided with the distribution.
 17//  
 18//      * Neither the name of Development Seed, Inc., nor the names of its
 19//        contributors may be used to endorse or promote products derived from
 20//        this software without specific prior written permission.
 21//  
 22//  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 23//  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 24//  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 25//  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 26//  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 27//  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 28//  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 29//  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 30//  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 31//  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 32//
 33
 34#import "RMTileStreamSource.h"
 35
 36@interface RMTileStreamSource ()
 37
 38@property (nonatomic, retain) NSDictionary *infoDictionary;
 39
 40@end
 41
 42#pragma mark -
 43
 44@implementation RMTileStreamSource
 45
 46@synthesize infoDictionary;
 47
 48- (id)initWithInfo:(NSDictionary *)info
 49{
 50	if (self = [super init])
 51        infoDictionary = [[NSDictionary dictionaryWithDictionary:info] retain];
 52    
 53	return self;
 54}
 55
 56- (id)initWithReferenceURL:(NSURL *)referenceURL
 57{
 58    return [self initWithInfo:[NSDictionary dictionaryWithContentsOfURL:referenceURL]];
 59}
 60
 61- (void)dealloc
 62{
 63    [infoDictionary release];
 64    
 65    [super dealloc];
 66}
 67
 68#pragma mark 
 69
 70- (NSString *)tileURL:(RMTile)tile
 71{
 72    // flip y value per OSM-style
 73    //
 74    NSInteger zoom = tile.zoom;
 75    NSInteger x    = tile.x;
 76    NSInteger y    = pow(2, zoom) - tile.y - 1;
 77    
 78    NSString *tileURLString = [self.infoDictionary objectForKey:@"tileURL"];
 79    
 80    tileURLString = [tileURLString stringByReplacingOccurrencesOfString:@"{z}" withString:[[NSNumber numberWithInteger:zoom] stringValue]];
 81    tileURLString = [tileURLString stringByReplacingOccurrencesOfString:@"{x}" withString:[[NSNumber numberWithInteger:x]    stringValue]];
 82    tileURLString = [tileURLString stringByReplacingOccurrencesOfString:@"{y}" withString:[[NSNumber numberWithInteger:y]    stringValue]];
 83    
 84	return tileURLString;
 85}
 86
 87- (float)minZoom
 88{
 89    return [[self.infoDictionary objectForKey:@"minzoom"] floatValue];
 90}
 91
 92- (float)maxZoom
 93{
 94    return [[self.infoDictionary objectForKey:@"maxzoom"] floatValue];
 95}
 96
 97- (RMSphericalTrapezium)latitudeLongitudeBoundingBox
 98{
 99    NSArray *parts = [[self.infoDictionary objectForKey:@"bounds"] componentsSeparatedByString:@","];
100        
101    if ([parts count] == 4)
102    {
103        RMSphericalTrapezium bounds = {
104            .southwest = {
105                .longitude = [[parts objectAtIndex:0] doubleValue],
106                .latitude  = [[parts objectAtIndex:1] doubleValue],
107            },
108            .northeast = {
109                .longitude = [[parts objectAtIndex:2] doubleValue],
110                .latitude  = [[parts objectAtIndex:3] doubleValue],
111            },
112        };
113        
114        return bounds;
115    }
116    
117    return kTileStreamDefaultLatLonBoundingBox;
118}
119
120- (BOOL)coversFullWorld
121{
122    RMSphericalTrapezium ownBounds     = [self latitudeLongitudeBoundingBox];
123    RMSphericalTrapezium defaultBounds = kTileStreamDefaultLatLonBoundingBox;
124    
125    if (ownBounds.southwest.longitude <= defaultBounds.southwest.longitude + 10 && 
126        ownBounds.northeast.longitude >= defaultBounds.northeast.longitude - 10)
127        return YES;
128    
129    return NO;
130}
131
132- (NSString *)uniqueTilecacheKey
133{
134	return [NSString stringWithFormat:@"%@-%@", [self.infoDictionary objectForKey:@"id"], [self.infoDictionary objectForKey:@"version"]];
135}
136
137- (NSString *)shortName
138{
139	return [self.infoDictionary objectForKey:@"name"];
140}
141
142- (NSString *)longDescription
143{
144	return [self.infoDictionary objectForKey:@"description"];
145}
146
147- (NSString *)shortAttribution
148{
149	return [self.infoDictionary objectForKey:@"attribution"];
150}
151
152- (NSString *)longAttribution
153{
154	return [self shortAttribution];
155}
156
157- (NSString *)legend
158{
159    return [self.infoDictionary objectForKey:@"legend"];
160}
161
162- (RMTileStreamLayerType)layerType
163{
164    return ([[self.infoDictionary objectForKey:@"type"] isEqualToString:@"overlay"] ? RMTileStreamLayerTypeOverlay : RMTileStreamLayerTypeBaselayer);
165}
166
167@end
168
169#pragma mark -
170
171@implementation RMCachedTileSource (RMTileStreamSourceExtensions)
172
173- (NSString *)legend
174{
175    if ([tileSource isKindOfClass:[RMTileStreamSource class]])
176        return [(RMTileStreamSource *)tileSource legend];
177    
178    return nil;
179}
180
181@end