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