Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@maptastik
Created December 18, 2018 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maptastik/4e0be68d936a6d6531cdc85da8347181 to your computer and use it in GitHub Desktop.
Save maptastik/4e0be68d936a6d6531cdc85da8347181 to your computer and use it in GitHub Desktop.
import arcpy
import numpy as np
def randomFCSample(fc, fd='sample_fd', sample_field='OBJECTID', sample_pct=10):
"""Return a randomish sample of a feature class as a feature layer
Parameters:
fc (str): Input feature class to sample
fd (str): Name of output feature layer
sample_field (int): Numeric field to sample values from
sample_pct (float): Percentage of feature class to sample
Returns:
feature layer sample of input feature class
"""
count_class = arcpy.GetCount_management(fc)
count = int(count_class[0])
random_vals = np.random.choice(count, int(count*(sample_pct/100)))
arcpy.MakeFeatureLayer_management(fc,
fd + "_" + str(sample_pct) + 'pct',
sample_field + ' IN ' + str(tuple(random_vals))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment