-
Notifications
You must be signed in to change notification settings - Fork 15
Implemeted SQL function center() that calculates the gravity center(centroid) from array of points #33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Implemeted SQL function center() that calculates the gravity center(centroid) from array of points #33
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -143,7 +143,7 @@ | |
</sect3> | ||
|
||
</sect2> | ||
|
||
<sect2 id="funcs.strans"> | ||
<title> | ||
<type>strans</type> functions | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
psql:pg_sphere.test.sql:9153: NOTICE: return type smoc is only a shell | ||
psql:pg_sphere.test.sql:9159: NOTICE: argument type smoc is only a shell | ||
psql:pg_sphere.test.sql:9183: NOTICE: return type smoc is only a shell | ||
psql:pg_sphere.test.sql:9189: NOTICE: argument type smoc is only a shell |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
SELECT center(ARRAY[ | ||
spoint(40.7128, -74.0060), | ||
spoint(34.0522, -118.2437), | ||
spoint(37.7749, -122.4194) | ||
]); | ||
center | ||
---------------------------------------- | ||
(3.04366980631979 , 0.858938068921891) | ||
(1 row) | ||
|
||
SELECT center('{}'::SPoint[]); | ||
center | ||
-------- | ||
|
||
(1 row) | ||
|
||
CREATE FUNCTION spoint_from_xyz(FLOAT8, FLOAT8, FLOAT8) | ||
RETURNS spoint | ||
AS 'pg_sphere', 'spoint_from_xyz' | ||
LANGUAGE 'c' | ||
IMMUTABLE STRICT PARALLEL SAFE; | ||
SELECT spoint_from_xyz(1, 0, 0); | ||
spoint_from_xyz | ||
----------------- | ||
(0 , 0) | ||
(1 row) | ||
|
||
SELECT spoint_from_xyz(0, 0, 0); | ||
spoint_from_xyz | ||
----------------- | ||
(0 , 0) | ||
(1 row) | ||
|
||
SELECT center(ARRAY[ | ||
spoint_from_xyz(1, 0, 0), | ||
spoint_from_xyz(-1, 0, 0) | ||
]); | ||
center | ||
----------------------- | ||
(1.5707963267949 , 0) | ||
(1 row) | ||
|
||
SELECT center(NULL::SPoint[]); | ||
center | ||
-------- | ||
|
||
(1 row) | ||
|
||
SELECT @@ ARRAY[ | ||
spoint(40.7128, -74.0060), | ||
spoint(34.0522, -118.2437), | ||
spoint(37.7749, -122.4194) | ||
] AS center; | ||
center | ||
---------------------------------------- | ||
(3.04366980631979 , 0.858938068921891) | ||
(1 row) | ||
|
||
SELECT @@ ARRAY[]::spoint[] AS center; | ||
center | ||
-------- | ||
|
||
(1 row) | ||
|
||
SELECT @@ ARRAY[ | ||
spoint_from_xyz(1, 0, 0), | ||
spoint_from_xyz(-1, 0, 0) | ||
] AS center; | ||
center | ||
----------------------- | ||
(1.5707963267949 , 0) | ||
(1 row) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
SELECT center(ARRAY[ | ||
spoint(40.7128, -74.0060), | ||
spoint(34.0522, -118.2437), | ||
spoint(37.7749, -122.4194) | ||
]); | ||
|
||
SELECT center('{}'::SPoint[]); | ||
|
||
CREATE FUNCTION spoint_from_xyz(FLOAT8, FLOAT8, FLOAT8) | ||
RETURNS spoint | ||
AS 'pg_sphere', 'spoint_from_xyz' | ||
LANGUAGE 'c' | ||
IMMUTABLE STRICT PARALLEL SAFE; | ||
|
||
SELECT spoint_from_xyz(1, 0, 0); | ||
|
||
SELECT spoint_from_xyz(0, 0, 0); | ||
|
||
SELECT center(ARRAY[ | ||
spoint_from_xyz(1, 0, 0), | ||
spoint_from_xyz(-1, 0, 0) | ||
]); | ||
|
||
SELECT center(NULL::SPoint[]); | ||
|
||
SELECT @@ ARRAY[ | ||
spoint(40.7128, -74.0060), | ||
spoint(34.0522, -118.2437), | ||
spoint(37.7749, -122.4194) | ||
] AS center; | ||
|
||
SELECT @@ ARRAY[]::spoint[] AS center; | ||
|
||
SELECT @@ ARRAY[ | ||
spoint_from_xyz(1, 0, 0), | ||
spoint_from_xyz(-1, 0, 0) | ||
] AS center; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought we had settled on naming this function
centroid
? Why wascenter
chosen?I see it referred to as "centroid" at these URLs:
https://stackoverflow.com/a/38201499/5153779
https://gis.stackexchange.com/questions/43505/calculating-a-spherical-polygon-centroid
However, "center" is used here:
https://github.com/chrisveness/geodesy/blob/8f4ef33d/latlon-nvector-spherical.js#L783
So I'm not sure which term is correct.
Does your implementation give the same results as the Python and JavaScript implementations at https://stackoverflow.com/questions/19897187#answer-38201499 and https://github.com/chrisveness/geodesy/blob/8f4ef33d/latlon-nvector-spherical.js#L783 ? If not, why not?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@vitcpp and I discussed the name "centroid" earlier, but in the end we came to the conclusion that the best name is the center for the sql function to match the interface with the operator @@
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@esabol The main reason to choose 'center' function because such function already exists. It returns the center of circle or ellipse and it is intended to be expanded to other object types. In the doc it is described as center of an object. It was decided to overload this function and allow to pass array of points and the point itself (for interface completeness). It is why we decided to put on display PR with 'center' function. Furthermore, we may utilize operator @@. My opinion is that center and centroid are interchangeable in most cases. So, our idea is to reuse center and to avoid introducing new terms.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, that makes sense (the
center
name) then, but I'd still like an answer to this:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@esabol But this is function, which
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@esabol Sorry... Give me time, then I'll try to figure out why the answers are completely different
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dear Colleagues, it was intended that the proposed center function calculates the mass center of a set of points (particles) that do not form a figure. Such function may be useful in some astronomical tasks when we have to deal with stellar clusters or with other groups of astronomical objects which mass center on the sphere we'd like to find. When we want to find the center of a figure we definitely should use the Stokes theorem as it was implemented in the referenced JS library (center(spoly) should be implemented that way). I guess it is why @stepan-neretin7 sees some differences when trying to compare the proposed function against the function to calculate polygon center from the referenced JS library. Adding some more points on a polygon edge doesn't change the shape of the polygon. But adding more points change the mass center of the set of particles (points). There is another question - is it reasonable to implement such function? I think yes. Probably, the name center is best suited to calculate the center of a figure than to calculate the mass center of a set of points (may be come back to centroid - ?). I'm not sure about having such similar function in the JS library. If yes, it is reasonable to compare our function against its counterpart.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK with me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@vitcpp @esabol
Dear colleagues, I want to revive the discussion of my pull-request. I have two questions that I would like to discuss? First; After all, we'd better leave such an implementation (the center of an arbitrary set of points, not a polygon.. If we add more points on the side of an imaginary polygon from points, then my implementation will shift to this side) or we will think in the direction of that implementation for polygons through the Stokes theorem
Second: When two points are at the poles: point(0,-pi()/2), point(0,pi()/2), then the middle point will be ~epsilon and it is incorrect to translate this into lat, lng, because such a point is not projected onto the sphere. What should I give out in this case? NULL? But let's say the case with the points 0,0 and 0, pi
The center here is any point on a large circle perpendicular to the line connecting the points
What is better to return here? NAN? NULL?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Deal All, the final result of this operation is to find centroid of a set of points, not the center of a polygon. We do not find the center of a figure like polygon in this task. The averaged vector is pretty fine solution. There are two special cases. Case 1: we find the center of two opposite points. In this case, the equidistant place is a circle, not a point. The geometric
center in the Cartesian coordinate system is the center of the sphere. Case 2: when we have, for example, two points on poles and 4 points on equator with longitudes 0, 90, 180, 270 degrees. In this case the geometric center in 3D is (0,0,0) but there is no geometric center on the sphere. Case 3: we have all points on the same big circle. In this case, there are two points are equidistant. I'm not sure about other special cases. I guess, there are no other special cases.
This function may be useful to find the approximate center of a group of astronomical objects that are placed in a local area on the sphere: the center of a star cluster, the center of a selected group of objects, etc. I think the defined special cases are impractical; I'm not sure in what tasks it can happen.
I propose this function to return NULL if 3D geometric center is (0,0,0) +- EPSILON. It means, the algorithm can't find centroid of the point set. I think, NULL is better because one can use ifull or coalesce to define some result in such cases.