DBIO-PostgreSQL-PostGIS
view release on metacpan or search on metacpan
lib/DBIO/PostgreSQL/PostGIS/Codec/WKB/Decoder.pm view on Meta::CPAN
my $has_m = ($type_raw & $M_FLAG) ? 1 : 0;
my $type_id = $type_raw & 0xFFFF;
my $srid;
if ($has_srid) {
$srid = _read_uint32($buf, $pos, $le, $len);
}
my $header = {
type => $WKB_TYPES{$type_id}, srid => $srid, has_z => $has_z, has_m => $has_m,
};
return ($header, $le, $type_id);
}
sub _check_bounds {
my ($buf, $pos, $need, $len) = @_;
DBIO::Exception->throw(
"decode_hex: underrun at pos $$pos (need $need, have " . ($len - $$pos) . ")"
) if $$pos + $need > $len;
}
sub _read_uint8 {
my ($buf, $pos, $len) = @_;
_check_bounds($buf, $pos, 1, $len);
my $v = unpack('C', substr($$buf, $$pos, 1));
$$pos += 1; $v;
}
sub _read_uint32 {
my ($buf, $pos, $le, $len) = @_;
_check_bounds($buf, $pos, 4, $len);
my $v = unpack($le ? 'V' : 'N', substr($$buf, $$pos, 4));
$$pos += 4; $v;
}
# On a perl whose NV is wider than a double (long double, __float128) the
# unpacked double is that exact binary value: 13.4 reads as
# 13.4000000000000004. Decoded coordinates are the shortest decimal whose
# double is the 8 bytes read -- the number PostGIS writes as text. A perl
# with a double NV does no extra work.
use constant _WIDE_NV => $Config{nvsize} > 8;
sub _shortest_double {
my ($v) = @_;
return $v if $v != $v || $v == 9**9**9 || $v == -9**9**9;
my $bytes = pack('d', $v);
for my $p (15, 16, 17) {
my $s = sprintf('%.' . $p . 'g', $v);
return 0 + $s if pack('d', $s) eq $bytes;
}
$v;
}
sub _read_double {
my ($buf, $pos, $le, $len) = @_;
_check_bounds($buf, $pos, 8, $len);
my $v = unpack($le ? 'd<' : 'd>', substr($$buf, $$pos, 8));
$$pos += 8;
return _WIDE_NV ? _shortest_double($v) : $v;
}
# The point of a POINT, or of a MULTIPOINT or a collection: PostGIS writes
# POINT EMPTY as a point of NaN ordinates. That is no point; its
# coordinates are an empty list. (A point in a line is read by _read_point
# and is never empty.)
sub _read_geometry_point {
my ($c) = @_;
return [] if @$c >= 2 && $c->[0] != $c->[0] && $c->[1] != $c->[1];
return $c;
}
sub _read_point {
my ($buf, $pos, $le, $has_z, $has_m, $len) = @_;
my @c = (_read_double($buf, $pos, $le, $len), _read_double($buf, $pos, $le, $len));
push @c, _read_double($buf, $pos, $le, $len) if $has_z;
push @c, _read_double($buf, $pos, $le, $len) if $has_m;
return \@c;
}
# A count of things that follow, each of them at least $min bytes long. A
# count the rest of the input cannot hold is refused here, before a list
# is sized by it: the four bytes are input like any other, and a list of
# 0xFFFFFFFF elements is the end of the process, not an exception.
sub _read_count {
my ($buf, $pos, $le, $len, $min) = @_;
my $n = _read_uint32($buf, $pos, $le, $len);
_check_bounds($buf, $pos, $n * $min, $len);
return $n;
}
# The coordinates of the geometry at $pos, or undef for one that is not
# decoded. undef is not an error and says nothing about the bytes; it has
# to reach the caller as it is, so nothing here may go on reading after
# it -- where an undecoded body ends is not known.
sub _read_geometry {
my ($buf, $pos, $le, $type_id, $has_z, $has_m, $len, $depth) = @_;
return undef unless $DECODED{$type_id};
DBIO::Exception->throw('decode_hex: nesting too deep')
if $type_id == 7 && ++$depth > $MAX_DEPTH;
if ($type_id == 1) {
return _read_geometry_point(_read_point($buf, $pos, $le, $has_z, $has_m, $len));
}
my $point_size = 8 * (2 + $has_z + $has_m);
if ($type_id == 2) {
my $n = _read_count($buf, $pos, $le, $len, $point_size);
return [ map { _read_point($buf, $pos, $le, $has_z, $has_m, $len) } 1..$n ];
}
if ($type_id == 3) {
# A ring is at least its own count.
my $n = _read_count($buf, $pos, $le, $len, 4);
my @rings;
for (1..$n) {
my $m = _read_count($buf, $pos, $le, $len, $point_size);
push @rings, [ map { _read_point($buf, $pos, $le, $has_z, $has_m, $len) } 1..$m ];
}
return \@rings;
}
# Members, each with a header of its own: byte order and type word. A
# MULTI* holds their coordinates, a collection the members themselves,
# with their type.
lib/DBIO/PostgreSQL/PostGIS/Codec/WKB/Decoder.pm view on Meta::CPAN
B<Decoded.> The types C<point>, C<linestring>, C<polygon>,
C<multipoint>, C<multilinestring>, C<multipolygon> and
C<geometrycollection> are decoded to their coordinates, and to whether
they are empty.
=item *
B<Named, not decoded.> PostGIS writes more types than those:
C<circularstring>, C<compoundcurve>, C<curvepolygon>, C<multicurve>,
C<multisurface>, C<polyhedralsurface>, C<tin> and C<triangle>. Their
header is read, their body is not, and L</decode_hex> returns C<coords>
as C<undef>. So it does for a C<geometrycollection> that holds a member
of such a type, at any depth -- a collection is decoded as a whole or
not at all -- and for a type id that has no name here, whose C<type> is
C<undef> as well. No coordinates are returned for bytes this decoder
does not know the layout of.
=item *
B<Refused.> Input that is no EWKB -- not hex (a character that is no hex
digit, or an odd number of them), empty, a byte order that is neither 0
nor 1 (in the header or in a member), cut off, a count the rest of the
input cannot hold, bytes left over behind the end of a decoded geometry,
a C<multipoint> whose member is not a point, a member whose Z or M flag
differs from that of the whole, a C<geometrycollection> nested deeper
than 32 levels (the outermost is level 1) -- throws a L<DBIO::Exception>.
L</decode_header> tells what the header of such an input says, if it has
one (not for input that is not hex).
=back
PostGIS does not send input of the third kind: a value read from a
database is decoded, or named and left alone.
=head1 METHODS
=head2 decode_hex
my $result = DBIO::PostgreSQL::PostGIS::Codec::WKB::Decoder->decode_hex($hex);
Decodes an EWKB hex string into a hashref:
{
type => 'point',
srid => 4326, # undef if not present
has_z => 0,
has_m => 0,
coords => [x, y], # nested for non-point types
is_empty => 0,
}
C<coords> is C<undef> for a value that is not decoded (see
L</DESCRIPTION>); C<type>, C<srid>, C<has_z> and C<has_m> are those of
its header all the same. C<type> is C<undef> for a type id that has no
name here. Throws for input that is refused.
C<is_empty> is true for what PostGIS calls empty (C<ST_IsEmpty>): a
geometry without parts -- a C<linestring> or C<polygon> with no points or
rings, a C<multi*> or C<geometrycollection> with no members -- a C<point>
whose X and Y are both NaN (this is how PostGIS writes C<POINT EMPTY>),
and a C<multi*> or C<geometrycollection> whose members are all empty,
C<MULTIPOINT(EMPTY)> for one. C<coords> of an empty geometry is C<[]>,
and so is that of an empty point: not C<[NaN, NaN]>. A point with just
one NaN ordinate is a point. C<is_empty> is C<undef>, not false, for a
value that is not decoded: nothing is known about it.
An empty member of a C<multi*> keeps its place in C<coords> as an empty
list: C<MULTIPOINT(EMPTY,(1 2))> is C<[[], [1,2]]>. The structure of a
C<multi*> is otherwise the plain coordinates of its parts.
B<The members of a C<geometrycollection>> are a list of hashes, one per
member, in order, each carrying what the whole has:
{
type => 'linestring', # any decoded type, a collection too
coords => [[0,0],[1,1]], # as for that type; a collection's
# coords are again such a list
is_empty => 0, # that of the member
}
The members are returned with their types, not as bare coordinates, so the
geometry and a valid WKT can be told from them. The Z and M flags of a member
are those of the whole, the decoder refuses a member that differs.
What is read, and what is not, where PostGIS (C<ST_GeomFromEWKB>, checked
against 3.6) is more lenient than this decoder or the other way round:
=over 4
=item *
Both byte orders are read, and so are the members of a C<MULTI*> or a
collection in a byte order of their own: a big-endian member in a
little-endian value is read as big-endian, as PostGIS does. Nothing but
0 and 1 is a byte order.
=item *
A member with an SRID flag: PostGIS never writes one, but accepts it and
drops it; the SRID of the geometry is that of the outer header. The
decoder reads the SRID word of the member and drops it as well.
=item *
A member must have the Z and M flags of the whole. PostGIS refuses
anything else ("Dimensions mismatch in lwcollection"); so does the
decoder, which would otherwise read the member at the wrong width.
=item *
Bytes left behind the end of a decoded geometry are refused ("trailing
bytes"). PostGIS ignores them; but a value it sends has none, so they
mean the input is not what it claims to be. For a value that is not
decoded the end of the body is not known, and nothing is checked.
=item *
The hex must be hex: pairs of digits, either case, nothing else -- no
C<0x> prefix, no whitespace.
=item *
ISO WKB type codes (1001 for C<POINT Z>, 2001 for C<POINT M>, 3001 for
C<POINT ZM> and so on) are B<not> read. PostGIS understands them when it
( run in 1.101 second using v1.01-cache-2.11-cpan-60884a8df76 )