DBIO-PostgreSQL-PostGIS

 view release on metacpan or  search on metacpan

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

    is $back->{has_z}, $case->{has_z} ? 1 : 0, '... with the same Z flag';
    is $back->{has_m}, $case->{has_m} ? 1 : 0, '... with the same M flag';
  };
}

subtest 'ewkt of a decoded line carries the SRID of the EWKB' => sub {
  my $g = $G->from_ewkb_hex($hex_of->(
    $head->(2 | $SRID), pack('V', 4326), $ring->([0,0],[1,1],[2,2]),
  ));
  ok $g, 'decodes' or return;
  is $g->ewkt, 'SRID=4326;LINESTRING(0 0,1 1,2 2)';
};

# An object built from bare coordinates has no flags to go by. The
# dimension is then read off one point: three ordinates are Z, four ZM --
# never off the number of points or rings. has_z / has_m given to new
# decide over that (three ordinates can only be told to be M).
subtest 'to_wkt of an object built from coordinates' => sub {
  my $wkt = sub { $G->new(@_)->to_wkt };
  is $wkt->(geometry_type => 'POINT', coordinates => [1,2]),
    'POINT(1 2)', '2D point';
  is $wkt->(geometry_type => 'POINT', coordinates => [1,2,3]),
    'POINT Z(1 2 3)', '3D point';
  is $wkt->(geometry_type => 'POINT', coordinates => [1,2,3,4]),
    'POINT ZM(1 2 3 4)', '4D point';
  is $wkt->(geometry_type => 'POINT', coordinates => [1,2,9], has_m => 1),
    'POINT M(1 2 9)', 'has_m given to new decides for three ordinates';
  is $wkt->(geometry_type => 'LINESTRING', coordinates => [[0,0],[1,1],[2,2]]),
    'LINESTRING(0 0,1 1,2 2)', '2D line of three points';
  is $wkt->(geometry_type => 'LINESTRING', coordinates => [[0,0,5],[1,1,6]]),
    'LINESTRING Z(0 0 5,1 1 6)', '3D line of two points';
  is $wkt->(
    geometry_type => 'POLYGON',
    coordinates   => [
      [[0,0],[9,0],[9,9],[0,0]],
      [[1,1],[2,1],[2,2],[1,1]],
      [[5,5],[6,5],[6,6],[5,5]],
    ],
  ), 'POLYGON((0 0,9 0,9 9,0 0),(1 1,2 1,2 2,1 1),(5 5,6 5,6 6,5 5))',
    '2D polygon of three rings';
};

# from_geojson writes MULTIPOINT without the per-point parentheses. Both
# forms are valid WKT; the parser has to read the one this dist writes.
subtest 'from_geojson MultiPoint survives the way through its WKT' => sub {
  my $gj = { type => 'MultiPoint', coordinates => [[1,2],[3,4],[5,6]] };
  my $g  = $G->from_geojson($gj);
  is_deeply $g->coordinates, $gj->{coordinates}, 'coordinates';
  is_deeply $g->to_geojson,  $gj,                'to_geojson';

  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)),
    'multilinestring', 'MULTILINESTRING EMPTY', 'MultiLineString' ],
  [ 'MULTIPOLYGON EMPTY', $hex_of->($head->(6 | $SRID), pack('V', 4326), pack('V', 0)),
    'multipolygon', 'MULTIPOLYGON EMPTY', 'MultiPolygon' ],
) {
  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';
};

# The same for a geometry made from WKT (karr k19).
subtest 'empty from WKT' => sub {
  for my $case (
    [ 'POINT EMPTY',            'Point' ],
    [ 'LINESTRING EMPTY',       'LineString' ],
    [ 'POLYGON EMPTY',          'Polygon' ],
    [ 'MULTIPOINT EMPTY',       'MultiPoint' ],
    [ 'POINT Z EMPTY',          'Point' ],
  ) {
    my ($wkt, $gj_type) = @$case;
    my $g = $G->from_wkt($wkt);
    ok $g->is_empty, "$wkt: is_empty";
    is $g->wkt, $wkt, "$wkt: wkt";
    is_deeply $g->to_geojson, { type => $gj_type, coordinates => [] },
      "$wkt: to_geojson, the same as for the EWKB-read one";
  }
  is_deeply $G->from_wkt('GEOMETRYCOLLECTION EMPTY')->to_geojson,
    { type => 'GeometryCollection', geometries => [] }, 'GEOMETRYCOLLECTION EMPTY: no geometries';
  ok $G->from_wkt('GEOMETRYCOLLECTION EMPTY')->is_empty, 'GEOMETRYCOLLECTION EMPTY is empty';

  # Nothing but empty parts is empty; one real part is not (ST_IsEmpty).
  ok  $G->from_wkt('MULTIPOINT(EMPTY)')->is_empty, 'MULTIPOINT(EMPTY): empty';
  ok  $G->from_wkt('GEOMETRYCOLLECTION(POINT EMPTY,POLYGON EMPTY)')->is_empty,
    'a collection of empty members: empty';
  ok !$G->from_wkt('MULTIPOINT(EMPTY,(1 2))')->is_empty, 'MULTIPOINT(EMPTY,(1 2)): not empty';
  ok !$G->from_wkt('POINT(1 2)')->is_empty, 'POINT(1 2): not empty';
};

