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.900004';
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 336 ▶sub parse {
Subroutine parse: called CC=6 SCAR=18.1
Pod documented
27 336 my ($class, $wkt) = @_;
28 336 ▶ return undef unless defined $wkt;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $wkt 0 336 336
29 336 my $text = "$wkt";
30 336 my $result = eval {
31 336 my $g = _geometry(\$text, 0);
32 304 ▶ $text =~ /\G\s*\z/ or _unreadable();
Branch: 1 branch
BranchTrueFalseTotal
$text =~ /\G\s*\z/ or _unreadable() 304 0 304
33 304 $g;
34 };
35 336 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 32 304 336
36 32 ▶ return undef if !ref $err && $err eq $UNREADABLE;
Branch: 1 branch
BranchTrueFalseTotal
if not ref $err and $err eq $UNREADABLE 30 2 32
Condition66%2/3
not ref $err and $err eq $UNREADABLE
!ll&&!rl&&r
2030
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 2 die $err;
38 }
39 304 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 100509 ▶sub _geometry {
Subroutine _geometry: called CC=42 SCAR=37.4
49 100509 my ($s, $depth) = @_;
50  
51 # The marker is glued to the type (POINTM) or a word of its own, never both.
52 100509 ▶ $$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() 100505 4 100509
53 or _unreadable();
54 100505 ▶ my ($type, $mark, $open) = (lc $1, uc($2 // $3 // ''), $4);
Condition100%5/5
($2 // $3) // ""
l!l
175100330
$2 // $3
l!l&&r!l&&!r
34140100331
MC/DC100%2/2
($2 // $3) // ""
$2 $3
Truth table100%3/3
A // B
A= $2B= $3
ABresult
1X1
011
000
55 100505 ▶ my $marked = length $mark ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
length $mark ? : 175 100330 100505
56 100505 ▶ my $has_z = $mark =~ /Z/ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$mark =~ /Z/ ? : 83 100422 100505
57 100505 ▶ my $has_m = $mark =~ /M/ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$mark =~ /M/ ? : 129 100376 100505
58  
59 100505 ▶ DBIO::Exception->throw("WKT nesting too deep")
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "geometrycollection" and ++$depth > $MAX_DEPTH 2 100503 100505
Condition100%3/3
$type eq "geometrycollection" and ++$depth > $MAX_DEPTH
!ll&&!rl&&r
1003421612
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 100503 ▶ if ($open ne '(') {
Branch: 1 branch
BranchTrueFalseTotal
if ($open ne "(") 68 100435 100503
63 68 return { type => $type, coords => [], has_z => $has_z, has_m => $has_m, is_empty => 1 };
64 }
65  
66 100435 my ($coords, $is_empty);
67 100435 ▶ if ($type eq 'point') {
Branch: 6 branches
BranchTrueFalseTotal
if ($type eq 'point') { } 100118 317 100435
elsif ($type eq 'linestring') { } 40 277 317
elsif ($type eq 'multipoint') { } 59 218 277
elsif ($type eq 'polygon') { } 25 193 218
elsif ($type eq 'multilinestring') { } 19 174 193
elsif ($type eq 'multipolygon') { } 23 151 174
68 100118 my $leaf = _leaf($s);
69 100117 ▶ _unreadable() unless $leaf =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $leaf =~ /\S/ 0 100117 100117
70 100117 $coords = _parse_point_str($leaf);
71 } elsif ($type eq 'linestring') {
72 40 my $leaf = _leaf($s);
73 40 ▶ _unreadable() unless $leaf =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $leaf =~ /\S/ 0 40 40
74 40 $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 59 my @points;
80 59 ▶ 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); } } } 200058 57 200115
81 200117 ▶ if (_empty($s)) {
Branch: 2 branches
BranchTrueFalseTotal
if (_empty($s)) { } 22 200095 200117
elsif ($$s =~ /\G\(/cg) { } 200034 61 200095
82 22 push @points, [];
83 } elsif ($$s =~ /\G\(/gc) {
84 200034 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 57 $coords = \@points;
93 } elsif ($type eq 'polygon') {
94 25 $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 151 my (@members, $z, $m);
109 151 ▶ 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"}} } } 100022 84 100106
110 100173 my $member = _geometry($s, $depth);
111 100107 ▶ $z ||= $member->{has_z};
Condition100%3/3
$z ||= $member->{"has_z"}
l!l&&r!l&&!r
213100092
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 100107 ▶ $m ||= $member->{has_m};
Condition100%3/3
$m ||= $member->{"has_m"}
l!l&&r!l&&!r
217100088
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 100107 };
116 } while (_separator($s));
117 84 $coords = \@members;
118 84 ▶ ($has_z, $has_m) = ($z ? 1 : 0, $m ? 1 : 0) unless $marked;
Branch: 3 branches
BranchTrueFalseTotal
unless $marked 61 23 84
$z ? : 3 58 61
$m ? : 2 59 61
119 84 ▶ $is_empty = (grep { !$_->{is_empty} } @members) ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
grep({not $_->{'is_empty'};} @members) ? : 71 13 84
120 }
121  
122 # No marker: the ordinates say it, three are Z and four are ZM, as PostGIS
123 # reads them.
124 100343 ▶ if (!$marked && $type ne 'geometrycollection') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $marked and $type ne "geometrycollection") 100141 202 100343
Condition100%3/3
not $marked and $type ne "geometrycollection"
!ll&&!rl&&r
14161100141
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 100141 my $n = _first_point_size($coords);
126 100141 ▶ ($has_z, $has_m) = ($n >= 3 ? 1 : 0, $n >= 4 ? 1 : 0);
Branch: 2 branches
BranchTrueFalseTotal
$n >= 3 ? : 16 100125 100141
$n >= 4 ? : 8 100133 100141
127 }
128  
129 $is_empty //= ($type eq 'point' || $type eq 'linestring' || $type eq 'polygon')
130 ? 0
131 100343 ▶ : ((grep { @$_ } @$coords) ? 0 : 1);
Branch: 2 branches
BranchTrueFalseTotal
$type eq 'point' || $type eq 'linestring' || $type eq 'polygon' ? : 100162 97 100259
grep({@$_;} @$coords) ? : 88 9 97
Condition100%8/8
$is_empty //= $type eq "point" || $type eq "linestring" || $type eq "polygon" ? 0 : (grep({@$_;} @$coords) ? 0 : 1)
l!l
84100259
$type eq "point" or $type eq "linestring" or $type eq "polygon"
l!l&&r!l&&!r
1001382497
$type eq "point" or $type eq "linestring"
l!l&&r!l&&!r
10010335121
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 100343 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 300271 ▶sub _leaf {
Subroutine _leaf: called CC=2 SCAR=6.9
139 300271 my ($s) = @_;
140 300271 ▶ $$s =~ /\G([^()]*)\)/gc or _unreadable();
Branch: 1 branch
BranchTrueFalseTotal
$$s =~ /\G([^()]*)\)/cg or _unreadable() 300270 1 300271
141 300270 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 300336 ▶sub _separator {
Subroutine _separator: called CC=3 SCAR=11.0
147 300336 my ($s) = @_;
148 300336 ▶ $$s =~ /\G\s*(?:(,)|\))/gc or _unreadable();
Branch: 1 branch
BranchTrueFalseTotal
$$s =~ /\G\s*(?:(,)|\))/cg or _unreadable() 300335 1 300336
149 300335 ▶ return defined $1 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $1 ? : 300106 229 300335
150 }
151  
152 # True at EMPTY (consumed); a MULTI* member that is empty keeps its place as [].
153 200179 ▶sub _empty {
Subroutine _empty: called CC=2 SCAR=6.9
154 200179 my ($s) = @_;
155 200179 $$s =~ /\G\s*/gc;
156 200179 ▶ return $$s =~ /\GEMPTY/gci ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$$s =~ /\GEMPTY/cig ? : 36 200143 200179
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 50 ▶sub _rings {
Subroutine _rings: called CC=3 SCAR=11.0
168 50 my ($s) = @_;
169 50 my @rings;
170 50 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
_separator($s) and do { do { $$s =~ /\G\s*\(/cg or _unreadable(); push @rings, _parse_ring(_leaf($s)) } } 6 48 54
171 56 ▶ $$s =~ /\G\s*\(/gc or _unreadable();
Branch: 1 branch
BranchTrueFalseTotal
$$s =~ /\G\s*\(/cg or _unreadable() 56 0 56
172 56 push @rings, _parse_ring(_leaf($s));
173 } while (_separator($s));
174 48 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 100294 ▶sub _first_point_size {
Subroutine _first_point_size: called CC=6 SCAR=17.9
180 100294 my ($c) = @_;
181 100294 ▶ return 0 unless ref $c eq 'ARRAY' && @$c;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $c eq "ARRAY" and @$c 22 100272 100294
Condition66%2/3
ref $c eq "ARRAY" and @$c
!ll&&!rl&&r
022100272
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 100272 ▶ return scalar @$c unless ref $c->[0];
Branch: 1 branch
BranchTrueFalseTotal
unless ref $c->[0] 100132 140 100272
183 140 for (@$c) {
184 153 my $n = _first_point_size($_);
185 153 ▶ return $n if $n;
Branch: 1 branch
BranchTrueFalseTotal
if $n 131 22 153
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 300579 ▶sub _parse_point_str {
Subroutine _parse_point_str: called CC=7 SCAR=19.5
193 300579 my ($str) = @_;
194 300579 my @ord = split ' ', $str;
195 300579 ▶ _unreadable() if @ord < 2 || @ord > 4;
Branch: 1 branch
BranchTrueFalseTotal
if @ord < 2 or @ord > 4 9 300570 300579
Condition100%3/3
@ord < 2 or @ord > 4
l!l&&r!l&&!r
72300570
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 300570 ▶ _unreadable() if grep { !/$NUMBER_RE/ } @ord;
Branch: 1 branch
BranchTrueFalseTotal
if grep {not /$NUMBER_RE/;} @ord 13 300557 300570
197 300557 my @num = map { 0 + $_ } @ord;
198 300557 ▶ _unreadable() if grep { $_ != $_ || $_ == 9**9**9 || $_ == -9**9**9 } @num;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ != $_ or $_ == "Inf" or $_ == "-Inf";} @num 2 300555 300557
Condition83%5/6
$_ != $_ || $_ == 9**9**9 || $_ == -9**9**9
l!l&&r!l&&!r
11601433
$_ != $_ or $_ == "Inf"
l!l&&r!l&&!r
01601434
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 300555 \@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 119 ▶sub _parse_ring {
Subroutine _parse_ring: called CC=2 SCAR=7.0
205 119 my ($str) = @_;
206 119 ▶ return [] unless $str =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $str =~ /\S/ 0 119 119
207 119 [ map { _parse_point_str($_) } split /,/, $str, -1 ];
208 }
209  
210 13 1;
211  
212 __END__
↑ Summary PostGIS/Extension.pm »