如何使用要素集和记录集
FeatureSet 对象是要素类的轻量级表示。它们是一种既包含架构(几何类型、字段和空间参考)也包含数据(包括几何自身)的特殊数据元素。RecordSet 对象与 FeatureSet 对象类似,但其功能可与表相媲美。在脚本工具中使用时,要素集和记录集可用于以交互方式定义要素和记录。
注:服务器工具使用要素集和记录集进行通信,这意味着在使用服务器工具时必须使用这些对象来创建数据或将数据加载到这些对象中。
FeatureSet 和 RecordSet 类有相同的两种方法。
|
属性 |
说明 |
|---|---|
|
加载 |
将要素类导入到 FeatureSet 对象中。 |
|
保存 |
导出到地理数据库要素类或 shapefile。 |
|
属性 |
说明 |
|---|---|
|
加载 |
将要素类导入到 RecordSet 对象中。 |
|
保存 |
导出到地理数据库表或 dBASE 文件。 |
创建和使用 FeatureSet 和 RecordSet 对象
可根据需要和应用以多种方法创建 FeatureSet 和 RecordSet 对象。加载方法可用于将新要素或行添加到对象,而保存方法可用于将要素或行保留在磁盘中。此外,还可将输入要素类或表作为参数提供给类。FeatureSet 和 RecordSet 对象还可以直接作为地理处理工具的输入来使用。
创建空要素集。
import arcpy # Create an empty FeatureSet object # featSet = arcpy.FeatureSet()
根据输入要素类构造要素集
import arcpy
# Construct a FeatureSet from a feature class
#
featSet = arcpy.FeatureSet("c:/base/roads.shp")
GetParameterValue 函数
如果要创建具有工具输入特定架构的要素集或记录集,请使用 GetParameterValue() 创建具有相应架构的空 FeatureSet 或 RecordSet 对象。
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)
了解有关 GetParameterValue 函数的详细信息
使用 GetParameter 函数
使用脚本工具时,可使用 GetParameter 函数通过工具来获取 FeatureSet 和 RecordSet 对象。
import arcpy # Get the RecordSet from a script tool # inRecSet = arcpy.GetParameter(0)
结果类的 getInput/getOutput 方法
使用服务器工具时,必须显式请求其输出。当输出是要素集或记录集时,可使用结果类的 getOutput() 方法将以 FeatureSet 或 RecordSet 对象的形式返回工具的输出。还可以使用结果对象的 getInput 方法获取输入 FeatureSet 或 RecordSet 对象。
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")
示例:使用光标和内存中的要素类将数据加载到要素集
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)