# A GEOMETRYCOLLECTION read from EWKB (karr k24): the members come with
# their types, so the WKT is the collection, with its dimension.
subtest 'a collection read from EWKB' => sub {
  my $hex = $hex_of->(
    $head->(7 | $SRID), pack('V', 4326), pack('V', 2),
    $head->(1), pack('d< d<', 1, 2),
    $head->(2), $ring->([0,0],[1,1]),
  );
  my $g = $G->from_ewkb_hex($hex);
  is $g->geometry_type, 'GEOMETRYCOLLECTION', 'type';
  is $g->wkt, 'GEOMETRYCOLLECTION(POINT(1 2),LINESTRING(0 0,1 1))', 'wkt';
  is $g->ewkt, 'SRID=4326;GEOMETRYCOLLECTION(POINT(1 2),LINESTRING(0 0,1 1))', 'ewkt';
  ok !$g->is_empty, 'not empty';
  is_deeply $g->coordinates, [
    { type => 'point',      coords => [1,2],         is_empty => 0 },
    { type => 'linestring', coords => [[0,0],[1,1]], is_empty => 0 },
  ], 'coordinates: the members, each with its type';
  is_deeply $g->bbox, [0,0,1,2], 'bbox over the members';
  is_deeply $g->to_geojson, {
    type       => 'GeometryCollection',
    geometries => [
      { type => 'Point',      coordinates => [1,2] },
      { type => 'LineString', coordinates => [[0,0],[1,1]] },
    ],
  }, 'to_geojson: a GeometryCollection of the members, as ST_AsGeoJSON';
  is $g->ewkb_hex, $hex, 'the hex is kept';


  my $z = $G->from_ewkb_hex($hex_of->(
    $head->(7 | $SRID | $Z), pack('V', 4326), pack('V', 2),
    $head->(1 | $Z), pack('d< d< d<', 1, 2, 3),
    $head->(2 | $Z), $ring->([0,0,1],[1,1,2]),
  ));
  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->(
    $head->(7 | $SRID), pack('V', 4326), pack('V', 2),
    $head->(7), pack('V', 1), $head->(1), pack('d< d<', 1, 2),
    $head->(1), pack('d< d<', 3, 4),
  ));
  is $nested->wkt, 'GEOMETRYCOLLECTION(GEOMETRYCOLLECTION(POINT(1 2)),POINT(3 4))', 'nested';
  is_deeply $nested->bbox, [1,2,3,4], 'bbox of a nested collection';

  my $emptyc = $G->from_ewkb_hex($hex_of->($head->(7 | $SRID), pack('V', 4326), pack('V', 0)));
  is $emptyc->wkt, 'GEOMETRYCOLLECTION EMPTY', 'GEOMETRYCOLLECTION EMPTY';
  ok $emptyc->is_empty, 'is empty';
  is_deeply $emptyc->to_geojson, { type => 'GeometryCollection', geometries => [] },
    'to_geojson: no geometries';

  # One that is not decoded has no members to build from.
  my $curved = $G->from_ewkb_hex(
    '0107000020E6100000020000000101000000000000000000F03F000000000000004001080000000300000000000000000000000000000000000000000000000000F03F000000000000F03F00000000000000400000000000000000'
  );
  is $curved->wkt, undef, 'a collection with an undecoded member: no wkt';
  is $curved->to_geojson, undef, '... and no to_geojson';
  is $curved->bbox, undef, '... and no bbox';
};

# The EWKT ST_AsEWKT prints: M glued to the type, Z and ZM without a
# marker (karr k21). The type is the type, not POINTM; the coordinates are
# there; the text is kept as it came.
subtest 'EWKT as ST_AsEWKT prints it' => sub {
  my $m = $G->from_ewkt('SRID=4326;POINTM(1 2 9)');
  is $m->geometry_type, 'POINT', 'POINTM: the type is point';
  is $m->srid, 4326, '... srid';
  is_deeply $m->coordinates, [1, 2, 9], '... coordinates';
  is $m->wkt,  'POINTM(1 2 9)', '... wkt is the text given';
  is $m->ewkt, 'SRID=4326;POINTM(1 2 9)', '... ewkt round-trips';
  is_deeply $m->bbox, [1, 2, 1, 2], '... bbox';
  is_deeply $m->to_geojson, { type => 'Point', coordinates => [1, 2, 9] }, '... to_geojson';
  ok !$m->is_empty, '... not empty';

  # Built from what the parser read, the object writes the marker as the
  # builder does, and the parser reads that again.
  my $parsed = DBIO::PostgreSQL::PostGIS::Codec::WKT::Parser->parse($m->wkt);
  ok $parsed->{has_m} && !$parsed->{has_z}, '... the parser says M, not Z';
  my $built = $G->new(
    srid => 4326, geometry_type => $parsed->{type}, coordinates => $parsed->{coords},
    has_z => $parsed->{has_z}, has_m => $parsed->{has_m},
  );
  is $built->wkt, 'POINT M(1 2 9)', '... built from the parsed parts: the builder writes M';

  for my $c (
    [ 'MULTIPOINTM(1 2 9,3 4 8)',          'multipoint',      [[1,2,9],[3,4,8]] ],
    [ 'LINESTRINGM(0 0 1,1 1 2)',          'linestring',      [[0,0,1],[1,1,2]] ],
    [ 'POLYGONM((0 0 1,1 0 1,1 1 1,0 0 1))', 'polygon',       [[[0,0,1],[1,0,1],[1,1,1],[0,0,1]]] ],



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