You are here

public function Polygon::area in geoPHP 8

Same name and namespace in other branches
  1. 7 geoPHP/lib/geometry/Polygon.class.php \Polygon::area()

Overrides Collection::area

1 call to Polygon::area()
Polygon::centroid in geoPHP/lib/geometry/Polygon.class.php

File

geoPHP/lib/geometry/Polygon.class.php, line 11

Class

Polygon
Polygon: A polygon is a plane figure that is bounded by a closed path, composed of a finite sequence of straight line segments

Code

public function area($exterior_only = FALSE, $signed = FALSE) {
  if ($this
    ->isEmpty()) {
    return 0;
  }
  if ($this
    ->geos() && $exterior_only == FALSE) {
    return $this
      ->geos()
      ->area();
  }
  $exterior_ring = $this->components[0];
  $pts = $exterior_ring
    ->getComponents();
  $c = count($pts);
  if ((int) $c == '0') {
    return NULL;
  }
  $a = '0';
  foreach ($pts as $k => $p) {
    $j = ($k + 1) % $c;
    $a = $a + $p
      ->getX() * $pts[$j]
      ->getY() - $p
      ->getY() * $pts[$j]
      ->getX();
  }
  if ($signed) {
    $area = $a / 2;
  }
  else {
    $area = abs($a / 2);
  }
  if ($exterior_only == TRUE) {
    return $area;
  }
  foreach ($this->components as $delta => $component) {
    if ($delta != 0) {
      $inner_poly = new Polygon(array(
        $component,
      ));
      $area -= $inner_poly
        ->area();
    }
  }
  return $area;
}