POST
|
Wow, thanks everyone for your responses! So far I've only had a chance to try Ted's code. Ted, for some reason, it didn't work, but after seeing what you did, which made a lot of sense to me (my background is also in VB), I simply replaced this line from the original code: numValues = int(round(totpnts * float(inpct) / 100.0)) with this: numValues = int(inpct) Which seems to work. Thanks everyone else for your scripts, too; going through each of them, figuring out what you've done, and then tinkering with them will be very helpful as I try to familiarize myself with Python. Much appreciated! -Jason
... View more
02-16-2015
06:03 AM
|
0
|
1
|
1143
|
POST
|
Hello, Someone wrote the code below which works wonderfully to select a given percentage of polygons. I have about 1 hour of experience with python, but would like to alter the code so that it selects a number of polygons (opposed to a percentage). In particular, i would like it to allow me to select just one polygon at random. Thanks in advance for your help. # Given an input layer, return an output *.lyr layer file containing a random # selection of features. # # Original author: Leah Saunders, ESRI Inc, on Arcscripts.com. July 2007 # Major modification by Stephen Lead, 21st Feb 2008 import os, sys, random, arcgisscripting try: gp = arcgisscripting.create() gp.overwriteoutput = 1 # Specify input featureclass, output *.lyr file and the percentage of # random points to return. Set these parameters in ArcToolbox as shown. inputFC = sys.argv[1] # Feature Class or Feature Layer outputLyr = sys.argv[2] # Layer File inpct = sys.argv[3] # Long # Ensure that the input percentage is between 1 and 100% inpct = min(int(inpct),100) inpct = max(int(inpct),1) # Work out how many features to select inputDirname = os.path.dirname(inputFC) inputBasename = os.path.basename(inputFC) gp.workspace = inputDirname desc = gp.describe(inputFC) totpnts = gp.getcount(inputFC) numValues = int(round(totpnts * float(inpct) / 100.0)) gp.addmessage("Selecting " + str(numValues) + " random features") # Generate a list of all features, and select randomly from this inList = [] randomList = [] fldname = desc.OIDFieldName rows = gp.SearchCursor(inputFC) row = rows.next() gp.addmessage ("Loading all IDs into a list") while row: id = row.GetValue(fldname) inList.append(id) row = rows.next() selpnts = 0 gp.addmessage("Creating the list of randomly selected features") while len(randomList) < numValues: selpnts += 1 selItem = random.choice(inList) randomList.append(selItem) inList.remove(selItem) # Select features whose OID value occurs in the random list, generate # a *.lyr file from this selection. (Leading and trailing [ and ] marks # need to be removed from the list object) theLen = len(str(randomList)) sqlexp = '"' + fldname + '"' + " in " + "(" + str(randomList)[1:theLen - 1] + ")" selectionLyr = inputBasename + " selection" gp.MakeFeatureLayer_management(inputFC, selectionLyr, sqlexp) gp.SaveToLayerFile_management(selectionLyr, outputLyr) gp.addmessage("\nOutput layer " + outputLyr + " contains features randomly selected from " + inputBasename + "\n") except: gp.adderror("Error running script. Try specifying the full path to the input layer") # END OF FILE
... View more
02-13-2015
12:12 PM
|
0
|
8
|
5414
|
Online Status |
Offline
|
Date Last Visited |
11-11-2020
02:25 AM
|