CÓMO

Agregar entidades a una clase de entidad con Python

Last Published: December 8, 2023

Resumen

Adding features to a feature class can be automated using a Python script. The ArcPy function, InsertCursor() is used to insert a new row in an attribute table. The function can be used as an object to insert the values for the newly created rows.

Note:
The InsertCursor() function can also be used in conjunction with the SearchCursor() function to replicate certain fields.

Procedimiento

The following procedure demonstrates how to add a feature to a feature class using Python:

  1. Import the ArcPy module.
import arcpy
  1. Define a new array parameter to include the desired values in the new row.
row_values = [('Anderson', (1409934.4442000017, 1076766.8192000017)), ('Andrews', (752000.2489000037, 1128929.8114))]
  1. Create an object item to utilize the InsertCursor() function. The function requires inputs for the desired feature location and the row field names.
cursor = arcpy.da.InsertCursor("C://sample.gdb/test",("[row 1]", "[row 2]"))
  1. Start a loop to create and insert new rows to be filled in the attribute table with the predefined array values in Step 2.
for row in row_values:
    cursor.insertRow(row)
  1. Delete the cursor object to end the function.
del cursor

The following code sample demonstrates a full working code:

import arcpy

# A list of values that will be used to construct new rows

row_values = [('Atascosa', (1409934.4442000017, 1076766.8192000017)),
              ('Bosque', (752000.2489000037, 1128929.8114))]

# Open an InsertCursor.
# Specify the location for the desired feature. The sample inserts the values into the field, NAME and SHAPE.

cursor = arcpy.da.InsertCursor("C:/data/texas.gdb/counties",
                               ("NAME", "SHAPE@XY"))

# Insert new rows that include the county name and the x and y coordinate
# pair that represents the county center

for row in row_values:
    cursor.insertRow(row)

# Delete cursor object

del cursor

To replicate data from another feature class, the SearchCursor() function must be used in conjunction with the InsertCursor() function. The following code sample demonstrates how to use the SearchCursor() and the InsertCursor() functions.

sourceCursor = arcpy.da.SearchCursor([Feature_Name], [Field_Name])
inputCursor = arcpy.da.InsertCursor([New_Feature_Name], [Field_Name])

for row in sourceCursor:
    inputCursor.insertRow(row)

Id. de artículo: 000018755

Recibir notificaciones y encontrar soluciones a problemas nuevos o comunes

Obtenga respuestas resumidas y soluciones de vídeo de nuestro nuevo chatbot de IA.

Descargar la aplicación de soporte de Esri

Información relacionada

Descubrir más sobre este tema

Obtener ayuda de expertos en ArcGIS

Contactar con el soporte técnico

Descargar la aplicación de soporte de Esri

Ir a opciones de descarga