Skip to content

Commit 9b11cc8

Browse files
committed
fix error for unreferenced variable; update module imports; use formatted string literals
1 parent c7c5444 commit 9b11cc8

File tree

1 file changed

+20
-24
lines changed

1 file changed

+20
-24
lines changed

rebuild_map_service_tiles_in_updated_areas.py

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@
77
#
88
# Created: 9/2/2020
99
#
10-
# Updated: 12/23/2021
10+
# Updated: 9/21/2022
1111
# -------------------------------------------------------------------------------------------------------------------
1212

1313
# Import system modules
1414
import arcpy
1515
import sys
1616
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
1921

2022
try:
2123
# get timestamp for starting processing
@@ -25,18 +27,17 @@
2527
service_name = 'Name of Service'
2628

2729
# date for the day the script is run on
28-
date_today = datetime.date.today()
30+
date_today = date.today()
2931
# Date formatted as month-day-year (1-1-2017)
3032
formatted_date_today = date_today.strftime('%m-%d-%Y')
3133
# date for how many days back you want to check for changes in a dataset
3234
# 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)
3436

3537
# variable to store messages for log file. Messages written in finally statement at end of script
3638
log_message = ''
3739
# 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')
4041

4142
# layer you want to check for changes in
4243
# there needs to be a Date field that captures when edits occur
@@ -51,20 +52,18 @@
5152
# the format for queries using date fields changes based upon your data's format
5253
# read the docs > https://pro.arcgis.com/en/pro-app/help/mapping/navigation/sql-reference-for-elements-used-in-query-expressions.htm
5354
# 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}'"""
5656

5757
# select features from reference layer that have been modified within your specified date range (i.e., within last week)
5858
arcpy.SelectLayerByAttribute_management(
5959
ref_lyr_file, 'NEW_SELECTION', where_clause)
6060

6161
# 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]
6363
# verify records have been selected; if not, add message and exit script
6464
if count_selected_reference == 0:
6565
# 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'
6867
# exit
6968
sys.exit()
7069

@@ -83,8 +82,7 @@
8382
# verify records have been selected; if not, add message and exit script
8483
if count_selected_grids == 0:
8584
# 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'
8886
# exit
8987
sys.exit()
9088

@@ -94,16 +92,15 @@
9492
arcpy.CopyFeatures_management(cache_grid_tiles_lyr, area_of_interest_lyr)
9593

9694
# 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'
9996
log_message += '\nSelected grids:\n\n'
10097

10198
# loop through Grid layer and list what records have been selected
10299
# you can then use these as areas to check to verify your tiles have rebuilt the data
103100
# replace 'LabelField' with a field in your Grid layer
104101
with arcpy.da.SearchCursor(area_of_interest_lyr, 'LabelField') as cursor:
105102
for row in cursor:
106-
log_message += '\t{}\n'.format(row[0])
103+
log_message += f'\t{row[0]}\n'
107104

108105
# create feature set object
109106
# see https://pro.arcgis.com/en/pro-app/arcpy/classes/featureset.htm
@@ -115,10 +112,10 @@
115112
portal = ""
116113
# user name of owner of item (admin users may be able to overwrite)
117114
# 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')
119116
# password of owner of item (admin users may be able to overwrite)
120117
# 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')
122119

123120
# sign-in to Portal or ArcGIS Online
124121
arcpy.SignInToPortal(portal, user, password)
@@ -139,20 +136,19 @@
139136
# time in hours
140137
elapsed_time_hours = round((elapsed_time_minutes / 60), 2)
141138

142-
log_message += '\n\nRebuilt cached tiles for {} in {}-hours on {}\n'.format(
143-
service_name, elapsed_time_hours, formatted_date_today)
139+
log_message += f'\n\nRebuilt cached tiles for {service_name} in {elapsed_time_hours}-hours on {formatted_date_today}\n'
144140
# If an error occurs running geoprocessing tool(s) capture error and write message
145141
# handle error outside of Python system
146142
except (Exception, EnvironmentError) as e:
147143
tbE = sys.exc_info()[2]
148144
# Write the line number the error occured to the log file
149-
log_message += '\nFailed at Line {}\n'.format(tbE.tb_lineno)
145+
log_message += f'\nFailed at Line {tbE.tb_lineno}\n'
150146
# Write the error message to the log file
151-
log_message += 'Error: {}'.format(str(e))
147+
log_message += f'Error: {str(e)}'
152148
finally:
153149
# write message to log file
154150
try:
155151
with open(log_file, 'w') as f:
156152
f.write(str(log_message))
157153
except:
158-
pass
154+
pass

0 commit comments

Comments
 (0)