1<?php
2
3/**
4 * Point: The most basic geometry type. All other geometries
5 * are built out of Points.
6 */
7class Point extends Geometry
8{
9  public $coords = array(2);
10  protected $geom_type = 'Point';
11  protected $dimension = 2;
12
13  /**
14   * Constructor
15   *
16   * @param numeric $x The x coordinate (or longitude)
17   * @param numeric $y The y coordinate (or latitude)
18   * @param numeric $z The z coordinate (or altitude) - optional
19   */
20  public function __construct($x = NULL, $y = NULL, $z = NULL) {
21
22    // Check if it's an empty point
23    if ($x === NULL && $y === NULL) {
24      $this->coords = array(NULL, NULL);
25      $this->dimension = 0;
26      return;
27    }
28
29    // Basic validation on x and y
30    if (!is_numeric($x) || !is_numeric($y)) {
31      throw new Exception("Cannot construct Point. x and y should be numeric");
32    }
33
34    // Check to see if this is a 3D point
35    if ($z !== NULL) {
36      if (!is_numeric($z)) {
37       throw new Exception("Cannot construct Point. z should be numeric");
38      }
39      $this->dimension = 3;
40    }
41
42    // Convert to floatval in case they are passed in as a string or integer etc.
43    $x = floatval($x);
44    $y = floatval($y);
45    $z = floatval($z);
46
47    // Add poitional elements
48    if ($this->dimension == 2) {
49      $this->coords = array($x, $y);
50    }
51    if ($this->dimension == 3) {
52      $this->coords = array($x, $y, $z);
53    }
54  }
55
56  /**
57   * Get X (longitude) coordinate
58   *
59   * @return float The X coordinate
60   */
61  public function x() {
62    return $this->coords[0];
63  }
64
65  /**
66   * Returns Y (latitude) coordinate
67   *
68   * @return float The Y coordinate
69   */
70  public function y() {
71    return $this->coords[1];
72  }
73
74  /**
75   * Returns Z (altitude) coordinate
76   *
77   * @return float The Z coordinate or NULL is not a 3D point
78   */
79  public function z() {
80    if ($this->dimension == 3) {
81      return $this->coords[2];
82    }
83    else return NULL;
84  }
85
86  /**
87   * Author : Adam Cherti
88   * inverts x and y coordinates
89   * Useful with old applications still using lng lat
90   *
91   * @return void
92   * */
93  public function invertxy()
94  {
95	$x=$this->coords[0];
96	$this->coords[0]=$this->coords[1];
97	$this->coords[1]=$x;
98  }
99
100
101  // A point's centroid is itself
102  public function centroid() {
103    return $this;
104  }
105
106  public function getBBox() {
107    return array(
108      'maxy' => $this->getY(),
109      'miny' => $this->getY(),
110      'maxx' => $this->getX(),
111      'minx' => $this->getX(),
112    );
113  }
114
115  public function asArray($assoc = FALSE) {
116    return $this->coords;
117  }
118
119  public function area() {
120    return 0;
121  }
122
123  public function length() {
124    return 0;
125  }
126
127  public function greatCircleLength() {
128    return 0;
129  }
130
131  public function haversineLength() {
132    return 0;
133  }
134
135  // The boundary of a point is itself
136  public function boundary() {
137    return $this;
138  }
139
140  public function dimension() {
141    return 0;
142  }
143
144  public function isEmpty() {
145    if ($this->dimension == 0) {
146      return TRUE;
147    }
148    else {
149      return FALSE;
150    }
151  }
152
153  public function numPoints() {
154    return 1;
155  }
156
157  public function getPoints() {
158    return array($this);
159  }
160
161  public function equals($geometry) {
162    if (get_class($geometry) != 'Point') {
163      return FALSE;
164    }
165    if (!$this->isEmpty() && !$geometry->isEmpty()) {
166      /**
167       * @see: http://php.net/manual/en/function.bccomp.php
168       * @see: http://php.net/manual/en/language.types.float.php
169       * @see: http://tubalmartin.github.io/spherical-geometry-php/#LatLng
170       */
171      return (abs($this->x() - $geometry->x()) <= 1.0E-9 && abs($this->y() - $geometry->y()) <= 1.0E-9);
172    }
173    else if ($this->isEmpty() && $geometry->isEmpty()) {
174      return TRUE;
175    }
176    else {
177      return FALSE;
178    }
179  }
180
181  public function isSimple() {
182    return TRUE;
183  }
184
185  // Not valid for this geometry type
186  public function numGeometries()    { return NULL; }
187  public function geometryN($n)      { return NULL; }
188  public function startPoint()       { return NULL; }
189  public function endPoint()         { return NULL; }
190  public function isRing()           { return NULL; }
191  public function isClosed()         { return NULL; }
192  public function pointN($n)         { return NULL; }
193  public function exteriorRing()     { return NULL; }
194  public function numInteriorRings() { return NULL; }
195  public function interiorRingN($n)  { return NULL; }
196  public function pointOnSurface()   { return NULL; }
197  public function explode()          { return NULL; }
198}
199
200