Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Programming ArcGIS 10.1 with Python Cookbook

You're reading from   Programming ArcGIS 10.1 with Python Cookbook This book provides the recipes you need to use Python with AcrGIS for more effective geoprocessing. Shortcuts, scripts, tools, and customizations put you in the driving seat and can dramatically speed up your workflow.

Arrow left icon
Product type Paperback
Published in Feb 2013
Publisher Packt
ISBN-13 9781849694445
Length 304 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Eric Pimpler Eric Pimpler
Author Profile Icon Eric Pimpler
Eric Pimpler
Arrow right icon
View More author details
Toc

Table of Contents (21) Chapters Close

Programming ArcGIS 10.1 with Python Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
1. Fundamentals of the Python Language for ArcGIS 2. Writing Basic Geoprocessing Scripts with ArcPy FREE CHAPTER 3. Managing Map Documents and Layers 4. Finding and Fixing Broken Data Links 5. Automating Map Production and Printing 6. Executing Geoprocessing Tools from Scripts 7. Creating Custom Geoprocessing Tools 8. Querying and Selecting Data 9. Using the ArcPy Data Access Module to Select, Insert, and Update Geographic Data and Tables 10. Listing and Describing GIS Data 11. Customizing the ArcGIS Interface with Add-Ins 12. Error Handling and Troubleshooting Automating Python Scripts Five Things Every GIS Programmer Should Know How to Do with Python Index

Using sys.argv[ ] to capture command-line input


Instead of hardcoding your scripts with paths to specific datasets, you can make your scripts more flexible by allowing them to accept input in the form of parameters from the command prompt. These input parameters can be captured using Python's sys.argv[] object.

Getting ready

Python's sys.argv[] object allows you to capture input parameters from the command line when a script is executed. An example is useful for illustrating how this works. Take a look at the following screenshot:

Each word must be separated by a space. These words are stored in a zero-based list object called sys.argv[]. With sys.argv[], the first item in the list, referenced by index 0, stores the name of the script. In this case, it would be ListFields.py. Each successive word is referenced by the next integer. Therefore, the first parameter (c:\ArcpyBook\data) will be stored in sys.argv[1], and the second parameter (Burglaries.shp) will be stored in sys.argv[2]. Each of the arguments in the sys.argv[] object can be accessed and used inside your geoprocessing script. In this recipe, you're going to update the ListFields.py script to accept input parameters from the command line.

How to do it...

Follow these steps to create a Python script that can accept input parameters from the command prompt using sys.argv[]:

  1. Open C:\ArcpyBook\Appendix1\ListFields.py in IDLE.

  2. Import the sys module:

    import arcpy, sys
  3. Create a variable to hold the workspace that will be passed into the script:

    wkspace = sys.argv[1]
  4. Create a variable to hold the feature class that will be passed into the script:

    fc = sys.argv[2]
  5. Update the lines of code that set the workspace and call the ListFields() function:

    arcpy.env.workspace = wkspace
    fields = arcpy.ListFields(fc)

    Your completed script should appear as follows:

    import arcpy, sys
    wkspace = sys.argv[1]
    fc = sys.argv[2]
    try:
      arcpy.env.workspace = wkspace
      fields = arcpy.ListFields(fc)
      for fld in fields:
        print fld.name
    except:
      print arcpy.GetMessages()
  6. Save the script.

  7. If necessary, open the command prompt and navigate to c:\ArcpyBook\Appendix1.

  8. On the command line, type the following and press on the Enter key:

    python ListFields.py c:\ArcpyBook\data Burglaries_2009.shp
  9. Once again you should see the output detailing the attribute fields for the Burglaries_2009.shp file. The difference is that your script no longer has a hardcoded workspace and feature class name. You now have a more flexible script capable of listing the attribute fields for any feature class.

How it works...

The sys module contains a list of objects called argv[], which is used to store the input parameters for the command-line execution of a Python script. The first item stored in the list is always the name of the script. So, in this case, sys.argv[0] contains the word ListFields.py. Two parameters are passed into the script, including the workspace and a feature class. These are stored in sys.argv[1] and sys.argv[2] respectively. These values are then assigned to variables and used in the script.


lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image