/sites/all/libraries/geoPHP/lib/geometry/Geometry.class.php

https://github.com/zzolo/incl · PHP · 358 lines · 254 code · 62 blank · 42 comment · 30 complexity · 3415a153f4c6efdd97ec0ac59071e2bb MD5 · raw file

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