Re: circle @> box, polygon points access - Mailing list pgsql-hackers

From Erik Wienhold
Subject Re: circle @> box, polygon points access
Date
Msg-id 04b8523b-aaf9-4467-b75b-ac929caca085@ewie.name
Whole thread Raw
In response to circle @> box, polygon points access  (Thomas Simpson <ts@talentstack.to>)
List pgsql-hackers
On 2024-11-21 19:18 +0100, Thomas Simpson wrote:
> Looking through the archives, around 20 years ago someone asked about
> getting a list of points from a polygon [15 AUG 2004] and the reply was this
> was not available but something like point(polygon, n) could be added
> relatively easily as a new feature to extract points.
> 
> I'm hitting an issue trying to check if a box is entirely contained within a
> circle and I had thought since that comparison is not directly supported to
> cast the box to a polygon and then check each of the points in the polygon
> since that is what the circle containing a box is essentially going to do.
> 
> However, there does not seem to be an easy way to access the polygon
> elements either (unnest failed, indexing doesn't work and the point(poly,n)
> is not there).
> 
> Can anyone point me to an approach to do what I need or is this going to be
> best by adding the box comparison in the mainline code?  I'm happy to take a
> go if someone points [no pun intended] me to where in the code to go.

You can treat point and box as arrays to access their components.  See
the very last paragraph on [1].  That requires extra parenthesis,
though:

    regress=# select ((box '(1,2),(3,4)')[0])[0];
     box
    -----
       3
    (1 row)

But you'd have to calculate the remaining two points.

I think your specific use case (Is box in a circle?) can be also be
answered by considering the smallest enclosing circle of that box and
test if that circle is contained in your input circle.  For example:

    regress=# select circle(box '(1,2),(3,4)') <@ circle '(0,0),10';
     ?column?
    ----------
     t
    (1 row)

PostGIS may also be worth looking at.

[1] https://www.postgresql.org/docs/current/functions-geometry.html



pgsql-hackers by date:

Previous
From: Andres Freund
Date:
Subject: Re: Forbid to DROP temp tables of other sessions
Next
From: Nathan Bossart
Date:
Subject: Re: Parametrization minimum password lenght