DBIO-PostgreSQL-PostGIS

 view release on metacpan or  search on metacpan

lib/DBIO/PostgreSQL/PostGIS/Codec/WKB/Decoder.pm  view on Meta::CPAN


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) = @_;

lib/DBIO/PostgreSQL/PostGIS/Codec/WKB/Decoder.pm  view on Meta::CPAN

    }

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:

    {

lib/DBIO/PostgreSQL/PostGIS/Geometry.pm  view on Meta::CPAN


=head2 coordinates

Returns the parsed coordinate structure for simple geometry types
(POINT → [x,y], LINESTRING → [[x,y],...], POLYGON → [[[x,y],...],...]).
Returns undef when the geometry is too complex to parse from WKT alone
(use L</to_geojson> via L<Geo::OGR> for those), and when the object has
neither stored coordinates nor a WKT to parse them from.

An empty geometry has an empty list, C<[]>: C<POINT EMPTY> is C<[]>, not
C<[NaN, NaN]> as PostGIS writes it. An empty part of a C<MULTI*> keeps
its place as an empty list (C<MULTIPOINT(EMPTY,(1 2))> is C<[[], [1,2]]>).

For a C<GEOMETRYCOLLECTION> read from EWKB the structure is a list of its
members, each a hashref C<< { type, coords, is_empty } >> with the
lower-case type of the member and the C<coordinates> it would have on its
own (for a collection in a collection, again such a list). Not the bare
coordinates: those could not be told from each other. A collection made
from WKT has the same list, read by the WKT parser; it has none (C<undef>)
if one member is of a type the parser does not read.

lib/DBIO/PostgreSQL/PostGIS/Geometry.pm  view on Meta::CPAN

B<Behaviour change:> they used to return, for a geometry that is not a
point, the first, second and third part of its coordinates -- an arrayref.

=head2 is_empty

True if this represents an empty geometry (e.g. C<POINT EMPTY>), as
C<ST_IsEmpty> says: a geometry without parts, or a C<MULTI*> or
C<GEOMETRYCOLLECTION> with nothing but empty ones (C<MULTIPOINT(EMPTY)>).
For an object from L</from_ewkb_hex> this is what the decoder found in
the EWKB: a C<LINESTRING> or C<POLYGON> without points or rings, a
C<MULTI*> or C<GEOMETRYCOLLECTION> without members, a C<POINT> of NaN
ordinates (this is how PostGIS writes C<POINT EMPTY>). For any other
object it is judged from the WKT: C<EMPTY> in it and no number.

False for an object that has no WKT at all, and for a value the decoder
does not decode (see L</from_ewkb_hex>): nothing is known about what
such a value holds.

=head2 bbox

Returns the bounding box as C<[xmin, ymin, xmax, ymax]>. Requires

lib/DBIO/PostgreSQL/PostGIS/Geometry.pm  view on Meta::CPAN


Returns a GeoJSON-shaped hashref, whatever constructor built the object
— the case L</geometry_type> reports plays no part. Returns C<undef> when
no coordinates can be derived, and for a type that has no GeoJSON form
(the curve types and the like).

An empty geometry gives its type with empty C<coordinates>: C<< { type =>
'Point', coordinates => [] } >> for C<POINT EMPTY>, the same for an
object read from EWKB and one made from WKT; this is what C<ST_AsGeoJSON>
answers, and RFC 7946 allows it. An empty point is C<[]>, not
C<["NaN","NaN"]>.

A C<GEOMETRYCOLLECTION> gives C<< { type => 'GeometryCollection',
geometries => [ ... ] } >>, a GeoJSON hashref for each member, nested for
a collection in a collection (RFC 7946 allows it; PostGIS refuses to
print one). C<GEOMETRYCOLLECTION EMPTY> has C<< geometries => [] >>. An
empty member is kept, as C<< { type => 'Polygon', coordinates => [] } >>;
C<ST_AsGeoJSON> drops some of them (all but an empty two-dimensional
point). Z and M are not told apart: a member's C<coordinates> hold every
ordinate the geometry has, an M one included, where C<ST_AsGeoJSON>
leaves M out.

t/36-empty-collection-live.t  view on Meta::CPAN

use strict;
use warnings;
use Test::More;
use JSON::PP ();

# Live read of empty geometries (karr k19) and of GEOMETRYCOLLECTIONs
# (karr k24).
#
# An empty geometry read from the database used to say it was not empty
# and to be "LINESTRING()" or "POINT(NaN NaN)"; a collection lost the types
# of its members and was "GEOMETRYCOLLECTION()". Rows are written with
# plain SQL (ST_GeomFromText), read through the inflater, and then held
# against what the server says about the same row:
#
#  - is_empty against ST_IsEmpty;
#  - wkt against ST_AsText, where the two print the same text (they do not
#    for MULTIPOINT, which PostGIS prints with a parenthesis around every
#    point, and the dimension marker is told apart by the space the server
#    puts before the parenthesis);
#  - the WKT and EWKT the object builds are read by the server as the very

t/codec-wkb-decoder.t  view on Meta::CPAN

    ok $got, "$iso: decode_hex answers" or diag $@ or next;
    is $got->{type},   undef, "$iso: no type";
    is $got->{coords}, undef, "$iso: no coordinates";
    is $got->{has_z},  0,     "$iso: no Z flag";
  }
};

