def add_lat_lon(self, lat, lon, precision=1000000): ''' Add lat, lon to gps (lat, lon in float) @source: originally from geotag_from_gpx.py ''' # convert decimal coordinates into degrees, minutes and seconds lat_deg = decimal_to_dms(lat, ["S", "N"]) lon_deg = decimal_to_dms(lon, ["W", "E"]) # convert degrees, minutes and seconds as fractions for EXIF exiv_lat = (make_fraction(lat_deg[0], 1), make_fraction(int(lat_deg[1]), 1), make_fraction(int(lat_deg[2] * precision), precision)) exiv_lon = (make_fraction(lon_deg[0], 1), make_fraction(int(lon_deg[1]), 1), make_fraction(int(lon_deg[2] * precision), precision)) # add to exif self.metadata["Exif.GPSInfo.GPSLatitude"] = exiv_lat self.metadata["Exif.GPSInfo.GPSLatitudeRef"] = lat_deg[3] self.metadata["Exif.GPSInfo.GPSLongitude"] = exiv_lon self.metadata["Exif.GPSInfo.GPSLongitudeRef"] = lon_deg[3] self.metadata["Exif.Image.GPSTag"] = 654 self.metadata["Exif.GPSInfo.GPSMapDatum"] = "WGS-84" self.metadata["Exif.GPSInfo.GPSVersionID"] = '2 0 0 0'
def add_exif_using_timestamp(filename, time, points, offset_time=0, offset_angle=0): ''' Find lat, lon and bearing of filename and write to EXIF. ''' metadata = pyexiv2.ImageMetadata(filename) metadata.read() # subtract offset in s beween gpx time and exif time t = time - datetime.timedelta(seconds=offset_time) try: lat, lon, bearing, elevation, speed = interpolate_lat_lon(points, t) lat_deg = decimal_to_dms(lat, ["S", "N"]) lon_deg = decimal_to_dms(lon, ["W", "E"]) # convert decimal coordinates into degrees, minutes and seconds as fractions for EXIF exiv_lat = (make_fraction(lat_deg[0], 1), make_fraction(int(lat_deg[1]), 1), make_fraction(int(lat_deg[2] * 1000000), 1000000)) exiv_lon = (make_fraction(lon_deg[0], 1), make_fraction(int(lon_deg[1]), 1), make_fraction(int(lon_deg[2] * 1000000), 1000000)) # convert direction into fraction bearing = offset_bearing(bearing, offset_angle) exiv_bearing = make_fraction(int(bearing * 100), 100) # add to exif metadata["Exif.GPSInfo.GPSLatitude"] = exiv_lat metadata["Exif.GPSInfo.GPSLatitudeRef"] = lat_deg[3] metadata["Exif.GPSInfo.GPSLongitude"] = exiv_lon metadata["Exif.GPSInfo.GPSLongitudeRef"] = lon_deg[3] metadata["Exif.Image.GPSTag"] = 654 metadata["Exif.GPSInfo.GPSMapDatum"] = "WGS-84" metadata["Exif.GPSInfo.GPSVersionID"] = '2 0 0 0' metadata["Exif.GPSInfo.GPSImgDirection"] = exiv_bearing metadata["Exif.GPSInfo.GPSImgDirectionRef"] = "T" if elevation is not None: exiv_elevation = make_fraction(abs(int(elevation * 10)), 10) metadata["Exif.GPSInfo.GPSAltitude"] = exiv_elevation metadata[ "Exif.GPSInfo.GPSAltitudeRef"] = '0' if elevation >= 0 else '1' metadata.write() print("Added geodata to {}: {}, {:.6f}, {:.6f}, {:.1f}".format( os.path.basename(filename), time, lat, lon, bearing)) if (speed is not None and speed < 0.6): move_into_dir(filename, os.path.dirname(filename) + "\S0") except ValueError, e: print("Skipping {0}: {1}".format(filename, e))
def add_lat_lon(self, lat, lon, precision=1e7): """Add lat, lon to gps (lat, lon in float).""" self._ef["GPS"][piexif.GPSIFD.GPSLatitudeRef] = "N" if lat > 0 else "S" self._ef["GPS"][piexif.GPSIFD.GPSLongitudeRef] = "E" if lon > 0 else "W" self._ef["GPS"][piexif.GPSIFD.GPSLongitude] = decimal_to_dms( abs(lon), int(precision)) self._ef["GPS"][piexif.GPSIFD.GPSLatitude] = decimal_to_dms( abs(lat), int(precision))
def add_exif_using_timestamp(filename, time, points, offset_time=0, offset_bearing=0): ''' Find lat, lon and bearing of filename and write to EXIF. ''' metadata = pyexiv2.ImageMetadata(filename) metadata.read() print(time) print(datetime.timedelta(seconds=offset_time)) #print(points) # subtract offset in s beween gpx time and exif time t = time - datetime.timedelta(seconds=offset_time) try: lat, lon, bearing, elevation = interpolate_lat_lon(points, t) lat_deg = decimal_to_dms(lat, ["S", "N"]) lon_deg = decimal_to_dms(lon, ["W", "E"]) # convert decimal coordinates into degrees, minutes and seconds as fractions for EXIF exiv_lat = (make_fraction(lat_deg[0],1), make_fraction(int(lat_deg[1]),1), make_fraction(int(lat_deg[2]*1000000),1000000)) exiv_lon = (make_fraction(lon_deg[0],1), make_fraction(int(lon_deg[1]),1), make_fraction(int(lon_deg[2]*1000000),1000000)) corrected_bearing = (bearing + offset_bearing) % 360 # convert direction into fraction exiv_bearing = make_fraction(int(corrected_bearing*100),100) # add to exif metadata["Exif.GPSInfo.GPSLatitude"] = exiv_lat metadata["Exif.GPSInfo.GPSLatitudeRef"] = lat_deg[3] metadata["Exif.GPSInfo.GPSLongitude"] = exiv_lon metadata["Exif.GPSInfo.GPSLongitudeRef"] = lon_deg[3] metadata["Exif.Image.GPSTag"] = 654 metadata["Exif.GPSInfo.GPSMapDatum"] = "WGS-84" metadata["Exif.GPSInfo.GPSVersionID"] = '2 0 0 0' metadata["Exif.GPSInfo.GPSImgDirection"] = exiv_bearing metadata["Exif.GPSInfo.GPSImgDirectionRef"] = "T" if elevation is not None: exiv_elevation = make_fraction(abs(int(elevation*10)),10) metadata["Exif.GPSInfo.GPSAltitude"] = exiv_elevation metadata["Exif.GPSInfo.GPSAltitudeRef"] = '0' if elevation >= 0 else '1' metadata.write() print("Added geodata to: {} time {} lat {} lon {} alt {} bearing {}".format(filename, time, lat, lon, elevation, exiv_bearing)) except ValueError, e: print("Skipping {0}: {1}".format(filename, e))
def add_lat_lon(self, lat, lon, precision=1000000): ''' Add lat, lon to gps (lat, lon in float) @source: originally from geotag_from_gpx.py ''' # convert decimal coordinates into degrees, minutes and seconds lat_deg = decimal_to_dms(lat, ["S", "N"]) lon_deg = decimal_to_dms(lon, ["W", "E"]) # convert degrees, minutes and seconds as fractions for EXIF exiv_lat = (make_fraction(lat_deg[0],1), make_fraction(int(lat_deg[1]),1), make_fraction(int(lat_deg[2]*precision),precision)) exiv_lon = (make_fraction(lon_deg[0],1), make_fraction(int(lon_deg[1]),1), make_fraction(int(lon_deg[2]*precision),precision)) # add to exif self.metadata["Exif.GPSInfo.GPSLatitude"] = exiv_lat self.metadata["Exif.GPSInfo.GPSLatitudeRef"] = lat_deg[3] self.metadata["Exif.GPSInfo.GPSLongitude"] = exiv_lon self.metadata["Exif.GPSInfo.GPSLongitudeRef"] = lon_deg[3] self.metadata["Exif.Image.GPSTag"] = 654 self.metadata["Exif.GPSInfo.GPSMapDatum"] = "WGS-84" self.metadata["Exif.GPSInfo.GPSVersionID"] = '2 0 0 0'
def add_lat_lon_general(test_obj, filename): test_latitude = 50.5475894785 test_longitude = 15.595866685 precision = 1e7 empty_exifedit = ExifEdit(filename) empty_exifedit.add_lat_lon(test_latitude, test_longitude, precision) empty_exifedit.write(EMPTY_EXIF_FILE_TEST) exif_data = load_exif() test_obj.assertEqual( (decimal_to_dms(abs(test_latitude), precision), decimal_to_dms(abs(test_longitude), precision), "N", "E"), (exif_data[EXIF_PRIMARY_TAGS_DICT['GPSInfo']][ EXIF_GPS_TAGS_DICT['GPSLatitude']], exif_data[EXIF_PRIMARY_TAGS_DICT['GPSInfo']][ EXIF_GPS_TAGS_DICT['GPSLongitude']], exif_data[EXIF_PRIMARY_TAGS_DICT['GPSInfo']][ EXIF_GPS_TAGS_DICT['GPSLatitudeRef']], exif_data[EXIF_PRIMARY_TAGS_DICT['GPSInfo']][ EXIF_GPS_TAGS_DICT['GPSLongitudeRef']]))
def test_add_negative_lat_lon(self): test_latitude = -50.5 test_longitude = -15.5 precision = 1e7 empty_exifedit = ExifEdit(EMPTY_EXIF_FILE_TEST) empty_exifedit.add_lat_lon(test_latitude, test_longitude, precision) empty_exifedit.write(EMPTY_EXIF_FILE_TEST) exif_data = load_exif() self.assertEqual( (decimal_to_dms(abs(test_latitude), precision), decimal_to_dms(abs(test_longitude), precision), "S", "W"), (exif_data[EXIF_PRIMARY_TAGS_DICT['GPSInfo']][ EXIF_GPS_TAGS_DICT['GPSLatitude']], exif_data[EXIF_PRIMARY_TAGS_DICT['GPSInfo']][ EXIF_GPS_TAGS_DICT['GPSLongitude']], exif_data[EXIF_PRIMARY_TAGS_DICT['GPSInfo']][ EXIF_GPS_TAGS_DICT['GPSLatitudeRef']], exif_data[EXIF_PRIMARY_TAGS_DICT['GPSInfo']][ EXIF_GPS_TAGS_DICT['GPSLongitudeRef']]))