I want to generate a certain number of random points in N number of zip codes. The number of points to be generated for each individual zip code is stored the attribute of the feature class of all state zip codes, field type = Long. There is also a field with a unique ID for each zip code. This unique ID is a 5 character string.
This is the python script I wrote to accomplish the task. The code throws multiple errors.
import arcpy # Import ArcPy site-package and os modules fc = r'C:\ArcData\2018Cases\ZCTA_Cases.shp' # Path to input feature class fields = ['id2','Num'] # Fields to be searched. id2 is zip code. Num is the number of points to be generated in the zip code p = r'C:\ArcData\2018Cases\\ZCTAScript_Results' # Output Path scur = arcpy.da.SearchCursor(fc,fields) # Create a searchcursor object, which consists of rows of data. Each row will be a tuple of (id2,Numcase) for row in scur: # Iterate scur query = ' "id2" = row' # Query used to select current zip selection = arcpy.SelectLayerByAttribute_management(fc, 'NEW_SELECTION', query) # Selection of current zip arcpy.CreateRandomPoints_management(p, row, selection,'', row) #Create random points for each # row in scur using id2 as output name and Num as number of points del scur # Delete the cursor to release schema locks
This code is based off of code posted by a previous user in response to a previous question that I asked on this issue that was placed on HOLD. The code the user kindly provided did not throw errors but it did not produce the desired result; it generated random points in the full feature class (all zip codes in the state) extent not in the extent of a given individual zip code. Here is that code:
import arcpy fc = r'path/to/feature/class' #path to input feature class fields = ["ID","PointNum"] #the fields you want to search p = r'output/path' #output folder or gdb scur = arcpy.da.SearchCursor(fc,fields) #create a searchcursor object, #which consists of rows of data. each row will be a tuple of (ID,PointNum). there will be N rows. for row in scur: #iterate scur arcpy.CreateRandomPoints_management(p, row, fc, '#', row) #create random points for each #row in scur using ID as output name and PointNum as number of points del scur #delete the cursor to release schema locks
My code tries to resolve the issue by selecting the individual zip code that is being read by the search cursor and setting the constraining extent of the random point tool to that zip code. I cannot figure out how to write the code to accomplish the desired task.