/MapView/Map/RMWMSSource.m

http://github.com/route-me/route-me · Objective C · 99 lines · 51 code · 17 blank · 31 comment · 1 complexity · a9eca615f17895a82d998b9d8ffe78ba MD5 · raw file

  1. //
  2. // RMWMSSource.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 "RMWMSSource.h"
  28. @implementation RMWMSSource
  29. @synthesize minZoom;
  30. @synthesize maxZoom;
  31. @synthesize name;
  32. @synthesize uniqueTilecacheKey;
  33. @synthesize wms;
  34. -(id) init
  35. {
  36. if (![super init])
  37. return nil;
  38. // The code below is based on the followin URL, but fixed for this use
  39. // http://groups.google.com/group/route-me-map/browse_thread/thread/b6aa3757d46055aa/c93e7b0c861973e5?lnk=gst&q=900913#c93e7b0c861973e5
  40. initialResolution = 2 * M_PI * 6378137 / kDefaultTileSize;
  41. originShift = 2 * M_PI * 6378137 / 2.0;
  42. [self setMinZoom:1.0];
  43. [self setMaxZoom:18.0];
  44. // some default values
  45. [self setName:@"wms"];
  46. [self setUniqueTilecacheKey:@"wms"];
  47. return self;
  48. }
  49. -(NSString*) bboxForTile: (RMTile) tile
  50. {
  51. float resolution = [self resolutionAtZoom: tile.zoom];
  52. CGPoint min = [self pixelsToMetersAtZoom: (tile.x * kDefaultTileSize) PixelY:((tile.y+1) * kDefaultTileSize) atResolution:resolution];
  53. CGPoint max = [self pixelsToMetersAtZoom: ((tile.x+1) * kDefaultTileSize) PixelY:((tile.y) * kDefaultTileSize) atResolution:resolution];
  54. return [NSString stringWithFormat:@"%f,%f,%f,%f",
  55. min.x, min.y, max.x, max.y];
  56. }
  57. -(NSString*) tileURL: (RMTile) tile
  58. {
  59. NSString *bbox = [self bboxForTile:tile];
  60. return [wms createGetMapForBbox:bbox size:CGSizeMake(kDefaultTileSize, kDefaultTileSize)];
  61. }
  62. //Resolution (meters/pixel) for given zoom level (measured at Equator)
  63. -(float) resolutionAtZoom : (int) zoom
  64. {
  65. return initialResolution /pow (2,zoom);
  66. }
  67. // Converts pixel coordinates in given resolution to EPSG: 900913
  68. -(CGPoint) pixelsToMetersAtZoom: (int) px PixelY:(int)py atResolution:(float) resolution
  69. {
  70. CGPoint meters;
  71. meters.x = (px * resolution) - originShift;
  72. meters.y = originShift - (py * resolution);
  73. return meters;
  74. }
  75. - (void) dealloc
  76. {
  77. [self setName:nil];
  78. [self setUniqueTilecacheKey:nil];
  79. [self setWms:nil];
  80. [super dealloc];
  81. }
  82. @end