postgrespro / pgsphere

PgSphere provides spherical data types, functions, operators, and indexing for PostgreSQL.
https://pgsphere.org
BSD 3-Clause "New" or "Revised" License
17 stars 14 forks source link

Implement @@spoly #129

Open msdemlei opened 2 months ago

msdemlei commented 2 months ago

Currently, the @@ operator only works for scircles (and I think spoints). It is occasionally useful to also obtain the centroid of a polygon.

I have not thought about this in depth, but I think it ought to be fine just to compute the mean of the vertexes; what with poles and the stitching line, this has quite a few subtleties, too. What I do in some non-DB code is this:

        vertices = [numpy.array(mathtricks.spherToCart(p.x, p.y))
            for p in self.points]
        center = numpy.average(vertices, axis=0)
        center = center/(center@center)
        return mathtricks.cartToSpher(center)

– that is, I'm taking the mean of the unit sphere coordinates of the vertices and then convert the resulting vector (after normalisation) to spherical coordinates. Do people think this is the right way to do that?

esabol commented 2 months ago

@msdemlei : There was an attempt at implementing this in PR #33, so you should read through that. @vitvpp closed that issue due to "conceptual problems", presumably related to whether such a function should calculate the true centroid in 3D space or its projection on the unit sphere surface (the moment?).

At the time, my research into the issue led me to the following links:

https://stackoverflow.com/questions/19897187/locating-the-centroid-center-of-mass-of-spherical-polygons (but especially this answer: https://stackoverflow.com/questions/19897187#answer-38201499) https://github.com/chrisveness/geodesy/blob/8f4ef33d/latlon-nvector-spherical.js#L783 (a JavaScript implementation of that answer)

msdemlei commented 2 months ago

On Tue, Aug 13, 2024 at 08:44:13AM -0700, Ed Sabol wrote:

There was an attempt at implementing this in PR #33, so you should

Ooops. Sorry I've missed that (I wonder how that happened, given my notification is on "All activity"... hm).

but especially this answer: https://stackoverflow.com/questions/19897187#answer-38201499)

This seems eminently implementable and mathematically reasonable. I'd further say that non-surprise (i.e., @@ just working) is enough of a requirement to make putting this into pgsphere worthwhile, no?

@vitcpp, do you still see conceptual problems in @@spoly or were your doubts specifically on what PR #33 was doing?

NB I'm not promising I'll produce code soon-ish; regrettably in this context, I have no urgent requirement, and I see that there is a problem I'd have to research: Don Hatch's stackoverflow answer assumes a set winding direction, which is reasonable; our polygons, on the other hand, are unreasonable (winding direction depending on how their they'll be smaller than 2 pi). One would have to establish the winding direction of the particular polygon before running Hatch's code.

Can anyone point to code in pgsphere that would already do that?

esabol commented 2 months ago

@msdemlei wrote:

One would have to establish the winding direction of the particular polygon before running Hatch's code.

The JavaScript implementation I linked to seemingly just negates the angle of the vector to handle that? It has the following code comment: "if centreV is pointing in opposite direction to 1st vertex (depending on cw/ccw), negate it".

Can anyone point to code in pgsphere that would already do that?

That sounds like issue #2. Nobody has tackled it yet.

esabol commented 2 months ago

By the way, there's been no sign of @vitcpp on GitHub since June 3. I hope he's ok!

vitcpp commented 2 months ago

Dear All,

I'm ok and online, thanks! Sorry for the delayed answer. There was an attempt to implement centroid() function for an array of points. We faced some "conceptual" problems. The solution was incomplete at that moment, I decided to close it. It is not a problem to reopen.

There are some problems with the centroid() function that takes the array of points:

We have to decide which result to return in each case. If needed, we can reopen the issue #33.

For spoly I do not see such issues because spoly is a closed figure with interior. There should be no conceptual problems to find the center of the polygon interior on spherical 2d surface.