Skip to content
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

Calculate centroid? #175

Open
sckott opened this issue Mar 22, 2023 · 5 comments
Open

Calculate centroid? #175

sckott opened this issue Mar 22, 2023 · 5 comments

Comments

@sckott
Copy link

sckott commented Mar 22, 2023

Is there a way to calculate a centroid of a wkt polygon/multipolygon with wk? I don't see anything, but may have missed it. I realize there's methods in other R pkgs, but I'd prefer to avoid heavy dependencies like them

@paleolimbot
Copy link
Owner

I've never looked into it! I am guessing that the algorithm for multipolygon is not trivial and is best left to geos, sf, or s2.

@sckott
Copy link
Author

sckott commented Mar 22, 2023

Okay, thanks

@mdsumner
Copy link
Contributor

mdsumner commented Apr 23, 2023

it might be pretty trivial, depending on what kind of centroid you want, there's weighted, circle, box, inner, and any number of possible variants for coalescing from multiple rings to a point, or just choosing something - and even given what you might care about with respect to the coordinate system in use

what kind of centroid do you want? does it have to intersect the polygon? I'm interested to expand this out and have been thinking about it for a long time, there's some odd advice out there that "this centroid is not correct" but doesn't actually define any aspects of what correct might be. From my GIS days you would choose weighted (just average the coords), box (find the centre of the bounding box), circle (centre of minimum enclosing circle), or inner (something like a centre that's actually intersecting). Then there's the geographic reality, but it's not just an issue for longlat, every projection has questionable properties for what "centroid" might mean. :) There's that mapbox project that finds the most "inaccessible point" (it's super cool), and there's endless things you can do with a triangulated version of polygon to classify something meaningful. (With {geos} all these things would be easy enough to explore I think). I will eventually, but maybe you just want a simple thing?

@mdsumner
Copy link
Contributor

You've got me really interested now, we've got access to everything we need with {geos}, I think:

https://github.com/mapbox/polylabel

@sckott
Copy link
Author

sckott commented Apr 25, 2023

Thanks for popping in @mdsumner

I ended up using s2::s2_centroid (PR link). The use case was for a POLYGON or MULTIPOLYGON. It's for a package I used to maintain but no longer do - so I'm not super invested in this. The goal in this use case was to be able to limit dependencies, i.e., just have wk do all the stuff we needed, but had to go to s2 for the centroid calculation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants