lib/Geo/LibProj/cs2cs.pm

filter: Statementstmt 100% 153 / 153 Branchbran 94.4% 85 / 90 Conditioncond 97.3% 36 / 37 MC/DCmcdc 95.4% 21 / 22 Subroutinesub 100% 20 / 20 Podpod 100% 4 / 4 totaltotal 97.8% 319 / 326 CC 65 SCAR 21.8
CC
65
Cov
98%
_ffi_init
15.0
transform
15.0
_special_params
10.0
CRAP 8.8
↑ Summary
1 5 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Geo::LibProj::cs2cs;
6 # ABSTRACT: IPC interface to PROJ cs2cs
7 5 $Geo::LibProj::cs2cs::VERSION = '1.03';
8  
9 5 ▶use Carp qw(carp croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use File::Basename qw(basename);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use Scalar::Util 1.10 qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 5 ▶use IPC::Run3 qw(run3);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16  
17 5 our $CMD = 'cs2cs';
18 5 our @PATH = ();
19 5 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.6
20 # optional modules
21 5 ▶ eval "require Alien::proj" or eval "require Alien::Proj4";
Branch: 1 branch
BranchTrueFalseTotal
eval "require Alien::proj" or eval "require Alien::Proj4" 0 5 5
22 }
23 5 eval { unshift @PATH, File::Spec->catdir(Alien::proj->dist_dir, 'bin') };
24 5 eval { push @PATH, undef, File::Spec->catdir(Alien::Proj4->dist_dir, 'bin') };
25  
26  
27 # default stringification formats for cs2cs stdin and stdout
28 5 our $FORMAT_IN = '%.15g';
29 5 our $FORMAT_OUT = '%.12g';
30  
31 5 our %PARAMS = (
32 -f => $FORMAT_OUT,
33 );
34  
35  
36 12 ▶sub new {
Subroutine new: called CC=7 SCAR=19.5
Pod documented
37 12 my $class = shift;
38
39 12 my ($source_crs, $target_crs, $user_params);
40 12 ▶ if ( ref($_[0]) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 1 11 12
41 1 ($user_params, $source_crs, $target_crs) = @_;
42 }
43 else {
44 11 ($source_crs, $target_crs, $user_params) = @_;
45 }
46
47 12 my $self = bless {}, $class;
48
49 12 ▶ my $params = { %PARAMS, defined $user_params ? %$user_params : () };
Branch: 1 branch
BranchTrueFalseTotal
defined $user_params ? : 9 3 12
50 12 $self->_special_params($params);
51 9 $self->{format_in} = $FORMAT_IN;
52
53 # assemble cs2cs call line
54 9 for my $key (keys %$params) {
55 12 ▶ delete $params->{$key} unless defined $params->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params->{$key} 3 9 12
56 }
57 9 ▶ my @source_crs = split m/ /, $source_crs // 'undef';
Condition100%2/2
$source_crs // "undef"
l!l
81
MC/DC100%1/1
$source_crs // "undef"
$source_crs
Truth table100%2/2
A
A= $source_crs
Aresult
11
00
58 9 ▶ my @target_crs = split m/ /, $target_crs // 'undef';
Condition100%2/2
$target_crs // "undef"
l!l
81
MC/DC100%1/1
$target_crs // "undef"
$target_crs
Truth table100%2/2
A
A= $target_crs
Aresult
11
00
59 9 $self->{cmd} = $self->_cmd();
60 9 $self->{call} = [$self->{cmd}, %$params, @source_crs, '+to', @target_crs, '-'];
61
62 9 $self->_ffi_init($source_crs, $target_crs, $params);
63
64 9 return $self;
65 }
66  
67  
68 15 ▶sub _special_params {
Subroutine _special_params: called CC=10 SCAR=23.0
69 15 my ($self, $params) = @_;
70
71 # support -d even for older cs2cs versions
72 15 ▶ if (defined $params->{-d} && defined $params->{-f}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params->{"-d"} and defined $params->{"-f"}) 1 14 15
Condition100%3/3
defined $params->{"-d"} and defined $params->{"-f"}
!ll&&!rl&&r
1311
MC/DC100%2/2
defined $params->{"-d"} and defined $params->{"-f"}
defined $params->{"-d"} defined $params->{"-f"}
Truth table100%3/3
A and B
A= defined $params->{"-d"}B= defined $params->{"-f"}
ABresult
0X0
100
111
73 1 $params->{-f} = '%.' . (0 + $params->{-d}) . 'f';
74 1 delete $params->{-d};
75 }
76
77 15 ▶ croak "-E is unsupported" if defined $params->{'-E'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params->{"-E"} 1 14 15
78 14 ▶ croak "-t is unsupported" if defined $params->{'-t'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params->{"-t"} 1 13 14
79 13 ▶ croak "-v is unsupported" if defined $params->{'-v'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params->{"-v"} 1 12 13
80
81 # -w3 must be supplied as a single parameter to cs2cs
82 12 ▶ if (defined $params->{-w}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params->{"-w"}) 1 11 12
83 1 $params->{"-w$params->{-w}"} = '';
84 1 delete $params->{-w};
85 }
86 12 ▶ if (defined $params->{-W}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params->{"-W"}) 1 11 12
87 1 $params->{"-W$params->{-W}"} = '';
88 1 delete $params->{-W};
89 }
90
91 $self->{ffi} = $INC{'Geo/LibProj/FFI.pm'}
92 12 ▶ && ( $params->{XS} || ! defined $params->{XS} );
Condition100%6/6
$INC{'Geo/LibProj/FFI.pm'} && ($params->{'XS'} || !defined($params->{'XS'}))
!ll&&!rl&&r
912
$params->{"XS"} or not defined $params->{"XS"}
l!l&&r!l&&!r
111
MC/DC100%3/3
$INC{'Geo/LibProj/FFI.pm'} && ($params->{'XS'} || !defined($params->{'XS'}))
$INC{'Geo/LibProj/FFI.pm'} $params->{"XS"} not defined $params->{"XS"}
Truth table100%4/4
A && B or not(C)
A= $INC{'Geo/LibProj/FFI.pm'}B= $params->{"XS"}C= not defined $params->{"XS"}
ABCresult
0XX0
1000
11X1
1011
93 12 $self->{ffi_warn} = $params->{XS};
94 12 delete $params->{XS};
95 }
96  
97  
98 16 ▶sub _cmd {
Subroutine _cmd: called CC=6 SCAR=17.9
99 # try to find the cs2cs binary
100 16 foreach my $path (@PATH) {
101 5 ▶ if (defined $path) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $path) { } 3 2 5
102 3 my $cmd = File::Spec->catfile($path, $CMD);
103 3 ▶ return $cmd if -e $cmd;
Branch: 1 branch
BranchTrueFalseTotal
if -e $cmd 1 2 3
104 }
105 else {
106 # when the @PATH element is undefined, try the env PATH
107 2 eval { run3 [$CMD, '-lp'], \undef, \undef, \undef };
108 2 ▶ return $CMD if ! $@ && $? == 0;
Branch: 1 branch
BranchTrueFalseTotal
if not $@ and $? == 0 1 1 2
Condition66%2/3
not $@ and $? == 0
!ll&&!rl&&r
011
MC/DC50%1/2
not $@ and $? == 0
not $@ $? == 0
Truth table66%2/3
not(A) and B
A= not $@B= $? == 0
ABresult
0X0
100
111
109 }
110 }
111
112 # no luck; let's just hope it'll be on the PATH somewhere
113 14 return $CMD;
114 }
115  
116  
117 25 ▶sub _ffi_init {
Subroutine _ffi_init: called CC=15 SCAR=27.1
118 25 my ($self, $source_crs, $target_crs, $params) = @_;
119
120 25 ▶ carp "Geo::LibProj::FFI is not loaded; falling back to IPC mode" if $self->{ffi_warn} && ! $self->{ffi};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ffi_warn"} and not $self->{"ffi"} 1 24 25
Condition100%3/3
$self->{"ffi_warn"} and not $self->{"ffi"}
!ll&&!rl&&r
1771
MC/DC100%2/2
$self->{"ffi_warn"} and not $self->{"ffi"}
$self->{"ffi_warn"} not $self->{"ffi"}
Truth table100%3/3
A and not(B)
A= $self->{"ffi_warn"}B= not $self->{"ffi"}
ABresult
0X0
100
111
121 25 ▶ return unless $self->{ffi};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"ffi"} 11 14 25
122
123 my @params = grep {
124 14 $_ eq '-f'
125 ? defined $params->{$_} && $params->{$_} ne $FORMAT_OUT
126 8 ▶ : defined $params->{$_}
Branch: 1 branch
BranchTrueFalseTotal
$_ eq '-f' ? : 6 2 8
Condition100%3/3
defined $params->{$_} and $params->{$_} ne $Geo::LibProj::cs2cs::FORMAT_OUT
!ll&&!rl&&r
222
MC/DC100%2/2
defined $params->{$_} and $params->{$_} ne $Geo::LibProj::cs2cs::FORMAT_OUT
defined $params->{$_} $params->{$_} ne $Geo::LibProj::cs2cs::FORMAT_OUT
Truth table100%3/3
A and B
A= defined $params->{$_}B= $params->{$_} ne $Geo::LibProj::cs2cs::FORMAT_OUT
ABresult
0X0
100
111
127 } keys %$params;
128 14 ▶ carp "cs2cs control parameters are unsupported in XS mode; falling back to IPC mode" if $self->{ffi_warn} && @params;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ffi_warn"} and @params 2 12 14
Condition100%3/3
$self->{"ffi_warn"} and @params
!ll&&!rl&&r
752
MC/DC100%2/2
$self->{"ffi_warn"} and @params
$self->{"ffi_warn"} @params
Truth table100%3/3
A and B
A= $self->{"ffi_warn"}B= @params
ABresult
0X0
100
111
129 14 ▶ return $self->{ffi} = 0 if @params;
Branch: 1 branch
BranchTrueFalseTotal
if @params 4 10 14
130
131 10 my $ctx = $self->{ffi_ctx} = Geo::LibProj::FFI::proj_context_create();
132 10 ▶ carp "proj_context_create() failed; falling back to IPC mode" if $self->{ffi_warn} && ! $ctx;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ffi_warn"} and not $ctx 1 9 10
Condition100%3/3
$self->{"ffi_warn"} and not $ctx
!ll&&!rl&&r
541
MC/DC100%2/2
$self->{"ffi_warn"} and not $ctx
$self->{"ffi_warn"} not $ctx
Truth table100%3/3
A and not(B)
A= $self->{"ffi_warn"}B= not $ctx
ABresult
0X0
100
111
133 10 ▶ return $self->{ffi} = 0 if ! $ctx;
Branch: 1 branch
BranchTrueFalseTotal
unless $ctx 2 8 10
134
135 8 Geo::LibProj::FFI::proj_context_use_proj4_init_rules($ctx, 1);
136
137 8 my $pj = $self->{ffi_pj} = Geo::LibProj::FFI::proj_create_crs_to_crs(
138 $ctx, $source_crs, $target_crs, undef );
139 8 ▶ carp "proj_create_crs_to_crs() failed; falling back to IPC mode" if $self->{ffi_warn} && ! $pj;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ffi_warn"} and not $pj 1 7 8
Condition100%3/3
$self->{"ffi_warn"} and not $pj
!ll&&!rl&&r
431
MC/DC100%2/2
$self->{"ffi_warn"} and not $pj
$self->{"ffi_warn"} not $pj
Truth table100%3/3
A and not(B)
A= $self->{"ffi_warn"}B= not $pj
ABresult
0X0
100
111
140 8 ▶ return $self->{ffi} = 0 if ! $pj;
Branch: 1 branch
BranchTrueFalseTotal
if not $pj 2 6 8
141 }
142  
143  
144 15 ▶sub DESTROY {
Subroutine DESTROY: called CC=3 SCAR=11.0
145 15 my ($self) = @_;
146
147 15 ▶ Geo::LibProj::FFI::proj_destroy($self->{ffi_pj}) if $self->{ffi_pj};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ffi_pj"} 1 14 15
148 15 ▶ Geo::LibProj::FFI::proj_context_destroy($self->{ffi_ctx}) if $self->{ffi_ctx};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ffi_ctx"} 1 14 15
149 15 $self->{ffi_pj} = $self->{ffi_ctx} = 0;
150 }
151  
152  
153 10 ▶sub _ipc_error_check {
Subroutine _ipc_error_check: called CC=7 SCAR=19.6
154 10 my ($self, $eval_err, $os_err, $code, $stderr) = @_;
155
156 10 my $cmd = $CMD;
157 10 ▶ if (ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) 7 3 10
158 7 $self->{stderr} = $stderr;
159 7 $self->{status} = $code >> 8;
160 }
161
162 10 ▶ $stderr =~ s/^(.*\S)\s*\z/: $1/s if length $stderr;
Branch: 1 branch
BranchTrueFalseTotal
if length $stderr 0 10 10
163 10 ▶ croak "`$cmd` failed to execute: $os_err" if $code == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $code == -1 1 9 10
164 9 ▶ croak "`$cmd` died with signal " . ($code & 0x7f) . $stderr if $code & 0x7f;
Branch: 1 branch
BranchTrueFalseTotal
if $code & 127 0 9 9
165 9 ▶ croak "`$cmd` exited with status " . ($code >> 8) . $stderr if $code;
Branch: 1 branch
BranchTrueFalseTotal
if $code 1 8 9
166 8 ▶ croak $eval_err =~ s/\s+\z//r if $eval_err;
Branch: 1 branch
BranchTrueFalseTotal
if $eval_err 0 8 8
167 }
168  
169  
170 27 ▶sub _format {
Subroutine _format: called CC=2 SCAR=6.9
171 27 my ($self, $value) = @_;
172
173 27 ▶ return sprintf $self->{format_in}, $value if looks_like_number $value;
Branch: 1 branch
BranchTrueFalseTotal
if &looks_like_number($value) 25 2 27
174 2 return $value;
175 }
176  
177  
178 10 ▶sub transform {
Subroutine transform: called CC=15 SCAR=27.1
Pod documented
179 10 my ($self, @source_points) = @_;
180
181 10 ▶ return $self->_ffi_transform(@source_points) if $self->{ffi};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ffi"} 3 7 10
182
183 7 my @in = ();
184 7 foreach my $i (0 .. $#source_points) {
185 9 my $p = $source_points[$i];
186 9 ▶ push @in, $self->_format($p->[0] || 0) . " "
Condition100%6/6
$p->[0] || 0
l!l
36
$p->[1] || 0
l!l
36
$p->[2] || 0
l!l
27
MC/DC100%1/1
$p->[0] || 0
$p->[0]
MC/DC100%1/1
$p->[1] || 0
$p->[1]
MC/DC100%1/1
$p->[2] || 0
$p->[2]
Truth table100%2/2
A
A= $p->[0]
Aresult
11
00
Truth table100%2/2
A
A= $p->[1]
Aresult
11
00
Truth table100%2/2
A
A= $p->[2]
Aresult
11
00
187 . $self->_format($p->[1] || 0) . " "
188 . $self->_format($p->[2] || 0) . " $i";
189 }
190 7 my $in = join "\n", @in;
191
192 7 my @out = ();
193 7 my $err = '';
194 7 eval {
195 7 local $/ = "\n";
196 7 run3 $self->{call}, \$in, \@out, \$err;
197 };
198 7 $self->_ipc_error_check($@, $!, $?, $err);
199
200 7 my @target_points = ();
201 7 foreach my $line (@out) {
202 8 ▶ next unless $line =~ m{\s(\d+)\s*$}xa;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /\s(\d+)\s*$/ax 1 7 8
203 7 my $aux = $source_points[$1]->[3];
204 7 ▶ next unless $line =~ m{^\s* (\S+) \s+ (\S+) \s+ (\S+) \s}xa;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /^\s* (\S+) \s+ (\S+) \s+ (\S+) \s/ax 1 6 7
205 6 ▶ my @p = defined $aux ? ($1, $2, $3, $aux) : ($1, $2, $3);
Branch: 1 branch
BranchTrueFalseTotal
defined $aux ? : 1 5 6
206
207 6 foreach my $j (0..2) {
208 18 ▶ $p[$j] = 0 + $p[$j] if looks_like_number $p[$j];
Branch: 1 branch
BranchTrueFalseTotal
if &looks_like_number($p[$j]) 16 2 18
209 }
210
211 6 push @target_points, \@p;
212 }
213
214 7 ▶ if ( (my $s = @source_points) != (my $t = @target_points) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $s = @source_points) != (my $t = @target_points)) 3 4 7
215 3 croak "Source/target point count doesn't match ($s/$t): Assertion failed";
216 }
217
218 4 ▶ return @target_points if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 1 3 4
219 3 ▶ return $target_points[0] if @target_points < 2;
Branch: 1 branch
BranchTrueFalseTotal
if @target_points < 2 2 1 3
220 1 croak "transform() with list argument prohibited in scalar context";
221 }
222  
223  
224 3 ▶sub _ffi_transform {
Subroutine _ffi_transform: called CC=4 SCAR=13.9
225 3 my ($self, @source_points) = @_;
226
227 my @target_points = map {
228 3 my $p = Geo::LibProj::FFI::_trans( $self->{ffi_pj}, 1, [$_->[0], $_->[1], $_->[2], 'Inf'] );
229 7 $p->[3] = $_->[3];
230 7 ▶ delete $p->[3] unless defined $p->[3];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $p->[3] 2 5 7
231 7 $p;
232 } @source_points;
233
234 3 ▶ return @target_points if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 1 2 3
235 2 ▶ return $target_points[0] if @target_points < 2;
Branch: 1 branch
BranchTrueFalseTotal
if @target_points < 2 1 1 2
236 1 croak "transform() with list argument prohibited in scalar context";
237 }
238  
239  
240 4 ▶sub version {
Subroutine version: called CC=4 SCAR=13.9
Pod documented
241 4 my ($self) = @_;
242
243 4 ▶ my $ffi = ref $self ? $self->{ffi} : $INC{'Geo/LibProj/FFI.pm'};
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 1 3 4
244 4 ▶ return Geo::LibProj::FFI::proj_info()->version if $ffi;
Branch: 1 branch
BranchTrueFalseTotal
if $ffi 1 3 4
245
246 3 my $out = '';
247 3 eval {
248 3 run3 [ $self->_cmd ], \undef, \$out, \$out;
249 };
250 3 $self->_ipc_error_check($@, $!, $?, '');
251
252 1 ▶ return $1 if $out =~ m/\b(\d+\.\d+(?:\.\d\w*)?)\b/;
Branch: 1 branch
BranchTrueFalseTotal
if $out =~ /\b(\d+\.\d+(?:\.\d\w*)?)\b/u 0 1 1
253 1 return $out;
254 }
255  
256  
257 1 ▶sub xs { shift->{ffi} }
Subroutine xs: called CC=1 SCAR=0.0
Pod documented
258  
259  
260 5 1;
261  
262 __END__
↑ Summary