You are here

public function Point::__construct in geoPHP 8

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

Constructor

Parameters

numeric $x The x coordinate (or longitude):

numeric $y The y coordinate (or latitude):

numeric $z The z coordinate (or altitude) - optional:

File

geoPHP/lib/geometry/Point.class.php, line 20

Class

Point
Point: The most basic geometry type. All other geometries are built out of Points.

Code

public function __construct($x, $y, $z = NULL) {

  // Basic validation on x and y
  if (!is_numeric($x) || !is_numeric($y)) {
    throw new Exception("Cannot construct Point. x and y should be numeric");
  }

  // Check to see if this is a 3D point
  if ($z !== NULL) {
    if (!is_numeric($z)) {
      throw new Exception("Cannot construct Point. z should be numeric");
    }
    $this->dimention = 3;
  }

  // Convert to floatval in case they are passed in as a string or integer etc.
  $x = floatval($x);
  $y = floatval($y);
  $z = floatval($z);

  // Add poitional elements
  if ($this->dimention == 2) {
    $this->coords = array(
      $x,
      $y,
    );
  }
  if ($this->dimention == 3) {
    $this->coords = array(
      $x,
      $y,
      $z,
    );
  }
}