📄 printgeometry_forscripttool.py
字号:
#import relevant modules, create geoprocessing dispatch object
import win32com.client, sys, traceback
gp = win32com.client.Dispatch("esriGeoprocessing.gpDispatch.1")
def AddPrintMessage(msg, severity):
print msg
if severity == 0: gp.AddMessage(msg)
elif severity == 1: gp.AddWarning(msg)
elif severity == 2: gp.AddError(msg)
try:
#Get Inputs (feature class and feature id)
infc = gp.GetParameterAsText(0)
poly_id = gp.GetParameterAsText(1)
#Describe input fc. Describe object contains useful information used later.
desc = gp.describe(infc)
#set up query. !!BEWARE!!, diff data types have diff query syntax
q = '"' + desc.OIDFieldName + '" = ' + str(poly_id)
#open a search cursor with a query
rows = gp.searchcursor(infc, q)
row = rows.next()
AddPrintMessage("\nFEATURE " + str(row.GetValue(desc.OIDFieldName)) + " GEOMETRY", 0)
#get the geometry of the feature in question and print.
feature = row.GetValue(desc.ShapeFieldName)
#if the feature class is of type point the geometry, the part IS the point
if desc.ShapeType.lower() == "point":
pnt = feature.getpart()
AddPrintMessage(str(pnt.x) + " " + str(pnt.y), 0)
#if the feature class is of type poly or line the part is a list of points and there can be many parts
elif desc.ShapeType.lower() == "polygon" or desc.ShapeType.lower() == "line":
partcount = feature.partcount
partnumber = 0
#cycle through the parts
while partnumber < partcount:
part = feature.getpart(partnumber)
AddPrintMessage("\tPART " + str(partnumber) + ":", 0)
pnt = part.next()
pointnumber = 0
#cycle through the points in each part
while pnt:
AddPrintMessage("\t\tPoint " + str(pointnumber) + " x: " + str(pnt.x) + " y: " + str(pnt.y), 0)
#Get the next point in the part
pnt = part.next()
pointnumber += 1
#catch Interior Rings (Holes)
if not pnt:
pnt = part.next()
if pnt:
AddPrintMessage("\tINTERIOR RING:", 0)
partnumber += 1
AddPrintMessage("\n", 0)
del row
del rows
except:
tb = sys.exc_info()[2]
tbinfo = traceback.format_tb(tb)[0]
pymsg = "PYTHON ERRORS:\nTraceback Info:\n" + tbinfo + "\nError Info:\n " + \
str(sys.exc_type)+ ": " + str(sys.exc_value) + "\n"
AddPrintMessage(pymsg, 2)
msgs = "GP ERRORS:\n" + gp.GetMessages(2) + "\n"
AddPrintMessage(msgs, 2)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -