[geos-commits] r4109 - in trunk: . capi php php/test src/geom/util tests/unit/capi

svn_geos at osgeo.org svn_geos at osgeo.org
Tue Oct 13 04:20:53 PDT 2015


Author: strk
Date: 2015-10-13 04:20:53 -0700 (Tue, 13 Oct 2015)
New Revision: 4109

Modified:
   trunk/NEWS
   trunk/capi/geos_c.cpp
   trunk/capi/geos_c.h.in
   trunk/capi/geos_ts_c.cpp
   trunk/php/geos.c
   trunk/php/test/test.php
   trunk/src/geom/util/GeometryEditor.cpp
   trunk/tests/unit/capi/GEOSGeom_setPrecisionTest.cpp
Log:
Add a GEOSGeom_setPrecision funciton in C-API and PHP

Also fixes a bug in GeometryEditor that failed to update
GeometryFactory for empty polygons (#749)

Modified: trunk/NEWS
===================================================================
--- trunk/NEWS	2015-10-07 07:20:22 UTC (rev 4108)
+++ trunk/NEWS	2015-10-13 11:20:53 UTC (rev 4109)
@@ -3,6 +3,7 @@
 
 - New things:
   - CAPI: GEOSGeom_setPrecision (#713) - PHP: Geometry->setPrecision
+  - CAPI: GEOSGeom_getPrecision - PHP: Geometry->getPrecision
 - Improvements:
   - ...
 - C++ API changes:

Modified: trunk/capi/geos_c.cpp
===================================================================
--- trunk/capi/geos_c.cpp	2015-10-07 07:20:22 UTC (rev 4108)
+++ trunk/capi/geos_c.cpp	2015-10-13 11:20:53 UTC (rev 4109)
@@ -844,6 +844,12 @@
 	return GEOSGeom_setPrecision_r(handle, g, gridSize, flags);
 }
 
+double
+GEOSGeom_getPrecision(const GEOSGeometry *g)
+{
+	return GEOSGeom_getPrecision_r(handle, g);
+}
+
 int
 GEOSGeom_getDimensions(const Geometry *g)
 {

Modified: trunk/capi/geos_c.h.in
===================================================================
--- trunk/capi/geos_c.h.in	2015-10-07 07:20:22 UTC (rev 4108)
+++ trunk/capi/geos_c.h.in	2015-10-13 11:20:53 UTC (rev 4109)
@@ -969,6 +969,16 @@
                                        const GEOSGeometry *g,
                                        double gridSize, int flags);
 
+/**
+ * Get a geometry's precision
+ *
+ * @return the size of the geometry's precision grid, 0 for FLOATING
+ *         precision or -1 on exception
+ */
+extern double GEOS_DLL GEOSGeom_getPrecision_r(
+                                       GEOSContextHandle_t handle,
+                                       const GEOSGeometry *g);
+
 /* Return -1 on exception */
 extern int GEOS_DLL GEOSGetNumInteriorRings_r(GEOSContextHandle_t handle,
                                               const GEOSGeometry* g);
@@ -1720,6 +1730,9 @@
 	const GEOSGeometry *g, double gridSize, int flags);
 
 /* Return -1 on exception */
+extern double GEOS_DLL GEOSGeom_getPrecision(const GEOSGeometry *g);
+
+/* Return -1 on exception */
 extern int GEOS_DLL GEOSGetNumInteriorRings(const GEOSGeometry* g);
 
 /* Return -1 on exception, Geometry must be a LineString. */

Modified: trunk/capi/geos_ts_c.cpp
===================================================================
--- trunk/capi/geos_ts_c.cpp	2015-10-07 07:20:22 UTC (rev 4108)
+++ trunk/capi/geos_ts_c.cpp	2015-10-13 11:20:53 UTC (rev 4109)
@@ -4285,6 +4285,43 @@
     return NULL;
 }
 
+double
+GEOSGeom_getPrecision_r(GEOSContextHandle_t extHandle, const GEOSGeometry *g)
+{
+    using namespace geos::geom;
+
+    assert(0 != g);
+
+    if ( 0 == extHandle )
+    {
+        return -1;
+    }
+
+    GEOSContextHandleInternal_t *handle = 0;
+    handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
+    if ( 0 == handle->initialized )
+    {
+        return -1;
+    }
+
+    try
+    {
+        const PrecisionModel *pm = g->getPrecisionModel();
+        double cursize = pm->isFloating() ? 0 : 1.0/pm->getScale();
+        return cursize;
+    }
+    catch (const std::exception &e)
+    {
+        handle->ERROR_MESSAGE("%s", e.what());
+    }
+    catch (...)
+    {
+        handle->ERROR_MESSAGE("Unknown exception thrown");
+    }
+
+    return -1;
+}
+
 int
 GEOSGeom_getDimensions_r(GEOSContextHandle_t extHandle, const Geometry *g)
 {

Modified: trunk/php/geos.c
===================================================================
--- trunk/php/geos.c	2015-10-07 07:20:22 UTC (rev 4108)
+++ trunk/php/geos.c	2015-10-13 11:20:53 UTC (rev 4109)
@@ -205,6 +205,7 @@
 PHP_METHOD(Geometry, simplify); /* also does topology-preserving */
 PHP_METHOD(Geometry, normalize);
 PHP_METHOD(Geometry, setPrecision);
+PHP_METHOD(Geometry, getPrecision);
 PHP_METHOD(Geometry, extractUniquePoints); 
 PHP_METHOD(Geometry, disjoint);
 PHP_METHOD(Geometry, touches);
@@ -272,6 +273,7 @@
     PHP_ME(Geometry, simplify, NULL, 0)
     PHP_ME(Geometry, normalize, NULL, 0)
     PHP_ME(Geometry, setPrecision, NULL, 0)
+    PHP_ME(Geometry, getPrecision, NULL, 0)
     PHP_ME(Geometry, extractUniquePoints, NULL, 0)
     PHP_ME(Geometry, disjoint, NULL, 0)
     PHP_ME(Geometry, touches, NULL, 0)
@@ -1070,6 +1072,22 @@
 }
 
 /**
+ * double GEOSGeometry::getPrecision()
+ */
+PHP_METHOD(Geometry, getPrecision)
+{
+    GEOSGeometry *geom;
+    double prec;
+
+    geom = (GEOSGeometry*)getRelay(getThis(), Geometry_ce_ptr);
+
+    prec = GEOSGeom_getPrecision(geom);
+    if ( prec < 0 ) RETURN_NULL(); /* should get an exception first */
+
+    RETURN_DOUBLE(prec);
+}
+
+/**
  * GEOSGeometry GEOSGeometry::normalize()
  */
 PHP_METHOD(Geometry, normalize)

