Select to view content in your preferred language

Pull Attributes from one layer to populate a field in the editable layer

653
1
06-29-2023 09:22 AM
Labels (1)
ZBigioni
Emerging Contributor

I have an editable layer for the survey which will ask for the civic address (among other info). Is there any way to reduce typing mistakes in Field Maps (also to make life easier and quicker) by either somehow making the addresses self populate/autocomplete in the Address field? Right now I can type literally anything I want (even a sentence that is not an address). I would like the collected points to also snap to the appropriate address points layer. I know you can click on the point and press "Collect Here" but it's pretty buried and it doesn't copy the attributes.

So essentially I want the end user to be able to press the plus button, then type/select an address, then it will drop a pin on the address point, auto-complete/fill the address field, and continue with the survey.

The civic addresses is available to add into the map I am using for the Field Maps App (what a mouthful) if there is a way to use it. I added it and tested it out, there is a "Collect Here" option but it's pretty buried IMO and doesn't copy attributes (There is a "copy attributes" button but it didn't seem to do anything lol)

So far I have managed to get this snippet to test FeatureSetByName and it works! I was happy as a beginner:

var address = FeatureSetByName($map, "AddressPoints")
var result = Intersects($feature, address)
return result
 
the result will return the entire table of addresses which means it can read the address points, but I need it to populate the correct address into the Address Field in the survey layer. In the "AddressPoints" layer the field I need it to read is called LOOKUP2, and I need it to write to $feature.fulladdr
 
However if there is another/easier way to do this, (I've thought of just adding all the civic addresses into domains so they can be a drop down but yikes) please let me know!
 

Please forgive me if this was a poor explanation, I am super new to Arcade and will be more than happy to try my best to answer any questions for clarification

Thank you in advance

1 Reply
ZBigioni
Emerging Contributor

I figured out a script that will pull the nearest (so the exact one if you click on the feature). I set the field to be calculated by this expression: 

// If feature doesn't have geometry, return null
if (IsEmpty(Geometry($feature))) { 
    return null; 
}

// Get the AddressPoints layer
var addressPoints = FeatureSetByName($map, 'AddressPoints');

// Buffer the current location and intersect with addressPoints
var bufferedLocation = Buffer($feature, 100, 'feet');
var candidateAddressPoints = Intersects(addressPoints, bufferedLocation);

// Calculate the distance between the addressPoint and the current location
// Store the feature and distance as a dictionary and push it into an array
var featuresWithDistances = [];
for (var f in candidateAddressPoints) {
    Push(featuresWithDistances, 
        {
            'distance': Distance($feature, f, 'feet'),
            'feature': f
        }
    );
}

// Sort the candidate addressPoints by distance using a custom function
function sortByDistance(a, b) {
    return a['distance'] - b['distance'];
}
var sorted = Sort(featuresWithDistances, sortByDistance);

// Get the closest feature
var closestFeatureWithDistance = First(sorted);

// If there was no feature, return null
if (IsEmpty(closestFeatureWithDistance)) { 
    return null; 
}

// Return the HOUSENUM attribute of the closest addressPoint
return closestFeatureWithDistance['feature']['HOUSENUMBE'];

 

0 Kudos