Skip to content
Permalink
Browse files

Merge pull request #83 from codisart/add-other-coordinate-method

[TECH] Add a method to vertex to get the other coordinate.
  • Loading branch information...
toin0u committed Feb 12, 2016
2 parents 8dc1e98 + 3bb4d7a commit 7ba1484762d891d67715919bcfbe752d03bb9c88
Showing with 92 additions and 1 deletion.
  1. +36 −0 src/Coordinate/Coordinate.php
  2. +13 −0 src/Vertex/Vertex.php
  3. +43 −1 tests/Vertex/VertexTest.php
@@ -43,6 +43,13 @@ class Coordinate implements CoordinateInterface, \JsonSerializable
protected $ellipsoid;
/**
* The precision to use to compare big numbers
*
* @var integer
*/
private $precision = 8;
/**
* Set the latitude and the longitude of the coordinates into an selected ellipsoid.
*
@@ -155,6 +162,26 @@ public function setFromString($coordinates)
}
}
/**
* @return integer
*/
public function getPrecision()
{
return $this->precision;
}
/**
* @param integer $precision
* @return $this
*/
public function setPrecision($precision)
{
$this->precision = $precision;
return $this;
}
/**
* Converts a valid and acceptable geographic coordinates to decimal degrees coordinate.
*
@@ -232,4 +259,13 @@ public function jsonSerialize()
{
return [$this->latitude, $this->longitude];
}
/**
* Returns a boolean determining coordinates equality
* @param Coordinate $coordinate
* @return boolean
*/
public function isEqual(Coordinate $coordinate) {
return bccomp($this->latitude, $coordinate->getLatitude(), $this->getPrecision()) === 0 && bccomp($this->longitude, $coordinate->getLongitude(), $this->getPrecision()) === 0;
}
}
@@ -258,5 +258,18 @@ public function isOnSameLine(Vertex $vertex) {
}
}
/**
* Returns the other coordinate who is not the coordinate passed on argument
* @param Coordinate $point
* @return null|Coordinate
*/
public function getOtherCoordinate(CoordinateInterface $coordinate) {
if ($coordinate->isEqual($this->from)) {
return $this->to;
} else if ($coordinate->isEqual($this->to)) {
return $this->from;
}
return null;
}
}
@@ -353,7 +353,6 @@ public function testDestinationShouldHaveTheSameEllipsoid()
$this->assertSame($this->vertex->destination(123, 456)->getEllipsoid(), $FOO);
}
/**
* @dataProvider VertexCoordinatesAndExpectedSameLineStatusProvider
*/
@@ -431,4 +430,47 @@ public function VertexCoordinatesAndExpectedSameLineStatusProvider()
),
);
}
/**
* @dataProvider VertexCoordinatesOriginalCoordinatesAndOtherOneProvider
*/
public function testGetOtherCoordinate($vertexCoordinates, $oneCoordinate, $otherCoordinate)
{
$this->vertex->setFrom($vertexCoordinates['from']);
$this->vertex->setTo($vertexCoordinates['to']);
$this->assertInstanceOf('League\Geotools\Vertex\Vertex', $this->vertex);
$this->assertEquals($otherCoordinate, $this->vertex->getOtherCoordinate($oneCoordinate));
}
public function VertexCoordinatesOriginalCoordinatesAndOtherOneProvider()
{
return array(
array(
array(
'from' => new Coordinate(array(48.8234055, 2.3072664)),
'to' => new Coordinate(array(43.296482, 5.36978))
),
new Coordinate(array(48.8234055, 2.3072664)),
new Coordinate(array(43.296482, 5.36978))
),
array(
array(
'from' => new Coordinate(array(48.8234055, 2.3072664)),
'to' => new Coordinate(array(43.296482, 5.36978))
),
new Coordinate(array(43.296482, 5.36978)),
new Coordinate(array(48.8234055, 2.3072664))
),
array(
array(
'from' => new Coordinate(array(48.8234055, 2.3072664)),
'to' => new Coordinate(array(43.296482, 5.36978))
),
new Coordinate(array(2, 5)),
null
),
);
}
}

0 comments on commit 7ba1484

Please sign in to comment.
You can’t perform that action at this time.