Skip to content

Commit

Permalink
Fixing multi-part loads
Browse files Browse the repository at this point in the history
  • Loading branch information
phayes committed Aug 20, 2012
1 parent ead1376 commit d595cd4
Showing 1 changed file with 25 additions and 25 deletions.
50 changes: 25 additions & 25 deletions geoPHP.inc
Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,30 @@ include_once("lib/geometry/GeometryCollection.class.php");

class geoPHP
{

static function version() {
return '1.0';
}

// geoPHP::load($data, $type, $other_args);
// if $data is an array, all passed in values will be combined into a single geometry
static function load() {
$args = func_get_args();

$data = array_shift($args);
$type = array_shift($args);

$type_map = geoPHP::getAdapterMap();

$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();

// Data is not an array, just pass it normally
if (!is_array($data)) {
$result = call_user_func_array(array($processor, "read"), array_merge(array($data), $args));
Expand All @@ -64,14 +64,14 @@ class geoPHP
else {
$geoms = array();
foreach ($data as $item) {
$geoms[] = call_user_func_array(array($processor, "read"), array_merge(array($data), $args));
$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',
Expand All @@ -85,7 +85,7 @@ class geoPHP
'google_geocode' => 'GoogleGeocode',
);
}

static function geometryList() {
return array(
'point' => 'Point',
Expand All @@ -97,17 +97,17 @@ class geoPHP
'geometrycollection' => 'GeometryCollection',
);
}

static function geosInstalled($force = NULL) {
static $geos_installed = NULL;
if ($force !== NULL) $geos_installed = $force;
if ($geos_installed !== NULL) {
return $geos_installed;
return $geos_installed;
}
$geos_installed = class_exists('GEOSGeometry');
return $geos_installed;
}

static function geosToGeometry($geos) {
if (!geoPHP::geosInstalled()) {
return NULL;
Expand All @@ -120,7 +120,7 @@ class geoPHP
return $geometry;
}
}

// Reduce a geometry, or an array of geometries, into their 'lowest' available common geometry.
// For example a GeometryCollection of only points will become a MultiPoint
// A multi-point containing a single point will return a point.
Expand All @@ -130,15 +130,15 @@ class geoPHP
if (is_array($geometry)) {
if (count($geometry) == 1) return geoPHP::geometryReduce($geometry[0]);
}

// If the geometry cannot even theoretically be reduced more, then pass it back
if (gettype($geometry) == 'object') {
$passbacks = array('Point','LineString','Polygon');
if (in_array($geometry->geometryType(),$passbacks)) {
return $geometry;
}
}

// If it is a mutlti-geometry, check to see if it just has one member
// If it does, then pass the member, if not, then just pass back the geometry
if (gettype($geometry) == 'object') {
Expand All @@ -153,17 +153,17 @@ class geoPHP
}
}
}
// So now we either have an array of geometries, a GeometryCollection, or an array of GeometryCollections

// So now we either have an array of geometries, a GeometryCollection, or an array of GeometryCollections
if (!is_array($geometry)) {
$geometry = array($geometry);
}

$geometries = array();
$geom_types = array();

$collections = array('MultiPoint','MultiLineString','MultiPolygon','GeometryCollection');

foreach ($geometry as $item) {
if (in_array(get_class($item), $collections)) {
foreach ($item->getComponents() as $component) {
Expand All @@ -176,9 +176,9 @@ class geoPHP
$geom_types[] = $item->geometryType();
}
}

$geom_types = array_unique($geom_types);

if (count($geom_types) == 1) {
if (count($geometries) == 1) {
return $geometries[0];
Expand Down

0 comments on commit d595cd4

Please sign in to comment.