Other posts in this series:
Scripting Your ArcGIS Geoprocessing Tasks (Part 1)
Scripting Your ArcGIS Geoprocessing Tasks with Cursors (Part 2)
More ArcGIS Geoprocessing Concepts – Enumeration Objects (Part 3)
Obtaining Descriptive Information About GIS Datasets with ArcGIS Geoprocessing Objects (Part 4)
Executing ArcGIS Tools from Geoprocessing Scripts (Part 5)
Through the use of parameters, also known as arguments, you can pass data into your geoprocessing scripts. The use of parameters makes your scripts more generic and flexible since they allow you to provide for the dynamic input of datasets, tool parameters, or to control the processing logic of a script. For example, if you had a geoprocessing script that called the Buffer tool you would need to specify the input features, output feature class, and a buffer distance at a minimum. To make your script more flexible you could take advantage of these dynamic features to input variable data sources and buffer distances depending upon the circumstance.
In this post we will examine two methods for passing data into your scripts. The first, GetParameterAsText( ) is a method on GpDispatch that allows you to pass parameters into your scripts through a tool dialog available through ArcToolbox. We’ll also look at Python’s sys module which can also be used to pass data into a geoprocessing script.
The GetParameterAsText method on the GpDispatch object can only be used with ArcToolbox. The advantage of using this method is that it provides for the use of a visual dialog that accepts input data from the user. This input data is then retrieved inside your geoprocessing script through the GetParameterAsText method. GetParameterAsText is zero based, with the first argument located at position zero, and each successive argument increments to the next largest integer (see figure below).
There are a number of advantages to running your scripts from ArcToolbox using GetParameterAsText. When you take this approach, the script becomes part of the geoprocessing framework which means that it can be run from a model, ArcGIS command line or another script. In addition, the script has access to ArcMap environment settings and help documentation. Other advantages include a nice user interface that is easier to use, and error prevention capabilities. Error prevention capabilities provided include a dialog box that informs the user of certain errors.
There are a couple negatives to using GetParameterAsText that I should point out. As I mentioned above GetParameterAsText can only be used within ArcToolbox which means that you can’t execute a script using this method from PythonWin or a DOS prompt. One additional negative to consider with GetParameterAsText is that it is more difficult to debug the scripts since they can’t be tested from PythonWin.
Python system Module
Python, like other scripting languages, provides a mechanism for accessing arguments passed into the script. Python’s system, or sys, module is used to accomplish this task. Specifically, the sys.argv list contains the name of the script being executed along with any parameters being passed into the script. As with any other list in Python you can access the parameters through an integer value. This list, like GetParameterAsText, is zero based. The difference is that the first item in the list references the script that is being run whereas with GetParameterAsText, the first item references an actual parameter value. So, let’s take a look at a quick example.
In this example we use Python from a command line prompt to start a script named PointDistance.py. In addition, we pass in three arguments to the script by including them on the same line, but separated by a space. The sys.argv function stores each of the arguments in a list which we can access. Notice that the first argument, referenced by the integer 0 contains the name of the script, PointDistance.py. The arguments that are passed into the script can then be referred to by each successive integer. Each of these arguments can then be used in your script as necessary.
A similar script called from ArcToolbox:
We will be presenting a new Virtual GIS Classroom course, “GIS Programming 101: Mastering Python for Geoprocessing in ArcGIS” starting March 24th and ending April 18th. We still have 1 seat available so hurry to reserve your seat now. We do offer a 15% discount for government, educational, and non-profit entities.
We also offer an e-learning course entitled “Introduction to Geoprocessing with Python” which contains even more detailed information on ArcGIS geoprocessing with approximately 200 slides of audio and video lectures, visual software demonstrations, exercises and data.
Read Full Post
| Make a Comment ( None so far )
Over the next few weeks I’m going to be writing a series of posts on some of the lesser know tools provided by Google for creating data layers in Google Earth and Google Maps. The Google Earth Outreach program provides a number of these tools. According to a press release from early last summer, Google Earth Outreach is “designed to help nonprofit organizations around the world leverage the power of Google Earth to illustrate and advocate the important work they do.” The program includes comprehensive online guides, video tutorials, and case studies about using Google Earth specifically targeted to the needs of nonprofit organizations. In addition, organizations can also apply for a Google Earth Pro grant ($400 value).
In this initial post we’ll take a look at the Spreadsheet Mapper 2.0 tool created by the Outreach team for creating Google Earth and Google Maps placemark layers using Google Docs. Google Docs is a great way to create, share and collaborate on documents, spreadsheets and presentations online. Spreadsheet Mapper takes advantage of this online, collaborative environment by allowing you to create placemark layers for display in Google Earth and Google Maps through a spreadsheet created in Google Docs. Because Google Docs is a collaborative tool, members of your team can simultaneously enter data and instantly publish updates to GE and GMaps. So, let’s take a look at how this is done through a basic example.
In this example we’re going to create a placemark KML file containing existing Starbucks locations. The Starbucks data contains a unique identifer for each location along with the latitude, longitude coordinate pair and a physical address. This data is contained within the “Starbucks No Linefeeds.csv” file which was last updated in January 2008. This file contains almost 9,000 Starbucks locations. Since there are so many locations I’m only going to use a sample area for this example. The download also contains a Starbucks.bmp image which we’ll use in our next post in the series which covers Spreadsheet Mapper templates for styling icons and information balloons. Let’s get started. By default, Spreadsheet Mapper will use various templates to create some sample data. We’ll do this first and then replace the sample data with our own Starbucks location data.
Open the starter spreadsheet
. If you haven’t already done so you’ll need to login to your Google account or create one if necessary.
Select File –> Rename and give you spreadsheet a name (“Starbucks Locations”).
Fill in the “Author’s Information” and “About your KML Document” sections.
Click the Publish tab and select Publish Now. This step will publish the document to the web at the URL listed.
Copy the publisher URL and paste it into the white cell provided under “Publish spreadsheet”.
Copy the “Network Link KML” cell that you see below, open Google Earth, select My Places, then right-click and paste.
capability in Google Earth provides for the delivery of dynamic data to your users. We are using a Google Docs spreadsheet which can be edited by multiple users simultaneously. Network Links in Google Earth are a perfect complement to a Google Docs spreadsheet since they can automatically refresh the Google Earth display to reflect updated data from a spreadsheet. Get more information on Network Links in our “Mastering KML in Google Earth
” e-learning course.So, at this point we’ve copied the sample data contained in the template into Google Earth. You will notice a variety of folders, placemark icons and information balloons which have been created based on parameters found in the templates contained within Spreadsheet Mapper.
The Spreadsheet Mapper comes with six templates that can be used to control icon and balloon styles. Click any of the links at the bottom of the spreadsheet to get more information about each of these templates. We’ll cover detailed information about the templates in a coming slide, but for now you can get an idea of how they are structured.
At this point you’d want to prepare your template values. However, as I mentioned above I’m going to save the details of altering the template values for another post since it really deserves a thorough explanation.
Now let’s add in the Starbucks data. Open the “Starbucks No Linefeeds” Excel spreadsheet which contains the latitude,longitude, identification, and address values for each Starbucks location. Due to the large size of the file I’m only going to add in Starbucks locations for my city, San Antonio. Go to the PlacemarkData link at the bottom of the spreadsheet.
The Folder Name is optional, but in this case we’re going to define the folder name as “Starbucks” for each placemark instance. What this will do is group the placemarks under the same Folder in Google Earth. This would be helpful if you’d like to group Starbucks locations by city (i.e. San Antonio, Dallas, Houston, Austin).
For the Placemark Name column I’m going to add in the unique identifier for each location based on the information pulled from the Excel spreadsheet we downloaded. You should be able to copy and paste the data from MS Excel to the Google Docs spreadsheet to save time.
We’ll also enter the latitude, longitude values for each Starbucks location, also pulled from the Excel spreadsheet we downloaded. If you don’t have coordinate values for each placemark you can enter an address which can be used to generate the placemark.
Finally, we specify a template (#5 in this case) to define our icon and balloon styles. The spreadsheet should look something like you see below.
Click Publish –> Re-publish document in your Google Docs spreadsheet. Then refresh your network link in Google Earth by right clicking on “Link to – Spreadsheet” in the Places panel and selecting Refresh. This should refresh the Google Earth display with the new Starbucks locations we entered in the Google Docs spreadsheet.
In our next post we’ll cover the templates provides by Spreadsheet Mapper and also take a look at how you can create your own templates.
Read Full Post
| Make a Comment ( 4 so far )