[geos-commits] r3031 - in trunk/php: . test

svn_geos at osgeo.org svn_geos at osgeo.org
Sun Jun 20 04:32:29 EDT 2010


Author: strk
Date: 2010-06-20 08:32:29 +0000 (Sun, 20 Jun 2010)
New Revision: 3031

Modified:
   trunk/php/TODO
   trunk/php/geos.c
   trunk/php/test/test.php
Log:
Polygonize (testing need further review for a possible bug in core lib)


Modified: trunk/php/TODO
===================================================================
--- trunk/php/TODO	2010-06-19 21:38:23 UTC (rev 3030)
+++ trunk/php/TODO	2010-06-20 08:32:29 UTC (rev 3031)
@@ -1,5 +1,7 @@
 In order of priority
 
+- Equip GEOSGeometry with standard string output
+  (and make sure it comes out on var_dump)
 - Complete interfaces of Geometry
 - Add interfaces for WKBReader/WKBWriter ?
 - Doxygen based documentation ?

Modified: trunk/php/geos.c
===================================================================
--- trunk/php/geos.c	2010-06-19 21:38:23 UTC (rev 3030)
+++ trunk/php/geos.c	2010-06-20 08:32:29 UTC (rev 3031)
@@ -188,6 +188,7 @@
 PHP_METHOD(Geometry, pointOnSurface); 
 PHP_METHOD(Geometry, centroid); 
 PHP_METHOD(Geometry, relate); 
+PHP_METHOD(Geometry, polygonize); 
 
 PHP_METHOD(Geometry, numGeometries);
 
@@ -206,6 +207,7 @@
     PHP_ME(Geometry, pointOnSurface, NULL, 0)
     PHP_ME(Geometry, centroid, NULL, 0)
     PHP_ME(Geometry, relate, NULL, 0)
+    PHP_ME(Geometry, polygonize, NULL, 0)
 
     PHP_ME(Geometry, numGeometries, NULL, 0)
     {NULL, NULL, NULL}
@@ -215,6 +217,41 @@
 
 static zend_object_handlers Geometry_object_handlers;
 
+/*
+ * Return a newly created array zval containing
+ * a clone of all components of given geometry
+ * NOTE: collection components are not descended into
+ */
+static zval*
+dumpGeometry(GEOSGeometry* g)
+{
+    zval *array;
+    int ngeoms, i;
+
+    MAKE_STD_ZVAL(array);
+    array_init(array);
+
+    ngeoms = GEOSGetNumGeometries(g);
+    for (i=0; i<ngeoms; ++i)
+    {
+        zval *tmp;
+        GEOSGeometry* cc;
+        const GEOSGeometry* c = GEOSGetGeometryN(g, i);
+        if ( ! c ) continue; /* should get an exception */
+        /* we _need_ to clone as this one is owned by 'g' */
+        cc = GEOSGeom_clone(c);
+        if ( ! cc ) continue; /* should get an exception */
+
+        MAKE_STD_ZVAL(tmp);
+        object_init_ex(tmp, Geometry_ce_ptr);
+        setRelay(tmp, cc);
+        add_next_index_zval(array, tmp); 
+    }
+
+    return array;
+}
+
+
 static void
 Geometry_dtor (void *object TSRMLS_DC)
 {
@@ -614,6 +651,65 @@
 
 }
 
