-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextractservice.py
416 lines (333 loc) · 17.6 KB
/
extractservice.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#-------------------------------------------------------------------------------
# Name: Portal Feature Copier
# Purpose: This script extracts features and their attachments stored within
# ArcGIS Online or Portal. It then strips any downloaded relationship
# drops the attached table and then projects the feature class into its
# target projection. Mail notifications for failures and completion are
# included in the process.
#
# Author: John Spence
#
# Created: 8/25/2020
# Modified:
# Modification Purpose:
#
#
#-------------------------------------------------------------------------------
# 888888888888888888888888888888888888888888888888888888888888888888888888888888
# ------------------------------- Configuration --------------------------------
# Pretty simple setup. Just change your settings/configuration below. Do not
# go below the "DO NOT UPDATE...." line.
#
# 888888888888888888888888888888888888888888888888888888888888888888888888888888
# Project Store Location
path_Proj = r'C:\ProjectPath\project.aprx'
# Data Store Location
path_DataProcessing = r'C:\ProjectPath\Pull-Processing.sde'
path_DataFinal = r'C:\ProjectPath\Pull-Final.sde'
# Target Projection
targetSRID = 6597
# Send confirmation of rebuild to
email_target = '[email protected]'
email_customer = '[email protected]'
email_subjectSchema = '' #Whatever you want the subject to be here.
# Configure the e-mail server and other info here.
mail_server = 'smtprelay.gis.dev'
mail_from = 'Message<[email protected]>'
# Portal Configs
portalURL = 'https://www.arcgis.com'
portalUser = '' #Your User Name
portalPW = '' #Your Password encoded Base64. Remove decode if you are not practicing security by obscurity.
# ------------------------------------------------------------------------------
# DO NOT UPDATE BELOW THIS LINE OR RISK DOOM AND DISPAIR! Have a nice day!
# ------------------------------------------------------------------------------
import arcpy, datetime, smtplib, string, os, base64
# ------------------------------------------------------------------------------
# Functions
# ------------------------------------------------------------------------------
def main(portalURL,portalUser,portalPW):
#-------------------------------------------------------------------------------
# Name: Function - main
# Purpose: Starts the whole thing.
#-------------------------------------------------------------------------------
connect(portalURL,portalUser,portalPW)
findInfo()
findProj()
sendFinish()
return
def connect(portalURL,portalUser,portalPW):
#-------------------------------------------------------------------------------
# Name: Function - connect
# Purpose: Logs into ArcGIS online for secured applications.
#-------------------------------------------------------------------------------
try:
portalPWd = base64.b64decode(portalPW)
arcpy.SignInToPortal(portalURL, portalUser, portalPWd)
print (' Portal sign-on success...')
except:
print (' WARNING - Unable to log on to Portal. Please check your settings and try again.')
return
def findInfo():
#-------------------------------------------------------------------------------
# Name: Function - findInfo
# Purpose: Looks up information about the databases and stores it for use.
#-------------------------------------------------------------------------------
try:
desc = arcpy.Describe(path_DataProcessing)
dbNameTemp = desc.connectionProperties.database
dbName.append(dbNameTemp)
dbUserTemp = desc.connectionProperties.user
dbUser.append(dbUserTemp)
except:
print ('FAILURE: Unable to snag info from {}'.format(path_DataProcessing))
mail_priority = '1'
mail_subject = 'Portal Feature Copier Failure: Find Info'
mail_msg = ('A failure to find info has occured. The error is as follows: \n\n' +
'Error: Unable to snag info from {}'.format(path_DataProcessing))
sentMailNote(mail_priority, mail_subject, mail_msg)
exit()
try:
desc = arcpy.Describe(path_DataFinal)
dbNameTemp = desc.connectionProperties.database
dbName.append(dbNameTemp)
dbUserTemp = desc.connectionProperties.user
dbUser.append(dbUserTemp)
except:
print ('FAILURE: Unable to snag info from {}'.format(path_DataFinal))
mail_priority = '1'
mail_subject = 'Portal Feature Copier Failure: Find Info'
mail_msg = ('A failure to find info has occured. The error is as follows: \n\n' +
'Error: Unable to snag info from {}'.format(path_DataFinal))
sentMailNote(mail_priority, mail_subject, mail_msg)
exit()
print (dbName, dbUser)
return
def findProj():
#-------------------------------------------------------------------------------
# Name: Function - findProj
# Purpose: Pulls up the ArcGIS Pro Project and begins the extraction process.
#-------------------------------------------------------------------------------
aprx = arcpy.mp.ArcGISProject(path_Proj)
for maps in aprx.listMaps():
mapName = maps.name
currentDT = datetime.datetime.now()
print ('Publish Task Started: {}'.format(str(currentDT)))
print (' Map name: {}'.format(mapName))
m_var = '{}'.format(mapName)
m = aprx.listMaps(m_var)[0]
for lyr in m.listLayers('*'):
if lyr.isGroupLayer == False and lyr.isBasemapLayer == False:
print (' \nTarget ID\'d as: {}'.format(lyr))
extractService(lyr)
currentDT = datetime.datetime.now()
return
def findExists_SCRATCH(target):
#-------------------------------------------------------------------------------
# Name: Function - findExists_Scratch
# Purpose: Checks if the item that is going to be copied exists in the scratch
# DB prior to copying in.
#-------------------------------------------------------------------------------
arcpy.env.workspace = path_DataProcessing
fcTarget = dbUser[0] + '.' + target
if arcpy.Exists(fcTarget):
print (' Found {}'.format(fcTarget))
targetDesc = path_DataProcessing + '\\' + fcTarget
desc = arcpy.Describe(targetDesc)
fcTarget_REL = desc.relationshipClassNames
if fcTarget_REL:
print (' Relationship Detected.')
print (' ',fcTarget_REL)
for RELItem in fcTarget_REL:
RelDescription = arcpy.Describe(RELItem)
print('%-25s %s' % (' Backward Path Label:', RelDescription.backwardPathLabel))
print('%-25s %s' % (' Cardinality:', RelDescription.cardinality))
print('%-25s %s' % (' Class key:', RelDescription.classKey))
print('%-25s %s' % (' Destination Class Names:', RelDescription.destinationClassNames))
print('%-25s %s' % (' Forward Path Label:', RelDescription.forwardPathLabel))
print('%-25s %s' % (' Is Attributed:', RelDescription.isAttributed))
print('%-25s %s' % (' Is Composite:', RelDescription.isComposite))
print('%-25s %s' % (' Is Reflexive:', RelDescription.isReflexive))
print('%-25s %s' % (' Key Type:', RelDescription.keyType))
print('%-25s %s' % (' Notification Direction:', RelDescription.notification))
print('%-25s %s' % (' Origin Class Names:', RelDescription.originClassNames))
for RELTable in RelDescription.destinationClassNames:
print ('\n')
print (' Deleting: {}'.format(RELTable))
arcpy.Delete_management(RELTable)
print (' Deleting: {}'.format(RELItem))
arcpy.Delete_management(RELItem)
print (' Deleting: {}'.format(fcTarget))
arcpy.Delete_management(fcTarget)
else:
print (' No Relationship Detected.')
print ('\n')
print (' Deleting: {}'.format(fcTarget))
arcpy.Delete_management(fcTarget)
return
def findExists_PREP(target):
#-------------------------------------------------------------------------------
# Name: Function - Prep
# Purpose: Performs the same functions as scratch. Checks if it exists and
# deletes it prior to starting work.
#-------------------------------------------------------------------------------
arcpy.env.workspace = path_DataProcessing
fcTarget = dbUser[0] + '.' + target
if arcpy.Exists(fcTarget):
print (' Found {}'.format(fcTarget))
targetDesc = path_DataProcessing + '\\' + fcTarget
desc = arcpy.Describe(targetDesc)
fcTarget_REL = desc.relationshipClassNames
if fcTarget_REL:
print (' Relationship Detected.')
print (' ',fcTarget_REL)
for RELItem in fcTarget_REL:
RelDescription = arcpy.Describe(RELItem)
print('%-25s %s' % (' Backward Path Label:', RelDescription.backwardPathLabel))
print('%-25s %s' % (' Cardinality:', RelDescription.cardinality))
print('%-25s %s' % (' Class key:', RelDescription.classKey))
print('%-25s %s' % (' Destination Class Names:', RelDescription.destinationClassNames))
print('%-25s %s' % (' Forward Path Label:', RelDescription.forwardPathLabel))
print('%-25s %s' % (' Is Attributed:', RelDescription.isAttributed))
print('%-25s %s' % (' Is Composite:', RelDescription.isComposite))
print('%-25s %s' % (' Is Reflexive:', RelDescription.isReflexive))
print('%-25s %s' % (' Key Type:', RelDescription.keyType))
print('%-25s %s' % (' Notification Direction:', RelDescription.notification))
print('%-25s %s' % (' Origin Class Names:', RelDescription.originClassNames))
for RELTable in RelDescription.destinationClassNames:
print ('\n')
print (' Deleting: {}'.format(RELTable))
arcpy.Delete_management(RELTable)
print (' Deleting: {}'.format(RELItem))
arcpy.Delete_management(RELItem)
else:
print (' No Relationship Detected.')
return
def findExists_FINAL(target, path_Workspace):
#-------------------------------------------------------------------------------
# Name: Function - findExists_Final
# Purpose: Checks the final destination if an item exists.
#-------------------------------------------------------------------------------
arcpy.env.workspace = path_Workspace
fcTarget = dbUser[1] + '.' + target
if arcpy.Exists(fcTarget):
print (' Found {}'.format(fcTarget))
arcpy.Delete_management(fcTarget)
else:
print (' {} not found at target'.format(fcTarget))
return
def projectInto_FINAL(target):
#-------------------------------------------------------------------------------
# Name: Function - projectInto_Final
# Purpose: Projects the source in Scratch to the Final Destination
#-------------------------------------------------------------------------------
fcSource = dbName[0] + '.' + dbUser[0] + '.' + target
inFC = os.path.join(path_DataProcessing, fcSource)
fcTarget = dbName[1] + '.' + dbUser[1] + '.' + target
outFC = os.path.join(path_DataFinal, fcTarget)
out_coordinate_system = arcpy.SpatialReference(targetSRID)
try:
# Add editor tracking fields
arcpy.AddField_management(in_table=inFC, field_name="SysCreateDate", field_type="DATE", field_precision="", field_scale="", field_length="", field_alias="", field_is_nullable="NULLABLE", field_is_required="NON_REQUIRED", field_domain="")
arcpy.AddField_management(in_table=inFC, field_name="SysCreateUser", field_type="TEXT", field_precision="", field_scale="", field_length="80", field_alias="", field_is_nullable="NULLABLE", field_is_required="NON_REQUIRED", field_domain="")
arcpy.AddField_management(in_table=inFC, field_name="SysChangeDate", field_type="DATE", field_precision="", field_scale="", field_length="", field_alias="", field_is_nullable="NULLABLE", field_is_required="NON_REQUIRED", field_domain="")
arcpy.AddField_management(in_table=inFC, field_name="SysChangeUser", field_type="TEXT", field_precision="", field_scale="", field_length="80", field_alias="", field_is_nullable="NULLABLE", field_is_required="NON_REQUIRED", field_domain="")
# Enable editor tracking
arcpy.EnableEditorTracking_management(inFC, "SysCreateUser", "SysCreateDate", "SysChangeUser", "SysChangeDate", "NO_ADD_FIELDS","UTC")
except Exception as error_editorTracking:
print ('Status: Failure!')
print(error_editorTracking.args[0])
mail_priority = '1'
mail_subject = 'Portal Feature Copier Failure: Add editor tracking fields failed'
mail_msg = ('A failure to project into final has occured. The error is as follows: \n\n' +
'Error: {}'.format(error_editorTracking.args[0]))
sentMailNote(mail_priority, mail_subject, mail_msg)
exit()
try:
arcpy.Project_management(inFC, outFC, out_coordinate_system)
except Exception as error_project_layer:
print ('Status: Failure!')
print(error_project_layer.args[0])
mail_priority = '1'
mail_subject = 'Portal Feature Copier Failure: Projection Into Final'
mail_msg = ('A failure to project into final has occured. The error is as follows: \n\n' +
'Error: {}'.format(error_project_layer.args[0]))
sentMailNote(mail_priority, mail_subject, mail_msg)
exit()
return
def extractService(lyr):
#-------------------------------------------------------------------------------
# Name: Function - extractService
# Purpose: Runs through various layers in the project and extracts them into
# a single schema for use.
#-------------------------------------------------------------------------------
if '\\' in str(lyr):
target_split = str(lyr).split('\\', 1)
target = target_split[1]
else:
target = str(lyr)
target = target.replace(' ', '')
findExists_SCRATCH(target)
try:
arcpy.management.CopyFeatures(lyr, r'{}\{}'.format(path_DataProcessing, target), '', None, None, None)
print (' Status: Successful Copy Complete')
print (' Scrubbing data')
findExists_PREP(target)
print (' Status: Successful Data Scrub')
print (' Preparing final destination')
path_Workspace = path_DataFinal
findExists_FINAL(target, path_Workspace)
print (' Status: Final destination prepared')
print (' Preparing for projection to final destination')
projectInto_FINAL(target)
print (' Publishing complete for {}'.format(target))
except Exception as error_copy:
print (' Status: Failure!')
print (error_copy)
mail_priority = '1'
mail_subject = 'Portal Feature Copier Failure: Service Extraction'
mail_msg = ('A failure to extract a service from Portal has occured. The error is as follows: \n\n' +
'Error: {}'.format(error_copy))
sentMailNote(mail_priority, mail_subject, mail_msg)
exit()
return
def sentMailNote(mail_priority, mail_subject, mail_msg):
#-------------------------------------------------------------------------------
# Name: Function - sentMailNote
# Purpose: Universal use for both good and bad messages.
#-------------------------------------------------------------------------------
mail_msg = mail_msg + ('\n\n[SYSTEM AUTO GENERATED MESSAGE]')
mail_subject = email_subjectSchema + ' ' + mail_subject
# Set SMTP Server and configuration of message.
server = smtplib.SMTP(mail_server)
send_mail = 'To: {0}\nFrom: {1}\nX-Priority: {2}\nSubject: {3}\n\n{4}'.format(email_target, mail_from, mail_priority, mail_subject, mail_msg)
server.sendmail(mail_from, email_target, send_mail)
server.quit()
return
def sendFinish():
#-------------------------------------------------------------------------------
# Name: Function - sendFinish
# Purpose: Pops the I'm complete message via the sentMailNote function.
#-------------------------------------------------------------------------------
mail_priority = '5'
mail_subject = 'Portal Feature Staging Complete'
mail_msg = 'Feature extraction from Portal has completed.'
sentMailNote(mail_priority, mail_subject, mail_msg)
return
#-------------------------------------------------------------------------------
#
#
# MAIN SCRIPT
#
#
#-------------------------------------------------------------------------------
# Turn off history
arcpy.SetLogHistory(False)
# Set Overwrite Rules
arcpy.env.overwriteOutput = True
print ('***** Starting.....')
global worklist, dbName, dbUser, error_MSG
worklist = []
dbName = []
dbUser = []
if __name__ == '__main__':
main(portalURL,portalUser,portalPW)