Python でのジオプロセシング サービスの使用

他のジオプロセシング ツールと同様に、ジオプロセシング サーバ ツールにはツールを実行するのに必要な情報を提供する固定のパラメータがあります。スクリプトで非同期サーバ ツールを使用する場合、ResultgetOutput メソッドで出力を取得できます。

ヒントヒント:

ツールが同期または非同期のどちらで実行されているか判別するには、IsSynchronous 関数を使用できます。ツールが同期型の場合、結果は自動的に返されますが、ツールが完了するまで他のアクションを行うことはできません。

次の例では、GetParameterValue 関数を使用してサーバ ツールから FeatureSet オブジェクトを取得しています。FeatureSet オブジェクトには、ツールの入力パラメータのスキーマが格納されています。その後、FeatureSet オブジェクトにフィーチャクラスがロードされ、サーバ上でサーバ ツールが実行されます。スクリプトの終了時には、結果オブジェクトの getOutput メソッドを使用してツールの出力を取得します。この出力は、FeatureSet の save メソッドを使用してローカルに保存されます。

import arcpy
import time

# Add a toolbox from a server
#
arcpy.ImportToolbox("http://flame7/arcgis/services;GP/BufferByVal", "mytools")

# 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_mytools(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")

サーバ ツールからのラスタ結果の取得

ラスタ形式の結果は TIFF(Tagged Image File Format)として返されます。デフォルトでは、getOutput を使用すると、TIFF はシステムの TEMP フォルダに書き込まれます。TIFF の場所を変更するには、scratchWorkspace 環境をいずれかのフォルダに設定します。

import arcpy 
import time

# Set the scratchworkspace to a folder.
#
arcpy.env.scratchWorkspace = "c:/temp/rasteroutput"

# Add a toolbox from a server
#
arcpy.ImportToolbox("http://flame7/arcgis/services;SlopeByVal", "mytools")

dem = "c:/dems/k_ne_g"

# Run a server tool named RunSlope
#
result = arcpy.RunSlope_mytools(dem)

# 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:
    print result.status
    time.sleep(0.2)

# Raster output will be written to the scratchworkspace    
#
outTIFF = result.getOutput(0)

マップ イメージの取得

ジオプロセシング サービスでは、タスク結果のデジタル マップ イメージを作成するために、結果を表示するマップ サービスを使用できます。デジタル マップには、情報を伝達する地理データセットのビジュアル表現が含まれています。デジタル マップは、Web 上で画像として(*.jpeg など)転送されます。マップ イメージには、人間が解釈できる情報がフィーチャクラスのフィーチャよりもはるかに多く含まれています。マップ イメージは、管理も簡単であり、簡単に圧縮したり、管理しやすいタイルに分割したりできるだけではなく、Web 上で転送および表示するための方法も確立されています。

マップ イメージは、ArcGIS for Server マップ サービスによって作成される、ArcMap ドキュメント(*.mxd)を公開したものです。マップ イメージにはこのような特性があるため、一般的には、ジオプロセシング タスクの結果に対してマップ イメージを 1 つ作成し、Web 上では結果として生成されたデータセットを転送する代わりにマップ イメージを転送するほうが適切です。ジオプロセシング サービスには、出力データのマップ イメージを作成するために ArcGIS for Server によって使用される結果マップ サービスを追加することができます。

import arcpy
import time
import urllib

# Add a toolbox from a server
#
arcpy.ImportToolbox("http://flame7/arcgis/services;GP/BufferByVal", "mytools")

# 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_mytools(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)
    print result.status

# Return a map service
#
mapimage = result.getMapImageURL(0)

# Use Python's urllib module's urlretrieve method to copy the image locally
#
urllib.urlretrieve(mapimage, "c:/base/road_buffer.jpg")
5/10/2014