📄 printgeometry_standalone.py
字号:
#import relevant modules, create geoprocessing dispatch object
import win32com.client, sys, traceback
gp = win32com.client.Dispatch("esriGeoprocessing.gpDispatch.1")
try:
#Get Inputs (feature class and feature id)
infc = r"C:\Demos\UC2005\AdvPython\CursorDemo\BasicShapesCopy.shp"
poly_id = 0
#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()
print "\nFEATURE " + str(row.GetValue(desc.OIDFieldName)) + \
" GEOMETRY"
#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()
print str(pnt.x) + " " + str(pnt.y)
#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)
print "\tPART " + str(partnumber) + ":"
pnt = part.next()
pointnumber = 0
#cycle through the points in each part
while pnt:
print "\t\tPoint " + str(pointnumber) + \
" x: " + str(pnt.x) + " y: " + str(pnt.y)
#Get the next point in the part
pnt = part.next()
pointnumber += 1
#catch Interior Rings (Holes)
if not pnt:
pnt = part.next()
if pnt:
print "\tINTERIOR RING:"
partnumber += 1
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"
print pymsg
msgs = "GP ERRORS:\n" + gp.GetMessages(2) + "\n"
print msgs
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -