Skip to main content

LineString

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 LineString?​

A LineString is an ordered sequence of points joined by straight segments. Each consecutive pair of points defines one segment.

A two-point segment and a four-point open line with three straight segments.

LineString — Supported representations​

Availability in the current version of Spatial Types:

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

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;

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

$a = new PointImmutable(6.5, 8);
$b = new PointImmutable(27.5, 18.5);
$c = new PointImmutable(43, 8);
$d = new PointImmutable(50.5, 19);
$e = new PointImmutable(59, 10);
$f = new PointImmutable(68.5, 19.5);

$segment = new LineStringImmutable([$a, $b]);
$line = new LineStringImmutable([$c, $d, $e, $f]);

Mutable objects​

<?php

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

$a = new Point(6.5, 8);
$b = new Point(27.5, 18.5);
$c = new Point(43, 8);
$d = new Point(50.5, 19);
$e = new Point(59, 10);
$f = new Point(68.5, 19.5);

// Using the constructor.
$segment = new LineString([$a, $b]);

// Starting empty and adding points with methods.
$line = new LineString([])
->addPoint($c)
->addPoint($d)
->addPoint($e)
->addPoint($f);