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

svn_geos at osgeo.org svn_geos at osgeo.org
Mon Sep 22 00:42:03 PDT 2014


Author: strk
Date: 2014-09-22 00:42:02 -0700 (Mon, 22 Sep 2014)
New Revision: 4015

Modified:
   trunk/NEWS
   trunk/php/geos.c
   trunk/php/test/test.php
Log:
Add WKBReader::read() & WKBWriter::write() PHP bindings

Includes tests.
Patch by Benjamin Morel <benjamin.morel at gmail.com>

Signed-off-by: Sandro Santilli <strk at keybit.net>

Modified: trunk/NEWS
===================================================================
--- trunk/NEWS	2014-09-19 13:50:40 UTC (rev 4014)
+++ trunk/NEWS	2014-09-22 07:42:02 UTC (rev 4015)
@@ -5,6 +5,7 @@
   - Voronoi API (#627)
   - PHP: Geometry->normalize method
   - GEOS_USE_ONLY_R_API macro support (#695)
+  - PHP: WKBReader->read() & WKBWriter::write() methods (Benjamin Morel)
   ...
 
 Changes in 3.4.2

Modified: trunk/php/geos.c
===================================================================
--- trunk/php/geos.c	2014-09-19 13:50:40 UTC (rev 4014)
+++ trunk/php/geos.c	2014-09-22 07:42:02 UTC (rev 4015)
@@ -2205,6 +2205,7 @@
 PHP_METHOD(WKBWriter, setByteOrder);
 PHP_METHOD(WKBWriter, setIncludeSRID);
 PHP_METHOD(WKBWriter, getIncludeSRID);
+PHP_METHOD(WKBWriter, write);
 PHP_METHOD(WKBWriter, writeHEX);
 
 static zend_function_entry WKBWriter_methods[] = {
@@ -2215,6 +2216,7 @@
     PHP_ME(WKBWriter, setByteOrder, NULL, 0)
     PHP_ME(WKBWriter, getIncludeSRID, NULL, 0)
     PHP_ME(WKBWriter, setIncludeSRID, NULL, 0)
+    PHP_ME(WKBWriter, write, NULL, 0)
     PHP_ME(WKBWriter, writeHEX, NULL, 0)
     {NULL, NULL, NULL}
 };
@@ -2294,6 +2296,38 @@
 }
 
 /**
+ * string GEOSWKBWriter::write(GEOSGeometry)
+ */
+PHP_METHOD(WKBWriter, write)
+{
+    GEOSWKBWriter *writer;
+    zval *zobj;
+    GEOSGeometry *geom;
+    char *ret;
+    size_t retsize;
+    char* retstr;
+
+    writer = (GEOSWKBWriter*)getRelay(getThis(), WKBWriter_ce_ptr);
+
+    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "o", &zobj)
+        == FAILURE)
+    {
+        RETURN_NULL();
+    }
+
+    geom = getRelay(zobj, Geometry_ce_ptr);
+
+    ret = (char*)GEOSWKBWriter_write(writer, geom, &retsize);
+    /* we'll probably get an exception if ret is null */
+    if ( ! ret ) RETURN_NULL();
+
+    retstr = estrndup(ret, retsize);
+    GEOSFree(ret);
+
+    RETURN_STRINGL(retstr, retsize, 0);
+}
+
+/**
  * string GEOSWKBWriter::writeHEX(GEOSGeometry)
  */
 PHP_METHOD(WKBWriter, writeHEX)
@@ -2401,10 +2435,12 @@
 /* -- class GEOSWKBReader -------------------- */
 
 PHP_METHOD(WKBReader, __construct);
+PHP_METHOD(WKBReader, read);
 PHP_METHOD(WKBReader, readHEX);
 
 static zend_function_entry WKBReader_methods[] = {
     PHP_ME(WKBReader, __construct, NULL, 0)
+    PHP_ME(WKBReader, read, NULL, 0)
     PHP_ME(WKBReader, readHEX, NULL, 0)
     {NULL, NULL, NULL}
 };
@@ -2446,6 +2482,31 @@
     setRelay(object, obj);
 }
 
+PHP_METHOD(WKBReader, read)
+{
+    GEOSWKBReader *reader;
+    GEOSGeometry *geom;
+    unsigned char* wkb;
+    int wkblen;
+
+    reader = (GEOSWKBReader*)getRelay(getThis(), WKBReader_ce_ptr);
+
+    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s",
+        &wkb, &wkblen) == FAILURE)
+    {
+        RETURN_NULL();
+    }
+
+    geom = GEOSWKBReader_read(reader, wkb, wkblen);
+    /* we'll probably get an exception if geom is null */
+    if ( ! geom ) RETURN_NULL();
+
+    /* return_value is a zval */
+    object_init_ex(return_value, Geometry_ce_ptr);
+    setRelay(return_value, geom);
+
+}
+
 PHP_METHOD(WKBReader, readHEX)
 {
     GEOSWKBReader *reader;

Modified: trunk/php/test/test.php
===================================================================
--- trunk/php/test/test.php	2014-09-19 13:50:40 UTC (rev 4014)
+++ trunk/php/test/test.php	2014-09-22 07:42:02 UTC (rev 4015)
@@ -2165,6 +2165,119 @@
         $this->assertEquals(FALSE, $writer->getIncludeSRID());
     }
 
+    public function testWKBWriter_write()
+    {
+        $writer = new GEOSWKBWriter();
+        $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)');
+        $g->setSRID(43);
+
+        $writer->setOutputDimension(2); // 2D
+
+        // 2D LITTLE endian
+        $writer->setByteOrder(1);
+        $this->assertEquals(
+            hex2bin('010100000000000000000018400000000000001C40'),
+            $writer->write($g));
+        // 2D LITTLE endian + SRID
+        $writer->setIncludeSRID(TRUE);
+        $this->assertEquals(
+            hex2bin('01010000202B00000000000000000018400000000000001C40'),
+            $writer->write($g));
+        $writer->setIncludeSRID(FALSE);
+
+        // 2D BIG endian
+        $writer->setByteOrder(0);
+        $this->assertEquals(hex2bin('00000000014018000000000000401C000000000000'),
+            $writer->write($g));
+        // 2D BIG endian + SRID
+        $writer->setIncludeSRID(TRUE);
+        $this->assertEquals(
+            hex2bin('00200000010000002B4018000000000000401C000000000000'),
+            $writer->write($g));
+        $writer->setIncludeSRID(FALSE);
+
+        $writer->setOutputDimension(3); // 3D
+
+        // 3D LITTLE endian (2D input)
+        $writer->setByteOrder(1);
+        $this->assertEquals(
+            hex2bin('010100000000000000000018400000000000001C40'),
+            $writer->write($g));
+        // 3D LITTLE endian + SRID  (2D input)
+        $writer->setIncludeSRID(TRUE);
+        $this->assertEquals(
+            hex2bin('01010000202B00000000000000000018400000000000001C40'),
+            $writer->write($g));
+        $writer->setIncludeSRID(FALSE);
+
+        // 3D BIG endian (2D input)
+        $writer->setByteOrder(0);
+        $this->assertEquals(hex2bin('00000000014018000000000000401C000000000000'),
+            $writer->write($g));
+        // 3D BIG endian + SRID (2D input)
+        $writer->setIncludeSRID(TRUE);
+        $this->assertEquals(
+            hex2bin('00200000010000002B4018000000000000401C000000000000'),
+            $writer->write($g));
+        $writer->setIncludeSRID(FALSE);
+
+
+        $g = $reader->read('POINT(6 7 8)');
+        $g->setSRID(53);
+
+        $writer->setOutputDimension(2); // 2D
+
+        // 2D LITTLE endian (3D input)
+        $writer->setByteOrder(1);
+        $this->assertEquals(hex2bin('010100000000000000000018400000000000001C40'),
+            $writer->write($g));
+        // 2D LITTLE endian + SRID (3D input)
+        $writer->setIncludeSRID(TRUE);
+        $writer->setByteOrder(1);
+        $this->assertEquals(
+            hex2bin('01010000203500000000000000000018400000000000001C40'),
+            $writer->write($g));
+        $writer->setIncludeSRID(FALSE);
+        // 2D BIG endian (3D input)
+        $writer->setByteOrder(0);
+        $this->assertEquals(hex2bin('00000000014018000000000000401C000000000000'),
+            $writer->write($g));
+        // 2D BIG endian + SRID (3D input)
+        $writer->setIncludeSRID(TRUE);
+        $this->assertEquals(
+            hex2bin('0020000001000000354018000000000000401C000000000000'),
+            $writer->write($g));
+        $writer->setIncludeSRID(FALSE);
+
+        $writer->setOutputDimension(3); // 3D
+
+        // 3D LITTLE endian (3D input)
+        $writer->setByteOrder(1);
+        $this->assertEquals(
+            hex2bin('010100008000000000000018400000000000001C400000000000002040'),
+            $writer->write($g));
+        // 3D BIG endian (3D input)
+        $writer->setByteOrder(0);
+        $this->assertEquals(
+            hex2bin('00800000014018000000000000401C0000000000004020000000000000'),
+            $writer->write($g));
+        // 3D BIG endian + SRID (3D input)
+        $writer->setIncludeSRID(TRUE);
+        $this->assertEquals(
+            hex2bin('00A0000001000000354018000000000000401C0000000000004020000000000000'),
+            $writer->write($g));
+        $writer->setIncludeSRID(FALSE);
+    }
+
     public function testWKBWriter_writeHEX()
     {
         $writer = new GEOSWKBWriter();
@@ -2284,6 +2397,64 @@
         $this->assertNotNull($reader);
     }
 
