ArcGIS 10

每个地理处理工具都具有一组固定的参数,这些参数为工具提供执行所需的信息。工具通常具有定义一个或多个数据集的输入参数,这些数据集一般用于生成新的输出数据。参数具有几个重要属性:

名称

每个工具参数都具有一个唯一名称。

类型

所需数据的类型,如要素类、整型、字符串或栅格。

方向

该参数定义是输入值还是输出值。

必填信息

表示必须为参数提供值还是参数为可选。

在脚本中使用工具时,必须正确设置工具的参数值,以便在运行脚本时工具可以执行。每个工具的文档都明确定义了其参数和属性。提供一组有效的参数值后,工具即准备好执行。

参数将被指定为字符串或对象。字符串是唯一标识参数值的简单文本,如数据集的路径或关键字。

缓冲区工具定义了输入和输出参数。请注意,工具名称始终附加其工具箱的别名。在该示例中,两个字符串变量用于定义输入和输出参数,以便对工具的调用更容易阅读。

import arcpy

roads = "c:/St_Johns/data.gdb/roads"
output = "c:/St_Johns/data.gdb/roads_Buffer"

# Run Buffer using the variables set above and pass the remaining parameters
#   in as strings
#
arcpy.Buffer_analysis(roads, output, "distance", "FULL", "ROUND", "NONE")

在下面的代码示例中, CreateFeatureClass 工具使用其可选“坐标系”参数的空间参考对象执行。空间参考对象使用 SpatialReference 类创建,其信息从投影文件中加载。

import arcpy

inputWorkspace = "c:/temp"
outputName =  "rivers.shp"

# Create a spatial reference object
#
spatialRef = arcpy.SpatialReference()

# Use a projection file to define the spatial reference's properties
#
spatialRef.createFromFile("c:/program files/arcgis/Desktop10.0/Coordinate Systems/" + \
                          "Projected Coordinate Systems/Continental/North America/North America Equidistant Conic.prj")

# Run CreateFeatureclass using the spatial reference object
#
arcpy.CreateFeatureclass_management(inputWorkspace, outputName, 
                                    "POLYLINE", "", "", "", spatialRef)

 

工具组织

GetCount 工具。

import arcpy

inFeatures = "c:/temp/rivers.shp"

# Tools can be accessed as functions on the arcpy module, and
#  from modules matching the toolbox name.
#
arcpy.GetCount_management(inFeatures)
arcpy.management.GetCount(inFeatures)

from - import - as 的形式。

# Clean up street centerlines that were digitized without having set
#  proper snapping environments
#
import arcpy
from arcpy import edit as EDIT
from arcpy import management as DM

streets = "c:/data/streets.gdb/majorrds"
streetsCopy = "c:/output/Output.gdb/streetsBackup"

DM.CopyFeatures(streets, streetsBackup)
EDIT.TrimLine(streets, "10 Feet", "KEEP_SHORT")
EDIT.ExtendLine(streets, "15 Feet", "EXTENSION")
arcgis通过 Python 使用工具 获得结果信息许可:

arcpy.sa 模块调用,而不以 ArcPy 函数的形式提供。

从工具获取结果

结果对象执行时,ArcPy 会返回工具的输出值。结果对象的优点是可以保留工具执行的相关信息,包括消息、参数和输出。即使在运行了多个其他工具后仍可保留这些结果。

下面的示例说明了如何在执行地理处理工具后获取结果对象的输出。

import arcpy

arcpy.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)

 

 

arcgis通过 Python 使用工具 获得结果信息注:

float())从 Unicode 字符串进行转换。

import arcpy
from arcpy import env
import types

env.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 count
print types.TypeType(count) 
结果属性和方法

属性和方法

说明

inputCount

返回输入数目。

outputCount

返回输出数目。

messageCount

返回消息数目。

maxSeverity

返回最大严重性。返回的严重性可以为 0(未产生错误/警告)、1(产生了警告)或 2(产生了错误)。

resultID

status

返回服务器上作业的状态。

  • 0 - 新建
  • 1 - 提交
  • 2 - 正在等待
  • 3 - 正在执行
  • 4 - 成功
  • 5 - 失败
  • 6 - 超时
  • 7 - 正在取消
  • 8 - 取消
  • 9 - 正在删除
  • 10 - 删除

cancel()

取消服务器上的作业。

getInput(index)

返回给定的输入。如果给定输出是记录集或栅格数据对象,则返回 RecordSet 或 RasterData 对象。

getMapImageURL(ParameterList, Height, Width, Resolution)

获取给定输出的地图服务影像。

getMessage(index)

返回特定消息。

getMessages(severity)

要返回的消息类型。0 = 消息,1 = 警告,2 = 错误。如果未指定值,则返回所有消息类型。

getOutput(index)

返回给定的输出。如果给定输出是记录集或栅格数据对象,则返回 RecordSet 或 RasterData 对象。

getSeverity(index)

返回特定消息的严重性。

结果属性和方法

从服务器工具获取结果

getOutput 方法检索输出。

arcgis通过 Python 使用工具 获得结果信息提示:

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

FeatureSet 的保存方法将输出保存在本地。

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")

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

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)

获取地图影像

.jpeg)通过 Web 进行传输。与要素类中的原始要素相比,字节组成的地图图像中包含的信息更易于为人类所理解。地图图像也是易于管理的 - 可轻松地将其压缩,将其分成易于管理的块,并且已建立在 Web 上传输和查看图像的方法。

.mxd) 而生成。鉴于地图图像的上述特征,您最好为地理处理任务的结果创建一个地图图像,然后在 Web 上传输该图像,而不是传输一个或多个结果数据集。地理处理服务可包含由 ArcGIS 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")

相关文章:

  • 2021-06-13
  • 2021-07-08
  • 2021-05-23
  • 2021-11-25
  • 2021-05-21
  • 2021-05-01
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-12-15
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2023-03-09
相关资源
相似解决方案