From 3f8ed8a88b91d71db8feb41bc8b26154899fa11f Mon Sep 17 00:00:00 2001 From: Llewellyn Marshall Date: Thu, 14 Jul 2022 15:15:58 -0400 Subject: [PATCH] add Geo::Coder::Bing to geosort application module --- Open-ILS/src/extras/install/Makefile.debian-buster | 1 + Open-ILS/src/extras/install/Makefile.debian-jessie | 1 + .../src/extras/install/Makefile.debian-stretch | 1 + Open-ILS/src/extras/install/Makefile.fedora | 1 + Open-ILS/src/extras/install/Makefile.ubuntu-bionic | 1 + Open-ILS/src/extras/install/Makefile.ubuntu-focal | 1 + .../src/perlmods/lib/OpenILS/Application/Geo.pm | 55 ++++++++++++++-------- 7 files changed, 41 insertions(+), 20 deletions(-) diff --git a/Open-ILS/src/extras/install/Makefile.debian-buster b/Open-ILS/src/extras/install/Makefile.debian-buster index 5c8ff0816b..0c1f29b105 100644 --- a/Open-ILS/src/extras/install/Makefile.debian-buster +++ b/Open-ILS/src/extras/install/Makefile.debian-buster @@ -97,6 +97,7 @@ export DEB_APACHE_DISCONF = \ export CPAN_MODULES = \ Geo::Coder::Google \ + Geo::Coder::Bing \ Business::OnlinePayment::PayPal \ String::KeyboardDistance \ Text::Levenshtein::Damerau::XS \ diff --git a/Open-ILS/src/extras/install/Makefile.debian-jessie b/Open-ILS/src/extras/install/Makefile.debian-jessie index 545a1a516f..346d779cd3 100644 --- a/Open-ILS/src/extras/install/Makefile.debian-jessie +++ b/Open-ILS/src/extras/install/Makefile.debian-jessie @@ -97,6 +97,7 @@ export DEB_APACHE_DISCONF = \ export CPAN_MODULES = \ Geo::Coder::Google \ + Geo::Coder::Bing \ Business::OnlinePayment::PayPal \ String::KeyboardDistance \ Text::Levenshtein::Damerau::XS \ diff --git a/Open-ILS/src/extras/install/Makefile.debian-stretch b/Open-ILS/src/extras/install/Makefile.debian-stretch index 48ba5a6a14..37a427cd23 100644 --- a/Open-ILS/src/extras/install/Makefile.debian-stretch +++ b/Open-ILS/src/extras/install/Makefile.debian-stretch @@ -97,6 +97,7 @@ export DEB_APACHE_DISCONF = \ export CPAN_MODULES = \ Geo::Coder::Google \ + Geo::Coder::Bing \ Business::OnlinePayment::PayPal \ String::KeyboardDistance \ Text::Levenshtein::Damerau::XS \ diff --git a/Open-ILS/src/extras/install/Makefile.fedora b/Open-ILS/src/extras/install/Makefile.fedora index 907bd99b85..2d1bb9900b 100644 --- a/Open-ILS/src/extras/install/Makefile.fedora +++ b/Open-ILS/src/extras/install/Makefile.fedora @@ -74,6 +74,7 @@ FEDORA_RPMS = \ export CPAN_MODULES = \ Geo::Coder::OSM \ Geo::Coder::Google \ + Geo::Coder::Bing \ Excel::Writer::XLSX \ String::KeyboardDistance \ Text::Levenshtein::Damerau::XS \ diff --git a/Open-ILS/src/extras/install/Makefile.ubuntu-bionic b/Open-ILS/src/extras/install/Makefile.ubuntu-bionic index 6a6476528e..f0231a7425 100644 --- a/Open-ILS/src/extras/install/Makefile.ubuntu-bionic +++ b/Open-ILS/src/extras/install/Makefile.ubuntu-bionic @@ -93,6 +93,7 @@ export DEB_APACHE_DISCONF = \ export CPAN_MODULES = \ Geo::Coder::Google \ + Geo::Coder::Bing \ Business::OnlinePayment::PayPal \ Email::Send \ MARC::Charset \ diff --git a/Open-ILS/src/extras/install/Makefile.ubuntu-focal b/Open-ILS/src/extras/install/Makefile.ubuntu-focal index 9ebe327fab..df5ed763fd 100644 --- a/Open-ILS/src/extras/install/Makefile.ubuntu-focal +++ b/Open-ILS/src/extras/install/Makefile.ubuntu-focal @@ -93,6 +93,7 @@ export DEB_APACHE_DISCONF = \ export CPAN_MODULES = \ Geo::Coder::Google \ + Geo::Coder::Bing \ Business::OnlinePayment::PayPal \ Email::Send \ MARC::Charset \ diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Application/Geo.pm b/Open-ILS/src/perlmods/lib/OpenILS/Application/Geo.pm index 3f735e5547..763733b115 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/Application/Geo.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/Application/Geo.pm @@ -23,6 +23,7 @@ my $have_geocoder_free = eval { }; use Geo::Coder::OSM; use Geo::Coder::Google; +use Geo::Coder::Bing; use Math::Trig qw(great_circle_distance deg2rad); use Digest::SHA qw(sha256_base64); @@ -131,6 +132,35 @@ __PACKAGE__->register_method( } ); +# creates a Geo::Coder object +sub _create_geocoder { + my $service = shift; + my $service_id = $service->id; + my $geo_coder; + eval { + if ($service->service_code eq 'Free') { + if ($have_geocoder_free) { + $logger->debug("Using Geo::Coder::Free (service id $service_id)"); + $geo_coder = Geo::Coder::Free->new(); + } else { + $logger->error("geosort: Geo::Coder::Free not installed but referenced."); + } + } elsif ($service->service_code eq 'Google') { + $logger->debug("Using Geo::Coder::Google (service id $service_id)"); + $geo_coder = Geo::Coder::Google->new(key => $service->api_key); + } elsif ($service->service_code eq 'Bing') { + $logger->debug("Using Geo::Coder::Bing (service id $service_id)"); + $geo_coder = Geo::Coder::Bing->new(key => $service->api_key); + } else { + $logger->debug("Using Geo::Coder::OSM (service id $service_id)"); + $geo_coder = Geo::Coder::OSM->new(); + } + }; + if ($@ || !$geo_coder) { + $logger->error("geosort: problem creating Geo::Coder instance : $@"); + } + return $geo_coder; +} sub retrieve_coordinates { # invoke 3rd party API for latitude/longitude lookup my ($self, $conn, $org, $address) = @_; @@ -165,26 +195,8 @@ sub retrieve_coordinates { # invoke 3rd party API for latitude/longitude lookup my $coords = OpenSRF::Utils::JSON->JSON2perl($cache->get_cache($cache_key)); return $coords if $coords; - my $geo_coder; - eval { - if ($service->service_code eq 'Free') { - if ($have_geocoder_free) { - $logger->debug("Using Geo::Coder::Free (service id $service_id)"); - $geo_coder = Geo::Coder::Free->new(); - } else { - $logger->error("geosort: Geo::Coder::Free not installed but referenced."); - return OpenILS::Event->new('GEOCODING_LOCATION_NOT_FOUND'); - } - } elsif ($service->service_code eq 'Google') { - $logger->debug("Using Geo::Coder::Google (service id $service_id)"); - $geo_coder = Geo::Coder::Google->new(key => $service->api_key); - } else { - $logger->debug("Using Geo::Coder::OSM (service id $service_id)"); - $geo_coder = Geo::Coder::OSM->new(); - } - }; - if ($@ || !$geo_coder) { - $logger->error("geosort: problem creating Geo::Coder instance : $@"); + my $geo_coder = _create_geocoder($service); + if (!$geo_coder) { return OpenILS::Event->new('GEOCODING_LOCATION_NOT_FOUND'); } my $location; @@ -204,6 +216,9 @@ sub retrieve_coordinates { # invoke 3rd party API for latitude/longitude lookup } elsif ($service->service_code eq 'Google') { $latitude = $location->{'geometry'}->{'location'}->{'lat'}; $longitude = $location->{'geometry'}->{'location'}->{'lng'}; + } elsif ($service->service_code eq 'Bing') { + $latitude = $location->{point}{coordinates}[0]; + $longitude = $location->{point}{coordinates}[1]; } else { $latitude = $location->{lat}; $longitude = $location->{lon}; -- 2.11.0