1//
2//  AIRMapWMSTile.m
3//  AirMaps
4//
5//  Created by nizam on 10/28/18.
6//  Copyright © 2018. All rights reserved.
7//
8
9#import "AIRMapWMSTile.h"
10#import <React/UIView+React.h>
11
12@implementation AIRMapWMSTile
13
14- (void)createTileOverlayAndRendererIfPossible
15{
16    if (!_urlTemplateSet) return;
17    if (_tileCachePathSet || _maximumNativeZSet) {
18        NSLog(@"tileCache new overlay dir %@", self.tileCachePath);
19        NSLog(@"tileCache %d", _tileCachePathSet);
20        NSLog(@"tileCache %d", _maximumNativeZSet);
21        self.tileOverlay = [[AIRMapWMSTileCachedOverlay alloc] initWithURLTemplate:self.urlTemplate];
22        _cachedOverlayCreated = YES;
23        if (_tileCachePathSet) {
24            NSURL *urlPath = [NSURL URLWithString:[self.tileCachePath stringByAppendingString:@"/"]];
25            if (urlPath.fileURL) {
26                self.tileOverlay.tileCachePath = urlPath;
27            } else {
28                NSURL *filePath = [NSURL fileURLWithPath:self.tileCachePath isDirectory:YES];
29                self.tileOverlay.tileCachePath = filePath;
30            }
31
32            if (_tileCacheMaxAgeSet) {
33                self.tileOverlay.tileCacheMaxAge = self.tileCacheMaxAge;
34            }
35        }
36    } else {
37        NSLog(@"tileCache normal overlay");
38        self.tileOverlay = [[AIRMapWMSTileOverlay alloc] initWithURLTemplate:self.urlTemplate];
39        _cachedOverlayCreated = NO;
40    }
41
42    [self updateProperties];
43
44    self.renderer = [[MKTileOverlayRenderer alloc] initWithTileOverlay:self.tileOverlay];
45    if (_opacitySet) {
46        self.renderer.alpha = self.opacity;
47    }
48}
49
50@end
51
52
53@implementation AIRMapWMSTileOverlay
54
55- (id)initWithURLTemplate:(NSString *)URLTemplate
56{
57    self = [super initWithURLTemplate:URLTemplate];
58    return self;
59}
60
61- (NSURL *)URLForTilePath:(MKTileOverlayPath)path
62{
63   return [AIRMapWMSTileHelper URLForTilePath:path withURLTemplate:self.URLTemplate withTileSize:self.tileSize.width];
64}
65
66@end
67
68
69@implementation AIRMapWMSTileCachedOverlay
70
71- (id)initWithURLTemplate:(NSString *)URLTemplate
72{
73    self = [super initWithURLTemplate:URLTemplate];
74    return self;
75}
76
77- (NSURL *)URLForTilePath:(MKTileOverlayPath)path
78{
79   return [AIRMapWMSTileHelper URLForTilePath:path withURLTemplate:self.URLTemplate withTileSize:self.tileSize.width];
80}
81
82@end
83
84
85@implementation AIRMapWMSTileHelper
86+ (NSURL *)URLForTilePath:(MKTileOverlayPath)path withURLTemplate:(NSString *)URLTemplate withTileSize:(NSInteger)tileSize
87{
88    NSArray *bb = [self getBoundBox:path.x yAxis:path.y zoom:path.z];
89    NSMutableString *url = [URLTemplate mutableCopy];
90    [url replaceOccurrencesOfString: @"{minX}" withString:[NSString stringWithFormat:@"%@", bb[0]] options:0 range:NSMakeRange(0, url.length)];
91    [url replaceOccurrencesOfString: @"{minY}" withString:[NSString stringWithFormat:@"%@", bb[1]] options:0 range:NSMakeRange(0, url.length)];
92    [url replaceOccurrencesOfString: @"{maxX}" withString:[NSString stringWithFormat:@"%@", bb[2]] options:0 range:NSMakeRange(0, url.length)];
93    [url replaceOccurrencesOfString: @"{maxY}" withString:[NSString stringWithFormat:@"%@", bb[3]] options:0 range:NSMakeRange(0, url.length)];
94    [url replaceOccurrencesOfString: @"{width}" withString:[NSString stringWithFormat:@"%d", (int)tileSize] options:0 range:NSMakeRange(0, url.length)];
95    [url replaceOccurrencesOfString: @"{height}" withString:[NSString stringWithFormat:@"%d", (int)tileSize] options:0 range:NSMakeRange(0, url.length)];
96    return [NSURL URLWithString:url];
97}
98
99+ (NSArray *)getBoundBox:(NSInteger)x yAxis:(NSInteger)y zoom:(NSInteger)zoom
100{
101    double MapX = -20037508.34789244;
102    double MapY = 20037508.34789244;
103    double FULL = 20037508.34789244 * 2;
104    double tile = FULL / pow(2.0, (double)zoom);
105
106    NSArray *result  =[[NSArray alloc] initWithObjects:
107                       [NSNumber numberWithDouble:MapX + (double)x * tile],
108                       [NSNumber numberWithDouble:MapY - (double)(y + 1) * tile],
109                       [NSNumber numberWithDouble:MapX + (double)(x + 1) * tile],
110                       [NSNumber numberWithDouble:MapY - (double)y * tile],
111                       nil];
112
113    return result;
114}
115
116@end