Arcgis-Geoprocessing-Helferchen

Code-Stücke können hier veröffentlicht werden.
Antworten
pudeldestodes
User
Beiträge: 65
Registriert: Samstag 9. Juni 2007, 23:45

Da ich immer wieder Wegwerfcode zur ArcGIS-"Automatisierung" (9.3) schreibe, habe ich mir eine Art Schablone als Ausgangspunkt dafür erstellt. Mit dem vom Geoprocessor zurückgegebenen `result object` mache ich bisher noch gar nichts, insbesondere gibt es noch keine Fehlerbehandlung. Einige Sachen habe ich mir hier im Forum abgeschaut und der Sinn von run_tool ist schlicht die Möglichkeit einer formatierten Ausgabe des Geoprocessor-Funktionsaufrufs. Vielleicht kann's ja jemand gebrauchen, da ja doch der eine oder andere GIS-Nutzer im Forum umherschwirrt.

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
Edit: Hm, da hat die betriessystemübergreifende Editiererei wohl doch Folgen - ich kopiere unter Linux den Text korrekt hierher (keine zusätzlichen Leerzeilen), gehe auf Vorschau und da sind auf einmal ein paar Leerzeilen mehr. Mit meinem ungeschlagenen Halbwissen erinnere ich mich an '\r\n' <-> '\n' unter Windows <-> Linux für Zeilenumbrüche. Ob das damit zusammenhängt?
Antworten