0

I'm using ArcGIS desktop 10.6 and executing an arcpy script.

I'm creating a script that checks an input shapefiles field values against a set of rules. To do so I've created 2 lists with unique values of 2 fields within my input shapefile.

These are the UniqueDispTypeValues list and RelUnqID list.

I also have created a list of allowable values to check against.

This is the DispTypeValues list.

I created an if else statement to check a basic rule (If only RTF value exists the Rel_Unq_ID must be 0), however I want to check the current value of the field after the if statement(kind of validating a field value, or passing a rule in my case) instead of just the print statement.

Here is a screenshot of the attribute table: enter image description here

Here is the code:

# Importing necessary modules
import arcpy, os

arcpy.env.overwriteOutput = True

# Setting path to mxd and Data Frame
mxd = arcpy.mapping.MapDocument(r"CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]

# Assign the OneSTOP shapefile to a variable
shp = r"P:\JOBS\2020\20-1095\Outgoing_Information\OneSTOP\Disposition.shp"

#Create the unique list of allowable Disposition Types
def DTV(CHK_TABLE , Disp_Type):
    with arcpy.da.SearchCursor(CHK_TABLE, [Disp_Type]) as cursor:
        return sorted({row[0] for row in cursor})
    
DispTypeValues = DTV(r'Q:\GIS\OneSTOP\OneSTOP2.gdb\Checklist' , 'Disp_Type')

print (DispTypeValues)

#Create the unique list of Disposition Types in the input shapefile
def UDTV(shp , Disp_Type):
    with arcpy.da.SearchCursor(shp, [Disp_Type]) as cursor:
        return sorted({row[0] for row in cursor})
    
UniqueDispTypeValues = UDTV(shp , 'Disp_Type')

print (UniqueDispTypeValues)

#Create the unique list of Rel_Unq_ID in the input shapefile
def RUID(shp , Rel_Unq_ID):
    with arcpy.da.SearchCursor(shp, [Rel_Unq_ID]) as cursor:
        return sorted({row[0] for row in cursor})
    
RelUnqID = UDTV(shp , 'Rel_Unq_ID')

print (RelUnqID)

length = len(UniqueDispTypeValues)

print (length)

for value in UniqueDispTypeValues:
    if value == 'RTF' and length == 1:
        print "Rel_Unq_ID must be 0"
    else:
        print "Rel_Unq_ID must be non zero"
2
  • I want a warning or message if the condition isn't met and then a print of the invalid row. In this example everything passes , however if one of the Rel_Unq_ID values was non-zero I would want the script to warn me and list the invalid row – AJC Dec 1 '20 at 17:55
  • It would be helpful to provide a clearly defined question. – Aaron Dec 1 '20 at 22:07
1

For starters, you don't need to declare a function for each attempt to get unique values. See below for an example of how to create a single re-usable function. This makes your code easier to maintain and read.

Second - you can use that same function to pull the object ids of rows that meet whatever criteria you determine is a fail. In the example, I've just pulled rows with a Rel_Unq_Id < 0. The 'criteria' parameter in my GetUniqueValues function is optional - if you don't specify it, you'll get values from all records in the shapefile. If you do specify it, you'll only get values from rows that match the criteria.

# Importing necessary modules
import arcpy, os

arcpy.env.overwriteOutput = True

def GetUniqueValues(shp , field, criteria = None):
    with arcpy.da.SearchCursor(shp, [field], criteria) as cursor:
        return sorted({row[0] for row in cursor})

# Setting path to mxd and Data Frame
mxd = arcpy.mapping.MapDocument(r"CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]

# Assign the OneSTOP shapefile to a variable
shp = r"P:\JOBS\2020\20-1095\Outgoing_Information\OneSTOP\Disposition.shp"

#Create the unique list of allowable Disposition Types   
DispTypeValues = GetUniqueValues(r'Q:\GIS\OneSTOP\OneSTOP2.gdb\Checklist' , 'Disp_Type')

print (DispTypeValues)

#Create the unique list of Disposition Types in the input shapefile   
UniqueDispTypeValues = GetUniqueValues(shp , 'Disp_Type')

print (UniqueDispTypeValues)

#Create the unique list of Rel_Unq_ID in the input shapefile
RelUnqID = GetUniqueValues(shp , 'Rel_Unq_ID')

print (RelUnqID)

length = len(UniqueDispTypeValues)

print (length)

for value in RelUnqID:
    if value < 0:
        ids = GetUniqueValues(shp, 'OBJECTID', 'Rel_Unq_ID = ' + str(value))
        print ("Rows with Rel_Unq_ID of " + str(value))
        print (ids)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.