TransWikia.com

Identifying coordinate format from exif data

Geographic Information Systems Asked by Digfield on January 2, 2021

I am using the Python exif module to extract coordinates from photos. The methods for doing so are: gps_latitude and gps_longitude.

The output for latitude is: (32.0, 37.0, 37.15) and longitude is: (116.0, 30.0, 37.0)

I extracted the location using another tool as well:

32°37'37.2"N 116°30'37.0"W and 32.6270000, -116.5102778

How can I identify the output format: (32.0, 37.0, 37.15), (116.0, 30.0, 37.0) so I can covert it to perhaps decimal degrees?

from exif import Image

with open('IMG_0252.jpg', 'rb') as image_file:
    my_image = Image(image_file)
    print(my_image.gps_latitude, my_image.gps_longitude)

One Answer

They are in degrees, minutes, seconds (DMS) format.

To convert to decimal degrees, the basic formula is:

degrees + minutes / 60 + seconds / 3600

Note that 'W' longitudes and 'S' latitudes are negative, so you need to use the gps_latitude_ref and gps_longitude_ref properties to determine that. Here's a worked example:

from exif import Image

def dms_to_dd(gps_coords, gps_coords_ref):
    d, m, s =  gps_coords
    dd = d + m / 60 + s / 3600
    if gps_coords_ref.upper() in ('S', 'W'):
        return -dd
    elif gps_coords_ref.upper() in ('N', 'E'):
        return dd
    else:
        raise RuntimeError('Incorrect gps_coords_ref {}'.format(gps_coords_ref))

with open('IMG_0252.jpg', 'rb') as image_file:
    my_image = Image(image_file)

    # gps_latitude = (32.0, 37.0, 37.15)
    # gps_longitude = (116.0, 30.0, 37.0)
    # 
    # gps_latitude_ref = 'N'
    # gps_longitude_ref = 'W'
    
    print(dms_to_dd(my_image.gps_latitude, my_image.gps_latitude_ref))
    print(dms_to_dd(my_image.gps_longitude, my_image.gps_longitude_ref))

    32.62699
    -116.51028

Correct answer by user2856 on January 2, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP