4

I need to move apart points with identical coordinates.

I have a point layer where there can be as high as 4 points on each other (same coordinate). I want to separate these points from each other (with a defined distance in x and y), using a pyscript. Right now, only the script's first part (Part 1) works, but then for some reason the script stops moving points at the second "cursor loop". Is there a better way to solve the task?

#------------------------------------------------------
#Enviroments
 import arcpy
 arcpy.env.overwriteOutput = True
 workspace = r"*"
 arcpy.env.workspace = workspace
 fc = "*"
 fields = ['SHAPE@X', 'SHAPE@Y']
 #Copy feature class:
 arcpy.CopyFeatures_management(fc, fc + "Copy")
 #Set FC to Copied data
 fcCopy = fc + "Copy"
 edit = arcpy.da.Editor(workspace)
 ----------------------Part 1----------------------
 Xlist1 = []
 Ylist1 = []
 edit.startEditing(False, False)
 edit.startOperation()
 with arcpy.da.UpdateCursor(fcCopy, fields) as cursor:
 for row in cursor:
 if row[0] in Xlist1 and row[1] in Ylist1:
 row[0] = row[0] + 10.0 #Move the point 10 meters in X
 if row[0] not in Xlist1 and row[1] not in Ylist1:
 Xlist1.append(row[0])
 Ylist1.append(row[1])
 cursor.updateRow(row)
 print(Xlist1, Ylist1)
 del row
 del cursor
 edit.stopOperation()
 edit.stopEditing(True)
 print("Script Part Done 1")
#----------------------Part 2 ----------------------
Xlist = []
Ylist = []
 edit.startEditing(False, False)
 edit.startOperation()
 with arcpy.da.UpdateCursor(fcCopy, fields) as cursor:
 for row in cursor:
 if row[0] in Xlist and row[1] in Ylist:
 row[1] = row[1] + 10.0 #Move the point 10 meters in Y
 if row[0] not in Xlist and row[1] not in Ylist:
 Xlist.append(row[0])
 Ylist.append(row[1])
 cursor.updateRow(row)
 del row
 del cursor
 edit.stopOperation()
 edit.stopEditing(True)
 print("Script Part Done 2")

From enter image description here

To:

enter image description here

asked Feb 6, 2019 at 12:23
2
  • Could you add a screenshot showing how you want the four coordiantes in the same spot to end up? Commented Feb 6, 2019 at 12:27
  • 1
    Hi, approx like this this Commented Feb 6, 2019 at 12:45

1 Answer 1

2

You have this set up correctly, where you essentially "skip over" the first point at a given location and then move subsequent points at that location. The problem I think is that your xlist and ylist are independent, so you could have a coordinate with no duplicates show up as being in there if EACH of its x and y coordinates matches another point. Also it's unnecessary to start an edit session as the da module will update the data directly without that.

Using your own approach, you could change "Part 1" to accomplish both x and y movements, and sidestep the issue of independent x and y lists with the zip command:

Xlist1 = Ylist1 = []
with arcpy.da.UpdateCursor(fcCopy, fields) as cursor:
 for row in cursor:
 if (row[0], row[1]) in zip(Xlist1, Ylist1):
 row[0] = row[0] + 10.0 #Move the point 10 meters in X
 row[1] = row[1] + 10.0
 cursor.updateRow(row)
 else:
 Xlist1.append(row[0])
 Ylist1.append(row[1])
 cursor.updateRow(row)
 print(Xlist1, Ylist1)
answered Feb 6, 2019 at 12:49

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.