I got an issue with the code: ‘IFDRational’ object is not subscriptable
Here is the trace:
lat = get_decimal_from_dms(geotags['GPSLatitude'], geotags['GPSLatitudeRef']) degrees = dms[0][0] / dms[0][1]
the is issue is: Variable Value dms (45.0, 46.0, 34.29) ref ‘N’
{'GPSAltitude': 22.90324416619237, 'GPSAltitudeRef': b'x00', 'GPSDateStamp': '2020:10:17', 'GPSDestBearing': 18.1919587128, 'GPSDestBearingRef': 'M', 'GPSHPositioningError': 65.0, 'GPSImgDirection': 108.199192887128, 'GPSImgDirectionRef': 'M', 'GPSLatitude': (12.0, 26.0, 14.09), 'GPSLatitudeRef': 'N', 'GPSLongitude': (1.0, 47.0, 39.38), 'GPSLongitudeRef': 'E', 'GPSSpeed': 0.0, 'GPSSpeedRef': 'K', 'GPSTimeStamp': (12.0, 58.0, 42.0)}
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.
def get_geotagging(exifgps): geotagging = {} for (idx, tag) in TAGS.items(): if tag == 'GPSInfo': if idx not in exifgps: raise ValueError("No EXIF geotagging found") for (key, val) in GPSTAGS.items(): if key in exifgps[idx]: geotagging[val] = exifgps[idx][key] return geotagging def get_decimal_from_dms(dms, ref): degrees = dms[0][0] / dms[0][1] minutes = dms[1][0] / dms[1][1] / 60.0 seconds = dms[2][0] / dms[2][1] / 3600.0 if ref in ['S', 'W']: degrees = -degrees minutes = -minutes seconds = -seconds return round(degrees + minutes + seconds, 5) def get_lat_coordinates(geotags): lat = get_decimal_from_dms(geotags['GPSLatitude'], geotags['GPSLatitudeRef']) return lat def get_lon_coordinates(geotags): lon = get_decimal_from_dms(geotags['GPSLongitude'], geotags['GPSLongitudeRef']) return lon try: exifgps = img.getexif() geotags = get_geotagging(exifgps) self.gpsinfo = geotags except Exception as e: pass #try: exifgps = img.getexif() geotags = get_geotagging(exifgps) self.latitude = get_lat_coordinates(geotags) #except Exception as e: #pass #try: exifgps = img.getexif() geotags = get_geotagging(exifgps) self.longitude = get_lon_coordinates(geotags) #except Exception as e: #pass
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
def get_decimal_from_dms(dms, ref): degrees = dms[0][0] / dms[0][1] minutes = dms[1][0] / dms[1][1] / 60.0 seconds = dms[2][0] / dms[2][1] / 3600.0
With
def get_decimal_from_dms(dms, ref): degrees = dms[0] minutes = dms[1] / 60.0 seconds = dms[2] / 3600.0