[postgis-tickets] [SCM] PostGIS branch main updated. 3.1.0rc1-336-gf2cd119

git at osgeo.org git at osgeo.org
Wed Jul 14 07:13:55 PDT 2021


This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "PostGIS".

The branch, main has been updated
       via  f2cd119bba43fdffd3ace23406d145ce914096a7 (commit)
      from  04aa7f611dbedd47b9b56993696032fbd3ef4331 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
commit f2cd119bba43fdffd3ace23406d145ce914096a7
Author: Sandro Santilli <strk at kbt.io>
Date:   Wed Jul 14 15:49:36 2021 +0200

    Add ptarray_closest_vertex_2d liblwgeom function
    
    Includes unit test.

diff --git a/liblwgeom/cunit/cu_ptarray.c b/liblwgeom/cunit/cu_ptarray.c
index 0ac5fa0..c7cdf48 100644
--- a/liblwgeom/cunit/cu_ptarray.c
+++ b/liblwgeom/cunit/cu_ptarray.c
@@ -653,6 +653,39 @@ static void test_ptarray_scroll()
   lwline_free(line);
 }
 
+static void test_ptarray_closest_vertex_2d()
+{
+  LWLINE *line;
+  POINTARRAY *pa;
+	double dist;
+  POINT2D qp;
+  const char *wkt;
+  char *wktout;
+	int rv;
+
+  wkt = "LINESTRING (0 0 0, 1 0 0, 2 0 0, 3 0 10)";
+  line = lwgeom_as_lwline(lwgeom_from_text(wkt));
+  pa = line->points;
+
+	qp.x = qp.y = 0;
+	rv = ptarray_closest_vertex_2d(pa, &qp, &dist);
+	ASSERT_INT_EQUAL(rv, 0);
+	ASSERT_DOUBLE_EQUAL(dist, 0);
+
+	qp.x = qp.y = 1;
+	rv = ptarray_closest_vertex_2d(pa, &qp, &dist);
+	ASSERT_INT_EQUAL(rv, 1);
+	ASSERT_DOUBLE_EQUAL(dist, 1);
+
+	qp.x = 5; qp.y = 0;
+	rv = ptarray_closest_vertex_2d(pa, &qp, &dist);
+	ASSERT_INT_EQUAL(rv, 3);
+	ASSERT_DOUBLE_EQUAL(dist, 2);
+
+
+  lwline_free(line);
+}
+
 
 /*
 ** Used by the test harness to register the tests in this file.
@@ -671,4 +704,5 @@ void ptarray_suite_setup(void)
 	PG_ADD_TEST(suite, test_ptarrayarc_contains_point);
 	PG_ADD_TEST(suite, test_ptarray_scale);
 	PG_ADD_TEST(suite, test_ptarray_scroll);
+	PG_ADD_TEST(suite, test_ptarray_closest_vertex_2d);
 }
diff --git a/liblwgeom/liblwgeom.h.in b/liblwgeom/liblwgeom.h.in
index bbacc19..a48921f 100644
--- a/liblwgeom/liblwgeom.h.in
+++ b/liblwgeom/liblwgeom.h.in
@@ -1089,6 +1089,15 @@ extern POINTARRAY* ptarray_flip_coordinates(POINTARRAY *pa);
 extern POINTARRAY *ptarray_substring(POINTARRAY *pa, double d1, double d2,
                                                           double tolerance);
 
+/**
+ * @param pa the subject pointarray
+ * @param qp the query point
+ * @param dist optional output for actual distance from vertex
+ */
+extern int ptarray_closest_vertex_2d(const POINTARRAY *pa,
+                                     const POINT2D *qp, double *dist);
+
+
 
 /**
 * Strip out the Z/M components of an #LWGEOM
diff --git a/liblwgeom/ptarray.c b/liblwgeom/ptarray.c
index 5f1f22c..003e3f6 100644
--- a/liblwgeom/ptarray.c
+++ b/liblwgeom/ptarray.c
@@ -1295,6 +1295,35 @@ closest_point_on_segment(const POINT4D *p, const POINT4D *A, const POINT4D *B, P
 	ret->m = A->m + ( (B->m - A->m) * r );
 }
 
+int
+ptarray_closest_vertex_2d(const POINTARRAY *pa, const POINT2D *qp, double *dist)
+{
+	uint32_t t, pn=0;
+	POINT2D *p;
+	double mindist = DBL_MAX;
+
+	/* Loop through pointarray looking for nearest segment */
+	for (t=0; t<pa->npoints; t++)
+	{
+		double dist_sqr;
+		getPoint2d_p_ro(pa, t, &p);
+		dist_sqr = distance2d_sqr_pt_pt(p, qp);
+
+		if (dist_sqr < mindist)
+		{
+			mindist = dist_sqr;
+			pn = t;
+			if ( mindist == 0 )
+			{
+				LWDEBUG(3, "Breaking on mindist=0");
+				break;
+			}
+		}
+	}
+	if ( dist ) *dist = sqrt(mindist);
+	return pn;
+}
+
 /*
  * Given a point, returns the location of closest point on pointarray
  * and, optionally, it's actual distance from the point array.

-----------------------------------------------------------------------

Summary of changes:
 liblwgeom/cunit/cu_ptarray.c | 34 ++++++++++++++++++++++++++++++++++
 liblwgeom/liblwgeom.h.in     |  9 +++++++++
 liblwgeom/ptarray.c          | 29 +++++++++++++++++++++++++++++
 3 files changed, 72 insertions(+)


hooks/post-receive
-- 
PostGIS


More information about the postgis-tickets mailing list