Running ArcPy Scripts from the Python Command Line
Why?
Sometimes
there might not be a real reason to use a dialog box
We can still
get input into the script
A Simple Example
Open ArcMap
and either open an existing mxd or create a new one
If you created
a new one, save it in a convenient folder
Open Notepad
or Sublime, create a new file, and save it as HelloCommandLine.py in the same folder as your mxd
file (not necessary, but possibly convenient)
Add this text
to your file:
import arcpy
print 'loaded HelloCommandLine.py'
def greetings(name):
print 'Howdy ' + name + '!'
Open the
Python window in ArcMap and enter
>>>
execfile('PATH\ HelloCommandLine.py')
Where PATH is the pathname of the folder containing your Python script
For me, the
command looks like this:
>>>
execfile('r:\GOG530ArcPython\HelloCommandLine.py')
loaded HelloCommandLine.py
Now you can
run methods defined in the script like this:
>>>
greetings('fred')
Howdy fred!
Not much yet,
but we�ll build on this
The important
thing to note is that we load a script containing� functions, data structures, classes,
etc.
Then we can
call functions, fill data structures, and instantiate class objects from the
command line
A More Useful Example
Let�s create
a script that loads all shapefiles in a given folder
We need to
get a folder from the user and scour it for shapefiles
A good way to
do this will be to create a new method, LoadAllShapefiles(PATH), that takes a path to a specific folder
(I have
modified this ArcGIS
code sample found here as a base)
Copy and paste the code here into a Python
module called LoadAllMyShapefiles.py
You can download and uncompress
this set of 3 shapefiles to play with
Now load the
module using the execfile() command (substitute your own path):
>>>
execfile('r:\\gog530ArcPython\LoadAllMyShapefiles111517.py')
Here is the
only function in this module:
LoadAllMyShapefiles(mypath)
Calling this
function with a path loads and displays all shapefiles in the folder identified
by mypath
You could
call it like this:
>>>
LoadAllMyShapefiles('r:\\gog530ArcPython\\shapefiles')
And all the
shapefiles in r:\gog530ArcPython\shapefiles\ would be loaded onto the current
map
Let�s look at
the code line by line:
import arcpy # All
of our ArcGIS scripts will refer to this module
from arcpy import env # for setting the working folder
Looking at
the function LoadAllMyShapefiles(mypath)
env.workspace = mypath
Sets the
workspace to the folder containing the user�s shapefiles
fcList = arcpy.ListFeatureClasses()
Gets all the
shapefiles in the workspace, if any, and saves them in a list (fcList)
�� mxd
= arcpy.mapping.MapDocument("CURRENT")
�� df
= arcpy.mapping.ListDataFrames(mxd,
'')[0]
Old
stuff�get the current map document and dataframe 0
Now we can
loop through fcList to load our shapefiles into our
current map document:
for fc in fcList:
#
for each shapefile found in the folder,
#
create a complete path that includes the shapefile
pathToFC = env.workspace + '/'
+ fc
#
create a layer from the path
newlayer = arcpy.mapping.Layer(pathToFC)
#
add the layer to the dataframe
arcpy.mapping.AddLayer(df,newlayer,"BOTTOM")
Finishing up
(more familiar stuff):
�� #Refresh Table of Contents
�� arcpy.RefreshTOC()
�� #Refresh Active View
�� arcpy.RefreshActiveView()
Things for
you to try:
Implement a ZoomToLayer() function that is called with the layer name as a string
Implement a BringToTop() function that raises a layer to the top of the TOC by name
Write a DeleteAllLayers() function that deletes all layers in a map document (be
careful�)
Here is one of my attempts using for loops
Here is the same thing using dictionaries
(not entirely working yet)