| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%4/6
$dim_str =~ /Z/i and not $dim_str =~ /M/i or $dim_str =~ /ZM/i
$dim_str =~ /Z/i and not $dim_str =~ /M/i
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 18 | ▶ | my $has_m = ($dim_str =~ /M/i) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 18 | ▶ | if ($body eq 'EMPTY') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 12 | ▶ | $start = $i if $depth++ == 0; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 78 | } elsif ($ch eq ')') { | ||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 12 | ▶ | if (--$depth == 0) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 4 | ▶ | $start = $i if $depth++ == 0; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 98 | } elsif ($ch eq ')') { | ||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 4 | ▶ | if (--$depth == 0) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||||||||||||||||||||