读取几何
要素类中的每个要素都包含一组用来定义面或线折点的点要素,或者包含单个用来定义点要素的坐标。可以使用几何对象访问这些点,几何对象将以点对象的数组形式返回这些点。
|
属性 |
说明 |
|---|---|
|
count |
数组中的对象数目。 |
|
方法 |
说明 |
|---|---|
|
add(value) |
在数组中的最后一个位置添加数组或点对象。 |
|
append(value) |
在数组中的最后一个位置追加一个对象。 |
|
clone(point_object) |
克隆点对象。 |
|
extend(items) |
通过追加元素扩展数组。 |
|
getObject(index) |
返回数组中的特定对象。 |
|
insert(index, value) |
在数组中的特定位置添加一个对象。 |
|
next() |
返回数组中的下一个对象。 |
|
remove(index) |
从数组中移除特定对象。 |
|
removeAll() |
移除所有对象并创建一个空数组。 |
|
replace(index, value) |
根据索引位置替换对象。 |
|
reset() |
将数组重置为第一个对象。 注:重置仅用于非光标数组。对光标数组使用重置将不会重置为第一个记录。 |
地理数据库或 shapefile 中的要素可以包含多个部分。几何对象的 partCount 属性将返回要素的部分数目。如果指定了索引,则 getPart 方法将返回特定几何部分的点对象数组。如果未指定索引,则返回的数组将包含每个几何部分的点对象数组。
点要素将返回单个点对象而不是点对象数组。所有其他要素类型(面、折线和多点)将返回一个点对象数组,或者,如果要素具有多个部分,则返回包含多个点对象数组的数组。
|
属性 |
说明 |
|---|---|
|
ID |
点的形状 ID |
|
X |
点的水平坐标 |
|
Y |
点的垂直坐标 |
|
Z |
点的高程值 |
|
M |
点的测量值 |
如果一个面包含多个洞,它将由多个环组成。针对面返回的点对象数组将包含外部环及所有内部环的点。外部环总是先返回,接着是内部环,其中以空点对象作为环之间的分隔符。当脚本在地理数据库或 shapefile 中读取面的坐标时,它应包含用于处理内部环的逻辑(如果脚本需要此信息);否则,将只读取外部环。
多部分 (multipart) 要素是由多个物理部分组成的,但是只引用数据库中的一组属性。例如,在州行政区图层中,可将夏威夷州看作是一个多部分要素。虽然它是由许多岛屿组成的,但在数据库中仍将其记录为一个要素。
环是一个用于定义二维区域的闭合路径。有效的环是由有效路径组成的,因而环的起点和终点具有相同的 x,y 坐标。顺时针环是外部环,逆时针环定义内部环。
以下示例将打印所有要素的坐标:
读取点几何
点要素类上的搜索光标
import arcpy
infc = arcpy.GetParameterAsText(0)
# Identify the geometry field
#
desc = arcpy.Describe(infc)
shapefieldname = desc.ShapeFieldName
# Create search cursor
#
rows = arcpy.SearchCursor(infc)
# Enter for loop for each feature/row
#
for row in rows:
# Create the geometry object 'feat'
#
feat = row.getValue(shapefieldname)
pnt = feat.getPart()
# Print x,y coordinates of current point
#
print pnt.X, pnt.Y
![]() |
对于上述要素类,脚本将返回以下信息:
2.0 4.0 8.0 10.0 7.0 5.0
读取多点几何
多点要素类上的搜索光标
import arcpy
infc = arcpy.GetParameterAsText(0)
# Identify the geometry field
#
desc = arcpy.Describe(infc)
shapefieldname = desc.ShapeFieldName
# Create search cursor
#
rows = arcpy.SearchCursor(infc)
# Enter for loop for each feature/row
#
for row in rows:
# Create the geometry object
#
feat = row.getValue(shapefieldname)
# Print the current multipoint's ID
#
print "Feature %i:" % row.getValue(desc.OIDFieldName)
# For each point in the multipoint feature,
# print the x,y coordinates
for pnt in feat:
print pnt.X, pnt.Y
![]() |
对于上述要素类,脚本将返回以下信息:
Feature 0: 3.0 8.0 4.0 4.0 6.0 6.0 Feature 1: 5.0 9.0 8.0 10.0 Feature 2: 9.0 5.0
读取多义线或多边形几何
面或线要素类上的搜索游标
import arcpy
infc = arcpy.GetParameterAsText(0)
# Identify the geometry field
#
desc = arcpy.Describe(infc)
shapefieldname = desc.ShapeFieldName
# Create search cursor
#
rows = arcpy.SearchCursor(infc)
# Enter for loop for each feature/row
#
for row in rows:
# Create the geometry object
#
feat = row.getValue(shapefieldname)
# Print the current multipoint's ID
#
print "Feature %i:" % row.getValue(desc.OIDFieldName)
partnum = 0
# Step through each part of the feature
#
for part in feat:
# Print the part number
#
print "Part %i:" % partnum
# Step through each vertex in the feature
#
for pnt in feat.getPart(partnum):
if pnt:
# Print x,y coordinates of current point
#
print pnt.X, pnt.Y
else:
# If pnt is None, this represents an interior ring
#
print "Interior Ring:"
partnum += 1
![]() |
对于上述要素类,脚本将返回以下信息。要素 0 是单部分面,要素 1 是两部分面,而要素 2 是带有内部环的单部分面。
Feature 0: Part 0: 3.0 8.0 1.0 8.0 2.0 10.0 3.0 8.0 Feature 1: Part 0: 5.0 3.0 3.0 3.0 3.0 5.0 5.0 3.0 Part 1: 7.0 5.0 5.0 5.0 5.0 7.0 7.0 5.0 Feature 2: Part 0: 9.0 11.0 9.0 8.0 6.0 8.0 6.0 11.0 9.0 11.0 Interior Ring: 7.0 10.0 7.0 9.0 8.0 9.0 8.0 10.0 7.0 10.0



