/MapView/Map/RMTileLoader.m

http://github.com/route-me/route-me · Objective C · 225 lines · 113 code · 41 blank · 71 comment · 21 complexity · d187ea07c5858bf7fc5dd43fc0ad46e1 MD5 · raw file

  1. //
  2. // RMTimeImageSet.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 "RMGlobalConstants.h"
  28. #import "RMTileLoader.h"
  29. #import "RMTileImage.h"
  30. #import "RMTileSource.h"
  31. #import "RMPixel.h"
  32. #import "RMMercatorToScreenProjection.h"
  33. #import "RMFractalTileProjection.h"
  34. #import "RMTileImageSet.h"
  35. #import "RMTileCache.h"
  36. @implementation RMTileLoader
  37. @synthesize loadedBounds, loadedZoom;
  38. -(id) init
  39. {
  40. if (![self initWithContent: nil])
  41. return nil;
  42. return self;
  43. }
  44. -(id) initWithContent: (RMMapContents *)_contents
  45. {
  46. if (![super init])
  47. return nil;
  48. content = _contents;
  49. [self clearLoadedBounds];
  50. loadedTiles.origin.tile = RMTileDummy();
  51. suppressLoading = NO;
  52. return self;
  53. }
  54. -(void) dealloc
  55. {
  56. [[NSNotificationCenter defaultCenter] removeObserver:self];
  57. [super dealloc];
  58. }
  59. -(void) clearLoadedBounds
  60. {
  61. loadedBounds = CGRectZero;
  62. }
  63. -(BOOL) screenIsLoaded
  64. {
  65. // RMTileRect targetRect = [content tileBounds];
  66. BOOL contained = CGRectContainsRect(loadedBounds, [content screenBounds]);
  67. int targetZoom = (int)([[content mercatorToTileProjection] calculateNormalisedZoomFromScale:[content scaledMetersPerPixel]]);
  68. if((targetZoom > content.maxZoom) || (targetZoom < content.minZoom))
  69. RMLog(@"target zoom %d is outside of RMMapContents limits %f to %f",
  70. targetZoom, content.minZoom, content.maxZoom);
  71. if (contained == NO)
  72. {
  73. // RMLog(@"reassembling because its not contained");
  74. }
  75. if (targetZoom != loadedZoom)
  76. {
  77. // RMLog(@"reassembling because target zoom = %f, loaded zoom = %d", targetZoom, loadedZoom);
  78. }
  79. return contained && targetZoom == loadedZoom;
  80. }
  81. -(void) updateLoadedImages
  82. {
  83. if (suppressLoading)
  84. return;
  85. if ([content mercatorToTileProjection] == nil || [content
  86. mercatorToScreenProjection] == nil)
  87. return;
  88. // delay display of new images until expensive operations are
  89. //allowed
  90. [[NSNotificationCenter defaultCenter] removeObserver:self
  91. name:RMResumeExpensiveOperations object:nil];
  92. if ([RMMapContents performExpensiveOperations] == NO)
  93. {
  94. [[NSNotificationCenter defaultCenter] addObserver:self
  95. selector:@selector(updateLoadedImages)
  96. name:RMResumeExpensiveOperations object:nil];
  97. return;
  98. }
  99. if ([self screenIsLoaded])
  100. return;
  101. //RMLog(@"updateLoadedImages initial count = %d", [[content imagesOnScreen] count]);
  102. RMTileRect newTileRect = [content tileBounds];
  103. RMTileImageSet *images = [content imagesOnScreen];
  104. images.zoom = newTileRect.origin.tile.zoom;
  105. CGRect newLoadedBounds = [images addTiles:newTileRect ToDisplayIn:
  106. [content screenBounds]];
  107. //RMLog(@"updateLoadedImages added count = %d", [images count]);
  108. if (!RMTileIsDummy(loadedTiles.origin.tile))
  109. {
  110. [images removeTilesOutsideOf:newTileRect];
  111. }
  112. //RMLog(@"updateLoadedImages final count = %d", [images count]);
  113. loadedBounds = newLoadedBounds;
  114. loadedZoom = newTileRect.origin.tile.zoom;
  115. loadedTiles = newTileRect;
  116. [content tilesUpdatedRegion:newLoadedBounds];
  117. }
  118. /*
  119. -(void) updateLoadedImages
  120. {
  121. if (suppressLoading)
  122. return;
  123. if ([content mercatorToTileProjection] == nil || [content mercatorToScreenProjection] == nil)
  124. return;
  125. if ([self screenIsLoaded])
  126. return;
  127. RMLog(@"assemble count = %d", [[content imagesOnScreen] count]);
  128. RMTileRect newTileRect = [content tileBounds];
  129. RMTileImageSet *images = [content imagesOnScreen];
  130. CGRect newLoadedBounds = [images addTiles:newTileRect ToDisplayIn:[content screenBounds]];
  131. RMLog(@"-> mid count = %d", [images count]);
  132. if (!RMTileIsDummy(loadedTiles.origin.tile))
  133. [images removeTiles:loadedTiles];
  134. RMLog(@"-> count = %d", [images count]);
  135. loadedBounds = newLoadedBounds;
  136. loadedZoom = newTileRect.origin.tile.zoom;
  137. loadedTiles = newTileRect;
  138. [content tilesUpdatedRegion:newLoadedBounds];
  139. }*/
  140. - (void)moveBy: (CGSize) delta
  141. {
  142. // RMLog(@"loadedBounds %f %f %f %f -> ", loadedBounds.origin.x, loadedBounds.origin.y, loadedBounds.size.width, loadedBounds.size.height);
  143. loadedBounds = RMTranslateCGRectBy(loadedBounds, delta);
  144. // RMLog(@" -> %f %f %f %f", loadedBounds.origin.x, loadedBounds.origin.y, loadedBounds.size.width, loadedBounds.size.height);
  145. [self updateLoadedImages];
  146. }
  147. - (void)zoomByFactor: (float) zoomFactor near:(CGPoint) center
  148. {
  149. loadedBounds = RMScaleCGRectAboutPoint(loadedBounds, zoomFactor, center);
  150. [self updateLoadedImages];
  151. }
  152. - (BOOL) suppressLoading
  153. {
  154. return suppressLoading;
  155. }
  156. - (void) setSuppressLoading: (BOOL) suppress
  157. {
  158. suppressLoading = suppress;
  159. if (suppress == NO)
  160. [self updateLoadedImages];
  161. }
  162. - (void)reset
  163. {
  164. loadedTiles.origin.tile = RMTileDummy();
  165. }
  166. - (void)reload
  167. {
  168. [self clearLoadedBounds];
  169. [self updateLoadedImages];
  170. }
  171. //-(BOOL) containsRect: (CGRect)bounds
  172. //{
  173. // return CGRectContainsRect(loadedBounds, bounds);
  174. //}
  175. @end