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

svn_geos at osgeo.org svn_geos at osgeo.org
Sat Jun 19 07:34:09 EDT 2010


Author: strk
Date: 2010-06-19 11:34:09 +0000 (Sat, 19 Jun 2010)
New Revision: 3017

Modified:
   trunk/php/TODO
   trunk/php/geos.c
   trunk/php/test/Makefile.am
   trunk/php/test/test.php
Log:
Complete implementation of WKTWriter interfaces, add phpunit-based testing, add .project interface to Geometry


Modified: trunk/php/TODO
===================================================================
--- trunk/php/TODO	2010-06-19 11:32:49 UTC (rev 3016)
+++ trunk/php/TODO	2010-06-19 11:34:09 UTC (rev 3017)
@@ -1,7 +1,5 @@
 In order of priority
 
-- Complete all interfaces for WKTReader/WKTWriter/Geometry
-- Doxygen based documentation
-- Proper automated testing (phpunit2?)
-
+- Complete interfaces of Geometry
 - Add interfaces for WKBReader/WKBWriter ?
+- Doxygen based documentation ?

Modified: trunk/php/geos.c
===================================================================
--- trunk/php/geos.c	2010-06-19 11:32:49 UTC (rev 3016)
+++ trunk/php/geos.c	2010-06-19 11:34:09 UTC (rev 3017)
@@ -38,7 +38,6 @@
 PHP_RSHUTDOWN_FUNCTION(geos);
 PHP_MINFO_FUNCTION(geos);
 PHP_FUNCTION(GEOSVersion);
-/*PHP_METHOD(WKTReader, write);*/
 
 static function_entry geos_functions[] = {
     PHP_FE(GEOSVersion, NULL)
@@ -150,10 +149,13 @@
 /* -- class GEOSGeometry -------------------- */
 
 PHP_METHOD(Geometry, __construct);
+PHP_METHOD(Geometry, project);
+
 PHP_METHOD(Geometry, numGeometries);
 
 static function_entry Geometry_methods[] = {
     PHP_ME(Geometry, __construct, NULL, 0)
+    PHP_ME(Geometry, project, NULL, 0)
     PHP_ME(Geometry, numGeometries, NULL, 0)
     {NULL, NULL, NULL}
 };
@@ -201,6 +203,27 @@
     RETURN_LONG(ret);
 }
 
+PHP_METHOD(Geometry, project)
+{
+    GEOSGeometry *this;
+    GEOSGeometry *other;
+    zval *zobj;
+    double ret;
+
+    this = (GEOSGeometry*)getRelay(getThis(), Geometry_ce_ptr);
+
+    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "o", &zobj)
+        == FAILURE) {
+        RETURN_NULL();
+    }
+    other = getRelay(zobj, Geometry_ce_ptr);
+
+    ret = GEOSProject(this, other);
+    if ( ret < 0 ) RETURN_NULL(); /* should get an exception first */
+
+    RETURN_DOUBLE(ret);
+}
+
 /* -- class GEOSWKTReader -------------------- */
 
 PHP_METHOD(WKTReader, __construct);
@@ -279,11 +302,17 @@
 PHP_METHOD(WKTWriter, __construct);
 PHP_METHOD(WKTWriter, write);
 PHP_METHOD(WKTWriter, setTrim);
+PHP_METHOD(WKTWriter, setRoundingPrecision);
+PHP_METHOD(WKTWriter, setOutputDimension);
+PHP_METHOD(WKTWriter, setOld3D);
 
 static function_entry WKTWriter_methods[] = {
     PHP_ME(WKTWriter, __construct, NULL, 0)
     PHP_ME(WKTWriter, write, NULL, 0)
     PHP_ME(WKTWriter, setTrim, NULL, 0)
+    PHP_ME(WKTWriter, setRoundingPrecision, NULL, 0)
+    PHP_ME(WKTWriter, setOutputDimension, NULL, 0)
+    PHP_ME(WKTWriter, setOld3D, NULL, 0)
     {NULL, NULL, NULL}
 };
 
@@ -374,6 +403,56 @@
     GEOSWKTWriter_setTrim(writer, trim);
 }
 
