2

I am trying to use the script from Randomly subsetting X% of selected points using ArcPy? to randomly select and export points in ArcGIS.

Specifically, I am using this:

def SelectRandomByCount (layer, count):
 import random
 layerCount = int (arcpy.GetCount_management (layer).getOutput (0))
 if layerCount < count:
 print "input count is greater than layer count"
 return
 oids = [oid for oid, in arcpy.da.SearchCursor (layer, "OID@")]
 oidFldName = arcpy.Describe (layer).OIDFieldName
 delimOidFld = arcpy.AddFieldDelimiters (layer, oidFldName)
 randOids = random.sample (oids, count)
 oidsStr = ", ".join (map (str, randOids))
 sql = "{0} IN ({1})".format (delimOidFld, oidsStr)
 arcpy.SelectLayerByAttribute_management (layer, "", sql)
 arcpy.CopyFeatures_management(layer, "random_start_20pop")
 arcpy.SelectLayerByAttribute_management(layer, "SWITCH_SELECTION")
 arcpy.CopyFeatures_management(layer, "endings_for_20pop")

What should I do to create Python tool in ArcToolbox instead of pasting it to the Python window? I've tried already to save it in toolbox as a script but I assume that I did it wrong. I tried to proceed as it is shown here Adding a script tool

Bera
81.7k14 gold badges84 silver badges198 bronze badges
asked Jul 4, 2017 at 8:26
3
  • 1
    "Why does it work only in python window in ArcMAP, but not as a saved tool in toolbox so I can't use it in ModelBuilder? I saved it, I can insert parameters, but than nothing happens." - What have you tried? Add more info Commented Jul 4, 2017 at 8:44
  • You defined a python function, which on itself does nothing. So you'll need additional code to get the parameters from your script tool (i assume you are using a script tool not a python toolbox). Read this, to get a running tool pro.arcgis.com/de/pro-app/arcpy/geoprocessing_and_python/… Commented Jul 4, 2017 at 8:44
  • @BERA - I used add script on arctoolbox and than set parameters. Commented Jul 4, 2017 at 13:50

2 Answers 2

1

To use this function inside a Python Toolbox in ArcMap:

  • In ArcCatalog, right click on a folder -> New -> Python Toolbox.
  • Right click on your newly created Python Toolbox -> Edit...
  • Put your funtion definition at the bottom of the file (watch the indentation so it is inside the class definition)
  • Define the toolbox parameters. (http://pro.arcgis.com/en/pro-app/arcpy/geoprocessing_and_python/defining-parameter-data-types-in-a-python-toolbox.htm. For example:

    def getParameterInfo(self):

    # Parameter: Input layer
    param0 = arcpy.Parameter(
     displayName="Input layer",
     name="input_folder",
     datatype="GPFeatureLayer",
     parameterType="Required",
     direction="Input")
    # Parameter: Output File Geodatabase
    param1 = arcpy.Parameter(
     displayName="Output File Geodatabase",
     name="output_location",
     datatype="DEWorkspace",
     parameterType="Required",
     direction="Input")
    param2 = arcpy.Parameter(
     displayName="blablabla",
     name="blablabla",
     datatype="GPLong",
     parameterType="Required",
     direction="Input")
    params = [param0, param1, param2]
    return params
    
  • Get the parameters and call the function. For example:

def execute(self, parameters, messages): inputlayer = parameters[0].valueAsText outputgdb = parameters[1].valueAsText number = parameters[2].valueAsText self.SelectRandomByCount(inputlayer, number, outputgdb)

Of course, you would have to modify it a bit to suite your needs. This code is mostly an example of how to proceed.

answered Jul 4, 2017 at 22:42
1

The current workspace will be the output geodatabase. For example the Default geodatabase. If you want you can add the output database as an input to the function:

def SelectRandomByCount (layer, Count, outputdb):
 import random, os
 layerCount = int (arcpy.GetCount_management (layer).getOutput (0))
 if layerCount < count:
 print "input count is greater than layer count"
 return
 oids = [oid for oid, in arcpy.da.SearchCursor (layer, "OID@")]
 oidFldName = arcpy.Describe (layer).OIDFieldName
 delimOidFld = arcpy.AddFieldDelimiters (layer, oidFldName)
 randOids = random.sample (oids, count)
 oidsStr = ", ".join (map (str, randOids))
 sql = "{0} IN ({1})".format (delimOidFld, oidsStr)
 arcpy.SelectLayerByAttribute_management (layer, "", sql)
 arcpy.CopyFeatures_management(layer, os.path.join(outputdb, "random_start_20pop"))
 arcpy.SelectLayerByAttribute_management(layer, "SWITCH_SELECTION")
 arcpy.CopyFeatures_management(layer, os.path.join(outputdb, "endings_for_20pop"))

Then use the function:

SelectRandomByCount("fc", 10, r"C:\output.gdb")
answered Jul 4, 2017 at 8:41
3
  • It occurred that in line 15 is mistake :/ "Parsing error SyntaxError: invalid syntax (line 15)" Commented Jul 4, 2017 at 13:59
  • Forgot two paranthesis, try again Commented Jul 4, 2017 at 14:01
  • Thx, but I just changed workspace because this code didn't worked anyway :* Commented Jul 4, 2017 at 19:27

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.