Created
January 12, 2017 11:35
-
-
Save jontwo/17ebcbf2c67aff63a7532b8aba34137d to your computer and use it in GitHub Desktop.
Generic tool template for ArcGIS commandline script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ---------------------------------------------------------------------------- | |
# Name: My tool | |
# Purpose: TODO | |
# | |
# Author: Jon Morris | |
# | |
# Created: 2017 | |
# Copyright: MY COMPANY 2017 | |
# Licence: ArcGIS for Desktop Advanced | |
# ---------------------------------------------------------------------------- | |
import arcpy | |
class LicenseError(Exception): | |
pass | |
class MyTool(object): | |
def __init__(self): | |
"""Define the tool (tool name is the name of the class).""" | |
self.label = "My tool" | |
self.description = "TODO" | |
self.canRunInBackground = True | |
arcpy.env.overwriteOutput = True | |
# arcpy.CheckOutExtension("Spatial") | |
def isLicensed(self): | |
"""Set whether tool is licensed to execute.""" | |
# try: | |
# if arcpy.CheckExtension("Spatial") != "Available": | |
# raise LicenseError | |
# except LicenseError: | |
# return False # tool cannot be executed | |
return True # tool can be executed | |
def execute(self): | |
"""The source code of the tool.""" | |
try: | |
OUTWorkspace = arcpy.env.scratchGDB | |
MyParam = arcpy.GetParameterAsText(0) | |
arcpy.env.workspace = OUTWorkspace | |
arcpy.env.extent = arcpy.Extent(-180.0, -90.0, 180.0, 90.0) | |
arcpy.AddWarning('Generic warning') | |
arcpy.SetParameter(1, MyParam) | |
# Refresh Catalog | |
arcpy.RefreshCatalog(OUTWorkspace) | |
except Exception, e: | |
arcpy.AddError(e.message) | |
# finally: | |
# arcpy.CheckInExtension("Spatial") | |
if __name__ == '__main__': | |
tool = MyTool() | |
if tool.isLicensed(): | |
tool.execute() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Based on python toolbox, but intended to be run straight from command line. Results are not added to toolbox history though, see http://gis.stackexchange.com/questions/156628/saving-result-file-from-command-line-arcpy