Find central ploygon

Balasaheb Molawade 136 Reputation points
2024-06-20T07:03:22.71+00:00

Hi,

We are using the Bing Maps API to plot polygons on a map. We plot around 10K to 15K polygons, and we want to find the central polygon to show the name of the plotted region. Is there any way to find the central polygon and display a label on it?

This is necessary because we plot multiple regions on the map, each containing thousands of polygons. To easily understand which region is in which area, displaying a label is the best option.

Untitled Thanks

Azure Maps
Azure Maps
An Azure service that provides geospatial APIs to add maps, spatial analytics, and mobility solutions to apps.
649 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. rbrundritt 16,331 Reputation points Microsoft Employee
    2024-06-20T16:01:56.9666667+00:00

    There are a lot of different ways to do this as "central" is a bit ambiguous. Here are a couple examples of what "central polygon" could mean:

    1. Take the bounding box (or bounding circle) of all polygons you have and find the polygon that is in the center of that. It's possible that no polygon is under the center coordinate.
    2. Merge all the polygons together to create one big polygon, then find it's centroid and use that as there center. Again its possible that no polygon is under the center coordinate.
    3. There is a geospatial center and a visual center, these are not the same as the map projection stretches the more you move towards the poles. If your map displays all the data nicely, it is likely centered based on the visual center of the bounding box.
    4. Center could also be calculated based on the center of mass of coordinates, or based on distances between edges (e.g. Voronoi diagram).

    For 1 and 2, a good example of when a center may not intersect a polygon is if the shape of the overall area is like a C. In your case, if no polygon intersects the "center" point, you could simply grab the nearest polygon.

    Bing Maps does have a built-in spatial math library that could be used, however, given the recent retirement announcement of Bing Maps, relying on an open source like [Turf.js] would be smart and make it easy to migrate to Azure Maps in the future (Azure Maps is uses open data standards which work with tons of open-source libraries).

    I've put together a code sample showing several different methods here: https://rbrundritt.azurewebsites.net/Demos/AzureMaps/PolygonCollectionCenter/index.html