[geos-commits] r3792 - in trunk: . capi tests/unit tests/unit/capi
svn_geos at osgeo.org
svn_geos at osgeo.org
Fri Mar 15 11:05:58 PDT 2013
Author: strk
Date: 2013-03-15 11:05:58 -0700 (Fri, 15 Mar 2013)
New Revision: 3792
Added:
trunk/tests/unit/capi/GEOSNearestPointsTest.cpp
Modified:
trunk/NEWS
trunk/capi/geos_c.cpp
trunk/capi/geos_c.h.in
trunk/capi/geos_ts_c.cpp
trunk/tests/unit/Makefile.am
Log:
Add GEOSNearestPoints CAPI function
Contributed by Richard Frith-Macdonald <richard at tiptree.demon.co.uk>
Modified: trunk/NEWS
===================================================================
--- trunk/NEWS 2013-03-15 15:36:19 UTC (rev 3791)
+++ trunk/NEWS 2013-03-15 18:05:58 UTC (rev 3792)
@@ -9,6 +9,7 @@
- BufferInputLineSimplifier header exposed (#548)
- New Centroid class supporting mixed geometry components (#612)
- io::Writer::reserve() method
+ - CAPI: GEOSNearestPoints
- C++ API changes:
- New noding::GeometryNoder class
- Added BufferOp::setSingleSided
Modified: trunk/capi/geos_c.cpp
===================================================================
--- trunk/capi/geos_c.cpp 2013-03-15 15:36:19 UTC (rev 3791)
+++ trunk/capi/geos_c.cpp 2013-03-15 18:05:58 UTC (rev 3792)
@@ -302,6 +302,12 @@
return GEOSLength_r( handle, g, length );
}
+CoordinateSequence *
+GEOSNearestPoints(const Geometry *g1, const Geometry *g2)
+{
+ return GEOSNearestPoints_r( handle, g1, g2 );
+}
+
Geometry *
GEOSGeomFromWKT(const char *wkt)
{
Modified: trunk/capi/geos_c.h.in
===================================================================
--- trunk/capi/geos_c.h.in 2013-03-15 15:36:19 UTC (rev 3791)
+++ trunk/capi/geos_c.h.in 2013-03-15 18:05:58 UTC (rev 3792)
@@ -1191,6 +1191,15 @@
extern int GEOS_DLL GEOSGeomGetLength_r(GEOSContextHandle_t handle,
const GEOSGeometry *g1, double *length);
+/* Return 0 on exception, the closest points of the two geometries otherwise.
+ * The first point comes from g1 geometry and the second point comes from g2.
+ */
+extern GEOSCoordSequence GEOS_DLL *GEOSNearestPoints(
+ const GEOSGeometry* g1, const GEOSGeometry* g2);
+extern GEOSCoordSequence GEOS_DLL *GEOSNearestPoints_r(
+ GEOSContextHandle_t handle, const GEOSGeometry* g1, const GEOSGeometry* g2);
+
+
/************************************************************************
*
* Algorithms
Modified: trunk/capi/geos_ts_c.cpp
===================================================================
--- trunk/capi/geos_ts_c.cpp 2013-03-15 15:36:19 UTC (rev 3791)
+++ trunk/capi/geos_ts_c.cpp 2013-03-15 18:05:58 UTC (rev 3792)
@@ -46,17 +46,18 @@
#include <geos/simplify/DouglasPeuckerSimplifier.h>
#include <geos/simplify/TopologyPreservingSimplifier.h>
#include <geos/noding/GeometryNoder.h>
-#include <geos/operation/valid/IsValidOp.h>
-#include <geos/operation/polygonize/Polygonizer.h>
+#include <geos/operation/buffer/BufferBuilder.h>
+#include <geos/operation/buffer/BufferOp.h>
+#include <geos/operation/buffer/BufferParameters.h>
+#include <geos/operation/distance/DistanceOp.h>
#include <geos/operation/linemerge/LineMerger.h>
#include <geos/operation/overlay/OverlayOp.h>
#include <geos/operation/overlay/snap/GeometrySnapper.h>
-#include <geos/operation/union/CascadedPolygonUnion.h>
-#include <geos/operation/buffer/BufferOp.h>
-#include <geos/operation/buffer/BufferParameters.h>
-#include <geos/operation/buffer/BufferBuilder.h>
+#include <geos/operation/polygonize/Polygonizer.h>
#include <geos/operation/relate/RelateOp.h>
#include <geos/operation/sharedpaths/SharedPathsOp.h>
+#include <geos/operation/union/CascadedPolygonUnion.h>
+#include <geos/operation/valid/IsValidOp.h>
#include <geos/linearref/LengthIndexedLine.h>
#include <geos/triangulate/DelaunayTriangulationBuilder.h>
#include <geos/util/IllegalArgumentException.h>
@@ -1150,6 +1151,39 @@
return 0;
}
+CoordinateSequence *
+GEOSNearestPoints_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
+{
+ if ( 0 == extHandle )
+ {
+ return NULL;
+ }
+
+ GEOSContextHandleInternal_t *handle = 0;
+ handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
+ if ( 0 == handle->initialized )
+ {
+ return NULL;
+ }
+
+ try
+ {
+ if (g1->isEmpty() || g2->isEmpty()) return 0;
+ return geos::operation::distance::DistanceOp::nearestPoints(g1, g2);
+ }
+ catch (const std::exception &e)
+ {
+ handle->ERROR_MESSAGE("%s", e.what());
+ }
+ catch (...)
+ {
+ handle->ERROR_MESSAGE("Unknown exception thrown");
+ }
+
+ return NULL;
+}
+
+
Geometry *
GEOSGeomFromWKT_r(GEOSContextHandle_t extHandle, const char *wkt)
{
Modified: trunk/tests/unit/Makefile.am
===================================================================
--- trunk/tests/unit/Makefile.am 2013-03-15 15:36:19 UTC (rev 3791)
+++ trunk/tests/unit/Makefile.am 2013-03-15 18:05:58 UTC (rev 3792)
@@ -116,10 +116,11 @@
capi/GEOSDistanceTest.cpp \
capi/GEOSInterruptTest.cpp \
capi/GEOSIntersectsTest.cpp \
+ capi/GEOSNearestPointsTest.cpp \
capi/GEOSWithinTest.cpp \
capi/GEOSSimplifyTest.cpp \
capi/GEOSPreparedGeometryTest.cpp \
- capi/GEOSPointOnSurfaceTest.cpp \
+ capi/GEOSPointOnSurfaceTest.cpp \
capi/GEOSPolygonizer_getCutEdgesTest.cpp \
capi/GEOSBufferTest.cpp \
capi/GEOSOffsetCurveTest.cpp \
Added: trunk/tests/unit/capi/GEOSNearestPointsTest.cpp
===================================================================
--- trunk/tests/unit/capi/GEOSNearestPointsTest.cpp (rev 0)
+++ trunk/tests/unit/capi/GEOSNearestPointsTest.cpp 2013-03-15 18:05:58 UTC (rev 3792)
@@ -0,0 +1,120 @@
+// $Id: GEOSNearestPointsTest.cpp 2424 2009-04-29 23:52:36Z mloskot $
+//
+// Test Suite for C-API GEOSNearestPoints
+
+#include <tut.hpp>
+// geos
+#include <geos_c.h>
+// std
+#include <cstdarg>
+#include <cstdio>
+#include <cstdlib>
+
+namespace tut
+{
+ //
+ // Test Group
+ //
+
+ // Common data used in test cases.
+ struct test_capigeosnearestpoints_data
+ {
+ GEOSGeometry *geom1_;
+ GEOSGeometry *geom2_;
+
+ 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_capigeosnearestpoints_data()
+ : geom1_(0), geom2_(0)
+ {
+ initGEOS(notice, notice);
+ }
+
+ ~test_capigeosnearestpoints_data()
+ {
+ GEOSGeom_destroy(geom1_);
+ GEOSGeom_destroy(geom2_);
+ geom1_ = 0;
+ geom2_ = 0;
+ finishGEOS();
+ }
+
+ };
+
+ typedef test_group<test_capigeosnearestpoints_data> group;
+ typedef group::object object;
+
+ group test_capigeosnearestpoints_group("capi::GEOSNearestPoints");
+
+ //
+ // Test Cases
+ //
+
+ template<>
+ template<>
+ void object::test<1>()
+ {
+ geom1_ = GEOSGeomFromWKT("POLYGON EMPTY");
+ geom2_ = GEOSGeomFromWKT("POLYGON EMPTY");
+
+ ensure( 0 != geom1_ );
+ ensure( 0 != geom2_ );
+
+ GEOSCoordSequence *coords_;
+ coords_ = GEOSNearestPoints(geom1_, geom2_);
+
+ ensure( 0 == coords_ );
+ }
+
+ template<>
+ template<>
+ void object::test<2>()
+ {
+ geom1_ = GEOSGeomFromWKT("POLYGON((1 1,1 5,5 5,5 1,1 1))");
+ // geom2_ = GEOSGeomFromWKT("POINT(8 8)");
+ geom2_ = GEOSGeomFromWKT("POLYGON((8 8, 9 9, 9 10, 8 8))");
+
+ ensure( 0 != geom1_ );
+ ensure( 0 != geom2_ );
+
+ GEOSCoordSequence *coords_;
+ coords_ = GEOSNearestPoints(geom1_, geom2_);
+
+ ensure( 0 != coords_ );
+
+ unsigned int size;
+ GEOSCoordSeq_getSize(coords_, &size);
+ ensure( 2 == size );
+
+ double x1, x2, y1, y2;
+
+ /* Point in geom1_
+ */
+ GEOSCoordSeq_getOrdinate(coords_, 0, 0, &x1);
+ GEOSCoordSeq_getOrdinate(coords_, 0, 1, &y1);
+
+ /* Point in geom2_
+ */
+ GEOSCoordSeq_getOrdinate(coords_, 1, 0, &x2);
+ GEOSCoordSeq_getOrdinate(coords_, 1, 1, &y2);
+
+ ensure( 5 == x1 );
+ ensure( 5 == y1 );
+ ensure( 8 == x2 );
+ ensure( 8 == y2 );
+
+ GEOSCoordSeq_destroy(coords_);
+ }
+
+} // namespace tut
+
More information about the geos-commits
mailing list