pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Bruce Momjian <bruce@momjian.us>
To: Josef Grahn <josef.grahn@gmail.com>
Cc: pgsql-bugs@postgresql.org
Subject: Re: Bad behaviour on some geometric operations on degenerate polygons
Date: Fri, 14 Feb 2014 23:55:25 -0500
Message-ID: <20140215045525.GC15047@momjian.us> (raw)
In-Reply-To: <CAMBOBOmnoquwMuEiNABC_BKmHuNiW=6DQybZvM2SQUS=S5TT4g@mail.gmail.com>
References: <CAMBOBOmnoquwMuEiNABC_BKmHuNiW=6DQybZvM2SQUS=S5TT4g@mail.gmail.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

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  <bruce@momjian.us>        http://momjian.us
  EnterpriseDB                             http://enterprisedb.com

  + Everyone has their own god. +

-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs

Attachments:

poly.diff (text/x-diff, 1.3K) download 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); ! } }

view thread (5+ messages)  latest in thread

Message-ID: <20140215045525.GC15047@momjian.us>
Permalink:  ../20140215045525.GC15047@momjian.us/
Also on:    postgresql.org/message-id/20140215045525.GC15047@momjian.us

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: bruce@momjian.us, josef.grahn@gmail.com
  Subject: Re: Bad behaviour on some geometric operations on degenerate polygons
  In-Reply-To: <20140215045525.GC15047@momjian.us>
  References: <CAMBOBOmnoquwMuEiNABC_BKmHuNiW=6DQybZvM2SQUS=S5TT4g@mail.gmail.com> <20140215045525.GC15047@momjian.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox