Count number of vertices in all Feature Classes

I needed to get the number of points it takes to build all the lines and polygons in our admin boundaries. Here’s the python script I wrote to accomplish that.

Run in a shell as it doesn’t use AddMessages() yet. Table formatting is naive and needs improvement. Best solution might be to use the `tabulate` module, but I was unsure about introducing a new dependency. Total dev time including research, writing, testing, cleaning up for general consumption, and writing this post: 4.5 hours.

count-vertices.py

import os
import arcpy

workspace = arcpy.GetParameterAsText(0)
if not workspace:
    workspace = r'Z:\V5\ENV_250k.gdb\admin_env'

def count_vertices(fc, table):
    '''Count vertices in Feature Class, insert to dictionary named "table"'''
    # Adapted from Alex Tereshenkov (@alex-tereshenkov)
    # https://gis.stackexchange.com/questions/84796/extracting-number-of-vertices-in-each-polygon
    features = [feature[0] for feature in arcpy.da.SearchCursor(fc,"SHAPE@")]
    count_vertices = sum([f.pointCount-f.partCount for f in features])
    table[fc] = count_vertices

def print_report(table):
    '''Print dictionary as table
    (Naive, paths longer than X characters mess up the table)'''
    print "{:60}\t:\t{:>12}".format('-' * 60, '-' * 12)
    print "{:60}\t:\t{:>12}".format('Feature Class', 'Vertices')
    print "{:60}\t:\t{:>12}".format('-' * 60, '-' * 12)
    for k,v in table.items():
        print "{:60}\t:\t{:>12,}".format(k,v)    

def get_feature_classes(workspace):
    '''Return list of all feature classes under Workspace (recursive)'''
    # https://gis.stackexchange.com/questions/5893/listing-all-feature-classes-in-file-geodatabase-including-within-feature-datase
    feature_classes = []
    walk = arcpy.da.Walk(workspace, datatype="FeatureClass")
    print 'Finding feature classes in', workspace
    for dirpath, dirnames, filenames in walk:
        for filename in filenames:
            feature_classes.append(os.path.join(dirpath, filename))
    return feature_classes

table = {}
for fc in get_feature_classes(workspace):
    print 'Counting', fc
    count_vertices(fc,table)

print_report(table)<span style="display: inline-block; width: 0px; overflow: hidden; line-height: 0;" data-mce-type="bookmark" class="mce_SELRES_start"></span>

Output

Finding feature classes in Z:\V5\ENV_250k.gdb\admin_env
Counting Z:\V5\ENV_250k.gdb\admin_env\CO_Districts
Counting Z:\V5\ENV_250k.gdb\admin_env\Bison_Management_Area
Counting Z:\V5\ENV_250k.gdb\admin_env\GMA_Deactivated
Counting Z:\V5\ENV_250k.gdb\admin_env\GMA_Red_Mtn_Subdiv
Counting Z:\V5\ENV_250k.gdb\admin_env\Game_Management_Subzones
Counting Z:\V5\ENV_250k.gdb\admin_env\Game_Management_Zones
Counting Z:\V5\ENV_250k.gdb\admin_env\Traplines_Group
Counting Z:\V5\ENV_250k.gdb\admin_env\Traplines_Single
Counting Z:\V5\ENV_250k.gdb\admin_env\Regional_Management_Areas
Counting Z:\V5\ENV_250k.gdb\admin_env\Outfitting_Concessions
Counting Z:\V5\ENV_250k.gdb\admin_env\Elk_Hunt_Areas
Counting Z:\V5\ENV_250k.gdb\admin_env\Trapping_Concessions
------------------------------------------------------------	:	------------
Feature Class                                               	:	    Vertices
------------------------------------------------------------	:	------------
Z:\V5\ENV_250k.gdb\admin_env\Game_Management_Zones          	:	      49,773
Z:\V5\ENV_250k.gdb\admin_env\Elk_Hunt_Areas                 	:	       6,524
Z:\V5\ENV_250k.gdb\admin_env\GMA_Deactivated                	:	      15,236
Z:\V5\ENV_250k.gdb\admin_env\Outfitting_Concessions         	:	      76,523
Z:\V5\ENV_250k.gdb\admin_env\Regional_Management_Areas      	:	      29,161
Z:\V5\ENV_250k.gdb\admin_env\Traplines_Group                	:	      47,973
Z:\V5\ENV_250k.gdb\admin_env\CO_Districts                   	:	      73,692
Z:\V5\ENV_250k.gdb\admin_env\Bison_Management_Area          	:	       6,335
Z:\V5\ENV_250k.gdb\admin_env\GMA_Red_Mtn_Subdiv             	:	      12,769
Z:\V5\ENV_250k.gdb\admin_env\Game_Management_Subzones       	:	     527,229
Z:\V5\ENV_250k.gdb\admin_env\Traplines_Single               	:	     415,566
Z:\V5\ENV_250k.gdb\admin_env\Trapping_Concessions           	:	     453,457

Sidebar: I developed and tested this in the Pyzo development environment. I’m impressed with how straightforward it was to get started and how smooth the minute by minute experience is. I really like how easy it is to switch between python versions. No problem at all jumping between ArcMap’s 2x and ArcPro’s 3x python installs in the same session and without a restart. It even stayed up when I crashed the python kernel (needed to start a new shell though). I’ve think I’ve found a replacement for PyScripter, whose development has been getting slow and creaky.

Leave a Reply

Your email address will not be published.