Skip to main content

SpatialCollection

View Markdown versionEdit this page on GitHub

Next version — This documentation describes a major version that is not yet the current stable release of spatial-types.

View the library roadmap

What is a SpatialCollection?​

A SpatialCollection groups spatial objects, potentially of different types, into one value. It can also contain nested collections. The immutable variants are named GeometryCollectionImmutable and GeographyCollectionImmutable.

One spatial collection containing a Point, a LineString and a Polygon.

SpatialCollection — Supported representations​

Availability in the current version of Spatial Types:

Coordinate layoutGeometry MutableGeometry ImmutableGeography MutableGeography Immutable
XY✅✅✅✅
XYZ✅✅✅✅
XYM✅✅✅✅
XYZM✅✅✅✅

Mutable representations use SpatialCollection; immutable representations use GeometryCollectionImmutable or GeographyCollectionImmutable.

Creating the illustrated objects​

The examples recreate the labelled objects above using Geometry in XY. Composer’s autoloader must already be loaded. Variable names match the point letters in the illustration.

Immutable objects​

<?php

use LongitudeOne\SpatialTypes\Types\Dimension2\Geometry\PointImmutable;
use LongitudeOne\SpatialTypes\Types\Dimension2\Geometry\LineStringImmutable;
use LongitudeOne\SpatialTypes\Types\Dimension2\Geometry\PolygonImmutable;
use LongitudeOne\SpatialTypes\Types\Dimension2\Geometry\GeometryCollectionImmutable;

// Immutable objects must be fully initialized through their constructors.

$a = new PointImmutable(10, 13);
$b = new PointImmutable(23.5, 7.5);
$c = new PointImmutable(31, 18.5);
$d = new PointImmutable(36.5, 10);
$e = new PointImmutable(47, 7.5);
$f = new PointImmutable(68, 7.5);
$g = new PointImmutable(63.5, 19.5);
$h = new PointImmutable(51, 18);

$line = new LineStringImmutable([$b, $c, $d]);
$ring = new LineStringImmutable([$e, $f, $g, $h, $e]);
$polygon = new PolygonImmutable([$ring]);
$collection = new GeometryCollectionImmutable(elements: [$a, $line, $polygon]);

Mutable objects​

<?php

use LongitudeOne\SpatialTypes\Types\Dimension2\Point;
use LongitudeOne\SpatialTypes\Types\Dimension2\LineString;
use LongitudeOne\SpatialTypes\Types\Dimension2\Polygon;
use LongitudeOne\SpatialTypes\Types\Dimension2\SpatialCollection;

$a = new Point(10, 13);
$b = new Point(23.5, 7.5);
$c = new Point(31, 18.5);
$d = new Point(36.5, 10);
$e = new Point(47, 7.5);
$f = new Point(68, 7.5);
$g = new Point(63.5, 19.5);
$h = new Point(51, 18);

$line = new LineString([$b, $c, $d]);
$ring = new LineString([$e, $f, $g, $h, $e]);
$polygon = new Polygon([$ring]);
// Using the constructor.
$collection = new SpatialCollection(elements: [$a, $line, $polygon]);

// Alternatively, adding members with methods.
$collection = new SpatialCollection()
->addElement($a)
->addElement($line)
->addElement($polygon);