Select to view content in your preferred language

Field Maps Field Calculation error

1537
6
03-31-2022 02:57 PM
TaylorGreenup
Emerging Contributor

I have a data collection layer with a field for Trail ID that I would like to have auto-populate from the Trail ID field of the trails layer that I have in the same field map. These are separate layers within the same web map. I found this post and modified the calculated expression for populating this Trail ID field to the Arcade expression below (with a change from $datastore to $map to select the trails layer). This passed the error test in AGOL but when I try to do a test data collection in the Field Maps app, I get a calculation error. Does anyone have any ideas how I can get this to work? 

 

var trailLayer = FeatureSetByName($map,"R1 EDW Trail NFS Publish", ["TRAIL_NO"]);
var searchDistance = 50;
var trailIntersect = Intersects(trailLayer, BufferGeodetic($feature, searchDistance, "feet"));
var cnt = Count(trailIntersect);

var minDistance = Infinity;
var name = Null
if (cnt > 0) {
    for (trail in trailIntersect) {
        var dist = DistanceGeodetic(trail, $feature, "feet");
        if (dist < minDistance) {
            name = trail.TRAIL_NO;
            minDistance = dist
        }
    }
} else {
    // pass no features found within search distance, id remains null
}

return name;

 

 

0 Kudos
6 Replies
ajevans429
Occasional Contributor

@TaylorGreenup , I'm definitely not the best person to answer this question for ya, but I did what seemed like an error in your code... To initially call "trail" out as a variable, don't you need to add "var" prior to the variable name "for (var trail in trailintersect)", instead of just "for (trail in trailintersect)? 

Sorry if I'm off track, but hopefully this generates some other replies for ya.

0 Kudos
TaylorGreenup
Emerging Contributor

Thanks for the suggestions! My coworker David Hood, who knows Arcade well, came up with the following that is working to calcualte Trail ID:

 

var trailLayer = FeatureSetByName($map,"R1 EDW Trail NFS Publish", ["TRAIL_NO"]);
var bufferDistance = 100;
var trailIntersect = Intersects(Buffer($feature, bufferDistance, "feet"), trailLayer)

var cnt = Count(trailIntersect);
var minDistance = Infinity;
var name = Null
if (cnt > 0) {
    for (var trail in trailIntersect) {
        var dist = Distance(trail, $feature, 'feet');
        if (dist < minDistance) {
            name = trail.TRAIL_NO;
        }
    }
} else {
    // pass no features found within search distance, id remains null
}

return name;

 

0 Kudos
kmsmikrud
Frequent Contributor

Hi,

Thanks so much for sharing code and what you are using Field Maps for. I'm interested in using calculated expressions within Field Maps. Are you working offline and having good luck with the expressions? This still isn't very clear to me if the arcade calculated expressions work offline well or what the limitations are. Thanks! 

Kathy

0 Kudos
TaylorGreenup
Emerging Contributor

Hi Kathy! The thanks is really all to my co-worker David Hood who's been helping me with Arcade expressions. I've set these expressions for use in Field Maps on a point and line layer collecting information on trail features. I am having the folks in charge of the data collection effort test this before field season hits in earnest.

Yes it does calculate in Field Maps and apparently these expressions do not work in Map Viewer (tells you that when you set them up). Other limitations that we are aware of are the following: 1) it looks for the trail ID within 100ft of the collection site, 2) if you are near a junction it's going to populate with the trail ID that's higher up in the record list, 3) the record isn't editable so it can't be manually overwritten in the field (that's a function of using an expression to calculate it). So we may want to tweak the search radius and also have data collectors comment if they are near a trail junction so those will get flagged for QAQC. Or we might pull the expression out. I'm hoping the data organizers can do enough testing to be confident that having this calculation is preferrrable to manual entry. Thanks!

0 Kudos
kmsmikrud
Frequent Contributor

Thank-you @TaylorGreenup !  The information you provided is SUPER useful. I didn't realize the expression couldn't be overwritten in the field. I wanted one expression to use the last rating from previous observation and then only change if the user wanted to change the field. So I guess that functionality is out for now. Good luck and thank-you for taking the time to share. 

0 Kudos
BenSmithEsriUK
Esri Contributor

Hi Taylor,

I may be missing something, but what's the purpose of line 13?

Have you tried adding name = "" into the else loop?

Thanks,

Ben

0 Kudos