Skip to content

Commit

Permalink
Introduced a CoordinateSystem object
Browse files Browse the repository at this point in the history
This aggregates the $is3D, $isMeasured and $SRID variables for geometries.
  • Loading branch information
BenMorel committed Apr 16, 2015
1 parent 7af01bd commit 8657fcd
Show file tree
Hide file tree
Showing 27 changed files with 767 additions and 953 deletions.
17 changes: 5 additions & 12 deletions src/CircularString.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,16 @@ class CircularString extends Curve implements \Countable, \IteratorAggregate
protected $points = [];

/**
* @param Point[] $points
* @param boolean $is3D
* @param boolean $isMeasured
* @param integer $srid
* @param Point[] $points
* @param CoordinateSystem|null $cs
*
* @return CircularString
*
* @throws GeometryException
*/
public static function create(array $points, $is3D, $isMeasured, $srid = 0)
public static function create(array $points, CoordinateSystem $cs = null)
{
$is3D = (bool) $is3D;
$isMeasured = (bool) $isMeasured;

$srid = (int) $srid;

self::checkGeometries($points, Point::class, $is3D, $isMeasured, $srid);
$cs = self::checkGeometries($points, Point::class, $cs);

if ($points) {
$numPoints = count($points);
Expand All @@ -50,7 +43,7 @@ public static function create(array $points, $is3D, $isMeasured, $srid = 0)
}
}

$circularString = new CircularString(! $points, $is3D, $isMeasured, $srid);
$circularString = new CircularString($cs, ! $points);
$circularString->points = array_values($points);

return $circularString;
Expand Down
17 changes: 5 additions & 12 deletions src/CompoundCurve.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,16 @@ class CompoundCurve extends Curve implements \Countable, \IteratorAggregate
protected $curves = [];

/**
* @param Curve[] $curves
* @param boolean $is3D
* @param boolean $isMeasured
* @param integer $srid
* @param Curve[] $curves
* @param CoordinateSystem|null $cs
*
* @return CompoundCurve
*
* @throws GeometryException
*/
public static function create(array $curves, $is3D, $isMeasured, $srid = 0)
public static function create(array $curves, CoordinateSystem $cs = null)
{
$is3D = (bool) $is3D;
$isMeasured = (bool) $isMeasured;

$srid = (int) $srid;

self::checkGeometries($curves, Curve::class, $is3D, $isMeasured, $srid);
$cs = self::checkGeometries($curves, Curve::class, $cs);

/** @var Curve|null $previousCurve */
$previousCurve = null;
Expand All @@ -51,7 +44,7 @@ public static function create(array $curves, $is3D, $isMeasured, $srid = 0)
$previousCurve = $curve;
}

$compoundCurve = new CompoundCurve(! $curves, $is3D, $isMeasured, $srid);
$compoundCurve = new CompoundCurve($cs, ! $curves);
$compoundCurve->curves = array_values($curves);

return $compoundCurve;
Expand Down
176 changes: 176 additions & 0 deletions src/CoordinateSystem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
<?php

namespace Brick\Geo;

use Brick\Geo\Exception\GeometryException;