+PHP_METHOD(WKTWriter, setRoundingPrecision)
+{
+    GEOSWKTWriter *writer;
+    long int prec;
+
+    writer = (GEOSWKTWriter*)getRelay(getThis(), WKTWriter_ce_ptr);
+
+    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &prec)
+        == FAILURE)
+    {
+        RETURN_NULL();
+    }
+
+    GEOSWKTWriter_setRoundingPrecision(writer, prec);
+}
+
+PHP_METHOD(WKTWriter, setOutputDimension)
+{
+    GEOSWKTWriter *writer;
+    long int dim;
+
+    writer = (GEOSWKTWriter*)getRelay(getThis(), WKTWriter_ce_ptr);
+
+    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &dim)
+        == FAILURE)
+    {
+        RETURN_NULL();
+    }
+
+    GEOSWKTWriter_setOutputDimension(writer, dim);
+}
+
+PHP_METHOD(WKTWriter, setOld3D)
+{
+    GEOSWKTWriter *writer;
+    zend_bool bval;
+    int val;
+
+    writer = (GEOSWKTWriter*)getRelay(getThis(), WKTWriter_ce_ptr);
+
+    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "b", &bval)
+        == FAILURE)
+    {
+        RETURN_NULL();
+    }
+
+    val = bval;
+    GEOSWKTWriter_setOld3D(writer, val);
+}
+
 /* ------ Initialization / Deinitialization / Meta ------------------ */
 
 /* per-module initialization */

Modified: trunk/php/test/Makefile.am
===================================================================
--- trunk/php/test/Makefile.am	2010-06-19 11:32:49 UTC (rev 3016)
+++ trunk/php/test/Makefile.am	2010-06-19 11:34:09 UTC (rev 3017)
@@ -24,12 +24,15 @@
 
 EXTRA_DIST = test.php crashme.php
 
-if ENABLE_PHP
+if ENABLE_PHP_TESTS
 
+PHPOPTS = -n -d enable_dl=On -d extension_dir=../.libs
+
 check:
-	php -n -d enable_dl=On -d extension_dir=../.libs test.php
+	$(PHP) $(PHPOPTS) $(PHPUNIT) test.php
 
 crashtest:
-	php -n -d enable_dl=On -d extension_dir=../.libs crashme.php
+	$(PHP) $(PHPOPTS) crashme.php
 
+
 endif

Modified: trunk/php/test/test.php
===================================================================
--- trunk/php/test/test.php	2010-06-19 11:32:49 UTC (rev 3016)
+++ trunk/php/test/test.php	2010-06-19 11:34:09 UTC (rev 3017)
@@ -5,39 +5,241 @@
 
 dl("geos.so");
 
-echo "\n";
-echo "Geos version is: " . GEOSVersion() . "\n";
-echo "\n";
+require_once 'PHPUnit/Framework.php';
 
-$reader = new GEOSWKTReader();
-$writer = new GEOSWKTWriter();
 
