pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Bad behaviour on some geometric operations on degenerate polygons
5+ messages / 3 participants
[nested] [flat]

* Bad behaviour on some geometric operations on degenerate polygons
@ 2013-10-15 14:03  Josef Grahn <josef.grahn@gmail.com>
  0 siblings, 1 reply; 5+ messages in thread

From: Josef Grahn @ 2013-10-15 14:03 UTC (permalink / raw)
  To: pgsql-bugs

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.


Best Regards,

Josef Grahn

^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Bad behaviour on some geometric operations on degenerate polygons
@ 2014-02-15 04:55  Bruce Momjian <bruce@momjian.us>
  parent: Josef Grahn <josef.grahn@gmail.com>
  0 siblings, 1 reply; 5+ messages in thread

From: Bruce Momjian @ 2014-02-15 04:55 UTC (permalink / raw)
  To: Josef Grahn <josef.grahn@gmail.com>; +Cc: pgsql-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); ! } }
^ permalink raw reply [nested|flat] 5+ messages in thread

* Re: Bad behaviour on some geometric operations on degenerate polygons
@ 2014-02-15 05:00  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Bruce Momjian <bruce@momjian.us>
  0 siblings, 1 reply; 5+ messages in thread

From: Tom Lane @ 2014-02-15 05:00 UTC (permalink / raw)
  To: Bruce Momjian <bruce@momjian.us>; +Cc: Josef Grahn <josef.grahn@gmail.com>; pgsql-bugs

Bruce Momjian <bruce@momjian.us> writes:
> 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.

Maybe we should rethink that.  What you've done here seems like a kluge.

			regards, tom lane


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



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Bad behaviour on some geometric operations on degenerate polygons
@ 2014-02-15 05:07  Bruce Momjian <bruce@momjian.us>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 5+ messages in thread

From: Bruce Momjian @ 2014-02-15 05:07 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Josef Grahn <josef.grahn@gmail.com>; pgsql-bugs

On Sat, Feb 15, 2014 at 12:00:09AM -0500, Tom Lane wrote:
> Bruce Momjian <bruce@momjian.us> writes:
> > 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.
> 
> Maybe we should rethink that.  What you've done here seems like a kluge.

Yes, it would be much easier to allow the zero-radius circles.  The
attached smaller patch allows all the cases to work:

	test=> SELECT @@ polygon '((1, 1))';
	 ?column?
	----------
	 (1,1)
	(1 row)
	
	test=> SELECT point(polygon '((1, 1))');
	 point
	-------
	 (1,1)
	(1 row)
	
	test=> SELECT circle(polygon '((1, 1))');
	  circle
	-----------
	 <(1,1),0>
	(1 row)

-- 
  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:

geo2.diff (text/x-diff, 1.0K) 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..e8fbe4e *** a/src/backend/utils/adt/geo_ops.c --- b/src/backend/utils/adt/geo_ops.c *************** poly_circle(PG_FUNCTION_ARGS) *** 5165,5171 **** CIRCLE *circle; int i; ! if (poly->npts < 2) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("cannot convert empty polygon to circle"))); --- 5165,5171 ---- CIRCLE *circle; int i; ! if (poly->npts < 1) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("cannot convert empty polygon to circle"))); *************** poly_circle(PG_FUNCTION_ARGS) *** 5188,5198 **** circle->radius += point_dt(&poly->p[i], &circle->center); circle->radius /= poly->npts; - if (FPzero(circle->radius)) - ereport(ERROR, - (errcode(ERRCODE_INVALID_PARAMETER_VALUE), - errmsg("cannot convert empty polygon to circle"))); - PG_RETURN_CIRCLE_P(circle); } --- 5188,5193 ----
^ permalink raw reply [nested|flat] 5+ messages in thread

* Re: Bad behaviour on some geometric operations on degenerate polygons
@ 2014-02-24 17:24  Bruce Momjian <bruce@momjian.us>
  parent: Bruce Momjian <bruce@momjian.us>
  0 siblings, 0 replies; 5+ messages in thread

From: Bruce Momjian @ 2014-02-24 17:24 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Josef Grahn <josef.grahn@gmail.com>; pgsql-bugs

On Sat, Feb 15, 2014 at 12:07:20AM -0500, Bruce Momjian wrote:
> On Sat, Feb 15, 2014 at 12:00:09AM -0500, Tom Lane wrote:
> > Bruce Momjian <bruce@momjian.us> writes:
> > > 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.
> > 
> > Maybe we should rethink that.  What you've done here seems like a kluge.
> 
> Yes, it would be much easier to allow the zero-radius circles.  The
> attached smaller patch allows all the cases to work:

Applied.

-- 
  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



^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2014-02-24 17:24 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2013-10-15 14:03 Bad behaviour on some geometric operations on degenerate polygons Josef Grahn <josef.grahn@gmail.com>
2014-02-15 04:55 ` Bruce Momjian <bruce@momjian.us>
2014-02-15 05:00   ` Tom Lane <tgl@sss.pgh.pa.us>
2014-02-15 05:07     ` Bruce Momjian <bruce@momjian.us>
2014-02-24 17:24       ` Bruce Momjian <bruce@momjian.us>

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