geoPHP.inc 9.7 KB

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