By Mac11


2019-02-06 12:23:21 8 Comments

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 = ['[email protected]', '[email protected]']


    #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

1 comments

@AlecZ 2019-02-06 12:49:01

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)

Related Questions

Sponsored Content

2 Answered Questions

[SOLVED] Switching from Nested Search Cursors to Dictionaries

1 Answered Questions

[SOLVED] Creating points using ArcPy?

  • 2019-03-12 11:22:53
  • Velugoti Venkateswarlu
  • 47 View
  • 0 Score
  • 1 Answer
  • Tags:   arcpy point

1 Answered Questions

[SOLVED] Creating points with attributes from coordinates in ArcPy

3 Answered Questions

[SOLVED] Comparing value with value from the next row

  • 2019-01-22 19:51:24
  • Seb
  • 101 View
  • 3 Score
  • 3 Answer
  • Tags:   arcpy cursor

1 Answered Questions

[SOLVED] Stratified random point sampling in Python

2 Answered Questions

[SOLVED] QGIS "delete identical" points?

  • 2018-03-28 14:46:00
  • Toll
  • 98 View
  • 3 Score
  • 2 Answer
  • Tags:   qgis point

1 Answered Questions

[SOLVED] Trying to preserve attributes using SearchCursor

  • 2014-10-21 19:05:20
  • Squinney
  • 108 View
  • 2 Score
  • 1 Answer
  • Tags:   arcpy cursor

0 Answered Questions

Arcpy connecting points with lines using coordinates

  • 2017-05-17 14:48:14
  • Mike Chen
  • 58 View
  • 1 Score
  • 0 Answer
  • Tags:   arcpy

1 Answered Questions

[SOLVED] Update Cursor to Polyline Creates Records with Missing Geometry

  • 2016-08-12 21:24:38
  • spaine
  • 322 View
  • 0 Score
  • 1 Answer
  • Tags:   arcpy cursor line

1 Answered Questions

Sponsored Content