PostGIS/Codec/WKT/Parser.pm

filter: Statementstmt 94.2% 66 / 70 Branchbran 81.5% 31 / 38 Conditioncond 66.6% 4 / 6 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 100% 9 / 9 Podpod 100% 1 / 1 totaltotal 88.1% 112 / 127 CC 24 SCAR 22.3
CC
24
Cov
89%
parse
15.7
_split_by_top_level_commas
6.0
_split_multipolygon
6.0
CRAP 9.3
« PostGIS/ResultSet.pm ↑ Summary PostGIS/Codec/WKB/Decoder.pm »
1 package DBIO::PostgreSQL::PostGIS::Codec::WKT::Parser;
2 # ABSTRACT: WKT/EWKT parser for PostGIS geometry values
3  
4 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7  
8 18 ▶sub parse {
Subroutine parse: called CC=14 SCAR=27.5
Pod documented
9 18 my ($class, $wkt) = @_;
10 18 ▶ return undef unless defined $wkt;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $wkt 0 18 18
11 18 $wkt =~ s/\A\s+|\s+\z//g;
12  
13 18 ▶ if ($wkt !~ /\A([A-Z]+)((?:\s+(?:Z|M|ZM))?)\s*(\(.*\)|EMPTY)\z/si) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($wkt =~ /\A([A-Z]+)((?:\s+(?:Z|M|ZM))?)\s*(\(.*\)|EMPTY)\z/is) 0 18 18
14 0 return undef;
15 }
16 18 my ($type_str, $dim_str, $body) = ($1, $2, $3);
17  
18 18 my $type = lc($type_str);
19 18 ▶ my $has_z = ($dim_str =~ /Z/i && $dim_str !~ /M/i) || $dim_str =~ /ZM/i ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$dim_str =~ /Z/i && !($dim_str =~ /M/i) || $dim_str =~ /ZM/i ? : 2 16 18
Condition66%4/6
$dim_str =~ /Z/i and not $dim_str =~ /M/i or $dim_str =~ /ZM/i
l!l&&r!l&&!r
2016
$dim_str =~ /Z/i and not $dim_str =~ /M/i
!ll&&!rl&&r
1602
MC/DC33%1/3
$dim_str =~ /Z/i and not $dim_str =~ /M/i or $dim_str =~ /ZM/i
$dim_str =~ /Z/i not $dim_str =~ /M/i $dim_str =~ /ZM/i
Truth table40%2/5
A and not(B) or C
A= $dim_str =~ /Z/iB= not $dim_str =~ /M/iC= $dim_str =~ /ZM/i
ABCresult
11X1
0X11
1011
0X00
1000
20 18 ▶ my $has_m = ($dim_str =~ /M/i) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$dim_str =~ /M/i ? : 0 18 18
21  
22 18 ▶ if ($body eq 'EMPTY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($body eq "EMPTY") 2 16 18
23 2 return { type => $type, coords => [], has_z => $has_z, has_m => $has_m, is_empty => 1 };
24 }
25  
26 # Strip only the outermost layer of parentheses
27 16 $body =~ s/\A\((.+)\)\z/$1/;
28  
29 16 my $coords;
30 16 ▶ if ($type eq 'point') {
Branch: 6 branches
BranchTrueFalseTotal
if ($type eq 'point') { } 6 10 16
elsif ($type eq 'linestring') { } 2 8 10
elsif ($type eq 'multipoint') { } 1 7 8
elsif ($type eq 'polygon') { } 6 1 7
elsif ($type eq 'multilinestring') { } 0 1 1
elsif ($type eq 'multipolygon') { } 1 0 1
31 6 $coords = _parse_point_str($body, $has_z, $has_m);
32 } elsif ($type eq 'linestring') {
33 2 $coords = _parse_ring($body, $has_z, $has_m);
34 } elsif ($type eq 'multipoint') {
35 1 $coords = [ map { _parse_point($_, $has_z, $has_m) } _split_by_top_level_commas($body) ];
36 } elsif ($type eq 'polygon') {
37 6 $coords = [ map { _parse_ring($_, $has_z, $has_m) } _split_by_top_level_commas($body) ];
38 } elsif ($type eq 'multilinestring') {
39 0 $coords = [ map { _parse_ring($_, $has_z, $has_m) } _split_by_top_level_commas($body) ];
40 } elsif ($type eq 'multipolygon') {
41 1 $coords = [ map { _parse_one_polygon($_, $has_z, $has_m) } _split_multipolygon($body) ];
42 } else {
43 0 return undef;
44 }
45  
46 16 return { type => $type, coords => $coords, has_z => $has_z, has_m => $has_m, is_empty => 0 };
47 }
48  
49 64 ▶sub _parse_point_str {
Subroutine _parse_point_str: called CC=1 SCAR=0.0
50 64 my ($str, $has_z, $has_m) = @_;
51 64 $str =~ s/\A\s+|\s+\z//g;
52 64 [ map { 0 + $_ } split /\s+/, $str ];
53 }
54  
55 # Parse a parenthesized point: "(x y)" or "(x y z)"
56 2 ▶sub _parse_point {
Subroutine _parse_point: called CC=1 SCAR=0.0
57 2 my ($str, $has_z, $has_m) = @_;
58 2 $str =~ s/\A\s*\(|\)\s*\z//g;
59 2 _parse_point_str($str, $has_z, $has_m);
60 }
61  
62 12 ▶sub _parse_ring {
Subroutine _parse_ring: called CC=1 SCAR=0.0
63 12 my ($str, $has_z, $has_m) = @_;
64 12 $str =~ s/\A\s+|\s+\z//g;
65 12 [ map { _parse_point_str($_, $has_z, $has_m) } split /\s*,\s*/, $str ];
66 }
67  
68 # Split at top-level commas only (depth tracked via parens)
69 9 ▶sub _split_by_top_level_commas {
Subroutine _split_by_top_level_commas: called CC=6 SCAR=18.0
70 9 my ($str) = @_;
71 9 $str =~ s/\A\s+|\s+\z//g;
72 9 my @parts;
73 9 my ($depth, $start) = (0, 0);
74 9 for my $i (0 .. length($str) - 1) {
75 247 my $ch = substr($str, $i, 1);
76 247 ▶ if ($ch eq '(') {
Branch: 2 branches
BranchTrueFalseTotal
if ($ch eq '(') { } 12 235 247
elsif ($ch eq ')') { } 12 223 235
77 12 ▶ $start = $i if $depth++ == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $depth++ == 0 12 0 12
78 } elsif ($ch eq ')') {
79 12 ▶ if (--$depth == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (--$depth == 0) 12 0 12
80 12 push @parts, substr($str, $start + 1, $i - $start - 1);
81 }
82 }
83 }
84 9 return @parts;
85 }
86  
87 # Split MULTIPOLYGON body into individual polygon bodies
88 # e.g. "((0 0,1 0...)),((2 2,3 2...)))" → ["(0 0,1 0...)", "(2 2,3 2...)"]
89 1 ▶sub _split_multipolygon {
Subroutine _split_multipolygon: called CC=6 SCAR=17.9
90 1 my ($str) = @_;
91 1 $str =~ s/\A\s+|\s+\z//g;
92 1 my @polys;
93 1 my ($depth, $start) = (0, 0);
94 1 for my $i (0 .. length($str) - 1) {
95 47 my $ch = substr($str, $i, 1);
96 47 ▶ if ($ch eq '(') {
Branch: 2 branches
BranchTrueFalseTotal
if ($ch eq '(') { } 4 43 47
elsif ($ch eq ')') { } 4 39 43
97 4 ▶ $start = $i if $depth++ == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $depth++ == 0 2 2 4
98 } elsif ($ch eq ')') {
99 4 ▶ if (--$depth == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (--$depth == 0) 2 2 4
100 2 push @polys, substr($str, $start, $i - $start + 1);
101 }
102 }
103 }
104 1 return @polys;
105 }
106  
107 # Parse a single polygon body (including outer parens) into rings.
108 # Strip ONE layer of parens — polygon body is "(x y,x y,...,x y)".
109 2 ▶sub _parse_one_polygon {
Subroutine _parse_one_polygon: called CC=1 SCAR=0.0
110 2 my ($str, $has_z, $has_m) = @_;
111 2 $str =~ s/\A\s*\((.+)\)\s*\z/$1/s;
112 2 [ map { _parse_ring($_, $has_z, $has_m) } _split_by_top_level_commas($str) ];
113 }
114  
115 8 1;
116  
117 __END__
118  
119 =pod
120  
121 =encoding UTF-8
122  
123 =head1 NAME
124  
125 DBIO::PostgreSQL::PostGIS::Codec::WKT::Parser - WKT/EWKT parser for PostGIS geometry values
126  
127 =head1 VERSION
128  
129 version 0.900001
130  
131 =head1 METHODS
132  
133 =head2 parse
134  
135 my $result = DBIO::PostgreSQL::PostGIS::Codec::WKT::Parser->parse($wkt);
136  
137 Parses a WKT string and returns a hashref:
138  
139 {
140 type => 'point',
141 coords => [...],
142 has_z => 0,
143 has_m => 0,
144 is_empty => 0,
145 }
146  
147 Returns C<undef> for unsupported types.
148  
149 =head1 AUTHOR
150  
151 DBIO Authors
152  
153 =head1 COPYRIGHT AND LICENSE
154  
155 Copyright (C) 2026 DBIO Authors
156  
157 This is free software; you can redistribute it and/or modify it under
158 the same terms as the Perl 5 programming language system itself.
159  
160 =cut
« PostGIS/ResultSet.pm ↑ Summary PostGIS/Codec/WKB/Decoder.pm »