Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optionally recursive $geometry->getComponents #74

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 48 additions & 2 deletions lib/geometry/Collection.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,56 @@ public function __construct($components = array()) {
/**
* Returns Collection component geometries
*
* If no parameters are provided it simply returns the array of components stored in this collection
* instance. If the types parameter is provided and contains a list of valid geometries as listed in
* geoPHP::geometryTypes() then the list of components will be further broken down to match the types
* provided. For example, if $geometry->getComponents('Point') is called, then the collection
* will be recursively broken down until all geometries have been broken down into points. A common
* use case may be to call $geometry->getComponents(array('Point', 'LineString', 'Polygon')) to break
* a collection down to the three basic geometry types.
*
* @param types an array of strings matching valid features types as listed in geoPHP::geometryTypes()
* @return array
*/
public function getComponents() {
return $this->components;
public function getComponents($types = array()) {
// If no parameters are provided behave as always and return the components stored in this instance.
if (!$types) {
return $this->components;
}

// If a string is provided as a parameter place it into an array before continuing.
if (is_string($types)) {
$types = array($types);
}

// If the provided parameter is neither a string nor an array do nothing.
if (is_array($types)) {
// Make sure that the types array contains at least the Point geometry type.
if (!in_array('Point', $types)) {
$types[] = 'Point';
}
return $this->getComponentsRecursive($types);
}
}

private function getComponentsRecursive($types = array('Point')) {
// If the current geometry is of a type in the types array we can return it.
if (in_array($this->geometryType(), $types)) {
return array($this);
}

$components = array();
foreach ($this->components as $component) {
// Simply add geometries that match the given types to the return array to avoid calling
// nonexistent getComponentsRecursive method on Point geometries.
if (in_array($component->geometryType(), $types)) {
$components[] = $component;
} else {
$components = array_merge($components, $component->getComponentsRecursive($types));
}
}

return $components;
}

public function centroid() {
Expand Down