<?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: Using SelectLayerByLocation and CalculateField - all records populated, not just selected in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/using-selectlayerbylocation-and-calculatefield-all/m-p/1364575#M69490</link>
    <description>That's great, Alfred. Thank you. I simply had to define the selection. An easy fix!&lt;BR /&gt;Karl&lt;BR /&gt;</description>
    <pubDate>Wed, 27 Dec 2023 18:58:50 GMT</pubDate>
    <dc:creator>KarlKeough2</dc:creator>
    <dc:date>2023-12-27T18:58:50Z</dc:date>
    <item>
      <title>Using SelectLayerByLocation and CalculateField - all records populated, not just selected</title>
      <link>https://community.esri.com/t5/python-questions/using-selectlayerbylocation-and-calculatefield-all/m-p/1364063#M69466</link>
      <description>&lt;P&gt;I am using for loops to loop through a list of hexagon feature classes, selecting those hexagons in each feature class that overlap hexagons in each of the other feature classes and then calculating conflict scores for each of the overlapping records. The conflict scores are derived from an Excel table containing names of the two feature classes being compared and the relevant conflict score. The code seems to work fine, with no errors, except that it populates all records with the relevant conflict score, not just those records that overlap (the selected records). The code below shows the last few lines of the code. fc1 and fc2 represent the two layers being compared; fieldnames 1, 2 and 3 are defined earlier in the code and are derived by modifying feature class and field names.&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;with arcpy.da.SearchCursor(conflictScores, ["Activity1", "Activity2", "ConflictScore"]) as cursor:
            for row in cursor:
                print("START ROWS)")
                if row[0] == fieldname1 and row[1] == fieldname2:
                    score = row[2]
                    print(score)
                    arcpy.management.SelectLayerByLocation(fc1, "HAVE_THEIR_CENTER_IN", fc2, "", "NEW_SELECTION")
                    arcpy.management.CalculateField(fc1, fieldname3, score)
                    print(fc1, fieldname3, score)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 22 Dec 2023 18:02:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-selectlayerbylocation-and-calculatefield-all/m-p/1364063#M69466</guid>
      <dc:creator>KarlKeough2</dc:creator>
      <dc:date>2023-12-22T18:02:20Z</dc:date>
    </item>
    <item>
      <title>Re: Using SelectLayerByLocation and CalculateField - all records populated, not just selected</title>
      <link>https://community.esri.com/t5/python-questions/using-selectlayerbylocation-and-calculatefield-all/m-p/1364093#M69474</link>
      <description>&lt;P&gt;I've never seen a select by location in a cursor but I'd guess you need to use the geometry of each cursor row - rather than fc1 and fc2 which I assume are your entire features.&lt;/P&gt;&lt;P&gt;Maybe try this but no idea if it would work.&amp;nbsp; If not I'd prefer to compare with a geometry.disjoint on the SHAPE token.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;#needs to be a feature layer if not already
arcpy.MakeFeatureLayer_management(fc1,"fc1_layer")

