diff --git a/lib/geometry/Collection.class.php b/lib/geometry/Collection.class.php index b247c1e7..1b4ee505 100644 --- a/lib/geometry/Collection.class.php +++ b/lib/geometry/Collection.class.php @@ -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') { @@ -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') { @@ -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(); } diff --git a/lib/geometry/Geometry.class.php b/lib/geometry/Geometry.class.php index 03e4505d..db76f517 100644 --- a/lib/geometry/Geometry.class.php +++ b/lib/geometry/Geometry.class.php @@ -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()); } diff --git a/lib/geometry/LineString.class.php b/lib/geometry/LineString.class.php index b3e14c66..f7bb34c2 100644 --- a/lib/geometry/LineString.class.php +++ b/lib/geometry/LineString.class.php @@ -55,6 +55,7 @@ public function pointN($n) { } public function dimension() { + if ($this->isEmpty()) return 0; return 1; } diff --git a/lib/geometry/Polygon.class.php b/lib/geometry/Polygon.class.php index 8cca643e..10c369ef 100644 --- a/lib/geometry/Polygon.class.php +++ b/lib/geometry/Polygon.class.php @@ -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(); } @@ -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()); } @@ -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; } diff --git a/tests/input/an_empty_polygon.wkt b/tests/input/an_empty_polygon.wkt new file mode 100644 index 00000000..ecaad4f7 --- /dev/null +++ b/tests/input/an_empty_polygon.wkt @@ -0,0 +1 @@ +POLYGON EMPTY diff --git a/tests/input/polygon4.wkt b/tests/input/polygon4.wkt new file mode 100644 index 00000000..a713e7d6 --- /dev/null +++ b/tests/input/polygon4.wkt @@ -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))