1

This Validates that all Polygons have a map unit. verify that the absence of a Map unit is caught by the check... here we test for the empty string or the python "None" type. Verify that this is how an empty MapUnit will be reflected in the script. I would like the script to also message more than one object id if more is null or empty.

How would I go about that?

Right now it just prints out one record but I would like it to print out all that are empty or null.

with arcpy.da.SearchCursor(MapUnitPolys, ['SHAPE@', 'MapUnit', 'OBJECTID']) as cursor:
    for row in cursor:
        #arcpy.AddMessage(str(row[1]))
        # Does this Polygon have a map unit
        if row[1] == "" or row[1] is None or row[1] is 'NULL':
            arcpy.AddMessage('Polygon OBJECT ID:{} is missing map unit... exiting.'.format(row[2]))
            sys.exit(1)
3

sys.exit (1) ends the script. Get rid of this line.

with arcpy.da.SearchCursor(MapUnitPolys, ['SHAPE@', 'MapUnit', 'OBJECTID']) as cursor:
    for row in cursor:
        #arcpy.AddMessage(str(row[1]))
        # Does this Polygon have a map unit
        if row[1] == "" or row[1] is None or row[1] is 'NULL':
            arcpy.AddMessage('Polygon OBJECT ID:{} is missing map unit.'.format(row[2]))
  • That just keeps the script going if I get rid of that line. I am looking to just show all objectids that have a missing MapUnit. and I want the script to end if there are missing mapunits – Lbook Aug 6 at 16:12
  • 1
    @Lbook That's not what the question states. – Emil Brundage Aug 6 at 16:14
  • okay. But do you understand what I am looking for? – Lbook Aug 6 at 16:22
  • 1
    Create a variable set to False before your cursor. If you find a missing unit set the variable to True. Once your cursor is complete check if the variable returns True and if so exit. – Emil Brundage Aug 6 at 16:29
1

You'll need to create a variable to track your condition. In the examples, I used "failure", which is initialized to False--indicating that no rows are missing a map unit. Later, during the cursor, if any row is missing a map unit, you update the failure variable to True. Then, after your cursor iteration, you can check whether failure is True.

If you want each row with a missing map unit to have its own message, you'll need a boolean variable.

Here's how to do it with printing each row as its own message:

failure = False

with arcpy.da.SearchCursor(MapUnitPolys, ['SHAPE@', 'MapUnit', 'OBJECTID']) as cursor:
    for row in cursor:
        #arcpy.AddMessage(str(row[1]))
        # Does this Polygon have a map unit
        if row[1] == "" or row[1] is None or row[1] is 'NULL':
            arcpy.AddMessage('Polygon OBJECT ID:{} is missing map unit... exiting.'.format(row[2]))
            if not failure:
                failure = True

if failure:
    sys.exit(1)

If you want to print a single message that lists all OIDs with missing map units, then you can bypass the boolean, because whether your list is empty answers your question. In this case, the variable oids will store the OIDs of rows that are missing a map unit.

Here's how to do it with printing a single message listing all OIDs:

oids = []

with arcpy.da.SearchCursor(MapUnitPolys, ['SHAPE@', 'MapUnit', 'OBJECTID']) as cursor:
    for row in cursor:
        #arcpy.AddMessage(str(row[1]))
        # Does this Polygon have a map unit
        if row[1] == "" or row[1] is None or row[1] is 'NULL':
            oids.append(row[2])

if oids:
    arcpy.AddMessage('The following OBJECT IDs are missing map units: {}'.format(', '.join(oids)))
    sys.exit(1)

By the way, you can simplify this and speed it up by putting the condition as a where clause on the cursor:

cursor_where_clause = "MapUnit in ('', 'NULL') or MapUnit IS NULL"
with arcpy.da.SearchCursor(MapUnitPolys, 'OBJECTID', cursor_where_clause) as cursor:
    for row in cursor:
            oids.append(row[0])

...or even simply:

cursor_where_clause = "MapUnit in ('', 'NULL') or MapUnit IS NULL"
ids = [row[0] for row in arcpy.da.SearchCursor(MapUnitPolys, 'OBJECTID', cursor_where_clause)]

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.