Select to view content in your preferred language

Help with UdateCursor populating a  layer from another selected layer

1622
13
Jump to solution
11-22-2013 08:15 AM
TonyAlmeida
MVP Regular Contributor
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.

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).

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.
Tags (2)
0 Kudos
13 Replies
T__WayneWhitley
Honored Contributor
Tony, you were almost there, see this -- definitely need to test the code below (I did not).  I wasn't sure how you wanted the fields coded since your last code was a little mixed up.  But no worries, for anything conditional to code a field, you'd put the under the if statement for whether it should be applied to Mobile Homes or Single Family Homes... see my comments in the code below - notice the indention alignment; for anything NOT conditional, that is it is coded the same way whether Mobile Home or Single Family Homes, enter your statements above the line, curW.updateRow(rowW).

Also, notice the 1st line where the 'time' module was imported...then further down, how the time was formatted for the Verified field.  Insert extra text formatting as you need it, as in this example (without '-' or '/' the date is 'mashed' together) - this is how you return the current local time (verify this yourself, I tested the following in IDLE; your corrected script it further below for you to test):

>>> import time
>>> time.strftime('%m%d%Y')
'11262013'
>>> time.strftime('%m-%d-%Y')
'11-26-2013'

>>> 'Yes, GRM, TA, ' + time.strftime('%m-%d-%Y')
'Yes, GRM, TA, 11-26-2013'
>>>


import arcpy, time arcpy.env.overwriteOutput = True  arcpy.env.workspace = r"C:\Temp\Defult.gdb" fcTarget = 'Points_1' fcJoin = 'Points_2' fcOutput = 'Points_joined'  arcpy.SpatialJoin_analysis(fcTarget, fcJoin, fcOutput, 'JOIN_ONE_TO_ONE', 'KEEP_COMMON')  curR = arcpy.SearchCursor(fcOutput, '', '', '', 'AddressID A') curW = arcpy.UpdateCursor(fcTarget, '', '', '', 'AddressID A')  # init rowW and rowR rowW = curW.next() rowR = curR.next()  while rowR:     currentAddress = rowR.AddressID     print 'current add: ' + currentAddress     while rowW.AddressID != currentAddress:         rowW = curW.next()     if rowR.CPUC == 'DWELL':         rowW.FacltyType = 'Single Family Home'         rowW.APA_CODE = '1110'         rowW.StructType = 'Primary, Private'         rowW.Verified = 'Yes, GRM, TA, ' + time.strftime('%m-%d-%Y')         rowW.Status = 'Active'         rowW.StructCat = 'Residential'     else:         rowW.FacltyType = 'MobileHome'         rowW.APA_CODE = '1150'         # put any additional conditional field statements here      # put any 'global' field statements here (applies to both SF/MH)             curW.updateRow(rowW)     rowR = curR.next()  # changed the delete statement, targeting the cursor objs (rather than the row objs) if curW:     del curW if curR:     del curR


Hope that helps - and by the way, you should probably mark this post answered...

Enjoy,
Wayne


EDIT:
I notice you made an earlier note about 'disappearance' of data - this could be due to cursor-locking.  I mentioned that you may explicitly need a del statement at the end of the script, but my previous one targeted only the row objects -- this should really be on the cursor objs (at minimum), and I don't think you caught that, so I'm editing the script in this post to reflect that correction.
0 Kudos
TonyAlmeida
MVP Regular Contributor
Wayne that works perfect.

I jumped the gun a little to early, I added the script as a tool. I noticed that some of the attributes in the CPUC filed had other attributes besides DWELL. So my next question (hopefully i have used up all mine up) is. Would there be a way to write something that states if there is something with DWELL populate with House, but if it is NULL populate with Mobilehome but if it is something else populate it with what is on the Points_2 CPUC field?

Thanks very much for all your help.
0 Kudos
T__WayneWhitley
Honored Contributor
Yep, not a problem - however, you have used up all your questions for this thread.
Mark the post in this already-long-thread that best answers your original question.  I'll look for your new posted question.

As a matter of fact, I agree with Richard about leaving Null values in your data (point for him) - it does lean toward data integrity problems, think about it - how in the world would you validate (if you run any validation procedures) any of these Null values?  Just note that FYI.

Good job so far, but let's close this one - in your next question, you can even refer to this thread to make your request better understood.


Wayne
0 Kudos
TonyAlmeida
MVP Regular Contributor
Thank you for your help. The Null attributes is how i get the data. I could calculate to insert something to replace the Nulls.

I should have asked this in the beginning but of course things come up as you are testing, but lets say there already was text in the FacltyType field in Points_1. and the text was correct. so before checking to see if there is any thing in the CPUC of Points_2 could i ask it to first check the FacltyType field in Points_1 and if there is text in that field to just by pass that feature point and go on to the next one and if there nothing on FacltyType field in Points_1 to then do check CPUC Field in Points_2 and if there is DWELL populate FacltyType with Home.
0 Kudos