<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Arcade expression for getting shortest distance between line feature to point feature in Attribute Rules Questions</title>
    <link>https://community.esri.com/t5/attribute-rules-questions/arcade-expression-for-getting-shortest-distance/m-p/1155219#M350</link>
    <description>&lt;P&gt;This is excellent - thank you so much!&lt;/P&gt;</description>
    <pubDate>Fri, 18 Mar 2022 13:35:39 GMT</pubDate>
    <dc:creator>cwlee27</dc:creator>
    <dc:date>2022-03-18T13:35:39Z</dc:date>
    <item>
      <title>Arcade expression for getting shortest distance between line feature to point feature</title>
      <link>https://community.esri.com/t5/attribute-rules-questions/arcade-expression-for-getting-shortest-distance/m-p/1025171#M88</link>
      <description>&lt;P&gt;We have a requirement to get the nearest line feature with respect to point feature using arcade expression in ArcGIS pro attribute rule.&lt;/P&gt;&lt;P&gt;(or)&lt;/P&gt;&lt;P&gt;Can we get the perpendicular distance between the &lt;U&gt;&lt;STRONG&gt;Line Feature&lt;/STRONG&gt; &lt;STRONG&gt;and&amp;nbsp;&lt;/STRONG&gt;&amp;nbsp;&lt;STRONG&gt;Point Feature&lt;/STRONG&gt;&lt;/U&gt; in ArcGIS pro using attribute rule (arcade expression)&lt;/P&gt;&lt;P&gt;Any quick help on the above requirement would be much appreciated.&lt;/P&gt;&lt;P&gt;Thanks in advance...!&lt;/P&gt;&lt;P&gt;@Anonymous User&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 10 Feb 2021 06:04:16 GMT</pubDate>
      <guid>https://community.esri.com/t5/attribute-rules-questions/arcade-expression-for-getting-shortest-distance/m-p/1025171#M88</guid>
      <dc:creator>sayamshivakumar</dc:creator>
      <dc:date>2021-02-10T06:04:16Z</dc:date>
    </item>
    <item>
      <title>Re: Arcade expression for getting shortest distance between line feature to point feature</title>
      <link>https://community.esri.com/t5/attribute-rules-questions/arcade-expression-for-getting-shortest-distance/m-p/1025269#M89</link>
      <description>&lt;LI-CODE lang="javascript"&gt;// Calculation Attribute Rule for your point FC
// Field: NearestLineObjectID
// Triggers: Insert (, Update)


// Get your line FC
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 &amp;lt; min_dist) {
    min_dist = dist
    nearest_line_oid = line.ObjectID
  }
}

