<?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: Error when trying to find closest feature using buffer in Attribute Rules Questions</title>
    <link>https://community.esri.com/t5/attribute-rules-questions/error-when-trying-to-find-closest-feature-using/m-p/1287759#M896</link>
    <description>&lt;P&gt;Multiple problems here:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;line 2: &lt;A href="https://developers.arcgis.com/arcade/function-reference/geometry_functions/#buffer" target="_blank" rel="noopener"&gt;Buffer()&lt;/A&gt; takes a Feature or a Geometry, you're giving it a field value.&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;Buffer($feature, 50, 'feet');&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;line 6&amp;amp;7: If you loop through a Featureset, the loop variable is actually the Feature:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;for(var f in fs) {
    Console(f.Field)
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;line 8: &lt;A href="https://developers.arcgis.com/arcade/function-reference/geometry_functions/#distance" target="_blank" rel="noopener"&gt;Distance()&lt;/A&gt; takes tow Features or Geometries. You're supplying a non-existing field.&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;var d = Distance($feature, other_feature, "feet")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;line 14: this looks like you're used to the truthy values of Python. This doesn't work with Arcade, you have to check actual booleans.&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;if (closestHydrant != null) {&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;throughout the expression: You're using function names as variable names (Buffer, Feature, Distance). This is a very bad practice, because you're overwriting these functions.&lt;/P&gt;&lt;P&gt;After line 2, you can't call Buffer() anymore, because that name is now bound to a polygon. This isn't a problem in your expression, but you have the same thing going on with Distance(), and you try to call that in a loop, which will fail.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;var hydrantLayer = FeatureSetByName($datastore, 'SDEP.SDEADMIN.w_watr_hydrant');
var b = Buffer($feature, 50, 'feet');
var closestHydrant;
var closestDistance = 9999999;
var intersectedFeatures = Intersects(hydrantLayer, b);
for (var f in intersectedFeatures) {
  var d = Distance($feature, f, 'feet');
  if (d &amp;lt; closestDistance) {
    closestDistance = d;
    closestHydrant = f;
  }
}
if (closestHydrant != null) {
  return closestHydrant['UID'];
}
return null;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 10 May 2023 15:36:05 GMT</pubDate>
    <dc:creator>JohannesLindner</dc:creator>
    <dc:date>2023-05-10T15:36:05Z</dc:date>
    <item>
      <title>Error when trying to find closest feature using buffer</title>
      <link>https://community.esri.com/t5/attribute-rules-questions/error-when-trying-to-find-closest-feature-using/m-p/1287743#M893</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I'm having trouble with an attribute rule expression in ArcGIS Pro 2.9. I'm trying to find the nearest hydrant within 50 feet of a water sample site using a buffer, but I keep getting errors that say "field not found" or "expected convertible to geometry".&lt;/P&gt;&lt;P&gt;Here's the code I'm using:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;var hydrantLayer = FeatureSetByName($datastore, 'SDEP.SDEADMIN.w_watr_hydrant');
