Bash function for setting simple GPS coordinates

Started by Andi, January 13, 2022, 04:57:55 PM

Previous topic - Next topic

Andi

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 "
}'

StarGeek

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"
"It didn't work" isn't helpful. What was the exact command used and the output.
Read FAQ #3 and use that cmd
Please use the Code button for exiftool output

Please include your OS/Exiftool version/filetype

Andi


Phil Harvey

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
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).