From 0ad165667840ae5fdc4901288421935c0b98a8d9 Mon Sep 17 00:00:00 2001 From: Patrick Hayes Date: Sun, 8 Jan 2012 16:40:17 -0800 Subject: [PATCH] Adding option for areas to output signed (negative) areas. Chaning centroid method to use signed-areas --- lib/geometry/Polygon.class.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/geometry/Polygon.class.php b/lib/geometry/Polygon.class.php index 49a4a4ff..0c0dcf9a 100644 --- a/lib/geometry/Polygon.class.php +++ b/lib/geometry/Polygon.class.php @@ -34,7 +34,7 @@ public function __construct(array $linestrings) { } } - public function area($exterior_only = FALSE) { + public function area($exterior_only = FALSE, $signed = FALSE) { if ($this->geos() && $exterior_only == FALSE) { return $this->geos()->area(); } @@ -50,7 +50,9 @@ public function area($exterior_only = FALSE) { $a = $a + ($p->getX() * $pts[$j]->getY()) - ($p->getY() * $pts[$j]->getX()); } - $area = abs(($a / 2)); + if ($signed) $area = ($a / 2); + else $area = abs(($a / 2)); + if ($exterior_only == TRUE) { return $area; } @@ -74,7 +76,7 @@ public function centroid() { $c = count($pts); if((int)$c == '0') return NULL; $cn = array('x' => '0', 'y' => '0'); - $a = $this->area(TRUE); + $a = $this->area(TRUE, TRUE); // If this is a polygon with no area. Just return the first point. if ($a == 0) { @@ -90,7 +92,7 @@ public function centroid() { $cn['x'] = $cn['x'] / ( 6 * $a); $cn['y'] = $cn['y'] / ( 6 * $a); - + $centroid = new Point($cn['x'], $cn['y']); return $centroid; }