Using insert and update cursors, scripts can create new features in a feature class or update existing ones. A script can define a feature by creating a Point object, populating its properties, and placing it in an Array. That array can then be used to set a feature's geometry using Polygon, Polyline, PointGeometry, or Multipoint geometry classes.
import arcpy fc = "c:/data/gdb.gdb/roads" cursor = arcpy.da.InsertCursor(fc, ["SHAPE@"]) array = arcpy.Array([arcpy.Point(5997611.48964, 2069897.7022), arcpy.Point(5997577.46097, 2069905.81145)]) spatial_reference = arcpy.SpatialReference(4326) polyline = arcpy.Polyline(array, spatial_reference) cursor.insertRow([polyline])
As shown above, a single geometry part is defined by an array of points. Likewise, a multipart feature can be created from an array of arrays of points as shown below using the same cursor.
firstPart = arcpy.Array([arcpy.Point(5997624.6225, 2069868.8208), arcpy.Point(5997674.94199, 2069833.81741)]) secondPart = arcpy.Array([arcpy.Point(5997616.44497, 2069862.32774), arcpy.Point(5997670.57373, 2069824.67456)]) array = arcpy.Array([firstPart, secondPart]) spatial_reference = arcpy.SpatialReference(4326) multipartFeature = arcpy.Polyline(array, spatial_reference) cursor.insertRow([multipartFeature])
When writing point features, only a single point object is used to set the geometry of a point feature. Points can also be created more easily (and efficiently) using the SHAPE@XY token (and SHAPE@M and SHAPE@Z tokens, as needed).
import arcpy # fc is a point feature class fc = "c:/data/gdb.gdb/stops" cursor = arcpy.da.InsertCursor(fc, ["SHAPE@XY"]) xy = (5997594.4753, 2069901.75682) cursor.insertRow([xy])
All geometries are validated before they are written to a feature class. Issues such as incorrect ring orientation and self-intersecting polygons, among others, are corrected when the geometry is simplified before its insertion.
The following example shows how to read a set of coordinates (defined by coords_list) containing a series of linear coordinates and use them to create a new feature class.
# 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.CreateFeatureclass_management(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 # Add the point to the feature's array of points # If the ID has changed, create a new feature if ID != coords: cur.insertRow([arcpy.Polyline(array)]) array.removeAll() array.add(arcpy.Point(coords, coords, ID=coords)) ID = coords # Add the last feature polyline = arcpy.Polyline(array, spatial_reference) cur.insertRow([polyline]) except Exception as e: print(e) finally: # Cleanup the cursor if necessary if cur: del cur
Multipart polygon and polyline features and polygon features with interior rings can be created by creating an array of arrays and passing that to Polygon and Polyline classes.
Geometry creation from lists of coordinates
Geometry can also be created from a list of coordinates. This approach can provide performance gains, as it avoids the overhead of creating geometry objects. However, it is limited to only features that are singlepart, and in the case of polygons, without interior rings. All coordinates should be in the units of the feature class's spatial reference.
In the example below, a single polygon feature is created from a list of x,y pairs.
import arcpy import os 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", "POLYGON", spatial_reference=4326) feature_class = result # Write feature to new feature class with arcpy.da.InsertCursor(feature_class, ['SHAPE@']) as cursor: cursor.insertRow([coordinates])