I'm trying to write a script and as I want to automate it as much as possible.
Is it possible to set the workspace as a variable so that when it is run as a standalone script, or as a tool in ArcToolbox, that it will allow everything else to run?
Below, for example, I want the user to select a workspace and then for the script to pick up the shapefiles from this workspace and also create the new geodatabase there.
# Create file Geodatabase
# Import ArcGIS modules
import arcpy
print arcpy.ProductInfo()
# Check out the ArcGIS Spatial Analyst Extension
arcpy.CheckOutExtension("spatial")
# Need to be able to OverWrite Outputs
arcpy.env.overwriteOutput = True
# Set workspace
arcpy.env.workspace = "C:/Prog_Data"
# Set up variables
out_folder_path = "C:/Prog_Data"
out_name = "Prog.gdb"
# Execute CreateFileGDB
arcpy.CreateFileGDB_management(out_folder_path, out_name)
# Set local variables before importing shapefiles to Geodatabase
inFeatures = ["Points.shp", "Extent.shp", "Rivers.shp", "Population.shp"]
outLocation = "Prog.gdb"
# Execute shapefile to Geodatabase
arcpy.FeatureClassToGeodatabase_conversion(inFeatures, outLocation)
Answer
Rather than suggest modifications to your script, I am going to recommend that you consider adopting Esri's PythonTemplate which includes this functionality and more. The blog posting has since been removed but I'll leave the link in case it is ever put back. The code for Esri's template.py appears below.
#--------------------------------
# Name: template.py
# Purpose:
# Author:
# Created 23/06/2011
# Copyright: (c) company name
# ArcGIS Version: 10
# Python Version: 2.6
#--------------------------------
import os
import sys
import arcpy
def do_analysis(*argv):
"""TODO: Add documentation about this function here"""
try:
#TODO: Add analysis here
pass
except arcpy.ExecuteError:
print arcpy.GetMessages(2)
except Exception as e:
print e.args[0]
# End do_analysis function
# This test allows the script to be used from the operating
# system command prompt (stand-alone), in a Python IDE,
# as a geoprocessing script tool, or as a module imported in
# another script
if __name__ == '__main__':
# Arguments are optional
argv = tuple(arcpy.GetParameterAsText(i)
for i in range(arcpy.GetArgumentCount()))
do_analysis(*argv)
An alternative template that you may wish to review can be found in GitHub at https://github.com/brian32768/ArcGIS_Python_Template.
No comments:
Post a Comment