<?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: Return value based on area or length of overlap in ArcGIS Field Maps Questions</title>
    <link>https://community.esri.com/t5/arcgis-field-maps-questions/arcade-expression-return-value-based-on-area-or/m-p/1308241#M6507</link>
    <description>&lt;P&gt;If you iterate over a FeatureSet, you should always code on the assumption that multiple features may be returned. I would create an array and push the results into it.&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;// intersection
var UTMgrid = Intersects(FeatureSetByName($map, "UTM Zone Grid"), $feature)

// array to hold zone information
var zones = [];

// check if any features in the intersection
if (Count(UTMgrid) &amp;gt; 0) {
  // iterate over zones, populate array
  for (var g in UTMgrid) {
    Push(zones, g)
  }
} else {
  return "No Data";
}&lt;/LI-CODE&gt;&lt;P&gt;Once we get to this point, assuming there were intersecting features found, we'll have an array of one or more features in our &lt;STRONG&gt;zones&lt;/STRONG&gt; array.&lt;/P&gt;&lt;P&gt;Now, to determine which overlap is bigger, we can use the function &lt;STRONG&gt;Intersection&lt;/STRONG&gt;. This will give us a geometry that we can measure and compare. You &lt;EM&gt;could &lt;/EM&gt;create an array of dicts and use a custom sorting function to compare them, but we'll try a simpler approach.&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;// variables for overlap comparison
var biggest_overlap = '';
var overlap_amount = 0;

for (var z in zones) {
  var xs = Intersection($feature, zones[z])
  
  // get area of overlap; subsitute length if linear features
  var xs_overlap = Area(xs)
  
  // check against variables, replace of greater
  if (xs_overlap &amp;gt; overlap_amount) {
    overlap_amount = xs_overlap
    biggest_overlap = zones[z]['Zone']
  }
}

return Text(biggest_overlap)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 14 Jul 2023 15:47:27 GMT</pubDate>
    <dc:creator>jcarlson</dc:creator>
    <dc:date>2023-07-14T15:47:27Z</dc:date>
    <item>
      <title>Arcade Expression: Return value based on area or length of overlap</title>
      <link>https://community.esri.com/t5/arcgis-field-maps-questions/arcade-expression-return-value-based-on-area-or/m-p/1308217#M6505</link>
      <description>&lt;P&gt;Below is the arcade expression I'm using to intersect my field maps collection layers with the UTM grid layer in order to return the zone value in a field. This works fine for points but in the case for lines and polygons they might span two UTM zones. I'm trying to develop a way to deal with this. I was thinking I could tweak this expression in order to return the value of the grid polygon the line or polygon overlaps with the most. Wondering if anyone knows how to do that? Would I have to get the length or area first then return the value based on that number?&amp;nbsp;&lt;/P&gt;&lt;P&gt;If anyone has an easier solution please let me know.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Arcade Expression:&lt;/P&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;var&lt;/SPAN&gt; &lt;SPAN&gt;UTMgrid&lt;/SPAN&gt;&lt;SPAN&gt; = &lt;/SPAN&gt;&lt;SPAN&gt;Intersects&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;FeatureSetByName&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;$map&lt;/SPAN&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;SPAN&gt;"UTM Zone Grid"&lt;/SPAN&gt;&lt;SPAN&gt;), &lt;/SPAN&gt;&lt;SPAN&gt;$feature&lt;/SPAN&gt;&lt;SPAN&gt;)&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;var&lt;/SPAN&gt; &lt;SPAN&gt;zone&lt;/SPAN&gt;&lt;SPAN&gt; = &lt;/SPAN&gt;&lt;SPAN&gt;false&lt;/SPAN&gt;&lt;SPAN&gt;;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DI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SPAN&gt; &lt;SPAN&gt;"No Data"&lt;/SPAN&gt;&lt;SPAN&gt;;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;}&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Fri, 14 Jul 2023 14:22:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-field-maps-questions/arcade-expression-return-value-based-on-area-or/m-p/1308217#M6505</guid>
      <dc:creator>ChelseaP</dc:creator>
      <dc:date>2023-07-14T14:22:00Z</dc:date>
    </item>
    <item>
      <title>Re: Arcade Expression: Return value based on area or length of overlap</title>
      <link>https://community.esri.com/t5/arcgis-field-maps-questions/arcade-expression-return-value-based-on-area-or/m-p/1308241#M6507</link>
      <description>&lt;P&gt;If you iterate over a FeatureSet, you should always code on the assumption that multiple features may be returned. I would create an array and push the results into it.&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;// intersection
