# Arcade expression for getting shortest distance between line feature to point feature

515
5
02-09-2021 10:04 PM
New Contributor II

We have a requirement to get the nearest line feature with respect to point feature using arcade expression in ArcGIS pro attribute rule.

(or)

Can we get the perpendicular distance between the Line Feature and  Point Feature in ArcGIS pro using attribute rule (arcade expression)

Any quick help on the above requirement would be much appreciated.

Tags (2)
1 Solution

Accepted Solutions
Occasional Contributor II
``````// Calculation Attribute Rule for your point FC
// Field: NearestLineObjectID
// Triggers: Insert (, Update)

var line_fs = FeatureSetByName(\$datastore, "FullNameOfLineFeatureclass", ["ObjectID"], true)

// If you have many lines, just select those that are within a certain distance of your point
// I tested it without this line and around 10.000 line features and it took a good time to compute. With this line it was done almost instantly.
// Choose a distance in which a line feature will certainly be!
line_fs = Intersects(line_fs, Buffer(\$feature, 100, "Meters"))

// Cycle through the line features and find the nearest one
var min_dist = 999999  // arbitrary large number
var nearest_line_oid = null
var geo = Geometry(\$feature)
for(var line in line_fs) {
var line_geo = Geometry(line)
var dist = Distance(geo, line_geo)
if(dist < min_dist) {
min_dist = dist
nearest_line_oid = line.ObjectID
}
}

// Return the ObjectID of the nearest line feature
return nearest_line_oid``````

This will only work when you insert or update point features. If you insert or edit a line feature that would place it nearer to a point than another line, you have to update the point!

Or, you could add an Attribute Rule to the line featureclass that updates the point featureclass when you edit a line. Let me know if you need help with that.

@HusseinNasser2: This should probably be moved to the Attribute Rules group.

Have a great day!
Johannes
5 Replies
Occasional Contributor II
``````// Calculation Attribute Rule for your point FC
// Field: NearestLineObjectID
// Triggers: Insert (, Update)

var line_fs = FeatureSetByName(\$datastore, "FullNameOfLineFeatureclass", ["ObjectID"], true)

// If you have many lines, just select those that are within a certain distance of your point
// I tested it without this line and around 10.000 line features and it took a good time to compute. With this line it was done almost instantly.
// Choose a distance in which a line feature will certainly be!
line_fs = Intersects(line_fs, Buffer(\$feature, 100, "Meters"))

// Cycle through the line features and find the nearest one
var min_dist = 999999  // arbitrary large number
var nearest_line_oid = null
var geo = Geometry(\$feature)
for(var line in line_fs) {
var line_geo = Geometry(line)
var dist = Distance(geo, line_geo)
if(dist < min_dist) {
min_dist = dist
nearest_line_oid = line.ObjectID
}
}

// Return the ObjectID of the nearest line feature
return nearest_line_oid``````

This will only work when you insert or update point features. If you insert or edit a line feature that would place it nearer to a point than another line, you have to update the point!

Or, you could add an Attribute Rule to the line featureclass that updates the point featureclass when you edit a line. Let me know if you need help with that.

@HusseinNasser2: This should probably be moved to the Attribute Rules group.

Have a great day!
Johannes
New Contributor II

Thanks @JohannesLindner
code working as expected ✌️

MVP Notable Contributor

are you saying that the perpendicular distance to the line isn't the shortest distance?

... sort of retired...
New Contributor II

Actually not, i need to get the shortest distance, which is provided by @JohannesLindner .

MVP Notable Contributor

so moved

... sort of retired...