var buffer = Buffer($feature.Shape, 50, 'feet');
var closestHydrant;
var closestDistance = 9999999;
var intersectedFeatures = Intersects(hydrantLayer, buffer);
for (var i in intersectedFeatures) {
  var feature = intersectedFeatures[i];
  var distance = Distance($feature.geometry, feature.geometry, 'feet');
  if (distance &amp;lt; closestDistance) {
    closestDistance = distance;
    closestHydrant = feature;
  }
}
if (closestHydrant) {
  return closestHydrant['UID'];
} else {
  return null;
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've tried changing "geometry" to "Shape" and using FeatureSetByName to reference the hydrant layer, but I keep getting errors. Can anyone help me figure out what I'm doing wrong?&lt;/P&gt;&lt;P&gt;Thank you in advance for your help!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 10 May 2023 15:15:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/attribute-rules-questions/error-when-trying-to-find-closest-feature-using/m-p/1287743#M893</guid>
      <dc:creator>BrianHumphries1</dc:creator>
      <dc:date>2023-05-10T15:15:34Z</dc:date>
    </item>
    <item>
      <title>Re: Error when trying to find closest feature using buffer</title>
      <link>https://community.esri.com/t5/attribute-rules-questions/error-when-trying-to-find-closest-feature-using/m-p/1287753#M894</link>
      <description>Try passing in True as the forth argument, this will insure geometry will be returned&lt;BR /&gt;&lt;A href="https://developers.arcgis.com/arcade/function-reference/featureset_functions/#featuresetbyname" target="_blank"&gt;https://developers.arcgis.com/arcade/function-reference/featureset_functions/#featuresetbyname&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Wed, 10 May 2023 15:25:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/attribute-rules-questions/error-when-trying-to-find-closest-feature-using/m-p/1287753#M894</guid>
      <dc:creator>MikeMillerGIS</dc:creator>
      <dc:date>2023-05-10T15:25:23Z</dc:date>
    </item>
    <item>
      <title>Re: Error when trying to find closest feature using buffer</title>
      <link>https://community.esri.com/t5/attribute-rules-questions/error-when-trying-to-find-closest-feature-using/m-p/1287756#M895</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Couple other observations&lt;/P&gt;&lt;P&gt;i in this case, is the feature.&amp;nbsp; Looping over the FeatureSet gets access to the feaure&lt;/P&gt;&lt;PRE&gt;for (var i in intersectedFeatures) {&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also, you need fields and geometry in the FeatureSetByName&lt;/P&gt;&lt;P&gt;var hydrantLayer = FeatureSetByName($datastore, 'SDEP.SDEADMIN.w_watr_hydrant',['UID'], true);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 10 May 2023 15:29:49 GMT</pubDate>
      <guid>https://community.esri.com/t5/attribute-rules-questions/error-when-trying-to-find-closest-feature-using/m-p/1287756#M895</guid>
      <dc:creator>MikeMillerGIS</dc:creator>
      <dc:date>2023-05-10T15:29:49Z</dc:date>
    </item>
    <item>
      <title>Re: Error when trying to find closest feature using buffer</title>
      <link>https://community.esri.com/t5/attribute-rules-questions/error-when-trying-to-find-closest-feature-using/m-p/1287759#M896</link>
      <description>&lt;P&gt;Multiple problems here:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;line 2: &lt;A href="https://developers.arcgis.com/arcade/function-reference/geometry_functions/#buffer" target="_blank" rel="noopener"&gt;Buffer()&lt;/A&gt; takes a Feature or a Geometry, you're giving it a field value.&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;Buffer($feature, 50, 'feet');&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;line 6&amp;amp;7: If you loop through a Featureset, the loop variable is actually the Feature:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;for(var f in fs) {
    Console(f.Field)
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;line 8: &lt;A href="https://developers.arcgis.com/arcade/function-reference/geometry_functions/#distance" target="_blank" rel="noopener"&gt;Distance()&lt;/A&gt; takes tow Features or Geometries. You're supplying a non-existing field.&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;var d = Distance($feature, other_feature, "feet")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;line 14: this looks like you're used to the truthy values of Python. This doesn't work with Arcade, you have to check actual booleans.&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;if (closestHydrant != null) {&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;throughout the expression: You're using function names as variable names (Buffer, Feature, Distance). This is a very bad practice, because you're overwriting these functions.&lt;/P&gt;&lt;P&gt;After line 2, you can't call Buffer() anymore, because that name is now bound to a polygon. This isn't a problem in your expression, but you have the same thing going on with Distance(), and you try to call that in a loop, which will fail.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;var hydrantLayer = FeatureSetByName($datastore, 'SDEP.SDEADMIN.w_watr_hydrant');
var b = Buffer($feature, 50, 'feet');
var closestHydrant;
var closestDistance = 9999999;
var intersectedFeatures = Intersects(hydrantLayer, b);
for (var f in intersectedFeatures) {
  var d = Distance($feature, f, 'feet');
  if (d &amp;lt; closestDistance) {
    closestDistance = d;
    closestHydrant = f;
  }
}
if (closestHydrant != null) {
  return closestHydrant['UID'];
}
return null;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 10 May 2023 15:36:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/attribute-rules-questions/error-when-trying-to-find-closest-feature-using/m-p/1287759#M896</guid>
      <dc:creator>JohannesLindner</dc:creator>
      <dc:date>2023-05-10T15:36:05Z</dc:date>
    </item>
    <item>
      <title>Re: Error when trying to find closest feature using buffer</title>
      <link>https://community.esri.com/t5/attribute-rules-questions/error-when-trying-to-find-closest-feature-using/m-p/1288350#M898</link>
      <description>&lt;P&gt;Thank you Johannes&lt;SPAN&gt;&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&amp;nbsp;that did the trick! &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Your a &lt;span class="lia-unicode-emoji" title=":shooting_star:"&gt;🌠&lt;/span&gt;ROCK STAR&lt;span class="lia-unicode-emoji" title=":shooting_star:"&gt;🌠&lt;/span&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 11 May 2023 19:47:09 GMT</pubDate>
      <guid>https://community.esri.com/t5/attribute-rules-questions/error-when-trying-to-find-closest-feature-using/m-p/1288350#M898</guid>
      <dc:creator>BrianHumphries1</dc:creator>
      <dc:date>2023-05-11T19:47:09Z</dc:date>
    </item>
  </channel>
</rss>