var UTMgrid = Intersects(FeatureSetByName($map, "UTM Zone Grid"), $feature)

// array to hold zone information
var zones = [];

// check if any features in the intersection
if (Count(UTMgrid) &amp;gt; 0) {
  // iterate over zones, populate array
  for (var g in UTMgrid) {
    Push(zones, g)
  }
} else {
  return "No Data";
}&lt;/LI-CODE&gt;&lt;P&gt;Once we get to this point, assuming there were intersecting features found, we'll have an array of one or more features in our &lt;STRONG&gt;zones&lt;/STRONG&gt; array.&lt;/P&gt;&lt;P&gt;Now, to determine which overlap is bigger, we can use the function &lt;STRONG&gt;Intersection&lt;/STRONG&gt;. This will give us a geometry that we can measure and compare. You &lt;EM&gt;could &lt;/EM&gt;create an array of dicts and use a custom sorting function to compare them, but we'll try a simpler approach.&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;// variables for overlap comparison
var biggest_overlap = '';
var overlap_amount = 0;

for (var z in zones) {
  var xs = Intersection($feature, zones[z])
  
  // get area of overlap; subsitute length if linear features
  var xs_overlap = Area(xs)
  
  // check against variables, replace of greater
  if (xs_overlap &amp;gt; overlap_amount) {
    overlap_amount = xs_overlap
    biggest_overlap = zones[z]['Zone']
  }
}

return Text(biggest_overlap)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 14 Jul 2023 15:47:27 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-field-maps-questions/arcade-expression-return-value-based-on-area-or/m-p/1308241#M6507</guid>
      <dc:creator>jcarlson</dc:creator>
      <dc:date>2023-07-14T15:47:27Z</dc:date>
    </item>
    <item>
      <title>Re: Arcade Expression: Return value based on area or length of overlap</title>
      <link>https://community.esri.com/t5/arcgis-field-maps-questions/arcade-expression-return-value-based-on-area-or/m-p/1308243#M6508</link>
      <description>&lt;P&gt;Note: there's really no reason to have the two portions separate, but I split it out for explanatory purposes. If you wanted, you could get the comparison section folded right into the &lt;STRONG&gt;for (var g...)&lt;/STRONG&gt; loop:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;// variables for overlap comparison
var biggest_overlap = '';
var overlap_amount = 0;

// intersection
var UTMgrid = Intersects(FeatureSetByName($map, "UTM Zone Grid"), $feature)

// check if any features in the intersection
if (Count(UTMgrid) &amp;gt; 0) {
  // iterate over zones, calculate intersection amount and compare
  for (var g in UTMgrid) {
    var xs = Intersection($feature, g)

    // get area of overlap; subsitute length if linear features
    var xs_overlap = Area(xs)

    if (xs_overlap &amp;gt; overlap_amount) {
      overlap_amount = xs_overlap
      biggest_overlap = Text(g['Zone'])
    }
  }
} else {
  return "No Data";
}

return biggest_overlap&lt;/LI-CODE&gt;</description>
      <pubDate>Fri, 14 Jul 2023 15:51:18 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-field-maps-questions/arcade-expression-return-value-based-on-area-or/m-p/1308243#M6508</guid>
      <dc:creator>jcarlson</dc:creator>
      <dc:date>2023-07-14T15:51:18Z</dc:date>
    </item>
    <item>
      <title>Re: Arcade Expression: Return value based on area or length of overlap</title>
      <link>https://community.esri.com/t5/arcgis-field-maps-questions/arcade-expression-return-value-based-on-area-or/m-p/1308307#M6512</link>
      <description>&lt;P&gt;Wow thanks! This works exactly how I hoped it would!&lt;/P&gt;</description>
      <pubDate>Fri, 14 Jul 2023 18:47:47 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-field-maps-questions/arcade-expression-return-value-based-on-area-or/m-p/1308307#M6512</guid>
      <dc:creator>ChelseaP</dc:creator>
      <dc:date>2023-07-14T18:47:47Z</dc:date>
    </item>
    <item>
      <title>Re: Arcade Expression: Return value based on area or length of overlap</title>
      <link>https://community.esri.com/t5/arcgis-field-maps-questions/arcade-expression-return-value-based-on-area-or/m-p/1340214#M7223</link>
      <description>&lt;P&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/363906"&gt;@jcarlson&lt;/a&gt;&amp;nbsp;Would you have any suggestions on how to adapt this to compare what would effectively be the null part of the geometry for the non-intersecting part of feature to the intersecting part? I'm attempting this same operation on a dataset that is not wall-to-wall. The logic to handle non-intersection works fine and returns the desired value. The problem is even if a tiny part of the feature intersects a zone it brings back the value for that zone.&lt;/P&gt;&lt;P&gt;What I would like is, if the area of non-intersection is greater than the area intersected, return the 'null' value for non-intersection. My attempt is below, where I was thinking to define it as 'if there is no intersection or if the intersected area constitutes less than half of the total area of the feature':&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;var overlap_amount = 0

var cdfPortal = Portal("*")
var fsFHSZ = FeatureSetByPortalItem(cdfPortal, "*", 0, ['FHSZ'])

var fsFHSZIntersect = Intersects(fsFHSZ, $feature)
var areaFeature = Area($feature)
var areaFHSZIntersect = Area(fsFHSZIntersect)

if (IsEmpty(fsFHSZIntersect) || areaFHSZIntersect &amp;lt; areaFeature/2) {
    return 0
} else {
   for(var z in fsFHSZIntersect) {
    var zone = Intersection($feature, z)
    var zoneOverlap = Area(zone)
    if (zoneOverlap &amp;gt; overlap_amount) {
       overlap_amount = zoneOverlap
       var largest_overlap = z["FHSZ"]
       return largest_overlap 
    }
   }
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;A direct adaptation of your code has the same problem, as does the function/dictionary technique that I've attempted in a few variations:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;var cdfPortal = Portal("*")
var fsFHSZ = FeatureSetByPortalItem(cdfPortal, "*", 0, ['FHSZ'])

var fsFHSZIntersect = Intersects(fsFHSZ, $feature)
var intersected_zones = []
for(var z in fsFHSZIntersect) {
    var zone = {
        Zone: z.FHSZ,
        Overlap: Area(Intersection(z, $feature))
    }
    Push(intersected_zones, zone)
}

function sort_by_overlap(z1, z2) { return z1.Overlap &amp;lt; z2.Overlap }

var zone = First(Sort(intersected_zones, sort_by_overlap))

return zone.Zone

if (Zone == null) {
     return 0
 } else {
     return zone.Zone
 }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In the below image the result should be 0, but instead it brings up 'Moderate' (the area in yellow which it intersects slightly).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="SFM_TravisBott_0-1697836483138.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/83634iF02FED1CE8B0DD26/image-size/medium?v=v2&amp;amp;px=400" role="button" title="SFM_TravisBott_0-1697836483138.png" alt="SFM_TravisBott_0-1697836483138.png" /&gt;&lt;/span&gt;&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;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 20 Oct 2023 21:17:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-field-maps-questions/arcade-expression-return-value-based-on-area-or/m-p/1340214#M7223</guid>
      <dc:creator>SFM_TravisBott</dc:creator>
      <dc:date>2023-10-20T21:17:33Z</dc:date>
    </item>
  </channel>
</rss>

