A FeatureSet object is a lightweight representation of a feature class. A FeatureSet object is a special data element that contains not only schema (geometry type, fields, spatial reference) but also the data, including the geometry. A RecordSet object is similar but is comparable to a table. When used in a script tool, feature sets and record sets can be used to interactively define features and records.
Remarque :
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
Both FeatureSet and RecordSet objects can be used directly as input to a geoprocessing tool. The objects can be created in a number of ways depending on the need and application; from arguments supplied to the class constructor or from the various means described in the examples below. The load method can be used to load a different set of features or rows to the object, and the save method can be used to preserve the features or rows on disk. Additionally, the class constructors and the class load methods accept various data sources as described in the FeatureSet and RecordSet topics.
Create an empty FeatureSet object.
feature_set = arcpy.FeatureSet()
Create a FeatureSet object from an input feature class.
feature_set = arcpy.FeatureSet("c:/base/roads.shp")
Load a set of features using an SQL expression into a FeatureSet object.
feature_set.load("c:/base/roads.shp", "CITY = 'Redlands'")
GetParameterValue
To create a FeatureSet or RecordSet object with the specific schema of a tool's input, use the GetParameterValue function 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)
GetParameter
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 FeatureSet or RecordSet object, the Result object getOutput method can be used to return the tool's output in a FeatureSet or RecordSet object. Additionally, 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_class)
results = arcpy.BufferPoints_servertools(feature_set)
Vous avez un commentaire à formuler concernant cette rubrique ?