Collection.class.php in geoPHP 7
File
geoPHP/lib/geometry/Collection.class.php
View source
<?php
abstract class Collection extends Geometry {
public $components = array();
public function __construct($components = array()) {
if (!is_array($components)) {
throw new Exception("Component geometries must be passed as an array");
}
foreach ($components as $component) {
if ($component instanceof Geometry) {
$this->components[] = $component;
}
else {
throw new Exception("Cannot create a collection with non-geometries");
}
}
}
public function getComponents() {
return $this->components;
}
public function centroid() {
if ($this
->isEmpty()) {
return NULL;
}
if ($this
->geos()) {
$geos_centroid = $this
->geos()
->centroid();
if ($geos_centroid
->typeName() == 'Point') {
return geoPHP::geosToGeometry($this
->geos()
->centroid());
}
}
$centroid = $this
->envelope()
->centroid();
return $centroid;
}
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(),
);
}
$i = 0;
foreach ($this->components as $component) {
$component_bbox = $component
->getBBox();
if ($i == 0) {
$maxx = $component_bbox['maxx'];
$maxy = $component_bbox['maxy'];
$minx = $component_bbox['minx'];
$miny = $component_bbox['miny'];
}
$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,
);
}
public function asArray() {
$array = array();
foreach ($this->components as $component) {
$array[] = $component
->asArray();
}
return $array;
}
public function area() {
if ($this
->geos()) {
return $this
->geos()
->area();
}
$area = 0;
foreach ($this->components as $component) {
$area += $component
->area();
}
return $area;
}
public function boundary() {
if ($this
->isEmpty()) {
return new LineString();
}
if ($this
->geos()) {
return $this
->geos()
->boundary();
}
$components_boundaries = array();
foreach ($this->components as $component) {
$components_boundaries[] = $component
->boundary();
}
return geoPHP::geometryReduce($components_boundaries);
}
public function numGeometries() {
return count($this->components);
}
public function geometryN($n) {
$n = intval($n);
if (array_key_exists($n - 1, $this->components)) {
return $this->components[$n - 1];
}
else {
return NULL;
}
}
public function length() {
$length = 0;
foreach ($this->components as $delta => $component) {
$length += $component
->length();
}
return $length;
}
public function greatCircleLength($radius = 6378137) {
$length = 0;
foreach ($this->components as $component) {
$length += $component
->greatCircleLength($radius);
}
return $length;
}
public function haversineLength() {
$length = 0;
foreach ($this->components as $component) {
$length += $component
->haversineLength();
}
return $length;
}
public function dimension() {
$dimension = 0;
foreach ($this->components as $component) {
if ($component
->dimension() > $dimension) {
$dimension = $component
->dimension();
}
}
return $dimension;
}
public function isEmpty() {
if (!count($this->components)) {
return TRUE;
}
else {
foreach ($this->components as $component) {
if (!$component
->isEmpty()) {
return FALSE;
}
}
return TRUE;
}
}
public function numPoints() {
$num = 0;
foreach ($this->components as $component) {
$num += $component
->numPoints();
}
return $num;
}
public function getPoints() {
$points = array();
foreach ($this->components as $component) {
$points = array_merge($points, $component
->getPoints());
}
return $points;
}
public function equals($geometry) {
if ($this
->geos()) {
return $this
->geos()
->equals($geometry
->geos());
}
$this_points = $this
->getPoints();
$other_points = $geometry
->getPoints();
if (count($this_points) != count($other_points)) {
return FALSE;
}
foreach ($this_points as $point) {
$found_match = FALSE;
foreach ($other_points as $key => $test_point) {
if ($point
->equals($test_point)) {
$found_match = TRUE;
unset($other_points[$key]);
break;
}
}
if (!$found_match) {
return FALSE;
}
}
return TRUE;
}
public function isSimple() {
if ($this
->geos()) {
return $this
->geos()
->isSimple();
}
foreach ($this->components as $component) {
if (!$component
->isSimple()) {
return FALSE;
}
}
return TRUE;
}
public function explode() {
$parts = array();
foreach ($this->components as $component) {
$parts = array_merge($parts, $component
->explode());
}
return $parts;
}
public function x() {
return NULL;
}
public function y() {
return NULL;
}
public function startPoint() {
return NULL;
}
public function endPoint() {
return NULL;
}
public function isRing() {
return NULL;
}
public function isClosed() {
return NULL;
}
public function pointN($n) {
return NULL;
}
public function exteriorRing() {
return NULL;
}
public function numInteriorRings() {
return NULL;
}
public function interiorRingN($n) {
return NULL;
}
public function pointOnSurface() {
return NULL;
}
}
Classes
Name |
Description |
Collection |
Collection: Abstract class for compound geometries |