GeoJSON.class.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. if (!empty($array)) {
  53. return new Point($array[0], $array[1]);
  54. }
  55. else {
  56. return new Point();
  57. }
  58. }
  59. private function arrayToLineString($array) {
  60. $points = array();
  61. foreach ($array as $comp_array) {
  62. $points[] = $this->arrayToPoint($comp_array);
  63. }
  64. return new LineString($points);
  65. }
  66. private function arrayToPolygon($array) {
  67. $lines = array();
  68. foreach ($array as $comp_array) {
  69. $lines[] = $this->arrayToLineString($comp_array);
  70. }
  71. return new Polygon($lines);
  72. }
  73. private function arrayToMultiPoint($array) {
  74. $points = array();
  75. foreach ($array as $comp_array) {
  76. $points[] = $this->arrayToPoint($comp_array);
  77. }
  78. return new MultiPoint($points);
  79. }
  80. private function arrayToMultiLineString($array) {
  81. $lines = array();
  82. foreach ($array as $comp_array) {
  83. $lines[] = $this->arrayToLineString($comp_array);
  84. }
  85. return new MultiLineString($lines);
  86. }
  87. private function arrayToMultiPolygon($array) {
  88. $polys = array();
  89. foreach ($array as $comp_array) {
  90. $polys[] = $this->arrayToPolygon($comp_array);
  91. }
  92. return new MultiPolygon($polys);
  93. }
  94. private function objToGeometryCollection($obj) {
  95. $geoms = array();
  96. if (empty($obj->geometries)) {
  97. throw new Exception('Invalid GeoJSON: GeometryCollection with no component geometries');
  98. }
  99. foreach ($obj->geometries as $comp_object) {
  100. $geoms[] = $this->objToGeom($comp_object);
  101. }
  102. return new GeometryCollection($geoms);
  103. }
  104. /**
  105. * Serializes an object into a geojson string
  106. *
  107. *
  108. * @param Geometry $obj The object to serialize
  109. *
  110. * @return string The GeoJSON string
  111. */
  112. public function write(Geometry $geometry, $return_array = FALSE) {
  113. if ($return_array) {
  114. return $this->getArray($geometry);
  115. }
  116. else {
  117. return json_encode($this->getArray($geometry));
  118. }
  119. }
  120. public function getArray($geometry) {
  121. if ($geometry->getGeomType() == 'GeometryCollection') {
  122. $component_array = array();
  123. foreach ($geometry->components as $component) {
  124. $component_array[] = array(
  125. 'type' => $component->geometryType(),
  126. 'coordinates' => $component->asArray(),
  127. );
  128. }
  129. return array(
  130. 'type'=> 'GeometryCollection',
  131. 'geometries'=> $component_array,
  132. );
  133. }
  134. else return array(
  135. 'type'=> $geometry->getGeomType(),
  136. 'coordinates'=> $geometry->asArray(),
  137. );
  138. }
  139. }