Modified: trunk/php/test/test.php
===================================================================
--- trunk/php/test/test.php	2015-10-07 07:20:22 UTC (rev 4108)
+++ trunk/php/test/test.php	2015-10-13 11:20:53 UTC (rev 4109)
@@ -1367,32 +1367,38 @@
         $this->assertEquals(
             'LINESTRING (0 0, 4 4, 6 10, 10 0, 10 10, 6 12, 0 10)'
             , $writer->write($g));
+        $this->assertEquals($g->getPrecision(), 2);
 
         $g = $g->setPrecision(0);
         $this->assertEquals(
             'LINESTRING (0 0, 4 4, 6 10, 10 0, 10 10, 6 12, 0 10)'
             , $writer->write($g));
+        $this->assertEquals($g->getPrecision(), 0);
 
         $g = $g->setPrecision(8);
         $this->assertEquals(
             'LINESTRING (0 0, 8 8, 8 0, 8 8, 8 16, 0 8)'
             , $writer->write($g));
+        $this->assertEquals($g->getPrecision(), 8);
 
         $g = $g->setPrecision(10);
         $this->assertEquals(
             'LINESTRING (0 0, 10 10, 10 0, 10 10, 10 20, 0 10)'
             , $writer->write($g));
+        $this->assertEquals($g->getPrecision(), 10);
 
         $g = $g->setPrecision(20);
         $this->assertEquals(
             'LINESTRING (0 0, 20 20, 20 0, 20 20, 0 20)'
             , $writer->write($g));
+        $this->assertEquals($g->getPrecision(), 20);
 
         $g = $reader->read('POLYGON((0 0, 2 0, 2 2, 0 2, 0 0))');
         $g = $g->setPrecision(20);
         $this->assertEquals(
             'POLYGON EMPTY'
             , $writer->write($g));
+        $this->assertEquals($g->getPrecision(), 20);
     }
 
     public function testGeometry_extractUniquePoints()

Modified: trunk/src/geom/util/GeometryEditor.cpp
===================================================================
--- trunk/src/geom/util/GeometryEditor.cpp	2015-10-07 07:20:22 UTC (rev 4108)
+++ trunk/src/geom/util/GeometryEditor.cpp	2015-10-13 11:20:53 UTC (rev 4109)
@@ -111,7 +111,13 @@
   );
 	if (newPolygon->isEmpty()) {
 		//RemoveSelectedPlugIn relies on this behaviour. [Jon Aquino]
-		return newPolygon;
+		if ( newPolygon->getFactory() != factory ) {
+		  Polygon *ret = factory->createPolygon(NULL, NULL);
+		  delete newPolygon;
+		  return ret;
+		} else {
+		  return newPolygon;
+		}
 	}
 
 	Geometry* editResult = edit(newPolygon->getExteriorRing(),operation);

Modified: trunk/tests/unit/capi/GEOSGeom_setPrecisionTest.cpp
===================================================================
--- trunk/tests/unit/capi/GEOSGeom_setPrecisionTest.cpp	2015-10-07 07:20:22 UTC (rev 4108)
+++ trunk/tests/unit/capi/GEOSGeom_setPrecisionTest.cpp	2015-10-13 11:20:53 UTC (rev 4109)
@@ -90,9 +90,13 @@
     {
         geom1_ = fromWKT("POLYGON EMPTY");
         ensure(geom1_);
+        double scale = GEOSGeom_getPrecision(geom1_);
+        ensure_equals(scale, 0.0);
         geom3_ = GEOSGeom_setPrecision(geom1_, 2.0, 0);
         ensure(geom3_);
         ensure_equals(toWKT(geom3_), std::string("POLYGON EMPTY"));
+        scale = GEOSGeom_getPrecision(geom3_);
+        ensure_equals(scale, 2.0);
     }
 
     template<>
@@ -138,6 +142,10 @@
         geom3_ = GEOSIntersection(geom1_, geom2_);
         ensure(geom3_);
         ensure_equals(toWKT(geom3_), std::string("POINT (3 20)"));
+        double scale = GEOSGeom_getPrecision(geom1_);
+        ensure_equals(scale, 0.5);
+        scale = GEOSGeom_getPrecision(geom2_);
+        ensure_equals(scale, 2.0);
     }
 
     // Retain (or not) topology



More information about the geos-commits mailing list