/**
* Represents the dimensionality and spatial reference system of a geometry.
*/
class CoordinateSystem
{
/**
* @var boolean
*/
private $hasZ;

/**
* @var boolean
*/
private $hasM;

/**
* @var integer
*/
private $srid;

/**
* @param boolean $hasZ
* @param boolean $hasM
* @param integer $srid
*/
private function __construct($hasZ, $hasM, $srid)
{
$this->hasZ = $hasZ;
$this->hasM = $hasM;
$this->srid = $srid;
}

/**
* @param boolean $hasZ
* @param boolean $hasM
* @param integer $srid
*
* @return CoordinateSystem
*/
public static function create($hasZ, $hasM, $srid = 0)
{
return new CoordinateSystem((bool) $hasZ, (bool) $hasM, (int) $srid);
}

/**
* @param integer $srid
*
* @return CoordinateSystem
*/
public static function xy($srid = 0)
{
return new self(false, false, (int) $srid);
}

/**
* @param integer $srid
*
* @return CoordinateSystem
*/
public static function xyz($srid = 0)
{
return new self(true, false, (int) $srid);
}

/**
* @param integer $srid
*
* @return CoordinateSystem
*/
public static function xym($srid = 0)
{
return new self(false, true, (int) $srid);
}

/**
* @param integer $srid
*
* @return CoordinateSystem
*/
public static function xyzm($srid = 0)
{
return new self(true, true, (int) $srid);
}

/**
* @return boolean
*/
public function hasZ()
{
return $this->hasZ;
}

/**
* @return boolean
*/
public function hasM()
{
return $this->hasM;
}

/**
* @return integer
*/
public function SRID()
{
return $this->srid;
}

/**
* @return integer
*
* @throws GeometryException
*/
public function coordinateDimension()
{
$coordinateDimension = 2;

if ($this->hasZ) {
$coordinateDimension++;
}

if ($this->hasM) {
$coordinateDimension++;
}

return $coordinateDimension;
}

/**
* @return integer
*/
public function spatialDimension()
{
return $this->hasZ ? 3 : 2;
}

/**
* Returns the dimensionality name.
*
* @return string XY, XYZ, XYM, or XYZM.
*/
public function name()
{
$name = 'XY';

if ($this->hasZ) {
$name .= 'Z';
}

if ($this->hasM) {
$name .= 'M';
}

return $name;
}

/**
* @param Geometry $geometry
*
* @return void
*
* @throws GeometryException
*/
public function check(Geometry $geometry)
{
if ($geometry->is3D() !== $this->hasZ || $geometry->isMeasured() !== $this->hasM) {
throw new GeometryException('Invalid geometry dimension');
}
}
}
17 changes: 5 additions & 12 deletions src/CurvePolygon.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,18 @@ class CurvePolygon extends Surface implements \Countable, \IteratorAggregate
protected $rings = [];

/**
* @param Curve[] $rings
* @param boolean $is3D
* @param boolean $isMeasured
* @param integer $srid
* @param Curve[] $rings
* @param CoordinateSystem|null $cs
*
* @return static
*
* @throws GeometryException
*/
public static function create(array $rings, $is3D, $isMeasured, $srid = 0)
public static function create(array $rings, CoordinateSystem $cs = null)
{
$is3D = (bool) $is3D;
$isMeasured = (bool) $isMeasured;
$cs = self::checkGeometries($rings, Curve::class, $cs);

$srid = (int) $srid;

self::checkGeometries($rings, Curve::class, $is3D, $isMeasured, $srid);

$CurvePolygon = new static(! $rings, $is3D, $isMeasured, $srid);
$CurvePolygon = new static($cs, ! $rings);
$CurvePolygon->rings = array_values($rings);

return $CurvePolygon;
Expand Down
4 changes: 2 additions & 2 deletions src/Exception/GeometryException.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public static function dimensionalityMix($a, $b)
*/
public static function incompatibleDimensionality(Geometry $geometry, $geometryType, $is3D, $isMeasured)
{
$message = 'Incompatible dimensionality: %s cannot contain %s.';
$message = 'Incompatible dimensionality: cannot mix %s with %s.';

$a = self::geometryType($geometryType, $is3D, $isMeasured);
$b = self::typeOf($geometry);
Expand All @@ -102,7 +102,7 @@ public static function incompatibleDimensionality(Geometry $geometry, $geometryT
*/
public static function incompatibleSRID(Geometry $geometry, $geometryType, $srid)
{
$message = 'Incompatible SRID: %s with SRID %d cannot contain %s with SRID %d.';
$message = 'Incompatible SRID: cannot mix %s using SRID %d with %s using SRID %d.';

return new self(sprintf($message, $geometryType, $srid, $geometry->geometryType(), $geometry->SRID()));
}
Expand Down

0 comments on commit 8657fcd

Please sign in to comment.