Main modules

GISPython package core modules

GISPythonModule

Main module, which contains frame for all GISPython package modules. Module allows the code unification, and ensures the code execution from both the ArcGIS Desktop Python console and the Command Prompt.

Module for the GISPython module frame and code unification

class GISPythonModule.GISPythonModule(ToolName, SysGISParams, ExecutePatch='/home/docs/checkouts/readthedocs.org/user_builds/gispython/checkouts/latest/GISPython/GISPythonModule.py', statusMailRecipients=[], errorMailRecipients=[], licenceLevel='arceditor', toollevel='full')

Bases: object

Interface class for all the GISPython modules. Interface class allows the code unification, and ensures the code execution from both the ArcGIS Desktop Python console and the Command Prompt.

Standalone tool execution:
SetName(‘Tool Name’) DoJob()
The tool executes within an another tool:
SetTool(reference to SysGISTools.GISTools10) Get the tool name with the command ‘PrintText()’ mainModule()
DoJob()

Procedure which runs the tool with environment preparation and deletion (in case the tool runs as a standalone tool)

MyDispose()

Procedure which closes the tool environment after running it (in case the tool runs as a standalone tool)

MyEnd()

Procedure for the tool end message output, if the tool runs as a standalone tool

PrintText()

The auxiliary procedure for the tool name output

SetTool(Tool)

Sets up the GISPython environment object, if the tool runs within an another tool

initModule()

Procedure which initializes the GISPython environment, if the tool runs as a standalone tool

mainModule()

Rewritable procedure which contains the logic of the module

runInsideJob(Tool)

Procedure executes the tool, if it’s to be executed within an another Python tool

Parameters:
  • self – The reserved object ‘self’
  • Tool – The tool name to execute
class GISPythonModule.GISPythonModuleArgsHelper(InitValue=None)

Bases: object

Class for handling the argument passing for the module in three different places:

  1. In the class initialization process.
  2. In the arguments.
  3. In the “main” operation call.
GetResultValue(asBool=False, Default=None)

Procedure makes a choice from the given attributes

Parameters:
  • self – The reserved object self
  • value – Setup value
SetInitValue(value)

Procedure processes a parameter setup from the tool initialization procedure

Parameters:
  • self – The reserved object ‘self’
  • value – Setup value
SetMainModuleValue(value)

Procedure processes a parameter setup from the base module of the tool

Parameters:
  • self – The reserved object ‘self’
  • value – Setup value
processArgument(argumentNumber=1)

Procedure processes a parameter acquisition from the argument

Parameters:
  • self – The reserved object ‘self’
  • argumentNumber – Argument from which to read the data

Examples

Executes the tool if it’s to be executed within an another Python tool:

from  GISPython import TimerHelper
import OnlineCompress

with TimerHelper.TimedSubprocess(Tool, u'Compress DB'): # Adds a message to the tool output
        with TimerHelper.TimedSubprocess(Tool, u'disconnect users from DB', 2): # Adds a message to the tool output
                self.Tool.RunSQL('KillUsers', Pr.u_sde, Pr.p_sde) # Runs custom SQL script
        OnlineCompress.MainModule(None, False).runInsideJob(Tool) # Runs SDE Compression procedure from OnlineCompress module (custom geoprocessing module)

GISPythonTool

Module defines abstract classes for the ESRI Toolbox tool definition and contains functions which helps to create an ArcGIS Toolbox, validates the tool’s parameter values and controls a behavior of the tool’s dialog.

Module defines abstract classes for the ESRI Toolbox tool definition

class GISPythonTool.GISPythonTool

Bases: object

Class which helps to create an ArcGIS Toolbox

execute(parameters, messages)

Executes a tool

getParameterInfo()

Define parameter definitions

isLicensed()

Determines if the tool is licenced for execution

updateMessages(parameters)

This method is called after an inner validation, and is intended for carrying out an additional validations

updateParameters(parameters)

This method is called in case the parameters are changed, and is used for setting up the parameters

class GISPythonTool.ToolValidator(parameters)

Bases: object

Class for validating the tool’s parameter values and controlling the behavior of the tool’s dialog.

initializeParameters()

Refine properties of the tool’s parameters. This method is called when the tool is opened.

updateMessages()

Modify the messages created by internal validation for each tool parameter. This method is called after internal validation.

updateParameters()

