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

Is there a way to get the center of the polygon? #145

Closed
bagusflyer opened this issue Apr 23, 2021 · 3 comments
Closed

Is there a way to get the center of the polygon? #145

bagusflyer opened this issue Apr 23, 2021 · 3 comments
Labels
improvement Improvement for an existing feature. Java parity JS parity
Milestone

Comments

@bagusflyer
Copy link

Is there a way to get the center of the polygon by using turf-swift?

@Udumft
Copy link
Contributor

Udumft commented Apr 30, 2021

Hello, @bagusflyer! Unfortunately there is no API method to do that. But I think it should be as simple as calculating "average" among all of Polygon's coordinates.

@1ec5
Copy link
Contributor

1ec5 commented Apr 30, 2021

But I think it should be as simple as calculating "average" among all of Polygon's coordinates.

That’s one kind of center, but it only yields reasonable results for convex polygons.

Turf.js has several center- and centroid-related functions that we could port to Swift:

The iOS map SDK (until v10) has an MGLPolyline.coordinate property that’s very useful for this purpose. Under the hood, it relies on Polylabel, a C++ implementation of the pole-of-inaccessibility algorithm (equivalent to ST_MaximumInscribedCircle in PostGIS). There are other centroid algorithms, but the pole of inaccessibility algorithm yields very good results overall. Now that iOS map SDK v10 relies on Turf for geometric functionality, it’s going to be more important that Turf provides some sort of centroid functionality equivalent to previous map SDK releases.

/cc @mapbox/maps-ios @mapbox/navigation-ios

@1ec5
Copy link
Contributor

1ec5 commented Jul 27, 2021

Fixed in #148.

@1ec5 1ec5 closed this as completed Jul 27, 2021
@1ec5 1ec5 added this to the v2.0.0 (RC) milestone Jul 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
improvement Improvement for an existing feature. Java parity JS parity
Projects
None yet
Development

No branches or pull requests

3 participants