Select to view content in your preferred language

Arcade Expression: Calculate Road Class from nearest Line Feature

457
2
Jump to solution
12-28-2022 12:28 PM
by Anonymous User
Not applicable

I have created an arcade expression to automatically populate the DueDate field for a point feature. When a point is assigned in workforce, I would like to run a calculate expression that searches for the nearest road and then populates the due date based on minimum maintenance standards (i.e. Class 1 must be fixed in 5 days, Class  2 in 10 days etc.):

 

 

var AssignedDate = $feature.assigneddate;
var line_fs = FeatureSetByName($datastore, 'Road');
line_fs = Intersects(line_fs, Buffer($feature, 15, "Meters"));

// Cycle through road segments & find the nearest one
var min_dist = 99999
var nearest_RoadClass = null
var geo = Geometry($feature)
for(var line in line_fs) {
  var line_geo = Geometry(line)
  var dist = Distance(geo, line_geo)
  if(dist < min_dist) {
    min_dist = dist
    nearest_RoadClass = line.Class
  }

if (IsEmpty(nearest_RoadClass)){
return null;
}
if (nearest_RoadClass == "Class 1"){
return DateAdd(AssignedDate, 5, 'days');
}
if (nearest_RoadClass == "Class 2"){
return DateAdd(AssignedDate, 10, 'days');
}
else return null;}

 

 

 

Can someone tell me why points nearest to a Class 6 road are sometimes assigned a due date rather than a null value?

SHartholt_0-1672259151202.png

 

0 Kudos
1 Solution

Accepted Solutions
RhettZufelt
MVP Notable Contributor

Does the Roads feature class have a column named "Class"?

Not sure if it has anything to do with it, but there is a circular reference in the Intersect (line_fs).

line_fs = Intersects(line_fs, Buffer($feature, 15, "Meters"))

Not sure if you can assign a value that is dependant on itself.  Sometimes this works, just not sure in Arcade.

You are also missing the opening curly bracket on the else statement:

else {
   return null
   }

If you add a console statement around line 16, what does it report in the messages?

Console(nearest_RoadClass) 

The console should be  reporting "Class 1", "Class 2", etc. 

R_

View solution in original post

0 Kudos
2 Replies
RhettZufelt
MVP Notable Contributor

Does the Roads feature class have a column named "Class"?

Not sure if it has anything to do with it, but there is a circular reference in the Intersect (line_fs).

line_fs = Intersects(line_fs, Buffer($feature, 15, "Meters"))

Not sure if you can assign a value that is dependant on itself.  Sometimes this works, just not sure in Arcade.

You are also missing the opening curly bracket on the else statement:

else {
   return null
   }

If you add a console statement around line 16, what does it report in the messages?

Console(nearest_RoadClass) 

The console should be  reporting "Class 1", "Class 2", etc. 

R_

0 Kudos
by Anonymous User
Not applicable

Thank you! I implemented your suggestions and realized that I forgot a curly bracket before my series of if statements which might have been the main culprit! 

0 Kudos