You are here

public function Collection::equals in geoPHP 8

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

Overrides Geometry::equals

File

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

Class

Collection
Collection: Abstract class for compound geometries

Code

public function equals($geometry) {
  if ($this
    ->geos()) {
    return $this
      ->geos()
      ->equals($geometry
      ->geos());
  }

  // To test for equality we check to make sure that there is a matching point
  // in the other geometry for every point in this geometry.
  // This is slightly more strict than the standard, which
  // uses Within(A,B) = true and Within(B,A) = true
  // @@TODO: Eventually we could fix this by using some sort of simplification
  // method that strips redundant vertices (that are all in a row)
  $this_points = $this
    ->getPoints();
  $other_points = $geometry
    ->getPoints();

  // First do a check to make sure they have the same number of vertices
  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;
    }
  }

  // All points match, return TRUE
  return TRUE;
}