[geos-commits] r2144 - in trunk: capi tests/unit tests/unit/capi

svn_geos at osgeo.org svn_geos at osgeo.org
Thu Jul 31 13:01:15 EDT 2008


Author: mloskot
Date: 2008-07-31 13:01:15 -0400 (Thu, 31 Jul 2008)
New Revision: 2144

Added:
   trunk/tests/unit/capi/GEOSPolygonizer_getCutEdgesTest.cpp
Modified:
   trunk/capi/geos_c.cpp
   trunk/capi/geos_c.h.in
   trunk/tests/unit/Makefile.am
Log:
Patch completing C API interface with wrapper on Polygonizer::getCutEdges (Ticket #195). Unit test included in tests/unit/capi/GEOSPolygonizer_getCutEdgeTest.cpp. Thanks to Jurgen E. Fischer for this patch.

Modified: trunk/capi/geos_c.cpp
===================================================================
--- trunk/capi/geos_c.cpp	2008-07-26 23:06:11 UTC (rev 2143)
+++ trunk/capi/geos_c.cpp	2008-07-31 17:01:15 UTC (rev 2144)
@@ -1352,6 +1352,54 @@
 }
 
 Geometry *
+GEOSPolygonizer_getCutEdges(const Geometry * const * g, unsigned int ngeoms)
+{
+	using geos::operation::polygonize::Polygonizer;
+	unsigned int i;
+	Geometry *out = NULL;
+
+	try{
+		// Polygonize
+		Polygonizer plgnzr;
+		for (i=0; i<ngeoms; i++) plgnzr.add(g[i]);
+#if GEOS_DEBUG
+	NOTICE_MESSAGE("geometry vector added to polygonizer");
+#endif
+
+		std::vector<const LineString *>*lines = plgnzr.getCutEdges();
+
+#if GEOS_DEBUG
+	NOTICE_MESSAGE("output polygons got");
+#endif
+
+		// We need a vector of Geometry pointers, not
+		// Polygon pointers.
+		// STL vector doesn't allow transparent upcast of this
+		// nature, so we explicitly convert.
+		// (it's just a waste of processor and memory, btw)
+    std::vector<Geometry*> *linevec =
+				new std::vector<Geometry *>(lines->size());
+		for (i=0; i<lines->size(); i++) (*linevec)[i] = (*lines)[i]->clone();
+
+		out = geomFactory->createGeometryCollection(linevec);
+		// the above method takes ownership of the passed
+		// vector, so we must *not* delete it
+	}
+	catch (const std::exception &e)
+	{
+		ERROR_MESSAGE("%s", e.what());
+		return NULL;
+	}
+	catch (...)
+	{
+		ERROR_MESSAGE("Unknown exception thrown");
+		return NULL;
+	}
+
+	return out;
+}
+
+Geometry *
 GEOSLineMerge(const Geometry *g)
 {
 		using geos::operation::linemerge::LineMerger;

Modified: trunk/capi/geos_c.h.in
===================================================================
--- trunk/capi/geos_c.h.in	2008-07-26 23:06:11 UTC (rev 2143)
+++ trunk/capi/geos_c.h.in	2008-07-31 17:01:15 UTC (rev 2144)
@@ -294,8 +294,8 @@
  * all arguments remain ownership of the caller
  * (both Geometries and pointers)
  */
-extern GEOSGeometry GEOS_DLL *GEOSPolygonize(const GEOSGeometry * const geoms[],
-	unsigned int ngeoms);
+extern GEOSGeometry GEOS_DLL *GEOSPolygonize(const GEOSGeometry * const geoms[], unsigned int ngeoms);
+extern GEOSGeometry GEOS_DLL *GEOSPolygonizer_getCutEdges(const GEOSGeometry * const geoms[], unsigned int ngeoms);
 
 extern GEOSGeometry GEOS_DLL *GEOSLineMerge(const GEOSGeometry* g);
 extern GEOSGeometry GEOS_DLL *GEOSSimplify(const GEOSGeometry* g1, double tolerance);

Modified: trunk/tests/unit/Makefile.am
===================================================================
--- trunk/tests/unit/Makefile.am	2008-07-26 23:06:11 UTC (rev 2143)
+++ trunk/tests/unit/Makefile.am	2008-07-31 17:01:15 UTC (rev 2144)
@@ -57,7 +57,8 @@
 	simplify/TopologyPreservingSimplifierTest.cpp \
 	util/UniqueCoordinateArrayFilterTest.cpp \
 	capi/GEOSCoordSeqTest.cpp	\
-	capi/GEOSSimplifyTest.cpp
+	capi/GEOSSimplifyTest.cpp \
+	capi/GEOSPolygonizer_getCutEdgesTest.cpp
 
-noinst_HEADERS = 			\
+noinst_HEADERS = \
 	utility.h

Added: trunk/tests/unit/capi/GEOSPolygonizer_getCutEdgesTest.cpp
===================================================================
--- trunk/tests/unit/capi/GEOSPolygonizer_getCutEdgesTest.cpp	                        (rev 0)
+++ trunk/tests/unit/capi/GEOSPolygonizer_getCutEdgesTest.cpp	2008-07-31 17:01:15 UTC (rev 2144)
@@ -0,0 +1,107 @@
+// $Id$
+// 
+// Test Suite for C-API GEOSPolygonizeGetCutEdges
+
+// TUT
+#include <tut.h>
+// GEOS CAPI
+#include <geos_c.h>
+// C+
+#include <cstdarg>
+#include <cstdio>
+#include <cstdlib>
+#include <memory>
+
+namespace tut
+{
+    //
+    // Test Group
+    //
+
+    // Common data used in test cases.
+    struct test_capigeospolygonizegetcutedges_data
+	{
+		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_capigeospolygonizegetcutedges_data()
+		{
+			initGEOS(notice, notice);
+		}		
+
+		~test_capigeospolygonizegetcutedges_data()
+		{
+			finishGEOS();
+		}
+
+	};
+
+	typedef test_group<test_capigeospolygonizegetcutedges_data> group;
+	typedef group::object object;
+
+	group test_capigeospolygonizegetcutedges_group("capi::GEOSPolygonizeGetCutEdges");
+
+    //
+    // Test Cases
+    //
+
+    template<>
+    template<>
+    void object::test<1>()
+    {
+        const int size = 2;
+        GEOSGeometry* geoms[size] = { 0 };
+
+	    geoms[0] = GEOSGeomFromWKT("LINESTRING(1 3, 3 3, 3 1, 1 1, 1 3)");
+	    geoms[1] = GEOSGeomFromWKT("LINESTRING(1 3, 3 3, 3 1, 1 1, 1 3)");
+
+		GEOSGeometry* g = GEOSPolygonizer_getCutEdges(geoms, size);
+
+        ensure(0 != g);
+        ensure_equals(GEOSGetNumGeometries(g), size);
+
+        GEOSGeom_destroy(g);
+        GEOSGeom_destroy(geoms[0]);
+        GEOSGeom_destroy(geoms[1]);
+    }
+
+    template<>
+    template<>
+    void object::test<2>()
+    {
+        const int size = 6;
+        GEOSGeometry* geoms[size] = { 0 };
+
+        // Example from JTS Developer's Guide, Chapter 6 - Polygonization
+	    geoms[0] = GEOSGeomFromWKT("LINESTRING(0 0, 10 10)"); // isolated edge
+	    geoms[1] = GEOSGeomFromWKT("LINESTRING(185 221, 100 100)"); // dangling edge
+	    geoms[2] = GEOSGeomFromWKT("LINESTRING(185 221, 88 275, 180 316)");
+	    geoms[3] = GEOSGeomFromWKT("LINESTRING(185 221, 292 281, 180 316)");
+	    geoms[4] = GEOSGeomFromWKT("LINESTRING(189 98, 83 187, 185 221)");
+	    geoms[5] = GEOSGeomFromWKT("LINESTRING(189 98, 325 168, 185 221)");
+
+		GEOSGeometry* g = GEOSPolygonizer_getCutEdges(geoms, size);
+
+        ensure(0 != g);
+        ensure_equals(GEOSGetNumGeometries(g), 0);
+
+        GEOSGeom_destroy(g);
+
+        for (int i = 0; i < size; ++i)
+        {
+            if (0 != geoms[i])
+                GEOSGeom_destroy(geoms[i]);
+        }
+    }
+    
+} // namespace tut
+



More information about the geos-commits mailing list