-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharcmapVersionedEditing.py
More file actions
126 lines (115 loc) · 5.12 KB
/
arcmapVersionedEditing.py
File metadata and controls
126 lines (115 loc) · 5.12 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
################################################################################
#
# Version: 1.0
#
# Creates a temporary sde file that is used to create a version of the feature
# classes listed for editing. If a feature class that you are trying to edit
# using arcpy is not set in the fcLST it will not be versioned. The name used
# in fcLST (without the user ex USER.FeatureClass = "FeatureClass") is how the
# feature class can be called by arcpy geoprocessing and editing tools.
#
# To run the tool in your script you will need to import the listed libraries
# and set the appropriate environment settings. There are also two examples
# listed at the end on how to call the functions. One with variables previously
# set and the other with variables set during the function call. The end editing
# function does not require any variables as it reuses stuff from the start
# editing function.
#
# The only things that I can think of that will make it fall apart are if you
# reuse a variable name somewhere else in your script or something with the
# global variable stuff as they were set in not necessarily the best way, but in
# a way that made sense to me and worked.
#
# This is a modified version of some code I found on gis.stackexchange.com to
# make it more plug and play.
#
# https://pro.arcgis.com/en/pro-app/help/data/geodatabases/overview/the-version-editing-process.htm
#
# Author: https://github.com/jtgis
#
# Date: 26 Nov 2019
#
################################################################################
import arcpy
import time
import os
# add these two functions to the beginning of your code
def createVersionStartEdit(fcLST,temploc,sdecon,user,password):
global version, workspace, edit, fcLSTglob, userglob
fcLSTglob = fcLST
userglob = user
version = "ARCPY"+time.strftime("%d%m%Y%I%M%S")
tempsdecon = "SDECONN"+time.strftime("%d%m%Y%I%M%S")
workspace = os.path.join(temploc,
tempsdecon+".sde")
arcpy.CreateVersion_management(sdecon,
"SDE.DEFAULT",
version,
"PRIVATE")
arcpy.CreateArcSDEConnectionFile_management(temploc,
tempsdecon,
" ",
"sde:oracle11g:somesde.world",
"",
"",
userglob,password,
"",
user+"."+version)
edit = arcpy.da.Editor(workspace)
edit.startEditing(False,True)
edit.startOperation()
for i in fcLSTglob:
editfeature = os.path.join(workspace,
i)
arcpy.MakeFeatureLayer_management(editfeature,
i.split(".")[1])
print("versioned editing started")
arcpy.AddMessage("versioned editing started")
return version, workspace, edit, fcLSTglob, userglob
def stopEditReconcileVersion():
edit.stopOperation()
edit.stopEditing(True)
for i in fcLSTglob:
arcpy.ChangeVersion_management(i.split(".")[1],
"TRANSACTIONAL",
"SDE.DEFAULT")
arcpy.ReconcileVersions_management(workspace,
"",
"SDE.DEFAULT",
userglob+"."+version,
"LOCK_ACQUIRED",
"NO_ABORT",
"BY_OBJECT",
"FAVOR_EDIT_VERSION",
"POST",
"DELETE_VERSION")
os.remove(workspace)
print("versioned editing ended")
arcpy.AddMessage("versioned editing ended")
################################################################################
if __name__ == '__main__':
arcpy.env.overwriteOutput = True
#you can call the functions like this
#ex1 using the predefined variables
#set variables
fcLST = ["username.fc1","username.fc2,etc"]
temploc = r"pathto\temploc"
sdecon = r"pathto\somesde.sde"
user = "username"
password = "password"
#start edit
createVersionStartEdit(fcLST,temploc,sdecon,user,password)
#do some stuff
#stop edit
stopEditReconcileVersion()
#ex2 hardcoding the values to the function
#start edit
createVersionStartEdit(["username.fc1","username.fc2,etc"],
r"pathto\temploc",
r"pathto\somesde.sde",
"username",
"password")
#do some stuff
#stop edit
stopEditReconcileVersion()
################################################################################