I had a point feature class containing features with unique names. I also had a table containing hundreds of records, each “linking” to the point feature classes via name. Normally, I could create points for the records by using Make Query Table.
I say “linking” because from looking at the data, I could see which records belonged to which point, but unfortunately there were spelling mistakes and different naming conventions e.g. if the point was called “ABC Sewage Treatment Works”, some of its matching records in the table would be “ABC WWTP”, “AB-C Waste Water Treatment Plant”.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
@author Cindy Jayakumar | |
@date 31/01/2017 | |
– Inserts a new point with the selected geometry in to_lyr | |
– Adds attributes to that point from from_lyr | |
– Updates the record in from_lyr | |
Uses selected features | |
For the python window in ArcMap | |
''' | |
import arcpy | |
from_lyr = r'C:\Some\Arb\Folder\work.gdb\tbl_test' | |
mxd = arcpy.mapping.MapDocument("CURRENT") | |
out_lyr = arcpy.mapping.ListLayers(mxd, "lyr")[0] | |
''' | |
– Select the single point in to_lyr that will have its geometry duplicated | |
– Select the rows in from_lyr that you want to be inserted into to_lyr | |
''' | |
def duplicateAssets(to_lyr): | |
# Matching point is selected in to_lyr | |
geom = [geo[0] for geo in arcpy.da.SearchCursor(to_lyr, "SHAPE@")][0] | |
# Create insert cursor on same layer | |
inscursor = arcpy.da.InsertCursor(to_lyr, ("SHAPE@", "Unique_ID")) | |
with arcpy.da.UpdateCursor(from_lyr, ("Unique_ID", "Matched")) as cursor: | |
for row in cursor: | |
# Build the new point to be inserted | |
point = [geom, row[0]] | |
inscursor.insertRow(point) | |
print("Inserted " +str(int(row[0]))) | |
# Update the record with the name of the layer the point was inserted into | |
row[1] = to_lyr.datasetName | |
cursor.updateRow(row) | |
del inscursor | |
duplicatePoint(out_lyr) |