Code: Alles auswählen
# coding: utf-8
import arcgisscripting, os.path
from datetime import datetime as datetimebase
# use forward slashes for directories - less hassle
# don't concatenate directories like 'c:/temp/' + 'test.shp' - use os.path.join,
# instead
# you can use relative paths or absolute paths
# relative path: . expands to the directory in which the script was started
# .. expands to the parent directory
IN_DIR = os.path.abspath(ur'./sample_dir/shp')
OUT_DIR = os.path.abspath(ur'./sample_dir/script_out')
# 'IN_MEMORY' workspace might come in handy for intermediate data
TMP_DIR = os.path.abspath(ur'./sample_dir/tmp')
VERBOSE = True # True or False - case sensitive!
# tells the geoprocessor to overwrite files
OVERWRITE = False # True or False - case sensitive!
DIRS = {'IN_DIR': IN_DIR, 'TMP_DIR': TMP_DIR, 'OUT_DIR': OUT_DIR}
class datetime(datetimebase):
def __str__(self):
return "%i.%i.%i %i:%i:%i" % (self.day, self.month, self.year,
self.hour, self.minute, self.second)
def run_tool(tool, *args):
'''run a geoprocessor tool and if VERBOSE == True tell us what\'s going on.
tool: for example gp.Intersect
arguments: all arguments necessary for running the tool in the right order
'''
if VERBOSE:
start = datetime.now()
print 'Running %s at %s' % (tool.__name__, start)
print 'Arguments:'
print '\t', args
tool(*args)
if VERBOSE:
end = datetime.now()
print "Finished %s at %s. Duration ~ %s " % (tool.__name__,
end, end - start)
if __name__ == '__main__':
# check existence of the user specified directories
error = False
for key in DIRS:
if not DIRS[key]:
# emptry string
continue
elif not os.path.exists(DIRS[key]):
print '%s %s does not exist.' % (key, DIRS[key])
error = True
else:
if error:
raise IOError('One ore more directories does not exist.')
gp = arcgisscripting.create(9.3)
if OVERWRITE:
gp.OverWriteOutput = True
if VERBOSE:
print 'geoprocessor initialisation complete.'
# your code goes here
#run_tool(gp.Intersect, ...)
# handy loop pattern
#cursor = gp.SearchCursor(.....)
#for row in iter(cur.next, None):
# do some stuff