| 1 | package Geo::Coder::Many::Util; | ||
| 2 | |||
| 3 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 0 | ▶ | use Geo::Distance::XS; # for calculating precision |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 6 | use List::Util qw( reduce ); | ||
| 7 | use List::MoreUtils qw( any ); | ||
| 8 | |||
| 9 | our @EXPORT_OK = qw( | ||
| 10 | min_precision_filter | ||
| 11 | max_precision_picker | ||
| 12 | consensus_picker | ||
| 13 | country_filter | ||
| 14 | ); | ||
| 15 | use Exporter; | ||
| 16 | use base qw(Exporter); | ||
| 17 | |||
| 18 | our $VERSION = '0.01'; | ||
| 19 | |||
| 20 | my $GDXS = Geo::Distance->new; | ||
| 21 | |||
| 22 | =head1 NAME | ||
| 23 | |||
| 24 | Geo::Coder::Many::Util | ||
| 25 | |||
| 26 | =head1 DESCRIPTION | ||
| 27 | |||
| 28 | Miscellaneous routines that are convenient for, for example, generating | ||
| 29 | commonly used callback methods to be used with Geo::Coder::Many. | ||
| 30 | |||
| 31 | =head1 SUBROUTINES | ||
| 32 | |||
| 33 | =head2 min_precision_filter | ||
| 34 | |||
| 35 | Constructs a result filter callback which only passes results which exceed the | ||
| 36 | specified precision. | ||
| 37 | |||
| 38 | =cut | ||
| 39 | |||
| 40 | sub min_precision_filter { | ||
| 41 | my $precision_cutoff = shift; | ||
| 42 | return sub { | ||
| 43 | my $result = shift; | ||
| 44 | if ( !defined $result->{precision} ) { | ||
| 45 | return 0; | ||
| 46 | } | ||
| 47 | return $result->{precision} >= $precision_cutoff; | ||
| 48 | } | ||
| 49 | } | ||
| 50 | |||
| 51 | =head2 country_filter | ||
| 52 | |||
| 53 | Constructs a result filter callback which only passes results with the | ||
| 54 | specified 'country' value. | ||
| 55 | |||
| 56 | =cut | ||
| 57 | |||
| 58 | sub country_filter { | ||
| 59 | my $country_name = shift; | ||
| 60 | return sub { | ||
| 61 | my $result = shift; | ||
| 62 | if ( !exists $result->{country} ) { | ||
| 63 | return 0; | ||
| 64 | } | ||
| 65 | return $result->{country} eq $country_name; | ||
| 66 | } | ||
| 67 | } | ||
| 68 | |||
| 69 | =head2 max_precision_picker | ||
| 70 | |||
| 71 | A picker callback that requests all available results, and then picks the one | ||
| 72 | with the highest precision. Note that querying all available geocoders may take | ||
| 73 | a comparatively long time. | ||
| 74 | |||
| 75 | Example: | ||
| 76 | |||
| 77 | $GCMU->set_picker_callback( \&max_precision_picker ); | ||
| 78 | |||
| 79 | =cut | ||
| 80 | |||
| 81 | sub max_precision_picker { | ||
| 82 | my ($ra_results, $more_available) = @_; | ||
| 83 | |||
| 84 | # If more results are available, request them | ||
| 85 | return if $more_available; | ||
| 86 | |||
| 87 | # If we have all of the results, find the best | ||
| 88 | return &_find_max_precision($ra_results); | ||
| 89 | } | ||
| 90 | |||
| 91 | =head2 consensus_picker | ||
| 92 | |||
| 93 | Returns a picker callback that requires at least 'required_consensus' separate | ||
| 94 | geocoder results to be within a bounding square of side-length 'nearness'. If | ||
| 95 | this can be satisfied, the result from that square which has the highest | ||
| 96 | precision will be returned. Otherwise, asks for more/returns undef. | ||
| 97 | |||
| 98 | WARNING: quadratic time in length of @$ra_results - could be improved if | ||
| 99 | necessary. | ||
| 100 | |||
| 101 | Example: | ||
| 102 | |||
| 103 | $GCMU->set_picker_callback( | ||
| 104 | consensus_picker({nearness => 0.1, required_consensus => 2}) | ||
| 105 | ); | ||
| 106 | |||
| 107 | =cut | ||
| 108 | |||
| 109 | sub consensus_picker { | ||
| 110 | my $rh_args = shift; | ||
| 111 | my $nearness = $rh_args->{nearness}; | ||
| 112 | my $required_consensus = $rh_args->{required_consensus}; | ||
| 113 | return sub { | ||
| 114 | my $ra_results = shift; | ||
| 115 | |||
| 116 | for my $result_a (@{$ra_results}) { | ||
| 117 | |||
| 118 | my $lat_a = $result_a->{latitude}; | ||
| 119 | my $lon_a = $result_a->{longitude}; | ||
| 120 | |||
| 121 | # Find all of the other results that are close to this one | ||
| 122 | my @consensus = grep { | ||
| 123 | _in_box( | ||
| 124 | $lat_a, | ||
| 125 | $lon_a, | ||
| 126 | $nearness, | ||
| 127 | $_->{latitude}, | ||
| 128 | $_->{longitude} | ||
| 129 | ) | ||
| 130 | } @$ra_results; | ||
| 131 | |||
| 132 | if ($required_consensus <= @consensus) { | ||
| 133 | # If the consensus is sufficiently large, return the result | ||
| 134 | # with the highest precision | ||
| 135 | return _find_max_precision(\@consensus); | ||
| 136 | } | ||
| 137 | |||
| 138 | } | ||
| 139 | |||
| 140 | # No consensus reached | ||
| 141 | return; | ||
| 142 | }; | ||
| 143 | } | ||
| 144 | |||
| 145 | =head2 determine_precision_from_bbox | ||
| 146 | |||
| 147 | my $precision = Geo::Coder::Many::Util->determine_precision_from_bbox({ | ||
| 148 | 'lon1' => $sw_lon, | ||
| 149 | 'lat1' => $sw_lat, | ||
| 150 | 'lon2' => $ne_lon, | ||
| 151 | 'lat2' => $ne_lat, | ||
| 152 | }); | ||
| 153 | |||
| 154 | returns a precison between 0 (unknown) and 1 (highly precise) based on | ||
| 155 | the size of the box supplied | ||
| 156 | |||
| 157 | =cut | ||
| 158 | |||
| 159 | sub determine_precision_from_bbox { | ||
| 160 | my $rh_args = shift || return 0; | ||
| 161 | |||
| 162 | my $distance = $GDXS->distance('kilometer', | ||
| 163 | $rh_args->{lon1}, $rh_args->{lat1} => | ||
| 164 | $rh_args->{lon2}, $rh_args->{lat2}); | ||
| 165 | |||
| 166 | return 0 if (!defined($distance)); | ||
| 167 | return 1.0 if ($distance < 0.25); | ||
| 168 | return 0.9 if ($distance < 0.5); | ||
| 169 | return 0.8 if ($distance < 1); | ||
| 170 | return 0.7 if ($distance < 5); | ||
| 171 | return 0.6 if ($distance < 7.5); | ||
| 172 | return 0.5 if ($distance < 10); | ||
| 173 | return 0.4 if ($distance < 15); | ||
| 174 | return 0.3 if ($distance < 20); | ||
| 175 | return 0.2 if ($distance < 25); | ||
| 176 | return 0.1; | ||
| 177 | } | ||
| 178 | |||
| 179 | =head1 INTERNAL ROUTINES | ||
| 180 | |||
| 181 | =head2 _in_box | ||
| 182 | |||
| 183 | Used by consensus_picker - returns true if ($lat, $lon) is inside the square | ||
| 184 | with centre ($centre_lat, $centre_lon) and side length 2*$half_width. | ||
| 185 | |||
| 186 | =cut | ||
| 187 | |||
| 188 | sub _in_box { | ||
| 189 | my ($centre_lat, $centre_lon, $half_width, $lat, $lon) = @_; | ||
| 190 | |||
| 191 | return $centre_lat - $half_width < $lat | ||
| 192 | && $centre_lat + $half_width > $lat | ||
| 193 | && $centre_lon - $half_width < $lon | ||
| 194 | && $centre_lon + $half_width > $lon; | ||
| 195 | } | ||
| 196 | |||
| 197 | =head2 _find_max_precision | ||
| 198 | |||
| 199 | Given a reference to an array of result hashes, returns the one with the | ||
| 200 | highest precision value | ||
| 201 | |||
| 202 | =cut | ||
| 203 | |||
| 204 | sub _find_max_precision { | ||
| 205 | my $ra_results = shift; | ||
| 206 | return reduce { | ||
| 207 | ($a->{precision} || 0.0) > ($b->{precision} || 0.0) ? $a : $b | ||
| 208 | } @{$ra_results}; | ||
| 209 | } | ||
| 210 | |||
| 211 | 1; | ||