I'm working on converting Attribute Assistant Methods to Attribute Rules. The intent of the below AR is to populate several fields in a newly created Water Main (line feature) from attributes of an existing Water Main feature that intersects either the start point or the endpoint of the new feature.
So the Arcade expression is first looking to see if an existing Water Main intersects the startpoint of the new Water Main, and if so copy over the attributes. This part of the expression works just fine for all 4 attributes. No matter what I've tried, I can't seem to get the next part of the expression to work.
If I create a new feature where the startpoint doesn't intersect an existing feature but the endpoint does intersect an existing feature, the attributes of the new feature remain null. Similarly, if I create a new feature where neither the startpoint or endpoint intersect an existing feature, the attributes of the new feature remain null.
var linegeometry = Geometry($feature)
var wMain = FeatureSetByName($datastore,"wMain",["WATERLEVEL","PRESSUREZONE","PZONETYPE","SERVICEAREA"],true)
var startpoint = linegeometry.paths[0][0]
var endpoint = linegeometry.paths[-1][-1]
var startpoint_intMain = First(Intersects(wMain,startpoint))
var endpoint_intMain = First(Intersects(wMain,endpoint))
var waterlevel
var pressurezone
var pzonetype
var servicearea
if(!IsEmpty(startpoint_intMain)) {
waterlevel = startpoint_intMain.WATERLEVEL
pressurezone = startpoint_intMain.PRESSUREZONE
pzonetype = startpoint_intMain.PZONETYPE
servicearea = startpoint_intMain.SERVICEAREA
}
else if(!IsEmpty(endpoint_intMain)) {
waterlevel = endpoint_intMain.WATERLEVEL
pressurezone = endpoint_intMain.PRESSUREZONE
pzonetype = endpoint_intMain.PZONETYPE
servicearea = endpoint_intMain.SERVICEAREA
}
else {
waterlevel = 'NA'
pressurezone = 'NA'
pzonetype = 'NA'
servicearea = 'NA'
}
return {
"result": {
"attributes": {
"WATERLEVEL": waterlevel,
"PRESSUREZONE": pressurezone,
"PZONETYPE": pzonetype,
"SERVICEAREA": servicearea
}
}
}
Here is another attempt which provided the same result:
var linegeometry = Geometry($feature)
var wMain = FeatureSetByName($datastore,"wMain",["WATERLEVEL","PRESSUREZONE","PZONETYPE","SERVICEAREA"],true)
var startpoint = linegeometry.paths[0][0]
var endpoint = linegeometry.paths[-1][-1]
var startpoint_intMain = First(Intersects(wMain,startpoint))
var endpoint_intMain = First(Intersects(wMain,endpoint))
var waterlevel
var pressurezone
var pzonetype
var servicearea
if(!IsEmpty(startpoint_intMain)) {
waterlevel = startpoint_intMain.WATERLEVEL
pressurezone = startpoint_intMain.PRESSUREZONE
pzonetype = startpoint_intMain.PZONETYPE
servicearea = startpoint_intMain.SERVICEAREA
return {
"result": {
"attributes": {
"WATERLEVEL": waterlevel,
"PRESSUREZONE": pressurezone,
"PZONETYPE": pzonetype,
"SERVICEAREA": servicearea
}
}
}
}
else if(!IsEmpty(endpoint_intMain)) {
waterlevel = endpoint_intMain.WATERLEVEL
pressurezone = endpoint_intMain.PRESSUREZONE
pzonetype = endpoint_intMain.PZONETYPE
servicearea = endpoint_intMain.SERVICEAREA
return {
"result": {
"attributes": {
"WATERLEVEL": waterlevel,
"PRESSUREZONE": pressurezone,
"PZONETYPE": pzonetype,
"SERVICEAREA": servicearea
}
}
}
}
else {
waterlevel = 'NA'
pressurezone = 'NA'
pzonetype = 'NA'
servicearea = 'NA'
return {
"result": {
"attributes": {
"WATERLEVEL": waterlevel,
"PRESSUREZONE": pressurezone,
"PZONETYPE": pzonetype,
"SERVICEAREA": servicearea
}
}
}
}
Any idea why the else if / else statements might not be evaluating?
Thanks in advance for any thoughts!
Not where I can test, but think it might have to do with the if/elseif/else statements.
This would exclude any other matching items (if the first IF is true, then doesn't execute the elseif, and so on).
I am doing something similar here (and further in the post is more info), but looking for intersections with multiple point featureclasses.
But, I do an "IF" comparison for each condition, and if there is a value, add it to the dictionary. This way each if statement gets executed, regardless of the results of the others. Then, once the dict is populated, return it.
This code was originally taken from here.
Suspect you want something similar to what I have inside the 'for' loop (as you are only intersecting against a single FC) so shouldn't have to iterate.
In any case, I think if you set the 'NA' values first, then if they intersect, they get updated with the appropriate value, otherwise will return NA.
Hope this helps get you on track,
R_
Thanks for the reply! I had tried the IF statements originally. It seems to only be evaluating the first IF statement and doing nothing if it doesn't find an intersect with the start point. Even if I simplify the code and remove the second IF statement, it won't return "NA" values for the attributes if there isn't a startpoint intersect. The attributes just remain null.
I can get this to work simply with 4 separate Attribute Rules and using IIF statements for each attribute individually, was just hoping to keep things a bit tidier and combine into a single rule. It seems I must still be missing something.
var linegeometry = Geometry($feature)
var wMain = FeatureSetByName($datastore,"wMain",["WATERLEVEL","PRESSUREZONE","PZONETYPE","SERVICEAREA"],true)
var startpoint = linegeometry.paths[0][0]
var endpoint = linegeometry.paths[-1][-1]
var startpoint_intMain = First(Intersects(startpoint,wMain))
var endpoint_intMain = First(Intersects(endpoint,wMain))
var start_end_list = [startpoint_intMain, endpoint_intMain]
var waterlevel = 'NA'
var pressurezone = 'NA'
var pzonetype = 'NA'
var servicearea = 'NA'
if(!IsEmpty(startpoint_intMain)) {
waterlevel = startpoint_intMain.WATERLEVEL
pressurezone = startpoint_intMain.PRESSUREZONE
pzonetype = startpoint_intMain.PZONETYPE
servicearea = startpoint_intMain.SERVICEAREA
}
return {
"result": {
"attributes": {
"WATERLEVEL": waterlevel,
"PRESSUREZONE": pressurezone,
"PZONETYPE": pzonetype,
"SERVICEAREA": servicearea
}
}
}