Python scripting and creating random points

09-10-2010 04:24 PM
New Contributor III
I have created a model in model builder that takes a data set and then completes a select by attributes, buffer, and create random points geoprocesses (in that order).  This works.  When I export the model to a python script and run, Arc crashes when it gets to the create random points step.  Has anyone else had this issue?  Is there a work around?

# ---------------------------------------------------------------------------
# Created on: 2010-09-10 17:26:43.00000
#   (generated by ArcGIS/ModelBuilder)
# Usage: Location_Error_Arc10 <Distance__value_or_field_> <Dg_ysf> 
# Description: 
# ---------------------------------------------------------------------------

# Import arcpy module
import arcpy

arcpy.env.overwriteOutput = True

# Script arguments
Distance__value_or_field_ = arcpy.GetParameterAsText(0)
Dg_ysf = arcpy.GetParameterAsText(1)

# Local variables:
Output_Location = "C:\\Alamo_Project\\Alamo_Breccia.mdb\\Measured_Sections\\"
Temp_Ysf = "C:\\Alamo_Project\\Alamo_Breccia.mdb\\Measured_Sections\\Temp_Ysf"
Dg_ysf_Buffer = "C:\\Alamo_Project\\Alamo_Breccia.mdb\\Measured_Sections\\Dg_ysf_Buffer"
Dg_ysf_Random__2_ = "C:\\Alamo_Project\\Alamo_Breccia.mdb\\Measured_Sections\\Dg_ysf_Random"
Dg_ysf_Random__3_ = "C:\\Alamo_Project\\Alamo_Breccia.mdb\\Measured_Sections\\Dg_ysf_Random"

# Process: Select Layer By Attribute (6)
arcpy.SelectLayerByAttribute_management(Dg_ysf, "NEW_SELECTION", "[Min_Thick] = '2'")

# Process: Copy Features
arcpy.CopyFeatures_management(Dg_ysf, Temp_Ysf, "", "0", "0", "0")

# Process: Buffer
arcpy.Buffer_analysis(Temp_Ysf, Dg_ysf_Buffer, Distance__value_or_field_, "FULL", "ROUND", "NONE", "")

# Process: Create Random Points
arcpy.CreateRandomPoints_management(Output_Location, "Dg_ysf_Random", Dg_ysf_Buffer, "0 0 250 250", "1", "0 Meters", "POINT", "0")
0 Kudos
6 Replies
MVP Esteemed Contributor
from the arcmap 10 help files, the example shows that the number of points and the minimum distance are numbers, in your example you are passing strings, alternately try assigning a variable the values and pass that to the method.  See this link for other examples
outName = "randomMPs"
fcExtent = "C:/data/county.gdb/county"
numPoints = 100
numMP = 10
arcpy.CreateRandomPoints_management(outGDB, outName, "", fcExtent, numPoints, "", "MULTIPOINT", numMP)
0 Kudos
New Contributor III
CreateRandomPoints appears to be bugged when called from a toolbox (i.e. importing a python script to a Toolbox). The function runs properly when called directly from the script, but it will completely crash Arc (SOC if published, Catalog/Map if local) when called as a tool. It works in modelbuilder, but not in python. I've tested about every permutation of variables and they all crash. What gives?

import arcpy
minDist="1000 Meters"
arcpy.CreateRandomPoints_management(in_memory, "testfile", "", "", numPoints, minDist,"POINT","0")

I've tried changing numPoints to an int, the output location, the output name, the constraining FC and/or extent, all to no avail. This is pretty frustrating, as these unhandled errors crashing Arc are really poor reflections of the engine. Any insight would be extremely appreciated.
0 Kudos
New Contributor
I am experiencing the same problem. Python script works fine when called from Pythonwin but fails when added as a script in the arc toolbox. Has there been any resolutions or workarounds?
0 Kudos
New Contributor

This is a known problem that esri has acknowledged. Create random points in a python script added as a tool in a toolbox will not work and will crash the application every time. However, if you install Service Pack 2 for ArcGIS Desktop, the problem has been resolved.
0 Kudos
New Contributor
For me, the problem persists with ArcGIS Desktop Service Pack 2 (build 3200).
When running "outside" of ArcMap, it works fine.  When running from a toolbox, ArcGIS crashes.
0 Kudos
New Contributor III
Yes - in my experience this tool is buggy when used in a stand-alone python script.  I've had inconsistent results (in an iterative model, used for parameter estimation by monte carlo method) with this tool.  I've found that replacing the variable containing the string for the actual string improves reliablity, as does reading/writing from drive as opposed to the in-memory workspace. 
The 'explicit variable' workaround is a pain; I have to remember to adjust this path on line 1xx rather than just at the top, in the variable declaration block.
0 Kudos