Modify the values and properties of parameters before internal validation is performed. This method is called whenever a parameter has been changed.

Examples

Define parameters in ESRI Python toolbox:

    from  GISPython import GISPythonTool

    class ToolAtributeValidator(GISPythonTool.GISPythonTool):
def __init__(self):
    """Define tool (tool name is the class name)"""
    self.label = u"Tool for attribute data validation (test mode)"
    self.description = u"Tool for attribute data validation (test mode)"
    self.category = 'GEO Maintenance'

def getParameterInfo(self):
    """Define the parameters"""
    param_0 = arcpy.Parameter(
        displayName=r'Key:',
        name=u"key",
        datatype=u"String",
        parameterType=u"Required",
        direction=u"Input")

    ret_val = [param_0]
    return ret_val

def execute(self, parameters, messages):
    """Tool execution"""
    AtributeValidator.MainModule(parameters[0].valueAsText).DoJob()
    return

GISPythonToolBase

Module contains geoprocessing tool operations and automation mechanisms for different operations.

Base class for GISPython Tool object

class GISPythonToolBase.GISPythonToolBase(ToolName, Params)

Bases: object

AchiveFiles(Dir, AchiveDir, FileName, PrintOut=True)

Function moves log files to the archive

Parameters:
  • self – The reserved object ‘self’
  • Dir – Directory from which to archive
  • AchiveDir – Archive directory
  • FileName – Parameter for searching a file (full or partial filename)
AddError(strMessage, newline=True)

Procedure for the GP object error message output (screen, logfile and if necessary e-mail)

Parameters:
  • self – The reserved object ‘self’
  • strMessage – Output message text
AddMessage(strMessage, newline=True)

Procedure for a message output (screen, logfile and if necessary e-mail)

Parameters:
  • self – The reserved object ‘self’
  • strMessage – Output message text
  • newline – Flag that marks that the output must be continued in a new line
AddWarning(strMessage, newline=True)

Procedure for the GP object warning message output (screen, logfile and if necessary e-mail)

Parameters:
  • self – The reserved object ‘self’
  • strMessage – Output message text
AutorizeNTWLocation(Adress, user, pwd)

Network directory authorization

Parameters:
  • self – The reserved object ‘self’
  • Adress – Network adress
  • user – Username
  • pwd – Password
CorrectStr(Str)

Function doubles symbol ‘’ in path for some external execution compatibility :param self: The reserved object ‘self’ :param Str: Input string

Returns:string
GetPS(Name, Path='#')

Function gets the PowerShell file location from the installation directory

Parameters:
  • self – The reserved object ‘self’
  • Name – Filename without an extension
GetSQL(Name)

Function gets the SQL file location from the installation directory

Parameters:
  • self – The reserved object ‘self’
  • Name – Filename without an extension
Returns:

SQL file full path

MyDateForParam(paramStr, includeTime=False)

Function returns date from GEO parameter processing saved string

Parameters:
  • self – The reserved object ‘self’
  • paramStr – Parameter value as text
  • includeTime – Include time in param (True/False)
Returns:

datetime

MyDateFromParam(dateString, includeTime=False)

Function converts date written in the parameter file to a date object

Parameters:
  • self – The reserved object ‘self’
  • includeTime – Include time in param (True/False)
MyDispose()

Disposal of the class

Parameters:self – The reserved object ‘self’
MyEnd()

End of the process

Parameters:self – The reserved object ‘self’
MyNow()

Function returns formatted date for the output

Parameters:self – The reserved object ‘self’
Returns:Date, formatted as text
MyNowFile()

Function returns formatted date for the filename output

Parameters:self – The reserved object ‘self’
Returns:Date, formatted as text
MyNowFileSafe()

Function returns formatted date for the filename output (additional compatibility)

Parameters:self – The reserved object ‘self’
Returns:Date, formatted as text
MyNowForParam(minusdays=0, includeTime=False)

Function returns formatted date (date now) for the GEO parameter processing

Parameters:
  • self – The reserved object ‘self’
  • minusdays – Number of days to subtract from today
  • includeTime – Include time in param (True/False)
Returns:

Date, formatted as text

MyNowOracle()

Function returns formatted date for the data selection in SQL

Parameters:self – The reserved object ‘self’
Returns:Date, formatted as text
MyNowUTC()

Function returns formatted date for the UTC date output

Parameters:self – The reserved object ‘self’
Returns:Date, formatted as text
RunPS(Name, Args, Path='#')

Procedure for the PowerShell file execution

Parameters:
  • self – The reserved object ‘self’
  • Name – Filename without an extension
  • Args – Arguments
RunSQL(Name, user='#', pwd='#', SpoolFile='#', ErrorStrings=['ERROR', 'FAILED', u'K\u013b\u016aDA', 'EXCEPTION', 'ORA-'], params=[], DBType='Oracle', hidenStrings=[], DBName='#', Detached=False)

Procedure for SQL file execution (only Oracle sqlplus supported) Typically used for execution, passing only SQL filename parameter

Parameters:
  • self – The reserved object ‘self’
  • Name – Filename without an extension
  • u – Username
  • p – Password
  • SpoolFile – SQL output
  • ErrorStrings – A list of keyword error strings that defines an error in the execution
  • params – aditional parameters
  • DBType – only Oracle is supported
  • hidenStrings – List of strings tha has to be hiden in the output (used for hiding passwords)
  • DBName – Oracle TNS name
  • Detached – Whether to execute seperately from the main process (Default: False)
_outputLines(lines, doMessges, noErr=False, ErrorStrings=['ERROR', 'FAILED', u'K\u013b\u016aDA', 'EXCEPTION', 'ORA-'], Silent=False, hidenStrings=[])

Procedure for outputing set of lines to screen with error key word recognition. (for example for log file output processing)

Parameters:
  • self – The reserved object ‘self’
  • lines – Lines to process
  • noErr – True ir no error logging is necesery
  • ErrorStrings – List or keywords with will be recognized as errors
  • Silent – if True no Errors will be rised
  • hidenStrings – List of strings tha has to be hiden in the output (used for hiding passwords)
_runProcess(exe, noErr=False, Detached=False, Silent=False, hidenStrings=[])

Shell command execution support function (see the runShell function)

Parameters:
  • self – The reserved object ‘self’
  • exe – Executable command
Returns:

stdoutdata

_tryCovertStringEncoding(txt)

Function for working with strings in diferent encodings. Converts string from input to string in correct encoding.

Parameters:
  • self – The reserved object ‘self’
  • txt – String to be converted
Returns:

converted string

outputLogfile(file, encoding='utf8', noErr=False, ErrorStrings=['ERROR', 'FAILED', u'K\u013b\u016aDA', 'EXCEPTION', 'ORA-'], Silent=False, hidenStrings=[])

Procedure prints text file to screent - processing error keywords

Parameters:
  • self – The reserved object ‘self’
  • file – path to file to process
  • noErr – True ir no error logging is necesery
  • ErrorStrings – List or keywords with will be recognized as errors
  • Silent – if True no Errors will be rised
  • hidenStrings – List of strings tha has to be hiden in the output (used for hiding passwords)
runShell(exe, noErr=False, ErrorStrings=['ERROR', 'FAILED', u'K\u013b\u016aDA', 'EXCEPTION', 'ORA-'], Detached=False, Silent=False, hidenStrings=[])

Shell command execution procedure. It can detect errors in execution and can output results to screen.

Parameters:
  • self – The reserved object ‘self’
  • exe – Executable command
  • noErr – ‘True’ indicates that the errors doesn’t have to be logged
  • ErrorStrings – List of error strings to look for in the output. Found error will be considered as an error in the execution process
  • Detached – Whether to execute seperately from the main process (Default: False)
  • hidenStrings – List of strings tha has to be hiden in the output (used for hiding passwords)
run_with_limited_time(func, args, kwargs, time)

Runs a function with time limit

Parameters:
  • self – The reserved object ‘self’
  • func – The function to run
  • args – The functions args, given as a tuple
  • kwargs – The functions args, given as a tuple
  • time – The time limit in seconds
Returns:

True if the function ended successfully. False if it was terminated.

Examples

Run a script from Shell with parameters:

Tool = self.Tool
# Executes your custom process script (mainly maintenance scripts) within runShell function,
# which implements _runProcess function (message output in terminal window).
# Function executes script seperately from main process (Detached=True) and indicates,
# that the errors doesn't have to be logged (noErr=True).
Tool.runShell('SomeProcess.py', Detached = True, noErr = True)
time.sleep(10) # after 10 seconds launch another runShell process

