PageRenderTime 41ms CodeModel.GetById 9ms app.highlight 29ms RepoModel.GetById 1ms app.codeStats 0ms

/MapView/Map/RMSpatialCloudMapSource.m

http://github.com/route-me/route-me
Objective C | 135 lines | 86 code | 22 blank | 27 comment | 9 complexity | 81efc6c4605c959c19a9a6226d9ed28f MD5 | raw file
  1//
  2//  RMSpatialCloudMapSource.m
  3//
  4// Copyright (c) 2011, SpatialCloud
  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 "RMSpatialCloudMapSource.h"
 29#import <CommonCrypto/CommonDigest.h>
 30
 31
 32@implementation RMSpatialCloudMapSource
 33
 34@synthesize customServerURL;
 35@synthesize loginID;
 36@synthesize password;
 37
 38- (id)init {
 39	self = [super init];
 40	if (self) {
 41		[self setMaxZoom:18];
 42		[self setMinZoom:1];
 43	}
 44	return self;
 45}
 46
 47- (id)initWithLoginID:(NSString *)newLoginID password:(NSString *)newPassword {
 48	self = [self init];
 49	if (self) {
 50		loginID = [newLoginID retain];
 51		password = [newPassword retain];
 52	}
 53	return self;
 54}
 55
 56- (id)initWithCustomServerURL:(NSString *)newCustomServerURL {
 57	self = [self init];
 58	if (self) {
 59		customServerURL = [newCustomServerURL retain];
 60	}
 61	return self;
 62}
 63
 64- (void)dealloc {
 65	[customServerURL release];
 66	[loginID release];
 67	[password release];
 68	[super dealloc];
 69}
 70
 71- (NSString *)md5HexDigest:(NSString *)stringToHash {
 72	const char *cStringToHash = [stringToHash UTF8String];
 73	unsigned char hash[CC_MD5_DIGEST_LENGTH];
 74	CC_MD5(cStringToHash, strlen(cStringToHash), hash);
 75	
 76	NSMutableString *hashString = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
 77    for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
 78        [hashString appendFormat:@"%02X", hash[i]];
 79    }
 80	return hashString;
 81}
 82
 83- (NSString *)tileURL:(RMTile)tile {
 84	NSAssert4(((tile.zoom >= self.minZoom) && (tile.zoom <= self.maxZoom)),
 85			  @"%@ tried to retrieve tile with zoomLevel %d, outside source's defined range %f to %f", 
 86			  self, tile.zoom, self.minZoom, self.maxZoom);
 87	NSAssert(([self.loginID length] > 0) || ([self.customServerURL length] > 0),
 88			 @"Login ID or Custom Server URL for Spatial Cloud must be non-empty");
 89	NSAssert(([self.password length] > 0) || ([self.customServerURL length] > 0),
 90			 @"Password or Custom Server URL for Spatial Cloud must be non-empty");
 91	
 92	// Flip the y-coordinate from the default route-me scheme to the Spatial Cloud tile scheme
 93	uint32_t flippedYCoordinate = (1 << tile.zoom) - 1 - tile.y;
 94	
 95	NSString* fullURL = nil;
 96	if ([self.customServerURL length] > 0) {
 97		fullURL = [NSString stringWithFormat:@"%@%d/%d/%d.jpg",
 98				   customServerURL, tile.zoom, tile.x, flippedYCoordinate];
 99	} else {
100		NSString *serverURL = @"http://ss.spatialcloud.com/getsign.cfm";
101		NSString *fileKey = [NSString stringWithFormat:@"%d/%d/%d.jpg",
102							 tile.zoom, tile.x, flippedYCoordinate];
103		
104		NSString *authSign = [NSString stringWithFormat:@"%@%@%@",
105							  fileKey, self.loginID, self.password];
106		NSString *authSignHash = [self md5HexDigest:authSign];
107		
108		fullURL = [NSString stringWithFormat:@"%@/1.0.0/spatialcloud/%@?loginid=%@&authSign=%@&viewer=viewer",
109				   serverURL, fileKey, self.loginID, authSignHash];
110	}
111	
112	return fullURL;
113}
114
115- (NSString *)uniqueTilecacheKey {
116	return @"SpatialCloud";
117}
118
119- (NSString *)shortName {
120	return @"SpatialCloud.com";
121}
122
123- (NSString *)longDescription {
124	return @"SpatialCloud.com MapSources are available for purchase & resale for various US & world datasets; in addition, SpatialCloud allows you to host/serve/resell your own datasets. Learn more at SpatialCloud.com.";
125}
126
127- (NSString *)shortAttribution {
128	return @"Spatial Cloud Demo";
129}
130
131- (NSString *)longAttribution {
132	return @"SpatialCloud MapSource terms vary, but this SpatialCloud Demo is for Route-Me code demo purposes only.  The demo MapSource provided is SpatialCloud USA, which is typically licensed using the flexible Creative Commons Attribution 3.0 Unported terms when subscribed to. If implementing this Route-Me code in your application, please visit SpatialCloud.com and setup your own account, subscribe to the MapSource(s), & create your own MapStream(s). If the demo is abused SpatialCloud may be forced to disable this demo for all.";
133}
134
135@end