| 1 | # ABSTRACT: turn WGS84 coordinates into three word addresses and vice-versa using what3words.com HTTPS API | ||
| 2 | |||
| 3 | package Geo::What3Words; | ||
| 4 | $Geo::What3Words::VERSION = '3.0.1'; | ||
| 5 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use Cpanel::JSON::XS; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use Data::Dumper; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | $Data::Dumper::Sortkeys = 1; | ||
| 10 | 1 | ▶ | use Encode qw( decode_utf8 ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 1 | ▶ | use HTTP::Tiny; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 1 | ▶ | use Net::Ping; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | 1 | ▶ | use Net::Ping::External; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | 1 | ▶ | use Ref::Util qw( is_hashref is_coderef ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 15 | 1 | ▶ | use URI::XS; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 16 | |||
| 17 | my $JSONXS = Cpanel::JSON::XS->new->allow_nonref(1); | ||
| 18 | |||
| 19 | |||
| 20 | |||
| 21 | |||
| 22 | sub new { | ||
| 23 | my ($class, %params) = @_; | ||
| 24 | |||
| 25 | my $self = {}; | ||
| 26 | $self->{api_endpoint} = $params{api_endpoint} || 'https://api.what3words.com/v3/'; | ||
| 27 | $self->{key} = $params{key} || die "API key not set"; | ||
| 28 | $self->{language} = $params{language}; | ||
| 29 | $self->{logging} = $params{logging}; | ||
| 30 | |||
| 31 | ## _ua is used for testing. But could also be used to | ||
| 32 | ## set proxies or such | ||
| 33 | $self->{ua} = $params{ua} || HTTP::Tiny->new; | ||
| 34 | |||
| 35 | my $version = $Geo::What3Words::VERSION || ''; | ||
| 36 | $self->{ua}->agent("Perl Geo::What3Words $version"); | ||
| 37 | |||
| 38 | return bless($self, $class); | ||
| 39 | } | ||
| 40 | |||
| 41 | |||
| 42 | sub ping { | ||
| 43 | my $self = shift; | ||
| 44 | |||
| 45 | ## http://example.com/some/path => example.com | ||
| 46 | ## also works with IP addresses | ||
| 47 | my $host = URI::XS->new($self->{api_endpoint})->host; | ||
| 48 | |||
| 49 | $self->_log("pinging $host..."); | ||
| 50 | |||
| 51 | my $netping = Net::Ping->new('external'); | ||
| 52 | my $res = $netping->ping($host); | ||
| 53 | |||
| 54 | $self->_log($res ? 'available' : 'unavailable'); | ||
| 55 | |||
| 56 | return $res; | ||
| 57 | } | ||
| 58 | |||
| 59 | |||
| 60 | sub words2pos { | ||
| 61 | my ($self, @params) = @_; | ||
| 62 | |||
| 63 | my $res = $self->words_to_position(@params); | ||
| 64 | if ($res && is_hashref($res) && exists($res->{coordinates})) { | ||
| 65 | return $res->{coordinates}->{lat} . ',' . $res->{coordinates}->{lng}; | ||
| 66 | } | ||
| 67 | return; | ||
| 68 | } | ||
| 69 | |||
| 70 | |||
| 71 | |||
| 72 | sub pos2words { | ||
| 73 | my ($self, @params) = @_; | ||
| 74 | my $res = $self->position_to_words(@params); | ||
| 75 | if ($res && is_hashref($res) && exists($res->{words})) { | ||
| 76 | return $res->{words}; | ||
| 77 | } | ||
| 78 | return; | ||
| 79 | } | ||
| 80 | |||
| 81 | |||
| 82 | sub valid_words_format { | ||
| 83 | my $self = shift; | ||
| 84 | my $words = shift; | ||
| 85 | |||
| 86 | ## Translating the PHP regular expression w3w uses in their | ||
| 87 | ## documentation | ||
| 88 | ## http://perldoc.perl.org/perlunicode.html#Unicode-Character-Properties | ||
| 89 | ## http://php.net/manual/en/reference.pcre.pattern.differences.php | ||
| 90 | return 0 unless $words; | ||
| 91 | return 1 if ($words =~ m/^(\p{Lower}+)\.(\p{Lower}+)\.(\p{Lower}+)$/); | ||
| 92 | return 0; | ||
| 93 | } | ||
| 94 | |||
| 95 | |||
| 96 | sub words_to_position { | ||
| 97 | my $self = shift; | ||
| 98 | my $words = shift; | ||
| 99 | |||
| 100 | return $self->_query_remote_api('convert-to-coordinates', {words => $words}); | ||
| 101 | |||
| 102 | } | ||
| 103 | |||
| 104 | |||
| 105 | sub position_to_words { | ||
| 106 | my $self = shift; | ||
| 107 | my $position = shift; | ||
| 108 | my $language = shift || $self->{language}; | ||
| 109 | |||
| 110 | # https://developer.what3words.com/public-api/docs#convert-to-3wa | ||
| 111 | return $self->_query_remote_api( | ||
| 112 | 'convert-to-3wa', | ||
| 113 | { coordinates => $position, | ||
| 114 | language => $language | ||
| 115 | } | ||
| 116 | ); | ||
| 117 | } | ||
| 118 | |||
| 119 | |||
| 120 | sub get_languages { | ||
| 121 | my $self = shift; | ||
| 122 | my $position = shift; | ||
| 123 | return $self->_query_remote_api('available-languages'); | ||
| 124 | } | ||
| 125 | |||
| 126 | sub oneword_available { | ||
| 127 | warn 'deprecated method: oneword_available'; | ||
| 128 | return; | ||
| 129 | } | ||
| 130 | |||
| 131 | sub _query_remote_api { | ||
| 132 | my $self = shift; | ||
| 133 | my $method_name = shift; | ||
| 134 | my $rh_params = shift || {}; | ||
| 135 | |||
| 136 | my $rh_fields = { | ||
| 137 | #a => 1, | ||
| 138 | key => $self->{key}, | ||
| 139 | format => 'json', | ||
| 140 | %$rh_params | ||
| 141 | }; | ||
| 142 | |||
| 143 | foreach my $key (keys %$rh_fields) { | ||
| 144 | delete $rh_fields->{$key} if (!defined($rh_fields->{$key})); | ||
| 145 | } | ||
| 146 | |||
| 147 | my $uri = URI::XS->new($self->{api_endpoint} . $method_name); | ||
| 148 | $uri->query($rh_fields); | ||
| 149 | # if we use URI then need query_form, not query | ||
| 150 | # $uri->query_form($rh_fields); | ||
| 151 | my $url = $uri->as_string; | ||
| 152 | |||
| 153 | $self->_log("GET $url"); | ||
| 154 | my $response = $self->{ua}->get($url); | ||
| 155 | |||
| 156 | if (!$response->{success}) { | ||
| 157 | warn "got failed response from $url: " . $response->{status}; | ||
| 158 | $self->_log("got failed response from $url: " . $response->{status}); | ||
| 159 | return; | ||
| 160 | } | ||
| 161 | |||
| 162 | my $json = $response->{content}; | ||
| 163 | $json = decode_utf8($json); | ||
| 164 | $self->_log($json); | ||
| 165 | |||
| 166 | return $JSONXS->decode($json); | ||
| 167 | } | ||
| 168 | |||
| 169 | sub _log { | ||
| 170 | my $self = shift; | ||
| 171 | my $message = shift; | ||
| 172 | return unless $self->{logging}; | ||
| 173 | |||
| 174 | if (is_coderef($self->{logging})) { | ||
| 175 | my $lc = $self->{logging}; | ||
| 176 | &$lc("Geo::What3Words -- " . $message); | ||
| 177 | } else { | ||
| 178 | print "Geo::What3Words -- " . $message . "\n"; | ||
| 179 | } | ||
| 180 | return; | ||
| 181 | } | ||
| 182 | |||
| 183 | |||
| 184 | 1; | ||
| 185 | |||
| 186 | __END__ | ||
| 187 | |||
| 188 | =pod | ||
| 189 | |||
| 190 | =encoding UTF-8 | ||
| 191 | |||
| 192 | =head1 NAME | ||
| 193 | |||
| 194 | Geo::What3Words - turn WGS84 coordinates into three word addresses and vice-versa using what3words.com HTTPS API | ||
| 195 | |||
| 196 | =head1 VERSION | ||
| 197 | |||
| 198 | version 3.0.1 | ||
| 199 | |||
| 200 | =head1 SYNOPSIS | ||
| 201 | |||
| 202 | my $w3w = Geo::What3Words->new(); | ||
| 203 | |||
| 204 | $w3w->pos2words('51.484463,-0.195405'); | ||
| 205 | # returns 'three.example.words' | ||
| 206 | |||
| 207 | $w3w->pos2words('51.484463,-0.195405', 'ru'); | ||
| 208 | # returns 'три.пример.слова' | ||
| 209 | |||
| 210 | $w3w->words2pos('three.example.words'); | ||
| 211 | # returns '51.484463,-0.195405' (latitude,longitude) | ||
| 212 | |||
| 213 | =head1 DESCRIPTION | ||
| 214 | |||
| 215 | what3words (http://what3words.com/) divides the world into 57 trillion squares | ||
| 216 | of 3 metres x 3 metres. Each square has been given a 3 word address comprised | ||
| 217 | of 3 words from the dictionary. | ||
| 218 | |||
| 219 | This module calls API version 3 (https://docs.what3words.com/public-api/) | ||
| 220 | to convert coordinates into 3 word addresses (forward) and 3 | ||
| 221 | words into coordinates (reverse). | ||
| 222 | |||
| 223 | Versions 1 and 2 are deprecated and are no longer supported. | ||
| 224 | |||
| 225 | You need to sign up at http://what3words.com/login and then register for | ||
| 226 | an API key at https://developer.what3words.com | ||
| 227 | |||
| 228 | =head1 METHODS | ||
| 229 | |||
| 230 | =head2 new | ||
| 231 | |||
| 232 | Creates a new instance. The api key is required. | ||
| 233 | |||
| 234 | my $w3w = Geo::What3Words->new( key => 'your-api-key' ); | ||
| 235 | my $w3w = Geo::What3Words->new( key => 'your-api-key', language => 'ru' ); | ||
| 236 | |||
| 237 | For debugging you can either set logging or provide a callback. | ||
| 238 | |||
| 239 | my $w3w = Geo::What3Words->new( key => 'your-api-key', logging => 1 ); | ||
| 240 | # will print debugging output to STDOUT | ||
| 241 | |||
| 242 | my $callback = sub { my $msg = shift; $my_log4perl_logger->info($msg) }; | ||
| 243 | my $w3w = Geo::What3Words->new( key => 'your-api-key', logging => $callback ); | ||
| 244 | # will log with log4perl. | ||
| 245 | |||
| 246 | =head2 ping | ||
| 247 | |||
| 248 | Check if the remote server is available. This is helpful for debugging or | ||
| 249 | testing, but too slow to run for every conversion. | ||
| 250 | |||
| 251 | $w3w->ping(); | ||
| 252 | |||
| 253 | =head2 words2pos | ||
| 254 | |||
| 255 | Tiny wrapper around words_to_position. | ||
| 256 | |||
| 257 | $w3w->words2pos('three.example.words'); | ||
| 258 | # returns '51.484463,-0.195405' (latitude,longitude) | ||
| 259 | |||
| 260 | $w3w->words2pos('does.not.exist'); | ||
| 261 | # returns undef | ||
| 262 | |||
| 263 | =head2 pos2words | ||
| 264 | |||
| 265 | Tiny wrapper around position_to_words. | ||
| 266 | |||
| 267 | $w3w->pos2words('51.484463,-0.195405'); # latitude,longitude | ||
| 268 | # returns 'three.example.words' | ||
| 269 | |||
| 270 | $w3w->pos2words('51.484463,-0.195405', 'ru'); | ||
| 271 | # returns 'три.пример.слова' | ||
| 272 | |||
| 273 | $w3w->pos2words('invalid,coords'); | ||
| 274 | # returns undef | ||
| 275 | |||
| 276 | =head2 valid_words_format | ||
| 277 | |||
| 278 | Returns 1 if the string looks like three words, 0 otherwise. Does | ||
| 279 | not call the remote API. | ||
| 280 | |||
| 281 | $w3w->valid_words_format('one.two.three'); | ||
| 282 | # returns 1 | ||
| 283 | |||
| 284 | =head2 words_to_position | ||
| 285 | |||
| 286 | Returns a more verbose response than words2pos. | ||
| 287 | |||
| 288 | $w3w->words_to_position('prom.cape.pump'); | ||
| 289 | # { | ||
| 290 | # 'coordinates' => { | ||
| 291 | # 'lat' => '51.484463', | ||
| 292 | # 'lng' => '-0.195405' | ||
| 293 | # }, | ||
| 294 | # 'country' => 'GB', | ||
| 295 | # 'language' => 'en', | ||
| 296 | # 'map' => 'https://w3w.co/prom.cape.pump', | ||
| 297 | # 'nearestPlace' => 'Kensington, London', | ||
| 298 | # 'square' => { | ||
| 299 | # 'northeast' => { | ||
| 300 | # 'lat' => '51.484476', | ||
| 301 | # 'lng' => '-0.195383' | ||
| 302 | # }, | ||
| 303 | # 'southwest' => { | ||
| 304 | # 'lat' => '51.484449', | ||
| 305 | # 'lng' => '-0.195426' | ||
| 306 | # } | ||
| 307 | # }, | ||
| 308 | # 'words' => 'prom.cape.pump' | ||
| 309 | # }; | ||
| 310 | |||
| 311 | =head2 position_to_words | ||
| 312 | |||
| 313 | Returns a more verbose response than pos2words. | ||
| 314 | |||
| 315 | $w3w->position_to_words('51.484463,-0.195405') | ||
| 316 | |||
| 317 | # { | ||
| 318 | # 'coordinates' => { | ||
| 319 | # 'lat' => '51.484463', | ||
| 320 | # 'lng' => '-0.195405' | ||
| 321 | # }, | ||
| 322 | # 'country' => 'GB', | ||
| 323 | # 'language' => 'en', | ||
| 324 | # 'map' => 'https://w3w.co/prom.cape.pump', | ||
| 325 | # 'nearestPlace' => 'Kensington, London', | ||
| 326 | # 'square' => { | ||
| 327 | # 'northeast' => { | ||
| 328 | # 'lat' => '51.484476', | ||
| 329 | # 'lng' => '-0.195383' | ||
| 330 | # }, | ||
| 331 | # 'southwest' => { | ||
| 332 | # 'lat' => '51.484449', | ||
| 333 | # 'lng' => '-0.195426' | ||
| 334 | # } | ||
| 335 | # }, | ||
| 336 | # 'words' => 'prom.cape.pump' | ||
| 337 | # }; | ||
| 338 | |||
| 339 | =head2 get_languages | ||
| 340 | |||
| 341 | Retuns a list of language codes and names. | ||
| 342 | |||
| 343 | $w3w->get_languages(); | ||
| 344 | # { | ||
| 345 | # 'languages' => [ | ||
| 346 | # { | ||
| 347 | # 'name' => 'German', | ||
| 348 | # 'nativeName' => 'Deutsch', | ||
| 349 | # 'code' => 'de' | ||
| 350 | # }, | ||
| 351 | # { | ||
| 352 | # 'name' => 'English', | ||
| 353 | # 'nativeName' => 'English', | ||
| 354 | # 'code' => 'en' | ||
| 355 | # }, | ||
| 356 | # { | ||
| 357 | # 'name' => "Spanish", | ||
| 358 | # 'nativeName' => "Español", | ||
| 359 | # 'code' => 'es' | ||
| 360 | # }, | ||
| 361 | # ... | ||
| 362 | |||
| 363 | =head1 INSTALLATION | ||
| 364 | |||
| 365 | The test suite will use pre-recorded API responses. If you suspect something | ||
| 366 | changed in the API you can force the test suite to use live requests with | ||
| 367 | your API key | ||
| 368 | |||
| 369 | PERLLIB=./lib W3W_RECORD_REQUESTS=1 W3W_API_KEY=<your key> perl t/base.t | ||
| 370 | |||
| 371 | =head1 AUTHOR | ||
| 372 | |||
| 373 | mtmail <mtmail-cpan@gmx.net> | ||
| 374 | |||
| 375 | =head1 COPYRIGHT AND LICENSE | ||
| 376 | |||
| 377 | This software is copyright (c) 2021 by OpenCage GmbH. | ||
| 378 | |||
| 379 | This is free software; you can redistribute it and/or modify it under | ||
| 380 | the same terms as the Perl 5 programming language system itself. | ||
| 381 | |||
| 382 | =cut | ||