/MapView/Map/RMCachedTileSource.m

http://github.com/route-me/route-me · Objective C · 170 lines · 116 code · 25 blank · 29 comment · 4 complexity · 8d7be577edf18af579d31e4ee1154da6 MD5 · raw file

  1. //
  2. // RMCachedTileSource.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. #import "RMCachedTileSource.h"
  28. #import "RMTileCache.h"
  29. @implementation RMCachedTileSource
  30. - (id) initWithSource: (id<RMTileSource>) _source
  31. {
  32. if ([_source isKindOfClass:[RMCachedTileSource class]])
  33. {
  34. [self release];
  35. return [_source retain];
  36. }
  37. if (![super init])
  38. return nil;
  39. tileSource = [_source retain];
  40. cache = [[RMTileCache alloc] initWithTileSource:tileSource];
  41. return self;
  42. }
  43. - (void) dealloc
  44. {
  45. [tileSource release];
  46. [cache release];
  47. [super dealloc];
  48. }
  49. + (RMCachedTileSource*) cachedTileSourceWithSource: (id<RMTileSource>) source
  50. {
  51. // Doing this fixes a strange build warning...
  52. id theSource = source;
  53. return [[[RMCachedTileSource alloc] initWithSource:theSource] autorelease];
  54. }
  55. -(RMTileImage *) tileImage: (RMTile) tile
  56. {
  57. RMTileImage *cachedImage = [cache cachedImage:tile];
  58. if (cachedImage != nil)
  59. {
  60. return cachedImage;
  61. }
  62. else
  63. {
  64. RMTileImage *image = [tileSource tileImage:tile];
  65. [cache addTile:tile WithImage:image];
  66. return image;
  67. }
  68. }
  69. -(id<RMMercatorToTileProjection>) mercatorToTileProjection
  70. {
  71. return [tileSource mercatorToTileProjection];
  72. }
  73. -(RMProjection*) projection
  74. {
  75. return [tileSource projection];
  76. }
  77. - (id<RMTileSource>) underlyingTileSource
  78. {
  79. // I'm assuming that our tilesource isn't itself a cachedtilesource.
  80. // This class's initialiser should make sure of that.
  81. return tileSource;
  82. }
  83. -(float) minZoom
  84. {
  85. return [tileSource minZoom];
  86. }
  87. -(float) maxZoom
  88. {
  89. return [tileSource maxZoom];
  90. }
  91. -(RMSphericalTrapezium) latitudeLongitudeBoundingBox
  92. {
  93. return [tileSource latitudeLongitudeBoundingBox];
  94. }
  95. - (void) didReceiveMemoryWarning
  96. {
  97. LogMethod();
  98. [cache didReceiveMemoryWarning];
  99. [tileSource didReceiveMemoryWarning];
  100. }
  101. -(NSString*) uniqueTilecacheKey
  102. {
  103. return [tileSource uniqueTilecacheKey];
  104. }
  105. -(NSString *)shortName
  106. {
  107. return [tileSource shortName];
  108. }
  109. -(NSString *)longDescription
  110. {
  111. return [tileSource longDescription];
  112. }
  113. -(NSString *)shortAttribution
  114. {
  115. return [tileSource shortAttribution];
  116. }
  117. -(NSString *)longAttribution
  118. {
  119. return [tileSource longAttribution];
  120. }
  121. -(NSString *) tileURL: (RMTile) tile
  122. {
  123. return [tileSource tileURL:tile];
  124. }
  125. -(NSString *) tileFile: (RMTile) tile
  126. {
  127. return [tileSource tileFile:tile];
  128. }
  129. -(NSString *) tilePath
  130. {
  131. return [tileSource tilePath];
  132. }
  133. -(void) setMinZoom:(NSUInteger)aMinZoom
  134. {
  135. [tileSource setMinZoom:aMinZoom];
  136. }
  137. -(void) setMaxZoom:(NSUInteger)aMaxZoom
  138. {
  139. [tileSource setMaxZoom:aMaxZoom];
  140. }
  141. -(void) removeAllCachedImages
  142. {
  143. [cache removeAllCachedImages];
  144. }
  145. @end