<?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: How to use attribute rules to get nearest neighbors distance? in ArcGIS Pro Questions</title>
    <link>https://community.esri.com/t5/arcgis-pro-questions/how-to-use-attribute-rules-to-get-nearest/m-p/1204427#M58854</link>
    <description>&lt;P&gt;After line 10, you have to check is dist &amp;lt; minDistance. If yes, update minDistance and name. At the end, return minDistance.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Easier way:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;// Attribute Rule
// field: empty!

var NeighborLayer = FeatureSetByName($datastore, "TestNeighbors", ["NAME"]);
var searchDistance = 10000;
var NeighborIntersect = Intersects(NeighborLayer, Buffer($feature, searchDistance, "feet"));

// return early if nothing nearby, this saves you from all the indentation
if(First(NeighborIntersect) == null) {
    return
}

// create an array of dictionaries
var neighbor_attributes = []
for(var neighbor in NeighborIntersect) {
    var dist = Distance(neighbor, $feature, "feet")
    var att = {"NearestNeighbor": neighbor.Name, "Distance": dist}
    Push(neighbor_attributes, att)
}

// sort the array by distance
function sort_by_distance(a, b) {
    return a["Distance"] - b["Distance"]
}
neighbor_attributes = Sort(neighbor_attributes, sort_by_distance)

