Skip to content

Instantly share code, notes, and snippets.

@kensanata
Created August 14, 2020 21:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kensanata/5df54660bf6ac5e2420863e82f8731bc to your computer and use it in GitHub Desktop.
Save kensanata/5df54660bf6ac5e2420863e82f8731bc to your computer and use it in GitHub Desktop.
Geocoding: use Open Street Map for the location of a picture (e.g. from phones)
#!/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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment