Basic python in ArcGIS, need help!

3096
9
Jump to solution
04-21-2014 03:54 PM
JacobWagner
New Contributor
Okay, so i had to take this copy of the random_sample.py script and call it
random_percent.py.

Now im being asked to modify the script so that the third parameter
is a percentage of the number of input records as an integer between
1 and 100.

And then modify the script tool settings so that the input for this parameter is validated on the tool dialog box.

I'm still relatively new to python so it all sounds like jargon to me and after trying multiple times to figure it out i still can't find the proper code to use.

I'm using python in ArcMap, and have read the chapter in pyrhon scripting for ArcGIS and am still stumped. How do i do this?
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JoshuaChisholm
Occasional Contributor III

Hello Jacob,

Python is awesome. It can be tricky to learn, but it's great once you do. In the future, it would be a lot easier if you posted the python code as text in your posting (see here for instructions).

I've made your requested changes to the script and tried to add lots of comments (text after "#" are comments) to help you understand whats going on:

#import modules you'll need in your script: 
import arcpy 
import random 
from arcpy import env  

#allow outputs to be overwritten 
#(so you don't have to delete them before each time you run  
env.overwriteoutput = True 

#set input variables: 
inputfc = "C:/EsriPress/Python/Date/Exercise13/points.shp" 
outputic = "C:/EsriPress/Python/Data/Exercise13/Results/random.shp" 
pctRecords = 50  

#empty lists for later use: 
inlist = [] 
randomlist = []  

#get the unique id field for the input feature class 
desc = arcpy.Describe(inputfc) 
fldname = desc.OIDFieldName  

#iterate through the input feature class (see below for an alternative to the while loop)

#get the rows for the input feature class (this is similar to the attribute table) 
rows = arcpy.SearchCursor(inputfc) 
row = rows.next() #get the next single row (in this case, it's the first row) 
count=0  #set a counter to 0 
#while row exists, do some stuff:
while row: 
    id = row.getValue(fldname) #get the id from the current row  
    inlist.append(id) #add that id to a list we created earlier  
    count+=1 #add one to our counter  
    row = rows.next() #switch to the next row  

#Now that we have the count we can set our outcount 
#first turn input percent to a deciman (example 0.5 instead of 50), 
#then times it by the count  
outcount = (pctRecords*0.01)*count  

while len(randomlist) < outcount: 
    #while the randomlist is smaller than the desired outcount:  
    selitem = random.choice(inlist) 
    #pick a random item from the list we created in the previous while loop  
    randomlist.append(selitem) #add that randomly picked id to the random list  
    inlist.remove(selitem) #remove that id from the list so it won't randomly get picked again  

#create a sql statement to do a select by attribute: 
sqlexp = '"' + fldname + '"' + ' in ' + '(' + str(randomlist)[1:-1] + ')'  
#make a selection by selecting all the id's in the random list from the original file 
arcpy.MakeFeatureLayer_management(inputfc, "selection", sqlexp)  
#turn that selection into a shapefile: 
arcpy.CopyFeatures_management("selection", outputfc)  
print "Script is complete!"
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

As an alternative to your first while loop, I would prefer to use a for loop (but either will work). This is what the for loop would loop like:
#iterate through the input feature class (alternative to the while loop) rows = arcpy.SearchCursor(inputfc) #get the rows for the input feature class (this is similar to the attribute table) count=0 #set a counter to 0 for row in rows: #goes through each row in rows and does stuff with that row  id = row.getValue(fldname) #get the id from the current row  inlist.append(id) #add that id to a list we created earlier  count+=1 #add one to our counter

Let me know if you have any questions. Good luck!

View solution in original post

9 Replies
JacobWagner
New Contributor
Sorry i have the code file attached on this part
0 Kudos
JoshuaChisholm
Occasional Contributor III

Hello Jacob,

Python is awesome. It can be tricky to learn, but it's great once you do. In the future, it would be a lot easier if you posted the python code as text in your posting (see here for instructions).

I've made your requested changes to the script and tried to add lots of comments (text after "#" are comments) to help you understand whats going on:

#import modules you'll need in your script: 
import arcpy 
import random 
from arcpy import env  

#allow outputs to be overwritten 
#(so you don't have to delete them before each time you run  
env.overwriteoutput = True 

#set input variables: 
inputfc = "C:/EsriPress/Python/Date/Exercise13/points.shp" 
outputic = "C:/EsriPress/Python/Data/Exercise13/Results/random.shp" 
pctRecords = 50  

#empty lists for later use: 
inlist = [] 
randomlist = []  

#get the unique id field for the input feature class 
desc = arcpy.Describe(inputfc) 
fldname = desc.OIDFieldName  

#iterate through the input feature class (see below for an alternative to the while loop)

#get the rows for the input feature class (this is similar to the attribute table) 
rows = arcpy.SearchCursor(inputfc) 
row = rows.next() #get the next single row (in this case, it's the first row) 
count=0  #set a counter to 0 
#while row exists, do some stuff:
while row: 
    id = row.getValue(fldname) #get the id from the current row  
    inlist.append(id) #add that id to a list we created earlier  
    count+=1 #add one to our counter  
    row = rows.next() #switch to the next row  

#Now that we have the count we can set our outcount 
#first turn input percent to a deciman (example 0.5 instead of 50), 
#then times it by the count  
outcount = (pctRecords*0.01)*count  

while len(randomlist) < outcount: 
    #while the randomlist is smaller than the desired outcount:  
    selitem = random.choice(inlist) 
    #pick a random item from the list we created in the previous while loop  
    randomlist.append(selitem) #add that randomly picked id to the random list  
    inlist.remove(selitem) #remove that id from the list so it won't randomly get picked again  

#create a sql statement to do a select by attribute: 
sqlexp = '"' + fldname + '"' + ' in ' + '(' + str(randomlist)[1:-1] + ')'  
#make a selection by selecting all the id's in the random list from the original file 
arcpy.MakeFeatureLayer_management(inputfc, "selection", sqlexp)  
#turn that selection into a shapefile: 
arcpy.CopyFeatures_management("selection", outputfc)  
print "Script is complete!"
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

As an alternative to your first while loop, I would prefer to use a for loop (but either will work). This is what the for loop would loop like:
#iterate through the input feature class (alternative to the while loop) rows = arcpy.SearchCursor(inputfc) #get the rows for the input feature class (this is similar to the attribute table) count=0 #set a counter to 0 for row in rows: #goes through each row in rows and does stuff with that row  id = row.getValue(fldname) #get the id from the current row  inlist.append(id) #add that id to a list we created earlier  count+=1 #add one to our counter

Let me know if you have any questions. Good luck!

JacobWagner
New Contributor
You are amazing, thank you so much. So far in this class it has been difficult because the prof expects us to learn withotu much guidance other than vague walkthroughs in the assigned labs that dont really show me how to do anything the question asks. The explanations on this was great i really appreciate you taking the time to help me learn this, a majority of the labs i have been searching ESRI extensively to find out what im trying to do is simple with the right codes -.- Its a graduate class offered to undergrads to help us land jobs after finishing school
0 Kudos
JacobWagner
New Contributor
How do i submit that you answered what i had asked to give you credit for it?
0 Kudos
JohnDye
Occasional Contributor III
How do i submit that you answered what i had asked to give you credit for it?


Hit the check mark on the right side of his post that answered your question.
0 Kudos
JacobWagner
New Contributor
done, and thank you
0 Kudos
JoshuaChisholm
Occasional Contributor III
Thanks Jacob (and John!),

I'm happy you were able to understand my comments. I had a great python prof, but I still spent a lot of my time lost, confused and searching google (this forum and stackoverflow.com are great resources). Ask again (on this posting or a new one) if you hit another brick wall.

PS. Good luck finding a job. I just repeatedly told python and GIS were a 'golden tickets' on my resume and it still took me a bit over a year. Don't get discouraged, but be prepared for a challenge.
0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

and to add to the comment about the forums, etc.  There are many threads here on GeoNet that list different resources for helping to learn Python.  I just did a search for "learn python" in Geonet. 

note: "API for Python" is different than Python.  The API is fairly new and would be good to learn, but would cause confusion when you are still learning Python.

Check out some of these threads for some tips on python:

Can anyone suggest a good learning python website? 

Best book for learning Python 

https://community.esri.com/message/85013 

/blogs/dan_patterson/2016/05/09/the-links?sr=search&searchId=63ade22a-5b81-4e38-a2a3-e434c9fd94ba&se...

(not in any particular order)

0 Kudos
UcheJames
New Contributor

Hi Jacob, 

Please, can you help me with the solution to this question. you can email me please, jucoy2k@hotmail.com

0 Kudos