Select to view content in your preferred language

Arcade script not working (Portal)

450
5
Jump to solution
08-21-2024 01:28 PM
leahmaps
Frequent Contributor

Hi All,

 

I have an arcade script that I often use to return the closest value to a given point. In this case, an address number. When I try it in this particular instance, I receive "Test execution error: Execution error - Cannot access value using a key of this type. Verify test data."

This is my script. The funny thing is that I copied it from another calculation (in field maps) just to return an ID from a water main. I am not sure why this one isn't working.

var address = FeatureSetByName($map, "Addresses")
var searchDistance = 1
var closestPlace = Intersects(address, Buffer($feature, searchDistance, 'miles'))
var minDistance = Infinity

for (var place in closestPlace){
  var placeDistance = Distance(place, $feature, 'mile');
  if (placeDistance < minDistance){
    minDistance = placeDistance;
    closestPlace = place;
  }
};

return closestPlace.bldg_num
 
thank you!
Tags (1)
0 Kudos
1 Solution

Accepted Solutions
jcarlson
MVP Esteemed Contributor

Where are you running this? Depending on the context, $map may not be available.

The function Intersects returns a FeatureSet. Your expression replaces closestPlace with place if there's a feature found within the buffer, but if there isn't then closestPlace will still be a FeatureSet.

Personally, I would avoid re-using a variable for different types of objects. You might try something like this. By creating a placeholder place with a null value, and by keeping the FeatureSet and the Feature in their own variables, we don't get issues when we try to call the value at the end.

And yes, a dictionary and Feature are different types of objects, I know, but in this case, you can call their attributes in the same way.

var address = FeatureSetByName($map, "Addresses")
var searchDistance = 1
var closePlaces = Intersects(address, Buffer($feature, searchDistance, 'miles'))
var minDistance = Infinity

// create placeholder closest place with a building number
var closestPlace = {'bldg_num':null}

for (var place in closePlaces){
  var placeDistance = Distance(place, $feature, 'mile');
  if (placeDistance < minDistance){
    minDistance = placeDistance;
    closestPlace = place;
  }
};

return closestPlace['bldg_num']

 

- Josh Carlson
Kendall County GIS

View solution in original post

5 Replies
GregKeith
Frequent Contributor

Try running one line at a time, or use Console to check output, to see where the error occurs.

 

0 Kudos
jcarlson
MVP Esteemed Contributor

Where are you running this? Depending on the context, $map may not be available.

The function Intersects returns a FeatureSet. Your expression replaces closestPlace with place if there's a feature found within the buffer, but if there isn't then closestPlace will still be a FeatureSet.

Personally, I would avoid re-using a variable for different types of objects. You might try something like this. By creating a placeholder place with a null value, and by keeping the FeatureSet and the Feature in their own variables, we don't get issues when we try to call the value at the end.

And yes, a dictionary and Feature are different types of objects, I know, but in this case, you can call their attributes in the same way.

var address = FeatureSetByName($map, "Addresses")
var searchDistance = 1
var closePlaces = Intersects(address, Buffer($feature, searchDistance, 'miles'))
var minDistance = Infinity

// create placeholder closest place with a building number
var closestPlace = {'bldg_num':null}

for (var place in closePlaces){
  var placeDistance = Distance(place, $feature, 'mile');
  if (placeDistance < minDistance){
    minDistance = placeDistance;
    closestPlace = place;
  }
};

return closestPlace['bldg_num']

 

- Josh Carlson
Kendall County GIS
leahmaps
Frequent Contributor

Hi Josh,

This is running in the calculate section where you set up arcade for field maps. 

The only reason I am stumped as to why this one doesn't work, is because I am using the exact same expression (just different layers) in another field maps field and it works just fine.

leahmaps_0-1724329732289.png

I will give what you have a shot and see if that works instead.

0 Kudos
JohnFannon
Frequent Contributor

Have you checked the following?

  • That the "bldg_num" field exists in the feature layer you are querying. If not, then you would get a key error as that name would not exist in the attributes dictionary. To check for this you can do a check using HasKey before accessing that attribute.
  • That closestPlace is not null when you are trying to access it's attribute, which would be the case if the nearest feature was more than the search distance away.

You can also use Console statements to check variable values, which should appear in the browser developer console.

0 Kudos
leahmaps
Frequent Contributor

Yep, I checked both of these. bldg_num is in the layer (directly added from the variables tab). and I don't think closest place should ever be null, but it may be possible. There are also no null features in the feature layer.

0 Kudos