// Return the ObjectID of the nearest line feature
return nearest_line_oid&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/346994"&gt;@HusseinNasser2&lt;/a&gt;: This should probably be moved to the Attribute Rules group.&lt;/P&gt;</description>
      <pubDate>Wed, 10 Feb 2021 10:02:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/attribute-rules-questions/arcade-expression-for-getting-shortest-distance/m-p/1025269#M89</guid>
      <dc:creator>JohannesLindner</dc:creator>
      <dc:date>2021-02-10T10:02:31Z</dc:date>
    </item>
    <item>
      <title>Re: Arcade expression for getting shortest distance between line feature to point feature</title>
      <link>https://community.esri.com/t5/attribute-rules-questions/arcade-expression-for-getting-shortest-distance/m-p/1025286#M90</link>
      <description>&lt;P&gt;are you saying that the perpendicular distance to the line isn't the shortest distance?&lt;/P&gt;</description>
      <pubDate>Wed, 10 Feb 2021 12:13:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/attribute-rules-questions/arcade-expression-for-getting-shortest-distance/m-p/1025286#M90</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2021-02-10T12:13:04Z</dc:date>
    </item>
    <item>
      <title>Re: Arcade expression for getting shortest distance between line feature to point feature</title>
      <link>https://community.esri.com/t5/attribute-rules-questions/arcade-expression-for-getting-shortest-distance/m-p/1025295#M91</link>
      <description>&lt;P&gt;so moved&lt;/P&gt;</description>
      <pubDate>Wed, 10 Feb 2021 12:48:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/attribute-rules-questions/arcade-expression-for-getting-shortest-distance/m-p/1025295#M91</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2021-02-10T12:48:52Z</dc:date>
    </item>
    <item>
      <title>Re: Arcade expression for getting shortest distance between line feature to point feature</title>
      <link>https://community.esri.com/t5/attribute-rules-questions/arcade-expression-for-getting-shortest-distance/m-p/1025296#M92</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/294341"&gt;@JohannesLindner&lt;/a&gt;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;code working as expected&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":victory_hand:"&gt;✌️&lt;/span&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 10 Feb 2021 12:51:09 GMT</pubDate>
      <guid>https://community.esri.com/t5/attribute-rules-questions/arcade-expression-for-getting-shortest-distance/m-p/1025296#M92</guid>
      <dc:creator>sayamshivakumar</dc:creator>
      <dc:date>2021-02-10T12:51:09Z</dc:date>
    </item>
    <item>
      <title>Re: Arcade expression for getting shortest distance between line feature to point feature</title>
      <link>https://community.esri.com/t5/attribute-rules-questions/arcade-expression-for-getting-shortest-distance/m-p/1025297#M93</link>
      <description>&lt;P&gt;Actually not, i need to get the shortest distance, which is provided by&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/294341"&gt;@JohannesLindner&lt;/a&gt;&amp;nbsp;.&lt;/P&gt;</description>
      <pubDate>Wed, 10 Feb 2021 12:52:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/attribute-rules-questions/arcade-expression-for-getting-shortest-distance/m-p/1025297#M93</guid>
      <dc:creator>sayamshivakumar</dc:creator>
      <dc:date>2021-02-10T12:52:22Z</dc:date>
    </item>
    <item>
      <title>Re: Arcade expression for getting shortest distance between line feature to point feature</title>
      <link>https://community.esri.com/t5/attribute-rules-questions/arcade-expression-for-getting-shortest-distance/m-p/1154504#M347</link>
      <description>&lt;P&gt;Hello - I recently stumbled upon this post and I am looking at doing what you say 'can be done' which is to have an attribute for the line class that will update the point class with the closest feature.&amp;nbsp; Do you happen to have an example of that?&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 16 Mar 2022 20:42:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/attribute-rules-questions/arcade-expression-for-getting-shortest-distance/m-p/1154504#M347</guid>
      <dc:creator>cwlee27</dc:creator>
      <dc:date>2022-03-16T20:42:26Z</dc:date>
    </item>
    <item>
      <title>Re: Arcade expression for getting shortest distance between line feature to point feature</title>
      <link>https://community.esri.com/t5/attribute-rules-questions/arcade-expression-for-getting-shortest-distance/m-p/1155167#M349</link>
      <description>&lt;P&gt;Sure. It gets a little more complicated, that's why I didn't post it in the original answer.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Setup:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;Point feature class TestPoints&lt;/LI&gt;&lt;LI&gt;Line feature class TestLines&lt;/LI&gt;&lt;LI&gt;both have a field "IntegerField"&lt;/LI&gt;&lt;LI&gt;TestPoints.IntegerField has to be the same value as the closest feature of TestLines&lt;/LI&gt;&lt;LI&gt;This has to work when inserting points, updating points, inserting lines, updating lines, and deleting lines&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;// Calculation Attribute Rule on TestPoints
// Triggers: Insert, Update
// Field: IntegerField
// returns a field value of the closest TestLines feature


// This is the code from the original answer, slighlty modified to return the whole feature, not just a field value
// also, I made it into a function to keep the code below cleaner
function get_closest_feature(test_feature, compare_feature_set) {
  var min_distance = 9999999
  var closest_feature = null
  for(var f in compare_feature_set) {
    var d = Distance(test_feature, f)
    if(d &amp;lt; min_distance) {
      min_distance = d
      closest_feature = f
    }
  }
  return closest_feature
}


// only run if this is a new feature or the geometry got updated
if($editcontext.editType == "INSERT" || !Equals(Geometry($feature), Geometry($originalfeature))) {
  // load TestLines
  var lines = FeatureSetByName($datastore, "TestLines", ["IntegerField"], false)
  // find the closest TestLines feature
  var close_lines = Intersects(lines, Buffer($feature, 100, "Meters"))
  var closest_line = get_closest_feature($feature, close_lines)
  // return its field value
  if(closest_line != null) {
    return closest_line.IntegerField
  }
  // there is no TestLines feature within the specified buffer distance
  return null
}&lt;/LI-CODE&gt;&lt;LI-CODE lang="javascript"&gt;// Calculation Attribute Rule on TestLines
// Triggers: Insert, Update, Delete
// Field: empty
// Exclude from application evaluation
// Edits the IntegerField values of nearby TestPoints features

function get_closest_feature(test_feature, compare_feature_set) {
  var min_distance = 9999999
  var closest_feature = null
  for(var f in compare_feature_set) {
    var d = Distance(test_feature, f)
    if(d &amp;lt; min_distance) {
      min_distance = d
      closest_feature = f
    }
  }
  return closest_feature
}


// load features
var points = FeatureSetByName($datastore, "TestPoints", ["GlobalID"], false)   // we edit the point using their GlobalID as identifier
var lines = FeatureSetByName($datastore, "TestLines", ["IntegerField"], false)
// if we're deleting this line, we have to remove it from the lines feature set,
// because at this point, it's still there
if($editcontext.editType == "DELETE") {
  var gid = $feature.GlobalID
  lines = Filter(lines, "GlobalID &amp;lt;&amp;gt; @gid")
}

