<?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: Update layer based on another layer in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/update-layer-based-on-another-layer/m-p/1307465#M68140</link>
    <description>&lt;P&gt;I would try to restructure the&amp;nbsp; code roughly as follows:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import sys, os, arcpy
import arcpy

## Works in file geodatabase
arcpy.env.overwriteOutput = True
workspace = "C:/temp/temp.gdb"

PR = "PrivateRoads"
CityLimits = 'City_Limits'

arcpy.MakeFeatureLayer_management(PR, "polyLyr")
arcpy.MakeFeatureLayer_management(CityLimits, "CitLyr")
arcpy.management.SelectLayerByLocation("polyLyr", "HAVE_THEIR_CENTER_IN", "CitLyr", "", "NEW_SELECTION")

pr_oid_to_city_name = dict()

# Add code here to use a search cursor scan the polyLyr and 
# populate the dict that maps the pr oid to the city name

with arcpy.da.UpdateCursor(PR, ['OID@', 'Owner']) as cursor:
    for oid, owner in cursor:
        if oid in pr_oid_to_city_name.keys():
            cursor.updateRow([oid, pr_oid_to_city_name[oid])
 &lt;/LI-CODE&gt;</description>
    <pubDate>Tue, 11 Jul 2023 23:34:04 GMT</pubDate>
    <dc:creator>DonMorrison1</dc:creator>
    <dc:date>2023-07-11T23:34:04Z</dc:date>
    <item>
      <title>Update layer based on another layer</title>
      <link>https://community.esri.com/t5/python-questions/update-layer-based-on-another-layer/m-p/1307425#M68139</link>
      <description>&lt;P&gt;I need to update the PrivateRoads 'Owner' field based on what city the private road is in. I have the following but it take for ever but doesn't populate the correct city. I know I can do a spatial join but I don't want to create a separate layer.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import sys, os, arcpy
import arcpy

## Works in file geodatabase
arcpy.env.overwriteOutput = True
workspace = "C:/temp/temp.gdb"



PR = "PrivateRoads"
CityLimits = 'City_Limits'

arcpy.MakeFeatureLayer_management(PR, "polyLyr")
arcpy.MakeFeatureLayer_management(CityLimits, "CitLyr")

with arcpy.da.UpdateCursor(PR, ['SHAPE@', 'OID@', 'Owner']) as cursor:
    for row in cursor:
        arcpy.management.SelectLayerByLocation("polyLyr", "HAVE_THEIR_CENTER_IN", "CitLyr", "", "NEW_SELECTION")

        with arcpy.da.SearchCursor("CitLyr", ['CITY']) as cCursor:
                for cRow in cCursor:
                    row[2] = cRow[0]
                    cursor.updateRow(row)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 11 Jul 2023 20:17:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/update-layer-based-on-another-layer/m-p/1307425#M68139</guid>
      <dc:creator>2Quiker</dc:creator>
      <dc:date>2023-07-11T20:17:46Z</dc:date>
    </item>
    <item>
      <title>Re: Update layer based on another layer</title>
      <link>https://community.esri.com/t5/python-questions/update-layer-based-on-another-layer/m-p/1307465#M68140</link>
      <description>&lt;P&gt;I would try to restructure the&amp;nbsp; code roughly as follows:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import sys, os, arcpy
import arcpy

## Works in file geodatabase
arcpy.env.overwriteOutput = True
workspace = "C:/temp/temp.gdb"

PR = "PrivateRoads"
CityLimits = 'City_Limits'

arcpy.MakeFeatureLayer_management(PR, "polyLyr")
arcpy.MakeFeatureLayer_management(CityLimits, "CitLyr")
arcpy.management.SelectLayerByLocation("polyLyr", "HAVE_THEIR_CENTER_IN", "CitLyr", "", "NEW_SELECTION")

pr_oid_to_city_name = dict()

# Add code here to use a search cursor scan the polyLyr and 
# populate the dict that maps the pr oid to the city name

with arcpy.da.UpdateCursor(PR, ['OID@', 'Owner']) as cursor:
    for oid, owner in cursor:
        if oid in pr_oid_to_city_name.keys():
            cursor.updateRow([oid, pr_oid_to_city_name[oid])
 &lt;/LI-CODE&gt;</description>
      <pubDate>Tue, 11 Jul 2023 23:34:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/update-layer-based-on-another-layer/m-p/1307465#M68140</guid>
      <dc:creator>DonMorrison1</dc:creator>
      <dc:date>2023-07-11T23:34:04Z</dc:date>
    </item>
    <item>
      <title>Re: Update layer based on another layer</title>
      <link>https://community.esri.com/t5/python-questions/update-layer-based-on-another-layer/m-p/1307785#M68148</link>
      <description>&lt;P&gt;I tried the following but the print only prints one of the cities and for some reason it's populates only a a few private roads that are not inside city limits. Some of these will not be in a city.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I honestly thought it would simple and&amp;nbsp; wouldn't' involve .keys or dicts.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import sys, os, arcpy

arcpy.env.overwriteOutput = True
workspace = "C:/temp/temp.gdb"

PR = "PrivateRoads"
CityLimits = 'City_Limits'

arcpy.MakeFeatureLayer_management(PR, "polyLyr")
arcpy.MakeFeatureLayer_management(CityLimits, "CitLyr")
arcpy.management.SelectLayerByLocation("polyLyr", "HAVE_THEIR_CENTER_IN", "CitLyr", "", "NEW_SELECTION")

pr_oid_to_city_name = dict()

# Add code here to use a search cursor to scan the polyLyr and 
# populate the dict that maps the pr OID to the city name
with arcpy.da.SearchCursor("CitLyr", ['OID@', 'CITY']) as search_cursor:
    for oid, city_name in search_cursor:
        pr_oid_to_city_name[oid] = city_name

print(pr_oid_to_city_name[oid])
# Start an edit session. Must provide the workspace.
edit = arcpy.da.Editor(workspace)

# Edit session is started without an undo/redo stack for versioned data
# (for the second argument, use False for unversioned data)
edit.startEditing(True)

# Start an edit operation
edit.startOperation()

fields = ['OID@', 'Owner']
with arcpy.da.UpdateCursor(PR, fields) as cursor:
    for row in cursor:
        oid = row[0]
        if oid in pr_oid_to_city_name:
            row[1] = pr_oid_to_city_name[oid]
            cursor.updateRow(row)

edit.stopOperation()
# Stop the edit session and save the changes
edit.stopEditing(True)&lt;/LI-CODE&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>Wed, 12 Jul 2023 22:14:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/update-layer-based-on-another-layer/m-p/1307785#M68148</guid>
      <dc:creator>2Quiker</dc:creator>
      <dc:date>2023-07-12T22:14:17Z</dc:date>
    </item>
    <item>
      <title>Re: Update layer based on another layer</title>
      <link>https://community.esri.com/t5/python-questions/update-layer-based-on-another-layer/m-p/1308010#M68164</link>
      <description>&lt;P&gt;I was able to accomplish what I needed with the following.&lt;/P&gt;&lt;LI-CODE lang="c"&gt;PR = "PrivateRoads"
CityLimits = 'City_Limits'

arcpy.MakeFeatureLayer_management(PR, "polyLyr")
arcpy.MakeFeatureLayer_management(CityLimits, "CitLyr")

unique_strings = []
with arcpy.da.SearchCursor("CitLyr", 'CITY') as cursor:
    for row in cursor:
        unique_strings.append(row[0])
        #print (unique_strings)

for unique in unique_strings:
    arcpy.SelectLayerByAttribute_management("CitLyr", 'NEW_SELECTION', "CITY = '{}'".format(unique))
    arcpy.SelectLayerByLocation_management("polyLyr", 'HAVE_THEIR_CENTER_IN', "CitLyr", "", "NEW_SELECTION")
    with arcpy.da.UpdateCursor(PR, 'Owner') as cursor:
        for row in cursor:
            row[0] = unique
            cursor.updateRow(row)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 13 Jul 2023 17:56:53 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/update-layer-based-on-another-layer/m-p/1308010#M68164</guid>
      <dc:creator>2Quiker</dc:creator>
      <dc:date>2023-07-13T17:56:53Z</dc:date>
    </item>
  </channel>
</rss>

