Select to view content in your preferred language

# Arcade Expression for Distance Calculation

911
2
09-14-2023 01:55 AM
Labels (3)
by
Occasional Contributor
i have two layers one is High Tide Line and another one is Land Parcel. Land parcel is a polygon feature class and High tide line is a line feature class. I want to add the straight line distance at nearest vertex of the High Tide line layer to the centroid of the land parcel layer feature i am creating. I had written a Arcade Script and its not working for me. Can anyone help?. Thanks in Advance.

var landParcelLayer = FeatureSetByName(\$map, "Land Parcel"); // Replace with the actual name of your Land Parcel layer
var highTideLayer = FeatureSetByName(\$map, "High Tide Line HTL"); // Replace with the actual name of your High Tide Line layer
var attributeName = 'Dist_HTL'; // Replace with the desired attribute name

var editingFeature = \$feature;
var landParcelGeometry = Geometry(editingFeature);

var closestDistance = 999999999; // Set a large initial value

for (var vertex in highTideLayer) {
var vertexGeometry = Geometry(vertex);
var distance = Distance(vertexGeometry, Centroid(landParcelGeometry));
if (distance < closestDistance) {
closestDistance = distance;
}
}

if (closestDistance < 999999999) {
editingFeature[attributeName] = closestDistance;
return editingFeature;
} else {
return 'No Nearby High Tide Line Features';
}
Tags (2)
1 Solution

Accepted Solutions
by
Occasional Contributor

Hi Ken,

Thanks for connecting. I had revised script and solved the issue. Modified Script as follows :

var htl_lyr = FeatureSetByName(\$map, 'High Tide Line');
var editingFeature = \$feature;
var editingGeometry = Centroid(editingFeature);
var closestDistance = 999999999; // Set a large initial value

for (var htl_feature in htl_lyr) {
var dist = Distance(htl_feature, editingFeature, 'meter');
if (dist < closestDistance) {
closestDistance = dist;
}
}

var roundedDistance = Round(closestDistance, 2); // Round to 2 decimal places

roundedDistance; // This will return the rounded closest distance in meters
2 Replies
by
MVP Esteemed Contributor

Are you trying to calculate the value for the field 'Dist_HTL'? If so, is it a numeric field or text field?

by
Occasional Contributor

Hi Ken,

Thanks for connecting. I had revised script and solved the issue. Modified Script as follows :

var htl_lyr = FeatureSetByName(\$map, 'High Tide Line');
var editingFeature = \$feature;
var editingGeometry = Centroid(editingFeature);
var closestDistance = 999999999; // Set a large initial value

for (var htl_feature in htl_lyr) {
var dist = Distance(htl_feature, editingFeature, 'meter');
if (dist < closestDistance) {
closestDistance = dist;
}
}

var roundedDistance = Round(closestDistance, 2); // Round to 2 decimal places

roundedDistance; // This will return the rounded closest distance in meters