Welcome, guest | Sign In | My Account | Store | Cart

Generates a randomized list of unique IDs and iteratively assigns values to features in a featureclass.

Python, 39 lines
 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
# Name:  FeatureClassRandomizer.py
# Date:  23 July 2012
# Developed by:  Reclamation PNGIS - Boise ID
# Description: Randomizes a featureclass by assigning random, unique IDs
# to a featureclass attribute table.  Allows performing random sample of
# the dataset.
#=======================================================================
#import modules
#-----------------------------------------------------------------------
import random
import arcpy
from arcpy import env
#-----------------------------------------------------------------------
# set variables
#-----------------------------------------------------------------------
inFC = r'C:\GIS_WRKSPC\surface.gdb\pntz_2011'
rFld = 'RID'
#-----------------------------------------------------------------------
# run with it...
#-----------------------------------------------------------------------
arcpy.AddField_management(inFC, rFld, "LONG")
f_cnt = int(arcpy.GetCount_management(inFC).getOutput(0)) + 1
print 'A unique ID is being randomly assigned to each row...'
rlist = range(1,f_cnt)
random.shuffle(rlist)
ir = iter(rlist)
while True:
    try:
        rval = ir.next()
        with arcpy.da.UpdateCursor(inFC, rFld) as rcur:
            for row in rcur:
                row[0] = rval
                rcur.updateRow(row)
                rval = ir.next()
    except StopIteration:
        break
print 'Randomization is complete.'
del rlist
#=======================================================================

Randomized assignment of unique IDs allows making a random sample through attribute selection on the ID field (RID). For example, "RID" <= 41061 will produce a random sample of points representing 10% of a point featureclass containing 410610 features.