Convert decimal coordinate into degrees, minutes, seconds, direction
Asked Answered
P

6

12

I have the following so far, but can't figure out a tidy way to get the direction letters in without a bunch of messy if statements. Any ideas? Ideally I'd like to extend the CLLocation class with a category to do this.

-(NSString *)nicePosition{

double latitude = [self.latitude doubleValue];
double longitude = [self.longitude doubleValue];

int latSeconds = (int)round(latitude * 3600);
int latDegrees = latSeconds / 3600;
latSeconds = abs(latSeconds % 3600);
int latMinutes = latSeconds / 60;
latSeconds %= 60;

int longSeconds = (int)round(longitude * 3600);
int longDegrees = longSeconds / 3600;
longSeconds = abs(longSeconds % 3600);
int longMinutes = longSeconds / 60;
longSeconds %= 60;

//TODO: Use N,E,S,W notation in lat/long

return [NSString stringWithFormat:@"%i° %i' %i\", %i° %i' %i\"", latDegrees, latMinutes, latSeconds, longDegrees, longMinutes, longSeconds];
}

For the record I did the following.

-(NSString *)nicePosition{

double latitude = [self.latitude doubleValue];
double longitude = [self.longitude doubleValue];

int latSeconds = (int)round(abs(latitude * 3600));
int latDegrees = latSeconds / 3600;
latSeconds = latSeconds % 3600;
int latMinutes = latSeconds / 60;
latSeconds %= 60;

int longSeconds = (int)round(abs(longitude * 3600));
int longDegrees = longSeconds / 3600;
longSeconds = longSeconds % 3600;
int longMinutes = longSeconds / 60;
longSeconds %= 60;

char latDirection = (latitude >= 0) ? 'N' : 'S';
char longDirection = (longitude >= 0) ? 'E' : 'W';

return [NSString stringWithFormat:@"%i° %i' %i\" %c, %i° %i' %i\" %c", latDegrees, latMinutes, latSeconds, latDirection, longDegrees, longMinutes, longSeconds, longDirection];
}
Pomelo answered 13/1, 2012 at 14:9 Comment(1)
Am I being dense or is it just the numerical sign of the long/lat that determines NESW? Then you would just use: NSLog(@"%@%@", (lat > 0) ? @"N" : (lat < 0) ? @"S" : @"", (long > 0) ? @"E" : (long < 0) ? @"W"); Still has an element of "messy ifs" I suppose..Icosahedron
L
16

Standard way:

char lonLetter = (lon > 0) ? 'E' : 'W';
char latLetter = (lat > 0) ? 'N' : 'S';
Live answered 13/1, 2012 at 14:14 Comment(1)
Yeah this is what I ended up doing. You answered first. :)Pomelo
O
10

Here's some Objective-C based on Daniel's solution above:

- (NSString*)coordinateString {

    int latSeconds = (int)(self.latitude * 3600);
    int latDegrees = latSeconds / 3600;
    latSeconds = ABS(latSeconds % 3600);
    int latMinutes = latSeconds / 60;
    latSeconds %= 60;

    int longSeconds = (int)(self.longitude * 3600);
    int longDegrees = longSeconds / 3600;
    longSeconds = ABS(longSeconds % 3600);
    int longMinutes = longSeconds / 60;
    longSeconds %= 60;

    NSString* result = [NSString stringWithFormat:@"%d°%d'%d\"%@ %d°%d'%d\"%@",
                        ABS(latDegrees),
                        latMinutes,
                        latSeconds,
                        latDegrees >= 0 ? @"N" : @"S",
                        ABS(longDegrees),
                        longMinutes,
                        longSeconds,
                        longDegrees >= 0 ? @"E" : @"W"];

    return result;    
}
Odericus answered 3/9, 2013 at 0:40 Comment(2)
How would that look in swift?Calycine
Why are you truncating (with (int)(coordinate * 3600)) and not rounding to get seconds, as Daniel Lidström did?Leanto
J
3

Here's a solution in C#:

    void Run(double latitude, double longitude)
    {
        int latSeconds = (int)Math.Round(latitude * 3600);
        int latDegrees = latSeconds / 3600;
        latSeconds = Math.Abs(latSeconds % 3600);
        int latMinutes = latSeconds / 60;
        latSeconds %= 60;

        int longSeconds = (int)Math.Round(longitude * 3600);
        int longDegrees = longSeconds / 3600;
        longSeconds = Math.Abs(longSeconds % 3600);
        int longMinutes = longSeconds / 60;
        longSeconds %= 60;

        Console.WriteLine("{0}° {1}' {2}\" {3}, {4}° {5}' {6}\" {7}",
            Math.Abs(latDegrees),
            latMinutes,
            latSeconds,
            latDegrees >= 0 ? "N" : "S",
            Math.Abs(longDegrees),
            longMinutes,
            longSeconds,
            latDegrees >= 0 ? "E" : "W");
    }

This is an example run:

new Program().Run(-15.14131211, 56.345678);
new Program().Run(15.14131211, -56.345678);
new Program().Run(15.14131211, 56.345678);

Which prints:

15° 8' 29" S, 56° 20' 44" W
15° 8' 29" N, 56° 20' 44" E
15° 8' 29" N, 56° 20' 44" E

Hope this helps, and that it does the right thing. Good luck!

Jaguar answered 13/1, 2012 at 14:23 Comment(0)
S
3

If you want to do it in swift you can make something like that:

import MapKit

extension CLLocationCoordinate2D {

    var latitudeDegreeDescription: String {
        return fromDecToDeg(self.latitude) + " \(self.latitude >= 0 ? "N" : "S")"
    }
    var longitudeDegreeDescription: String {
        return fromDecToDeg(self.longitude) + " \(self.longitude >= 0 ? "E" : "W")"
    }
    private func fromDecToDeg(input: Double) -> String {
        var inputSeconds = Int(input * 3600)
        let inputDegrees = inputSeconds / 3600
        inputSeconds = abs(inputSeconds % 3600)
        let inputMinutes = inputSeconds / 60
        inputSeconds %= 60
        return "\(abs(inputDegrees))°\(inputMinutes)'\(inputSeconds)''"
    }
}
Sterculiaceous answered 21/6, 2016 at 14:42 Comment(0)
D
1

Regarding to Alex's answer, here is a solution in Swift 3 with an output tuple. It returns the coordinates in a tuple.

Furthermore, it really extends the class CLLocationDegrees and doesn't require an extra parameter.

import MapKit

extension CLLocationDegrees {

    func degreeRepresentation() -> (northOrEast: Bool, degrees: Int, minutes: Int, seconds: Int) {
        var inputSeconds = Int(self * 3600)
        let inputDegrees = inputSeconds / 3600
        inputSeconds = abs(inputSeconds % 3600)
        let inputMinutes = inputSeconds / 60
        inputSeconds %= 60

        return (inputDegrees > 0, abs(inputDegrees), inputMinutes, inputSeconds)
    }

}
Discreet answered 2/6, 2017 at 13:59 Comment(0)
S
0
int latSeconds = (int)round(abs(latitude * 3600));

This is mistake! Proper is

int latSeconds = abs(round(latitude * 3600));  
Sandeesandeep answered 16/4, 2013 at 17:22 Comment(1)
Please explain why.Leanto

© 2022 - 2024 — McMap. All rights reserved.