# What is the width of the beach of a little island polygon?

This is a "classic" problem, that have no practical utility (see note-1 for applications), but illustrate another GIS problems...

We can do some approximations to modeling the situation,

• the real center (coconut) is not at the ST_Centroid point, but can be approximated to;
• the real shape (a island) is not a circle, neither a square, they are optional references.

So, translating the question to an equation, and using OGC standard functions... "What is the value of w?" at the equation

``````ST_Area(ST_Buffer(ST_Centroid(littleIsland), w, quad_segs)) = ST_Area(littleIsland)
``````

When `quad_segs=1`, the reference is a square, when `quad_segs>=8` the reference is near to a circle (see examples of generated polygons).

NOTE-1: For understand this problem into a context of applications, see "Is there a st_buffer inverse function, that returns a width estimation?" and see "How to fit a polygon to a “best buffer” about a reference geometry in it?".

NOTE-2: the "inverse function" is so general and complex, because can operates with point, line and polygon inputs. Here we can discuss the simplest case, when the input is like a point, with no area or length.

-
depends if it is high tide or low tide.... – Mapperz Sep 25 '12 at 13:15
Yes ;-) Sorry about the "no practical utility" of my little beach... Well, with a real beach you need also the slope... And a really useful model need a function that transforms time into tide height. – Peter Krauss Sep 25 '12 at 18:32
What, precisely, is the question? All we have is a picture. Do you seek some distinguished point within a polygon? If so, what property distinguishes it? E.g., should it (a) be furthest from all exterior points; (b) minimize some average function of distance over the polygon (e.g., the centroid minimizes the squared distance); (c) the center of a minimal bounding circle/rectangle/ellipse/some other shape; (d) minimize some average function of distance to the boundary; (e) the center of a maximal inscribed circle/rectangle/triangle; etc., etc? – whuber Sep 26 '12 at 12:48
Sorry my english, you can edit it... The question is "What is the best estimated mean width, `w`, for a given reference-shape (ex. square or circle)?", where "best estimation" supply equal areas, the `ST_Area(st_buffer(point,bestW,shape))` and the `ST_Area(island)`. My answer below, complements my descriptions... About "best fit" see this complementary question. – Peter Krauss Sep 26 '12 at 14:15
It is still very difficult to determine what you are trying to do. The calculations appear to determine the radius of a circle of a specified area and the side of a square of specified area. What GIS problem does that solve? – whuber Sep 26 '12 at 21:11

I am using this function to solve the problem, but not have any source/reference (do you have? please inform posting a comment)... So, I posted also demonstrations and tests.

``````CREATE FUNCTION pointbuffer_width(g geometry, N integer DEFAULT 8) RETURNS float AS \$\$
-- Returns the value of W in the approximation "g ~ ST_Buffer(ST_Centroid(g),W,'quad_segs=N')"
-- where N>0 indicates the reference-shape, 1=square, 2=heptagon, ... circle.
--
SELECT sqrt(  area/(0.5*n*sin(2*pi()/n))  )
FROM ( SELECT ST_Area(\$1) as area,
(CASE WHEN \$2<=1 THEN 4 ELSE 4*\$2 END)::float as n
) as t;
\$\$ LANGUAGE SQL immutable;
``````

## DEMONSTRATING

The main shapes, circle and square, have areas

``````A = pi*w^2   # circle with radius w
A = 2*w^2    # square circumscribed into a circle of radius w
``````

The intermediary shapes are the regular polygons of `n` sides. The square have `n`=4, the octagon `n`=8, etc. So, we can substitute the factor of `w^2` by a function `f(n)` that supply the adequate value for the `n` sides. The formula of the area of a regular polygon of `n` sides with a circumradius `w`, is:

`````` A = 0.5*n*sin(2*pi/n)*w^2
``````

Where we can check: for `n=4` the factor `0.5*n*sin(2*pi/n)` is 2; for `n=400` (infinite) is 3.14 (~pi).

For the standard `ST_Buffer(g,'quad_segs=N')` polygon generator we can check that this `N` is `4*n`.

Confirmation of `N=4*n` with PostGIS:

``````SELECT ST_AsEWKT((p_geom).geom) As geom_ewkt
FROM (SELECT ST_DumpPoints(
) AS p_geom )  AS t;
``````

It returns 4+1 rows when N=1, 20+1 rows when N=5, etc. The dumped last row repeat the first, to close the polygon.

## TESTING

Tests simulating buffers (need more tests with irregular ones)

``````-- FORMULA TESTER FOR POINTS AND ITS BUFFERS:
SELECT w,
(array['square','octagon','','','','','','~circle'])[shape] as shape,
round(pointbuffer_width(circle8,shape),1) as circle8,
round(pointbuffer_width(octagon,shape),1) as octagon,
round(pointbuffer_width(square,shape),1) as square
FROM (
SELECT
w, shape, -- 1, 2, 3 or 10
ST_Buffer(g, w) as circle8, -- default 8, use 20 for perfect circle
FROM ( SELECT ST_GeomFromText('POINT(100 90)') as g,
unnest(array[1.0,150.0]) as w,
unnest(array[1,2,8]) as shape
) as t
) as t2;
``````

RESULTS:

``````   w   |  shape  | circle8 | octagon | square
-------+---------+---------+---------+--------
1.0 | square  |     1.2 |     1.2 |     *1
150.0 | octagon |   157.6 |    *150 |  126.1
1.0 | ~circle |      *1 |       1 |    0.8
150.0 | square  |   187.4 |   178.4 |   *150
1.0 | octagon |     1.1 |      *1 |    0.8
150.0 | ~circle |    *150 |   142.8 |  120.1
``````

Conclusion: it is stable for any w value (!); it is exact when the modeled shape fits the geometry shape (*).

-