1 package OpenILS::Application::Geo;
6 use OpenSRF::AppSession;
7 use OpenILS::Application;
8 use base qw/OpenILS::Application/;
10 use OpenSRF::Utils::SettingsClient;
11 use OpenILS::Utils::CStoreEditor qw/:funcs/;
12 use OpenILS::Utils::Fieldmapper;
13 use OpenSRF::Utils::Cache;
14 use OpenILS::Application::AppUtils;
15 my $U = "OpenILS::Application::AppUtils";
17 use OpenSRF::Utils::Logger qw/$logger/;
19 my $have_geocoder_free = eval {
20 require Geo::Coder::Free;
21 Geo::Coder::Free->import();
25 use Geo::Coder::Google;
27 use Math::Trig qw(great_circle_distance deg2rad);
28 use Digest::SHA qw(sha256_base64);
34 my $conf = OpenSRF::Utils::SettingsClient->new;
36 $cache_timeout = $conf->config_value(
37 "apps", "open-ils.geo", "app_settings", "cache_timeout" ) || 300;
40 $cache = OpenSRF::Utils::Cache->new('global');
43 sub calculate_distance {
44 my ($self, $conn, $pointA, $pointB) = @_;
46 return new OpenILS::Event("BAD_PARAMS", "desc" => "Missing coordinates") unless $pointA;
47 return new OpenILS::Event("BAD_PARAMS", "desc" => "Missing coordinates") unless $pointB;
48 return new OpenILS::Event("BAD_PARAMS", "desc" => "Malformed coordinates") unless scalar(@{ $pointA }) == 2;
49 return new OpenILS::Event("BAD_PARAMS", "desc" => "Malformed coordinates") unless scalar(@{ $pointB }) == 2;
51 sub NESW { deg2rad($_[1]), deg2rad(90 - $_[0]) } # longitude, latitude
52 my @A = NESW( $pointA->[0], $pointA->[1] );
53 my @B = NESW( $pointB->[0], $pointB->[1] );
54 my $km = great_circle_distance(@A, @B, 6378);
58 __PACKAGE__->register_method(
59 method => "calculate_distance",
60 api_name => "open-ils.geo.calculate_distance",
63 {type => 'array', desc => 'An array containing latitude and longitude for point A'},
64 {type => 'array', desc => 'An array containing latitude and longitude for point B'}
66 return => { desc => '"Great Circle (as the crow flies)" distance between points A and B in kilometers'}
70 sub sort_orgs_by_distance_from_coordinate {
71 my ($self, $conn, $pointA, $orgs) = @_;
73 return new OpenILS::Event("BAD_PARAMS", "desc" => "Missing coordinates") unless $pointA;
74 return new OpenILS::Event("BAD_PARAMS", "desc" => "Malformed coordinates") unless scalar(@{ $pointA }) == 2;
75 return new OpenILS::Event("BAD_PARAMS", "desc" => "Missing org list") unless $orgs;
76 return new OpenILS::Event("BAD_PARAMS", "desc" => "Empty org list") unless scalar(@{ $orgs }) > 0;
78 my $e = new_editor(xact => 1);
80 my $fleshed_orgs = $e->search_actor_org_unit([
85 "flesh_fields" => {"aou" => ["billing_address"]}
87 ]) or return (undef, $e->die_event);
89 my @orgs_with_coordinates = grep {
90 defined $_->billing_address
91 && defined $_->billing_address->latitude
92 && defined $_->billing_address->longitude } @$fleshed_orgs;
93 my @orgs_without_coordinates = grep {
94 !defined $_->billing_address
95 || !defined $_->billing_address->latitude
96 || !defined $_->billing_address->longitude } @$fleshed_orgs;
98 my @org_ids_with_distances = map {
99 [ $_->id, calculate_distance($self, $conn, $pointA, [
100 $_->billing_address->latitude,
101 $_->billing_address->longitude
103 } @orgs_with_coordinates;
105 my @sorted_orgs = sort { $a->[1] <=> $b->[1] } @org_ids_with_distances;
106 push @sorted_orgs, map { [ $_->id, -1 ] } sort { $a->name cmp $b->name } @orgs_without_coordinates;
107 my @sorted_org_ids = map { $_->[0] } @sorted_orgs;
109 return $self->api_name =~ /include_distances/ ? \@sorted_orgs : \@sorted_org_ids;
111 __PACKAGE__->register_method(
112 method => "sort_orgs_by_distance_from_coordinate",
113 api_name => "open-ils.geo.sort_orgs_by_distance_from_coordinate",
116 {type => 'array', desc => 'An array containing latitude and longitude for the reference point'},
117 {type => 'array', desc => 'An array of Context Organizational Unit IDs'}
119 return => { desc => 'An array of Context Organizational Unit IDs sorted by geographic proximity to the reference point (closest first). Units without coordinates are appended to the end of the list in alphabetical order by name relative to each other.'}
122 __PACKAGE__->register_method(
123 method => "sort_orgs_by_distance_from_coordinate",
124 api_name => "open-ils.geo.sort_orgs_by_distance_from_coordinate.include_distances",
127 {type => 'array', desc => 'An array containing latitude and longitude for the reference point'},
128 {type => 'array', desc => 'An array of Context Organizational Unit IDs'}
130 return => { desc => 'An array of Context Organizational Unit IDs and distances (each pair itself an array) sorted by geographic proximity to the reference point (closest first). Units without coordinates are appended to the end of the list in alphabetical order by name relative to each other and given a distance of -1.'}
135 sub retrieve_coordinates { # invoke 3rd party API for latitude/longitude lookup
136 my ($self, $conn, $org, $address) = @_;
138 my $e = new_editor(xact => 1);
139 # TODO: if we're not going to require authentication, we may want to consider
140 # implementing some options for limiting outgoing geo-coding API calls
141 # return $e->die_event unless $e->checkauth;
143 $org = ref($org) ? $org->id : $org; # never trust the caller :-)
145 my $use_geo = $e->retrieve_config_global_flag('opac.use_geolocation');
146 $use_geo = ($use_geo and $U->is_true($use_geo->enabled));
147 return new OpenILS::Event("GEOCODING_NOT_ENABLED") unless ($U->is_true($use_geo));
149 return new OpenILS::Event("BAD_PARAMS", "desc" => "No org ID supplied") unless $org;
150 my $service_id = $U->ou_ancestor_setting_value($org, 'opac.geographic_location_service_for_address');
151 return new OpenILS::Event("GEOCODING_NOT_ALLOWED") unless ($U->is_true($service_id));
153 my $service = $e->retrieve_config_geolocation_service($service_id);
154 return new OpenILS::Event("GEOCODING_NOT_ALLOWED") unless ($U->is_true($service));
156 $address =~ s/^\s+//;
157 $address =~ s/\s+$//;
158 return new OpenILS::Event("BAD_PARAMS", "desc" => "No address supplied") unless $address;
160 # Return cached coordinates if available. We're assuming that any
161 # geolocation service will give roughly equivalent results, so we're
162 # using a hash of the user-supplied address as the cache key, not
164 my $cache_key = 'geo.address.' . sha256_base64($address);
165 my $coords = OpenSRF::Utils::JSON->JSON2perl($cache->get_cache($cache_key));
166 return $coords if $coords;
170 if ($service->service_code eq 'Free') {
171 if ($have_geocoder_free) {
172 $logger->debug("Using Geo::Coder::Free (service id $service_id)");
173 $geo_coder = Geo::Coder::Free->new();
175 $logger->error("geosort: Geo::Coder::Free not installed but referenced.");
176 return OpenILS::Event->new('GEOCODING_LOCATION_NOT_FOUND');
178 } elsif ($service->service_code eq 'Google') {
179 $logger->debug("Using Geo::Coder::Google (service id $service_id)");
180 $geo_coder = Geo::Coder::Google->new(key => $service->api_key);
182 $logger->debug("Using Geo::Coder::OSM (service id $service_id)");
183 $geo_coder = Geo::Coder::OSM->new();
186 if ($@ || !$geo_coder) {
187 $logger->error("geosort: problem creating Geo::Coder instance : $@");
188 return OpenILS::Event->new('GEOCODING_LOCATION_NOT_FOUND');
192 $location = $geo_coder->geocode(location => $address);
195 $logger->error("geosort: problem invoking location lookup : $@");
196 return OpenILS::Event->new('GEOCODING_LOCATION_NOT_FOUND');
199 my $latitude; my $longitude;
200 return new OpenILS::Event("GEOCODING_LOCATION_NOT_FOUND") unless ($U->is_true($location));
201 if ($service->service_code eq 'Free') {
202 $latitude = $location->{'latitude'};
203 $longitude = $location->{'longitude'};
204 } elsif ($service->service_code eq 'Google') {
205 $latitude = $location->{'geometry'}->{'location'}->{'lat'};
206 $longitude = $location->{'geometry'}->{'location'}->{'lng'};
208 $latitude = $location->{lat};
209 $longitude = $location->{lon};
211 $coords = { latitude => $latitude, longitude => $longitude };
212 $cache->put_cache($cache_key, OpenSRF::Utils::JSON->perl2JSON($coords), $cache_timeout);
216 __PACKAGE__->register_method(
217 method => "retrieve_coordinates",
218 api_name => "open-ils.geo.retrieve_coordinates",
221 {type => 'number', desc => 'Context Organizational Unit'},
222 {type => 'string', desc => 'Address to look-up as a text string'}
224 return => { desc => 'Hash/object containing latitude and longitude for the provided address.'}