-2

I have a very basic knowledge in Python and arcpy. I want to create a stratified random sampling point on a continuous polygon. So far, I have dissolved the polygon (according to categories) and calculated their percent area; multiplied the extracted area by total number of points, eg 3000. This gives me number of points in each polygon, according to the proportion of area of each polygon. So far I have [FID, categories, PolygonArea, PercentArea, NumberPoints]. Secondly, I want to use "Create random points" tool on each row (categories) to create points (NumberPoints) shapefiles for each row (to be stored in a scratch workspace). And thirdly, I want to merge all the point shapefiles to one point shapefile.

To accomplish this, I am currently at my second step- trying to extract the parameters (such as constraining feature class, constraining extent etc.) of "Create random points too" by using arcpy.da.SearchCursor on list of fields ["FID", "Categories", "PolygonArea", "PercentArea", "NumberPoints"]. But I am failing to extract.

I was wondering, could I even do so? The error I am getting is-

RuntimeError: Too few parameters Expected 1.

Here is loop of the code:

with arcpy.da.SearchCursor(fc, ["FID", "Categories" "Polygon_Area", "Percent_Area", "Number_point"]) as cursor:
    for row in cursor:
        constrinaing_extent = row[2]
        number_points = row[4]
        arcpy.CreateRandomPoints_management("C:/Users/dguru/Desktop/Project/scratch","samplepoints.shp", constraining_extent, number_points)
del row
del cursor

I followed the answer syntax. But my problem and the error remains the same. To check where it is creating problem, I inserted print statement and changed the number of field to just one; it works fine and prints the polygon area for each row. But gives the same error if I want to extract multiple parameters(Polygon_Area= Fcextent; and number_points). So, the error under message section is displayed for the line where loop starts-"for row in cursor" (displaying the same error I mentioned), when I put more fields. Therefore, I am not able to extract the multiple parameters for tool.

Here is the code that worked extracting just one parameter by putting just one field); The #ed lines in the code are inactive, as I was checking where the code went wrong.

with arcpy.da.SearchCursor(fc, ["Polygon_Area"]) as cursor:
 for row in cursor:
    fc_extent = row[0]
    #number_points = row[4]
    print fc_extent
    #arcpy.CreateRandomPoints_management("C:/Users/nim/Downloads/pr.mdb", "randompoint", "", fc_extent, number_points)
del row
del cursor
  • 2
    The with obviates the need for del cursor. s a great deal of mystery in the execution of your code. Printing the contents of the variables before you use the and including the output in the question would be useful. – Vince Dec 2 '18 at 19:09
1

The logic of reading using a cursor and passing in values to a tool is sound but your problems stem from not reading the help file syntax section and misspelling a variable, for a start you set constrinaing_extent and then use constraining_extent in the tool...

Now look at the Syntax section of the tool. What is the third parameter? What have you used as the third parameter? All because it is optional does not mean you can chuck in variables without due consideration of what the tool is going to read in parameter order. You either use keywords to specify which parameter you are setting, or you can use "#" as a default place holder for an optional parameter.

Now keep scrolling down that help file! You will find a code section, look at this it will show you exactly how to use the tool in python and is giving you 3 examples. Note how optional parameters are being skipped with "", so parameter order is maintained.

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.