How do you duplicate a point within a single feature class using Python? I have a partial sample script below, but in a nutshell, I want to duplicate a point, copy it into the same shapefile, and populate the fields with user-provided strings. I suspect this is elementary, but I'm new at this and am frustrated with loops and cursors.

# Script tool will take a point file and allow the user to duplicate a single point
# while updating necessary field information. The purpose is not to delete or overwrite
# the original point object, but rather to add a duplicate point with the new information.
# This allows historical context for a point that spatially never changes, but who's 
# fields are dynamic. No information is hard-coded.

import arcpy
# set up input variables for target folder and target dataset.
targetFC=arcpy.GetParameterAsText(0)  # this is the point file
targetKeyField=arcpyGetparameterAsText(1)  # this is the key field in the point file
newKeyID=arcpy.GetParameterAsText(2)  # must be duplicate of a string in targetKeyField
newField2 = arcpy.GetParameterAsText(3)
newField3 = arcpy.GetParamaterAsText(4)

# Add new fields to table
in_table = targetFC
field_name2 = "Field_2"
field_name3 = "Field_3"
field_type = "TEXT"
arcpy.AddField_management(in_table, field_name2, field_type)
arcpy.AddField_management(in_table, field_name3, field_type)

# Loop through targetFC to find a match between newKeyID and a string in targetKeyField
with arcpy.da.SearchCursor(targetFC, [targetKeyField, "SHAPE@"]) as cursor:

     # insertCursor based on the match; duplicate the point in the table
          # Populate the new cursor with the new parameters (newField2, newField3, etc)
     # Error if the newParcelID does not match a string in the targetKeyField
     # User is brought back to enter a new newKeyID string.

You can just copy the geometry object into memory directly from the SearchCursor, using a SQL query to limit the cursor to a single point instead of looping through them all, and then inserting it with an InsertCursor.

It's not possible to open a prompt for a user to enter a different value when running a script as a script tool (which it looks like you're trying to create, given the arcpy.GetParameterAsText() calls), so in the code below I've used arcpy.AddError() and sys.exit(). This will stop the tool with an error message, and when the user closes the failed tool they should be presented with the parameter-specification window again for easy resubmission.

# Define SQL query to select point to be copied
query = ''' "{0}" = '{1}' '''.format(targetKeyField, newKeyID)

# Obtain geometry with a SearchCursor, or error if non-existent
geom_list = [r[0] for r in arcpy.da.SearchCursor(targetFC, ['SHAPE@'], query)]
if len(geom_list) > 0:
    geom = geom_list[0]
    arcpy.AddError('''Point "{0}"='{1}' doesn't exist!'''.format(targetKeyField, newKeyID))
    import sys

# Use an InsertCursor to append geometry with new field values
with arcpy.da.InsertCursor(targetFC, ['SHAPE@', targetKeyField, field_name2, field_name3]) as ic:
    new_row = [geom, newKeyID, newField2, newField3]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.