1//
2//  AIRGoogleMapWMSTile.m
3//  AirMaps
4//
5//  Created by nizam on 10/28/18.
6//  Copyright © 2018. All rights reserved.
7//
8
9#ifdef HAVE_GOOGLE_MAPS
10
11#import "AIRGoogleMapWMSTile.h"
12
13@implementation AIRGoogleMapWMSTile
14
15-(id) init
16{
17    self = [super init];
18    _opacity = 1;
19    return self ;
20}
21
22- (void)setZIndex:(int)zIndex
23{
24    _zIndex = zIndex;
25    _tileLayer.zIndex = zIndex;
26}
27- (void)setTileSize:(NSInteger)tileSize
28{
29    _tileSize = tileSize;
30    if(self.tileLayer) {
31        self.tileLayer.tileSize = tileSize;
32        [self.tileLayer clearTileCache];
33    }
34}
35- (void)setMinimumZ:(NSInteger)minimumZ
36{
37    _minimumZ = minimumZ;
38    if(self.tileLayer && _minimumZ) {
39        [self.tileLayer setMinimumZ: _minimumZ ];
40        [self.tileLayer clearTileCache];
41    }
42}
43
44- (void)setMaximumZ:(NSInteger)maximumZ
45{
46    _maximumZ = maximumZ;
47    if(self.tileLayer && maximumZ) {
48        [self.tileLayer setMaximumZ: _maximumZ ];
49        [self.tileLayer clearTileCache];
50    }
51}
52- (void)setOpacity:(float)opacity
53{
54    _opacity = opacity;
55    if(self.tileLayer ) {
56        [self.tileLayer setOpacity:opacity];
57        [self.tileLayer clearTileCache];
58    }
59}
60
61- (void)setUrlTemplate:(NSString *)urlTemplate
62{
63    _urlTemplate = urlTemplate;
64    WMSTileOverlay *tile = [[WMSTileOverlay alloc] init];
65    [tile setTemplate:urlTemplate];
66    [tile setMaximumZ:  _maximumZ];
67    [tile setMinimumZ: _minimumZ];
68    [tile setOpacity: _opacity];
69    [tile setTileSize: _tileSize];
70    [tile setZIndex: _zIndex];
71    _tileLayer = tile;
72}
73@end
74
75@implementation WMSTileOverlay
76-(id) init
77{
78    self = [super init];
79    _MapX = -20037508.34789244;
80    _MapY = 20037508.34789244;
81    _FULL = 20037508.34789244 * 2;
82    return self ;
83}
84
85-(NSArray *)getBoundBox:(NSInteger)x yAxis:(NSInteger)y zoom:(NSInteger)zoom
86{
87    double tile = _FULL / pow(2.0, (double)zoom);
88    NSArray *result  =[[NSArray alloc] initWithObjects:
89                       [NSNumber numberWithDouble:_MapX + (double)x * tile ],
90                       [NSNumber numberWithDouble:_MapY - (double)(y+1) * tile ],
91                       [NSNumber numberWithDouble:_MapX + (double)(x+1) * tile ],
92                       [NSNumber numberWithDouble:_MapY - (double)y * tile ],
93                       nil];
94
95    return result;
96
97}
98
99- (UIImage *)tileForX:(NSUInteger)x y:(NSUInteger)y zoom:(NSUInteger)zoom
100{
101    NSInteger maximumZ = self.maximumZ;
102    NSInteger minimumZ = self.minimumZ;
103    if(maximumZ && (long)zoom > (long)maximumZ) {
104        return nil;
105    }
106    if(minimumZ && (long)zoom < (long)minimumZ) {
107        return nil;
108    }
109    NSArray *bb = [self getBoundBox:x yAxis:y zoom:zoom];
110    NSMutableString *url = [self.template mutableCopy];
111    [url replaceOccurrencesOfString: @"{minX}" withString:[NSString stringWithFormat:@"%@", bb[0]] options:0 range:NSMakeRange(0, url.length)];
112    [url replaceOccurrencesOfString: @"{minY}" withString:[NSString stringWithFormat:@"%@", bb[1]] options:0 range:NSMakeRange(0, url.length)];
113    [url replaceOccurrencesOfString: @"{maxX}" withString:[NSString stringWithFormat:@"%@", bb[2]] options:0 range:NSMakeRange(0, url.length)];
114    [url replaceOccurrencesOfString: @"{maxY}" withString:[NSString stringWithFormat:@"%@", bb[3]] options:0 range:NSMakeRange(0, url.length)];
115    [url replaceOccurrencesOfString: @"{width}" withString:[NSString stringWithFormat:@"%d", (int)self.tileSize] options:0 range:NSMakeRange(0, url.length)];
116    [url replaceOccurrencesOfString: @"{height}" withString:[NSString stringWithFormat:@"%d", (int)self.tileSize] options:0 range:NSMakeRange(0, url.length)];
117    NSURL *uri =  [NSURL URLWithString:url];
118    NSData *data = [NSData dataWithContentsOfURL:uri];
119    UIImage *img = [[UIImage alloc] initWithData:data];
120    return img;
121}
122
123@end
124
125#endif
126