Executes a custom SQL script file (only Oracle sqlplus supported):

from GISPython import TimerHelper

Tool = self.Tool
# Executes process from SQL file
with TimerHelper.TimedSubprocess(self.Tool, u'datu atlasi no nogabaliem'): # Adds a message to the tool output
        Tool.RunSQL('LoadSomeDataFromTable') # Runs SQL file within RunSQL function, which implements GetSQL function (gets the SQL file location)

Duplicates path seperator symbol ‘’ for external execution compatibility:

from GISPython import SimpleFileOps
from GISPython import TimerHelper

Tool = self.Tool
# Your code
with TimerHelper.TimedSubprocess(Tool, u'prepare environment'): # Adds a message to the tool output
        DirHelper = SimpleFileOps.SimpleFileOps(Tool) # Set variable for SimpleFileOps module functions
        tmpFolder = os.path.join(Pr.TmpFolder, "Soil") # Set tmp folder path
        # Your code
        Tool.AddMessage(u'\n        ...delete previous tmp data') # Add tool output message
        DirHelper.CheckCreateDir(Tool.CorrectStr(tmpFolder)) # Check/create tmp directory (with modified path seperators)
        DirHelper.ClearDir(Tool.CorrectStr(tmpFolder)) # Clear tmp directory (with modified path seperators)

SysGISTools

Base module which contains GISPython scripting framework, geoprocessing message delivery, logging and error processing. Inherits GISPythonToolBase.

GIS function support module

class SysGISTools.GISTools10(ToolName, Params, licenceLevel='arceditor')

Bases: GISPythonToolBase.GISPythonToolBase

Class for storing the auxiliary batch processing and GIS geoprocesing functions

AddError(strMessage, newline=True)

Procedure for the GP object error message output (screen, logfile and if necessary e-mail)

Parameters:
  • self – The reserved object ‘self’
  • strMessage – Output message text
AddMessage(strMessage, newline=True)

Procedure for a message output (screen, logfile and if necessary e-mail)

Parameters:
  • self – The reserved object ‘self’
  • strMessage – Output message text
  • newline – Flag that marks that the output must be continued in a new line
AddWarning(strMessage)

Procedure for the GP object warning message output (screen, logfile and if necessary e-mail)

Parameters:
  • self – The reserved object ‘self’
  • strMessage – Output message text
MyEnd()

End of the process

Parameters:self – The reserved object ‘self’
OutputErrors()

Procedure to output messages and errors stored in the GP object. !!! Depricated - Left for backwards compatibility - use OutputMessages with ErrorSeverity 0 !!!

Parameters:self – The reserved object ‘self’
OutputMessages(ErrorSeverity=2)

Procedure to output messages stored in the GP object

Parameters:
  • self – The reserved object ‘self’
  • ErrorSeverity – Maximum Severity to report as error
callGP(functionName, *args)

Function to call the arcPy GP functions with automatic output messge display and output result returning

Parameters:
  • self – The reserved object ‘self’
  • functionName – Name of the arcPy GP function
  • args – arguments as Tuple
callGPSilent(functionName, *args)

Function to call the arcPy GP functions without output

Parameters:
  • self – The reserved object ‘self’
  • functionName – Name of the arcPy GP function
  • args – arguments as Tuple
class SysGISTools.MySR

Class for storing often used coordinate system parameters

SysGISToolsSysParams

Module for storing the scripting parameters

SysTools_unittest

Test procedure module

class SysTools_unittest.GISTools_unittest(methodName='runTest')

Bases: unittest.case.TestCase

GEOPython unit test class

setUp()

The Test setting up procedure

tearDown()

“The Test tear down - cleaning up objects after test

test_Tool_init()

Check if it is possible to get the geoprocessor object

test_shellRun()

Check the shell execution commands

class SysTools_unittest.Pr

Bases: object

Defines test parameters

ErrorLogDir = 'C:\\GIS\\Log\\ErrorLog\\'
ErrorLogDirArh = 'C:\\GIS\\Log\\ErrorLog\\Archive\\'
OutDir = 'C:\\GIS\\Log\\Outlog\\'
OutDirArh = 'C:\\GIS\\Log\\Outlog\\Archive\\'
encodingPrimary = 'cp1257'
encodingSecondary = 'cp775'