Arcade Expressions for Attribute Rule to find angle of a polyline

2178
5
Jump to solution
04-14-2021 02:39 PM
Labels (1)
GISUSER6
New Contributor III

Hello,

 

I am trying to make an Attribute Rule in ArcGIS Pro to auto populate a Rotation field for a sanitary flow direction point intersecting with a sanitary main feature class. This way with using the rotation field I can set the symbology rotation to reflect which was the sanitary main is flowing. With the arcade expression below, I keep coming up with the error Arcade Error: Unexpected Null. Any idea on what I am doing wrong? The error is attached and the code is below. 

 

// Find the first intersecting line from the intersecting class
var lineClass = FeatureSetByName($datastore, "ssGravityMain", ["objectid"], true)
var line = First(Intersects(lineClass, $feature))

// If no feature was found, return the original value
if (line == null)
{
return $feature.ROTATION
}
// Buffer the point by a small amount to extract the segment
var search = Extent(Buffer($feature, .01, "feet"))
var segment = Clip(line, search)["paths"][0]

// Get angle of line using the start and end vertex
if(isempty($feature.ROTATION)) return Angle(segment[0], segment[-1])

0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Frequent Contributor

Looks like it should work...

The error is thrown at line 12, which is one of those (can't tell from your code):

var search = Extent(Buffer($feature, .01, "feet"))
var segment = Clip(line, search)["paths"][0]

Try these (separate or in combination):

var search = Extent(Buffer($feature, 0.1, "feet"))  // bigger buffer
var clip_geometry = Clip(Geometry(line), search)  // use Geometry(line)
// test if the clip was successfull and return null (or a specific failure value) if not
// this won't solve the underlying problem, but will stop the error
if(clip_geometry == null) {
  return null  // return -9999
}
var segment = clip_geometry.paths[0]  // .paths instead of ["paths"]

 Also:

// Instead of doing the IsEmpty check before returning, do it at the start of the script, so you can return early.
if(!IsEmpty($feature.ROTATION)) {
  return $feature.ROTATION
}
var lineClass = ...
// ...
return Angle(segment[0], segment[-1])

 


Have a great day!
Johannes

View solution in original post

5 Replies
JohannesLindner
MVP Frequent Contributor

Looks like it should work...

The error is thrown at line 12, which is one of those (can't tell from your code):

var search = Extent(Buffer($feature, .01, "feet"))
var segment = Clip(line, search)["paths"][0]

Try these (separate or in combination):

var search = Extent(Buffer($feature, 0.1, "feet"))  // bigger buffer
var clip_geometry = Clip(Geometry(line), search)  // use Geometry(line)
// test if the clip was successfull and return null (or a specific failure value) if not
// this won't solve the underlying problem, but will stop the error
if(clip_geometry == null) {
  return null  // return -9999
}
var segment = clip_geometry.paths[0]  // .paths instead of ["paths"]

 Also:

// Instead of doing the IsEmpty check before returning, do it at the start of the script, so you can return early.
if(!IsEmpty($feature.ROTATION)) {
  return $feature.ROTATION
}
var lineClass = ...
// ...
return Angle(segment[0], segment[-1])

 


Have a great day!
Johannes
GISUSER6
New Contributor III

This worked perfectly! Either way worked for me. Thanks for all of your help. 

 

Anna

0 Kudos
GiovanniCalzadilla
New Contributor

I get this error when calculation is done "Edits could not be saved: Cannot convert a value of type 'java.lang.Double' to 'SMALLINT'." is there anyway to add something to the line to truncate what is being pushed to my rotation field "227.47006300882038" Thanks in advance. 

"return Angle(segment[0], segment[-1])"  ? 

Tags (1)
0 Kudos
JohannesLindner
MVP Frequent Contributor

Haven't seen this error before, but it sounds like your field is an integer field?

Try rounding the output:

return Round(Angle(segment[0], segment[-1]))

 


Have a great day!
Johannes
GiovanniCalzadilla
New Contributor

Thank you, I used it as part of a script to auto rotate asset based on a Line direction and it worked perfectly. 👍

0 Kudos