I want to share with you a little Bash function that allows me to pass simple GPS coordinates for writing. The documentation explains the problem and the solution:
# Contradicting the documentation (<https://exiftool.org/TagNames/GPS.html>), `exiftool` ignores signs when setting GPS positions. This functions creates correct parameters from simple GPS coordinates.
# Accepts unsigned floating point GPS position arguments in this order: latitude (y coordinate), longitude (x)[, altitude]
# All values must be free of any characters included in $IFS
# If a value starts with a negative sign "-", the corresponding separate reference identifiers are changed accordingly
# The output is a string of several arguments as accepted by `exiftool` for setting GPS values, including `-n` and simple reference values. So this string must be included in the argument list _without_ quotes, and all remaining arguments must be formatted for `-n`. Example:
# exiftool $(exiftool_setgpsargs -6.5 -50.123456 7) FILE
exiftool_setgpsargs() { # Updated implementation according to the reply
echo "-GPSLatitude*=$1 -GPSLongitude*=$2 ${3+-GPSAltitude*=$3}"
}
: 'exiftool_setgpsargs() {
declare latitudeRef=N latitude="$1" longitudeRef=E longitude="$2"
[ "${latitude:0:1}" == "-" ] && {
latitudeRef=S
latitude="${latitude:1}"
}
[ "${longitude:0:1}" == "-" ] && {
longitudeRef=W
longitude="${longitude:1}"
}
declare altitudeArgs=""
[ "${3-}" ] && {
declare altitude="$3"
declare altitudeRef=0
[ "${altitude:0:1}" == "-" ] && {
altitude="${altitude:1}"
altitudeRef=1
}
altitudeArgs="-GPSAltitudeRef=$altitudeRef -GPSAltitude=$altitude"
}
echo " -n -GPSLatitudeRef=$latitudeRef -GPSLatitude=$latitude -GPSLongitudeRef=$longitudeRef -GPSLongitude=$longitude $altitudeArgs "
}'
You can set the reference tag to the gps value directly and exiftool will figure out and set the correct direction. No need for any logic to see if a value is negative or not. So for example, you can use
exiftool -GPSLatitude=-22.95194 -GPSLatitudeRef=-22.95194 -GPSLongitude=-43.21056 -GPSLongitudeRef=-43.21056 file.jpg
and the results will be
C:\Programs\My_Stuff>exiftool -G1 -a -s -e -GPS* y:\!temp\Test4.jpg
[GPS] GPSVersionID : 2.3.0.0
[GPS] GPSLatitudeRef : South
[GPS] GPSLatitude : 22 deg 57' 6.98"
[GPS] GPSLongitudeRef : West
[GPS] GPSLongitude : 43 deg 12' 38.02"
Or even simpler, you can use wildcards in tag names and set both in one shot
C:\>exiftool -P -overwrite_original -GPSLatitude*=-22.95194 -GPSLongitude*=-43.21056 y:\!temp\Test4.jpg
1 image files updated
C:\>exiftool -G1 -a -s -e -GPS* y:\!temp\Test4.jpg
[GPS] GPSVersionID : 2.3.0.0
[GPS] GPSLatitudeRef : South
[GPS] GPSLatitude : 22 deg 57' 6.98"
[GPS] GPSLongitudeRef : West
[GPS] GPSLongitude : 43 deg 12' 38.02"
Thanks for this hint 8) !
Since ExifTool 12.36, this is also possible:
exiftool -gpsposition="-22.95194,-43.21056" FILE
This will set the 4 associated EXIF GPS tags.
- Phil