How can I split up GPS coordinate in Objective C?

410 Views Asked by At

I need to extract the different components from a GPS coordinate string. So for example:

+30° 18' 12" N  // pull out 30, 18 & 12

or

+10° 11' 1" E    // pull out 10, 11 & 1

or

-3° 1' 2" S    // pull out -3, 1 & 2

or

-7° 12' 2" W    // pull out -7, 12 & 2

I have had a look around online and I notice there is the NSRegularExpression. I was wondering if it's possible to use this in some way? I have also had a look at the documentation provided and I have tried to put together a regex to pull out the different parts. This is what I came up with:

('+'|'-')$n°\s$n'\s$n"\s(N|E|S|W)

I'm not really sure if this is correct or not, I'm also unclear on how to use it since there aren't many tutorials/example around. Please could someone help me out? If there is a better way of doing this rather than using NSRegularExpression I'm open to it, however as far as I'm aware objective c does't have any built in regex support.

5

There are 5 best solutions below

0
zaph On BEST ANSWER

Using NSScanner:

NSScanner *scanner;
NSCharacterSet *numbersSet = [NSCharacterSet characterSetWithCharactersInString:@" °'"];
int degrees;
int minutes;
int seconds;

NSString *string = @" -7° 12' 2\" W";
scanner = [NSScanner scannerWithString:string];
[scanner setCharactersToBeSkipped:numbersSet];
[scanner scanInt:&degrees];
[scanner scanInt:&minutes];
[scanner scanInt:&seconds];
NSLog(@"degrees: %i, minutes: %i, seconds: %i", degrees, minutes, seconds);

NSLog Output:

degrees: -7, minutes: 12, seconds: 2
0
Seva Alekseyev On

RegExps are an overkill, IMHO. Use [NSString componentsSeparatedByString:] with space as the separator to split the string into parts, then [NSString intValue] to tease the numeric value of each component except for the last one.

0
Logan Serman On
NSMutableArray *newCoords = [[NSMutableArray alloc] init];
NSArray *t = [oldCoords componentsSeparatedByString: @" "];

[newCoords addObject: [[t objectAtIndex: 0] intValue];
[newCoords addObject: [[t objectAtIndex: 1] intValue];
[newCoords addObject: [[t objectAtIndex: 2] intValue];

Assuming you had the coordinates given in your post in NSString oldCoords, this would result in an NSMutableArray called newCoords which would contain the three pieces of data you need.

0
CRD On

RE's overkill (Seva)? How about objects? ;-)

NSString *coords = @"+30° 18' 12\" N";

int deg, sec, min;
char dir;

if(sscanf([coords UTF8String], "%d° %d' %d\" %c", &deg, &min, &sec, &dir) != 4)
   NSLog(@"Bad format: %@\n", coords);
else
   NSLog(@"Parsed %d deg, %d min, %d sec, dir %c\n", deg, min, sec, dir);

Whether you like this depends on your view of dropping into C, but it is direct and simple.

0
zaph On

The re you need is: @"([+-]?[0-9]+)"

Here is example code:

NSString *string;
NSString *pattern;
NSRegularExpression *regex;
NSArray *matches;

pattern = @"([+-]?[0-9]+)";

regex = [NSRegularExpression
         regularExpressionWithPattern:pattern
         options:NSRegularExpressionCaseInsensitive
         error:nil];

string = @" -7° 12' 2\" W";
NSLog(@"%@", string);
matches = [regex matchesInString:string options:0 range:NSMakeRange(0, [string length])];
degrees = [[string substringWithRange:[[matches objectAtIndex:0] range]] intValue];
minutes = [[string substringWithRange:[[matches objectAtIndex:1] range]] intValue];
seconds = [[string substringWithRange:[[matches objectAtIndex:2] range]] intValue];
NSLog(@"degrees: %i, minutes: %i, seconds: %i", degrees, minutes, seconds);

NSLog output:

degrees: -7, minutes: 12, seconds: 2