with arcpy.da.SearchCursor(conflictScores, ["Activity1", "Activity2", "ConflictScore", "SHAPE@"]) as cursor:
            for row in cursor:
                print("START ROWS)")
                if row[0] == fieldname1 and row[1] == fieldname2:
                    score = row[2]
                    print(score)
                    arcpy.management.SelectLayerByLocation("fc1_layer", "HAVE_THEIR_CENTER_IN", row[3], "", "NEW_SELECTION")
                    arcpy.management.CalculateField("fc1_layer", fieldname3, score)
                    print(fc1, fieldname3, score)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 22 Dec 2023 19:39:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-selectlayerbylocation-and-calculatefield-all/m-p/1364093#M69474</guid>
      <dc:creator>DavidPike</dc:creator>
      <dc:date>2023-12-22T19:39:29Z</dc:date>
    </item>
    <item>
      <title>Re: Using SelectLayerByLocation and CalculateField - all records populated, not just selected</title>
      <link>https://community.esri.com/t5/python-questions/using-selectlayerbylocation-and-calculatefield-all/m-p/1364100#M69475</link>
      <description>Thanks for the quick reply David. The use of the SearchCursor may be a little confusing or unorthodox. The SearchCursor is on the Excel table which shows the various combinations of layer names (text fields) and the associated conflict score. I had to loop through the Excel records to find the name of the two layers that were being considered (fc1, fc2, from feature class lists earlier in the code) and the associated conflict score. That conflict score was then used to populate the selected records (using the SelectLayerByLocation tool) in the field called fieldname in fc1. I felt that had to be done within the cursor because the code had to look through the records of the Excel file to find the right combination of layers to assign the scores and then assign them before moving on to the next pair of layers. Not sure if this clarifies much. Perhaps I should try creating a layer file for the SelectLayerByLocation tool as you suggest. If seeing the rest of the code helps I can certainly send that. Thanks again for your help.&lt;BR /&gt;Karl&lt;BR /&gt;</description>
      <pubDate>Fri, 22 Dec 2023 20:15:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-selectlayerbylocation-and-calculatefield-all/m-p/1364100#M69475</guid>
      <dc:creator>KarlKeough2</dc:creator>
      <dc:date>2023-12-22T20:15:50Z</dc:date>
    </item>
    <item>
      <title>Re: Using SelectLayerByLocation and CalculateField - all records populated, not just selected</title>
      <link>https://community.esri.com/t5/python-questions/using-selectlayerbylocation-and-calculatefield-all/m-p/1364146#M69478</link>
      <description>&lt;P&gt;You have to make the selection a variable, then feed that variable into calculatefield&lt;/P&gt;</description>
      <pubDate>Fri, 22 Dec 2023 23:53:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-selectlayerbylocation-and-calculatefield-all/m-p/1364146#M69478</guid>
      <dc:creator>AlfredBaldenweck</dc:creator>
      <dc:date>2023-12-22T23:53:37Z</dc:date>
    </item>
    <item>
      <title>Re: Using SelectLayerByLocation and CalculateField - all records populated, not just selected</title>
      <link>https://community.esri.com/t5/python-questions/using-selectlayerbylocation-and-calculatefield-all/m-p/1364488#M69486</link>
      <description>Thank you for the response Alfred. From the examples I have seen in the CalculateField help page, it seems the expression that has to be passed into CalculateField (and that represents the selection), is based on attributes from the table, whereas in my case the selection is based on location in reference to another layer. It seems then that there must be two things passed into CalculateField - the records that are selected based on location and the value that will populate the selected records, which comes from the Excel table. Can these both be accommodated in the expression variable? Or is there another way?&lt;BR /&gt;Karl&lt;BR /&gt;[cid:image001.png@01DA38B8.60229CF0]&lt;BR /&gt;</description>
      <pubDate>Wed, 27 Dec 2023 15:02:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-selectlayerbylocation-and-calculatefield-all/m-p/1364488#M69486</guid>
      <dc:creator>KarlKeough2</dc:creator>
      <dc:date>2023-12-27T15:02:50Z</dc:date>
    </item>
    <item>
      <title>Re: Using SelectLayerByLocation and CalculateField - all records populated, not just selected</title>
      <link>https://community.esri.com/t5/python-questions/using-selectlayerbylocation-and-calculatefield-all/m-p/1364496#M69487</link>
      <description>&lt;P&gt;So, unless I'm missing something, this should be a very simple fix.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;with arcpy.da.SearchCursor(conflictScores, ["Activity1", "Activity2", "ConflictScore"]) as cursor:
    for row in cursor:
        print("START ROWS)")
        if row[0] == fieldname1 and row[1] == fieldname2:
            score = row[2]
            print(score)
            sel = arcpy.management.SelectLayerByLocation(fc1, "HAVE_THEIR_CENTER_IN", fc2, "", "NEW_SELECTION")
            arcpy.management.CalculateField(sel, fieldname3, score)
            print(fc1, fieldname3, score)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;What was happening was you were making a selection on a layer, but not feeding the selection to calculate field.&lt;/P&gt;&lt;P&gt;Give this a shot and see how it goes.&lt;/P&gt;&lt;P&gt;Also not sure if you meant to post a picture, but if you did, it didn't come through.&lt;/P&gt;</description>
      <pubDate>Wed, 27 Dec 2023 15:12:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-selectlayerbylocation-and-calculatefield-all/m-p/1364496#M69487</guid>
      <dc:creator>AlfredBaldenweck</dc:creator>
      <dc:date>2023-12-27T15:12:17Z</dc:date>
    </item>
    <item>
      <title>Re: Using SelectLayerByLocation and CalculateField - all records populated, not just selected</title>
      <link>https://community.esri.com/t5/python-questions/using-selectlayerbylocation-and-calculatefield-all/m-p/1364575#M69490</link>
      <description>That's great, Alfred. Thank you. I simply had to define the selection. An easy fix!&lt;BR /&gt;Karl&lt;BR /&gt;</description>
      <pubDate>Wed, 27 Dec 2023 18:58:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-selectlayerbylocation-and-calculatefield-all/m-p/1364575#M69490</guid>
      <dc:creator>KarlKeough2</dc:creator>
      <dc:date>2023-12-27T18:58:50Z</dc:date>
    </item>
  </channel>
</rss>

