-
Notifications
You must be signed in to change notification settings - Fork 0
/
kml_com.m
38 lines (33 loc) · 1.34 KB
/
kml_com.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
//
// kml_com.c
// KMLViewer
//
// Created by Mojtaba Cazi on 2/27/18.
//
#include "kml_com.h"
// Convert a KML coordinate list string to a C array of CLLocationCoordinate2Ds.
// KML coordinate lists are longitude,latitude[,altitude] tuples specified by whitespace.
void strToCoords(NSString *str, CLLocationCoordinate2D **coordsOut, NSUInteger *coordsLenOut) {
NSUInteger read = 0, space = 10;
CLLocationCoordinate2D *coords = malloc(sizeof(CLLocationCoordinate2D) * space);
NSArray *tuples = [str componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
for (NSString *tuple in tuples) {
if (read == space) {
space *= 2;
coords = realloc(coords, sizeof(CLLocationCoordinate2D) * space);
}
double lat, lon;
NSScanner *scanner = [[NSScanner alloc] initWithString:tuple];
[scanner setCharactersToBeSkipped:[NSCharacterSet characterSetWithCharactersInString:@","]];
BOOL success = [scanner scanDouble:&lon];
if (success)
success = [scanner scanDouble:&lat];
if (success) {
CLLocationCoordinate2D c = CLLocationCoordinate2DMake(lat, lon);
if (CLLocationCoordinate2DIsValid(c))
coords[read++] = c;
}
}
*coordsOut = coords;
*coordsLenOut = read;
}