使用插入和更新游标,您可以使用脚本在要素类中创建要素或更新现有要素。 脚本可以通过创建 Point 对象、填充要素属性和将要素放入 Array 中来定义要素。 然后,即可通过 Polygon、Polyline、PointGeometry 或 Multipoint 几何类使用该数组来设置要素几何。
import arcpy
fc = "c:/data/gdb.gdb/roads"
cursor = arcpy.da.InsertCursor(fc, ["SHAPE@"])
array = arcpy.Array(
[arcpy.Point(-77.4349451, 37.5408265), arcpy.Point(-78.6384349, 35.7780943)]
)
spatial_reference = arcpy.SpatialReference(4326)
polyline = arcpy.Polyline(array, spatial_reference)
cursor.insertRow([polyline])
如上所示,单个几何部分可以由点数组定义。 同样,可以使用同一游标从点数组的数组创建多部件要素,如下所示。
import arcpy
first_part = arcpy.Array(
[arcpy.Point(-77.4349451, 37.5408265), arcpy.Point(-78.6384349, 35.7780943)]
)
second_part = arcpy.Array(
[arcpy.Point(-79.7910143, 36.0786785), arcpy.Point(-80.8546435, 35.2315402)]
)
array = arcpy.Array([first_part, second_part])
spatial_reference = arcpy.SpatialReference(4326)
multipart_feature = arcpy.Polyline(array, spatial_reference)
cursor.insertRow([multipart_feature])
编写点要素时,仅使用单个点对象来设置点要素的几何。 使用 SHAPE@XY 令牌(以及 SHAPE@M 和 SHAPE@Z 令牌(如果需要的话)),可以更加轻松(而且高效)地创建点。
import arcpy
# fc is a point feature class
fc = "c:/data/gdb.gdb/stops"
with arcpy.da.InsertCursor(fc, ["SHAPE@XY"]) as cursor:
xy = (5997594.4753, 2069901.75682)
cursor.insertRow([xy])
使用 SHAPE@ 令牌也可编写点。
import arcpy
with arcpy.da.UpdateCursor(
"Shops", ["SHAPE@", "ST_ADDRESS", "ZIP"], where_clause="NAME = 'The Cupcakery'"
) as cursor:
for row in cursor:
pt = arcpy.Point(-117.18854177699995, 34.06044265100007)
row = [pt, "540 W Stuart Ave", 92374]
cursor.updateRow(row)
所有几何在写入要素类之前都经过验证。 在插入几何前的几何简化过程中,将纠正各类问题(例如,不正确的环方向和自相交面以及其他问题)。
以下示例显示如何读取包含一系列线性坐标的坐标组(由 coords_list 定义),并使用它们创建要素类。
# Create a new line feature class using a text file of coordinates.
# Each coordinate entry is semicolon delimited in the format of ID;X;Y
import arcpy
import os
# List of coordinates (ID, X, Y)
coords_list = [
[1, -61845879.0968, 45047635.4861],
[1, -3976119.96791, 46073695.0451],
[1, 1154177.8272, -25134838.3511],
[1, -62051091.0086, -26160897.9101],
[2, 17365918.8598, 44431999.7507],
[2, 39939229.1582, 45252847.3979],
[2, 41170500.6291, 27194199.1591],
[2, 17981554.5952, 27809834.8945],
[3, 15519011.6535, 11598093.8619],
[3, 52046731.9547, 13034577.2446],
[3, 52867579.6019, -16105514.2317],
[3, 17160706.948, -16515938.0553],
]
# The output feature class to be created
outFC = arcpy.GetParameterAsText(0)
# Get the template feature class
template = arcpy.GetParameterAsText(1)
cur = None
try:
# Create the output feature class
arcpy.management.CreateFeatureclass(
os.path.dirname(outFC), os.path.basename(outFC), "POLYLINE", template
)
# Access spatial reference of template to define spatial
# reference of geometries
spatial_reference = arcpy.Describe(template).spatialReference
# Open an insert cursor for the new feature class
cur = arcpy.da.InsertCursor(outFC, ["SHAPE@"])
# Create an array object needed to create features
array = arcpy.Array()
# Initialize a variable for keeping track of a feature's ID.
ID = -1
for coords in coords_list:
if ID == -1:
ID = coords[0]
# Add the point to the feature's array of points
# If the ID has changed, create a new feature
if ID != coords[0]:
cur.insertRow([arcpy.Polyline(array)])
array.removeAll()
array.add(arcpy.Point(coords[1], coords[2], ID=coords[0]))
ID = coords[0]
# Add the last feature
polyline = arcpy.Polyline(array, spatial_reference)
cur.insertRow([polyline])
except Exception as e:
print(e)
finally:
# Clean up the cursor if necessary
if cur:
del cur
在以下示例中,现有元素与新要素组合,现有要素使用组合要素更新。
import arcpy
array = arcpy.Array(
[
arcpy.Point(-117.1723261, 34.0240298),
arcpy.Point(-117.1722951, 34.0240060),
arcpy.Point(-117.1719831, 34.0240465),
arcpy.Point(-117.1716733, 34.0241251),
]
)
with arcpy.da.UpdateCursor(
"HikingTrails", ["SHAPE@"], where_clause="TRAILNAME = 'Sunset Ridge Trail'"
) as cursor:
for row in cursor:
new_polyline = arcpy.Polyline(array)
row[0] = row[0].union(new_polyline)
cursor.updateRow(row)
要创建多部分面要素和折线要素以及带有内部环的面要素,需要先创建由数组构成的数组。 然后将其传递到 Polygon 和 Polyline 类。
根据坐标列表创建几何
也可以根据坐标列表创建几何。 这种方法可以提高性能,因为其可免除创建几何对象的消耗。 但是,仅限于单部件要素;对于面要素,则没有内部环。 所有坐标应该以要素类的空间参考为单位。
在以下示例中,将根据 x,y 对列表创建单个折线要素。
import arcpy
coordinates = [
(-117.2000424, 34.0555514),
(-117.2000788, 34.0592066),
(-117.1957315, 34.0592309),
(-117.1956951, 34.0556001),
]
# Create a feature class with a spatial reference of GCS WGS 1984
result = arcpy.management.CreateFeatureclass(
arcpy.env.scratchGDB, "esri_square", "POLYLINE", spatial_reference=4326
)
feature_class = result[0]
# Write feature to new feature class
with arcpy.da.InsertCursor(feature_class, ["SHAPE@"]) as cursor:
cursor.insertRow([coordinates])
类似地,在以下示例中,将根据 x,y,z 坐标的列表创建单个 3D 折线要素。
import arcpy
coordinates = [
(-117.2000424, 34.0555514, 1),
(-117.2000788, 34.0592066, 2),
(-117.1957315, 34.0592309, 5),
(-117.1956951, 34.0556001, 2),
]
# Create a feature class with a spatial reference of GCS WGS 1984
result = arcpy.management.CreateFeatureclass(
arcpy.env.scratchGDB,
"esri_square_z",
"POLYLINE",
has_z="ENABLED",
spatial_reference=4326,
)
feature_class = result[0]
# Write feature to new feature class
with arcpy.da.InsertCursor(feature_class, ["SHAPE@"]) as cursor:
cursor.insertRow([coordinates])