GeoJSON.class.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. * GeoJSON class : a geojson reader/writer.
  4. *
  5. * Note that it will always return a GeoJSON geometry. This
  6. * means that if you pass it a feature, it will return the
  7. * geometry of that feature strip everything else.
  8. */
  9. class GeoJSON extends GeoAdapter
  10. {
  11. /**
  12. * Given an object or a string, return a Geometry
  13. *
  14. * @param mixed $input The GeoJSON string or object
  15. *
  16. * @return object Geometry
  17. */
  18. public function read($input) {
  19. if (is_string($input)) {
  20. $input = json_decode($input);
  21. }
  22. if (!is_object($input)) {
  23. throw new Exception('Invalid JSON');
  24. }
  25. if (!is_string($input->type)) {
  26. throw new Exception('Invalid JSON');
  27. }
  28. // Check to see if it's a FeatureCollection
  29. if ($input->type == 'FeatureCollection') {
  30. $geoms = array();
  31. foreach ($input->features as $feature) {
  32. $geoms[] = $this->read($feature);
  33. }
  34. return geoPHP::geometryReduce($geoms);
  35. }
  36. // Check to see if it's a Feature
  37. if ($input->type == 'Feature') {
  38. return $this->read($input->geometry);
  39. }
  40. // It's a geometry - process it
  41. return $this->objToGeom($input);
  42. }
  43. private function objToGeom($obj) {
  44. $type = $obj->type;
  45. if ($type == 'GeometryCollection') {
  46. return $this->objToGeometryCollection($obj);
  47. }
  48. $method = 'arrayTo' . $type;
  49. return $this->$method($obj->coordinates);
  50. }
  51. private function arrayToPoint($array) {
  52. return new Point($array[0], $array[1]);
  53. }
  54. private function arrayToLineString($array) {
  55. $points = array();
  56. foreach ($array as $comp_array) {
  57. $points[] = $this->arrayToPoint($comp_array);
  58. }
  59. return new LineString($points);
  60. }
  61. private function arrayToPolygon($array) {
  62. $lines = array();
  63. foreach ($array as $comp_array) {
  64. $lines[] = $this->arrayToLineString($comp_array);
  65. }
  66. return new Polygon($lines);
  67. }
  68. private function arrayToMultiPoint($array) {
  69. $points = array();
  70. foreach ($array as $comp_array) {
  71. $points[] = $this->arrayToPoint($comp_array);
  72. }
  73. return new MultiPoint($points);
  74. }
  75. private function arrayToMultiLineString($array) {
  76. $lines = array();
  77. foreach ($array as $comp_array) {
  78. $lines[] = $this->arrayToLineString($comp_array);
  79. }
  80. return new MultiLineString($lines);
  81. }
  82. private function arrayToMultiPolygon($array) {
  83. $polys = array();
  84. foreach ($array as $comp_array) {
  85. $polys[] = $this->arrayToPolygon($comp_array);
  86. }
  87. return new MultiPolygon($polys);
  88. }
  89. private function objToGeometryCollection($obj) {
  90. $geoms = array();
  91. if (empty($obj->geometries)) {
  92. throw new Exception('Invalid GeoJSON: GeometryCollection with no component geometries');
  93. }
  94. foreach ($obj->geometries as $comp_object) {
  95. $geoms[] = $this->objToGeom($comp_object);
  96. }
  97. return new GeometryCollection($geoms);
  98. }
  99. /**
  100. * Serializes an object into a geojson string
  101. *
  102. *
  103. * @param Geometry $obj The object to serialize
  104. *
  105. * @return string The GeoJSON string
  106. */
  107. public function write(Geometry $geometry, $return_array = FALSE) {
  108. if ($return_array) {
  109. return $this->getArray($geometry);
  110. }
  111. else {
  112. return json_encode($this->getArray($geometry));
  113. }
  114. }
  115. public function getArray($geometry) {
  116. if ($geometry->getGeomType() == 'GeometryCollection') {
  117. $component_array = array();
  118. foreach ($geometry->components as $component) {
  119. $component_array[] = array(
  120. 'type' => $component->geometryType(),
  121. 'coordinates' => $component->asArray(),
  122. );
  123. }
  124. return array(
  125. 'type'=> 'GeometryCollection',
  126. 'geometries'=> $component_array,
  127. );
  128. }
  129. else return array(
  130. 'type'=> $geometry->getGeomType(),
  131. 'coordinates'=> $geometry->asArray(),
  132. );
  133. }
  134. }