-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountIDs.py
More file actions
66 lines (51 loc) · 1.81 KB
/
CountIDs.py
File metadata and controls
66 lines (51 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
'''****************************************************************
CountIDs.py
Author(s): Anders Olson
Usage: Run script using python IDE or similar
Description:
Script traverses ID field and counts occurrence of each ID
****************************************************************'''
import arcpy
from collections import counter
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
#================================#
# Configure logger
#================================#
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
def outputMessage(msg):
print(msg)
arcpy.AddMessage(msg)
def outputError(msg):
print(msg)
arcpy.AddError(msg)
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
#================================#
# Define environment and messaging
#================================#
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
# Set the overwriteOutput ON
arcpy.gp.overwriteOutput = True
# Set workspace to be in memory for faster run time
arcpy.env.workspace = "in_memory"
outputMessage("Workspace is: {}".format(arcpy.env.workspace))
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
#================================#
# Define variables
#================================#
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
#Input shapefile or dbf with id field to count
inData = r"U:\Somedirectory\yourshapefile.shp"
# Empty list for storing all unique IDs
uniqueIDs = []
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
#================================#
# Call Functions
#================================#
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
# Populate a list of all IDs found in the desired ID field
for row in arcpy.da.SearchCursor(inData, ['<FIELD-NAME>']):
uniqueIDs.append(row[0])
# Print a list of all ids and no repeating ids i.e. 'keys'
print(Counter(uniqueIDs).keys())
# Print a count for occurrence of each id found in original list i.e. 'values'
print(Counter(uniqueIDs).values())