+class test extends PHPUnit_Framework_TestCase
+{
+    public function testGEOSVersion()
+    {
+        $this->assertContains('-CAPI-', GEOSVersion());
+    }
 
-/* BOGUS WKT */
-try {
-    $geom = $reader->read("ciao");
-} catch (Exception $e) {
-    echo 'EXPECTED [WKTReader::read("ciao")]: ' . $e->getMessage() . "\n";
-}
+    public function testWKTReader__construct()
+    {
+        $reader = new GEOSWKTReader();
+        $this->assertNotNull($reader);
+    }
 
-$geom = $reader->read("POINT(0 0)");
-$num = $geom->numGeometries();
-echo 'Geom has ' . $num . " geometries, WKT follows:\n";
-echo "[DEFAULT UNTRIMMED]: ".$writer->write($geom) . "\n";
-$writer->setTrim(TRUE);
-echo "[TRIMMED]: ".$writer->write($geom) . "\n";
-$writer->setTrim(FALSE);
-echo "[UNTRIMMED]: ".$writer->write($geom) . "\n";
+    public function testWKTReader_read()
+    {
+        $reader = new GEOSWKTReader();
 
-$writer->setTrim(TRUE);
+        /* Good WKT */
+        $geom = $reader->read('POINT(0 0)');
+        $this->assertNotNull($geom);
+        $geom = $reader->read('MULTIPOINT(0 0 1, 2 3 4)');
+        $this->assertNotNull($geom);
+        $geom = $reader->read('MULTIPOINT((0 0), (2 3))');
+        $this->assertNotNull($geom);
+        $geom = $reader->read('LINESTRING(0 0 1, 2 3 4)');
+        $this->assertNotNull($geom);
+        $geom = $reader->read('MULTILINESTRING((0 0 1, 2 3 4),
+                                               (10 10 2, 3 4 5))');
+        $this->assertNotNull($geom);
+        $geom = $reader->read('POLYGON((0 0, 1 0, 1 1, 0 1, 0 0))');
+        $this->assertNotNull($geom);
+        $geom = $reader->read('MULTIPOLYGON(
+                                ((0 0, 1 0, 1 1, 0 1, 0 0)),
+                                ((10 10, 10 14, 14 14, 14 10, 10 10),
+                                    (11 11, 11 12, 12 12, 12 11, 11 11))
+                               )');
+        $this->assertNotNull($geom);
+        $geom = $reader->read('GEOMETRYCOLLECTION(
+                MULTIPOLYGON(
+                 ((0 0, 1 0, 1 1, 0 1, 0 0)),
+                 ((10 10, 10 14, 14 14, 14 10, 10 10),
+                  (11 11, 11 12, 12 12, 12 11, 11 11))
+                ),
+                POLYGON((0 0, 1 0, 1 1, 0 1, 0 0)),
+                MULTILINESTRING((0 0, 2 3), (10 10, 3 4)),
+                LINESTRING(0 0, 2 3),
+                MULTIPOINT(0 0, 2 3),
+                POINT(9 0)
+        )');
+        $this->assertNotNull($geom);
 
-echo "We'll do trimmed WKT from now on";
+        /* BOGUS WKT */
+        try {
+            $reader->read("MULTIDOT(0 1 2 3)");
+            $this->assertTrue(FALSE); # this is just to fail if we get here
+        } catch (Exception $e) {
+            $this->assertContains('ParseException', $e->getMessage());
+        }
+    }
 
-$geom = $reader->read("MULTIPOINT(0 0, 1 1)");
-$num = $geom->numGeometries();
-echo 'Geom has ' . $num . " geometries. WKT follows:\n";
-echo $writer->write($geom) . "\n";
+    public function testWKTWriter__construct()
+    {
+        $writer = new GEOSWKTWriter();
+        $this->assertNotNull($writer);
+    }
 
-echo "\n";
-echo "\n";
+    public function testWKTWriter_write()
+    {
+        $writer = new GEOSWKTWriter();
+        $reader = new GEOSWKTReader();
 
+        try {
+            $writer->write(1);
+            $this->assertTrue(FALSE); # this is just to fail if we get here
+        } catch (Exception $e) {
+            $this->assertContains('expects parameter 1', $e->getMessage());
+        }
+
+        $g = $reader->read('POINT(6 7)');
+
+        $this->assertEquals('POINT (6.0000000000000000 7.0000000000000000)',
+            $writer->write($g));
+    }
+
+    public function testWKTWriter_setTrim()
+    {
+        $writer = new GEOSWKTWriter();
+        $reader = new GEOSWKTReader();
+
+        $g = $reader->read('POINT(6 7)');
+        $this->assertNotNull($g);
+
+        $writer->setTrim(TRUE);
+        $this->assertEquals('POINT (6 7)',
+            $writer->write($g));
+
+        $writer->setTrim(FALSE);
+        $this->assertEquals('POINT (6.0000000000000000 7.0000000000000000)',
+            $writer->write($g));
+
+    }
+
+    public function testWKT_roundTrip()
+    {
+        $r = new GEOSWKTReader();
+        $w = new GEOSWKTWriter();
+        $w->setTrim(TRUE);
+
+        $in[] = 'POINT (0 0)';
+        $in[] = 'MULTIPOINT (0 1, 2 3)';
+        $in[] = 'LINESTRING (0 0, 2 3)';
+        $in[] = 'MULTILINESTRING ((0 1, 2 3), (10 10, 3 4))';
+        $in[] = 'POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))';
+        $in[] = 'MULTIPOLYGON (((0 0, 1 0, 1 1, 0 1, 0 0)), ((10 10, 10 14, 14 14, 14 10, 10 10), (11 11, 11 12, 12 12, 12 11, 11 11)))';
+        $in[] = 'GEOMETRYCOLLECTION (MULTIPOLYGON (((0 0, 1 0, 1 1, 0 1, 0 0)), ((10 10, 10 14, 14 14, 14 10, 10 10), (11 11, 11 12, 12 12, 12 11, 11 11))), POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0)), MULTILINESTRING ((0 0, 2 3), (10 10, 3 4)), LINESTRING (0 0, 2 3), MULTIPOINT (0 0, 2 3), POINT (9 0))';
+
+        foreach ($in as $i) {
+            $this->assertEquals($i, $w->write($r->read($i)));
+        }
+
+    }
+
+    public function testWKTWriter_setRoundingPrecision()
+    {
+        $writer = new GEOSWKTWriter();
+        $reader = new GEOSWKTReader();
+
+        $g = $reader->read('POINT(6.123456 7.123456)');
+
+        $this->assertEquals('POINT (6.1234560000000000 7.1234560000000000)',
+            $writer->write($g));
+
+        $writer->setRoundingPrecision(2);
+        $this->assertEquals('POINT (6.12 7.12)', $writer->write($g));
+
+        $writer->setRoundingPrecision(5); /* rounds */
+        $this->assertEquals('POINT (6.12346 7.12346)', $writer->write($g));
+
+        $writer->setRoundingPrecision(1);
+        $this->assertEquals('POINT (6.1 7.1)', $writer->write($g));
+
+        $writer->setRoundingPrecision(0);
+        $this->assertEquals('POINT (6 7)', $writer->write($g));
+
+    }
+
+    public function testWKTWriter_setOutputDimension()
+    {
+        $reader = new GEOSWKTReader();
+        $g3d = $reader->read('POINT(1 2 3)');
+        $g2d = $reader->read('POINT(3 2)');
+
+        $writer = new GEOSWKTWriter();
+        $writer->setTrim(TRUE);
+
+        # Only 2d by default
+        $this->assertEquals('POINT (1 2)', $writer->write($g3d));
+
+        # 3d if requested _and_ available
+        $writer->setOutputDimension(3);
+        $this->assertEquals('POINT Z (1 2 3)', $writer->write($g3d));
+        $this->assertEquals('POINT (3 2)', $writer->write($g2d));
+
+    }
+
+    public function testWKTWriter_setOld3D()
+    {
+        $reader = new GEOSWKTReader();
+        $g3d = $reader->read('POINT(1 2 3)');
+
+        $writer = new GEOSWKTWriter();
+        $writer->setTrim(TRUE);
+
+        # New 3d WKT by default
+        $writer->setOutputDimension(3);
+        $this->assertEquals('POINT Z (1 2 3)', $writer->write($g3d));
+
+        # Switch to old
+        $writer->setOld3D(TRUE);
+        $this->assertEquals('POINT (1 2 3)', $writer->write($g3d));
+
+        # Old3d flag is not reset when changing dimensions
+        $writer->setOutputDimension(2);
+        $this->assertEquals('POINT (1 2)', $writer->write($g3d));
+        $writer->setOutputDimension(3);
+        $this->assertEquals('POINT (1 2 3)', $writer->write($g3d));
+
+        # Likewise, dimensions spec is not reset when changing old3d flag
+        $writer->setOld3D(FALSE);
+        $this->assertEquals('POINT Z (1 2 3)', $writer->write($g3d));
+
+    }
+
+    public function testGeometry_project()
+    {
+        $reader = new GEOSWKTReader();
+
+        $g = $reader->read('POINT(1 2)');
+        $g2 = $reader->read('POINT(3 4)');
+
+        /* The method only accept lineal geometries */
+        try {
+            $prj = $g->project($g2);
+            $this->assertTrue(FALSE); # this is just to fail if we get here
+        } catch (Exception $e) {
+            $this->assertContains('lineal', $e->getMessage());
+        }
+
+        $g = $reader->read('LINESTRING(0 0, 10 0)');
+
+        $g2 = $reader->read('POINT(0 0)');
+        $prj = $g->project($g2);
+        $this->assertEquals(0, $prj);
+
+        $g2 = $reader->read('POINT(10 0)');
+        $prj = $g->project($g2);
+        $this->assertEquals(10, $prj);
+
+        $g2 = $reader->read('POINT(5 0)');
+        $prj = $g->project($g2);
+        $this->assertEquals(5, $prj);
+
+        $g = $reader->read('MULTILINESTRING((0 0, 10 0),(20 10, 20 30))');
+
+        $g2 = $reader->read('POINT(20 0)');
+        $prj = $g->project($g2);
+        $this->assertEquals(10, $prj);
+
+        $g2 = $reader->read('POINT(20 5)');
+        $prj = $g->project($g2);
+        $this->assertEquals(10, $prj);
+
+
+    }
+}



More information about the geos-commits mailing list