2020-11-20 Geolocation using Exif data and Open Street Map

As I’ve shared this Perl script twice, now, I figured perhaps it should be on the blog as well. It allows me to tell where a picture was taken, if and only if the picture contains geolocation metadata, which most cell phone images should. I also used to have a regular camera with a built-in GPS, but then I bought a camera that took better pictures and had no GPS…

#!/usr/bin/env perl
use Modern::Perl;
use Image::ExifTool;
use Geo::Coder::OSM;
use JSON;
use Data::Dumper;
binmode(STDOUT, ':utf8'); # force UTF-8 output
my $geocoder = Geo::Coder::OSM->new;
my $exifTool = Image::ExifTool->new;
$exifTool->Options(CoordFormat => q{%+.6f},
		   DateFormat => "%Y-%m-%d %H:%M:%S");
for my $file (@ARGV) {
  die "Cannot read $file" unless -f "$file";
  say "$file";
  $exifTool->ExtractInfo("$file");
  # Date
  my $date = $exifTool->GetValue('CreateDate', '');
  say $date if $date;
  # City
  my $lat = $exifTool->GetValue('GPSLatitude', '');
  my $long = $exifTool->GetValue('GPSLongitude', '');
  my $location = $geocoder->reverse_geocode(lat => $lat, lon => $long);
  die "No location data found\n" unless $geocoder->response;
  my $json = decode_json($geocoder->response->content);
  die $json->{error} . " $lat/$long\n" if $json->{error};
  say $location->{display_name} if $location;
}

​#Pictures ​#Perl ​#Programming