<?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 create a Attribute Rule to populate a field based on its location inside a polygon? in Developers Questions</title>
    <link>https://community.esri.com/t5/developers-questions/how-to-create-a-attribute-rule-to-populate-a-field/m-p/1332835#M6768</link>
    <description>&lt;P&gt;If your land covers are all in the same feature class (untested):&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;var covers = FeaturesetByName(...)
var i_covers = Intersects(covers, $feature)

var out = []
for(var c in i_covers) {
    Push(out, c.LandCoverField)
}

return Concatenate(Distinct(out), ", ")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you have each land cover class in its own feature class (untested):&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;var covers = {
    "Forest": FeaturesetByName(...),
    "Agriculture": FeaturesetByName(...),
    "Water": FeaturesetByName(...),
    }

var out = []
for(var cover in covers) {
    var i_cover = First(Intersects(covers[cover], $feature))
    if(i_cover != null) {
        Push(out, cover)
    }
}

return Concatenate(out, ", ")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 27 Sep 2023 10:42:37 GMT</pubDate>
    <dc:creator>JohannesLindner</dc:creator>
    <dc:date>2023-09-27T10:42:37Z</dc:date>
    <item>
      <title>How to create a Attribute Rule to populate a field based on its location inside a polygon?</title>
      <link>https://community.esri.com/t5/developers-questions/how-to-create-a-attribute-rule-to-populate-a-field/m-p/1100505#M6015</link>
      <description>&lt;P&gt;Is it possible to have a polygon set as a boundary to define a specific value in a field based on which boundary it was created within?&lt;/P&gt;&lt;P&gt;Currently there are two different boundaries in two different locations.&amp;nbsp;&lt;/P&gt;&lt;P&gt;i.e., Area 1 is called Forest, Area 2 is called City. If anything is created in the Forest area the location id will be populated with Forest, and if anything is created in the City area it will be populated with City.&lt;/P&gt;&lt;P&gt;The polygon boundary is one layer by its self.&lt;/P&gt;&lt;P&gt;The others will be a variety of different layers and consist of points, lines, polygons.&lt;/P&gt;&lt;P&gt;Can this be created into a attribute rule?&lt;/P&gt;</description>
      <pubDate>Tue, 21 Sep 2021 19:00:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/developers-questions/how-to-create-a-attribute-rule-to-populate-a-field/m-p/1100505#M6015</guid>
      <dc:creator>Lownbey</dc:creator>
      <dc:date>2021-09-21T19:00:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a Attribute Rule to populate a field based on its location inside a polygon?</title>
      <link>https://community.esri.com/t5/developers-questions/how-to-create-a-attribute-rule-to-populate-a-field/m-p/1100678#M6016</link>
      <description>&lt;P&gt;Sure, that's one of the most common use cases for Attribute Rules.&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;// Calculation Attribute Rule
// Field: LocationID
// Triggers: Insert, Update
// Exclude: True

// load the Areas poylgon class
var areas = FeatureSetByName($datastore, "NameOfFeatureclass", ["NameField"], true)

// intersect the areas with the inserted/edited feature
var inter = Intersects(areas, $feature)

// if feature is not in any area, return null
if(inter == null || Count(inter) == 0) {
  return null
}

// else return the first intersecting area's name field
return First(inter).NameField&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 22 Sep 2021 07:05:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/developers-questions/how-to-create-a-attribute-rule-to-populate-a-field/m-p/1100678#M6016</guid>
      <dc:creator>JohannesLindner</dc:creator>
      <dc:date>2021-09-22T07:05:33Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a Attribute Rule to populate a field based on its location inside a polygon?</title>
      <link>https://community.esri.com/t5/developers-questions/how-to-create-a-attribute-rule-to-populate-a-field/m-p/1100781#M6019</link>
      <description>&lt;P&gt;Johannes,&lt;/P&gt;&lt;P&gt;Thank you for your help!&lt;/P&gt;&lt;P&gt;I have yet another questions. I have about 200 layers I need to add this to. What is the fastest or simplest way to do so?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 22 Sep 2021 18:49:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/developers-questions/how-to-create-a-attribute-rule-to-populate-a-field/m-p/1100781#M6019</guid>
      <dc:creator>Lownbey</dc:creator>
      <dc:date>2021-09-22T18:49:14Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a Attribute Rule to populate a field based on its location inside a polygon?</title>
      <link>https://community.esri.com/t5/developers-questions/how-to-create-a-attribute-rule-to-populate-a-field/m-p/1101146#M6021</link>
      <description>&lt;P&gt;Probably Python.&lt;/P&gt;&lt;P&gt;In ArcGIS Pro, open the Python window:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="JohannesLindner_0-1632384073958.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/23696i6C7652060930F071/image-size/medium?v=v2&amp;amp;px=400" role="button" title="JohannesLindner_0-1632384073958.png" alt="JohannesLindner_0-1632384073958.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Edit and run this code:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# characteristics of the attribute rule
rule_name = "LocationID"
rule_type = "CALCULATION"
triggers = "INSERT;UPDATE"
field = "LocationID"
is_editable = True
exclude = True
description = "Checks whether the feature is in a Forest or City area and inputs the corresponding value."
arcade_expression = """// Calculation Attribute Rule
// Field: LocationID
// Triggers: Insert, Update
// Exclude: True

// load the Areas poylgon class
var areas = FeatureSetByName($datastore, "NameOfFeatureclass", ["NameField"], true)

// intersect the areas with the inserted/edited feature
var inter = Intersects(areas, $feature)

// if feature is not in any area, return null
if(inter == null || Count(inter) == 0) {
  return null
}

// else return the first intersecting area's name field
return First(inter).NameField"""


# list of your layer names / feature class paths, must be connected as data owner to have the permission to add rules
layer_list = [
    "layer_1",
    "database.sde/featureclass_2",
    "layer_3",
    ]


for layer in layer_list:
    print(layer)
    try:
        arcpy.management.AddAttributeRule(
            in_table=layer,
            name=rule_name,
            type=rule_type,  
            triggering_events=triggers, 
            field=field,  
            is_editable=is_editable, 
            script_expression=arcade_expression,
            exclude_from_client_evaluation=exclude,
            description=self.description
            )
        print("success")
    except Exception as e:
        print("ERROR: {}".format(e))
        &lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 23 Sep 2021 08:17:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/developers-questions/how-to-create-a-attribute-rule-to-populate-a-field/m-p/1101146#M6021</guid>
      <dc:creator>JohannesLindner</dc:creator>
      <dc:date>2021-09-23T08:17:38Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a Attribute Rule to populate a field based on its location inside a polygon?</title>
      <link>https://community.esri.com/t5/developers-questions/how-to-create-a-attribute-rule-to-populate-a-field/m-p/1101178#M6022</link>
      <description>&lt;P&gt;Johannes,&lt;/P&gt;&lt;P&gt;I appreciate it, however, there is not an easier way to apply the rules besides typing 200 layers in a list?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there not a way on the server or api to automatically load x rules on y layers?&lt;/P&gt;</description>
      <pubDate>Thu, 23 Sep 2021 12:00:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/developers-questions/how-to-create-a-attribute-rule-to-populate-a-field/m-p/1101178#M6022</guid>
      <dc:creator>Lownbey</dc:creator>
      <dc:date>2021-09-23T12:00:45Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a Attribute Rule to populate a field based on its location inside a polygon?</title>
      <link>https://community.esri.com/t5/developers-questions/how-to-create-a-attribute-rule-to-populate-a-field/m-p/1101558#M6023</link>
      <description>&lt;P&gt;None that I know of (I know very little of the server stuff and of the python API). Maybe if you ask that as a new question, someone can tell you for sure.&lt;/P&gt;</description>
      <pubDate>Fri, 24 Sep 2021 06:53:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/developers-questions/how-to-create-a-attribute-rule-to-populate-a-field/m-p/1101558#M6023</guid>
      <dc:creator>JohannesLindner</dc:creator>
      <dc:date>2021-09-24T06:53:42Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a Attribute Rule to populate a field based on its location inside a polygon?</title>
      <link>https://community.esri.com/t5/developers-questions/how-to-create-a-attribute-rule-to-populate-a-field/m-p/1332715#M6767</link>
      <description>&lt;P&gt;Hi Johannes,&lt;/P&gt;&lt;P&gt;Is there a way to modify this if I wanted multiple land cover classes (converted to polygons) to be populated into a field rather than just one area (forest vs city) in this example? I have 9 total land covers that I converted to polygons in their own feature class, and I'd like to populate a field in a parcel feature class that would include all of the land covers included within each parcel. Thank you!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 26 Sep 2023 21:36:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/developers-questions/how-to-create-a-attribute-rule-to-populate-a-field/m-p/1332715#M6767</guid>
      <dc:creator>Jaime_Carlino</dc:creator>
      <dc:date>2023-09-26T21:36:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a Attribute Rule to populate a field based on its location inside a polygon?</title>
      <link>https://community.esri.com/t5/developers-questions/how-to-create-a-attribute-rule-to-populate-a-field/m-p/1332835#M6768</link>
      <description>&lt;P&gt;If your land covers are all in the same feature class (untested):&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;var covers = FeaturesetByName(...)
var i_covers = Intersects(covers, $feature)

var out = []
for(var c in i_covers) {
    Push(out, c.LandCoverField)
}

return Concatenate(Distinct(out), ", ")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you have each land cover class in its own feature class (untested):&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;var covers = {
    "Forest": FeaturesetByName(...),
    "Agriculture": FeaturesetByName(...),
    "Water": FeaturesetByName(...),
    }

var out = []
for(var cover in covers) {
    var i_cover = First(Intersects(covers[cover], $feature))
    if(i_cover != null) {
        Push(out, cover)
    }
}

return Concatenate(out, ", ")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Sep 2023 10:42:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/developers-questions/how-to-create-a-attribute-rule-to-populate-a-field/m-p/1332835#M6768</guid>
      <dc:creator>JohannesLindner</dc:creator>
      <dc:date>2023-09-27T10:42:37Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a Attribute Rule to populate a field based on its location inside a polygon?</title>
      <link>https://community.esri.com/t5/developers-questions/how-to-create-a-attribute-rule-to-populate-a-field/m-p/1332939#M6769</link>
      <description>&lt;P&gt;Johannes,&amp;nbsp;&lt;/P&gt;&lt;P&gt;The first group of code in your recent response worked just as I needed it to. Thank you so much for your help!&lt;/P&gt;</description>
      <pubDate>Wed, 27 Sep 2023 14:47:35 GMT</pubDate>
      <guid>https://community.esri.com/t5/developers-questions/how-to-create-a-attribute-rule-to-populate-a-field/m-p/1332939#M6769</guid>
      <dc:creator>Jaime_Carlino</dc:creator>
      <dc:date>2023-09-27T14:47:35Z</dc:date>
    </item>
  </channel>
</rss>