+    public function testWKBReader_read()
+    {
+        $reader = new GEOSWKBReader();
+
+        $writer = new GEOSWKTWriter();
+        $writer->setTrim(TRUE);
+        $writer->setOutputDimension(3);
+
+        // 2D LITTLE endian
+        $g = $reader->read(hex2bin(
+            '010100000000000000000018400000000000001C40'
+        ));
+        $this->assertEquals('POINT (6 7)', $writer->write($g));
+        $this->assertEquals(0, $g->getSRID());
+
+        // 2D BIG endian
+        $g = $reader->read(hex2bin(
+            '00000000014018000000000000401C000000000000'
+        ));
+        $this->assertEquals('POINT (6 7)', $writer->write($g));
+        $this->assertEquals(0, $g->getSRID());
+
+        // 2D LITTLE endian + SRID
+        $g = $reader->read(hex2bin(
+            '01010000202B00000000000000000018400000000000001C40'
+        ));
+        $this->assertEquals('POINT (6 7)', $writer->write($g));
+        $this->assertEquals(43, $g->getSRID());
+
+        // 2D BIG endian + SRID
+        $g = $reader->read(hex2bin(
+            '00200000010000002B4018000000000000401C000000000000'
+        ));
+        $this->assertEquals('POINT (6 7)', $writer->write($g));
+        $this->assertEquals(43, $g->getSRID());
+
+        // 3D LITTLE endian
+        $g = $reader->read(hex2bin(
+            '010100008000000000000018400000000000001C400000000000002040'
+        ));
+        $this->assertEquals('POINT Z (6 7 8)', $writer->write($g));
+        $this->assertEquals(0, $g->getSRID());
+
+        // 3D BIG endian
+        $g = $reader->read(hex2bin(
+            '00800000014018000000000000401C0000000000004020000000000000'
+        ));
+        $this->assertEquals('POINT Z (6 7 8)', $writer->write($g));
+        $this->assertEquals(0, $g->getSRID());
+
+        // 3D BIG endian + SRID
+        $g = $reader->read(hex2bin(
+            '00A0000001000000354018000000000000401C0000000000004020000000000000'
+        ));
+        $this->assertEquals('POINT Z (6 7 8)', $writer->write($g));
+        $this->assertEquals(53, $g->getSRID());
+    }
+
     public function testWKBReader_readHEX()
     {
         $reader = new GEOSWKBReader();



More information about the geos-commits mailing list