Showing results for 
Search instead for 
Did you mean: 

Alteryx designer Discussions

Find answers, ask questions, and share expertise about Alteryx Designer.

New macro on the Public Gallery: SmartCentroid

A new macro is available on the Public Gallery named SmartCentroid. A more accurate name would be The Centroid of the Largest Circle That Can Fit Inside Of A Polygon. But that was too long.



The centroid created by most GIS packages is simply the center of the Minimum Bounding Rectangle (MBR) of the polygon. Some software improves upon this centroid by ensuring that the centroid is actually inside of the polygon. (Imagine the letter C to imagine a centroid that could be outside the polygon) But what I want is a point inside of the polygon that will have the most area around it. The purpose would be to label the polygon with the most whitespace around the label.



I start by taking the MBR of the polygon and then use that to calculate the width and height. I then find the shortest side of the MBR and then divide that number by 100. The provides me with the size of a grid that I will use to cover the entire polygon. I then get the centroid of each of those grids. I then find the distance from each of those centroids to the edge of the polygon. I then sort all of those distances to find the point that is farthest from the edge of the polygon. This distance is now the radius of the largest circle that will fit into the polygon.



1 - The methodology is not elegant. It is brute force. There is a potential for greater performance and more efficient time to start with a larger grid size, choose a small subset of candidates, and then redo the analysis with a smaller grid size.


2 - The result is not exact. It is an approximation. However, with a small enough grid size the difference between the actual centroid of the largest circle and this result could be negligible.


The attached file is a picture of a zip code with two centroids. The point on the edge of the polygon is the result of using the Spatial Info tool in Alteryx. The point in the 'fattest' part of the polygon was calculated by this macro.


I would love to see the community to take this macro and improve upon it. Please let me know.



John Hollingsworth
Clear Channel Outdoor
ACE Emeritus
ACE Emeritus

This is really interesting.  I like your solution and am curious if you have a sample polygon that is prohibitively slow to calculate using your method.  That would help make it more obvious if anything we try actually improves upon your solution, which runs almost instantly on the sample provided.  Thanks!

Good question. I used a small set of ten zip codes when I was testing the macro. That set was small enough that I could rapidly test (~5 seconds), but large enough that I could see changes in the time as I made improvements.


When attempting to improve the macro, I would suggest creating a set of 10-20 polygons and see if your run time decreases due to your changes.


Also, congrats on becoming an ACE!

John Hollingsworth
Clear Channel Outdoor
ACE Emeritus
ACE Emeritus

John, this is so clever.  It solves a gap in Alteryx's labeling functionality in a replicable and scalable way for different polygons types.  I'll be incorporating it into some of our workflows, thanks for sharing!

This macro is now named LabelCentroid as I think that makes more sense. I made a number of updates over the weekend. 


The tool now runs faster as I added a process that starts at a low resolution and then increases the resolution of the grid creation.

The tool is also more robust as I found problems during additional testing and fixed them.

John Hollingsworth
Clear Channel Outdoor
ACE Emeritus
ACE Emeritus

Good work ACE!