#!/usr/local/bin/perl -w
###########################################
# photo-gps-match
# Find photos from similar GPS region
# Mike Schilli, 2014 (m@perlmeister.com)
###########################################
use strict;
use Elasticsearch;
use IPhonePicGeo;

my $idx = "photos";

my( $pic ) = @ARGV;
die "usage: $0 pic" if !defined $pic;

my( $lat, $lon ) = 
   IPhonePicGeo::photo_latlon( $pic );

my $es = Elasticsearch->new( );

my $results = $es->search(
    index => $idx,
    size  => 100,
    body  => {
      query => {
        match_all => {},
      },
      filter => {
        geo_distance => {
          distance => "1km",
          "Location" => [ $lat, $lon ],
        }
      }
    }
);

my @files = ();

for my $result ( 
    @{ $results->{ hits }->{ hits } } ) {
  push @files, 
       $result->{ _source }->{ file };
}

system "eog", @files;
