如何使用借助python完成 ARCGIS工具箱的調用


上個月使用python調用arcgis工具箱完成了火點txt文件轉shp文件的小功能, 感覺很不錯, 寫下這篇博客希望對大家有所幫助。

1.環境介紹:

  系統: win8.1(64位)

  arcgis:desktop10.2

2.ARCGIS 工具箱簡單介紹:

 

工具英文名稱 工具中文名稱
3D Analyst toolbox
 

三維分析工具箱

Data Reviewer toolbox 繪圖工具箱
Conversion toolbox 轉換工具箱
Data Management toolbox 數據管理工具箱
Data Reviewer toolbox 數據檢查工具箱
Geostatistical Analyst 地統計分析
Spatial Analyst toolbox 空間分析工具箱
Spatial Statistics toolbox 空間統計工具箱

詳細幫助頁面可以參考 : http://help.arcgis.com/zh-cn/arcgisdesktop/10.0/help/index.html#/na/00r90000001n000000/

3.使用python代碼完成arcgis工具箱的調用

3.1.在介紹這個調用之前,先學習一個簡單的python程序

import sys
def hello(name):
    print "hello " + name +"!"

if "__main__" == __name__:
    #get count of parameter
    nLen = len(sys.argv)
    #print parametes
    for i in range(0, nLen):
        print ("argv %d:%s"%(i, sys.argv[i]))
    #call method hello
    hello(sys.argv[1])

這個python程序的功能很簡單的, 就是接收一個name, 打印hello name!。 我們需要將這個文本保存到本地,修改名字為hello.py,主要擴展名為.py 。 這個hello.py就是python程序的源碼文件了。接着打開命令行(win +r) 輸入 d:\Python27\ArcGIS10.2\python.exe e:\test\hello.py zhaojiedi 回車就完成了程序的調用。d:\Python27\ArcGIS10.2\python.exe 是你python程序的所在位置,用於解釋后面的python程序文件,e:\test\hello.py是你編寫的python文件位置,zhaojiedi 這個就是你要傳遞給python程序的參數。這里是一個參數的, 如果多個參數, 參數中間空格分隔即可。

下面是我調用的輸出結果

argv 0:E:\test\hello.py
argv 1:zhaojiedi
hello zhaojiedi!

通過以上的簡單小程序,就可以完成python程序接收外部參數,完成指定的功能啦。

 3.2.接下來使用c#調用下這個python文件。(當然可以使用其他語言調用了,這里使用c#語言演示下,其他語言同理)

  void  TestPython(string name)
  {
    string pythonExe =  @"d:\Python27\ArcGIS10.2\python.exe";
    string pythonFile = @"e:\test\hello.py";
    string outMessage = "";
    //創建一個進程
    Process process = new Process();
    //設置進程的exe文件
    process.StartInfo.FileName =pythonExe;
    //設置啟動參數
    process.StartInfo.Arguments = string.Format(" {0} {1}", pythonFile, "zhaojiedi"); 
    //設置窗口風格
    process.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
    process.StartInfo.CreateNoWindow = false;
    //是否使用shell ,這個是false的時候才可以將下面的重定向參數設置為true
    process.StartInfo.UseShellExecute = false;
    //設置重定向
    process.StartInfo.RedirectStandardError = true;
    process.StartInfo.RedirectStandardInput = true;
    process.StartInfo.RedirectStandardOutput = true;
    //初始化退出碼
    exitCode = -1;
    try
    {
        process.Start();
   //獲取重定向的結果信息。    outMessage
= process.StandardOutput.ReadToEnd(); process.WaitForExit(); exitCode = process.ExitCode; } catch (Exception ex) { //todo log }
   //在終端打印結果信息。   Console.WriteLine(outMessage);
//todo 判斷退出碼 }

3.3. 有了上面的基礎, 接下來就可以我們的正題啦, 了解arcgis工具, 並使用python調用。

 這里提供一個樣例 , 就是通過python程序完成txt文本文件的點轉矢量shp文件。

 在arcgis中我們可以使用工具箱的Data management Tools | Layer and Table Views | Make XY EventLayer 這個工具 添加txt文本文件,如下圖:

data.txt 內容如下(中間制表符分割)

101.2	35.1	0.3
121.2	43.2	0.8
101.5	40.2	0.96
110.5	35.6	0.4

點擊OK 我們就可以將txt文件中經緯度轉成要素(還不是矢量文件呢)。 點擊ToolHelp(上圖中的按鈕) 獲取工具幫助信息,彈出如下幫助信息。

copy 這段代碼。

在打開arcgis 工具箱 Convert Tools | ToShapeFile | Feature Class To ShapeFile ,點擊ToolHelp 獲取工具幫助信息,copy調用樣例。

修改copy的代碼如下

#made by zhaojiedi1992
#import model
import os
import os.path
import arcpy
import sys

if "__main__" == __name__:
    nLen = len(sys.argv)
    for i in range(0, nLen):
        print ("argv %d:%s\n"%(i, sys.argv[i]))
    print "===parm ready============ Start ................"
  #get parameter
txtFile = sys.argv[1] curDir = os.path.split(txtFile)[0]; filename = os.path.split(txtFile)[1]; arcpy.env.workspace = curDir lonField = "Field1" latField = "Field2" featureLayer = filename.replace(".txt",""); sharpFile = txtFile.replace(".txt",".shp") if os.path.exists(sharpFile): os.remove(sharpFile) print "txtFile" + ":\t" + txtFile print "curDir" + ":\t" + curDir print "filename" + ":\t" + filename print "featureLayer" + ":\t" + featureLayer #step1 Creates an XY layer try: # Make the XY event layer... arcpy.MakeXYEventLayer_management(txtFile, lonField, latField, featureLayer) except: # If an error occurred print the message to the screen print arcpy.GetMessages() #step2 feature to sharpfile arcpy.FeatureClassToShapefile_conversion([featureLayer], curDir) print "===========================end============================================\n"

保存這個python代碼為txtToShp.py,打開命令行就可以調用了d:\Python27\ArcGIS10.2\python.exe e:\test\txtToShp.py e:\test\data.txt, 當然可以使用3.2中的方法創建一個進程去調用python.exe解析python程序完成調用。

把結果的data.shp文件加載到arcgis中如下圖:

看完我的這個文章是不是感覺,arcgis 工具箱手工點擊實現的功能都可以通過python程序調用了呢。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM