PageRenderTime 45ms CodeModel.GetById 18ms app.highlight 24ms RepoModel.GetById 1ms app.codeStats 1ms

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