Python 中的地理处理服务

与其他地理处理工具类似,地理处理服务器工具有一组固定的参数,这些参数为工具的执行提供所需的信息。 在脚本中使用异步服务器工具时,可通过 ResultgetOutput 方法对输出进行检索。

提示:

IsSynchronous 函数可用于确定工具是同步执行还是异步执行。 当工具为同步执行时,结果会自动返回,但在工具结束之前不能执行任何其他操作。

在下面的示例中,GetParameterValue 函数用于从服务器工具获取 FeatureSet 对象。 此 FeatureSet 对象包含工具输入参数的模式。 该 FeatureSet 对象随后随要素类加载,而服务器工具则在服务器上运行。 脚本以使用 Result 对象的 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.mytools.BufferPoints(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[0]
outFeatSet.save("c:/temp/base.gdb/towers_buffer")

从服务器工具获取栅格数据结果

栅格结果以标记图像文件格式 (TIFF) 的形式返回。 默认情况下,使用 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.mytools.RunSlope(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[0]

获取地图图像

地理处理服务可以包含一个结果地图服务,来创建任务结果的数字地图图像。 数字地图包含传达信息的地理数据集的视觉表示。 数字地图以图像形式(如 .jpeg)通过 Web 进行传输。 地图图像各个字节包含比要素类中的原始要素多得多的人类可解释的信息。 地图图像也是易于管理的 - 可轻松地压缩,被分成易于管理的块,而且也建立了在 Web 上传输和查看图像的各种方法。

地图图像由 ArcGIS Server 地图服务创建,通过发布 ArcMap 文档 (.mxd) 而生成。 由于地图图像的特性,您可能希望为地理处理任务的结果创建一个图像并在 Web 上传输图像,而不是传输结果数据集或数据集。 地理处理服务可包含一个 ArcGIS Server 结果地图服务,以创建输出数据的地图图像。

import arcpy
import time
from urllib.request import urlretrieve

# 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.mytools.BufferPoints(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.request's urlretrieve method to copy the image locally
urlretrieve(mapimage, "c:/base/road_buffer.jpg")