フィーチャ セットおよびレコード セットを使用する方法
FeatureSet オブジェクトは、フィーチャクラスの軽量な表現です。これらは、スキーマ(ジオメトリ タイプ、フィールド、空間参照)のみでなく、データ(ジオメトリ自体を含む)も含んだ特殊なデータ エレメントです。RecordSet オブジェクトも同様のものですが、これはテーブルと似ています。スクリプト ツール内で使用する場合は、フィーチャ セットとレコード セットを使用してフィーチャとレコードをインタラクティブに定義できます。
注意:サーバ ツールはフィーチャ セットとレコード セットを使用してやり取りを行います。つまり、サーバ ツールを使用する際には、これらのオブジェクトを使用してデータを作成したり、これらのオブジェクトにデータをロードしたりする必要があります。
FeatureSet クラスと RecordSet クラスには、同じ 2 つのメソッドがあります。
|
プロパティ |
説明 |
|---|---|
|
load |
フィーチャクラスを FeatureSet オブジェクトにインポートします。 |
|
save |
ジオデータベース フィーチャクラスまたはシェープファイルにエクスポートします。 |
|
プロパティ |
説明 |
|---|---|
|
load |
フィーチャクラスを RecordSet オブジェクトにインポートします。 |
|
save |
ジオデータベース テーブルまたは dBASE ファイルにエクスポートします。 |
FeatureSet オブジェクトおよび RecordSet オブジェクトの作成と使用
FeatureSet オブジェクトと RecordSet オブジェクトは、必要性と用途に応じていくつかの方法で作成できます。load メソッドを使用して新しいフィーチャまたは行をオブジェクトに追加でき、save メソッドを使用してフィーチャまたは行をディスクに保存できます。また、入力フィーチャクラスまたはテーブルをクラスの引数として指定することもできます。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)
GetParameter 関数の使用
スクリプト ツールを操作する際には、GetParameter 関数を使用して、FeatureSet オブジェクトと RecordSet オブジェクトをツールから取得できます。
import arcpy # Get the RecordSet from a script tool # inRecSet = arcpy.GetParameter(0)
結果クラスの getInput/getOutput メソッド
サーバ ツールを使用する際には、その出力を明示的に要求する必要があります。出力がフィーチャ セットまたはレコード セットである場合は、Result クラスの getOutput() メソッドを使用して、ツールの出力を FeatureSet オブジェクトまたは RecordSet オブジェクトに返すことができます。同様に、Result オブジェクトの 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)