'''Selects a random number of records from a feature class and populates a user-defined field with either a 1 or a 0 based on the input parameters. For example, if percent_to_include is set to 5, then a random ~5 percent of the records will be asigned a value of 1, the rest a value of 0. ''' try: import operator, os, random, traceback, sys, arcpy ##Path to the feature class. fc = r"Z:\GISpublic\GerryG\RestorationandMitigationConcept\20110425IvyPlotMonitoring\transects.shp" ##The attribute to populate with 1 or 0... attribute = r"issample" ##The percent threshold to include as a random selection. percent_to_include = 5 ##INSERT YOUR SELECT BY ATTRIBIUTE HERE uc = arcpy.UpdateCursor(fc) for row in uc: x = random.random() * 100 #arcpy.AddMessage(x) if x <= percent_to_include: row.setValue(attribute, 1) else: row.setValue(attribute, 0) uc.updateRow(row) print "Done" except: tb = sys.exc_info()[2] tbinfo = traceback.format_tb(tb)[0] pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + "\nError Info:\n" + str(sys.exc_info()[1]) print pymsg + "\n"
Los miembros registrados pueden publicar, seguir actualizaciones y más. ¿Nuevo aquí? Regístrate gratis.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.