gispython定义查询_arcgis通过 Python 使用工具 获得结果信息

通过 Python 使用工具

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

在下面的代码示例中,

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)

工具组织

地理处理工具以两种不同的方法进行组织。所有工具均以 ArcPy 中的函数的形式提供,但也可以通过匹配工具箱别名的模块调用。尽管帮助中的大多数示例将组织的工具显示为 ArcPy 提供的函数,但两种方法同样有效。使用哪一种方法取决于个人喜好和编码习惯。在下面的示例中,使用两种方法显示

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

许可:

为适应“地图代数”,“空间分析”工具的处理方式有所不同,这些工具只能通过 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)

注:

结果对象的 getOutput 方法会返回一个 Unicode 字符串,来表示含有输出值的结果对象。当运行诸如 GetCount(提供表中的记录数)或 CalculateDefaultClusterTolerance(提供拓扑容差值)之类的工具时,需要重点考虑这一点。要将值转换为所需类型,需要使用内置 Python 函数(如 int() 或 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

返回唯一结果 ID。如果工具不是地理处理服务,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 方法检索输出。

提示:

在下面的示例中,GetParameterValue 函数用于从服务器工具获取 FeatureSet 对象。此 FeatureSet 对象包含工具输入参数的模式。该 FeatureSet 对象随后通过要素类加载,而服务器工具则在服务器上执行。脚本以使用结果对象的 getOutput 方法获取工具的输出而结束,然后使用 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")

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

栅格数据结果以标记图像文件格式 (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.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 上传输和查看图像的方法。

地图图像由 ArcGIS Server 地图服务创建,通过发布 ArcMap 文档 (.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")

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值