Skip to content

Commit

Permalink
Finishing up support for empty geometries
Browse files Browse the repository at this point in the history
  • Loading branch information
phayes committed Apr 17, 2012
1 parent 920edec commit 851606d
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 1 deletion.
6 changes: 6 additions & 0 deletions lib/geometry/Collection.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public function getComponents() {
}

public function centroid() {
if ($this->isEmpty()) return NULL;

if ($this->geos()) {
$geos_centroid = $this->geos()->centroid();
if ($geos_centroid->typeName() == 'Point') {
Expand All @@ -57,6 +59,8 @@ public function centroid() {
}

public function getBBox() {
if ($this->isEmpty()) return NULL;

if ($this->geos()) {
$envelope = $this->geos()->envelope();
if ($envelope->typeName() == 'Point') {
Expand Down Expand Up @@ -123,6 +127,8 @@ public function area() {

// By default, the boundary of a collection is the boundary of it's components
public function boundary() {
if ($this->isEmpty()) return new LineString();

if ($this->geos()) {
return $this->geos()->boundary();
}
Expand Down
2 changes: 2 additions & 0 deletions lib/geometry/Geometry.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public function setSRID($srid) {
}

public function envelope() {
if ($this->isEmpty()) return new Polygon();

if ($this->geos()) {
return geoPHP::geosToGeometry($this->geos()->envelope());
}
Expand Down
1 change: 1 addition & 0 deletions lib/geometry/LineString.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public function pointN($n) {
}

public function dimension() {
if ($this->isEmpty()) return 0;
return 1;
}

Expand Down
9 changes: 8 additions & 1 deletion lib/geometry/Polygon.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class Polygon extends Collection
protected $geom_type = 'Polygon';

public function area($exterior_only = FALSE, $signed = FALSE) {
if ($this->isEmpty()) return 0;

if ($this->geos() && $exterior_only == FALSE) {
return $this->geos()->area();
}
Expand Down Expand Up @@ -40,6 +42,8 @@ public function area($exterior_only = FALSE, $signed = FALSE) {
}

public function centroid() {
if ($this->isEmpty()) return NULL;

if ($this->geos()) {
return geoPHP::geosToGeometry($this->geos()->centroid());
}
Expand Down Expand Up @@ -72,18 +76,21 @@ public function centroid() {
}

public function exteriorRing() {
if ($this->isEmpty()) return new LineString();
return $this->components[0];
}

public function numInteriorRings() {
if ($this->isEmpty()) return 0;
return $this->numGeometries()-1;
}

public function interiorRingN($n) {
return $this->geometryN($n+1);
}

public function dimension() {
if ($this->isEmpty()) return 0;
return 2;
}

Expand Down
1 change: 1 addition & 0 deletions tests/input/an_empty_polygon.wkt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
POLYGON EMPTY
1 change: 1 addition & 0 deletions tests/input/polygon4.wkt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
POLYGON((4.8352495472368009 52.3561217600921438,4.8354139113045580 52.3561243429663534,4.8356082266282945 52.3561267417385281,4.8358010085903622 52.3561273083083663,4.8358010085903622 52.3561273083083663,4.8358035242637225 52.3559935212917722,4.8363777606561538 52.3559985348227173,4.8365863082998608 52.3560003600829731,4.8365523717335313 52.3570990145454189,4.8365884597636066 52.3572643433297529,4.8366320506970659 52.3574639095218686,4.8366736405531485 52.3576544056339870,4.8367264446828226 52.3578947700094304,4.8367922739966023 52.3581940807800450,4.8368228816936947 52.3583326871276356,4.8368228816936947 52.3583326871276356,4.8346348012064322 52.3583075969840550,4.8346348012064322 52.3583075969840550,4.8346348010943823 52.3583076059723282,4.8346348010943823 52.3583076059723282,4.8344931735728114 52.3583059732702338,4.8343773230572911 52.3583046496785585,4.8342182417472204 52.3583028092031384,4.8340047277034000 52.3583004442080195,4.8340047277034000 52.3583004442080195,4.8340047286008216 52.3583003723016063,4.8340047286008216 52.3583003723016063,4.8333843154510516 52.3582932434377639,4.8333843154510516 52.3582932434377639,4.8333915914677918 52.3580669388087898,4.8333968982183286 52.3578913129544787,4.8334415565569193 52.3563602568407660,4.8336003450092706 52.3563614767834267,4.8336013166539615 52.3563318721204567,4.8336013166539615 52.3563318721204567,4.8339582156582548 52.3563361223319603,4.8339656498645338 52.3561015845598732,4.8340692910524092 52.3561032110135258,4.8340692910524092 52.3561032110135258,4.8345511248958477 52.3561107854074095,4.8345511248958477 52.3561107854074095,4.8345513450958055 52.3561107864365809,4.8345513450958055 52.3561107864365809,4.8346742584771087 52.3561127181661092,4.8346742584771087 52.3561127181661092,4.8347750227755597 52.3561143035917596,4.8347750227755597 52.3561143035917596,4.8352495472368009 52.3561217600921438))

0 comments on commit 851606d

Please sign in to comment.