Calculate Polygon Centroid Coordinates

627
4
08-08-2023 04:31 AM
MariusVerdes
New Contributor III

Hello,

 

I am trying to create an attribute rule to automatically calculate the centroid x and y coordinates of polygons from one layer. I managed to add the x and y centroid coordinates in decimal degrees using the "Calculate Feature Geometry" tool, then "centroid x/y-coordinates". 

Since this layer is populated by my colleagues, I want to create an attribute rule that will do this automatically when a new feature is added or an existing one is edited. I already created 2 specific fields "X" and "Y" in which I added the centroid coordinates using the geoprocessing tool. 

Can someone explain to me how I can create an Arcade expression that will calculate the centroid x/y coordinates of the polygons? Also, I would like to be stored in my X and Y fields. 

P.S. I don't want to create any centroid point, I just want the centroid coordinates as the geoprocessing tool does.

Thanks!

0 Kudos
4 Replies
clt_cabq
Occasional Contributor III

here's the arcade expression that you can add in the attribute rules, you'll need to add a rule for each field and set the 'triggering' event that calculates it (insert, update, or delete).

centroid($feature).x

Here's a help article on this topic too - this shows examples using points but the centroid function should get you what you need.

How To: Calculate XY Values Using Arcade in ArcGIS Pro (esri.com)

and here is a reference to Arcade Functions that can be very helpful:

Function Reference | ArcGIS Arcade | ArcGIS Developers

0 Kudos
MariusVerdes
New Contributor III

Hi @clt_cabq,

Thank you for your answer. I tried to use your first expression "centroid($feature).x" but it return the coordinates in meters and I want them to be in decimal degrees.

Then I tried to use the How to guide provided, and when I tried it, it returns 0 as a value. Maybe I did something wrong..but the expression was validated. Here is my expression: X = "

//To populate X coordinate in decimal

function MetersToLon(x) {

var originShift = 2.0 * PI * 6378137.0 / 2.0;

var lon = (x / originShift) * 180.0;

return Round(lon, 3);
}

return MetersToLon(Round(Geometry($feature.X), 6));"

and Y "

//To populate Y coordinate in decimal:

function MetersToLat(y) {

var originShift = 2.0 * PI * 6378137.0 / 2.0;

var lat = (y / originShift) * 180.0;

lat = 180.0 / PI * (2.0 * Atan( Exp( lat * PI / 180.0)) - PI / 2.0);
return Round(lat,3);
}
return MetersToLat(Round(Geometry($feature.Y), 6));"

0 Kudos
MJBiazar
Esri Contributor

Hi @MariusVerdes you can modify your Arcade expression according to the second example in the article linked below (which is very similar to what you are using) to get the coordinates of the centroid of a polygon in degrees. 

https://support.esri.com/en-us/knowledge-base/000028324

Hopefully this sample expression resolves the issue for you. 

Best,

MJ

0 Kudos
MariusVerdes
New Contributor III

Thanks @MJBiazar. Meanwhile, I managed to resolve the situation with the help of our local Esri provider.

0 Kudos