Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WEXHm-00082B-SP for pgsql-bugs@arkaria.postgresql.org; Sat, 15 Feb 2014 04:55:31 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1WEXHm-0000Dx-C4 for pgsql-bugs@arkaria.postgresql.org; Sat, 15 Feb 2014 04:55:30 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WEXHl-0000Dr-KH for pgsql-bugs@postgresql.org; Sat, 15 Feb 2014 04:55:29 +0000 Received: from momjian.us ([72.94.173.45]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WEXHi-00078K-CE for pgsql-bugs@postgresql.org; Sat, 15 Feb 2014 04:55:28 +0000 Received: from bruce by momjian.us with local (Exim 4.72) (envelope-from ) id 1WEXHh-0001ZQ-JA; Fri, 14 Feb 2014 23:55:25 -0500 Date: Fri, 14 Feb 2014 23:55:25 -0500 From: Bruce Momjian To: Josef Grahn Cc: pgsql-bugs@postgresql.org Subject: Re: Bad behaviour on some geometric operations on degenerate polygons Message-ID: <20140215045525.GC15047@momjian.us> References: MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="GPJrCs/72TxItFYR" Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.20 (2009-06-14) X-Pg-Spam-Score: -2.6 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org --GPJrCs/72TxItFYR Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Tue, Oct 15, 2013 at 04:03:58PM +0200, Josef Grahn wrote: > Hello. > > > I encountered an unexpected behaviour when doing some geometric operations on > polygons that consist of only one point (or more generally, when all points in > the polygon are identical). > > Specifically, taking the center point of such a polygon results in an error > message. > > == Code to reproduce == > SELECT @@ polygon '((1, 1))'; > > == Expected result == > (1,1) > > == Actual result == > ERROR: cannot convert empty polygon to circle > > The same problem exists when one tries to convert a polygon to a point using > the point() function (presumably because the same code path is invoked). > Ironically, the consequence of this is that the only non-empty polygon that can > *not* be converted to a point, is one that occupy precisely a single point. > > Other operations, such as overlap tests, on the other hand seem to work as one > would expect, which leads me to conclude that handling of degenerate polygons > is meant to function, and that the behaviour of this particular operation is > simply a bug. > > This bug report applies to at least PostgreSQL versions 9.1.9 and 9.3.0. Other > versions, I have not tested. Thank you for the report; I am just getting to study it. I think you are right. What is happening is that the system wants to convert the poly to a circle, then take its center, but we don't support poly-to-circles with radius zero. What I have done is to add special code to allow getting the center of a single-point polygon _without_ converting to a circle. I think this fixes the two cases you mentioned: test=> SELECT @@ polygon '((1, 1))'; ?column? ---------- (1,1) (1 row) test=> SELECT point(polygon '((1, 1))'); point ------- (1,1) (1 row) but still prevents the creation of a zero-radius circle from a poly: test=> SELECT circle(polygon '((1, 1))'); ERROR: cannot convert empty polygon to circle What is odd however, is that we allow _explicit_ creation of zero-radius circles: test=> SELECT circle '<(1,1),0>'; circle ----------- <(1,1),0> (1 row) so why do we prevent the conversion of single-point polygons to circles with zero radius? I don't know. Patch attached. -- Bruce Momjian http://momjian.us EnterpriseDB http://enterprisedb.com + Everyone has their own god. + --GPJrCs/72TxItFYR Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="poly.diff" diff --git a/src/backend/utils/adt/geo_ops.c b/src/backend/utils/adt/geo_ops.c new file mode 100644 index 0a68be6..154e45b *** a/src/backend/utils/adt/geo_ops.c --- b/src/backend/utils/adt/geo_ops.c *************** Datum *** 4380,4394 **** poly_center(PG_FUNCTION_ARGS) { POLYGON *poly = PG_GETARG_POLYGON_P(0); - Datum result; - CIRCLE *circle; ! circle = DatumGetCircleP(DirectFunctionCall1(poly_circle, ! PolygonPGetDatum(poly))); ! result = DirectFunctionCall1(circle_center, ! CirclePGetDatum(circle)); - PG_RETURN_DATUM(result); } --- 4380,4413 ---- poly_center(PG_FUNCTION_ARGS) { POLYGON *poly = PG_GETARG_POLYGON_P(0); ! if (poly->npts == 1) ! { ! /* ! * For some reason we don't support the conversion of single-point ! * polygons to zero-radius circles, so we just return a point. ! */ ! Point *result; ! ! result = (Point *) palloc(sizeof(Point)); ! result->x = poly->p[0].x; ! result->y = poly->p[0].y; ! ! PG_RETURN_POINT_P(result); ! } ! else ! { ! CIRCLE *circle; ! Datum result; ! ! circle = DatumGetCircleP(DirectFunctionCall1(poly_circle, ! PolygonPGetDatum(poly))); ! result = DirectFunctionCall1(circle_center, ! CirclePGetDatum(circle)); ! ! PG_RETURN_DATUM(result); ! } } --GPJrCs/72TxItFYR Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs --GPJrCs/72TxItFYR--