[geos-commits] r4254 - in trunk: . capi include/geos/geom tests/unit tests/unit/capi

Sandro Santilli strk at kbt.io
Wed Sep 7 05:57:03 PDT 2016


Author: strk
Date: 2016-09-07 05:57:02 -0700 (Wed, 07 Sep 2016)
New Revision: 4254

Added:
   trunk/tests/unit/capi/GEOSUserDataTest.cpp
Modified:
   trunk/NEWS
   trunk/capi/geos_c.cpp
   trunk/capi/geos_c.h.in
   trunk/capi/geos_ts_c.cpp
   trunk/include/geos/geom/Geometry.h
   trunk/tests/unit/Makefile.am
Log:
Add GEOSGeom_setUserData and GEOSGeom_getUserData

Patch by Rashad Kanavath <rashad.kanavath at c-s.fr>

Modified: trunk/NEWS
===================================================================
--- trunk/NEWS	2016-09-02 11:10:23 UTC (rev 4253)
+++ trunk/NEWS	2016-09-07 12:57:02 UTC (rev 4254)
@@ -5,6 +5,7 @@
   - The PHP binding moved to its own repository:
     http://git.osgeo.org/gogs/geos/php-geos (#765)
 - New things:
+  - CAPI: GEOSGeom_{get,set}UserData (Rashad Kanavath)
   - CAPI: GEOSGeom_setPrecision (#713) - PHP: Geometry->setPrecision
   - CAPI: GEOSGeom_getPrecision
   - CAPI: GEOSMinimumRotatedRectangle and GEOSMinimumWidth

Modified: trunk/capi/geos_c.cpp
===================================================================
--- trunk/capi/geos_c.cpp	2016-09-02 11:10:23 UTC (rev 4253)
+++ trunk/capi/geos_c.cpp	2016-09-07 12:57:02 UTC (rev 4254)
@@ -717,6 +717,18 @@
     return GEOSSetSRID_r( handle, g, srid );
 }
 
