PageRenderTime 33ms CodeModel.GetById 21ms RepoModel.GetById 0ms 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. #import "RMTileStreamSource.h"
  34. @interface RMTileStreamSource ()
  35. @property (nonatomic, retain) NSDictionary *infoDictionary;
  36. @end
  37. #pragma mark -
  38. @implementation RMTileStreamSource
  39. @synthesize infoDictionary;
  40. - (id)initWithInfo:(NSDictionary *)info
  41. {
  42. if (self = [super init])
  43. infoDictionary = [[NSDictionary dictionaryWithDictionary:info] retain];
  44. return self;
  45. }
  46. - (id)initWithReferenceURL:(NSURL *)referenceURL
  47. {
  48. return [self initWithInfo:[NSDictionary dictionaryWithContentsOfURL:referenceURL]];
  49. }
  50. - (void)dealloc
  51. {
  52. [infoDictionary release];
  53. [super dealloc];
  54. }
  55. #pragma mark
  56. - (NSString *)tileURL:(RMTile)tile
  57. {
  58. // flip y value per OSM-style
  59. //
  60. NSInteger zoom = tile.zoom;
  61. NSInteger x = tile.x;
  62. NSInteger y = pow(2, zoom) - tile.y - 1;
  63. NSString *tileURLString = [self.infoDictionary objectForKey:@"tileURL"];
  64. tileURLString = [tileURLString stringByReplacingOccurrencesOfString:@"{z}" withString:[[NSNumber numberWithInteger:zoom] stringValue]];
  65. tileURLString = [tileURLString stringByReplacingOccurrencesOfString:@"{x}" withString:[[NSNumber numberWithInteger:x] stringValue]];
  66. tileURLString = [tileURLString stringByReplacingOccurrencesOfString:@"{y}" withString:[[NSNumber numberWithInteger:y] stringValue]];
  67. return tileURLString;
  68. }
  69. - (float)minZoom
  70. {
  71. return [[self.infoDictionary objectForKey:@"minzoom"] floatValue];
  72. }
  73. - (float)maxZoom
  74. {
  75. return [[self.infoDictionary objectForKey:@"maxzoom"] floatValue];
  76. }
  77. - (RMSphericalTrapezium)latitudeLongitudeBoundingBox
  78. {
  79. NSArray *parts = [[self.infoDictionary objectForKey:@"bounds"] componentsSeparatedByString:@","];
  80. if ([parts count] == 4)
  81. {
  82. RMSphericalTrapezium bounds = {
  83. .southwest = {
  84. .longitude = [[parts objectAtIndex:0] doubleValue],
  85. .latitude = [[parts objectAtIndex:1] doubleValue],
  86. },
  87. .northeast = {
  88. .longitude = [[parts objectAtIndex:2] doubleValue],
  89. .latitude = [[parts objectAtIndex:3] doubleValue],
  90. },
  91. };
  92. return bounds;
  93. }
  94. return kTileStreamDefaultLatLonBoundingBox;
  95. }
  96. - (BOOL)coversFullWorld
  97. {
  98. RMSphericalTrapezium ownBounds = [self latitudeLongitudeBoundingBox];
  99. RMSphericalTrapezium defaultBounds = kTileStreamDefaultLatLonBoundingBox;
  100. if (ownBounds.southwest.longitude <= defaultBounds.southwest.longitude + 10 &&
  101. ownBounds.northeast.longitude >= defaultBounds.northeast.longitude - 10)
  102. return YES;
  103. return NO;
  104. }
  105. - (NSString *)uniqueTilecacheKey
  106. {
  107. return [NSString stringWithFormat:@"%@-%@", [self.infoDictionary objectForKey:@"id"], [self.infoDictionary objectForKey:@"version"]];
  108. }
  109. - (NSString *)shortName
  110. {
  111. return [self.infoDictionary objectForKey:@"name"];
  112. }
  113. - (NSString *)longDescription
  114. {
  115. return [self.infoDictionary objectForKey:@"description"];
  116. }
  117. - (NSString *)shortAttribution
  118. {
  119. return [self.infoDictionary objectForKey:@"attribution"];
  120. }
  121. - (NSString *)longAttribution
  122. {
  123. return [self shortAttribution];
  124. }
  125. - (NSString *)legend
  126. {
  127. return [self.infoDictionary objectForKey:@"legend"];
  128. }
  129. - (RMTileStreamLayerType)layerType
  130. {
  131. return ([[self.infoDictionary objectForKey:@"type"] isEqualToString:@"overlay"] ? RMTileStreamLayerTypeOverlay : RMTileStreamLayerTypeBaselayer);
  132. }
  133. @end
  134. #pragma mark -
  135. @implementation RMCachedTileSource (RMTileStreamSourceExtensions)
  136. - (NSString *)legend
  137. {
  138. if ([tileSource isKindOfClass:[RMTileStreamSource class]])
  139. return [(RMTileStreamSource *)tileSource legend];
  140. return nil;
  141. }
  142. @end