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. 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.
Note:
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 table 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 the 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 an 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
feature_set = arcpy.FeatureSet()
Construct a feature set from an input feature class.
import arcpy
# Construct a FeatureSet from a feature class
feature_set = 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
in_recordset = arcpy.GetParameterValue("bufferpoints", 0)
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
in_recordset = arcpy.GetParameter(0)
The Result class getInput and getOutput methods
When using a server tool, 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.
import time
import arcpy
# 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'
in_featureset = arcpy.GetParameterValue("bufferpoints", 0)
# Load a shapefile into the featureset
in_featureset.load("c:/base/roads.shp")
# Run a server tool named BufferPoints with featureset created above
result = arcpy.BufferPoints_servertools(in_featureset, "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
out_featureset = result[0]
out_featureset.save("c:/temp/base.gdb/towers_buffer")
Example: Loading data to a feature set using cursors and an in-memory feature class
import arcpy
arcpy.env.overwriteOutput = True
arcpy.ImportToolbox("http://flame7/arcgis/services;BufferByVal",
"servertools")
# List of coordinates
coordinates = [[-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
feature_class = arcpy.CreateFeatureclass_management(
"in_memory", "tempfc", "POINT")[0]
# Open an insert cursor
with arcpy.da.InsertCursor(feature_class, ["SHAPE@XY"]) as cursor:
# Iterate through list of coordinates and add to cursor
for (x, y) in coordinates:
cursor.insertRow([(x, y)])
# Create a FeatureSet object and load in_memory feature class
feature_set = arcpy.FeatureSet()
feature_set.load(feature_class)
results = arcpy.BufferPoints_servertools(feature_set)