How to use feature sets and record sets
FeatureSet objects are lightweight representations of a feature class. They are a special data element that contains not only schema (geometry type, fields, spatial reference) but also the data, including the geometry itself. RecordSet objects are similar but comparable to a table. When used in a script tool, feature sets and record sets can be used to interactively define features and records.
Server tools communicate using feature sets and record sets, meaning data must be created using or loaded into these objects when using server tools.
The FeatureSet and RecordSet classes have the same two methods.
Property |
Explanation |
---|---|
load |
Import a feature class into the FeatureSet object. |
save |
Export to a geodatabase feature class or shapefile. |
Property |
Explanation |
---|---|
load |
Import a feature class into the RecordSet object. |
save |
Export to a geodatabase table or dBASE file. |
Creating and using FeatureSet and RecordSet objects
FeatureSet and RecordSet objects can be created in a number of ways depending on need and application. The load method can be used to add new features or rows to the object, and the save method can be used to preserve the features or rows on disk. In addition, the input feature class or table can also be supplied as argument to the class. Both FeatureSet and RecordSet objects can also be used directly as input to a geoprocessing tool.
Create an empty feature set.
import arcpy # Create an empty FeatureSet object # featSet = arcpy.FeatureSet()
Construct a feature set from an input feature class
import arcpy # Construct a FeatureSet from a feature class # featSet = arcpy.FeatureSet("c:/base/roads.shp")
GetParameterValue function
If you want to create a feature set or record set with the specific schema of a tool's input, use GetParameterValue() to create an empty FeatureSet or RecordSet object with the appropriate schema.
import arcpy # Add a custom server toolbox # arcpy.ImportToolbox("http://flame7/arcgis/services;BufferByVal", "servertools") # Get the default input from a tool # inRecSet = arcpy.GetParameterValue("bufferpoints", 0)
Learn more about the GetParameterValue function
Using the GetParameter function
When working with script tools, FeatureSet and RecordSet objects can be acquired from the tool using the GetParameter function.
import arcpy # Get the RecordSet from a script tool # inRecSet = arcpy.GetParameter(0)
Learn more about the GetParameter function
Result class' getInput/getOutput methods
When using a server tool, you must explicitly ask for its output. When the output is a feature set or record set, the Result class' getOutput() method can be used to return the tool's output in a FeatureSet or RecordSet object. As well, the Result object's getInput method can be used to get an input FeatureSet or RecordSet object.
Learn more about getting results from a geoprocessing tool
import arcpy import time # Add a toolbox from a server # arcpy.ImportToolbox("http://flame7/arcgis/services;GP/BufferByVal", "servertools") # Use GetParameterValue to get a featureset object with the default schema of the # first parameter of the tool 'bufferpoints' # inFeatureSet = arcpy.GetParameterValue("bufferpoints", 0) # Load a shapefile into the featureset # inFeatureSet.load("c:/base/roads.shp") # Run a server tool named BufferPoints with featureset created above # result = arcpy.BufferPoints_servertools(inFeatureSet, "5 feet") # Check the status of the result object every 0.2 seconds until it has a value # of 4 (succeeded) or greater # while result.status < 4: time.sleep(0.2) # Get the output FeatureSet back from the server and save to a local geodatabase # outFeatSet = result.getOutput(0) outFeatSet.save("c:/temp/base.gdb/towers_buffer")
Example: Loading data to a feature set using cursors and an in-memory feature class
import arcpy from arcpy import env env.overwriteOutput = True arcpy.ImportToolbox("http://flame7/arcgis/services;BufferByVal", "server") # List of coordinates # coordinateL = ["-117.196717216;34.046944853","-117.186226483;34.046498438",\ "-117.179530271;34.038016569","-117.187454122;34.039132605",\ "-117.177744614;34.056765964","-117.156205131;34.064466609",\ "-117.145491191;34.068261129","-117.170825195;34.073618099",\ "-117.186784501;34.068149525","-117.158325598;34.03489167"] # Create an in_memory feature class to initially contain the coordinate pairs # fc = arcpy.CreateFeatureClass_management("in_memory", "tempfc", "POINT") # Open an insert cursor # cur = arcpy.InsertCursor(fc) pointArray = arcpy.Array() pnt = arcpy.Point() # Iterate through list of coordinates and add to cursor # for coords in coordinateL: x,y = coords.split(';') pnt.ID = coordinateL.index(coords) + 1 pnt.X = x pnt.Y = y feat = cur.newRow() feat.shape = pnt cur.InsertRow(feat) pointArray.add(pnt) # Delete the cursor del cur # Create a FeatureSet object and load in_memory feature class # featSet = arcpy.FeatureSet() featSet.load(fc) results = arcpy.BufferPoints_servertools(featSet)