Select to view content in your preferred language

Arcade Nearest Vertex / Nearest Coordinate Functions

693
3
08-07-2023 09:58 AM
Labels (1)
TimF
by
New Contributor III

Hi all,

I want to test the functions mentioned above (Nearest Vertex / Nearest Coordinate) however I'm not having much luck with what should be a fairly basic bit of Arcade ...

The aim was to click on a point on the map and it would run the following pop-up expression to test against the first candidate from a nearby polygonal dataset:

var featurePt = $feature
var featurePly = FeatureSetByPortalItem(
  Portal('https://www.arcgis.com'),
  '...ItemIDHere...', // A valid Item ID would be here...
  0,
  ['*'],
  true
);

var nearestPly = First(Intersects(featurePly, Buffer(featurePt, 10, "kilometers")));

var result = NearestVertex(nearestPly,featurePt)

return result

 

This however returns the following error:

Test execution error: Execution error - Invalid parameter. Verify test data.

 

When I debug it line-by-line, everything works until the NearestVertex line. From what I can see in the guidance I have all the parameters. Hit the same issue with NearestCoordinate function.

Can anyone advise what I'm doing wrong/what's missing?

Thanks!

 

 

 

 

0 Kudos
3 Replies
JohannesLindner
MVP Frequent Contributor

Hmm, I couldn't reproduce the error.

But you need a null check after line 10:

if(nearestPly == null) { return "No polygon found" }

 

And right now you're returning a dictionary with the keys "coordinate" and "distance". You probably want to return one of those values instead.


Have a great day!
Johannes
TimF
by
New Contributor III

Hi Johannes,

Thanks for the response. Good shout on the null check and specific dictionary response.

I did get these functions to work on different test data i.e., a new layer with a single point, comparing to another new layer with a single polygon. I'm not sure what was wrong with my hosted layers... but this at least allows me check the accuracy of the outputs.

Do you happen to know if the distance returned is source->target->source (i.e., double-counting)?

I found the Nearest Coordinate distance, when divided by two, returned the closest distance to the true distance calculated in Pro (and shown with the Measure tool). However I wasn't sure if that was a rule-of-thumb I could rely on....

Thanks!

TimF_0-1691581059484.pngTimF_1-1691581072258.png

 

 

0 Kudos
JohannesLindner
MVP Frequent Contributor

The coordinates suggest that you are using a geographic coordinate system. For distance measurements, you should use a projected coordinate system.


Have a great day!
Johannes
0 Kudos