You are here

public function Collection::getBBox in geoPHP 8

Same name and namespace in other branches
  1. 7 geoPHP/lib/geometry/Collection.class.php \Collection::getBBox()

Overrides Geometry::getBBox

File

geoPHP/lib/geometry/Collection.class.php, line 61

Class

Collection
Collection: Abstract class for compound geometries

Code

public function getBBox() {
  if ($this
    ->isEmpty()) {
    return NULL;
  }
  if ($this
    ->geos()) {
    $envelope = $this
      ->geos()
      ->envelope();
    if ($envelope
      ->typeName() == 'Point') {
      return geoPHP::geosToGeometry($envelope)
        ->getBBOX();
    }
    $geos_ring = $envelope
      ->exteriorRing();
    return array(
      'maxy' => $geos_ring
        ->pointN(3)
        ->getY(),
      'miny' => $geos_ring
        ->pointN(1)
        ->getY(),
      'maxx' => $geos_ring
        ->pointN(1)
        ->getX(),
      'minx' => $geos_ring
        ->pointN(3)
        ->getX(),
    );
  }

  // Go through each component and get the max and min x and y
  $i = 0;
  foreach ($this->components as $component) {
    $component_bbox = $component
      ->getBBox();

    // On the first run through, set the bbox to the component bbox
    if ($i == 0) {
      $maxx = $component_bbox['maxx'];
      $maxy = $component_bbox['maxy'];
      $minx = $component_bbox['minx'];
      $miny = $component_bbox['miny'];
    }

    // Do a check and replace on each boundary, slowly growing the bbox
    $maxx = $component_bbox['maxx'] > $maxx ? $component_bbox['maxx'] : $maxx;
    $maxy = $component_bbox['maxy'] > $maxy ? $component_bbox['maxy'] : $maxy;
    $minx = $component_bbox['minx'] < $minx ? $component_bbox['minx'] : $minx;
    $miny = $component_bbox['miny'] < $miny ? $component_bbox['miny'] : $miny;
    $i++;
  }
  return array(
    'maxy' => $maxy,
    'miny' => $miny,
    'maxx' => $maxx,
    'minx' => $minx,
  );
}