Reading geometries
arcgis操作、制图、开发、分析、培训、研发、单位机构和重大科技项目技术咨询,qq group ArcGisky: 878796212
Reading geometries
ArcGIS 10.7
Each feature in a feature class contains a set of points defining the vertices of a polygon or line, or a single coordinate defining a point feature. These points can be accessed with geometry objects (Polygon, Polyline, PointGeometry, or MultiPoint), which returns them in an array of Point objects.
Features can have multiple parts. The geometry object's partCount property returns the number of parts for a feature. The getPart method returns an array of point objects for a particular part of the geometry if an index is specified. If an index is not specified, an array containing an array of point objects for each geometry part is returned.
PointGeometry features return a single Point object instead of an array of point objects. All other feature types—polygon, polyline, and multipoint—return an array of point objects or if the feature has multiple parts, an array containing multiple arrays of point objects.
If a polygon contains holes, it consists of a number of rings. The array of point objects returned for a polygon contains the points for the exterior ring and all inner rings. The exterior ring is always returned first, followed by inner rings, with null point objects as the separator between rings. Whenever a script is reading coordinates for polygons in a geodatabase or shapefile, it should contain logic for handling inner rings if this information is required by the script; otherwise, only the exterior ring is read.
A multipart feature is composed of more than one physical part but only references one set of attributes in the database. For example, in a layer of states, the state of Hawaii could be considered a multipart feature. Although composed of many islands, it would be recorded in the database as one feature.
A ring is a closed path that defines a two-dimensional area. A valid ring consists of a valid path, such that the from and to points of the ring have the same x,y coordinates. A clockwise ring is an exterior ring, and a counterclockwise ring defines an interior ring.
Learn more about writing geometries
Using geometry tokens
Geometry tokens can also be used as shortcuts in place of accessing full geometry objects. Additional geometry tokens can be used to access specific geometry information. Accessing the full geometry is more costly timewise. If you only need specific properties of the geometry, use tokens to provide shortcuts to access geometry properties. For instance, SHAPE@XY returns a tuple of x,y coordinates that represent the feature's centroid.
Token |
Explanation |
SHAPE@ |
A geometry object for the feature. |
SHAPE@XY |
A tuple of the feature's centroid x,y coordinates. |
SHAPE@TRUECENTROID |
A tuple of the feature's true centroid x,y coordinates. |
SHAPE@X |
A double of the feature's x-coordinate. |
SHAPE@Y |
A double of the feature's y-coordinate. |
SHAPE@Z |
A double of the feature's z-coordinate. |
SHAPE@M |
A double of the feature's m-value. |
SHAPE@JSON |
The esri JSON string representing the geometry. |
SHAPE@WKB |
The well-known binary (WKB) representation for OGC geometry. It provides a portable representation of a geometry value as a contiguous stream of bytes. |
SHAPE@WKT |
The well-known text (WKT) representation for OGC geometry. It provides a portable representation of a geometry value as a text string. |
SHAPE@AREA |
A double of the feature's area. |
SHAPE@LENGTH |
A double of the feature's length. |
Reading point geometries
The examples below use SearchCursor to print the coordinates for all features:
Search cursor on a point feature class
import arcpy
infc = arcpy.GetParameterAsText(0)
# Enter for loop for each feature
#
for row in arcpy.da.SearchCursor(infc, ["SHAPE@XY"]):
# Print x,y coordinates of each point feature
#
x, y = row[0]
print("{}, {}".format(x, y))
With the above feature class, the script returns the information below:
2.0 4.0
8.0 10.0
7.0 5.0
Reading multipoint geometries
Search cursor on a multipoint feature class
import arcpy
infc = arcpy.GetParameterAsText(0)
# Enter for loop for each feature
#
for row in arcpy.da.SearchCursor(infc, ["OID@", "SHAPE@"]):
# Print the current multipoint's ID
#
print("Feature {}:".format(row[0]))
# For each point in the multipoint feature,
# print the x,y coordinates
for pnt in row[1]:
print("{}, {}".format(pnt.X, pnt.Y))
With the feature class above, the script returns the information below:
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
Reading polyline or polygon geometries
Search cursor on a polygon or line feature class
import arcpy
infc = arcpy.GetParameterAsText(0)
# Enter for loop for each feature
#
for row in arcpy.da.SearchCursor(infc, ["OID@", "SHAPE@"]):
# Print the current multipoint's ID
#
print("Feature {}:".format(row[0]))
partnum = 0
# Step through each part of the feature
#
for part in row[1]:
# Print the part number
#
print("Part {}:".format(partnum))
# Step through each vertex in the feature
#
for pnt in part:
if pnt:
# Print x,y coordinates of current point
#
print("{}, {}".format(pnt.X, pnt.Y))
else:
# If pnt is None, this represents an interior ring
#
print("Interior Ring:")
partnum += 1
|
With the feature class above, the script returns the information below. Feature 0 is a single-part polygon, feature 1 is a two-part polygon, and feature 2 is a single-part polygon with an interior ring.
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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?