# Emptiness (karr k19) and the parts of a collection (karr k24). The hex
# strings are what a PostGIS 3.6 server sends for ST_GeomFromText(<wkt>,
# 4326); t/36-empty-collection-live.t holds them against the server it
# runs on. PostGIS writes POINT EMPTY as a point of NaN ordinates, every
# other empty geometry as a count of 0. The decoder reports is_empty as
# ST_IsEmpty does: a geometry is empty if it has no parts or only empty
# ones (MULTIPOINT(EMPTY) is empty). An empty point has coords [], not
# [NaN, NaN].
my $NaN = 'NaN' + 0;
my $pt = sub { { type => 'point', coords => $_[0], is_empty => $_[1] // 0 } };
my @empty_cases = (
  [ 'POINT EMPTY', '0101000020e6100000000000000000f87f000000000000f87f',
    'point', [], 1, 0, 0 ],
  [ 'POINT Z EMPTY', '01010000a0e6100000000000000000f87f000000000000f87f000000000000f87f',
    'point', [], 1, 1, 0 ],
  [ 'POINT M EMPTY', '0101000060e6100000000000000000f87f000000000000f87f000000000000f87f',
    'point', [], 1, 0, 1 ],
  [ 'LINESTRING EMPTY', '0102000020e610000000000000', 'linestring', [], 1, 0, 0 ],
  [ 'LINESTRING ZM EMPTY', '01020000e0e610000000000000', 'linestring', [], 1, 1, 1 ],

t/codec-wkb-decoder.t  view on Meta::CPAN

    '0104000020e6100000020000000101000000000000000000f87f000000000000f87f0101000000000000000000f03f0000000000000040',
    'multipoint', [ [], [1,2] ], 0, 0, 0 ],
  [ 'MULTIPOINT((1 2),EMPTY)',
    '0104000020e6100000020000000101000000000000000000f03f00000000000000400101000000000000000000f87f000000000000f87f',
    'multipoint', [ [1,2], [] ], 0, 0, 0 ],
  [ 'MULTIPOLYGON(EMPTY,((0 0,1 0,1 1,0 0)))',
    '0106000020e6100000020000000103000000000000000103000000010000000400000000000000000000000000000000000000000000000000f03f0000000000000000000000000000f03f000000000000f03f00000000000000000000000000000000',
    'multipolygon', [ [], [[[0,0],[1,0],[1,1],[0,0]]] ], 0, 0, 0 ],
  # A geometry of nothing but empty parts is empty (ST_IsEmpty says so).
  [ 'MULTIPOINT(EMPTY)',
    '0104000020e6100000' . unpack('H*', pack('V C V d< d<', 1, 1, 1, $NaN, $NaN)),
    'multipoint', [ [] ], 1, 0, 0 ],
  [ 'GEOMETRYCOLLECTION(POLYGON EMPTY)', '0107000020e610000001000000010300000000000000',
    'geometrycollection', [ { type => 'polygon', coords => [], is_empty => 1 } ], 1, 0, 0 ],
  [ 'GEOMETRYCOLLECTION(MULTIPOINT EMPTY)', '0107000020e610000001000000010400000000000000',
    'geometrycollection', [ { type => 'multipoint', coords => [], is_empty => 1 } ], 1, 0, 0 ],
  [ 'GEOMETRYCOLLECTION Z (POINT Z EMPTY)',
    '01070000a0e6100000010000000101000080000000000000f87f000000000000f87f000000000000f87f',
    'geometrycollection', [ $pt->([], 1) ], 1, 1, 0 ],
  [ 'GEOMETRYCOLLECTION(POINT EMPTY,LINESTRING(0 0,1 1))',
    '0107000020e6100000020000000101000000000000000000f87f000000000000f87f01020000000200000000000000000000000000000000000000000000000000f03f000000000000f03f',

t/codec-wkb-decoder.t  view on Meta::CPAN

# A value that is not decoded says nothing about emptiness: is_empty is
# undef, not 0.
subtest 'a value that is not decoded has no is_empty' => sub {
  my $got = $d->decode_hex(
    '0108000020E61000000300000000000000000000000000000000000000000000000000F03F000000000000F03F00000000000000400000000000000000'
  );
  ok exists $got->{is_empty}, 'is_empty is in the answer ...';
  is $got->{is_empty}, undef, '... and is undef';
};

# A point with one NaN ordinate is a point, not an empty one.
subtest 'a point with a single NaN ordinate is not empty' => sub {
  my $got = $dec->($hex_of->($head->(1), $point->($NaN, 2)));
  is $got->{is_empty}, 0, 'not empty';
  is scalar @{ $got->{coords} }, 2, 'coords keep both ordinates';
};

# A NaN pair inside a line is a coordinate like any other, not an empty
# point: only a POINT (or a member of a MULTIPOINT or collection) is.
subtest 'NaN in a line is not an empty point' => sub {
  my $got = $dec->($hex_of->($head->(2), $line->([$NaN, $NaN], [1, 1])));
  is $got->{is_empty}, 0, 'not empty';
  is scalar @{ $got->{coords} }, 2, 'both points kept';
};

# Collections nest at most 32 levels deep, the outermost being level 1;
# deeper input is refused like any other that is no EWKB.
subtest 'nesting of collections is limited to 32 levels' => sub {
  my $nested = sub {
    my $depth = shift;
    my $hex = '';

t/codec-wkb-decoder.t  view on Meta::CPAN

  my $hex = unpack 'H*', pack('C V', 1, 1) . $bytes . pack('d<', 0);
  my $got = $d->decode_hex($hex)->{coords}[0];
  is pack('d<', $got), $bytes, "x=$x: read number packs to the input bytes";
}
for my $lit ('0.1', '13.4', '52.5', '-0.25', '1e+300') {
  my $hex = unpack 'H*', pack('C V d< d<', 1, 1, $lit, 0);
  is $d->decode_hex($hex)->{coords}[0], $lit, "$lit reads as the literal";
}
{
  my $hex = unpack 'H*', pack('C V', 1, 1) . pack('d<', 9**9**9 / 9**9**9) x 2;
  is_deeply $d->decode_hex($hex)->{coords}, [], 'NaN point stays an empty point';
}

done_testing;

t/codec-wkt-parser.t  view on Meta::CPAN

    my ($ratio, $small, $big, $r) = growth_ratio(
      sub { $shape->(' ' x $_[0]) }, sub { $p->parse($_[0]) }, $W, 8);
    ok $r, "8 MB of whitespace $name: read";
    cmp_ok $ratio, '<', $MAX_RATIO, '... in linear time' or diag "ratio $ratio ($small s, $big s)";
  }
}

# Ordinates are numbers, two to four per point; a point list has no empty entry.
for my $wkt ('POINT(abc def)', 'POINT((1 1))', 'POINT(1 2 3 4 5 6)', 'POINT(1 2 3 4 5)',
             'POINT(1)', 'POINT(1 x)', 'POINT(1 2e)', 'POINT(1 --2)', 'POINT(0x10 1)',
             'POINT(Inf NaN)', 'POINT(1e9999 0)', 'POINT(0 -1e9999)', 'POINT(. 1)', 'LINESTRING(0 0,1)',
             'POLYGON((0 0,1 0,1 abc,0 0))', 'MULTIPOINT(1 2,3)', 'MULTIPOINT((1 2),(3 x))',
             'MULTILINESTRING((0 0,1 1),(2 2,3 q))', 'MULTIPOLYGON(((0 0,1 0,1 1,z 0)))',
             'LINESTRING(1 2,)', 'LINESTRING(0 0,1 1,)', 'LINESTRING(,0 0,1 1)', 'LINESTRING(0 0,,1 1)',
             'GEOMETRYCOLLECTION(POINT(a b))') {
  my @warn;
  local $SIG{__WARN__} = sub { push @warn, @_ };
  is $p->parse($wkt), undef, "$wkt: not read";
  is scalar @warn, 0, '... without a warning';
}

t/geometry-roundtrip.t  view on Meta::CPAN

  my $again = $G->from_wkt($g->wkt);
  is_deeply $again->coordinates, $gj->{coordinates},
    'coordinates of the object rebuilt from its WKT';
  is_deeply $again->to_geojson, $gj,
    'to_geojson of the object rebuilt from its WKT';
};

# Empty geometries read from EWKB (karr k19). They carried no flag, and
# is_empty looked for "EMPTY" in a WKT that was never built, so every empty
# geometry read from the database said it was not empty and built
# "LINESTRING()" or "POINT(NaN NaN)". PostGIS writes POINT EMPTY as a point
# of NaN ordinates and every other empty geometry as a count of 0.
my $NaN = 'NaN' + 0;
my $EMPTY_POINT = $hex_of->($head->(1 | $SRID), pack('V', 4326), pack('d< d<', $NaN, $NaN));
for my $case (
  [ 'POINT EMPTY', $EMPTY_POINT, 'point', 'POINT EMPTY', 'Point' ],
  [ 'POINT Z EMPTY',
    $hex_of->($head->(1 | $SRID | $Z), pack('V', 4326), pack('d< d< d<', $NaN, $NaN, $NaN)),
    'point', 'POINT Z EMPTY', 'Point' ],
  [ 'POINT M EMPTY',
    $hex_of->($head->(1 | $SRID | $M), pack('V', 4326), pack('d< d< d<', $NaN, $NaN, $NaN)),
    'point', 'POINT M EMPTY', 'Point' ],
  [ 'LINESTRING EMPTY', $hex_of->($head->(2 | $SRID), pack('V', 4326), pack('V', 0)),
    'linestring', 'LINESTRING EMPTY', 'LineString' ],
  [ 'LINESTRING ZM EMPTY', $hex_of->($head->(2 | $SRID | $Z | $M), pack('V', 4326), pack('V', 0)),
    'linestring', 'LINESTRING ZM EMPTY', 'LineString' ],
  [ 'POLYGON EMPTY', $hex_of->($head->(3 | $SRID), pack('V', 4326), pack('V', 0)),
    'polygon', 'POLYGON EMPTY', 'Polygon' ],
  [ 'MULTIPOINT EMPTY', $hex_of->($head->(4 | $SRID), pack('V', 4326), pack('V', 0)),
    'multipoint', 'MULTIPOINT EMPTY', 'MultiPoint' ],
  [ 'MULTILINESTRING EMPTY', $hex_of->($head->(5 | $SRID), pack('V', 4326), pack('V', 0)),

t/geometry-roundtrip.t  view on Meta::CPAN

) {
  my ($name, $hex, $type, $wkt, $gj_type) = @$case;
  subtest "empty from EWKB: $name" => sub {
    my $g = $G->from_ewkb_hex($hex);
    ok $g, 'decodes' or return;
    is $g->geometry_type, uc $type, 'type';
    ok $g->is_empty, 'is_empty';
    is $g->wkt,    $wkt, 'wkt';
    is $g->to_wkt, $wkt, 'to_wkt';
    is $g->ewkt,   "SRID=4326;$wkt", 'ewkt';
    is_deeply $g->coordinates, [], 'coordinates are an empty list, not [NaN, NaN]';
    is $g->bbox, undef, 'no bbox';
    is $g->x, undef, 'no x';
    is_deeply $g->to_geojson, { type => $gj_type, coordinates => [] },
      'to_geojson: the type with empty coordinates, as ST_AsGeoJSON';
    is $g->ewkb_hex, $hex, 'the hex is the value, as given';

    my $back = $P->parse($g->wkt);
    ok $back && $back->{is_empty}, 'the built WKT parses as empty';
  };
}

subtest 'a geometry that is not empty is not' => sub {
  for my $hex (
    $hex_of->($head->(1), pack('d< d<', 1, 2)),
    $hex_of->($head->(1), pack('d< d<', $NaN, 2)),
    $hex_of->($head->(2), $ring->([0,0],[1,1])),
  ) {
    my $g = $G->from_ewkb_hex($hex);
    ok !$g->is_empty, 'is_empty is false: ' . $g->wkt;
  }
  my $curve = $G->from_ewkb_hex(
    '0108000020E61000000300000000000000000000000000000000000000000000000000F03F000000000000F03F00000000000000400000000000000000'
  );
  ok !$curve->is_empty, 'a value that is not decoded: false, nothing is known';
};

t/geometry-roundtrip.t  view on Meta::CPAN

  is $z->wkt, 'GEOMETRYCOLLECTION Z(POINT Z(1 2 3),LINESTRING Z(0 0 1,1 1 2))', 'a Z collection';

  my $m = $G->from_ewkb_hex($hex_of->(
    $head->(7 | $SRID | $M), pack('V', 4326), pack('V', 1),
    $head->(1 | $M), pack('d< d< d<', 1, 2, 3),
  ));
  is $m->wkt, 'GEOMETRYCOLLECTION M(POINT M(1 2 3))', 'an M collection';

  my $mixed = $G->from_ewkb_hex($hex_of->(
    $head->(7 | $SRID), pack('V', 4326), pack('V', 2),
    $head->(1), pack('d< d<', $NaN, $NaN),
    $head->(3), pack('V', 0),
  ));
  is $mixed->wkt, 'GEOMETRYCOLLECTION(POINT EMPTY,POLYGON EMPTY)', 'empty members';
  ok $mixed->is_empty, 'a collection of empty members is empty';
  is_deeply $mixed->to_geojson,
    { type => 'GeometryCollection', geometries => [
      { type => 'Point', coordinates => [] }, { type => 'Polygon', coordinates => [] },
    ] }, 'to_geojson keeps the empty members';

  my $nested = $G->from_ewkb_hex($hex_of->(



( run in 0.844 second using v1.01-cache-2.11-cpan-60884a8df76 )