<?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 Finding closest point, returning closest point's ID in newly created feature's attribute table. in Attribute Rules Questions</title>
    <link>https://community.esri.com/t5/attribute-rules-questions/finding-closest-point-returning-closest-point-s-id/m-p/1133441#M284</link>
    <description>&lt;P&gt;I'm trying to develop an immediate calculation attribute rule that finds the closest asset (point) and returns the unique ID to the newly created feature's attribute table (also a point).&amp;nbsp;For starters, I have been using a solved question thread originally posted by&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/467562"&gt;@James_Norquest&lt;/a&gt;,&amp;nbsp;which can be found&amp;nbsp;&lt;A href="https://community.esri.com/t5/attribute-rules-questions/attribute-rule-for-location-description/td-p/1040569" target="_self"&gt;here.&lt;/A&gt;&lt;/P&gt;&lt;P&gt;Here's the code that I've been using:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;//importing two fields, including geometry of the feature to be analyzed
var closestAsset = FeatureSetByName($datastore, "ClosestAssetFC",["label","type"], true);

//In OP, this is the "meat &amp;amp; potatoes"
var feature_buffer = Buffer($feature, 500, 'feet');
var nearest = First(Intersects(closestAsset, feature_buffer));

return nearest["label"];&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;When I create a new feature, the field to be populated by the rule returns the same asset ID from the ClosestAssetFC, no matter where I put it. Through some troubleshooting, I found that it was because the entire ClosestAssetFC was being returned by the script, and was only returning the first record, which was the same every time.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'd also like to subset the ClosestAssetFC to only include a specific asset type. Here's the code that I tried:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;var closestAsset = FeatureSetByName($datastore, "ClosestAssetFC", ["label","type"], true);

var feature_buffer = Buffer($feature, 500, 'feet');
var nearest = Intersects(closestAsset, feature_buffer);
var nearestFilter = Filter(nearest, 'type = "Asset Type 1"');

return First(nearestFilter["label"]);&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;This gives me an error, saying that a dictionary type was expected at line 8.&lt;/P&gt;&lt;P&gt;Any advice here would be much appreciated!&amp;nbsp;&lt;/P&gt;&lt;P&gt;Best,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Mike&lt;/P&gt;</description>
    <pubDate>Thu, 13 Jan 2022 16:18:54 GMT</pubDate>
    <dc:creator>wcozzens</dc:creator>
    <dc:date>2022-01-13T16:18:54Z</dc:date>
    <item>
      <title>Finding closest point, returning closest point's ID in newly created feature's attribute table.</title>
      <link>https://community.esri.com/t5/attribute-rules-questions/finding-closest-point-returning-closest-point-s-id/m-p/1133441#M284</link>
      <description>&lt;P&gt;I'm trying to develop an immediate calculation attribute rule that finds the closest asset (point) and returns the unique ID to the newly created feature's attribute table (also a point).&amp;nbsp;For starters, I have been using a solved question thread originally posted by&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/467562"&gt;@James_Norquest&lt;/a&gt;,&amp;nbsp;which can be found&amp;nbsp;&lt;A href="https://community.esri.com/t5/attribute-rules-questions/attribute-rule-for-location-description/td-p/1040569" target="_self"&gt;here.&lt;/A&gt;&lt;/P&gt;&lt;P&gt;Here's the code that I've been using:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;//importing two fields, including geometry of the feature to be analyzed
var closestAsset = FeatureSetByName($datastore, "ClosestAssetFC",["label","type"], true);

//In OP, this is the "meat &amp;amp; potatoes"
var feature_buffer = Buffer($feature, 500, 'feet');
var nearest = First(Intersects(closestAsset, feature_buffer));

return nearest["label"];&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;When I create a new feature, the field to be populated by the rule returns the same asset ID from the ClosestAssetFC, no matter where I put it. Through some troubleshooting, I found that it was because the entire ClosestAssetFC was being returned by the script, and was only returning the first record, which was the same every time.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'd also like to subset the ClosestAssetFC to only include a specific asset type. Here's the code that I tried:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;var closestAsset = FeatureSetByName($datastore, "ClosestAssetFC", ["label","type"], true);

var feature_buffer = Buffer($feature, 500, 'feet');
var nearest = Intersects(closestAsset, feature_buffer);
var nearestFilter = Filter(nearest, 'type = "Asset Type 1"');

return First(nearestFilter["label"]);&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;This gives me an error, saying that a dictionary type was expected at line 8.&lt;/P&gt;&lt;P&gt;Any advice here would be much appreciated!&amp;nbsp;&lt;/P&gt;&lt;P&gt;Best,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Mike&lt;/P&gt;</description>
      <pubDate>Thu, 13 Jan 2022 16:18:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/attribute-rules-questions/finding-closest-point-returning-closest-point-s-id/m-p/1133441#M284</guid>
      <dc:creator>wcozzens</dc:creator>
      <dc:date>2022-01-13T16:18:54Z</dc:date>
    </item>
    <item>
      <title>Re: Finding closest point, returning closest point's ID in newly created feature's attribute table.</title>
      <link>https://community.esri.com/t5/attribute-rules-questions/finding-closest-point-returning-closest-point-s-id/m-p/1133749#M285</link>
      <description>&lt;P&gt;&lt;STRONG&gt;First()&lt;/STRONG&gt; only finds the first feature intersecting the buffer, not the closest one.&lt;/P&gt;&lt;P&gt;You get the error because you try to get "label" from the feature set, not from the first feature.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;return First(nearestFilter)["label"];&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is how I do it in my Attribute Rules:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;function 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
}

var fs_assets = FeatureSetByName($datastore, "AssetFC", ["label", "type"], true)
var fs_filtered_assets = Filter(fs_assets, "type = 'Asset Type 1'")
var fs_close_assets = Intersects(fs_filtered_assets, Buffer($feature, 500, "feet"))
var closest_asset = closest_feature($feature, fs_close_assets)

if(closest_asset == null) {
  return null
}
return closest_asset.label&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 14 Jan 2022 06:15:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/attribute-rules-questions/finding-closest-point-returning-closest-point-s-id/m-p/1133749#M285</guid>
      <dc:creator>JohannesLindner</dc:creator>
      <dc:date>2022-01-14T06:15:43Z</dc:date>
    </item>
  </channel>
</rss>

