2018-07-18 Geocoding using Perl

Back from those holidays and don’t remember where the pictures were taken? If you took them with a smartphone, chances are the coordinates are part of the EXIF data encoded in the image itself. Here’s a Perl script which extracts that data and tries to find a city nearby using Open Street Map data.

#!/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;
}

Example usage:

$ where-was-it.pl IMG_1001.JPG
IMG_1001.JPG
2018-06-29 12:35:15
Stationen, Olof Palmes plats, Vaksalastaden, Centrum, Uppsala, Uppsala County, Svealand, 75460, Sweden

​#Perl ​#Pictures