I got an issue with the code: ‘IFDRational’ object is not subscriptable
Here is the trace:
JavaScript
x
3
1
lat = get_decimal_from_dms(geotags['GPSLatitude'], geotags['GPSLatitudeRef'])
2
degrees = dms[0][0] / dms[0][1]
3
the is issue is: Variable Value dms (45.0, 46.0, 34.29) ref ‘N’
JavaScript
1
16
16
1
{'GPSAltitude': 22.90324416619237,
2
'GPSAltitudeRef': b'x00',
3
'GPSDateStamp': '2020:10:17',
4
'GPSDestBearing': 18.1919587128,
5
'GPSDestBearingRef': 'M',
6
'GPSHPositioningError': 65.0,
7
'GPSImgDirection': 108.199192887128,
8
'GPSImgDirectionRef': 'M',
9
'GPSLatitude': (12.0, 26.0, 14.09),
10
'GPSLatitudeRef': 'N',
11
'GPSLongitude': (1.0, 47.0, 39.38),
12
'GPSLongitudeRef': 'E',
13
'GPSSpeed': 0.0,
14
'GPSSpeedRef': 'K',
15
'GPSTimeStamp': (12.0, 58.0, 42.0)}
16
That what I get from an image for instance. I think the code assimilate GPSLatitudeRef to degrees = dms[0][0] / dms[0][1]. The thing is GPSLatitudeRef is a letter none a number.
JavaScript
1
56
56
1
def get_geotagging(exifgps):
2
3
geotagging = {}
4
for (idx, tag) in TAGS.items():
5
if tag == 'GPSInfo':
6
if idx not in exifgps:
7
raise ValueError("No EXIF geotagging found")
8
9
for (key, val) in GPSTAGS.items():
10
if key in exifgps[idx]:
11
geotagging[val] = exifgps[idx][key]
12
13
return geotagging
14
15
def get_decimal_from_dms(dms, ref):
16
17
degrees = dms[0][0] / dms[0][1]
18
minutes = dms[1][0] / dms[1][1] / 60.0
19
seconds = dms[2][0] / dms[2][1] / 3600.0
20
21
if ref in ['S', 'W']:
22
degrees = -degrees
23
minutes = -minutes
24
seconds = -seconds
25
26
return round(degrees + minutes + seconds, 5)
27
28
def get_lat_coordinates(geotags):
29
lat = get_decimal_from_dms(geotags['GPSLatitude'], geotags['GPSLatitudeRef'])
30
return lat
31
32
def get_lon_coordinates(geotags):
33
lon = get_decimal_from_dms(geotags['GPSLongitude'], geotags['GPSLongitudeRef'])
34
return lon
35
36
try:
37
exifgps = img.getexif()
38
geotags = get_geotagging(exifgps)
39
self.gpsinfo = geotags
40
except Exception as e:
41
pass
42
43
#try:
44
exifgps = img.getexif()
45
geotags = get_geotagging(exifgps)
46
self.latitude = get_lat_coordinates(geotags)
47
#except Exception as e:
48
#pass
49
50
#try:
51
exifgps = img.getexif()
52
geotags = get_geotagging(exifgps)
53
self.longitude = get_lon_coordinates(geotags)
54
#except Exception as e:
55
#pass
56
Advertisement
Answer
I cannot comprehend why degrees = dms[0][0] / dms[0][1]
is necessary when you can simply extract degrees from the tuple using degrees = dms[0]
Replace
JavaScript
1
6
1
def get_decimal_from_dms(dms, ref):
2
3
degrees = dms[0][0] / dms[0][1]
4
minutes = dms[1][0] / dms[1][1] / 60.0
5
seconds = dms[2][0] / dms[2][1] / 3600.0
6
With
JavaScript
1
6
1
def get_decimal_from_dms(dms, ref):
2
3
degrees = dms[0]
4
minutes = dms[1] / 60.0
5
seconds = dms[2] / 3600.0
6