Thread

  1. Re: circle @> box, polygon points access

    Erik Wienhold <ewie@ewie.name> — 2024-11-21T22:05:27Z

    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