/MapView/Map/RMWMS.m

http://github.com/route-me/route-me · Objective C · 203 lines · 148 code · 26 blank · 29 comment · 14 complexity · 757936168b08b37e550a6bd456f0c621 MD5 · raw file

  1. //
  2. // RMWMS.m
  3. //
  4. // Copyright (c) 2008-2011, 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 "RMWMS.h"
  28. @implementation RMWMS
  29. @synthesize layers;
  30. @synthesize styles;
  31. @synthesize queryLayers;
  32. @synthesize queryable;
  33. @synthesize crs;
  34. @synthesize infoFormat;
  35. @synthesize format;
  36. @synthesize service;
  37. @synthesize version;
  38. @synthesize exceptions;
  39. - (id) init
  40. {
  41. self = [super init];
  42. if (self != nil) {
  43. // default values
  44. [self setInfoFormat:@"text/html"];
  45. [self setCrs:@"EPSG:900913"];
  46. [self setLayers:@""];
  47. [self setQueryLayers:@""];
  48. [self setStyles:@""];
  49. [self setFormat:@"image/png"];
  50. [self setService:@"WMS"];
  51. [self setVersion:@"1.1.1"];
  52. [self setExceptions:@"application/vnd.ogc.se_inimage"];
  53. }
  54. return self;
  55. }
  56. -(void)setUrlPrefix:(NSString *)newUrlPrefix
  57. {
  58. if (newUrlPrefix) {
  59. if (!([newUrlPrefix hasSuffix:@"?"]||[newUrlPrefix hasSuffix:@"&"])) {
  60. if ([newUrlPrefix rangeOfString:@"?"].location == NSNotFound) {
  61. newUrlPrefix = [newUrlPrefix stringByAppendingString:@"?"];
  62. } else {
  63. newUrlPrefix = [newUrlPrefix stringByAppendingString:@"&"];
  64. }
  65. }
  66. }
  67. [urlPrefix release];
  68. urlPrefix = [newUrlPrefix retain];
  69. }
  70. -(NSString *)urlPrefix
  71. {
  72. return urlPrefix;
  73. }
  74. -(NSString *)createBaseGet:(NSString *)bbox size:(CGSize)size
  75. {
  76. return [NSString
  77. stringWithFormat:@"%@FORMAT=%@&SERVICE=%@&VERSION=%@&EXCEPTIONS=%@&SRS=%@&BBOX=%@&WIDTH=%.0f&HEIGHT=%.0f&LAYERS=%@&STYLES=%@",
  78. urlPrefix, format, service, version, exceptions, crs, bbox, size.width, size.height, layers, styles];
  79. }
  80. -(NSString *)createGetMapForBbox:(NSString *)bbox size:(CGSize)size
  81. {
  82. return [NSString stringWithFormat:@"%@&REQUEST=GetMap", [self createBaseGet:bbox size:size]];
  83. }
  84. -(NSString *)createGetFeatureInfoForBbox:(NSString *)bbox size:(CGSize)size point:(CGPoint)point
  85. {
  86. return [NSString
  87. stringWithFormat:@"%@&REQUEST=GetFeatureInfo&INFO_FORMAT=%@&X=%.0f&Y=%.0f&QUERY_LAYERS=%@",
  88. [self createBaseGet:bbox size:size], infoFormat, point.x, point.y, queryLayers];
  89. }
  90. -(NSString *)createGetCapabilities
  91. {
  92. return [NSString stringWithFormat:@"%@SERVICE=%@&VERSION=%@&REQUEST=GetCapabilities", urlPrefix, service, version];
  93. }
  94. -(BOOL)isVisible
  95. {
  96. return ![@"" isEqualToString:layers];
  97. }
  98. // [ layerA, layer B ] -> layerA,layerB
  99. +(NSString *)escapeAndJoin:(NSArray *)elements
  100. {
  101. NSMutableArray *encoded = [NSMutableArray array];
  102. for (NSString *element in elements) {
  103. [encoded addObject:[element stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
  104. }
  105. return [encoded componentsJoinedByString:@","];
  106. }
  107. // layerA,layerB -> [ layerA, layer B ]
  108. +(NSArray *)splitAndDecode:(NSString *)joined
  109. {
  110. NSMutableArray *split = [NSMutableArray array];
  111. if ([joined length] == 0) {
  112. return split;
  113. }
  114. for (NSString *element in [joined componentsSeparatedByString:@","]) {
  115. [split addObject:[element stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
  116. }
  117. return split;
  118. }
  119. -(NSArray *)selectedLayerNames
  120. {
  121. return [RMWMS splitAndDecode:layers];
  122. }
  123. -(void)setSelectedLayerNames:(NSArray *)layerNames
  124. {
  125. [self setLayers:[RMWMS escapeAndJoin:layerNames]];
  126. }
  127. -(NSArray *)selectedQueryLayerNames
  128. {
  129. return [RMWMS splitAndDecode:queryLayers];
  130. }
  131. -(void)setSelectedQueryLayerNames:(NSArray *)layerNames
  132. {
  133. [self setQueryLayers:[RMWMS escapeAndJoin:layerNames]];
  134. }
  135. -(BOOL)selected:(NSString *)layerName
  136. {
  137. return [[self selectedLayerNames] containsObject:layerName];
  138. }
  139. -(void)select:(NSString *)layerName queryable:(BOOL)isQueryable
  140. {
  141. NSMutableArray *array = [NSMutableArray arrayWithArray:[self selectedLayerNames]];
  142. if (![array containsObject:layerName]) {
  143. [array addObject:layerName];
  144. [self setSelectedLayerNames:array];
  145. }
  146. if (isQueryable) {
  147. array = [NSMutableArray arrayWithArray:[self selectedQueryLayerNames]];
  148. if (![array containsObject:layerName]) {
  149. [array addObject:layerName];
  150. [self setSelectedQueryLayerNames:array];
  151. }
  152. }
  153. }
  154. -(void)deselect:(NSString *)layerName
  155. {
  156. NSMutableArray *array = [NSMutableArray arrayWithArray:[self selectedLayerNames]];
  157. [array removeObject:layerName];
  158. [self setSelectedLayerNames:array];
  159. array = [NSMutableArray arrayWithArray:[self selectedQueryLayerNames]];
  160. [array removeObject:layerName];
  161. [self setSelectedQueryLayerNames:array];
  162. }
  163. - (void) dealloc
  164. {
  165. [self setUrlPrefix:nil];
  166. [self setLayers:nil];
  167. [self setStyles:nil];
  168. [self setQueryLayers:nil];
  169. [self setCrs:nil];
  170. [self setInfoFormat:nil];
  171. [self setFormat:nil];
  172. [self setService:nil];
  173. [self setVersion:nil];
  174. [self setExceptions:nil];
  175. [super dealloc];
  176. }
  177. @end