geoPHP.inc 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <?php
  2. /*
  3. * (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. // Adapters
  10. include_once("lib/adapters/GeoAdapter.class.php"); // Abtract class
  11. include_once("lib/adapters/GeoJSON.class.php");
  12. include_once("lib/adapters/WKT.class.php");
  13. include_once("lib/adapters/EWKT.class.php");
  14. include_once("lib/adapters/WKB.class.php");
  15. include_once("lib/adapters/EWKB.class.php");
  16. include_once("lib/adapters/KML.class.php");
  17. include_once("lib/adapters/GPX.class.php");
  18. include_once("lib/adapters/GeoRSS.class.php");
  19. include_once("lib/adapters/GoogleGeocode.class.php");
  20. include_once("lib/adapters/GeoHash.class.php");
  21. // Geometries
  22. include_once("lib/geometry/Geometry.class.php"); // Abtract class
  23. include_once("lib/geometry/Point.class.php");
  24. include_once("lib/geometry/Collection.class.php"); // Abtract class
  25. include_once("lib/geometry/LineString.class.php");
  26. include_once("lib/geometry/MultiPoint.class.php");
  27. include_once("lib/geometry/Polygon.class.php");
  28. include_once("lib/geometry/MultiLineString.class.php");
  29. include_once("lib/geometry/MultiPolygon.class.php");
  30. include_once("lib/geometry/GeometryCollection.class.php");
  31. class geoPHP
  32. {
  33. static function version() {
  34. return '1.1';
  35. }
  36. // geoPHP::load($data, $type, $other_args);
  37. // if $data is an array, all passed in values will be combined into a single geometry
  38. static function load() {
  39. $args = func_get_args();
  40. $data = array_shift($args);
  41. $type = array_shift($args);
  42. $type_map = geoPHP::getAdapterMap();
  43. // Auto-detect type if needed
  44. if (!$type) {
  45. // If the user is trying to load a Geometry from a Geometry... Just pass it back
  46. if (is_object($data)) {
  47. if ($data instanceOf Geometry) return $data;
  48. }
  49. $format = explode(':', geoPHP::detectFormat($data));
  50. $type = array_shift($format);
  51. $args = $format;
  52. }
  53. $processor_type = $type_map[$type];
  54. if (!$processor_type) {
  55. throw new exception('geoPHP could not find an adapter of type '.htmlentities($type));
  56. exit;
  57. }
  58. $processor = new $processor_type();
  59. // Data is not an array, just pass it normally
  60. if (!is_array($data)) {
  61. $result = call_user_func_array(array($processor, "read"), array_merge(array($data), $args));
  62. }
  63. // Data is an array, combine all passed in items into a single geomtetry
  64. else {
  65. $geoms = array();
  66. foreach ($data as $item) {
  67. $geoms[] = call_user_func_array(array($processor, "read"), array_merge(array($item), $args));
  68. }
  69. $result = geoPHP::geometryReduce($geoms);
  70. }
  71. return $result;
  72. }
  73. static function getAdapterMap() {
  74. return array (
  75. 'wkt' => 'WKT',
  76. 'ewkt' => 'EWKT',
  77. 'wkb' => 'WKB',
  78. 'ewkb' => 'EWKB',
  79. 'json' => 'GeoJSON',
  80. 'kml' => 'KML',
  81. 'gpx' => 'GPX',
  82. 'georss' => 'GeoRSS',
  83. 'google_geocode' => 'GoogleGeocode',
  84. 'geohash' => 'GeoHash',
  85. );
  86. }
  87. static function geometryList() {
  88. return array(
  89. 'point' => 'Point',
  90. 'linestring' => 'LineString',
  91. 'polygon' => 'Polygon',
  92. 'multipoint' => 'MultiPoint',
  93. 'multilinestring' => 'MultiLineString',
  94. 'multipolygon' => 'MultiPolygon',
  95. 'geometrycollection' => 'GeometryCollection',
  96. );
  97. }
  98. static function geosInstalled($force = NULL) {
  99. static $geos_installed = NULL;
  100. if ($force !== NULL) $geos_installed = $force;
  101. if ($geos_installed !== NULL) {
  102. return $geos_installed;
  103. }
  104. $geos_installed = class_exists('GEOSGeometry');
  105. return $geos_installed;
  106. }
  107. static function geosToGeometry($geos) {
  108. if (!geoPHP::geosInstalled()) {
  109. return NULL;
  110. }
  111. $wkb_writer = new GEOSWKBWriter();
  112. $wkb = $wkb_writer->writeHEX($geos);
  113. $geometry = geoPHP::load($wkb, 'wkb', TRUE);
  114. if ($geometry) {
  115. $geometry->setGeos($geos);
  116. return $geometry;
  117. }
  118. }
  119. // Reduce a geometry, or an array of geometries, into their 'lowest' available common geometry.
  120. // For example a GeometryCollection of only points will become a MultiPoint
  121. // A multi-point containing a single point will return a point.
  122. // An array of geometries can be passed and they will be compiled into a single geometry
  123. static function geometryReduce($geometry) {
  124. // If it's an array of one, then just parse the one
  125. if (is_array($geometry)) {
  126. if (count($geometry) == 1) return geoPHP::geometryReduce($geometry[0]);
  127. }
  128. // If the geometry cannot even theoretically be reduced more, then pass it back
  129. if (gettype($geometry) == 'object') {
  130. $passbacks = array('Point','LineString','Polygon');
  131. if (in_array($geometry->geometryType(),$passbacks)) {
  132. return $geometry;
  133. }
  134. }
  135. // If it is a mutlti-geometry, check to see if it just has one member
  136. // If it does, then pass the member, if not, then just pass back the geometry
  137. if (gettype($geometry) == 'object') {
  138. $simple_collections = array('MultiPoint','MultiLineString','MultiPolygon');
  139. if (in_array(get_class($geometry),$passbacks)) {
  140. $components = $geometry->getComponents();
  141. if (count($components) == 1) {
  142. return $components[0];
  143. }
  144. else {
  145. return $geometry;
  146. }
  147. }
  148. }
  149. // So now we either have an array of geometries, a GeometryCollection, or an array of GeometryCollections
  150. if (!is_array($geometry)) {
  151. $geometry = array($geometry);
  152. }
  153. $geometries = array();
  154. $geom_types = array();
  155. $collections = array('MultiPoint','MultiLineString','MultiPolygon','GeometryCollection');
  156. foreach ($geometry as $item) {
  157. if (in_array(get_class($item), $collections)) {
  158. foreach ($item->getComponents() as $component) {
  159. $geometries[] = $component;
  160. $geom_types[] = $component->geometryType();
  161. }
  162. }
  163. else {
  164. $geometries[] = $item;
  165. $geom_types[] = $item->geometryType();
  166. }
  167. }
  168. $geom_types = array_unique($geom_types);
  169. if (count($geom_types) == 1) {
  170. if (count($geometries) == 1) {
  171. return $geometries[0];
  172. }
  173. else {
  174. $class = 'Multi'.$geom_types[0];
  175. return new $class($geometries);
  176. }
  177. }
  178. else {
  179. return new GeometryCollection($geometries);
  180. }
  181. }
  182. // Detect a format given a value. This function is meant to be SPEEDY.
  183. // It could make a mistake in XML detection if you are mixing or using namespaces in weird ways (ie, KML inside an RSS feed)
  184. static function detectFormat(&$input) {
  185. $mem = fopen('php://memory', 'r+');
  186. fwrite($mem, $input, 11); // Write 11 bytes - we can detect the vast majority of formats in the first 11 bytes
  187. fseek($mem, 0);
  188. $bytes = unpack("c*", fread($mem, 11));
  189. // If bytes is empty, then we were passed empty input
  190. if (empty($bytes)) return FALSE;
  191. // First char is a tab, space or carriage-return. trim it and try again
  192. if ($bytes[1] == 9 || $bytes[1] == 10 || $bytes[1] == 32) {
  193. return geoPHP::detectFormat(ltrim($input));
  194. }
  195. // Detect WKB or EWKB -- first byte is 1 (little endian indicator)
  196. if ($bytes[1] == 1) {
  197. // If SRID byte is TRUE (1), it's EWKB
  198. if ($bytes[5]) return 'ewkb';
  199. else return 'wkb';
  200. }
  201. // Detect HEX encoded WKB or EWKB (PostGIS format) -- first byte is 48, second byte is 49 (hex '01' => first-byte = 1)
  202. if ($bytes[1] == 48 && $bytes[2] == 49) {
  203. // The shortest possible WKB string (LINESTRING EMPTY) is 18 hex-chars (9 encoded bytes) long
  204. // This differentiates it from a geohash, which is always shorter than 18 characters.
  205. if (strlen($input) >= 18) {
  206. //@@TODO: Differentiate between EWKB and WKB -- check hex-char 10 or 11 (SRID bool indicator at encoded byte 5)
  207. return 'ewkb:1';
  208. }
  209. }
  210. // Detect GeoJSON - first char starts with {
  211. if ($bytes[1] == 123) {
  212. return 'json';
  213. }
  214. // Detect EWKT - first char is S
  215. if ($bytes[1] == 83) {
  216. return 'ewkt';
  217. }
  218. // Detect WKT - first char starts with P (80), L (76), M (77), or G (71)
  219. $wkt_chars = array(80, 76, 77, 71);
  220. if (in_array($bytes[1], $wkt_chars)) {
  221. return 'wkt';
  222. }
  223. // Detect XML -- first char is <
  224. if ($bytes[1] == 60) {
  225. // grab the first 256 characters
  226. $string = substr($input, 0, 256);
  227. if (strpos($string, '<kml') !== FALSE) return 'kml';
  228. if (strpos($string, '<coordinate') !== FALSE) return 'kml';
  229. if (strpos($string, '<gpx') !== FALSE) return 'gpx';
  230. if (strpos($string, '<georss') !== FALSE) return 'georss';
  231. if (strpos($string, '<rss') !== FALSE) return 'georss';
  232. if (strpos($string, '<feed') !== FALSE) return 'georss';
  233. }
  234. // We need an 8 byte string for geohash and unpacked WKB / WKT
  235. fseek($mem, 0);
  236. $string = trim(fread($mem, 8));
  237. // Detect geohash - geohash ONLY contains lowercase chars and numerics
  238. preg_match('/[a-z0-9]+/', $string, $matches);
  239. if ($matches[0] == $string) {
  240. return 'geohash';
  241. }
  242. // What do you get when you cross an elephant with a rhino?
  243. // http://youtu.be/RCBn5J83Poc
  244. return FALSE;
  245. }
  246. }