GeoRSS.class.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. /*
  3. * Copyright (c) Patrick Hayes
  4. *
  5. * This code is open-source and licenced under the Modified BSD License.
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * PHP Geometry/GeoRSS encoder/decoder
  11. */
  12. class GeoRSS extends GeoAdapter
  13. {
  14. private $namespace = FALSE;
  15. private $nss = ''; // Name-space string. eg 'georss:'
  16. /**
  17. * Read GeoRSS string into geometry objects
  18. *
  19. * @param string $georss - an XML feed containing geoRSS
  20. *
  21. * @return Geometry|GeometryCollection
  22. */
  23. public function read($gpx) {
  24. return $this->geomFromText($gpx);
  25. }
  26. /**
  27. * Serialize geometries into a GeoRSS string.
  28. *
  29. * @param Geometry $geometry
  30. *
  31. * @return string The georss string representation of the input geometries
  32. */
  33. public function write(Geometry $geometry, $namespace = FALSE) {
  34. if ($namespace) {
  35. $this->namespace = $namespace;
  36. $this->nss = $namespace.':';
  37. }
  38. return $this->geometryToGeoRSS($geometry);
  39. }
  40. public function geomFromText($text) {
  41. // Change to lower-case, strip all CDATA, and de-namespace
  42. $text = strtolower($text);
  43. $text = preg_replace('/<!\[cdata\[(.*?)\]\]>/s','',$text);
  44. // Load into DOMDOcument
  45. $xmlobj = new DOMDocument();
  46. @$xmlobj->loadXML($text);
  47. if ($xmlobj === false) {
  48. throw new Exception("Invalid GeoRSS: ". $text);
  49. }
  50. $this->xmlobj = $xmlobj;
  51. try {
  52. $geom = $this->geomFromXML();
  53. } catch(InvalidText $e) {
  54. throw new Exception("Cannot Read Geometry From GeoRSS: ". $text);
  55. } catch(Exception $e) {
  56. throw $e;
  57. }
  58. return $geom;
  59. }
  60. protected function geomFromXML() {
  61. $geometries = array();
  62. $geometries = array_merge($geometries, $this->parsePoints());
  63. $geometries = array_merge($geometries, $this->parseLines());
  64. $geometries = array_merge($geometries, $this->parsePolygons());
  65. $geometries = array_merge($geometries, $this->parseBoxes());
  66. $geometries = array_merge($geometries, $this->parseCircles());
  67. if (empty($geometries)) {
  68. throw new Exception("Invalid / Empty GeoRSS");
  69. }
  70. return geoPHP::geometryReduce($geometries);
  71. }
  72. protected function getPointsFromCoords($string) {
  73. $coords = array();
  74. $latlon = explode(' ',$string);
  75. foreach ($latlon as $key => $item) {
  76. if (!($key % 2)) {
  77. // It's a latitude
  78. $lat = $item;
  79. }
  80. else {
  81. // It's a longitude
  82. $lon = $item;
  83. $coords[] = new Point($lon, $lat);
  84. }
  85. }
  86. return $coords;
  87. }
  88. protected function parsePoints() {
  89. $points = array();
  90. $pt_elements = $this->xmlobj->getElementsByTagName('point');
  91. foreach ($pt_elements as $pt) {
  92. $point_array = $this->getPointsFromCoords(trim($pt->firstChild->nodeValue));
  93. $points[] = $point_array[0];
  94. }
  95. return $points;
  96. }
  97. protected function parseLines() {
  98. $lines = array();
  99. $line_elements = $this->xmlobj->getElementsByTagName('line');
  100. foreach ($line_elements as $line) {
  101. $components = $this->getPointsFromCoords(trim($line->firstChild->nodeValue));
  102. $lines[] = new LineString($components);
  103. }
  104. return $lines;
  105. }
  106. protected function parsePolygons() {
  107. $polygons = array();
  108. $poly_elements = $this->xmlobj->getElementsByTagName('polygon');
  109. foreach ($poly_elements as $poly) {
  110. if ($poly->hasChildNodes()) {
  111. $points = $this->getPointsFromCoords(trim($poly->firstChild->nodeValue));
  112. $exterior_ring = new LineString($points);
  113. $polygons[] = new Polygon(array($exterior_ring));
  114. }
  115. else {
  116. // It's an EMPTY polygon
  117. $polygons[] = new Polygon();
  118. }
  119. }
  120. return $polygons;
  121. }
  122. // Boxes are rendered into polygons
  123. protected function parseBoxes() {
  124. $polygons = array();
  125. $box_elements = $this->xmlobj->getElementsByTagName('box');
  126. foreach ($box_elements as $box) {
  127. $parts = explode(' ',trim($box->firstChild->nodeValue));
  128. $components = array(
  129. new Point($parts[3], $parts[2]),
  130. new Point($parts[3], $parts[0]),
  131. new Point($parts[1], $parts[0]),
  132. new Point($parts[1], $parts[2]),
  133. new Point($parts[3], $parts[2]),
  134. );
  135. $exterior_ring = new LineString($components);
  136. $polygons[] = new Polygon(array($exterior_ring));
  137. }
  138. return $polygons;
  139. }
  140. // Circles are rendered into points
  141. // @@TODO: Add good support once we have circular-string geometry support
  142. protected function parseCircles() {
  143. $points = array();
  144. $circle_elements = $this->xmlobj->getElementsByTagName('circle');
  145. foreach ($circle_elements as $circle) {
  146. $parts = explode(' ',trim($circle->firstChild->nodeValue));
  147. $points[] = new Point($parts[1], $parts[0]);
  148. }
  149. return $points;
  150. }
  151. protected function geometryToGeoRSS($geom) {
  152. $type = strtolower($geom->getGeomType());
  153. switch ($type) {
  154. case 'point':
  155. return $this->pointToGeoRSS($geom);
  156. break;
  157. case 'linestring':
  158. return $this->linestringToGeoRSS($geom);
  159. break;
  160. case 'polygon':
  161. return $this->PolygonToGeoRSS($geom);
  162. break;
  163. case 'multipoint':
  164. case 'multilinestring':
  165. case 'multipolygon':
  166. case 'geometrycollection':
  167. return $this->collectionToGeoRSS($geom);
  168. break;
  169. }
  170. return $output;
  171. }
  172. private function pointToGeoRSS($geom) {
  173. return '<'.$this->nss.'point>'.$geom->getY().' '.$geom->getX().'</'.$this->nss.'point>';
  174. }
  175. private function linestringToGeoRSS($geom) {
  176. $output = '<'.$this->nss.'line>';
  177. foreach ($geom->getComponents() as $k => $point) {
  178. $output .= $point->getY().' '.$point->getX();
  179. if ($k < ($geom->numGeometries() -1)) $output .= ' ';
  180. }
  181. $output .= '</'.$this->nss.'line>';
  182. return $output;
  183. }
  184. private function polygonToGeoRSS($geom) {
  185. $output = '<'.$this->nss.'polygon>';
  186. $exterior_ring = $geom->exteriorRing();
  187. foreach ($exterior_ring->getComponents() as $k => $point) {
  188. $output .= $point->getY().' '.$point->getX();
  189. if ($k < ($exterior_ring->numGeometries() -1)) $output .= ' ';
  190. }
  191. $output .= '</'.$this->nss.'polygon>';
  192. return $output;
  193. }
  194. public function collectionToGeoRSS($geom) {
  195. $georss = '<'.$this->nss.'where>';
  196. $components = $geom->getComponents();
  197. foreach ($geom->getComponents() as $comp) {
  198. $georss .= $this->geometryToGeoRSS($comp);
  199. }
  200. $georss .= '</'.$this->nss.'where>';
  201. return $georss;
  202. }
  203. }