RMCoordinateGridSource.m
13.1 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
//
// RMCoordinateGridSource.m
// MapView
//
// Copyright (c) 2008-2012, Route-Me Contributors
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
#import "RMCoordinateGridSource.h"
#import "RMTileCache.h"
#define kTileSidePadding 25.0 // px
static double coordinateGridSpacing[19] = {
45.0, // 0
45.0, // 1
45.0, // 2
10.0, // 3
5.0, // 4
5.0, // 5
2.0, // 6
1.0, // 7
1.0, // 8
0.5, // 9
0.25, // 10
(1.0 / 60.0) * 5.0, // 11
(1.0 / 60.0) * 3.0, // 12
(1.0 / 60.0) * 2.0, // 13
(1.0 / 60.0), // 14
(1.0 / 60.0), // 15
(1.0 / 60.0), // 16
(1.0 / 60.0), // 17
(1.0 / 60.0), // 18
};
static double coordinateGridSpacingDecimal[19] = {
45.0, // 0
45.0, // 1
45.0, // 2
10.0, // 3
5.0, // 4
5.0, // 5
2.0, // 6
1.0, // 7
1.0, // 8
0.5, // 9
0.25, // 10
0.10, // 11
0.05, // 12
0.05, // 13
0.01, // 14
0.01, // 15
0.01, // 16
0.01, // 17
0.01, // 18
};
@implementation RMCoordinateGridSource
@synthesize gridColor = _gridColor;
@synthesize gridLineWidth = _gridLineWidth;
@synthesize gridLabelInterval = _gridLabelInterval;
@synthesize gridMode = _gridMode;
@synthesize minorLabelColor = _minorLabelColor;
@synthesize minorLabelFont = _minorLabelFont;
@synthesize majorLabelColor = _majorLabelColor;
@synthesize majorLabelFont = _majorLabelFont;
- (id)init
{
if (!(self = [super init]))
return nil;
self.minZoom = 5;
self.maxZoom = 17;
self.opaque = NO;
self.gridColor = [UIColor colorWithWhite:0.1 alpha:0.6];
self.gridLineWidth = 2.0;
self.gridLabelInterval = 1;
self.gridMode = GridModeGeographicDecimal;
self.minorLabelColor = self.majorLabelColor = [UIColor colorWithWhite:0.1 alpha:0.7];
self.minorLabelFont = [UIFont boldSystemFontOfSize:14.0];
self.majorLabelFont = [UIFont boldSystemFontOfSize:11.0];
return self;
}
- (UIImage *)imageForTile:(RMTile)tile inCache:(RMTileCache *)tileCache
{
if (tile.zoom < 0 || tile.zoom > 18)
return nil;
UIImage *image = nil;
tile = [[self mercatorToTileProjection] normaliseTile:tile];
image = [tileCache cachedImage:tile withCacheKey:[self uniqueTilecacheKey]];
if (image)
return image;
// Implementation
RMProjectedRect planetBounds = self.projection.planetBounds;
double scale = (1<<tile.zoom);
double tileMetersPerPixel = planetBounds.size.width / (self.tileSideLength * scale);
double paddedTileSideLength = self.tileSideLength + (2.0 * kTileSidePadding);
CGPoint bottomLeft = CGPointMake((tile.x * self.tileSideLength) - kTileSidePadding,
((scale - tile.y - 1) * self.tileSideLength) - kTileSidePadding);
RMProjectedRect normalizedProjectedRect;
normalizedProjectedRect.origin.x = (bottomLeft.x * tileMetersPerPixel) - fabs(planetBounds.origin.x);
normalizedProjectedRect.origin.y = (bottomLeft.y * tileMetersPerPixel) - fabs(planetBounds.origin.y);
normalizedProjectedRect.size.width = paddedTileSideLength * tileMetersPerPixel;
normalizedProjectedRect.size.height = paddedTileSideLength * tileMetersPerPixel;
CLLocationCoordinate2D southWest = [self.projection projectedPointToCoordinate:
RMProjectedPointMake(normalizedProjectedRect.origin.x,
normalizedProjectedRect.origin.y)];
CLLocationCoordinate2D northEast = [self.projection projectedPointToCoordinate:
RMProjectedPointMake(normalizedProjectedRect.origin.x + normalizedProjectedRect.size.width,
normalizedProjectedRect.origin.y + normalizedProjectedRect.size.height)];
double gridSpacing;
switch (self.gridMode)
{
case GridModeGeographic: {
gridSpacing = coordinateGridSpacing[tile.zoom];
break;
}
case GridModeGeographicDecimal:
case GridModeUTM:
default: {
gridSpacing = coordinateGridSpacingDecimal[tile.zoom];
break;
}
}
double coordinatesLatitudeSpan = northEast.latitude - southWest.latitude,
coordinatesLongitudeSpan = northEast.longitude - southWest.longitude;
double bottom = floor(southWest.latitude / gridSpacing) * gridSpacing,
top = floor(northEast.latitude / gridSpacing) * gridSpacing;
double left = ceil(southWest.longitude / gridSpacing) * gridSpacing,
right = ceil(northEast.longitude / gridSpacing) * gridSpacing;
// Draw the tile
UIGraphicsBeginImageContext(CGSizeMake(paddedTileSideLength, paddedTileSideLength));
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context, self.gridColor.CGColor);
CGContextSetLineWidth(context, self.gridLineWidth);
// Grid lines
for (double row = top; row >= bottom; row -= gridSpacing)
{
CGFloat yCoordinate = paddedTileSideLength - (((row - southWest.latitude) / coordinatesLatitudeSpan) * paddedTileSideLength);
CGContextMoveToPoint(context, 0.0, yCoordinate);
CGContextAddLineToPoint(context, paddedTileSideLength, yCoordinate);
}
for (double column = left; column <= right; column += gridSpacing)
{
CGFloat xCoordinate = ((column - southWest.longitude) / coordinatesLongitudeSpan) * paddedTileSideLength;
CGContextMoveToPoint(context, xCoordinate, 0.0);
CGContextAddLineToPoint(context, xCoordinate, paddedTileSideLength);
}
CGContextStrokePath(context);
// Labels
for (double row = top; row >= bottom; row -= gridSpacing)
{
if (self.gridLabelInterval > 1 && fmod(round(row / gridSpacing), (double)self.gridLabelInterval) != 0)
continue;
CGFloat yCoordinate = paddedTileSideLength - (((row - southWest.latitude) / coordinatesLatitudeSpan) * paddedTileSideLength);
for (double column = (left - (gridSpacing/2.0)); column <= (right + (gridSpacing / 2.0)); column += gridSpacing)
{
CGFloat xCoordinate = ((column - southWest.longitude) / coordinatesLongitudeSpan) * paddedTileSideLength;
NSString *label1 = nil, *label2 = nil;
double degrees = (row > 0.0 ? floor(row) : ceil(row));
double fraction = (row > 0.0 ? row - floor(row) : ceil(row) - row);
switch (self.gridMode)
{
case GridModeGeographic: {
label1 = [NSString stringWithFormat:@"%.0f˚", degrees];
label2 = [NSString stringWithFormat:@"%02.0f'", fraction * 60.0];
break;
}
case GridModeGeographicDecimal:
case GridModeUTM:
default: {
label1 = [NSString stringWithFormat:@"%.0f", degrees];
label2 = [NSString stringWithFormat:@"%02.0f", fraction * 100.0];
}
}
// potential problem: some of these functions are not thread safe, so the app will crash
// if any other thread uses these functions outside of the @synchronized
@synchronized (self)
{
CGSize label1Size = [label1 sizeWithFont:self.majorLabelFont];
CGSize label2Size = [label2 sizeWithFont:self.minorLabelFont];
CGFloat upperBorder = yCoordinate - MAX((label1Size.height / 2.0), (label2Size.height / 2.0));
CGRect labelBackgroundRect = CGRectMake(xCoordinate - label1Size.width - 3.0, upperBorder - 1.0, label1Size.width + label2Size.width + 8.0, MAX(label1Size.height, label2Size.height) + 2.0);
CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);
UIRectFill(labelBackgroundRect);
CGContextSetFillColorWithColor(context, self.majorLabelColor.CGColor);
[label1 drawAtPoint:CGPointMake(xCoordinate - label1Size.width - 1.0, upperBorder) withFont:self.majorLabelFont];
CGContextSetFillColorWithColor(context, self.minorLabelColor.CGColor);
[label2 drawAtPoint:CGPointMake(xCoordinate + 1.0, upperBorder) withFont:self.minorLabelFont];
}
}
}
for (double column=left; column<=right; column += gridSpacing)
{
if (self.gridLabelInterval > 1 && fmod(round(column / gridSpacing), (double)self.gridLabelInterval) != 0)
continue;
CGFloat xCoordinate = ((column - southWest.longitude) / coordinatesLongitudeSpan) * paddedTileSideLength;
for (double row = (top + (gridSpacing/2.0)); row >= (bottom - (gridSpacing/2.0)); row -= gridSpacing)
{
CGFloat yCoordinate = paddedTileSideLength - (((row - southWest.latitude) / coordinatesLatitudeSpan) * paddedTileSideLength);
NSString *label1 = nil, *label2 = nil;
double degrees = (column > 0.0 ? floor(column) : ceil(column));
double fraction = (column > 0.0 ? column - floor(column) : ceil(column) - column);
switch (self.gridMode)
{
case GridModeGeographic: {
label1 = [NSString stringWithFormat:@"%.0f˚", degrees];
label2 = [NSString stringWithFormat:@"%02.0f'", fraction * 60.0];
break;
}
case GridModeGeographicDecimal:
case GridModeUTM:
default: {
label1 = [NSString stringWithFormat:@"%.0f", degrees];
label2 = [NSString stringWithFormat:@"%02.0f", fraction * 100.0];
}
}
@synchronized (self)
{
CGSize label1Size = [label1 sizeWithFont:self.majorLabelFont];
CGSize label2Size = [label2 sizeWithFont:self.minorLabelFont];
CGFloat upperBorder = yCoordinate - MAX((label1Size.height / 2.0), (label2Size.height / 2.0));
CGRect labelBackgroundRect = CGRectMake(xCoordinate - label1Size.width - 3.0, upperBorder - 1.0, label1Size.width + label2Size.width + 8.0, MAX(label1Size.height, label2Size.height) + 2.0);
CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);
UIRectFill(labelBackgroundRect);
CGContextSetFillColorWithColor(context, self.majorLabelColor.CGColor);
[label1 drawAtPoint:CGPointMake(xCoordinate - label1Size.width - 1.0, upperBorder) withFont:self.majorLabelFont];
CGContextSetFillColorWithColor(context, self.minorLabelColor.CGColor);
[label2 drawAtPoint:CGPointMake(xCoordinate + 1.0, upperBorder) withFont:self.minorLabelFont];
}
}
}
// Image
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], CGRectMake(kTileSidePadding, kTileSidePadding, self.tileSideLength, self.tileSideLength));
image = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
if (image)
[tileCache addImage:image forTile:tile withCacheKey:[self uniqueTilecacheKey]];
return image;
}
- (NSString *)uniqueTilecacheKey
{
NSString *tileCacheKey = nil;
switch (self.gridMode)
{
case GridModeGeographic: {
tileCacheKey = @"RMCoordinateGridGeographic";
break;
}
case GridModeGeographicDecimal:
{
tileCacheKey = @"RMCoordinateGridDecimal";
break;
}
case GridModeUTM: {
tileCacheKey = @"RMCoordinateGridUTM";
break;
}
}
if ( ! tileCacheKey)
tileCacheKey = @"RMCoordinateGrid";
return tileCacheKey;
}
- (NSString *)shortName
{
return @"Coordinate grid";
}
- (NSString *)longDescription
{
return [self shortName];
}
- (NSString *)shortAttribution
{
return @"n/a";
}
- (NSString *)longAttribution
{
return @"n/a";
}
@end