Select to view content in your preferred language

Arcade Nearest Feature Function

78
0
Thursday
Status: Open
Labels (1)
GeoGalvanic
Occasional Contributor

I think it would be beneficial to include an Arcade function that finds the nearest feature (or potentially features) from a featureSet to a specific feature/geometry.

 

This is a routine task that has been asked about several times before across the various forums:
https://community.esri.com/t5/arcgis-online-questions/using-arcade-to-find-the-nearest-address-point...

https://community.esri.com/t5/arcgis-online-questions/arcade-get-4-nearest-addresses-from-point-feat...

https://community.esri.com/t5/arcgis-field-maps-questions/nearest-address-calculation-expression-in-...

 

I have written my own code to get my desired result here:

var portalItem = FeatureSetByPortalItem(Portal('https://redacted.com/portal'), 'redactedportalitemid', 1, ["featureDescription"], true)
var inputFeature = $feature
var nearestFeature
var relevantFeaturesBuffer = Buffer($feature, 500, "meters")
var relevantFeatures = Intersects(portalItem, relevantFeaturesBuffer)

function compareShortestDistance(a,b){
  return Distance(Geometry(a), Geometry(inputFeature)) - Distance(Geometry(b), Geometry($feature))
}

for (var feature in relevantFeatures){
  if (nearestFeature == null) {
    nearestFeature = feature
  } else if (compareShortestDistance(nearestFeature, feature) > 0){
    nearestFeature = feature
  }
}

return nearestFeature.featureDescription

 

This does work and meet my needs, however I feel that it is still inefficient. You may notice that there is a buffer and intersection used to filter "relevant" features that are nearby (hardcoded 500m). This is necessary because if the portalItem contains a large number of features then the distance comparison becomes prohibitively slow. If instead there is a nearest function created by ESRI it should be possible to better optimize the function by taking advantage of backend tools like spatial indexes.

 

What I'm proposing is a function like such:
Nearest(featureSet, feature/geometry, numberOfReturnedFeatures?) => feature/featureSet

 

Tags (1)