Quantcast
Channel: Forums - Python
Viewing all articles
Browse latest Browse all 2485

Help with UdateCursor populating a layer from another selected layer

$
0
0
I am having trouble populating Points layer "FacltyType" field with the selected layers CPUC field.
I need to able to select multiple points from the Points_2 layer then select by location on the Points layer by using the SelectLayerByLocation. Then i need to update the "facltyType" field of Points layer with the selected Points_2 CPUC field.
If the Points_2 layer CPUC filed has "DWELL" i need the Points layer FacltyType field to populate as "Home", if it is NULL i need the FacltyType field of the Points layer to populate as "MobileHome"

My current code tempt makes a permanent Join between Points and Points_2 on the TOC. Is there a way on how to make a temporary Join then populate as i mentioned? Also the current code just populates the "FacltyType" with DELL, but i need to do as i mentioned above.

Code:

import arcpy, os
from arcpy import env

arcpy.env.overwriteOutput = True
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
lyr = arcpy.mapping.ListLayers(mxd, "Points")[0]

arcpy.env.workspace = os.path.dirname(mxd.filePath)
wp = os.path.dirname(mxd.filePath)
#env.workspace = r"C:\GIS\Addressing\test"

Points = "Points"
Points_2 = "Points_2"
Fileds = "APA_CODE"

selection = 1
while selection < 3:
        rows = arcpy.SearchCursor(Points) #opens search cursor on the info table
        for row in rows:
             
                facltyType = row.FacltyType # extracts the name of the destination field
 

        # Process: Select Layer By Location
        print "select by location"
        arcpy.SelectLayerByLocation_management(Points, "INTERSECT", Points_2, "", "NEW_SELECTION")
        if int(arcpy.GetCount_management(Points).getOutput(0)) > 0:

           
            arcpy.MakeFeatureLayer_management(Points, "PointsLyr")
            arcpy.MakeFeatureLayer_management(Points_2, "PointsLyr_2")
            arcpy.JoinField_management("PointsLyr", "Account", "PointsLyr_2", "Account", "")

       
        # Update the FacltyType field with the CPUC values
        rows = arcpy.UpdateCursor(Points)
        for row in rows:
            row.FacltyType = row.CPUC
            rows.updateRow(row)
        del rows
        del row
        selection +=1

arcpy.RemoveJoin_management("PointsLyr2")

I tried making a feature layer in_memory but i got the following error:
Runtime error Traceback (most recent call last): File "<string>", line 33, in <module> File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\management.py", line 5357, in JoinField raise e ExecuteError: Failed to execute. Parameters are not valid. ERROR 000732: Input Table: Dataset PointsLyrA does not exist or is not supported ERROR 000732: Join Table: Dataset Points2 does not exist or is not supported Failed to execute (JoinField).

Code:

import arcpy, os
from arcpy import env

arcpy.env.overwriteOutput = True
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
lyr = arcpy.mapping.ListLayers(mxd, "Points")[0]

arcpy.env.workspace = os.path.dirname(mxd.filePath)
wp = os.path.dirname(mxd.filePath)
#env.workspace = r"C:\GIS\Addressing\test"

Points = "Points"
Points_2 = "Points_2"
Fileds = "APA_CODE"

selection = 1
while selection < 3:
        rows = arcpy.SearchCursor(Points) #opens search cursor on the info table
        for row in rows:
             
                facltyType = row.FacltyType # extracts the name of the destination field
 

        # Process: Select Layer By Location
        print "select by location"
        arcpy.SelectLayerByLocation_management(Points, "INTERSECT", Points_2, "", "NEW_SELECTION")
        if int(arcpy.GetCount_management(Points).getOutput(0)) > 0:

           
            arcpy.MakeFeatureLayer_management(Points, "in_memory\PointsLyrA")
            arcpy.MakeFeatureLayer_management(Points_2, "in_memory\Points2")
            arcpy.JoinField_management("PointsLyrA", "Account", "Points2", "Account", "")

       
        # Update the FacltyType field with the CPUC values
        rows = arcpy.UpdateCursor(Points)
        for row in rows:
            row.FacltyType = row.CPUC
            rows.updateRow(row)
        del rows
        del row
        selection +=1


arcpy.Delete_management("in_memory")

Any code help would be great.

Thanks.

Viewing all articles
Browse latest Browse all 2485

Latest Images

Trending Articles



Latest Images