Geometry.class.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. <?php
  2. /**
  3. * Geometry abstract class
  4. */
  5. abstract class Geometry
  6. {
  7. private $geos = NULL;
  8. protected $srid = NULL;
  9. protected $geom_type;
  10. // Abtract: Standard
  11. // -----------------
  12. abstract public function area();
  13. abstract public function boundary();
  14. abstract public function centroid();
  15. abstract public function length();
  16. abstract public function y();
  17. abstract public function x();
  18. abstract public function numGeometries();
  19. abstract public function geometryN($n);
  20. abstract public function startPoint();
  21. abstract public function endPoint();
  22. abstract public function isRing(); // Mssing dependancy
  23. abstract public function isClosed(); // Missing dependancy
  24. abstract public function numPoints();
  25. abstract public function pointN($n);
  26. abstract public function exteriorRing();
  27. abstract public function numInteriorRings();
  28. abstract public function interiorRingN($n);
  29. abstract public function dimension();
  30. abstract public function equals($geom);
  31. abstract public function isEmpty();
  32. abstract public function isSimple();
  33. // Abtract: Non-Standard
  34. // ---------------------
  35. abstract public function getBBox();
  36. abstract public function asArray();
  37. abstract public function getPoints();
  38. abstract public function explode();
  39. abstract public function greatCircleLength(); //meters
  40. abstract public function haversineLength(); //degrees
  41. // Public: Standard -- Common to all geometries
  42. // --------------------------------------------
  43. public function SRID() {
  44. return $this->srid;
  45. }
  46. public function setSRID($srid) {
  47. if ($this->geos()) {
  48. $this->geos()->setSRID($srid);
  49. }
  50. $this->srid = $srid;
  51. }
  52. public function envelope() {
  53. if ($this->isEmpty()) return new Polygon();
  54. if ($this->geos()) {
  55. return geoPHP::geosToGeometry($this->geos()->envelope());
  56. }
  57. $bbox = $this->getBBox();
  58. $points = array (
  59. new Point($bbox['maxx'],$bbox['miny']),
  60. new Point($bbox['maxx'],$bbox['maxy']),
  61. new Point($bbox['minx'],$bbox['maxy']),
  62. new Point($bbox['minx'],$bbox['miny']),
  63. new Point($bbox['maxx'],$bbox['miny']),
  64. );
  65. $outer_boundary = new LineString($points);
  66. return new Polygon(array($outer_boundary));
  67. }
  68. public function geometryType() {
  69. return $this->geom_type;
  70. }
  71. // Public: Non-Standard -- Common to all geometries
  72. // ------------------------------------------------
  73. // $this->out($format, $other_args);
  74. public function out() {
  75. $args = func_get_args();
  76. $format = array_shift($args);
  77. $type_map = geoPHP::getAdapterMap();
  78. $processor_type = $type_map[$format];
  79. $processor = new $processor_type();
  80. array_unshift($args, $this);
  81. $result = call_user_func_array(array($processor, 'write'), $args);
  82. return $result;
  83. }
  84. // Public: Aliases
  85. // ---------------
  86. public function getCentroid() {
  87. return $this->centroid();
  88. }
  89. public function getArea() {
  90. return $this->area();
  91. }
  92. public function getX() {
  93. return $this->x();
  94. }
  95. public function getY() {
  96. return $this->y();
  97. }
  98. public function getGeos() {
  99. return $this->geos();
  100. }
  101. public function getGeomType() {
  102. return $this->geometryType();
  103. }
  104. public function getSRID() {
  105. return $this->SRID();
  106. }
  107. public function asText() {
  108. return $this->out('wkt');
  109. }
  110. public function asBinary() {
  111. return $this->out('wkb');
  112. }
  113. // Public: GEOS Only Functions
  114. // ---------------------------
  115. public function geos() {
  116. // If it's already been set, just return it
  117. if ($this->geos && geoPHP::geosInstalled()) {
  118. return $this->geos;
  119. }
  120. // It hasn't been set yet, generate it
  121. if (geoPHP::geosInstalled()) {
  122. $reader = new GEOSWKBReader();
  123. $this->geos = $reader->readHEX($this->out('wkb',TRUE));
  124. }
  125. else {
  126. $this->geos = FALSE;
  127. }
  128. return $this->geos;
  129. }
  130. public function setGeos($geos) {
  131. $this->geos = $geos;
  132. }
  133. public function pointOnSurface() {
  134. if ($this->geos()) {
  135. return geoPHP::geosToGeometry($this->geos()->pointOnSurface());
  136. }
  137. }
  138. public function equalsExact(Geometry $geometry) {
  139. if ($this->geos()) {
  140. return $this->geos()->equalsExact($geometry->geos());
  141. }
  142. }
  143. public function relate(Geometry $geometry, $pattern = NULL) {
  144. if ($this->geos()) {
  145. if ($pattern) {
  146. return $this->geos()->relate($geometry->geos(), $pattern);
  147. }
  148. else {
  149. return $this->geos()->relate($geometry->geos());
  150. }
  151. }
  152. }
  153. public function checkValidity() {
  154. if ($this->geos()) {
  155. return $this->geos()->checkValidity();
  156. }
  157. }
  158. public function buffer($distance) {
  159. if ($this->geos()) {
  160. return geoPHP::geosToGeometry($this->geos()->buffer($distance));
  161. }
  162. }
  163. public function intersection(Geometry $geometry) {
  164. if ($this->geos()) {
  165. return geoPHP::geosToGeometry($this->geos()->intersection($geometry->geos()));
  166. }
  167. }
  168. public function convexHull() {
  169. if ($this->geos()) {
  170. return geoPHP::geosToGeometry($this->geos()->convexHull());
  171. }
  172. }
  173. public function difference(Geometry $geometry) {
  174. if ($this->geos()) {
  175. return geoPHP::geosToGeometry($this->geos()->difference($geometry->geos()));
  176. }
  177. }
  178. public function symDifference(Geometry $geometry) {
  179. if ($this->geos()) {
  180. return geoPHP::geosToGeometry($this->geos()->symDifference($geometry->geos()));
  181. }
  182. }
  183. // Can pass in a geometry or an array of geometries
  184. public function union(Geometry $geometry) {
  185. if ($this->geos()) {
  186. if (is_array($geometry)) {
  187. $geom = $this->geos();
  188. foreach ($geometry as $item) {
  189. $geom = $geom->union($item->geos());
  190. }
  191. return geoPHP::geosToGeometry($geos);
  192. }
  193. else {
  194. return geoPHP::geosToGeometry($this->geos()->union($geometry->geos()));
  195. }
  196. }
  197. }
  198. public function simplify($tolerance, $preserveTopology = FALSE) {
  199. if ($this->geos()) {
  200. return geoPHP::geosToGeometry($this->geos()->simplify($tolerance, $preserveTopology));
  201. }
  202. }
  203. public function disjoint(Geometry $geometry) {
  204. if ($this->geos()) {
  205. return $this->geos()->disjoint($geometry->geos());
  206. }
  207. }
  208. public function touches(Geometry $geometry) {
  209. if ($this->geos()) {
  210. return $this->geos()->touches($geometry->geos());
  211. }
  212. }
  213. public function intersects(Geometry $geometry) {
  214. if ($this->geos()) {
  215. return $this->geos()->intersects($geometry->geos());
  216. }
  217. }
  218. public function crosses(Geometry $geometry) {
  219. if ($this->geos()) {
  220. return $this->geos()->crosses($geometry->geos());
  221. }
  222. }
  223. public function within(Geometry $geometry) {
  224. if ($this->geos()) {
  225. return $this->geos()->within($geometry->geos());
  226. }
  227. }
  228. public function contains(Geometry $geometry) {
  229. if ($this->geos()) {
  230. return $this->geos()->contains($geometry->geos());
  231. }
  232. }
  233. public function overlaps(Geometry $geometry) {
  234. if ($this->geos()) {
  235. return $this->geos()->overlaps($geometry->geos());
  236. }
  237. }
  238. public function covers(Geometry $geometry) {
  239. if ($this->geos()) {
  240. return $this->geos()->covers($geometry->geos());
  241. }
  242. }
  243. public function coveredBy(Geometry $geometry) {
  244. if ($this->geos()) {
  245. return $this->geos()->coveredBy($geometry->geos());
  246. }
  247. }
  248. public function distance(Geometry $geometry) {
  249. if ($this->geos()) {
  250. return $this->geos()->distance($geometry->geos());
  251. }
  252. }
  253. public function hausdorffDistance(Geometry $geometry) {
  254. if ($this->geos()) {
  255. return $this->geos()->hausdorffDistance($geometry->geos());
  256. }
  257. }
  258. public function project(Geometry $point, $normalized = NULL) {
  259. if ($this->geos()) {
  260. return $this->geos()->project($point->geos(), $normalized);
  261. }
  262. }
  263. // Public - Placeholders
  264. // ---------------------
  265. public function hasZ() {
  266. // geoPHP does not support Z values at the moment
  267. return FALSE;
  268. }
  269. public function is3D() {
  270. // geoPHP does not support 3D geometries at the moment
  271. return FALSE;
  272. }
  273. public function isMeasured() {
  274. // geoPHP does not yet support M values
  275. return FALSE;
  276. }
  277. public function coordinateDimension() {
  278. // geoPHP only supports 2-dimentional space
  279. return 2;
  280. }
  281. public function z() {
  282. // geoPHP only supports 2-dimentional space
  283. return NULL;
  284. }
  285. public function m() {
  286. // geoPHP only supports 2-dimentional space
  287. return NULL;
  288. }
  289. }