correction of GPS data by calculated modifications within exiftools?

Started by alfric, March 01, 2021, 04:25:58 PM

Previous topic - Next topic

alfric

Hi,
I am a bloody beginner in the use of scripts and programming languages. However, the great package of exiftool already allowed me to solve some issues in my project, but I assume that it could be done more elegantly.
My project: To install a uMap-based documentation of remarkable historical houses in our community. I collected >400 Fotos and was able to link them to the OSM map using uMap and the scripts Meijiro (here the links, if you are interested: [ http://umap.openstreetmap.fr] and [ https://github.com/dmpop/mejiro] (sorry, in German); the beta version of my map is [ http://u.osmfr.org/m/553313].
The problem was, that the position fixed in the exif is the position of the camera (iPhone), but not of the object. This makes it difficult, to assign a house number to the documented building.
I therefore designed a rough correction of the GPS data by an arbitrary offset of 15m into the image direction, so that at least the correct side of the street is hit. When exporting the location data and the imgDirection, I corrected a few test data by the following modification in an excel table:
Latitude offset by 0,00014, Longitude offset by =0,00021 (decimal degrees) what is equivalent to 15m.  The formula then is: corrected Latitude = Lat + (sin(Direction°+90°)*00014).  Corrected Longitude = Lon + (sin(Direction°)*00021).
My question: can that be calculated already within exiftool? Perhaps with composite tags? I have capitulated in understanding what to do.
Remains to say that exiftool is such a great project! And Phil, I love your fantastic bird photo gallery!
alfric

StarGeek

It sounds like it might be possible and it's giving me a few ideas.  Do you know what tag holds the Direction?  Run this command on the file to get the full data
exiftool -G1 -a -s file.jpg

Also, I think there could be an option to have a different offset if desired.  Can you share the formula you're using to get the offset?
"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

Phil Harvey

Hi Alfric,

I'm glad you found my bird pictures. :)

You're right, a user-defined Composite tag is what you want. 

Assuming the direction is in GPSImgDirection, the config file would be:

%Image::ExifTool::UserDefined = (
    'Image::ExifTool::Composite' => {
        MyLatitude => {
            Require => {
                0 => 'GPSLatitude',
                1 => 'GPSImgDirection',
            },
            ValueConv => '$val[0] + cos($val[1]*3.14159/180)*0.00014',
        },
        MyLongitude => {
            Require => {
                0 => 'GPSLongitude',
                1 => 'GPSImgDirection',
            },
            ValueConv => '$val[0] + sin($val[1]*3.14159/180)*0.00021',
        },
    },
);
1; # end


But this could easily be generalized to work for any lat/lon instead of using the fixed offsets that you calculated.

- 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 ($).

alfric

Dear Phil,
Thanks for the rapid response. but I must confess, that I do not understand how to run this piece. Obviously, not standalone, as the source folder Dir is not provided. Can I directly run it or must I start with a -config, or put it in the home folder? Have I to give it a name before?
Sorry, I am not really familiar with terminal procedures and Perl etc...

StarGeek

Copy the text, save it to a file, in the same directory as exiftool if possible.  Then start any command you want to use it on with the -Config option as the very first option
exiftool -config MyCorrectGPS.config ...morestuff...

If you can't save it to the same directory as exiftool, then you need the full path to the config file.

If you don't have an .ExifTool_config set up already, you can use this and skip having to add the -config option at the beginning.  See the example.config file for details.
"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

alfric

Its again me. I made a .txt file (by TextEdit, Mac, OS 10.14.6) of the script given by Phil, and named it MycorrectGPS.config; save in the BIN folder where exiftool is also deposited by the installation program. I started in Terminal as described by StarGeek with "exiftool -config MyCorrectGPS.config " followed by some commands for output used earlier. It seemed to find the config file, but then an error message was displayed: "Unrecognized character \xC2; marked by <-- HERE after <-- HERE near column 1 at MyCorrectGPS.config line 2." I have no idea what this means...

Phil Harvey

The Mac copies the non-breaking spaces, which don't work in Perl.  This is a problem with the forum software that the maintainers refuse to fix.

Anyway, try the attached config file instead.

- 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 ($).

alfric

No error messages anymore, but the GPS data read out are not altered...

StarGeek

The new locations are in the MyLatitude and MyLongitude tags.  You can use them as is or you can copy them back into the file.
exiftool -config MyCorrectGPS.config "-GpsLatitude*<MyLatitude" "-GpsLongitude*<MyLongitude" /path/to/files/

Though technically, they should in the LocationShownGPSLatitude and LocationShownGPSLongitude tags, but odds are you display program doesn't know about those tags.

Edit: Ooops, my mistake, forgot to include the config option
"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

alfric

It´s strange. When running  [exiftool -config MyCorrectGPS.config -T -FileName -GPSImgDirection -GPSLatitude -GPSLongitude -n -MyLatitude -MyLongitude ~/desktop/test_composite/ > out.tx] , both original and modified GPS values are exported. However, it seems that they are not written to the image files: no duplication of files into orig and new file, questioning the images by a new run of exif tool commands does not deliver any MyLongitude etc, neither values nor Tag names. So, also the command proposed by StarGeek in his last post fails.
Is it necessary to integrate a write command, e.g. in the above command line, or in the config file?

Phil Harvey

If you are using the -config option to see MyLatitude and MyLongitude, then you must also use that option when copying these tags to GPSLatitude and GPSLongitude (in StarGeeks' command).

- 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 ($).

alfric

Thanks to all - now it works!
In case other newbies would like to use this option, I have put together the idea, the parameters used and the calculations in the following:

Re-calculation of GPS position data in exif to incorporate an offset towards object

Problem to be solved: When taking a photo, the GPS position saved is that of the camera, not that of the object. If a series of photos is done with fixed distance camera-object, a correction of the GPS coordinates to reflect the true position of the object would be desirable. For that, the recorded GPSImgDirection [°] is used to calculate the coordinates of the desired offset and to correct the GPS values accordingly (Caveat: Not all cameras/smartphones might record this parameter!). In my example (documentation of historical buildings in the streets of our community) an offset of 15m was used. The parameters and formulas used are as follows:
#Remark: degree (°) used in decimal version [ddd.dddddd)

Latitude:    1° = 111 000 m   1m = 0.000009°    15m ≈ 0.00014°   
The factor [meter per 1°] for longitude is dependent on the latitude position and has to be extracted from appropriate tables.  For 52° Latitude it is:
Longitude:   1° = 68 000 m   1m = 0.000015°      15m ≈ 0.00022°

Formula for offset of 15m:
Latitude (N):  GPSLatitude° + (cos (GPSImgDirection°) x 0.00014°)       
Longitude (E): GPSLongitude° + (sin (GPSImgDirection°) x 0.00022°)

# The sin / cos function sometimes needs as argument the radiant; the formula then has to be:

Latitude (N):  GPSLatitude° + (cos (GPSImgDirection° * 3.1416/180) * 0.00014°)       
Longitude (E): GPSLongitude° + (sin (GPSImgDirection° * 3.1416/180) * 0.00022°)

exiftool commands to be used for calculation via Terminal

First a configuration file has to be saved in the same folder where the exiftool is deposited; in my case (Mac) this was /usr/local/bin.
The file is a .txt file named MyCorrectGPS.config containing the following text sequence provided by Phil:

%Image::ExifTool::UserDefined = (
    'Image::ExifTool::Composite' => {
        MyLatitude => {
            Require => {
                0 => 'GPSLatitude',
                1 => 'GPSImgDirection',
            },
            ValueConv => '$val[0] + cos($val[1]*3.14159/180)*0.00014',
        },
        MyLongitude => {
            Require => {
                0 => 'GPSLongitude',
                1 => 'GPSImgDirection',
            },
            ValueConv => '$val[0] + sin($val[1]*3.14159/180)*0.00021',
        },
    },
);
1; # end


# a) To write the new GPS position into the image files (and additional saving of the original, unmodified file as "ImgName.jpg_original"), exiftool is startet with the commands:

exiftool -config MyCorrectGPS.config "-GpsLatitude*<MyLatitude" "-GpsLongitude*<MyLongitude" -n -T ~/desktop/gpscorr/ > out.txt

# b) To export the GPS data of corrected or uncorrected files thereafter to a csv output file (saved as "out.txt" in the home folder; attention: is overwritten when a next folder is processed) run:

exiftool -FileName -GPSLatitude -GPSLongitude -n -csv ~/desktop/gpscorr/ > out.txt

# I thank Phil Harvey for developing the impressive exiftool program and for detailed help in realizing my thoughts in exiftool commands; also StarGeek helped to eliminate my mistakes in running the commands.