DBIO-PostgreSQL-PostGIS

 view release on metacpan or  search on metacpan

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


The geometry type, always upper-case, whatever built the object:
C<POINT>, C<LINESTRING>, C<POLYGON>, C<MULTIPOINT>, C<MULTILINESTRING>,
C<MULTIPOLYGON>, C<GEOMETRYCOLLECTION>. An object from L</from_ewkb_hex>
may also be one of the types that are named and not decoded:
C<CIRCULARSTRING>, C<COMPOUNDCURVE>, C<CURVEPOLYGON>, C<MULTICURVE>,
C<MULTISURFACE>, C<POLYHEDRALSURFACE>, C<TIN>, C<TRIANGLE>; and it has no
type (C<undef>) when the hex names none. This holds for every constructor
and for inflated column values, the codecs' lower-case type being
upper-cased on the way in (the column metadata
annotated by L<DBIO::PostgreSQL::PostGIS::Extension> keeps its own, lower-case
spelling). Derived from the WKT when the constructor did not set it,
without the dimension marker ST_AsEWKT glues to it: C<POINTM(1 2 9)> is a
C<POINT>.

=head2 ewkt

Returns C<SRID=N;WKT> if SRID is set, otherwise the bare WKT; C<undef>
when there is no WKT (see L</to_wkt>).

=head2 to_wkt

Returns the WKT string. Uses stored wkt if available; otherwise builds
from coordinates using L<DBIO::PostgreSQL::PostGIS::Codec::WKT::Builder>
-- the case for every object from L</from_ewkb_hex>, and so for every
value read from the database. Returns C<undef> when the object has
neither a stored WKT nor stored coordinates, and when it has coordinates
but no L</geometry_type> to build them under (C<< new(coordinates => [1,2]) >>):
the type is what the WKT is called, so there is nothing to write, and L</wkt>
and L</ewkt> are C<undef> with it. Written to a column or bound by a
spatial helper, such an object is an error, see
L<DBIO::PostgreSQL::PostGIS/register_column>.

An empty geometry builds C<TYPE EMPTY> (C<LINESTRING EMPTY>, with the
marker before C<EMPTY>: C<POINT Z EMPTY>, C<GEOMETRYCOLLECTION M EMPTY>).
A C<GEOMETRYCOLLECTION> builds from its members, each with its type, as
C<ST_AsText> prints it and PostGIS reads it:
C<GEOMETRYCOLLECTION Z(POINT Z(1 2 3),LINESTRING Z(0 0 1,1 1 2))>; the
dimension marker is on the collection and on each member.

The dimension marker of a built WKT (C<Z>, C<M>, C<ZM>) comes from the
C<has_z> / C<has_m> flags the object was constructed with;
L</from_ewkb_hex> takes them from the EWKB. An object constructed without
either flag is judged by the number of ordinates of one of its points:
three are C<Z>, four are C<ZM>. To have three ordinates written as C<M>,
pass C<< has_m => 1 >> to L</new>.

A built WKT holds each ordinate as Perl prints the double, which is 15
significant digits and not always the same number (C<0.1 + 0.2> prints as
C<0.3>). The exact value is in L</ewkb_hex>.

=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.

=head2 x

=head2 y

=head2 z

The ordinates of a POINT: C<x> and C<y>, and C<z> if the point has a Z
ordinate. For every other geometry type, for an empty point, and for
C<z> of a point without Z (a C<POINT M> has a measure, not a Z) they
return C<undef>. The type is judged case-independent.

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
coordinate parsing or a Geo::OGR fallback; C<undef> when
L</coordinates> has none. For a C<GEOMETRYCOLLECTION> it is the box of
all its members, at any depth. It is C<undef> for an empty geometry, and
for one whose members are all empty, as C<ST_Extent> is C<NULL> for it. An
empty member does not count.

=head2 to_geojson

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.

=head2 to_ogr

Returns a L<Geo::OGR::Geometry> object if Geo::OGR is installed; dies
otherwise.

=head1 AUTHOR

DBIO Authors

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2026 DBIO Authors

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut



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