GeoRSS.class.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. if (empty($string)) {
  75. return $coords;
  76. }
  77. $latlon = explode(' ',$string);
  78. foreach ($latlon as $key => $item) {
  79. if (!($key % 2)) {
  80. // It's a latitude
  81. $lat = $item;
  82. }
  83. else {
  84. // It's a longitude
  85. $lon = $item;
  86. $coords[] = new Point($lon, $lat);
  87. }
  88. }
  89. return $coords;
  90. }
  91. protected function parsePoints() {
  92. $points = array();
  93. $pt_elements = $this->xmlobj->getElementsByTagName('point');
  94. foreach ($pt_elements as $pt) {
  95. if ($pt->hasChildNodes()) {
  96. $point_array = $this->getPointsFromCoords(trim($pt->firstChild->nodeValue));
  97. }
  98. if (!empty($point_array)) {
  99. $points[] = $point_array[0];
  100. }
  101. else {
  102. $points[] = new Point();
  103. }
  104. }
  105. return $points;
  106. }
  107. protected function parseLines() {
  108. $lines = array();
  109. $line_elements = $this->xmlobj->getElementsByTagName('line');
  110. foreach ($line_elements as $line) {
  111. $components = $this->getPointsFromCoords(trim($line->firstChild->nodeValue));
  112. $lines[] = new LineString($components);
  113. }
  114. return $lines;
  115. }
  116. protected function parsePolygons() {
  117. $polygons = array();
  118. $poly_elements = $this->xmlobj->getElementsByTagName('polygon');
  119. foreach ($poly_elements as $poly) {
  120. if ($poly->hasChildNodes()) {
  121. $points = $this->getPointsFromCoords(trim($poly->firstChild->nodeValue));
  122. $exterior_ring = new LineString($points);
  123. $polygons[] = new Polygon(array($exterior_ring));
  124. }
  125. else {
  126. // It's an EMPTY polygon
  127. $polygons[] = new Polygon();
  128. }
  129. }
  130. return $polygons;
  131. }
  132. // Boxes are rendered into polygons
  133. protected function parseBoxes() {
  134. $polygons = array();
  135. $box_elements = $this->xmlobj->getElementsByTagName('box');
  136. foreach ($box_elements as $box) {
  137. $parts = explode(' ',trim($box->firstChild->nodeValue));
  138. $components = array(
  139. new Point($parts[3], $parts[2]),
  140. new Point($parts[3], $parts[0]),
  141. new Point($parts[1], $parts[0]),
  142. new Point($parts[1], $parts[2]),
  143. new Point($parts[3], $parts[2]),
  144. );
  145. $exterior_ring = new LineString($components);
  146. $polygons[] = new Polygon(array($exterior_ring));
  147. }
  148. return $polygons;
  149. }
  150. // Circles are rendered into points
  151. // @@TODO: Add good support once we have circular-string geometry support
  152. protected function parseCircles() {
  153. $points = array();
  154. $circle_elements = $this->xmlobj->getElementsByTagName('circle');
  155. foreach ($circle_elements as $circle) {
  156. $parts = explode(' ',trim($circle->firstChild->nodeValue));
  157. $points[] = new Point($parts[1], $parts[0]);
  158. }
  159. return $points;
  160. }
  161. protected function geometryToGeoRSS($geom) {
  162. $type = strtolower($geom->getGeomType());
  163. switch ($type) {
  164. case 'point':
  165. return $this->pointToGeoRSS($geom);
  166. break;
  167. case 'linestring':
  168. return $this->linestringToGeoRSS($geom);
  169. break;
  170. case 'polygon':
  171. return $this->PolygonToGeoRSS($geom);
  172. break;
  173. case 'multipoint':
  174. case 'multilinestring':
  175. case 'multipolygon':
  176. case 'geometrycollection':
  177. return $this->collectionToGeoRSS($geom);
  178. break;
  179. }
  180. return $output;
  181. }
  182. private function pointToGeoRSS($geom) {
  183. $out = '<'.$this->nss.'point>';
  184. if (!$geom->isEmpty()) {
  185. $out .= $geom->getY().' '.$geom->getX();
  186. }
  187. $out .= '</'.$this->nss.'point>';
  188. return $out;
  189. }
  190. private function linestringToGeoRSS($geom) {
  191. $output = '<'.$this->nss.'line>';
  192. foreach ($geom->getComponents() as $k => $point) {
  193. $output .= $point->getY().' '.$point->getX();
  194. if ($k < ($geom->numGeometries() -1)) $output .= ' ';
  195. }
  196. $output .= '</'.$this->nss.'line>';
  197. return $output;
  198. }
  199. private function polygonToGeoRSS($geom) {
  200. $output = '<'.$this->nss.'polygon>';
  201. $exterior_ring = $geom->exteriorRing();
  202. foreach ($exterior_ring->getComponents() as $k => $point) {
  203. $output .= $point->getY().' '.$point->getX();
  204. if ($k < ($exterior_ring->numGeometries() -1)) $output .= ' ';
  205. }
  206. $output .= '</'.$this->nss.'polygon>';
  207. return $output;
  208. }
  209. public function collectionToGeoRSS($geom) {
  210. $georss = '<'.$this->nss.'where>';
  211. $components = $geom->getComponents();
  212. foreach ($geom->getComponents() as $comp) {
  213. $georss .= $this->geometryToGeoRSS($comp);
  214. }
  215. $georss .= '</'.$this->nss.'where>';
  216. return $georss;
  217. }
  218. }