View source
<?php
include_once "lib/adapters/GeoAdapter.class.php";
include_once "lib/adapters/GeoJSON.class.php";
include_once "lib/adapters/WKT.class.php";
include_once "lib/adapters/EWKT.class.php";
include_once "lib/adapters/WKB.class.php";
include_once "lib/adapters/EWKB.class.php";
include_once "lib/adapters/KML.class.php";
include_once "lib/adapters/GPX.class.php";
include_once "lib/adapters/GeoRSS.class.php";
include_once "lib/adapters/GoogleGeocode.class.php";
include_once "lib/adapters/GeoHash.class.php";
include_once "lib/geometry/Geometry.class.php";
include_once "lib/geometry/Point.class.php";
include_once "lib/geometry/Collection.class.php";
include_once "lib/geometry/LineString.class.php";
include_once "lib/geometry/MultiPoint.class.php";
include_once "lib/geometry/Polygon.class.php";
include_once "lib/geometry/MultiLineString.class.php";
include_once "lib/geometry/MultiPolygon.class.php";
include_once "lib/geometry/GeometryCollection.class.php";
class geoPHP {
static function version() {
return '1.2';
}
static function load() {
$args = func_get_args();
$data = array_shift($args);
$type = array_shift($args);
if (empty($data)) {
return null;
}
$type_map = geoPHP::getAdapterMap();
if (!$type) {
if (is_object($data)) {
if ($data instanceof Geometry) {
return $data;
}
}
$detected = geoPHP::detectFormat($data);
if (!$detected) {
return FALSE;
}
$format = explode(':', $detected);
$type = array_shift($format);
$args = $format;
}
$processor_type = $type_map[$type];
if (!$processor_type) {
throw new exception('geoPHP could not find an adapter of type ' . htmlentities($type));
exit;
}
$processor = new $processor_type();
if (!is_array($data)) {
$result = call_user_func_array(array(
$processor,
"read",
), array_merge(array(
$data,
), $args));
}
else {
$geoms = array();
foreach ($data as $item) {
$geoms[] = call_user_func_array(array(
$processor,
"read",
), array_merge(array(
$item,
), $args));
}
$result = geoPHP::geometryReduce($geoms);
}
return $result;
}
static function getAdapterMap() {
return array(
'wkt' => 'WKT',
'ewkt' => 'EWKT',
'wkb' => 'WKB',
'ewkb' => 'EWKB',
'json' => 'GeoJSON',
'geojson' => 'GeoJSON',
'kml' => 'KML',
'gpx' => 'GPX',
'georss' => 'GeoRSS',
'google_geocode' => 'GoogleGeocode',
'geohash' => 'GeoHash',
);
}
static function geometryList() {
return array(
'point' => 'Point',
'linestring' => 'LineString',
'polygon' => 'Polygon',
'multipoint' => 'MultiPoint',
'multilinestring' => 'MultiLineString',
'multipolygon' => 'MultiPolygon',
'geometrycollection' => 'GeometryCollection',
);
}
static function geosInstalled($force = NULL) {
static $geos_installed = NULL;
if ($force !== NULL) {
$geos_installed = $force;
}
if ($geos_installed !== NULL) {
return $geos_installed;
}
$geos_installed = class_exists('GEOSGeometry');
return $geos_installed;
}
static function geosToGeometry($geos) {
if (!geoPHP::geosInstalled()) {
return NULL;
}
$wkb_writer = new GEOSWKBWriter();
$wkb = $wkb_writer
->writeHEX($geos);
$geometry = geoPHP::load($wkb, 'wkb', TRUE);
if ($geometry) {
$geometry
->setGeos($geos);
return $geometry;
}
}
static function geometryReduce($geometry) {
if (is_array($geometry)) {
if (empty($geometry)) {
return FALSE;
}
if (count($geometry) == 1) {
return geoPHP::geometryReduce(array_shift($geometry));
}
}
if (gettype($geometry) == 'object') {
$passbacks = array(
'Point',
'LineString',
'Polygon',
);
if (in_array($geometry
->geometryType(), $passbacks)) {
return $geometry;
}
}
if (gettype($geometry) == 'object') {
$simple_collections = array(
'MultiPoint',
'MultiLineString',
'MultiPolygon',
);
if (in_array(get_class($geometry), $passbacks)) {
$components = $geometry
->getComponents();
if (count($components) == 1) {
return $components[0];
}
else {
return $geometry;
}
}
}
if (!is_array($geometry)) {
$geometry = array(
$geometry,
);
}
$geometries = array();
$geom_types = array();
$collections = array(
'MultiPoint',
'MultiLineString',
'MultiPolygon',
'GeometryCollection',
);
foreach ($geometry as $item) {
if ($item) {
if (in_array(get_class($item), $collections)) {
foreach ($item
->getComponents() as $component) {
$geometries[] = $component;
$geom_types[] = $component
->geometryType();
}
}
else {
$geometries[] = $item;
$geom_types[] = $item
->geometryType();
}
}
}
$geom_types = array_unique($geom_types);
if (empty($geom_types)) {
return FALSE;
}
if (count($geom_types) == 1) {
if (count($geometries) == 1) {
return $geometries[0];
}
else {
$class = 'Multi' . $geom_types[0];
return new $class($geometries);
}
}
else {
return new GeometryCollection($geometries);
}
}
static function detectFormat(&$input) {
$mem = fopen('php://memory', 'r+');
fwrite($mem, $input, 11);
fseek($mem, 0);
$bytes = unpack("c*", fread($mem, 11));
if (empty($bytes)) {
return FALSE;
}
if ($bytes[1] == 9 || $bytes[1] == 10 || $bytes[1] == 32) {
$ltinput = ltrim($input);
return geoPHP::detectFormat($ltinput);
}
if ($bytes[1] == 1) {
if ($bytes[5]) {
return 'ewkb';
}
else {
return 'wkb';
}
}
if ($bytes[1] == 48 && $bytes[2] == 49) {
if (strlen($input) >= 18) {
return 'ewkb:1';
}
}
if ($bytes[1] == 123) {
return 'json';
}
if ($bytes[1] == 83) {
return 'ewkt';
}
$wkt_chars = array(
80,
76,
77,
71,
);
if (in_array($bytes[1], $wkt_chars)) {
return 'wkt';
}
if ($bytes[1] == 60) {
$string = substr($input, 0, 256);
if (strpos($string, '<kml') !== FALSE) {
return 'kml';
}
if (strpos($string, '<coordinate') !== FALSE) {
return 'kml';
}
if (strpos($string, '<gpx') !== FALSE) {
return 'gpx';
}
if (strpos($string, '<georss') !== FALSE) {
return 'georss';
}
if (strpos($string, '<rss') !== FALSE) {
return 'georss';
}
if (strpos($string, '<feed') !== FALSE) {
return 'georss';
}
}
fseek($mem, 0);
$string = trim(fread($mem, 8));
preg_match('/[a-z0-9]+/', $string, $matches);
if ($matches[0] == $string) {
return 'geohash';
}
return FALSE;
}
}