// return the nearest neighbor's name and distance in one go
return {
    "result": {"attributes": neighbor_attributes[0]}
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 19 Aug 2022 08:11:17 GMT</pubDate>
    <dc:creator>JohannesLindner</dc:creator>
    <dc:date>2022-08-19T08:11:17Z</dc:date>
    <item>
      <title>How to use attribute rules to get nearest neighbors distance?</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/how-to-use-attribute-rules-to-get-nearest/m-p/1204302#M58835</link>
      <description>&lt;P&gt;I am trying to use attribute rules to write in the distance to a nearest neighbor, I've been able to get the nearest neighbors name to show up, however the distance alludes me.&amp;nbsp; Here is the code I was using, however when add an area it seems to pick up the 3rd closest neighbor and not the nearest, can anyone help me figure out what I am doing wrong.&lt;/P&gt;&lt;LI-CODE lang="c"&gt;var NeighborLayer = FeatureSetByName($datastore, "TestNeighbors", ["NAME"]);
var searchDistance = 10000;
var NeighborIntersect = Intersects(NeighborLayer, Buffer($feature, searchDistance, "feet"));
var cnt = Count(NeighborIntersect);

var minDistance = 10000;
var name = Null
if (cnt &amp;gt; 0) {
    for (var Neighbor in NeighborIntersect) {
        var dist = Distance(Neighbor, $feature, "feet");
        
       
    }
} else {
    // pass no features found within search distance, name remains null
}

return dist;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Screenshot 2022-08-18 153518.png" style="width: 949px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/48938i34115AA337CC7A48/image-size/large?v=v2&amp;amp;px=999" role="button" title="Screenshot 2022-08-18 153518.png" alt="Screenshot 2022-08-18 153518.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 18 Aug 2022 19:38:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/how-to-use-attribute-rules-to-get-nearest/m-p/1204302#M58835</guid>
      <dc:creator>phess_luckstone</dc:creator>
      <dc:date>2022-08-18T19:38:21Z</dc:date>
    </item>
    <item>
      <title>Re: How to use attribute rules to get nearest neighbors distance?</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/how-to-use-attribute-rules-to-get-nearest/m-p/1204427#M58854</link>
      <description>&lt;P&gt;After line 10, you have to check is dist &amp;lt; minDistance. If yes, update minDistance and name. At the end, return minDistance.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Easier way:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;// Attribute Rule
// field: empty!

var NeighborLayer = FeatureSetByName($datastore, "TestNeighbors", ["NAME"]);
var searchDistance = 10000;
var NeighborIntersect = Intersects(NeighborLayer, Buffer($feature, searchDistance, "feet"));

// return early if nothing nearby, this saves you from all the indentation
if(First(NeighborIntersect) == null) {
    return
}

// create an array of dictionaries
var neighbor_attributes = []
for(var neighbor in NeighborIntersect) {
    var dist = Distance(neighbor, $feature, "feet")
    var att = {"NearestNeighbor": neighbor.Name, "Distance": dist}
    Push(neighbor_attributes, att)
}

// sort the array by distance
function sort_by_distance(a, b) {
    return a["Distance"] - b["Distance"]
}
neighbor_attributes = Sort(neighbor_attributes, sort_by_distance)

// return the nearest neighbor's name and distance in one go
return {
    "result": {"attributes": neighbor_attributes[0]}
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 19 Aug 2022 08:11:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/how-to-use-attribute-rules-to-get-nearest/m-p/1204427#M58854</guid>
      <dc:creator>JohannesLindner</dc:creator>
      <dc:date>2022-08-19T08:11:17Z</dc:date>
    </item>
    <item>
      <title>Re: How to use attribute rules to get nearest neighbors distance?</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/how-to-use-attribute-rules-to-get-nearest/m-p/1204455#M58860</link>
      <description>&lt;P&gt;This is awesome and it works on the database side when I bring in those layers directly into Pro from our SDE.&amp;nbsp; However when I tried to use that with a published service it didn't work, any ideas why, is it because it updates both fields at once?&amp;nbsp; When I tried to publish with this attribute rule turned on it wouldn't even let me publish but after deleting the attribute rule it let me.&lt;/P&gt;</description>
      <pubDate>Fri, 19 Aug 2022 10:35:06 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/how-to-use-attribute-rules-to-get-nearest/m-p/1204455#M58860</guid>
      <dc:creator>phess_luckstone</dc:creator>
      <dc:date>2022-08-19T10:35:06Z</dc:date>
    </item>
    <item>
      <title>Re: How to use attribute rules to get nearest neighbors distance?</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/how-to-use-attribute-rules-to-get-nearest/m-p/1204462#M58862</link>
      <description>&lt;P&gt;You probably have to publish the TestNeighbors in the same service, too.&lt;/P&gt;&lt;P&gt;If this doesn't work, then I don't know, the whole service thing is very trial-and-error for me.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You could try doing both fields in their own rule. Just select the field and change the return to&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;return neighbor_attributes[0]["NearestNeighbor"]&lt;/STRONG&gt;&amp;nbsp; and&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;return neighbor_attributes[0]["Distance"]&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;respectively.&lt;/P&gt;</description>
      <pubDate>Fri, 19 Aug 2022 10:54:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/how-to-use-attribute-rules-to-get-nearest/m-p/1204462#M58862</guid>
      <dc:creator>JohannesLindner</dc:creator>
      <dc:date>2022-08-19T10:54:24Z</dc:date>
    </item>
    <item>
      <title>Re: How to use attribute rules to get nearest neighbors distance?</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/how-to-use-attribute-rules-to-get-nearest/m-p/1206432#M59092</link>
      <description>&lt;P&gt;Thank you for your reply, I believe that services can't handle array's&amp;nbsp; and using what you had mentioned earlier this code works in our service perfectly, thank you for your help!&lt;/P&gt;&lt;LI-CODE lang="c"&gt;var NeighborLayer = FeatureSetByName($datastore, "Neighbors Feature Class", ["NAME"]);
var searchDistance = 10000;
var NeighborIntersect = Intersects(NeighborLayer, Buffer($feature, searchDistance, "feet"));
var cnt = Count(NeighborIntersect);

var minDistance = Infinity;
var name = Null
if (cnt &amp;gt; 0) {
    for (NeighborLayer in NeighborIntersect) {
var dist = Distance(NeighborLayer, $feature, "feet");
        if (dist &amp;lt; minDistance) {
            minDistance = dist
        }
        
       
    }
} else {
    // pass no features found within search distance, name remains null
}

return minDistance;&lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 25 Aug 2022 17:25:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/how-to-use-attribute-rules-to-get-nearest/m-p/1206432#M59092</guid>
      <dc:creator>phess_luckstone</dc:creator>
      <dc:date>2022-08-25T17:25:04Z</dc:date>
    </item>
  </channel>
</rss>

