-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin.py
178 lines (156 loc) · 8.38 KB
/
admin.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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
"""Overarching admin functions, like backups."""
import os.path as _path
import arcpy as _arcpy
import funclite.iolib as _iolib
class BackUps:
@staticmethod
def backup_sde(sde_file: str, out_gdb: str, overwrite: bool = False) -> None:
"""
Copies files from an SDE database to a GDB or SQLite Database (using
spatialite for spatial files). SQLite files must end in .sqlite to be
recognised by ArcGIS. Currently, rasters are only supported for GDB. With
overwrite set to True, any existing db is deleted and a full copy of the
SDE is made. If False, files within the database that are not present in
the output db are copied, and files where the number of features do not
match are replaced.
Args:
sde_file (str): Name of the database owner to copy.
out_gdb (str): Path for the output DB, which is created if it doesn't exist.
overwrite (bool): Select True to delete any existing GDB and create a new copy
Notes:
This script adapted from Shaun Astbury's scripts in UKCEH.
Temporaily changes workspace, then resets it back to the original
Credit:
This script was authored by Shaun Astbury who worked for CEH GMEP Project. It has subsequently been edited to run under arcgispro and Python >= 3.7
"""
# TODO Fix backup_sde up
sde_file = _path.normpath(sde_file)
if _arcpy.Exists(out_gdb) and overwrite:
_arcpy.management.Delete(out_gdb)
# Create new DB
if not _arcpy.Exists(out_gdb):
_arcpy.management.CreateFileGDB(dirname(out_gdb), basename(out_gdb))
current_ws = _arcpy.env.workspace
_arcpy.env.workspace = sde_file
# Iterate through tables in database, and attempt to copy any which do not
# already exist.
tbls = _arcpy.ListTables()
if tbls:
for tbl in tbls:
if tbl.split('.')[0] == owner:
path = osjoin(out_gdb, tbl.split('.')[1])
if _arcpy.Exists(path):
_arcpy.MakeTableView_management(tbl, "tbl")
in_count = int(
_arcpy.GetCount_management("tbl").getOutput(0))
_arcpy.Delete_management("tbl")
_arcpy.MakeTableView_management(path, "tbl")
out_count = int(
_arcpy.GetCount_management("tbl").getOutput(0))
_arcpy.Delete_management("tbl")
if in_count != out_count:
_arcpy.Delete_management(path)
if not _arcpy.Exists(path):
print(tbl)
try:
_arcpy.CopyRows_management(tbl, path)
except:
print('Failed importing {0}'.format(tbl))
# Iterate through rasters in database, and attempt to copy any which do not
# already exist.
rasters = _arcpy.ListRasters('*')
if rasters and not sqlite:
for raster in rasters:
if raster.split('.')[0] == owner:
path = osjoin(out_gdb, raster.split('.')[1].upper())
if not _arcpy.Exists(path):
print(raster)
try:
_arcpy.CopyRaster_management(raster, path)
except:
print('Failed importing {0}'.format(raster))
# Iterate through feature classes in database, and attempt to copy any
# which do not already exist.
feats = _arcpy.ListFeatureClasses()
if feats:
for feat in feats:
if feat.split('.')[0] == owner:
path = osjoin(out_gdb, feat.split('.')[1])
if _arcpy.Exists(path):
_arcpy.MakeTableView_management(feat, "tbl")
in_count = int(
_arcpy.GetCount_management("tbl").getOutput(0))
_arcpy.Delete_management("tbl")
_arcpy.MakeTableView_management(path, "tbl")
out_count = int(
_arcpy.GetCount_management("tbl").getOutput(0))
_arcpy.Delete_management("tbl")
if in_count != out_count:
_arcpy.Delete_management(path)
if not _arcpy.Exists(path):
print(feat)
try:
if sqlite:
add_sqlite_tbl(feat, out_gdb, True)
else:
_arcpy.CopyFeatures_management(feat, path)
except Exception as e:
print('Failed importing {0}:'.format(feat))
print('\t', e)
# Iterate through feature datasets in database, and attempt to copy any
# which do not already exist.
datasets = _arcpy.ListDatasets('*', 'Feature')
if datasets:
for dataset in datasets:
if dataset.split('.')[0] == owner:
# Create Feature Dataset if GDB.
if not sqlite:
path = osjoin(out_gdb, dataset.split('.')[1])
if not _arcpy.Exists(path):
_arcpy.CreateFeatureDataset_management(dirname(path),
basename(path))
# Set workspace to dataset.
_arcpy.env.workspace = osjoin(cnx, dataset)
# Iterate through feature classes in dataset, and attempt to
# copy any which do not already exist.
feats = _arcpy.ListFeatureClasses()
if feats:
for feat in feats:
if feat.split('.')[0] == owner:
if sqlite:
path = osjoin(out_gdb, feat.split('.')[1])
else:
path = osjoin(out_gdb,
dataset.split('.')[1],
feat.split('.')[1])
if _arcpy.Exists(path):
_arcpy.MakeTableView_management(feat, "tbl")
in_count = int(
_arcpy.GetCount_management(
"tbl").getOutput(0))
_arcpy.Delete_management("tbl")
_arcpy.MakeTableView_management(path, "tbl")
out_count = int(
_arcpy.GetCount_management(
"tbl").getOutput(0))
_arcpy.Delete_management("tbl")
if in_count != out_count:
_arcpy.Delete_management(path)
try:
# SQLite (and most other DBs) doesn't use
# Feature Datasets, so just export the file.
if sqlite:
if not _arcpy.Exists(path):
print(feat)
add_sqlite_tbl(feat, out_gdb, True)
# If GDB, include the dataset in the path.
else:
if not _arcpy.Exists(path):
print(feat)
_arcpy.CopyFeatures_management(feat,
path)
except Exception as e:
print('Failed importing {0}:'.format(feat))
print('\t', e)
# Reset workspace.
_arcpy.env.workspace = current_ws