+void * 
+GEOSGeom_getUserData(const Geometry *g)
+{
+    return GEOSGeom_getUserData_r( handle, g );
+}
+
+void
+GEOSGeom_setUserData(Geometry *g, void* userData)
+{
+    return GEOSGeom_setUserData_r( handle, g, userData );
+}
+
 char
 GEOSHasZ(const Geometry *g)
 {

Modified: trunk/capi/geos_c.h.in
===================================================================
--- trunk/capi/geos_c.h.in	2016-09-02 11:10:23 UTC (rev 4253)
+++ trunk/capi/geos_c.h.in	2016-09-07 12:57:02 UTC (rev 4254)
@@ -955,6 +955,12 @@
 extern void GEOS_DLL GEOSSetSRID_r(GEOSContextHandle_t handle,
                                    GEOSGeometry* g, int SRID);
 
+extern void* GEOS_DLL GEOSGeom_getUserData_r(GEOSContextHandle_t handle,
+const GEOSGeometry* g);
+
+extern void GEOS_DLL GEOSGeom_setUserData_r(GEOSContextHandle_t handle,
+                                   GEOSGeometry* g, void* userData);
+
 /* May be called on all geometries in GEOS 3.x, returns -1 on error and 1
  * for non-multi geometries. Older GEOS versions only accept
  * GeometryCollections or Multi* geometries here, and are likely to crash
@@ -1862,6 +1868,11 @@
 
 extern void GEOS_DLL GEOSSetSRID(GEOSGeometry* g, int SRID);
 
+extern void* GEOS_DLL GEOSGeom_getUserData(const GEOSGeometry* g);
+
+extern void GEOS_DLL GEOSGeom_setUserData(GEOSGeometry* g, void* userData);
+
+
 /* May be called on all geometries in GEOS 3.x, returns -1 on error and 1
  * for non-multi geometries. Older GEOS versions only accept
  * GeometryCollections or Multi* geometries here, and are likely to crash

Modified: trunk/capi/geos_ts_c.cpp
===================================================================
--- trunk/capi/geos_ts_c.cpp	2016-09-02 11:10:23 UTC (rev 4253)
+++ trunk/capi/geos_ts_c.cpp	2016-09-07 12:57:02 UTC (rev 4254)
@@ -2526,6 +2526,26 @@
 }
 
 void
+GEOSGeom_setUserData_r(GEOSContextHandle_t extHandle, Geometry *g, void* userData)
+{
+    assert(0 != g);
+
+    if ( 0 == extHandle )
+    {
+        return;
+    }
+
+    GEOSContextHandleInternal_t *handle = 0;
+    handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
+    if ( 0 == handle->initialized )
+    {
+        return;
+    }
+
+    g->setUserData(userData);
+}
+
+void
 GEOSSetSRID_r(GEOSContextHandle_t extHandle, Geometry *g, int srid)
 {
     assert(0 != g);
@@ -3601,6 +3621,39 @@
     return out;
 }
 
+ void*
+GEOSGeom_getUserData_r(GEOSContextHandle_t extHandle, const Geometry *g)
+{
+    assert(0 != g);
+
+    if ( 0 == extHandle )
+    {
+        return 0;
+    }
+
+    GEOSContextHandleInternal_t *handle = 0;
+    handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
+    if ( 0 == handle->initialized )
+    {
+        return 0;
+    }
+
+    try
+    {
+        return g->getUserData();
+    }
+    catch (const std::exception &e)
+    {
+        handle->ERROR_MESSAGE("%s", e.what());
+    }
+    catch (...)
+    {
+        handle->ERROR_MESSAGE("Unknown exception thrown");
+    }
+
+    return NULL;
+}
+
 int
 GEOSGetSRID_r(GEOSContextHandle_t extHandle, const Geometry *g)
 {

Modified: trunk/include/geos/geom/Geometry.h
===================================================================
--- trunk/include/geos/geom/Geometry.h	2016-09-02 11:10:23 UTC (rev 4253)
+++ trunk/include/geos/geom/Geometry.h	2016-09-07 12:57:02 UTC (rev 4254)
@@ -216,7 +216,7 @@
 	*
 	* @return the user data object, or <code>null</code> if none set
 	*/
-	void* getUserData() { return _userData; }
+	void* getUserData() const { return _userData; }
 
 	/*
 	 * \brief

Modified: trunk/tests/unit/Makefile.am
===================================================================
--- trunk/tests/unit/Makefile.am	2016-09-02 11:10:23 UTC (rev 4253)
+++ trunk/tests/unit/Makefile.am	2016-09-07 12:57:02 UTC (rev 4254)
@@ -138,6 +138,7 @@
 	capi/GEOSNearestPointsTest.cpp \
 	capi/GEOSWithinTest.cpp \
 	capi/GEOSSimplifyTest.cpp \
+	capi/GEOSUserDataTest.cpp \
 	capi/GEOSPreparedGeometryTest.cpp \
 	capi/GEOSPointOnSurfaceTest.cpp \
 	capi/GEOSPolygonizer_getCutEdgesTest.cpp \

Added: trunk/tests/unit/capi/GEOSUserDataTest.cpp
===================================================================
--- trunk/tests/unit/capi/GEOSUserDataTest.cpp	                        (rev 0)
+++ trunk/tests/unit/capi/GEOSUserDataTest.cpp	2016-09-07 12:57:02 UTC (rev 4254)
@@ -0,0 +1,105 @@
+// 
+// Test Suite for C-API GEOSSimplify
+
+#include <tut.hpp>
+// geos
+#include <geos_c.h>
+// std
+#include <cstdarg>
+#include <cstdio>
+#include <cstdlib>
+#include <memory>
+
+namespace tut
+{
+    //
+    // Test Group
+    //
+
+    // Common data used in test cases.
+    struct test_capigeouserdata_data
+    {
+        GEOSGeometry* geom_;
+
+        static void notice(const char *fmt, ...)
+        {
+            std::fprintf( stdout, "NOTICE: ");
+
+            va_list ap;
+            va_start(ap, fmt);
+            std::vfprintf(stdout, fmt, ap);
+            va_end(ap);
+        
+            std::fprintf(stdout, "\n");
+        }
+
+        test_capigeouserdata_data()
+            : geom_(0)
+        {
+            initGEOS(notice, notice);
+        }       
+
+        ~test_capigeouserdata_data()
+        {
+            GEOSGeom_destroy(geom_);
+            geom_ = 0;
+            finishGEOS();
+        }
+
+    };
+
+    typedef test_group<test_capigeouserdata_data> group;
+    typedef group::object object;
+
+    group test_capigeosuserdata_group("capi::GEOSUserData");
+
+    //
+    // Test Cases
+    //
+
+    // Test GEOSUserData
+    template<>
+    template<>
+    void object::test<1>()
+    {
+        geom_ = GEOSGeomFromWKT("POLYGON EMPTY");
+
+        ensure ( 0 != GEOSisEmpty(geom_) );
+
+        int in = 1;
+        GEOSGeom_setUserData(geom_, &in);
+
+        int* out = (int *)GEOSGeom_getUserData(geom_);
+
+        ensure_equals(*out, 1);
+    }
+
+    struct UserData
+    {
+        int a;
+        int b;
+    } ;
+
+    template<>
+    template<>
+    void object::test<2>()
+    {
+        geom_ = GEOSGeomFromWKT("POLYGON EMPTY");
+
+        ensure ( 0 != GEOSisEmpty(geom_) );
+
+        UserData in;
+        in.a = 1;
+        in.b = 2;
+
+        GEOSGeom_setUserData(geom_, &in);
+
+        struct UserData* out = (struct UserData* )GEOSGeom_getUserData(geom_);
+
+        ensure_equals(in.a, out->a);
+        ensure_equals(in.b, out->b);
+        ensure_equals(&in, out);
+    }
+
+} // namespace tut
+



More information about the geos-commits mailing list