+/**
+ * array GEOSGeometry::polygonize()
+ *
+ * The returned array contains the following elements:
+ *
+ *  - 'rings'
+ *      Type: array of GEOSGeometry 
+ *      Rings that can be formed by the costituent
+ *      linework of geometry.
+ *  - 'cut_edges' (optional)
+ *      Type: array of GEOSGeometry 
+ *      Edges which are connected at both ends but
+ *      which do not form part of polygon.
+ *  - 'dangles'
+ *      Type: array of GEOSGeometry 
+ *      Edges which have one or both ends which are
+ *      not incident on another edge endpoint
+ *  - 'invalid_rings' 
+ *      Type: array of GEOSGeometry
+ *      Edges which form rings which are invalid
+ *      (e.g. the component lines contain a self-intersection)
+ *
+ */
+PHP_METHOD(Geometry, polygonize)
+{
+    GEOSGeometry *this;
+    GEOSGeometry *rings;
+    GEOSGeometry *cut_edges;
+    GEOSGeometry *dangles;
+    GEOSGeometry *invalid_rings;
+    zval *rings_array;
+    zval *cut_edges_array;
+    zval *dangles_array;
+    zval *invalid_rings_array;
+
+    this = (GEOSGeometry*)getRelay(getThis(), Geometry_ce_ptr);
+
+    rings = GEOSPolygonize_full(this, &cut_edges, &dangles, &invalid_rings);
+    if ( ! rings ) RETURN_NULL(); /* should get an exception first */
+
+    /* return value should be an array */
+    array_init(return_value);
+
+    rings_array = dumpGeometry(rings);
+    GEOSGeom_destroy(rings);
+    cut_edges_array = dumpGeometry(cut_edges);
+    GEOSGeom_destroy(cut_edges);
+    dangles_array = dumpGeometry(dangles);
+    GEOSGeom_destroy(dangles);
+    invalid_rings_array = dumpGeometry(invalid_rings);
+    GEOSGeom_destroy(invalid_rings);
+
+    add_assoc_zval(return_value, "rings", rings_array); 
+    add_assoc_zval(return_value, "cut_edges", cut_edges_array);
+    add_assoc_zval(return_value, "dangles", dangles_array);
+    add_assoc_zval(return_value, "invalid_rings", invalid_rings_array);
+
+}
+
 /* -- class GEOSWKTReader -------------------- */
 
 PHP_METHOD(WKTReader, __construct);

Modified: trunk/php/test/test.php
===================================================================
--- trunk/php/test/test.php	2010-06-19 21:38:23 UTC (rev 3030)
+++ trunk/php/test/test.php	2010-06-20 08:32:29 UTC (rev 3031)
@@ -894,4 +894,65 @@
         $this->assertEquals(FALSE, $ret);
 
     }
+
+    public function testGeometry_polygonize()
+    {
+        $reader = new GEOSWKTReader();
+        $writer = new GEOSWKTWriter();
+        $writer->setRoundingPrecision(0);
+
+        $g = $reader->read('GEOMETRYCOLLECTION(
+            LINESTRING(0 0, 10 10),
+            LINESTRING(185 221, 100 100),
+            LINESTRING(185 221, 88 275, 180 316),
+            LINESTRING(185 221, 292 281, 180 316),
+            LINESTRING(189 98, 83 187, 185 221),
+            LINESTRING(189 98, 325 168, 185 221))
+            )');
+
+        $g2 = $reader->read('POINT(0 0)');
+        $g = $g->union($g2); /* Make sure linestrings are noded */
+
+        $ret = $g->polygonize();
+
+        /*
+         * NOTE: the following expected results are suspicious
+         *       due to the duplicated dangle and lack of a cut edge
+         */
+
+        //var_dump($ret);
+
+        $this->assertEquals('array', gettype($ret));
+        $this->assertEquals('array', gettype($ret['rings']));
+        $this->assertEquals('array', gettype($ret['cut_edges']));
+        $this->assertEquals('array', gettype($ret['dangles']));
+        $this->assertEquals('array', gettype($ret['invalid_rings']));
+
+        $this->assertEquals(3, count($ret['rings']));
+        $this->assertEquals(
+'POLYGON ((185 221, 132 146, 83 187, 185 221))'
+            , $writer->write($ret['rings'][0]));
+        $this->assertEquals(
+'POLYGON ((132 146, 185 221, 325 168, 189 98, 132 146))'
+            , $writer->write($ret['rings'][1]));
+        $this->assertEquals(
+'POLYGON ((185 221, 88 275, 180 316, 292 281, 185 221))'
+            , $writer->write($ret['rings'][2]));
+
+        $this->assertEquals(0, count($ret['cut_edges']));
+
+        $this->assertEquals(3, count($ret['dangles']));
+        $this->assertEquals(
+'LINESTRING (132 146, 100 100)'
+            , $writer->write($ret['dangles'][0]));
+        $this->assertEquals(
+'LINESTRING (0 0, 10 10)'
+            , $writer->write($ret['dangles'][1]));
+        $this->assertEquals(
+'LINESTRING (0 0, 10 10)'
+            , $writer->write($ret['dangles'][2]));
+
+        $this->assertEquals(0, count($ret['invalid_rings']));
+
+    }
 }



More information about the geos-commits mailing list