You are here

static function geoPHP::geometryReduce in geoPHP 7

Same name and namespace in other branches
  1. 8 geoPHP/geoPHP.inc \geoPHP::geometryReduce()
6 calls to geoPHP::geometryReduce()
Collection::boundary in geoPHP/lib/geometry/Collection.class.php
GeoJSON::read in geoPHP/lib/adapters/GeoJSON.class.php
Given an object or a string, return a Geometry
geoPHP::load in geoPHP/geoPHP.inc
GeoRSS::geomFromXML in geoPHP/lib/adapters/GeoRSS.class.php
GPX::geomFromXML in geoPHP/lib/adapters/GPX.class.php

... See full list

File

geoPHP/geoPHP.inc, line 152

Class

geoPHP

Code

static function geometryReduce($geometry) {

  // If it's an array of one, then just parse the one
  if (is_array($geometry)) {
    if (empty($geometry)) {
      return FALSE;
    }
    if (count($geometry) == 1) {
      return geoPHP::geometryReduce(array_shift($geometry));
    }
  }

  // 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 multi-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') {
    $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;
      }
    }
  }

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