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 out of the [-90,90] range.
check out the code below,
exif_dict = piexif.load('images/DJI_0026.JPG')
long = 0
latt = 0
value = exif_dict['GPS']
if value:
lat = value[2]
lon = value[4]
for i in range(3):
if i == 1:
latt += lat[i][0]/60.0
elif i == 2:
latt += lat[i][0]/3600.0
else:
latt += lat[i][0]
for i in range(3):
if i == 1:
long += lon[i][0]/60.0
elif i == 2:
long += lon[i][0]/3600.0
else:
long += lon[i][0]
print(latt, long)
value = {0: (2, 3, 0, 0), 1: b'N', 2: ((19, 1), (8, 1), (595773, 10000)), 3: b'E', 4: ((73, 1), (0, 1), (131775, 10000)), 5: 0, 6: (70989, 1000)}
I am concerned with latitude and longitude, that is stored in value of key 2 and 4.
latitude = 19+8/60.0+595773/3600.0
longitude = 73+0/60.0+131775/3600.0
that is what the output is.
OutPut: 184.62583333333333 109.60416666666666
Please let me know how to normalise the latitude in the range of [-90,90].
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 rationals
2: ((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:
def convert_DMS_tuple(tup):
d, m, s = [t[0]/t[1] for t in tup]
degrees = d + m/60 + s/3600
return degrees
def EXIF_to_lat_long(exif_data):
# Latitude is counted positive towards N
lat_sign = 1 if exif_data[1] == b'N' else -1
lat = lat_sign*convert_DMS_tuple(exif_data[2])
# Longitude is counted positive towards E
long_sign = 1 if 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))