Point.class.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 $dimention = 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, $y, $z = NULL) {
  19. // Basic validation on x and y
  20. if (!is_numeric($x) || !is_numeric($y)) {
  21. throw new Exception("Cannot construct Point. x and y should be numeric");
  22. }
  23. // Check to see if this is a 3D point
  24. if ($z !== NULL) {
  25. if (!is_numeric($z)) {
  26. throw new Exception("Cannot construct Point. z should be numeric");
  27. }
  28. $this->dimention = 3;
  29. }
  30. // Convert to floatval in case they are passed in as a string or integer etc.
  31. $x = floatval($x);
  32. $y = floatval($y);
  33. $z = floatval($z);
  34. // Add poitional elements
  35. if ($this->dimention == 2) {
  36. $this->coords = array($x, $y);
  37. }
  38. if ($this->dimention == 3) {
  39. $this->coords = array($x, $y, $z);
  40. }
  41. }
  42. /**
  43. * Get X (longitude) coordinate
  44. *
  45. * @return float The X coordinate
  46. */
  47. public function x() {
  48. return $this->coords[0];
  49. }
  50. /**
  51. * Returns Y (latitude) coordinate
  52. *
  53. * @return float The Y coordinate
  54. */
  55. public function y() {
  56. return $this->coords[1];
  57. }
  58. /**
  59. * Returns Z (altitude) coordinate
  60. *
  61. * @return float The Z coordinate or NULL is not a 3D point
  62. */
  63. public function z() {
  64. if ($this->dimention == 3) {
  65. return $this->coords[2];
  66. }
  67. else return NULL;
  68. }
  69. // A point's centroid is itself
  70. public function centroid() {
  71. return $this;
  72. }
  73. public function getBBox() {
  74. return array(
  75. 'maxy' => $this->getY(),
  76. 'miny' => $this->getY(),
  77. 'maxx' => $this->getX(),
  78. 'minx' => $this->getX(),
  79. );
  80. }
  81. public function asArray($assoc = FALSE) {
  82. return $this->coords;
  83. }
  84. public function area() {
  85. return 0;
  86. }
  87. public function length() {
  88. return 0;
  89. }
  90. public function greatCircleLength() {
  91. return 0;
  92. }
  93. public function haversineLength() {
  94. return 0;
  95. }
  96. // The boundary of a point is itself
  97. public function boundary() {
  98. return $this;
  99. }
  100. public function dimension() {
  101. return 0;
  102. }
  103. public function isEmpty() {
  104. return FALSE;
  105. }
  106. public function numPoints() {
  107. return 1;
  108. }
  109. public function getPoints() {
  110. return array($this);
  111. }
  112. public function equals($geometry) {
  113. return ($this->x() == $geometry->x() && $this->y() == $geometry->y());
  114. }
  115. public function isSimple() {
  116. return TRUE;
  117. }
  118. // Not valid for this geometry type
  119. public function numGeometries() { return NULL; }
  120. public function geometryN($n) { return NULL; }
  121. public function startPoint() { return NULL; }
  122. public function endPoint() { return NULL; }
  123. public function isRing() { return NULL; }
  124. public function isClosed() { return NULL; }
  125. public function pointN($n) { return NULL; }
  126. public function exteriorRing() { return NULL; }
  127. public function numInteriorRings() { return NULL; }
  128. public function interiorRingN($n) { return NULL; }
  129. public function pointOnSurface() { return NULL; }
  130. public function explode() { return NULL; }
  131. }