Select to view content in your preferred language

How To Use Attribute Rules to Calculate Geometry of Polylines

1342
3
10-31-2022 02:25 PM
Labels (3)
BenWagner3
Emerging Contributor

Hello,

I am creating Attribute Rules for some feature classes. I have been able to successfully create rules to calculate the X & Y of my point feature classes, but I have some polyline feature classes that need some geometry calculations performed. I need to be have an attribute rule(s) that would calculate the X & Y of both the start of the line and the end of the line. This rule would run on Insert and Update. I have four attribute fields (Upstream X, Upstream Y, Downstream X, & Downstream Y). 

Thanks in advance for any assistance.

-Ben

-Ben
0 Kudos
3 Replies
JohannesLindner
MVP Frequent Contributor
// Calculation attribute rule on the line fc
// triggers: Insert, Update
// Field: empty

// get the $feature's geometry
var geom = Geometry($feature)
// null geometry -> abort
if(geom == null) { return }
// get start and end point
var p_start = geom.paths[0][0]
var p_end = geom.paths[-1][-1]
// return the x and y values of those points
return {
    "result": {"attributes": {
        "UpstreamX": p_start.x,
        "UpstreamY": p_start.y,
        "DownstreamX": p_end.x,
        "DownstreamY": p_end.y
    }}
}

Have a great day!
Johannes
0 Kudos
BenWagner3
Emerging Contributor

Johannes,

When trying your expression, the expression validates fine, but when saving the attribute rule I get an Error 002941 "An expected Field was not found or could not be retrieved properly." The attribute field names match exact with my feature class attribute fields.

I've attached a screenshot.

Thanks

-Ben
0 Kudos
JohannesLindner
MVP Frequent Contributor

First thing that comes to mind: You have to use the actual field names, not the aliases. If you have spaces in lines 15-18, you're using aliases.

If that doesn't solve it, please post your code and a screenshot of the field view of your feature class.


Have a great day!
Johannes
0 Kudos