-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTLMapSimpleLayer.m
183 lines (154 loc) · 6.11 KB
/
TLMapSimpleLayer.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//
// TLMapSimpleLayer.m
// TileMap
//
// Created by Nathan Vander Wilt on 8/20/09.
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import "TLMapSimpleLayer.h"
#import <QuartzCore/QuartzCore.h>
#import "TLMapProjection.h"
#import "TLMapLayer.h"
#import "NSAffineTransform+TLAdditions.h"
@implementation TLMapSimpleLayer
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.wantsLayer = YES;
self.layer.needsDisplayOnBoundsChange = YES;
}
return self;
}
+ (NSRect)mercatorSquare {
static double RadiansToDegrees = 180.0 / M_PI;
CGFloat n = (CGFloat)(atan(sinh(M_PI)) * RadiansToDegrees);
CGFloat s = (CGFloat)(-atan(sinh(M_PI)) * RadiansToDegrees);
CGFloat e = 180.0f;
CGFloat w = -180.0f;
return NSMakeRect(w, s, e-w, n-s);
}
+ (NSRect)worldBounds {
CGFloat n = 90.0f;
CGFloat s = -90.0f;
CGFloat e = 180.0f;
CGFloat w = -180.0f;
return NSMakeRect(w, s, e-w, n-s);
}
+ (NSRect)conusBounds {
// from http://en.wikipedia.org/wiki/Extreme_points_of_the_United_States
CGFloat n = +49 + ((23 + (4 / 60.0f)) / 60.0f); // Lake of the Woods, Minnesota
CGFloat s = +25 + ((7 + (6 / 60.0f)) / 60.0f); // Cape Sable, Florida
CGFloat e = -66 - (57 / 60.0f); // West Quoddy Head, Maine
CGFloat w = -124 - ((43 + (59 / 60.0f)) / 60.0f); // Cape Alava, Washington
return NSMakeRect(w, s, e-w, n-s);
}
+ (NSRect)africaBounds {
// from http://en.wikipedia.org/wiki/Extreme_points_of_Africa#Extreme_points
CGFloat n = +37 + (21 / 60.0f); // Ras ben Sakka, Tunisia
CGFloat s = -34 - (50 / 60.0f); // Cape Agulhas, South Africa
CGFloat e = +51 + ((27 + (52 / 60.0f)) / 60.0f); // Ras Hafun, Somalia
CGFloat w = -17 - ((33 + (22 / 60.0f)) / 60.0f); // Pointe des Almadies, Senegal
return NSMakeRect(w, s, e-w, n-s);
}
+ (NSRect)europeBounds {
// from http://en.wikipedia.org/wiki/Extreme_points_of_Europe
CGFloat n = +71 + ((8 + (3 / 60.0f)) / 60.0f); // Cape Nordkinn, Norway
CGFloat s = +36; // Punta de Tarifa, Spain
CGFloat e = +68 + (11 / 60.0f); // mouth of Bajdarata river
CGFloat w = -9 - ((30 + (3 / 60.0f)) / 60.0f); // Cabo da Roca, Portugal
return NSMakeRect(w, s, e-w, n-s);
}
+ (NSRect)asiaBounds {
// from http://en.wikipedia.org/wiki/Extreme_points_of_Asia#Extreme_points
CGFloat n = +77 + (43 / 60.0f); // Cape Chelyuskin, Russia
CGFloat s = +1 + (16 / 60.0f); // Cape Piai, Malaysia
//CGFloat e = -169 - (40 / 60.0f); // Cape Dezhnev, Russia
CGFloat e = +180.0f; // Anti-meridian
CGFloat w = +26 + (4 / 60.0f); // Cape Baba, Turkey
return NSMakeRect(w, s, e-w, n-s);
}
+ (NSRect)australiaBounds {
// from http://en.wikipedia.org/wiki/Extreme_points_of_Australia
CGFloat n = -10 - (41 / 60.0f); // Cape York, Queensland
CGFloat s = -39 - (8 / 60.0f); // Wilsons Promontory, Victoria
CGFloat e = +153 + (38 / 60.0f); // Cape Byron, New South Wales
CGFloat w = +113 + (9 / 60.0f); // Steep Point, Western Australia
return NSMakeRect(w, s, e-w, n-s);
}
+ (NSRect)southAmericaBounds {
// from http://en.wikipedia.org/wiki/Extreme_points_of_South_America
CGFloat n = +12 + ((27 + (31 / 60.0f)) / 60.0f); // Punta Gallinas, Colombia
CGFloat s = -53 - ((53 + (47 / 60.0f)) / 60.0f); // Cape Froward, Chile
CGFloat e = -34 - ((47 + (35 / 60.0f)) / 60.0f); // Ponta do Seixas, Brazil
CGFloat w = -81 - ((19 + (43 / 60.0f)) / 60.0f); // Punta Pariñas, Peru
return NSMakeRect(w, s, e-w, n-s);
}
+ (NSRect)northAmericaBounds {
// from http://en.wikipedia.org/wiki/Extreme_points_of_North_America
CGFloat n = +71 + (58 / 60.0f); // Boothia Peninsula, Nunavut
CGFloat s = +7 + ((10 + (29 / 60.0f)) / 60.0f); // Punta Mariato, Panama
CGFloat e = -55 - ((37 + (15 / 60.0f)) / 60.0f); // Cape St Charles, Labrador
CGFloat w = -168 - (5 / 60.0f); // Cape Prince of Wales, Alaska
return NSMakeRect(w, s, e-w, n-s);
}
+ (NSRect)antarcticaBounds {
// from http://en.wikipedia.org/wiki/Extreme_points_of_Antarctica
CGFloat n = -63 - (23 / 60.0f); // Hope Bay, Antarctic Peninsula
CGFloat s = -90.0f; // South Pole
CGFloat e = +180.0f; // Anti-meridian
CGFloat w = -180.0f; // Anti-meridian
return NSMakeRect(w, s, e-w, n-s);
}
+ (NSRect)fakeProjectShape:(NSRect)shape
withProjection:(TLMapProjection*)proj
{
shape = NSIntersectionRect(shape, [[self class] mercatorSquare]);
NSPoint bottomLeft = [proj projectCoordinate:
TLMapCoordinateMake(shape.origin.y,
shape.origin.x)];
NSPoint topRight = [proj projectCoordinate:
TLMapCoordinateMake(shape.origin.y + shape.size.height,
shape.origin.x + shape.size.width)];
return NSMakeRect(bottomLeft.x, bottomLeft.y,
topRight.x - bottomLeft.x, topRight.y - bottomLeft.y);
}
- (void)drawShape:(NSRect)shape {
TLMapProjection* proj = [[self tl_mapHost] projection];
NSRect projShape = [[self class] fakeProjectShape:shape
withProjection:proj];
NSBezierPath* projectedPath = [NSBezierPath bezierPathWithRect:projShape];
NSAffineTransform* t = [[self tl_mapHost] drawTransform];
[[t transformBezierPath:projectedPath] fill];
}
- (void)drawRect:(NSRect)rect {
(void)rect;
NSRect shape = NSZeroRect;
[[NSGraphicsContext currentContext] saveGraphicsState];
CGContextSetAlpha([[NSGraphicsContext currentContext] graphicsPort], 0.125f);
[[NSColor blueColor] setFill];
shape = [[self class] worldBounds];
//[self drawShape:shape];
[[NSColor redColor] setFill];
shape = [[self class] northAmericaBounds];
[self drawShape:shape];
[[NSColor yellowColor] setFill];
shape = [[self class] asiaBounds];
[self drawShape:shape];
[[NSColor blackColor] setFill];
shape = [[self class] australiaBounds];
[self drawShape:shape];
[[NSColor brownColor] setFill];
shape = [[self class] africaBounds];
[self drawShape:shape];
[[NSColor whiteColor] setFill];
shape = [[self class] europeBounds];
[self drawShape:shape];
[[NSColor greenColor] setFill];
shape = [[self class] southAmericaBounds];
[self drawShape:shape];
[[NSColor grayColor] setFill];
shape = [[self class] antarcticaBounds];
[self drawShape:shape];
[[NSGraphicsContext currentContext] restoreGraphicsState];
}
@end