-
Notifications
You must be signed in to change notification settings - Fork 0
/
Swap 2 Point Locations + attributes.py
76 lines (67 loc) · 2.37 KB
/
Swap 2 Point Locations + attributes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import arcpy
ws = arcpy.GetParameterAsText(0)
fc= arcpy.GetParameterAsText(1)
FID_field = arcpy.GetParameterAsText(2)
FID1 = arcpy.GetParameterAsText(3)
FID2 = arcpy.GetParameterAsText(4)
field_names = arcpy.GetParameterAsText(5)
fields = field_names.split(';')
workspace = ws
arcpy.AddMessage('workspace: '+workspace)
xyfields = ["SHAPE@X","SHAPE@Y"]
p = []
list1 = []
list2 = []
query = "{} = '{}'".format(FID_field, FID1)
with arcpy.da.SearchCursor(fc, ["SHAPE@XY"], where_clause=query) as search:
for i in search:
p += i
if field_names != '':
with arcpy.da.SearchCursor(fc, fields, where_clause=query) as search:
for i in search:
list1 += i
query = "{} = '{}'".format(FID_field, FID2)
with arcpy.da.SearchCursor(fc, ["SHAPE@XY"], where_clause=query) as search:
for i in search:
p += i
if field_names != '':
with arcpy.da.SearchCursor(fc, fields, where_clause=query) as search:
for i in search:
list2 += i
xy = [p[0][0],p[0][1],p[1][0],p[1][1]]
edit = arcpy.da.Editor(workspace)
edit.startEditing(False, True) #undo/redo; versioned data
edit.startOperation()
changetopoint2 = "{} = '{}'".format(FID_field, FID1)
with arcpy.da.UpdateCursor(fc, xyfields, where_clause=changetopoint2) as firstchange:
for first in firstchange:
first[0]=(xy[2])
first[1]=(xy[3])
firstchange.updateRow(first)
continue
if field_names != '':
with arcpy.da.UpdateCursor(fc, fields, where_clause=changetopoint2) as firstchange:
for first in firstchange:
for i in range(len(fields)):
first[i]=(list2[i])
firstchange.updateRow(first)
continue
changetopoint1 = "{} = '{}'".format(FID_field, FID2)
with arcpy.da.UpdateCursor(fc, xyfields, where_clause=changetopoint1) as secondchange:
for second in secondchange:
second[0]=(xy[0])
second[1]=(xy[1])
secondchange.updateRow(second)
continue
if field_names != '':
with arcpy.da.UpdateCursor(fc, fields, where_clause=changetopoint1) as secondchange:
for second in secondchange:
for i in range(len(fields)):
second[i]=(list1[i])
secondchange.updateRow(second)
continue
edit.stopOperation()
edit.stopEditing(True)
del search
del firstchange
del secondchange