I am trying to geotag images from trips all over the world. DateTimeOriginal of the pics don't contain TimeZone information but are in local times. Every day has a gpx track available. The first point of a track could obviously be used to find out the correct time zone. Can exiftool somehow do this automatically?
If not, maybe that's a possible enhancement request? Exiftool would first need to find the correct gpx file by date (eg when passing world/*.jpg and world/*.gpx) and then query its time zone from first point and use that for geotagging images on that date.
// possible time zone query code from lat/lon:
// sorry for stupidities, not my programming language :-)
function get_nearest_timezone($cur_lat, $cur_long, $country_code = '')
{
$timezone_ids = ($country_code) ? DateTimeZone::listIdentifiers(DateTimeZone::PER_COUNTRY, $country_code)
: DateTimeZone::listIdentifiers();
if($timezone_ids && is_array($timezone_ids) && isset($timezone_ids[0])) {
$time_zone = '';
$tz_distance = 0;
//only one identifier?
if (count($timezone_ids) == 1) {
$time_zone = $timezone_ids[0];
} else {
foreach($timezone_ids as $timezone_id) {
$timezone = new DateTimeZone($timezone_id);
$location = $timezone->getLocation();
$tz_lat = $location['latitude'];
$tz_long = $location['longitude'];
$theta = $cur_long - $tz_long;
$distance = (sin(deg2rad($cur_lat)) * sin(deg2rad($tz_lat)))
+ (cos(deg2rad($cur_lat)) * cos(deg2rad($tz_lat)) * cos(deg2rad($theta)));
$distance = acos($distance);
$distance = abs(rad2deg($distance));
if (!$time_zone || $tz_distance > $distance) {
$time_zone = $timezone_id;
$tz_distance = $distance;
}
}
}
return $time_zone;
}
return 'unknown';
}
There is a file in the full distribution: time_zone.config (https://raw.githubusercontent.com/exiftool/exiftool/master/config_files/time_zone.config) that will do this calculation for you.
- Phil
Quote from: joeuser23 on April 03, 2020, 05:02:29 AMExiftool would first need to find the correct gpx file by date (eg when passing world/*.jpg and world/*.gpx) and then query its time zone from first point and use that for geotagging images on that date.
Phil's suggestion works if the file has already been geotagged, i.e. it has the
GPSDateStamp and
GPSTimeStamp already set. Correct me if I'm wrong but your phrasing here seems to me that you haven't done so yet.
You don't need to try to figure out the exact track log to use to geotag the images. You can just point exiftool to the correct directory. See the second to last paragraph under the
-geotag option (https://exiftool.org/exiftool_pod.html#geotag-TRKFILE).
Maybe I misunderstood what ExifTool can do... or I phrased incorrectly.
1) I have thousands of images from all over the planet, NOT geotagged, NO TimeZone information, but DateTimeOriginal properly set to the local time from whereever the picture was taken.
2) I have GPX tracks from all over the planet for every single day that I took pictures.
What do I do now? :-)
From my understanding, to properly geotag each image, ExifTool would have to know its time zone. Finding this time zone automatically means:
a) query an images DateTimeOriginal.
b) find the gpx track with date (of first track point) matching the images original date.
c) read lat/lon of first track point and use it to calculate the time zone with above algorithm.
d) geotag the image using proper offset from the calculated time zone.
Can ExifTool do this? Is this maybe the default mode of operation? From my experiences, when I don't manually specify the proper time zone, the locations would all be off +-X hours.
See Geotagging with ExifTool (https://exiftool.org/geotag.html).
Most of that exiftool can handle automatically. GPS tracks have the time set in UTC. Exiftool will grab the time stamp from the files and compare it to all the tracks it can access using the -geotag option.
But the problem in this case is that to figure out the offset, it would use the computer's local time to calculate the difference if the time zone wasn't already set. And since you say that these images were taken all over the world, then obviously the local time zone is incorrect
So basically, yea, exiftool can't automatically figure out the time zone from the lat/long.
One thing I'm not sure you compensated for in your example code is the possibility of Standard/Daylight Savings time.
Quote from: StarGeek on April 03, 2020, 11:45:18 AM
But the problem in this case is that to figure out the offset, it would use the computer's local time to calculate the difference if the time zone wasn't already set. And since you say that these images were taken all over the world, then obviously the local time zone is incorrect
So basically, yea, exiftool can't automatically figure out the time zone from the lat/long.
Then consider it an enhancement request, the algorithm is just above ;-). Not sure about daylight saving times either though. Anyway, all the information needed is in DateTimeOriginal and the GPX file. Why bother the user with such things as time zones? I agree it's not a big deal normally because you rarely skip time zones that often. Unfortunately, my images do.
So maybe I need to run two passes with ExifTool? First pass geotags everything ignoring time zones. Positions will be off by a few hours but at least should be in the right zone, geographically speaking. Then I run a little script that reads each images (wrong) position, calcs the correct timezone from lat/lon and writes it back to exif data. Then I run ExifTool geotagging again, this time with proper time zone already embedded in each exif.
Would that work?
QuoteThen consider it an enhancement request, the algorithm is just above
I don't understand. Your algorithm relies on the image being geotagged with lat/lon, correct? If this was done, then GPSDate/Time should also be available. Or perhaps I'm missing something because TLDR
- Phil
Edit: Ah. I get it. Two passes. But it is beyond the scope of ExifTool to calculate time zone from GPS position, so you need to do this some other way.
Quote from: Phil Harvey on April 03, 2020, 12:24:17 PM
QuoteThen consider it an enhancement request, the algorithm is just above
I don't understand. Your algorithm relies on the image being geotagged with lat/lon, correct? If this was done, then GPSDate/Time should also be available. Or perhaps I'm missing something because TLDR
- Phil
The images are not geotagged (yet) and have no timezone information. The algorithm is meant to be used on the first track point of a gps file with a date matching the image date, to automatically find out the time zone for the image.
Quote from: Phil Harvey on April 03, 2020, 12:24:17 PM
Edit: Ah. I get it. Two passes. But it is beyond the scope of ExifTool to calculate time zone from GPS position, so you need to do this some other way.
Ok... too bad... seemed like a rather useful idea, not having to deal with time zones manually any longer. Also looks like a rather simple addition, compared to other super complex things that ExifTool can handle.
But you rock anyway, thanks for a great tool... :-)
Quote from: joeuser23 on April 03, 2020, 01:01:54 PM
Also looks like a rather simple addition, compared to other super complex things that ExifTool can handle.
Time zones are actually very complex. See my favorite Computerphile video (https://www.youtube.com/watch?v=-5wpm-gesOY).
Quote from: StarGeek on April 03, 2020, 01:18:13 PM
Quote from: joeuser23 on April 03, 2020, 01:01:54 PM
Also looks like a rather simple addition, compared to other super complex things that ExifTool can handle.
Time zones are actually very complex. See my favorite Computerphile video (https://www.youtube.com/watch?v=-5wpm-gesOY).
That's why I was hoping ExifTool could do it for me :-).
StarGeek has just posted a config file here (https://exiftool.org/forum/index.php?topic=11026.msg58905#new) that uses the Google Maps API to look up the time zone based on GPS position.
- Phil
Unfortunately, after re-reading their posts, that config probably won't work in this case like I thought it might. The files still don't have the coordinates embedded in them. As they mentioned, it would have to take the date only, search through all the tracks for a track that has an entry taken on that date, then use the first gps coordinates to lookup the time zone, and then apply that to all the other files, assuming they're in the same directory.
Unfortunately, the flaw there is that taking the date isn't accurate enough. Because of the possible time zone difference, the local date in the image may be before or after the entry in the track.
I see no easy way to accurately script this.