streetsCopy = "c:/output/Output.gdb/streetsBackup"DM.CopyFeatures(streets, streetsBackup)EDIT.TrimLine(streets, "10 Feet", "KEEP_SHORT")EDIT.ExtendLine(streets, "15 Feet", "EXTENSION")许可:为适应“地图代数”,“空间分析”工具的处理方式有所不同,这些工具只能通过arcpy.sa 模块调用,而不以ArcPy 函数的形式提供。
从工具获取结果当作为结果对象执行时,ArcPy 会返回工具的输出值。
结果对象的优点是可以保留工具执行的相关信息,包括消息、参数和输出。
即使在运行了多个其他工具后仍可保留这些结果。
下面的示例说明了如何在执行地理处理工具后获取结果对象的输出。
import arcpyarcpy.env.workspace = "D:/St_Johns/data.gdb"# Geoprocessing tools return a result object of the derived# output dataset.#result = arcpy.CopyFeatures_management("roads", "urban_roads")# A print statement will display the string# representation of the output.#print result# To get the output value, the result object has a getOutput method#resultValue = result.getOutput(0)注意:结果对象的getOutput 方法会返回一个Unicode 字符串,来表示含有输出值的结果对象。
当运行诸如GetCount(提供表中的记录数)或CalculateDefaultClusterTolerance(提供拓扑容差值)之类的工具时,需要重点考虑这一点。
要将值转换为所需类型,需要使用内置Python 函数(如int() 或float())从Unicode 字符串进行转换。
import arcpyfrom arcpy import envimport typesenv.workspace = "c:/St_Johns/data.gdb"# Many geoprocessing tools return a result object of the derived# output dataset. A print statement will display the string# representation of the output.#result = arcpy.GetCount_management("roads")resultValue = result.getOutput(0)# The result object's getOutput method returns values as a# unicode string. To convert to a different Python type, use# built-in Python functions: str(), int(), long(), float() #count = int(resultValue)print countprint types.TypeType(count)结果属性和方法从服务器工具获取结果与其他地理处理工具类似,地理处理服务器工具有一组固定的参数,这些参数为工具提供执行所需的信息。
在脚本中使用异步服务器工具时,可通过结果的getOutput 方法检索输出。
提示提示:IsSynchronous 函数可用于确定工具是同步运行还是异步运行。
当工具为同步运行时,结果会自动返回,但在工具运行结束前不能执行任何其他操作。
在下面的示例中,GetParameterValue 函数用于从服务器工具获取FeatureSet 对象。
此FeatureSet 对象包含工具输入参数的模式。
该FeatureSet 对象随后通过要素类加载,而服务器工具则在服务器上执行。
脚本以使用结果对象的getOutput 方法获取工具的输出而结束,然后使用FeatureSet 的保存方法将输出保存在本地。
import arcpyimport 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) 的形式返回。
默认情况下,使用getOutput 时,TIFF 被写入到系统的TEMP 文件夹。
要控制TIFF 的位置,请将scratchWorkspace 环境设置为一个文件夹。
import arcpyimport 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.statustime.sleep(0.2)# Raster output will be written to the scratchworkspace#outTIFF = result.getOutput(0)获取地图影像地理处理服务可包含结果地图服务,以创建任务结果的数字地图图像。
数字地图包含向用户传达信息的地理数据集的直观表示。
数字地图以图像形式(如.jpeg)通过Web 进行传输。
与要素类中的原始要素相比,字节组成的地图图像中包含的信息更易于为人类所理解。
地图图像也是易于管理的- 可轻松地将其压缩,将其分成易于管理的块,并且已建立在Web 上传输和查看图像的方法。
地图图像由ArcGIS Server 地图服务创建,通过发布ArcMap 文档(.mxd) 而生成。
鉴于地图图像的上述特征,您最好为地理处理任务的结果创建一个地图图像,然后在Web 上传输该图像,而不是传输一个或多个结果数据集。
地理处理服务可包含由ArcGIS Server 使用的结果地图服务,以创建输出数据的地图图像。
import arcpyimport timeimport 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")通过Python使用函数函数是用于执行某项特定任务并能够纳入更大的程序的已定义功能。