InsertCursor

Résumé

InsertCursor establishes a write cursor on a feature class or table. InsertCursor can be used to add new rows.

Discussion

When using InsertCursor on a point feature class, creating a PointGeometry and setting it to the SHAPE@ token is a comparatively expensive operation. Instead, define the point feature using tokens such as SHAPE@XY, SHAPE@Z, and SHAPE@M for faster, more efficient access.

Opening simultaneous insert or update operations on the same workspace using different cursors requires the start of an edit session.

The following includes some dataset types that can only be edited within an edit session:

  • Feature classes participating in a topology
  • Feature classes participating in a geometric network
  • Feature classes participating in a network dataset
  • Versioned datasets in enterprise geodatabases
  • Some object and feature classes with class extensions
Remarque :

Using an InsertCursor on a layer with a joined table is not supported.

Syntaxe

InsertCursor (in_table, field_names, datum_transformation)
ParamètreExplicationType de données
in_table

Classe d'entités, couche, table ou vue tabulaire.

String
field_names
[field_names,...]

Liste (ou tuple) de noms de champs. Pour un champ unique, vous pouvez utiliser une chaîne de caractères au lieu d'une liste de chaînes.

Utilisez un astérisque (*) au lieu d’une liste de champs pour accéder à tous les champs de la table en entrée (les champs BLOB sont exclus). Toutefois, pour obtenir des performances plus rapides et un ordre des champs fiable, il est recommandé de limiter la liste des champs à ceux qui sont réellement nécessaires.

Les champs raster ne sont pas pris en charge.

Il est possible d’accéder à des informations supplémentaires en utilisant des jetons (tels que OID@) à la place des noms de champs :

  • SHAPE@XYTuple des coordonnées x,y du centroïde de l’entité.
  • SHAPE@XYZTuple des coordonnées x,y,z du centroïde de l’entité.
  • SHAPE@TRUECENTROIDTuple des coordonnées x,y du centroïde de l’entité. La valeur renvoyée est la même que celle renvoyée par SHAPE@XY.
  • SHAPE@XDouble de la coordonnée x de l'entité.
  • SHAPE@YDouble de la coordonnée y de l'entité.
  • SHAPE@ZDouble de la coordonnée z de l'entité.
  • SHAPE@MDouble de la valeur m de l'entité.
  • SHAPE@JSONChaîne JSON Esri représentant la géométrie.
  • SHAPE@WKBReprésentation binaire connue (WKB) de la géométrie de l'OGC. Cette représentation constitue une représentation portable d'une valeur de géométrie sous la forme d'un flux contigu d'octets.Les valeurs peuvent être ajoutées en tant qu’objet bytearray ou bytes.
  • SHAPE@WKTReprésentation textuelle connue (WKT) de la géométrie de l'OGC. Cette représentation constitue une représentation portable d'une valeur de géométrie sous la forme d'une chaîne de texte.
  • SHAPE@Objet géométrie de l'entité.

Polygon, polyline, or multipoint features can only be created using the SHAPE@ token.

String
datum_transformation

When features to be inserted have a different spatial reference than the target feature class, a projection will be performed automatically. If the two spatial references have a different datum, an appropriate transformation should be specified.

La fonction ListTransformations peut servir à fournir une liste de transformations de datum valides entre deux références spatiales.

En savoir plus sur les transformations de datum

String

Propriétés

PropriétéExplicationType de données
fields
(Lecture seule)

A tuple of field names used by the cursor.

The tuple will include all fields and tokens specified by the field_names argument.

The order of the field names on the fields property will be the same as passed in with the field_names argument.

If the field_names argument is set to *, the fields property will include all fields used by the cursor. A value of * will return geometry in a tuple of x,y coordinates (equivalent to the SHAPE@XY token).

tuple

Vue d’ensemble des méthodes

MéthodeExplication
insertRow (row)

Inserts a row into a table.

Méthodes

insertRow (row)
ParamètreExplicationType de données
row
[row,...]

A list or tuple of values. The order of values must be in the same order as specified when creating the cursor.

When updating fields, if the incoming values match the type of field, the values will be cast as necessary. For example, a value of 1.0 to a string field will be added as "1.0", and a value of "25" added to a float field will be added as 25.0.

tuple
Valeur renvoyée
Type de donnéesExplication
Integer

insertRow returns the objectid of the new row.

Exemple de code

InsertCursor example 1

Use InsertCursor to insert new rows into a table.

import arcpy
import datetime

# Create an insert cursor for a table specifying the fields that will
# have values provided
fields = ['rowid', 'distance', 'CFCC', 'DateInsp']
cursor = arcpy.da.InsertCursor('D:/data/base.gdb/roads_maint', fields)

# Create 25 new rows. Set default values on distance and CFCC code
for x in range(0, 25):
    cursor.insertRow((x, 100, 'A10', datetime.datetime.now()))

# Delete cursor object
del cursor
InsertCursor example 2

Use InsertCursor with the SHAPE@XY token to add point features to a point feature class.

import arcpy

# A list of values that will be used to construct new rows
row_values = [('Anderson', (1409934.4442000017, 1076766.8192000017)),
              ('Andrews', (752000.2489000037, 1128929.8114))]

# Open an InsertCursor
cursor = arcpy.da.InsertCursor('C:/data/texas.gdb/counties',
                               ['NAME', 'SHAPE@XY'])

# Insert new rows that include the county name and a x,y coordinate
#  pair that represents the county center
for row in row_values:
    cursor.insertRow(row)

# Delete cursor object
del cursor
InsertCursor example 3

Use InsertCursor with the SHAPE@ token to add a new feature using a geometry object.

import arcpy

# Create a polyline geometry
array = arcpy.Array([arcpy.Point(459111.6681, 5010433.1285),
                     arcpy.Point(472516.3818, 5001431.0808),
                     arcpy.Point(477710.8185, 4986587.1063)])
polyline = arcpy.Polyline(array)

# Open an InsertCursor and insert the new geometry
cursor = arcpy.da.InsertCursor('C:/data/texas.gdb/counties', ['SHAPE@'])
cursor.insertRow([polyline])

# Delete cursor object
del cursor

Rubriques connexes