You are here

public function Point::__construct in geoPHP 7

Same name and namespace in other branches
  1. 8 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 = NULL, $y = NULL, $z = NULL) {

  // Check if it's an empty point
  if ($x === NULL && $y === NULL) {
    $this->coords = array(
      NULL,
      NULL,
    );
    $this->dimension = 0;
    return;
  }
  $x = (double) str_replace(',', '.', $x);
  $y = (double) str_replace(',', '.', $y);

  // 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->dimension = 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->dimension == 2) {
    $this->coords = array(
      $x,
      $y,
    );
  }
  if ($this->dimension == 3) {
    $this->coords = array(
      $x,
      $y,
      $z,
    );
  }
}