How do you duplicate a point within a shapefile using Python?

731
1
07-28-2014 08:34 AM
DSelik
by
New Contributor

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 pupulate 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 updateing necessary field information.

# The purpose is not to delete or overwrite the orginal point object, but rather to ad a duplicate point with the new information.

# This allows historical context for apoint 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.

Tags (3)
0 Kudos
1 Reply
CodyScott
Occasional Contributor

If your trying to insert the feature simply based on the old stuff its a pretty straight forward

you'll want to create the insert cursor defined with your insert fields and just fill in the shape information from your old one into the new one.

For returning an error only if there is no match, I find it easier to stick it in is own subroutine in and return an error only if one doesn't match

#Only use shape@ if your insert into a table, otherwise its not needed.

#Unless you want to keep the XY coordinates, then create a field and populate those too

def findMatch(in_table,field_name2,field_name3,targetFC,targetKeyField,newKeyID,newField2,newField3):

     with arcpy.da.InsertCursor(in_table,[field_name2,field_name3,"SHAPE@"]) as ic:

          with arcpy.da.SearchCursor(targetFC,[targetKeyField,"SHAPE@") as sc:

               #Loop the features in your feature

               for row in sc:

                    #Find match

                    if row[0] == newKeyID:

                         #i don't know what your insertvalues are, but they go below in the first two spots as defined in your cursor

                         #For each item you want to add, you'll need to add it to your insert cursor

                         #Also, keep that extra comma at the end of the insert.

                         ic.insertRow((newField2,newField3,row[2],))

                         arcpy.AddMessage("{} added".format(newKeyID))

                         return True

     #This runs if not match found

     arcpy.AddMessage("{} No match".format(newKeyID))

     return False

then you call it using the following, filling in the values with your desired inputs:

findMatch(in_table,field_name2,field_name3,targetFC,targetKeyField,newKeyID,newField2,newField3)

you'll have to play around to make it work exactly how you want but it should put you on the right track.

Cheers

0 Kudos