In an ideal world, the primary, most up to date spatial layers are accessed and maintained on an enterprise spatial database. Here on the little blue planet, a lot of folks still like to edit and access data as shapefiles.
I can’t say I blame them. Despite doing spatial DBA work on Postgres/PostGIS and SDE/SQL Server for years, I have a soft spot for shapefiles. For small to mid-size layers they draw faster, they’re easy to consume, easy to exchange, and they’re the only open format ESRI has. Just the inexplicable delay between double-clicking a SDE connection and seeing a list of layers makes me want to kick something. But having shapefiles that are out of sync with your enterprise database server is bad for everybody.
Here’s a little Python script I threw together to keep track of shapefile changes, which we use to let us know when SDE layers could be out of sync. It reads a comma delimited text file which is formatted <sde layer>, <path to shape file with no extension>, finds the most recent date of .shp/.dbf change, and can email a report or write it to a file, highlighting changes within a certain time period from the present date and shapefiles that have vanished (as they sometimes do).
import os import datetime import time import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText
# get file date defmodification_date(filename): return os.path.getmtime(filename)
# see if file exists deffile_exists(filename): return os.path.exists(filename)
# convert system time to date defconvert_time(t): datestamp = datetime.datetime.fromtimestamp(t) date_str = datestamp.strftime("%Y-%m-%d %H:%M:%S") return date_str
defprocess(list_of_lines): data_points = [] for line in list_of_lines: # split file line into array of sde name and shapefile path parts = line.split(",") # check to see if file exists if file_exists(parts[1].rstrip() + ".shp") : # get the latest time stamp for the shape file timestamp1 = modification_date(parts[1].strip() + ".shp") timestamp2 = modification_date(parts[1].strip() + ".dbf") timestamp = timestamp2 if timestamp1 > timestamp2 : timestamp = timestamp1 timestamp_rpt = convert_time(timestamp) else: timestamp_rpt = "File be gone!" timestamp = 0 data_point = [timestamp, parts[0], parts[1], timestamp_rpt] data_points.append(data_point) return data_points
######################################################### # Customize script here ######################################################### input_file = "shape_check.txt"# path to the file containing input parameters, format: sde name, path to shape file (no file extension) output_file = "shape_check.htm"# where to put output file when finished (optional) time_period = 30 * 86400# number of days x 86400 to highlight (yellow if modified within last x days) email_address = "you@yourcompany.com"# where to send email report (optional) (NO HOTMAIL)
# open file f = open(input_file, "r") data = f.readlines() f.close()