// create array with updates to TestPoints
var update_array = []
// create array to keep track of already visited points
// we have to recalcluate all points that were nearby the old geometry and all points that are nearby the new geometry
// some/many points will fall in both categories, but we don't need to check them twice.
// if there are many points, this will save some time.
var visited = []

// we need to check all points close to the old and the new geometries.
var geometries = [Geometry($feature), Geometry($originalfeature)]

for(var g in geometries) {
  // when we insert a new line, Geometry($originalfeature) is null and fails the code below.
  // in that case, just continue with the next geometry.
  if(geometries[g] == null) { continue }
  // find all points nearby the geometry, remove the points we already checked
  var points_in_buffer = Intersects(points, Buffer(geometries[g], 100, "Meters"))
  if(Count(visited) &amp;gt; 0) {
    points_in_buffer = Filter(points_in_buffer, "GlobalID NOT IN @visited")
  }
  // recalculate each point
  for(var p in points_in_buffer) {
    var new_value = null  // default is null (in case there is no nearby line feature)
    var close_lines = Intersects(lines, Buffer(p, 100, "Meters"))
    var closest_line = get_closest_feature(p, close_lines)
    if(closest_line != null) {
      new_value = closest_line.IntegerField
    }
    // we identify the point feature that we want to update by its GlobalID and specify its new attributes in a dictionary.
    Push(update_array, {"globalID": p.GlobalID, "attributes": {"IntegerField": new_value}})
    // hey, we already checked this point, no need to do it again for the next geometry!
    Push(visited, p.GlobalID)
  }
}
// instead of returning a singular field value, we can also return a dictionary.
// this dictionary can contain edits that we want to make to other feature classes.
// documentation for the dictionary keywords can be found here:
// https://pro.arcgis.com/en/pro-app/latest/help/data/geodatabases/overview/attribute-rule-dictionary-keywords.htm
return {
  "edit": [
    {
      "className": "TestPoints",  // we want to edit TestPoints
      "updates": update_array  // we want to apply all the new IntegerField values that we calculated above
    }
  ]
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Inserting TestPoints&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="JohannesLindner_1-1647595674741.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/36706iBF410433E3E1AFD8/image-size/medium?v=v2&amp;amp;px=400" role="button" title="JohannesLindner_1-1647595674741.png" alt="JohannesLindner_1-1647595674741.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Moving TestPoints&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="JohannesLindner_2-1647595697842.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/36707iF44FC08BF1E4E5F4/image-size/medium?v=v2&amp;amp;px=400" role="button" title="JohannesLindner_2-1647595697842.png" alt="JohannesLindner_2-1647595697842.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Inserting TestLines&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="JohannesLindner_3-1647595756738.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/36708i50C8C475D5219129/image-size/medium?v=v2&amp;amp;px=400" role="button" title="JohannesLindner_3-1647595756738.png" alt="JohannesLindner_3-1647595756738.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Moving TestLines&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="JohannesLindner_4-1647595794841.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/36709iE3D8F361722C9016/image-size/medium?v=v2&amp;amp;px=400" role="button" title="JohannesLindner_4-1647595794841.png" alt="JohannesLindner_4-1647595794841.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Deleting TestLines&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="JohannesLindner_5-1647595814511.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/36710iB440B0DA2872BE52/image-size/medium?v=v2&amp;amp;px=400" role="button" title="JohannesLindner_5-1647595814511.png" alt="JohannesLindner_5-1647595814511.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;As I said in the original answer, be mindful of the buffer distance you chose. The point in the north east can't find line 1, because my buffer distance is too small.&lt;/P&gt;</description>
      <pubDate>Fri, 18 Mar 2022 09:38:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/attribute-rules-questions/arcade-expression-for-getting-shortest-distance/m-p/1155167#M349</guid>
      <dc:creator>JohannesLindner</dc:creator>
      <dc:date>2022-03-18T09:38:04Z</dc:date>
    </item>
    <item>
      <title>Re: Arcade expression for getting shortest distance between line feature to point feature</title>
      <link>https://community.esri.com/t5/attribute-rules-questions/arcade-expression-for-getting-shortest-distance/m-p/1155219#M350</link>
      <description>&lt;P&gt;This is excellent - thank you so much!&lt;/P&gt;</description>
      <pubDate>Fri, 18 Mar 2022 13:35:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/attribute-rules-questions/arcade-expression-for-getting-shortest-distance/m-p/1155219#M350</guid>
      <dc:creator>cwlee27</dc:creator>
      <dc:date>2022-03-18T13:35:39Z</dc:date>
    </item>
  </channel>
</rss>

