7
7
#
8
8
# Created: 9/2/2020
9
9
#
10
- # Updated: 12/23/2021
10
+ # Updated: 9/21/2022
11
11
# -------------------------------------------------------------------------------------------------------------------
12
12
13
13
# Import system modules
14
14
import arcpy
15
15
import sys
16
16
import time
17
- import datetime
18
- import os
17
+ from datetime import date
18
+ from datetime import timedelta
19
+ from os import path
20
+ from os import environ
19
21
20
22
try :
21
23
# get timestamp for starting processing
25
27
service_name = 'Name of Service'
26
28
27
29
# date for the day the script is run on
28
- date_today = datetime . date .today ()
30
+ date_today = date .today ()
29
31
# Date formatted as month-day-year (1-1-2017)
30
32
formatted_date_today = date_today .strftime ('%m-%d-%Y' )
31
33
# date for how many days back you want to check for changes in a dataset
32
34
# change 8 to how many days back you want to check for changes
33
- date_ago = date_today - datetime . timedelta (days = 8 )
35
+ date_ago = date_today - timedelta (days = 8 )
34
36
35
37
# variable to store messages for log file. Messages written in finally statement at end of script
36
38
log_message = ''
37
39
# Create text file for logging results of script
38
- log_file = r'C:\GIS\Logs\Rebuild Map Tiles Report {}.txt' .format (
39
- formatted_date_today )
40
+ log_file = path .join (r'C:\GIS\Logs' , f'Rebuild Map Tiles Report { formatted_date_today } .txt' )
40
41
41
42
# layer you want to check for changes in
42
43
# there needs to be a Date field that captures when edits occur
51
52
# the format for queries using date fields changes based upon your data's format
52
53
# read the docs > https://pro.arcgis.com/en/pro-app/help/mapping/navigation/sql-reference-for-elements-used-in-query-expressions.htm
53
54
# replace "last_edited_date" with whatever field represents the date last modiefied
54
- where_clause = """last_edited_date >= date '{}' AND last_edited_date <= date '{}'""" .format (
55
- date_ago , date_today )
55
+ where_clause = f"""last_edited_date >= date '{ date_ago } ' AND last_edited_date <= date '{ date_today } '"""
56
56
57
57
# select features from reference layer that have been modified within your specified date range (i.e., within last week)
58
58
arcpy .SelectLayerByAttribute_management (
59
59
ref_lyr_file , 'NEW_SELECTION' , where_clause )
60
60
61
61
# get count of features
62
- count_selected_reference = arcpy .GetCount_management (tax_parcels_lyr )[0 ]
62
+ count_selected_reference = arcpy .GetCount_management (ref_lyr_file )[0 ]
63
63
# verify records have been selected; if not, add message and exit script
64
64
if count_selected_reference == 0 :
65
65
# add message
66
- log_message += 'No "Reference Layer" records have been modified between {} and {}\n ' .format (
67
- date_ago , date_today )
66
+ log_message += f'No "Reference Layer" records have been modified between { date_ago } and { date_today } \n '
68
67
# exit
69
68
sys .exit ()
70
69
83
82
# verify records have been selected; if not, add message and exit script
84
83
if count_selected_grids == 0 :
85
84
# add message
86
- log_message += 'No "Grid" features intersect "Reference Layer" records that have been modified between {} and {}\n ' .format (
87
- date_ago , date_today )
85
+ log_message += f'No "Grid" features intersect "Reference Layer" records that have been modified between { date_ago } and { date_today } \n '
88
86
# exit
89
87
sys .exit ()
90
88
94
92
arcpy .CopyFeatures_management (cache_grid_tiles_lyr , area_of_interest_lyr )
95
93
96
94
# add message
97
- log_message += 'Added selected "Grid" features to {}\n ' .format (
98
- area_of_interest_lyr )
95
+ log_message += f'Added selected "Grid" features to { area_of_interest_lyr } \n '
99
96
log_message += '\n Selected grids:\n \n '
100
97
101
98
# loop through Grid layer and list what records have been selected
102
99
# you can then use these as areas to check to verify your tiles have rebuilt the data
103
100
# replace 'LabelField' with a field in your Grid layer
104
101
with arcpy .da .SearchCursor (area_of_interest_lyr , 'LabelField' ) as cursor :
105
102
for row in cursor :
106
- log_message += '\t {} \n ' . format ( row [0 ])
103
+ log_message += f '\t { row [0 ]} \n '
107
104
108
105
# create feature set object
109
106
# see https://pro.arcgis.com/en/pro-app/arcpy/classes/featureset.htm
115
112
portal = ""
116
113
# user name of owner of item (admin users may be able to overwrite)
117
114
# create environment variable to store username; pass that variable name into get() method
118
- user = os . environ .get ('user_name_environment_variable' )
115
+ user = environ .get ('user_name_environment_variable' )
119
116
# password of owner of item (admin users may be able to overwrite)
120
117
# create environment variable to store pasword; pass that variable name into get() method
121
- password = os . environ .get ('password_environment_variable' )
118
+ password = environ .get ('password_environment_variable' )
122
119
123
120
# sign-in to Portal or ArcGIS Online
124
121
arcpy .SignInToPortal (portal , user , password )
139
136
# time in hours
140
137
elapsed_time_hours = round ((elapsed_time_minutes / 60 ), 2 )
141
138
142
- log_message += '\n \n Rebuilt cached tiles for {} in {}-hours on {}\n ' .format (
143
- service_name , elapsed_time_hours , formatted_date_today )
139
+ log_message += f'\n \n Rebuilt cached tiles for { service_name } in { elapsed_time_hours } -hours on { formatted_date_today } \n '
144
140
# If an error occurs running geoprocessing tool(s) capture error and write message
145
141
# handle error outside of Python system
146
142
except (Exception , EnvironmentError ) as e :
147
143
tbE = sys .exc_info ()[2 ]
148
144
# Write the line number the error occured to the log file
149
- log_message += '\n Failed at Line {}\n ' . format ( tbE . tb_lineno )
145
+ log_message += f '\n Failed at Line { tbE . tb_lineno } \n '
150
146
# Write the error message to the log file
151
- log_message += 'Error: {}' . format ( str (e ))
147
+ log_message += f 'Error: { str (e )} '
152
148
finally :
153
149
# write message to log file
154
150
try :
155
151
with open (log_file , 'w' ) as f :
156
152
f .write (str (log_message ))
157
153
except :
158
- pass
154
+ pass
0 commit comments