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")
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")
Solved! Go to Solution.
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
If IsNull(InStr([fieldName], "DWELL")) then Output = "MOBILEHOME" ElseIf InSrt([fieldName], "DWELL") then Output = "HOME" Else Output = "MOBILEHOME" End If
import arcpy arcpy.env.overwriteOutput = True # Spatial Join (Analysis) [webhelp 10.0] # http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/Spatial_Join/00080000000q000000/ 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() print 'found current: ' + rowW.AddressID if rowR.CPUC == 'DWELL': rowW.FacltyType = 'HOME' print 'calc FacltyType HOME' else: rowW.FacltyType = 'MobileHome' print 'calc FacltyType MobileHome' curW.updateRow(rowW) rowR = curR.next() del rowW, rowR
import arcpy 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() print 'found current: ' + rowW.AddressID if rowR.CPUC == 'DWELL': rowW.FacltyType = 'Single Family Home' print 'calc FacltyType Single Family Home' else: rowW.FacltyType = 'MobileHome' print 'calc FacltyType MobileHome' rowW.StructType = 'Primary, Private' rowW.Verified = 'Yes, GRM, TA' #Date? rowW.Status = 'Active' rowW.StructCat = 'Residential' if rowR.FacltyType == 'Single Family Home': rowW.APA_CODE = '1110' print 'calc FacltyType Single Family Home' #Populate Single Family Home fiels** Does not do anything, does not populate if rowR.FacltyType == 'Single Family Home': rowW.StructType = 'Primary, Private' rowW.Verified = 'Yes, GRM, TA' #Date? rowW.Status = 'Active' rowW.StructCat = 'Residential' if rowR.FacltyType == 'MobileHome': rowW.APA_CODE = '1150' print 'calc FacltyType MobileHome' curW.updateRow(rowW) rowR = curR.next() del rowW, rowR
if rowR.CPUC == 'DWELL': rowW.FacltyType = 'Single Family Home'
import arcpy # Need overwrite turned on to enable temp layer # 'Points_joined' to be reused on successive executions. arcpy.env.overwriteOutput = True # Spatial Join (Analysis) [webhelp 10.0] # http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/Spatial_Join/00080000000q000000/ # fcTarget (Points_1) is to be written to... fcTarget = 'Points_1' # fcJoin (Points_2) contains the critical new info to assimilate... fcJoin = 'Points_2' # fcOutput (Points_joined) is simply the Spatial Join tool output class, # the Target and Join classes 'melded' together... fcOutput = 'Points_joined' # Critical webhelp doc: # Spatial Join (Analysis) [webhelp 10.0] # http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/Spatial_Join/00080000000q000000/ # I allowed fcOutput to be 'hard-written' to my default gdb...do as you like, # setting your workspace accordingly. # 'JOIN_ONE_TO_ONE' assures one record joined # if a 'spatial match' is found (intersect is the default spatial search method). # 'KEEP_COMMON' accommodates your selection set, dropping all records from Target not 'joined'. arcpy.SpatialJoin_analysis(fcTarget, fcJoin, fcOutput, 'JOIN_ONE_TO_ONE', 'KEEP_COMMON') # You wish to populate based on the spatial join your original Target. # Instead of adding a join, I elected to use 2 sorted cursors to search for # matching AddressID values (provided they're indeed unique) - this can actually # be based on any unique ID in the Target class that the Output class inherited # from the previous spatial join process. (Again, you can adapt this to 10.1 da cursors.) # 'AddressID A' is the sort parameter; 'A' means ascending order. # To keep track of what is read, what is write, I refer to: # curR as the 'Read'cursor (R - read) # curW as the 'Write' cursor (W - write) curR = arcpy.SearchCursor(fcOutput, '', '', '', 'AddressID A') curW = arcpy.UpdateCursor(fcTarget, '', '', '', 'AddressID A') # init rowW and rowR; same with the row obj, referring similarly: # rowW belongs to the write cursor; rowR belongs to the read cursor. rowW = curW.next() rowR = curR.next() # while loop; loops while there are read rows being handed out by read cursor. while rowR: # Current address to find a match for is from the 1st read row. # This var is 'reinitialized' every loop on read row. currentAddress = rowR.AddressID # This checks for the write row match on the current address. # No match means the next row is fetched from the write cursor. while rowW.AddressID != currentAddress: rowW = curW.next() # When the correct write row is found, i.e., the currentAddress is matched, # the CPUC value is read from the read row and conditionally set: # 'DWELL' in the read row means FacltyType is set to 'HOME' # Otherwise (else), FacltyType is set to 'MobileHome'. if rowR.CPUC == 'DWELL': rowW.FacltyType = 'HOME' else: rowW.FacltyType = 'MobileHome' # The write row obj must be committed to the write cursor obj. # If this step is forgotten, the table is not updated. curW.updateRow(rowW) # The next read row is fetched to start again back at 'while rowR'. rowR = curR.next() # cleanup, deleting obj refs; forgot about del the curW and curR objs. # This may be necessary to remove locks on your data... del rowW, rowR