Skip to content Skip to sidebar Skip to footer

How To Normalise Latitude Into The Range Of -90 To 90?

Using piexif I got the DMS of Latitude and longitude which I am trying to convert in decimal degree, but for some images I am getting lattitude value as 184.62583333333333 which is

Solution 1:

The data as returned by piexif for the GPS coordinates has the following format:

exif_data= {0:(2, 3, 0, 0),
             # Latitude: b'N' or b'S'1:b'N', 
             # deg, min, sec as (numerator,denominator) of rationals2:((19, 1), (8, 1), (595773, 10000)),
             # Longitude: b'E' or b'W'3:b'E', 
             4:((73, 1), (0, 1), (131775, 10000)), 
             5:0, 6:(70989, 1000)}

Latitude and longitude are given as positive, rational values, towards N or S (resp. E or W).

We need to convert the positive values from DMS to decimal, then give them the right sign depending on the direction:

defconvert_DMS_tuple(tup):
    d, m, s = [t[0]/t[1] for t in tup]
    degrees = d + m/60 + s/3600return degrees

defEXIF_to_lat_long(exif_data):
    # Latitude is counted positive towards N
    lat_sign = 1if exif_data[1] == b'N'else -1
    lat = lat_sign*convert_DMS_tuple(exif_data[2])

    # Longitude is counted positive towards E
    long_sign = 1if exif_data[3] == b'E'else -1
    long = long_sign*convert_DMS_tuple(exif_data[4])

    return lat, long

EXIF_to_lat_long(exif_data)
# (19.149882583333333, 73.00366041666666)

You can use this like:

exif_dict = piexif.load('images/DJI_0026.JPG')
value = exif_dict['GPS']
if value:
   print(EXIF_to_lat_long(value))

Solution 2:

From the example values you gave, it looks obvious that you should divide val[0] by val[1].

For example, 595773 / 10000 = 59, which makes perfect sense for a second.

By the way, you don't need the for loops. They make your code longer than necessary.

There's probably a reason why value[1] == b'N'. Your current code does not evaluate it though. This means that your code only works for 1/4 of the Earth's surface.

See https://sno.phy.queensu.ca/~phil/exiftool/TagNames/GPS.html for a good overview. Your code should be able to interpret South and West as well.

Post a Comment for "How To Normalise Latitude Into The Range Of -90 To 90?"