# Import standard library modules import win32com.client, sys, os, arcpy, ctypes ## # Create the Geoprocessor object GP = win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1") # Set the Workspace Location GP.Workspace= sys.argv[1] #"L:/GIS/DATA/2012/Ownership" #GP.Workspace= "L:/GIS/DATA/2012/Ownership" # Obtain a list of feature classes from the above Workspace fcs = GP.ListFeatureClasses("*","polygon") # Check to see if a field exists def FieldExists(myFC,myFN): myDesc = arcpy.Describe(myFC) for field in myDesc.fields: if field.name == myFN: return "true" # Reset the enumeration to make sure the first object is returned # An Enumeration is a list fcs.reset() # Get the first feature class name from the fcs enumeration fc = fcs.next() while fc: # While the feature class name is not None (check if it exists) # do the following, starting with the first feature class # Get the next feature class name GP.AddMessage("List of Feature Classes: " + fc) # Check for a specific feature class name if (fc == "QTRTEST.shp"): if (not FieldExists (fc,"ShortRoll") == "true"): GP.AddMessage ("Adding Field: ShortRoll") arcpy.AddField_management(fc, "ShortRoll", "TEXT", "", "", 10) else: arcpy.DeleteField_management(fc, "ShortRoll") arcpy.AddField_management(fc, "ShortRoll", "TEXT", "", "", 10) # Use a SearchCursor to obtain access to all the rows of data # in the above feature class rows = arcpy.UpdateCursor(fc,"","","ROLL_NUMBE; test; ShortRoll","") # Iterate through all the rows in the cursor for row in rows: myString = str(row.ROLL_NUMBE) myLen = len(myString) myString = myString[(myLen-4):] row.test = myString[:2] row.ShortRoll = "1234abcd" rows.updateRow(row) # this command returns to the beginning of the while loop # to see if there is another feature class del row del rows fc = fcs.next() Solved! Go to Solution.
fc = "Parcels" list = [] rows = arcpy.SearchCursor(fc) for row in rows: PIN = str(row.PIN)[0:5] list.append(PIN) del row, rows pinList = [] for item in list: if item not in pinList: pinList.append(item) def updateDirection(value1, value2, direction): for item in pinList: item1 = item + value1 rows = arcpy.SearchCursor(fc, "PIN = " + item1) for row in rows: Name = row.Name item2 = item + value2 rows2 = arcpy.UpdateCursor(fc, "PIN = " + item2) for row2 in rows2: if row2.Name == Name: row2.Direction = direction rows2.updateRow(row2) del row, rows, row2, rows2 updateDirection('20', '10', 'west') updateDirection('30', '20', 'north') updateDirection('40', '30', 'east') updateDirection('10', '40', 'south')fc = "Parcels" list = [] rows = arcpy.SearchCursor(fc) for row in rows: PIN = str(row.PIN)[0:5] list.append(PIN) del row, rows pinList = [] for item in list: if item not in pinList: pinList.append(item) def updateDirection(value1, value2, direction): for item in pinList: item1 = item + value1 rows = arcpy.SearchCursor(fc, "PIN = " + item1) for row in rows: Name = row.Name item2 = item + value2 rows2 = arcpy.UpdateCursor(fc, "PIN = " + item2) for row2 in rows2: if row2.Name == Name: row2.Direction = direction rows2.updateRow(row2) del row, rows, row2, rows2 updateDirection('20', '10', 'west') updateDirection('30', '20', 'north') updateDirection('40', '30', 'east') updateDirection('10', '40', 'south')# Import standard library modules
import win32com.client, sys, os, arcpy, ctypes
##
# Create the Geoprocessor object
GP = win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1")
# Set the Workspace Location
GP.Workspace= sys.argv[1] #"L:/GIS/DATA/2012/Ownership"
#GP.Workspace= "L:/GIS/DATA/2012/Ownership"
# Obtain a list of feature classes from the above Workspace
fcs = GP.ListFeatureClasses("*","polygon")
# Check to see if a field exists
def FieldExists(myFC,myFN):
myDesc = arcpy.Describe(myFC)
for field in myDesc.fields:
if field.name == myFN:
return "true"
def updateDirection(value1, value2, direction):
for item in pinList:
item1 = item + value1
rows = arcpy.SearchCursor(fc, "ROLL_NUMBE = " + item1)
for row in rows:
Name = row.Name
item2 = item + value2
rows2 = arcpy.UpdateCursor(fc, "ROLL_NUMBE = " + item2)
for row2 in rows2:
if row2.Name == Name:
row2.myNewField = direction
rows2.updateRow(row2)
del row, rows, row2, rows2
myNewField = "Direction"
# Reset the enumeration to make sure the first object is returned
# An Enumeration is a list
fcs.reset()
# Get the first feature class name from the fcs enumeration
fc = fcs.next()
while fc: ## While the feature class name is not None (check if it exists)
## do the following, starting with the first feature class
GP.AddMessage("List of Feature Classes: " + fc)
## Check for a specific feature class name
if (fc == "QTRTEST.shp"):
lockTest = arcpy.TestSchemaLock (fc)
if lockTest:
GP.AddMessage("If Loop: " + fc)
else:
print "Unable to aquire Schema Lock"
if (not FieldExists (fc,myNewField) == "true"):
GP.AddMessage ("Adding Field: " + myNewField)
arcpy.AddField_management(fc, myNewField, "TEXT", "", "", 10)
else:
arcpy.DeleteField_management(fc, myNewField)
arcpy.AddField_management(fc, myNewField, "TEXT", "", "", 10)
List = []
rows = arcpy.SearchCursor(fc)
for row in rows:
## myString = str(row.ROLL_NUMBE)
## myLen = len(myString)
## myString = myString[(myLen-4):]
## list.append(myString)
myString = str(row.ROLL_NUMBE)[((len(str(row.ROLL_NUMBE)))-4):]
list.append(myString)
print myString
del row, rows
for item in list:
if item not in pinList:
pinList.append(item)
updateDirection('20', '10', 'west')
updateDirection('30', '20', 'north')
updateDirection('40', '30', 'east')
updateDirection('10', '40', 'south')
fc = fcs.next()
arcpy.RefreshActiveView()
print "complete"