aboutsummaryrefslogtreecommitdiffstats
path: root/bin/geocode
blob: f6bf7914904b2fa89f1145c707cb1396a89f43c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env perl

=head1 NAME

geocode - commandline tool to test geocoders

=head1 SYNOPSIS

    $ bin/geocode --geocoder=Bing --cobrand=bromley "Glebe Rd"

    # ... if your conf/general.yml supplies GEOCODER:
    $ bin/geocode --cobrand=bromley "Glebe Rd"

    # ... if you want to use config that you have in conf/general.bing.yml
    $ bin/geocode --override-config=general.yml.bing --cobrand=bromley "Glebe Rd"

    # ... if your config only has a single entry in ALLOWED_COBRANDS
    $ bin/geocode --override-config=general.yml.zurich "Im eisernen Zeit"

    ## ... output from geocoder

=head1 OPTIONS

    --help|h           show this help
    --use-cache        use the configured GEO_CACHE (default OFF)

    # the following options take a string argument:
    --geocoder         e.g. OSM/Bing/Google/Zurich
    --cobrand          (default, bromley, zurich etc.)
    --override-config  e.g. conf/general.yml 

=cut

use strict;
use warnings;
require 5.8.0;

BEGIN {
    use File::Basename qw(dirname);
    use File::Spec;
    my $d = dirname(File::Spec->rel2abs($0));
    require "$d/../setenv.pl";
}

use Data::Dumper;
use Pod::Usage;
use feature 'say';
use File::Temp 'tempdir';

use Getopt::Long;

my %options = ( help => sub { pod2usage(0) } );

GetOptions \%options,
    'geocoder=s',
    'help|h',
    'cobrand=s',
    'override-config=s',
    'use-cache';

my $s = join ' ', @ARGV
    or pod2usage(0);

local $ENV{FMS_OVERRIDE_CONFIG} = $options{'override-config'} if $options{'override-config'};

eval 'use FixMyStreet';
eval 'use FixMyStreet::App';
eval 'use FixMyStreet::Cobrand';
eval 'use FixMyStreet::Geocode';

mySociety::Locale::gettext_domain( 'FixMyStreet' );

my $geocoder_type = $options{geocoder} || do {
    my $GEOCODER = FixMyStreet->config('GEOCODER');
    ref $GEOCODER ? $GEOCODER->{type} : $GEOCODER;
} or pod2usage(0);

my $geocoder_name = "FixMyStreet::Geocode::${geocoder_type}";
chomp $geocoder_name;
my $code_ref = $geocoder_name->can('string')
    or die "$geocoder_name is not a valid geocoder?";

my @allowed_cobrands = @{ FixMyStreet::Cobrand->get_allowed_cobrands() };

my $cobrand_option = $options{cobrand}
    || do {
        $allowed_cobrands[0]->{moniker} if scalar @allowed_cobrands == 1;
    } or pod2usage(0);

my $cobrand_name = FixMyStreet::Cobrand->get_class_for_moniker($cobrand_option);
my $cobrand = $cobrand_name->new();

say "USING COBRAND $cobrand_name";
if ($cobrand->moniker ne lc($cobrand_option)) {
    say "!!! asked for $cobrand_option";
    say "!!! Check ALLOWED_COBRANDS setting in conf/general.yml (or supplied --override-config file)";
    say Dumper(\@allowed_cobrands);
}

say "USING GEOCODER $geocoder_name "
    . ( $options{'use-cache'} ? 
        'WITH ' . FixMyStreet->config('GEO_CACHE') :
        'WITHOUT cache');

my $c = FixMyStreet::App->new();
$c->stash->{cobrand} = $cobrand;

FixMyStreet::override_config({
    # if we're not using cache, then set GEO_CACHE to a temporary directory
    $options{'use-cache'} ?
        () :
        ( GEO_CACHE => (tempdir( CLEANUP => 1 ) . '/') ),
}, sub {
    my $result = $code_ref->( $s, $c );
    say Dumper $result;
});