Skip to main content

Classes and interfaces

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

Spatial Types provides two groups of concrete classes: immutable values and mutable objects. Their namespaces help you find the right class; their interfaces describe the operations you can use. The trees below show namespace organization, not PHP inheritance.

Immutable classes​

Choose a dimension, then Geometry or Geography, then the spatial type with an Immutable suffix. For example, an immutable XY Geometry point is LongitudeOne\SpatialTypes\Types\Dimension2\Geometry\PointImmutable.

LongitudeOne\SpatialTypes\Types
└── <dimension>
├── Geometry
│ ├── PointImmutable
│ ├── LineStringImmutable
│ ├── CircularStringImmutable
│ ├── CompoundCurveImmutable
│ ├── PolygonImmutable
│ ├── TriangleImmutable
│ ├── PolyhedralSurfaceImmutable (XYZ and XYZM only)
│ ├── MultiPointImmutable
│ ├── MultiLineStringImmutable
│ ├── MultiPolygonImmutable
│ └── GeometryCollectionImmutable
└── Geography
├── Same spatial types as Geometry
└── GeographyCollectionImmutable instead of GeometryCollectionImmutable

Immutable objects return new values through their applicable with… operations. Their family and coordinate layout are fixed by the concrete class. See Immutability for an example of correcting one track without changing another.

Mutable classes​

Mutable classes live directly under the dimension namespace and have no Immutable suffix. For example, LongitudeOne\SpatialTypes\Types\Dimension2\Point represents an editable XY point. Its constructor selects Geometry or Geography, defaulting to Geometry.

LongitudeOne\SpatialTypes\Types
└── <dimension>
├── Point
├── LineString
├── CircularString
├── CompoundCurve
├── Polygon
├── Triangle
├── PolyhedralSurface (XYZ and XYZM only)
├── MultiPoint
├── MultiLineString
├── MultiPolygon
└── SpatialCollection

The same mutable class supports either family. SpatialCollection is the mutable counterpart of both immutable heterogeneous collection classes. Setters edit the receiver; supplied mutable components remain shared by reference. See Mutability.

Dimension namespaces​

Coordinate layoutReplace <dimension> withOrdinates
XYDimension2X, Y
XYZDimension3zX, Y, Z
XYMDimension3mX, Y, M
XYZMDimension4zmX, Y, Z, M

Both groups support these layouts, except PolyhedralSurface, which requires a layout containing Z. To select a type by its use case, see the spatial type catalogue.

Use interfaces for application code​

Interfaces live under LongitudeOne\SpatialTypes\Interfaces. Use a common interface when your code only reads a value, and a representation-specific interface when it needs to edit or replace it.

Your code needs to…Example interface
Read any spatial object's type, family or referenceSpatialInterface
Read point coordinates, regardless of mutabilityPointInterface
Read ordered points from a line stringLineStringInterface
Read a curve's start and end pointsCurveInterface
Copy an immutable value or replace its referenceImmutableSpatialInterface
Replace an immutable point's coordinatesImmutablePointInterface
Edit a point in placeMutablePointInterface

For example, a function reading a point's position can accept either representation without depending on a concrete class:

use LongitudeOne\SpatialTypes\Interfaces\PointInterface;
use LongitudeOne\SpatialTypes\Types\Dimension2\Geometry\PointImmutable;
use LongitudeOne\SpatialTypes\Types\Dimension2\Point;

function position(PointInterface $point): array
{
return $point->toArray();
}

assert([1, 2] === position(new PointImmutable(1, 2)));
assert([3, 4] === position(new Point(3, 4)));

Composer's autoloader must already be loaded. The objects implement the same read interface while retaining their own editing contracts. Internal abstract bases and traits are implementation details; application code can use public concrete classes and interfaces directly.

If you prefer not to select concrete classes yourself, use spatial factories or the spatial builder.