Point.class.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /**
  3. * Point: The most basic geometry type. All other geometries
  4. * are built out of Points.
  5. */
  6. class Point extends Geometry
  7. {
  8. public $coords = array(2);
  9. protected $geom_type = 'Point';
  10. protected $dimension = 2;
  11. /**
  12. * Constructor
  13. *
  14. * @param numeric $x The x coordinate (or longitude)
  15. * @param numeric $y The y coordinate (or latitude)
  16. * @param numeric $z The z coordinate (or altitude) - optional
  17. */
  18. public function __construct($x = NULL, $y = NULL, $z = NULL) {
  19. // Check if it's an empty point
  20. if ($x === NULL && $y === NULL) {
  21. $this->coords = array(NULL, NULL);
  22. $this->dimension = 0;
  23. return;
  24. }
  25. // Basic validation on x and y
  26. if (!is_numeric($x) || !is_numeric($y)) {
  27. throw new Exception("Cannot construct Point. x and y should be numeric");
  28. }
  29. // Check to see if this is a 3D point
  30. if ($z !== NULL) {
  31. if (!is_numeric($z)) {
  32. throw new Exception("Cannot construct Point. z should be numeric");
  33. }
  34. $this->dimension = 3;
  35. }
  36. // Convert to floatval in case they are passed in as a string or integer etc.
  37. $x = floatval($x);
  38. $y = floatval($y);
  39. $z = floatval($z);
  40. // Add poitional elements
  41. if ($this->dimension == 2) {
  42. $this->coords = array($x, $y);
  43. }
  44. if ($this->dimension == 3) {
  45. $this->coords = array($x, $y, $z);
  46. }
  47. }
  48. /**
  49. * Get X (longitude) coordinate
  50. *
  51. * @return float The X coordinate
  52. */
  53. public function x() {
  54. return $this->coords[0];
  55. }
  56. /**
  57. * Returns Y (latitude) coordinate
  58. *
  59. * @return float The Y coordinate
  60. */
  61. public function y() {
  62. return $this->coords[1];
  63. }
  64. /**
  65. * Returns Z (altitude) coordinate
  66. *
  67. * @return float The Z coordinate or NULL is not a 3D point
  68. */
  69. public function z() {
  70. if ($this->dimension == 3) {
  71. return $this->coords[2];
  72. }
  73. else return NULL;
  74. }
  75. // A point's centroid is itself
  76. public function centroid() {
  77. return $this;
  78. }
  79. public function getBBox() {
  80. return array(
  81. 'maxy' => $this->getY(),
  82. 'miny' => $this->getY(),
  83. 'maxx' => $this->getX(),
  84. 'minx' => $this->getX(),
  85. );
  86. }
  87. public function asArray($assoc = FALSE) {
  88. return $this->coords;
  89. }
  90. public function area() {
  91. return 0;
  92. }
  93. public function length() {
  94. return 0;
  95. }
  96. public function greatCircleLength() {
  97. return 0;
  98. }
  99. public function haversineLength() {
  100. return 0;
  101. }
  102. // The boundary of a point is itself
  103. public function boundary() {
  104. return $this;
  105. }
  106. public function dimension() {
  107. return 0;
  108. }
  109. public function isEmpty() {
  110. if ($this->dimension == 0) {
  111. return TRUE;
  112. }
  113. else {
  114. return FALSE;
  115. }
  116. }
  117. public function numPoints() {
  118. return 1;
  119. }
  120. public function getPoints() {
  121. return array($this);
  122. }
  123. public function equals($geometry) {
  124. if (get_class($geometry) != 'Point') {
  125. return FALSE;
  126. }
  127. if (!$this->isEmpty() && !$geometry->isEmpty()) {
  128. return ($this->x() == $geometry->x() && $this->y() == $geometry->y());
  129. }
  130. else if ($this->isEmpty() && $geometry->isEmpty()) {
  131. return TRUE;
  132. }
  133. else {
  134. return FALSE;
  135. }
  136. }
  137. public function isSimple() {
  138. return TRUE;
  139. }
  140. // Not valid for this geometry type
  141. public function numGeometries() { return NULL; }
  142. public function geometryN($n) { return NULL; }
  143. public function startPoint() { return NULL; }
  144. public function endPoint() { return NULL; }
  145. public function isRing() { return NULL; }
  146. public function isClosed() { return NULL; }
  147. public function pointN($n) { return NULL; }
  148. public function exteriorRing() { return NULL; }
  149. public function numInteriorRings() { return NULL; }
  150. public function interiorRingN($n) { return NULL; }
  151. public function pointOnSurface() { return NULL; }
  152. public function explode() { return NULL; }
  153. }