PostGIS/Codec/WKB/Decoder.pm

filter: Statementstmt 78.2% 61 / 78 Branchbran 60.7% 17 / 28 Conditioncond 60.0% 3 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 91.6% 11 / 12 Podpod 100% 1 / 1 totaltotal 74.0% 94 / 127 CC 18 SCAR 19.5
CC
18
Cov
73%
_read_geometry
18.7
decode_hex
9.1
_read_point
3.1
CRAP 7.0
« PostGIS/Codec/WKT/Parser.pm ↑ Summary PostGIS/Codec/WKT/Builder.pm »
1 package DBIO::PostgreSQL::PostGIS::Codec::WKB::Decoder;
2 # ABSTRACT: EWKB-hex decoder for PostGIS geometry values
3  
4 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use DBIO::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use DBIO::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 7 my %WKB_TYPES = (
10 1 => 'point', 2 => 'linestring', 3 => 'polygon',
11 4 => 'multipoint', 5 => 'multilinestring', 6 => 'multipolygon',
12 7 => 'geometrycollection',
13 );
14  
15 7 my $SRID_FLAG = 0x20000000;
16 7 my $Z_FLAG = 0x80000000;
17 7 my $M_FLAG = 0x40000000;
18  
19  
20 7 ▶sub decode_hex {
Subroutine decode_hex: called CC=9 SCAR=22.1
Pod documented
21 7 my ($class, $hex) = @_;
22 7 my $bin = pack('H*', $hex);
23 7 my $len = length($bin);
24 7 my $pos = 0;
25  
26 7 ▶ DBIO::Exception->throw('decode_hex: empty input') unless $len > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $len > 0 1 6 7
27  
28 6 my $byte_order = _read_uint8(\$bin, \$pos, $len);
29 6 ▶ DBIO::Exception->throw("decode_hex: invalid byte_order") unless $byte_order == 1 || $byte_order == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $byte_order == 1 or $byte_order == 0 1 5 6
Condition66%2/3
$byte_order == 1 or $byte_order == 0
l!l&&r!l&&!r
501
MC/DC50%1/2
$byte_order == 1 or $byte_order == 0
$byte_order == 1 $byte_order == 0
Truth table66%2/3
A or B
A= $byte_order == 1B= $byte_order == 0
ABresult
1X1
011
000
30 5 my $le = ($byte_order == 1);
31  
32 5 my $type_raw = _read_uint32(\$bin, \$pos, $le, $len);
33 4 ▶ my $has_srid = ($type_raw & $SRID_FLAG) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$type_raw & $SRID_FLAG ? : 3 1 4
34 4 ▶ my $has_z = ($type_raw & $Z_FLAG) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$type_raw & $Z_FLAG ? : 0 4 4
35 4 ▶ my $has_m = ($type_raw & $M_FLAG) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$type_raw & $M_FLAG ? : 0 4 4
36 4 my $type_id = $type_raw & 0xFFFF;
37 4 ▶ my $type_str = $WKB_TYPES{$type_id} // 'unknown';
Condition50%1/2
$WKB_TYPES{$type_id} // "unknown"
l!l
40
MC/DC0%0/1
$WKB_TYPES{$type_id} // "unknown"
$WKB_TYPES{$type_id}
Truth table50%1/2
A
A= $WKB_TYPES{$type_id}
Aresult
11
00
38  
39 4 my $srid;
40 4 ▶ if ($has_srid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_srid) 3 1 4
41 3 $srid = _read_uint32(\$bin, \$pos, $le, $len);
42 }
43  
44 4 my $coords = _read_geometry(\$bin, \$pos, $le, $type_id, $has_z, $has_m, $len);
45  
46 4 return { type => $type_str, srid => $srid, has_z => $has_z, has_m => $has_m, coords => $coords };
47 }
48  
49 22 ▶sub _check_bounds {
Subroutine _check_bounds: called CC=2 SCAR=6.9
50 22 my ($buf, $pos, $need, $len) = @_;
51 22 ▶ DBIO::Exception->throw(
Branch: 1 branch
BranchTrueFalseTotal
if $$pos + $need > $len 1 21 22
52 "decode_hex: underrun at pos $$pos (need $need, have " . ($len - $$pos) . ")"
53 ) if $$pos + $need > $len;
54 }
55  
56 6 ▶sub _read_uint8 {
Subroutine _read_uint8: called CC=1 SCAR=0.0
57 6 my ($buf, $pos, $len) = @_;
58 6 _check_bounds($buf, $pos, 1, $len);
59 6 my $v = unpack('C', substr($$buf, $$pos, 1));
60 6 $$pos += 1; $v;
61 }
62 8 ▶sub _read_uint32 {
Subroutine _read_uint32: called CC=2 SCAR=7.0
63 8 my ($buf, $pos, $le, $len) = @_;
64 8 _check_bounds($buf, $pos, 4, $len);
65 7 ▶ my $v = unpack($le ? 'V' : 'N', substr($$buf, $$pos, 4));
Branch: 1 branch
BranchTrueFalseTotal
$le ? : 7 0 7
66 7 $$pos += 4; $v;
67 }
68 8 ▶sub _read_double {
Subroutine _read_double: called CC=2 SCAR=7.0
69 8 my ($buf, $pos, $le, $len) = @_;
70 8 _check_bounds($buf, $pos, 8, $len);
71 8 ▶ my $v = unpack($le ? 'd<' : 'd>', substr($$buf, $$pos, 8));
Branch: 1 branch
BranchTrueFalseTotal
$le ? : 8 0 8
72 8 $$pos += 8; $v;
73 }
74  
75 4 ▶sub _read_point {
Subroutine _read_point: called CC=3 SCAR=11.3
76 4 my ($buf, $pos, $le, $has_z, $has_m, $len) = @_;
77 4 my @c = (_read_double($buf, $pos, $le, $len), _read_double($buf, $pos, $le, $len));
78 4 ▶ push @c, _read_double($buf, $pos, $le, $len) if $has_z;
Branch: 1 branch
BranchTrueFalseTotal
if $has_z 0 4 4
79 4 ▶ push @c, _read_double($buf, $pos, $le, $len) if $has_m;
Branch: 1 branch
BranchTrueFalseTotal
if $has_m 0 4 4
80 4 return \@c;
81 }
82  
83 4 ▶sub _read_geometry {
Subroutine _read_geometry: called CC=5 SCAR=29.3
84 4 my ($buf, $pos, $le, $type_id, $has_z, $has_m, $len) = @_;
85 4 ▶ if ($type_id == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type_id == 1) 4 0 4
86 4 return _read_point($buf, $pos, $le, $has_z, $has_m, $len);
87 }
88 0 my $n = _read_uint32($buf, $pos, $le, $len);
89 0 ▶ if ($type_id == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type_id == 2) 0 0 0
90 0 return [ map { _read_point($buf, $pos, $le, $has_z, $has_m, $len) } 1..$n ];
91 }
92 0 ▶ if ($type_id == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type_id == 3) 0 0 0
93 0 my @rings;
94 0 for (1..$n) {
95 0 my $m = _read_uint32($buf, $pos, $le, $len);
96 0 push @rings, [ map { _read_point($buf, $pos, $le, $has_z, $has_m, $len) } 1..$m ];
97 }
98 0 return \@rings;
99 }
100 0 return [ map { _read_subgeom($buf, $pos, $le, $has_z, $has_m, $len) } 1..$n ];
101 }
102  
103 0 ▶sub _read_subgeom {
Subroutine _read_subgeom: not called CC=1 SCAR=6.9
104 0 my ($buf, $pos, $le, $has_z, $has_m, $len) = @_;
105 0 _read_uint8($buf, $pos, $len);
106 0 my $type_raw = _read_uint32($buf, $pos, $le, $len);
107 0 return _read_geometry($buf, $pos, $le, $type_raw & 0xFFFF, $has_z, $has_m, $len);
108 }
109  
110 7 1;
111  
112 __END__
113  
114 =pod
115  
116 =encoding UTF-8
117  
118 =head1 NAME
119  
120 DBIO::PostgreSQL::PostGIS::Codec::WKB::Decoder - EWKB-hex decoder for PostGIS geometry values
121  
122 =head1 VERSION
123  
124 version 0.900001
125  
126 =head1 METHODS
127  
128 =head2 decode_hex
129  
130 my $result = DBIO::PostgreSQL::PostGIS::Codec::WKB::Decoder->decode_hex($hex);
131  
132 Decodes an EWKB hex string into a hashref:
133  
134 {
135 type => 'point',
136 srid => 4326, # undef if not present
137 has_z => 0,
138 has_m => 0,
139 coords => [x, y], # nested for non-point types
140 }
141  
142 =head1 AUTHOR
143  
144 DBIO Authors
145  
146 =head1 COPYRIGHT AND LICENSE
147  
148 Copyright (C) 2026 DBIO Authors
149  
150 This is free software; you can redistribute it and/or modify it under
151 the same terms as the Perl 5 programming language system itself.
152  
153 =cut
« PostGIS/Codec/WKT/Parser.pm ↑ Summary PostGIS/Codec/WKT/Builder.pm »