0

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[0]' # Query used to select current zip
    selection = arcpy.SelectLayerByAttribute_management(fc, 'NEW_SELECTION', query) # Selection of current zip
    arcpy.CreateRandomPoints_management(p, row[0], selection,'', row[1]) #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[0], fc, '#', row[1]) #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.

  • You mention that your code throws errors. Could you please edit your post to include the errors you are receiving? – Fezter Jan 7 at 22:42
  • Do you want all the points in one feature class, or a new feature class for points within each zip code? – Emil Brundage Jan 8 at 0:21
1

arcpy.CreateRandomPoints_management has an input parameter number_of_points_or_field. Input the field name containing your point count for this parameter.

Example:

arcpy.CreateRandomPoints_management (r"OutPath", "OutName", "PolygonFeatureClass", "", "NumberField")

enter image description here

Results:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.