PostGIS/Codec/WKT/Parser.pm

filter: Statementstmt 100% 118 / 118 Branchbran 92.0% 92 / 100 Conditioncond 92.5% 37 / 40 MC/DCmcdc 85.0% 17 / 20 Subroutinesub 100% 14 / 14 Podpod 100% 1 / 1 totaltotal 95.2% 279 / 293 CC 66 SCAR 31.9
CC
66
Cov
96%
_geometry
42.0
_parse_point_str
7.0
parse
6.1
CRAP 24.4
↑ Summary PostGIS/Extension.pm »
1 package DBIO::PostgreSQL::PostGIS::Codec::WKT::Parser;
2 # ABSTRACT: WKT/EWKT parser for PostGIS geometry values
3  
4 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 13 ▶use DBIO::Exception ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 13 our $VERSION = '0.900007';
10  
11  
12 # Collections nest at most this deep; the outermost counts as level 1.
13 13 my $MAX_DEPTH = 32;
14  
15 # A number as WKT writes it: optional minus, digits with an optional
16 # fraction or a fraction alone, optional exponent.
17 13 my $NUMBER_RE = qr/\A-?(?:\d+\.?\d*|\.\d+)(?:[eE][-+]?\d+)?\z/;
18  
19 13 my $TYPE_RE = qr/geometrycollection|multilinestring|multipolygon|multipoint|linestring|polygon|point/i;
20  
21 # Thrown by the helpers for text that is no geometry; parse turns it into
22 # undef and rethrows anything else.
23 13 my $UNREADABLE = "DBIO::PostgreSQL::PostGIS::Codec::WKT::Parser: unreadable\n";
24 30 ▶sub _unreadable { die $UNREADABLE }
Subroutine _unreadable: called CC=1 SCAR=0.0
25  
26 370 ▶sub parse {
Subroutine parse: called CC=6 SCAR=18.1
Pod documented
27 370 my ($class, $wkt) = @_;
28 370 ▶ return undef unless defined $wkt;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $wkt 0 370 370
29 370 my $text = "$wkt";
30 370 my $result = eval {
31 370 my $g = _geometry(\$text, 0);
32 332 ▶ $text =~ /\G\s*\z/ or _unreadable();
Branch: 1 branch
BranchTrueFalseTotal
$text =~ /\G\s*\z/ or _unreadable() 332 0 332
33 332 $g;
34 };
35 370 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 38 332 370
36 38 ▶ return undef if !ref $err && $err eq $UNREADABLE;
Branch: 1 branch
BranchTrueFalseTotal
if not ref $err and $err eq $UNREADABLE 30 8 38
Condition66%2/3
not ref $err and $err eq $UNREADABLE
!ll&&!rl&&r
8030
MC/DC50%1/2
not ref $err and $err eq $UNREADABLE
not ref $err $err eq $UNREADABLE
Truth table66%2/3
not(A) and B
A= not ref $errB= $err eq $UNREADABLE
ABresult
0X0
100
111
37 8 die $err;
38 }
39 332 return $result;
40 }
41  
42 # The parser reads the one string through pos() and \G: every helper takes a
43 # reference to it, continues where the previous one stopped and leaves pos()
44 # behind its own token. Nothing is copied but the text of a coordinate list.
45  
46 # One geometry at the position. $depth is the number of GEOMETRYCOLLECTIONs
47 # around it.
48 238235 ▶sub _geometry {
Subroutine _geometry: called CC=42 SCAR=37.4
49 238235 my ($s, $depth) = @_;
50  
51 # The marker is glued to the type (POINTM) or a word of its own, never both.
52 238235 ▶ $$s =~ /\G\s*($TYPE_RE)(?:(ZM|Z|M)|\s+(ZM|Z|M))?\s*(\(|EMPTY)/gci
Branch: 1 branch
BranchTrueFalseTotal
$$s =~ /\G\s*($TYPE_RE)(?:(ZM|Z|M)|\s+(ZM|Z|M))?\s*(\(|EMPTY)/cig or _unreadable() 238231 4 238235
53 or _unreadable();
54 238231 ▶ my ($type, $mark, $open) = (lc $1, uc($2 // $3 // ''), $4);
Condition100%5/5
($2 // $3) // ""
l!l
175238056
$2 // $3
l!l&&r!l&&!r
34140238057
MC/DC100%2/2
($2 // $3) // ""
$2 $3
Truth table100%3/3
A // B
A= $2B= $3
ABresult
1X1
011
000
55 238231 ▶ my $marked = length $mark ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
length $mark ? : 175 238056 238231
56 238231 ▶ my $has_z = $mark =~ /Z/ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$mark =~ /Z/ ? : 83 238148 238231
57 238231 ▶ my $has_m = $mark =~ /M/ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$mark =~ /M/ ? : 129 238102 238231
58  
59 238231 ▶ DBIO::Exception->throw("WKT nesting too deep")
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "geometrycollection" and ++$depth > $MAX_DEPTH 8 238223 238231
Condition100%3/3
$type eq "geometrycollection" and ++$depth > $MAX_DEPTH
!ll&&!rl&&r
2378663578
MC/DC100%2/2
$type eq "geometrycollection" and ++$depth > $MAX_DEPTH
$type eq "geometrycollection" ++$depth > $MAX_DEPTH
Truth table100%3/3
A and B
A= $type eq "geometrycollection"B= ++$depth > $MAX_DEPTH
ABresult
0X0
100
111
60 if $type eq 'geometrycollection' && ++$depth > $MAX_DEPTH;
61  
62 238223 ▶ if ($open ne '(') {
Branch: 1 branch
BranchTrueFalseTotal
if ($open ne "(") 68 238155 238223
63 68 return { type => $type, coords => [], has_z => $has_z, has_m => $has_m, is_empty => 1 };
64 }
65  
66 238155 my ($coords, $is_empty);
67 238155 ▶ if ($type eq 'point') {
Branch: 6 branches
BranchTrueFalseTotal
if ($type eq 'point') { } 237622 533 238155
elsif ($type eq 'linestring') { } 48 485 533
elsif ($type eq 'multipoint') { } 67 418 485
elsif ($type eq 'polygon') { } 29 389 418
elsif ($type eq 'multilinestring') { } 19 370 389
elsif ($type eq 'multipolygon') { } 23 347 370
68 237622 my $leaf = _leaf($s);
69 237621 ▶ _unreadable() unless $leaf =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $leaf =~ /\S/ 0 237621 237621
70 237621 $coords = _parse_point_str($leaf);
71 } elsif ($type eq 'linestring') {
72 48 my $leaf = _leaf($s);
73 48 ▶ _unreadable() unless $leaf =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $leaf =~ /\S/ 0 48 48
74 48 $coords = _parse_ring($leaf);
75 } elsif ($type eq 'multipoint') {
76 # Both forms: "1 2,3 4" and "(1 2),(3 4)". A point holds no comma, so
77 # the commas separate the points either way. An EMPTY member has no
78 # ordinates and keeps its place as [].
79 67 my @points;
80 67 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
_separator($s) and do { do { if (_empty($s)) { push @points, []; } elsif ($$s =~ /\G\(/cg) { push @points, _parse_point_str(_leaf($s)); } else { $$s =~ /\G([^(),]*)/cg; my $bare = $1; _unreadable() unless $bare =~ /\S/; push @points, _parse_point_str($bare); } } } 475058 65 475123
81 475125 ▶ if (_empty($s)) {
Branch: 2 branches
BranchTrueFalseTotal
if (_empty($s)) { } 22 475103 475125
elsif ($$s =~ /\G\(/cg) { } 475042 61 475103
82 22 push @points, [];
83 } elsif ($$s =~ /\G\(/gc) {
84 475042 push @points, _parse_point_str(_leaf($s));
85 } else {
86 61 $$s =~ /\G([^(),]*)/gc;
87 61 my $bare = $1;
88 61 ▶ _unreadable() unless $bare =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $bare =~ /\S/ 0 61 61
89 61 push @points, _parse_point_str($bare);
90 }
91 } while (_separator($s));
92 65 $coords = \@points;
93 } elsif ($type eq 'polygon') {
94 29 $coords = _rings($s);
95 } elsif ($type eq 'multilinestring') {
96 19 my @lines;
97 19 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
_separator($s) and do { do { push @lines, _empty($s) ? [] : _parse_ring(_leaf(_open($s))) } } 10 18 28
98 29 ▶ push @lines, _empty($s) ? [] : _parse_ring(_leaf(_open($s)));
Branch: 1 branch
BranchTrueFalseTotal
_empty($s) ? : 6 23 29
99 } while (_separator($s));
100 18 $coords = \@lines;
101 } elsif ($type eq 'multipolygon') {
102 23 my @polygons;
103 23 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
_separator($s) and do { do { push @polygons, _empty($s) ? [] : _rings(_open($s)) } } 10 22 32
104 33 ▶ push @polygons, _empty($s) ? [] : _rings(_open($s));
Branch: 1 branch
BranchTrueFalseTotal
_empty($s) ? : 8 25 33
105 } while (_separator($s));
106 22 $coords = \@polygons;
107 } else {
108 347 my (@members, $z, $m);
109 347 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
_separator($s) and do { do { my $member = _geometry($s, $depth); $z ||= $member->{"has_z"}; $m ||= $member->{"has_m"}; push @members, {"type", $member->{"type"}, "coords", $member->{"coords"}, "is_empty", $member->{"is_empty"}} } } 237518 88 237606
110 237865 my $member = _geometry($s, $depth);
111 237607 ▶ $z ||= $member->{has_z};
Condition100%3/3
$z ||= $member->{"has_z"}
l!l&&r!l&&!r
213237592
MC/DC100%2/2
$z ||= $member->{"has_z"}
$z $member->{"has_z"}
Truth table100%3/3
A ||= B
A= $zB= $member->{"has_z"}
ABresult
1X1
011
000
112 237607 ▶ $m ||= $member->{has_m};
Condition100%3/3
$m ||= $member->{"has_m"}
l!l&&r!l&&!r
217237588
MC/DC100%2/2
$m ||= $member->{"has_m"}
$m $member->{"has_m"}
Truth table100%3/3
A ||= B
A= $mB= $member->{"has_m"}
ABresult
1X1
011
000
113 push @members, {
114 type => $member->{type}, coords => $member->{coords}, is_empty => $member->{is_empty},
115 237607 };
116 } while (_separator($s));
117 88 $coords = \@members;
118 88 ▶ ($has_z, $has_m) = ($z ? 1 : 0, $m ? 1 : 0) unless $marked;
Branch: 3 branches
BranchTrueFalseTotal
unless $marked 65 23 88
$z ? : 3 62 65
$m ? : 2 63 65
119 88 ▶ $is_empty = (grep { !$_->{is_empty} } @members) ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
grep({not $_->{'is_empty'};} @members) ? : 75 13 88
120 }
121  
122 # No marker: the ordinates say it, three are Z and four are ZM, as PostGIS
123 # reads them.
124 237871 ▶ if (!$marked && $type ne 'geometrycollection') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $marked and $type ne "geometrycollection") 237665 206 237871
Condition100%3/3
not $marked and $type ne "geometrycollection"
!ll&&!rl&&r
14165237665
MC/DC100%2/2
not $marked and $type ne "geometrycollection"
not $marked $type ne "geometrycollection"
Truth table100%3/3
not(A) and B
A= not $markedB= $type ne "geometrycollection"
ABresult
0X0
100
111
125 237665 my $n = _first_point_size($coords);
126 237665 ▶ ($has_z, $has_m) = ($n >= 3 ? 1 : 0, $n >= 4 ? 1 : 0);
Branch: 2 branches
BranchTrueFalseTotal
$n >= 3 ? : 16 237649 237665
$n >= 4 ? : 8 237657 237665
127 }
128  
129 $is_empty //= ($type eq 'point' || $type eq 'linestring' || $type eq 'polygon')
130 ? 0
131 237871 ▶ : ((grep { @$_ } @$coords) ? 0 : 1);
Branch: 2 branches
BranchTrueFalseTotal
$type eq 'point' || $type eq 'linestring' || $type eq 'polygon' ? : 237678 105 237783
grep({@$_;} @$coords) ? : 96 9 105
Condition100%8/8
$is_empty //= $type eq "point" || $type eq "linestring" || $type eq "polygon" ? 0 : (grep({@$_;} @$coords) ? 0 : 1)
l!l
88237783
$type eq "point" or $type eq "linestring" or $type eq "polygon"
l!l&&r!l&&!r
23765028105
$type eq "point" or $type eq "linestring"
l!l&&r!l&&!r
23760743133
MC/DC100%1/1
$is_empty //= $type eq "point" || $type eq "linestring" || $type eq "polygon" ? 0 : (grep({@$_;} @$coords) ? 0 : 1)
$is_empty
Truth table100%2/2
A
A= $is_empty
Aresult
11
00
132  
133 237871 return { type => $type, coords => $coords, has_z => $has_z, has_m => $has_m, is_empty => $is_empty };
134 }
135  
136 # The text up to the closing parenthesis, which is consumed with it. A
137 # coordinate list holds no parenthesis.
138 712795 ▶sub _leaf {
Subroutine _leaf: called CC=2 SCAR=6.9
139 712795 my ($s) = @_;
140 712795 ▶ $$s =~ /\G([^()]*)\)/gc or _unreadable();
Branch: 1 branch
BranchTrueFalseTotal
$$s =~ /\G([^()]*)\)/cg or _unreadable() 712794 1 712795
141 712794 return $1;
142 }
143  
144 # After a member: true at a comma (another member follows), false at the
145 # closing parenthesis of the list, both consumed.
146 712848 ▶sub _separator {
Subroutine _separator: called CC=3 SCAR=11.0
147 712848 my ($s) = @_;
148 712848 ▶ $$s =~ /\G\s*(?:(,)|\))/gc or _unreadable();
Branch: 1 branch
BranchTrueFalseTotal
$$s =~ /\G\s*(?:(,)|\))/cg or _unreadable() 712847 1 712848
149 712847 ▶ return defined $1 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $1 ? : 712602 245 712847
150 }
151  
152 # True at EMPTY (consumed); a MULTI* member that is empty keeps its place as [].
153 475187 ▶sub _empty {
Subroutine _empty: called CC=2 SCAR=6.9
154 475187 my ($s) = @_;
155 475187 $$s =~ /\G\s*/gc;
156 475187 ▶ return $$s =~ /\GEMPTY/gci ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$$s =~ /\GEMPTY/cig ? : 36 475151 475187
157 }
158  
159 # The opening parenthesis of a member.
160 48 ▶sub _open {
Subroutine _open: called CC=2 SCAR=7.1
161 48 my ($s) = @_;
162 48 ▶ $$s =~ /\G\(/gc or _unreadable();
Branch: 1 branch
BranchTrueFalseTotal
$$s =~ /\G\(/cg or _unreadable() 48 0 48
163 48 return $s;
164 }
165  
166 # The rings of a polygon, behind its opening parenthesis: "(x y,x y),(...)".
167 54 ▶sub _rings {
Subroutine _rings: called CC=3 SCAR=11.0
168 54 my ($s) = @_;
169 54 my @rings;
170 54 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
_separator($s) and do { do { $$s =~ /\G\s*\(/cg or _unreadable(); push @rings, _parse_ring(_leaf($s)) } } 6 52 58
171 60 ▶ $$s =~ /\G\s*\(/gc or _unreadable();
Branch: 1 branch
BranchTrueFalseTotal
$$s =~ /\G\s*\(/cg or _unreadable() 60 0 60
172 60 push @rings, _parse_ring(_leaf($s));
173 } while (_separator($s));
174 52 return \@rings;
175 }
176  
177 # Number of ordinates of the first point there is, whatever the nesting; 0
178 # for a geometry without a point.
179 237842 ▶sub _first_point_size {
Subroutine _first_point_size: called CC=6 SCAR=17.9
180 237842 my ($c) = @_;
181 237842 ▶ return 0 unless ref $c eq 'ARRAY' && @$c;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $c eq "ARRAY" and @$c 22 237820 237842
Condition66%2/3
ref $c eq "ARRAY" and @$c
!ll&&!rl&&r
022237820
MC/DC50%1/2
ref $c eq "ARRAY" and @$c
ref $c eq "ARRAY" @$c
Truth table66%2/3
A and B
A= ref $c eq "ARRAY"B= @$c
ABresult
0X0
100
111
182 237820 ▶ return scalar @$c unless ref $c->[0];
Branch: 1 branch
BranchTrueFalseTotal
unless ref $c->[0] 237656 164 237820
183 164 for (@$c) {
184 177 my $n = _first_point_size($_);
185 177 ▶ return $n if $n;
Branch: 1 branch
BranchTrueFalseTotal
if $n 155 22 177
186 }
187 9 return 0;
188 }
189  
190 # split ' ' skips leading whitespace and splits at whitespace runs, both in
191 # one pass; a regex trim (\s+\z) would try every start of a run.
192 713123 ▶sub _parse_point_str {
Subroutine _parse_point_str: called CC=7 SCAR=19.5
193 713123 my ($str) = @_;
194 713123 my @ord = split ' ', $str;
195 713123 ▶ _unreadable() if @ord < 2 || @ord > 4;
Branch: 1 branch
BranchTrueFalseTotal
if @ord < 2 or @ord > 4 9 713114 713123
Condition100%3/3
@ord < 2 or @ord > 4
l!l&&r!l&&!r
72713114
MC/DC100%2/2
@ord < 2 or @ord > 4
@ord < 2 @ord > 4
Truth table100%3/3
A or B
A= @ord < 2B= @ord > 4
ABresult
1X1
011
000
196 713114 ▶ _unreadable() if grep { !/$NUMBER_RE/ } @ord;
Branch: 1 branch
BranchTrueFalseTotal
if grep {not /$NUMBER_RE/;} @ord 13 713101 713114
197 713101 my @num = map { 0 + $_ } @ord;
198 713101 ▶ _unreadable() if grep { $_ != $_ || $_ == 9**9**9 || $_ == -9**9**9 } @num;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ != $_ or $_ == "Inf" or $_ == "-Inf";} @num 2 713099 713101
Condition83%5/6
$_ != $_ || $_ == 9**9**9 || $_ == -9**9**9
l!l&&r!l&&!r
111426521
$_ != $_ or $_ == "Inf"
l!l&&r!l&&!r
011426522
MC/DC66%2/3
$_ != $_ || $_ == 9**9**9 || $_ == -9**9**9
$_ != $_ $_ == "Inf" $_ == -9**9**9
Truth table75%3/4
A or B || C
A= $_ != $_B= $_ == "Inf"C= $_ == -9**9**9
ABCresult
1XX1
01X1
0011
0000
199 713099 \@num;
200 }
201  
202 # The points of a list, split at the commas, empty entries kept (an empty one
203 # is unreadable); each point trims itself.
204 131 ▶sub _parse_ring {
Subroutine _parse_ring: called CC=2 SCAR=7.0
205 131 my ($str) = @_;
206 131 ▶ return [] unless $str =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $str =~ /\S/ 0 131 131
207 131 [ map { _parse_point_str($_) } split /,/, $str, -1 ];
208 }
209  
210 13 1;
211  
212 __END__
↑ Summary PostGIS/Extension.pm »