1. 程式人生 > 實用技巧 >1章程式碼

1章程式碼

arcpy面積計算

#coding=utf8
# -*- coding: UTF-8 -*-
import arcpy
from arcpy import env
import os

import sys
###############
##################################

fc= arcpy.GetParameterAsText(0)
fieldname= arcpy.GetParameterAsText(1)


rows = arcpy.da.UpdateCursor(fc,["shape@",fieldname])

i=1
try:
    for
row in rows: feat = row[0] row[1]=feat.area arcpy.AddMessage("No:"+str(i)+":"+str(feat.area)) rows.updateRow(row) i=i+1 del rows except Exception as e: arcpy.AddError(e.message)

Arcengine C#呼叫arcpy的工具方法

private static bool CalArea(IFeatureClass pFeatureClass, string
FieldName) { string tbxFileName = @"F:\2020book\原始資料\data\1Python基礎\工具箱.tbx"; if (!File.Exists(tbxFileName)) { MessageBox.Show("檔案" + tbxFileName + "不存在"); return false; } IGeoProcessor gp = new GeoProcessor(); gp.OverwriteOutput
= true; gp.AddOutputsToMap = false; gp.AddToolbox(tbxFileName); // Create a variant - data are in the workspace IVariantArray parameters = new VarArray(); parameters.Add(pFeatureClass);//引數 parameters.Add(FieldName);//引數 string toolname="計算面積";//工具名稱,建議工具名稱為英文,標籤為中文, //object sev = null; try { gp.Execute(toolname, parameters, null); } catch (Exception ex) { MessageBox.Show("錯誤:" + ex.Message + ",\n請自己允許" + tbxFileName + "/" + toolname); //string errorMsgs = gp.GetMessages(ref sev); //MessageBox.Show(errorMsgs); return false; } finally { gp = null; //MessageBox.Show("ok"); } return true; }