Permalink
Browse files

[TECH] Add a method to vertex to get the other coordinate.

will be used to browse through polygon's vertexes by continuity of the points.
Add a test
  • Loading branch information...
codisart committed Feb 12, 2016
1 parent 4e2149f commit 3bb4d7adb9d94438d349c2fefbb43ddb02642d0c
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;
+ }
}
View
@@ -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 3bb4d7a

Please sign in to comment.