Skip to content

Commit

Permalink
Adding length3D method - various other 3D cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
phayes committed Sep 3, 2012
1 parent 2c6d6af commit d259866
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 8 deletions.
9 changes: 9 additions & 0 deletions lib/geometry/Collection.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
abstract class Collection extends Geometry
{
public $components = array();
protected $dimention = 2;

/**
* Constructor: Checks and sets component geometries
Expand Down Expand Up @@ -166,6 +167,14 @@ public function length() {
return $length;
}

public function length3D() {
$length = 0;
foreach ($this->components as $delta => $component) {
$length += $component->length3D();
}
return $length;
}

public function greatCircleLength($radius = 6378137) {
$length = 0;
foreach ($this->components as $component) {
Expand Down
8 changes: 2 additions & 6 deletions lib/geometry/Geometry.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ abstract public function area();
abstract public function boundary();
abstract public function centroid();
abstract public function length();

This comment has been minimized.

Copy link
@mkdgs

mkdgs Sep 4, 2012

i think lenght is only for lines (6.1.6.1 http://www.opengeospatial.org/standards/sfa)
and x() y() .. only for point
i will make a pull request soon with this modification

This comment has been minimized.

Copy link
@phayes

phayes Sep 4, 2012

Author Owner

I've been following a convention of making every method an abstract, then implementing a method that returns NULL or 0 (depending circumstances) if it doesn't directly apply..

Why? I can do things like this for the Collections class:

method $length3D() {
$length = 0;
foreach ($this->components as $comp) {
$length += $comp->length3d();
}
return $length;
}

It just works for both MultiLineString and GeometryCollection (and all other collections too). I've been using this pattern everywhere in this library and it's saved a lot of headaches.

This comment has been minimized.

Copy link
@mkdgs

mkdgs Sep 5, 2012

length is only for line, and multiline, i understand why it is in GeometryCollection.

for x() y()... there is no need into GeometryCollection.
if you like autocompletion you would not see a huge list of wrong method.
and it can be the cause of future headaches.

abstract public function length3D();
abstract public function y();
abstract public function x();
abstract public function z();
Expand All @@ -31,6 +32,7 @@ abstract public function exteriorRing();
abstract public function numInteriorRings();
abstract public function interiorRingN($n);
abstract public function dimension();
abstract public function distance($geom);
abstract public function equals($geom);
abstract public function isEmpty();
abstract public function isSimple();
Expand Down Expand Up @@ -311,12 +313,6 @@ public function coveredBy(Geometry $geometry) {
}
}

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

public function hausdorffDistance(Geometry $geometry) {
if ($this->geos()) {
return $this->geos()->hausdorffDistance($geometry->geos());
Expand Down
12 changes: 12 additions & 0 deletions lib/geometry/LineString.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
class LineString extends Collection
{
protected $geom_type = 'LineString';
protected $dimention = 2;

/**
* Constructor
Expand Down Expand Up @@ -75,6 +76,17 @@ public function length() {
return $length;
}

public function length3D() {
$length = 0;
foreach ($this->getPoints() as $delta => $point) {
$previous_point = $this->geometryN($delta);
if ($previous_point) {
$length += sqrt(pow(($previous_point->x() - $point->x()), 2) + pow(($previous_point->y() - $point->y()), 2) + pow(($previous_point->z() - $point->z()), 2));
}
}
return $length;
}

public function greatCircleLength($radius = 6378137) {
$length = 0;
$points = $this->getPoints();
Expand Down
1 change: 1 addition & 0 deletions lib/geometry/MultiLineString.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
class MultiLineString extends Collection
{
protected $geom_type = 'MultiLineString';
protected $dimention = 2;

// MultiLineString is closed if all it's components are closed
public function isClosed() {
Expand Down
1 change: 1 addition & 0 deletions lib/geometry/MultiPoint.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
class MultiPoint extends Collection
{
protected $geom_type = 'MultiPoint';
protected $dimention = 2;

public function numPoints() {
return $this->numGeometries();
Expand Down
3 changes: 2 additions & 1 deletion lib/geometry/MultiPolygon.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
/**
* MultiPolygon: A collection of Polygons
*/
class MultiPolygon extends Collection
class MultiPolygon extends Collection
{
protected $geom_type = 'MultiPolygon';
protected $dimention = 2;
}
5 changes: 5 additions & 0 deletions lib/geometry/Point.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Point extends Geometry
{
public $coords = array(2);
protected $geom_type = 'Point';
protected $dimention = 2;

/**
* Constructor
Expand Down Expand Up @@ -100,6 +101,10 @@ public function length() {
return 0;
}

public function length3D() {
return 0;
}

public function greatCircleLength() {
return 0;
}
Expand Down
4 changes: 3 additions & 1 deletion lib/geometry/Polygon.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
class Polygon extends Collection
{
protected $geom_type = 'Polygon';
protected $dimention = 2;

public function area($exterior_only = FALSE, $signed = FALSE) {
if ($this->isEmpty()) return 0;
Expand Down Expand Up @@ -137,7 +138,8 @@ public function isSimple() {

// Not valid for this geometry type
// --------------------------------
public function length() { return NULL; }
public function length() { return NULL; }
public function length3D() { return NULL; }

}

0 comments on commit d259866

Please sign in to comment.