Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
95 lines (75 sloc) 3.18 KB
//
// WMSTileOverlay.m
// WMSOnMapKit
//
// Created by Stanislav Sumbera on 17/05/14.
// Copyright (c) 2014 sumbera. All rights reserved.
//
#import "WMSTileOverlay.h"
#import "MapViewUtils.h"
@implementation WMSTileOverlay
//------------------------------------------------------------
- (id)initWithUrl:(NSString*)urlArg UseMercator: (BOOL) useMercatorArg
{
if ((self = [self init])) {
self.url = urlArg;
self.useMercator = useMercatorArg;
}
return self;
}
//------------------------------------------------------------
- (NSURL *)URLForTilePath:(MKTileOverlayPath)path{
// BBOX in WGS84
double left = xOfColumn(path.x,path.z); //minX
double right = xOfColumn(path.x+1,path.z); //maxX
double bottom = yOfRow(path.y+1,path.z); //minY
double top = yOfRow(path.y,path.z); //maxY
// BBOX in mercator
if (self.useMercator){
left = MercatorXofLongitude(left); //minX
right = MercatorXofLongitude(right); //maxX
bottom = MercatorYofLatitude(bottom); //minY
top = MercatorYofLatitude(top); //maxY
}
NSString * resolvedUrl = [NSString stringWithFormat:@"%@&BBOX=%f,%f,%f,%f",self.url,left,bottom,right,top];
NSLog(@"Url tile overlay %@", resolvedUrl);
return [NSURL URLWithString:resolvedUrl];
}
//------------------------------------------------------------
- (NSDate *)creationDateForFile:(NSString *)fileName
{
NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:fileName error:nil];
NSDate *fileTimestamp = [fileAttributes fileCreationDate];
return fileTimestamp;
}
//------------------------------------------------------------
- (void)loadTileAtPath:(MKTileOverlayPath)path result:(void (^)(NSData *tileData, NSError *error)) result{
NSURL *url = [self URLForTilePath: path];
NSString *filePath = getFilePathForURL([url absoluteString],TILE_CACHE);
// -- check if tile is cached and if it has reached the tile cache time limit
if ([[NSFileManager defaultManager] fileExistsAtPath: filePath]){
NSDate *fileTimestamp = [self creationDateForFile:filePath];
int ageOfFile = (int) [[NSDate date] timeIntervalSinceDate:fileTimestamp];
if (ageOfFile <= TILE_CACHE_TIME_LIMIT){
NSData *tileData = [NSData dataWithContentsOfFile:filePath];
result (tileData, nil);
return;
}
}
// -- download
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]
delegate:nil
delegateQueue:[NSOperationQueue mainQueue]];
NSURLSessionDataTask *sessionTask = [session dataTaskWithURL:url
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error){
if (error) {
NSLog(@"Error downloading tile ! \n");
result(nil, error);
} else {
[data writeToFile: filePath atomically:YES];
result (data, nil);
}
}];
[sessionTask resume];
}
@end