File Coverage

blib/lib/RDF/Query/Functions/Geo.pm
Criterion Covered Total %
statement 28 33 84.8
branch 5 8 62.5
condition 2 3 66.6
subroutine 8 8 100.0
pod 1 1 100.0
total 44 53 83.0


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             RDF::Query::Functions::Geo - Geographic extension functions
4              
5             =head1 VERSION
6              
7             This document describes RDF::Query::Functions::Geo version 2.915_01.
8              
9             =head1 DESCRIPTION
10              
11             Defines the following function:
12              
13             =over
14              
15             =item * java:com.ldodds.sparql.Distance
16              
17             =back
18              
19             =cut
20              
21             package RDF::Query::Functions::Geo;
22              
23 35     35   32087 use strict;
  35         92  
  35         968  
24 35     35   196 use warnings;
  35         84  
  35         1155  
25 35     35   199 use Scalar::Util qw(blessed reftype refaddr looks_like_number);
  35         85  
  35         2412  
26 35     35   202 use Log::Log4perl;
  35         80  
  35         341  
27             our ($VERSION, $l);
28             BEGIN {
29 35     35   3227 $l = Log::Log4perl->get_logger("rdf.query.functions.geo");
30 35         18064 $VERSION = '2.915_01';
31             }
32              
33             our $GEO_DISTANCE_LOADED;
34             BEGIN {
35 35     35   93 $GEO_DISTANCE_LOADED = do {
36 35         86 eval {
37 35         28557 require Geo::Distance;
38             };
39 35 50       633536 ($@) ? 0 : 1;
40             };
41             }
42              
43             =begin private
44              
45             =item C<< install >>
46              
47             Documented in L<RDF::Query::Functions>.
48              
49             =end private
50              
51             =cut
52              
53             sub install {
54             RDF::Query::Functions->install_function(
55             "java:com.ldodds.sparql.Distance",
56             sub {
57             # http://xmlarmyknife.com/blog/archives/000281.html
58 17     17   28 my $query = shift;
59 17         21 my ($lat1, $lon1, $lat2, $lon2);
60            
61 17 50       42 unless ($GEO_DISTANCE_LOADED) {
62 0         0 throw RDF::Query::Error::FilterEvaluationError ( -text => "Cannot compute distance because Geo::Distance is not available" );
63             }
64            
65             my $geo = ref($query)
66 17 100 66     118 ? ($query->{_query_cache}{'java:com.ldodds.sparql.Distance'}{_geo_dist_obj} ||= new Geo::Distance)
67             : new Geo::Distance;
68 17 50       1215 if (2 == @_) {
69 0         0 my ($point1, $point2) = map { $_->literal_value } splice(@_,0,2);
  0         0  
70 0         0 ($lat1, $lon1) = split(/ /, $point1);
71 0         0 ($lat2, $lon2) = split(/ /, $point2);
72             } else {
73 17         39 ($lat1, $lon1, $lat2, $lon2) = map { $_->literal_value } splice(@_,0,4);
  68         356  
74             }
75            
76 17         144 my $dist = $geo->distance(
77             'kilometer',
78             $lon1,
79             $lat1,
80             $lon2,
81             $lat2,
82             );
83             # warn "ldodds:Distance => $dist\n";
84 17         2923 return RDF::Query::Node::Literal->new("$dist", undef, 'http://www.w3.org/2001/XMLSchema#float');
85             }
86 35     35 1 467 );
87             }
88              
89             1;
90              
91             __END__
92              
93             =head1 AUTHOR
94              
95             Gregory Williams <gwilliams@cpan.org>.
96              
97             =cut