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")
在下面的代码示例中, 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)
工具组织
地理处理工具以两种不同的方法进行组织。所有工具均以 ArcPy 中的函数的形式提供,但也可以通过匹配工具箱别名的模块调用。尽管帮助中的大多数示例将组织的工具显示为 ArcPy 提供的函数,但两种方法同样有效。使用哪一种方法取决于个人喜好和编码习惯。在下面的示例中,使用两种方法显示 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")
许可:为适应“地图代数”,“空间分析”工具的处理方式有所不同,这些工具只能通过 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 |
返回服务器上作业的状态。
|
|
cancel() |
取消服务器上的作业。 |
|
getInput(index) |
返回给定的输入。如果给定输出是记录集或栅格数据对象,则返回 RecordSet 或 RasterData 对象。 |
|
getMapImageURL(ParameterList, Height, Width, Resolution) |
获取给定输出的地图服务影像。 |
|
getMessage(index) |
返回特定消息。 |
|
getMessages(severity) |
要返回的消息类型。0 = 消息,1 = 警告,2 = 错误。如果未指定值,则返回所有消息类型。 |
|
getOutput(index) |
返回给定的输出。如果给定输出是记录集或栅格数据对象,则返回 RecordSet 或 RasterData 对象。 |
|
getSeverity(index) |
返回特定消息的严重性。 |
结果属性和方法
从服务器工具获取结果
与其他地理处理工具类似,地理处理服务器工具有一组固定的参数,这些参数为工具提供执行所需的信息。在脚本中使用异步服务器工具时,可通过结果的 getOutput 方法检索输出。
提示:IsSynchronous 函数可用于确定工具是同步运行还是异步运行。当工具为同步运行时,结果会自动返回,但在工具运行结束前不能执行任何其他操作。
在下面的示例中,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")
arcgis通过 Python 使用工具 获得结果信息的更多相关文章
- ArcGIS使用Python脚本工具
在Pyhton写的一些代码,用户交互不方便,用户体验比较差,不方便重用.在ArcGIS中可以将用写的Python代码导入到ToolBox中,这样用起来就比较方便了.这里用按要素裁剪栅格的Python来 ...
- arcgis python脚本工具实例教程—栅格范围提取至多边形要素类
arcgis python脚本工具实例教程-栅格范围提取至多边形要素类 商务合作,科技咨询,版权转让:向日葵,135-4855_4328,xiexiaokui#qq.com 功能:提取栅格数据的范围, ...
- 地理信息系统 - ArcGIS - 高/低聚类分析工具(High/Low Clustering ---Getis-Ord General G)
前段时间在学习空间统计相关的知识,于是把ArcGIS里Spatial Statistics工具箱里的工具好好研究了一遍,同时也整理了一些笔记上传分享.这一篇先聊一些基础概念,工具介绍篇随后上传. 空间 ...
- 计算异质性H值(运用arcgis和Python进行区域分析)
最近需要对ecognition分割结果进行统计分析,以此来进一步判断其分割结果中的欠分割和过分割对象,在看了一篇论文后,发现了可以用一个参数H来判断每个切割对象的异质性,由于此方法需要用到arcgis ...
- 『基于ArcGIS的Python编程秘籍(第2版)』书本源码
ArcPy学习 第1章 面向ArcGIS的Python编程语言的基础 略 第2章 管理地图文档和图层 引用当前的地图文档 引用磁盘上的地图文档 获取地图文档的图层列表 限制图层列表 缩放至所选要素 改 ...
- Python开发工具PyCharm个性化设置
Python开发工具PyCharm个性化设置,包括设置默认PyCharm解析器.设置缩进符为制表符.设置IDE皮肤主题等,大家参考使用吧 1.设置默认PyCharm解析器: 操作如下: Pyt ...
- 一种数据与逻辑分离的Python单元测试工具
一种数据与逻辑分离的Python单元测试工具 几个概念 TestCase TestCase是一个完整的测试单元,最小的测试执行实体,就是我们常说的测试用例. TestSuite 以某种特性将测试用例组 ...
- 20180903 - Python Pip 工具下载whl包与离线安装
20180903 - Python Pip 工具下载whl包与离线安装 1. 我的Blog 博客园 https://www.cnblogs.com/piggybaba 个人网站 http://pigg ...
- ArcGIS 要素类平移工具-arcgis案例实习教程
ArcGIS 要素类平移工具-arcgis案例实习教程 联系方式:谢老师,135-4855-4328,xiexiaokui#qq.com 目的:对整个要素类,按指定偏移距离,进行整体平移. 优点:使用 ...
随机推荐
- BZOJ_1016_[JSOI2008]_最小生成树计数_(dfs+乘法原理)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1016 给出一张图,其中具有相同权值的边的数目不超过10,求最小生成树的个数. 分析 生成树的计 ...
- [解决] [centOS] g++ 带 -static 参数编译时,报错 /usr/bin/ld: cannot find -lm
静态编译时缺少某个库 yum install glibc-static 从这里找到的 http://www.linuxquestions.org/questions/linux-software-2/ ...
- ruby编程语言-学习笔记4(第4章 表达式和操作符)
4.6.9 范围 Flip-Flops: ..和... ..和... 操作符不是基于方法的,无法重定义.(优先级比较低) x+1 .. x*x #可以认为是x+1 至 x*x 的范围 因为操作 ...
- wpa_supplicant使用笔记
还在搞8634,看不到头了..唉.wireless_tools的缺点是不支持WPA认证,所以有必要把wpa_supplicant也移植过来.无线 网卡是基于zydas芯片的,正好可以在网上搜到wpa_ ...
- HDU 1518
思路:从第一个数开始搜索,将其和与边长比对,相等则计数+1,计数达到3的时候说明可以组成,因为剩下那条必与边长相等,搜索过程注意剪枝,若某个数已被加入边长则不能重复计算,应将其标记,另外应在每一层递归 ...
- 【原】Spark Standalone如何通过start-all.sh启动集群
1.start-all.sh脚本分析 图1 start-all.sh部分内容 我们可以从start-all.sh脚本源文件中看到它其实是start-master.sh和start-slaves.sh两 ...
- Specular light 计算
Specular lighting is most commonly used to give light reflection off of metallic surfaces such as mi ...
- 安装Ambari
1.yum install pdsh 这玩意一般系统都没带 2.检查下umask码,022是需要的 3.获取ambari的官方repo文件,并安装repo文件 wget http://publi ...
- Codeforces182D - Common Divisors(KMP)
题目大意 如果把字符串a重复m次可以得到字符串b,那么我们称字符串a为字符串b的一个因子,现在给定两个字符串S1和S2,求它们的公共因子个数 题解 如果它们有公共因子,那么显然它们的最小公共因子肯定是 ...
- 费用提前计算相关的DP(BZOJ2037,POJ3042,ZOJ3469)
在刷ZeroClock大神的区间DP专辑,遇见了ZOJ3469,完全不无从下手,然后有人说是论问题,推荐看徐源盛<对一类动态规划问题的研究>这篇论文,果断得膜拜了下,感觉好神奇,可以把未来 ...