Professional Documents
Culture Documents
Ex 2
Ex 2
The city of Bismarck, North Dakota has a folder of newly created shapefiles. Because the shapefiles are new,
the GIS department needs a text file of these shapefiles added to the New Data folder so that they can be
reviewed. However, some of these shapefiles do not fit the standard naming template (ND_<data>.shp).
As data manager, you have decided to write a Python script that will create a list of these shapefiles; it will
later be used to update their names.
To complete the exercise, you need to download the data. If you have already downloaded and installed
the data, continue to the next step.
Because you will be writing and editing this script, you will use the PythonWin IDE environment.
Open PythonWin.
Remind me how
In Windows Explorer, navigate to C :\Python27\ArcGIS10.x\Lib\site-packages\pythonwin, then double-click
Pythonwin.exe. Tip: For easy access in the future, right-click Pythonwin.exe and choose Pin to Taskbar.
In the New dialog box, confirm that Python Script is highlighted, and then click OK to open a new
script window.
Step 2b: Set up script environment.
Your new script is named Script1 and appears in a new script window.
Remind me how
From the Window menu, choose Tile.
Click Save.
The scripting environment is now set up. You are ready to start writing your script.
Once the script environment is set up, you will write the pseudocode. Pseudocode is a description of the
ingredients and instructions you will use in your script. It helps guide you as you write the script and can
also help debug your script.
What ingredients will you need to create this script?
What data types will you need to use for the script ingredients?
Here, you will create variables for each of the shapefiles in the Bismarck folder.
Note: The hash mark indicates that this line will not run in the script, and it will serve as a
comment, or, in this case, pseudocode.
Here, you will create a list of the variables that you will assign under line 1.
You now have descriptions of the ingredients that this script will use.
After writing the pseudocode, the next step is to write the script.
park = "nd_park520.shp"
This is the line of code in your script that assigns the nd_park520.shpliteral to the variable named park.
Press Enter.
Scripts often need to combine and manipulate variables. One method used to manipulate variables is
concatenation. Concatenation uses the addition operator, +, to combine strings or string variables. In this
step, you will recreate the park shapefile using concatenation.
parkName = "nd_park"
Press Enter.
In the Interactive Window, pressing Enter runs the line of code that you just wrote.
printwrites the string value of the variable parkName. This is only part of the shapefile name, so you will
need a few other variables to manipulate.
In the Interactive Window, assign the shapefile extension, .shp, to a variable named shapeExt.
Next you will use the Python + operator to combine the two variables. The + operator will join, or
concatenate, the strings values.
In the Interactive Window, type and run the following line of code:
parkName + shapeExt
Note: If you have two numbers, whether literals or variables, and use the Python +
operator, the numbers will be added, not concatenated like strings.
Concatenating the parkNameand shapeExtvariables created a new string that was almost the full name
of the nd_park520.shp in the Bismarck folder. You only need to add the 520 to complete the shapefile
variable.
Assign the number 520to a variable named parkZip, and then press Enter.
Type and run the following line of code (it will return an error):
parkName + parkZip + shapeExt
The red Traceback message explains that you have encountered an error with this line of code.
Why can't you successfully concatenate the parkName, parkZip, and shapeExt variables?
In order to successfully concatenate these variables, you need to convert the parkZipvalue to a string.
There are several ways to do this. One option is to reassign the shapeExtvariable and enclose 520within
the quotation marks. The other is to use the str()function, which tells Python to use the string
equivalency of the value inside the parentheses.
You successfully recreated the park variable by using concatenation to manipulate variable values.
Other methods used to manipulate variables include indexing and slicing. In this step, you will recreate the
school variable using a combination of concatenation, indexing, and slicing.
In the Interactive Window, type and run the following line of code:
park[0]
You can also index characters from right to left; you can do this by starting with -1 and moving left.
park[-1]
Slicing can be used to return multiple characters by indicating a starting and ending point.
In the Interactive Window, type and run the following line of code:
park[7:10]
The colon separates the starting index with the ending index. Python will return the characters from the
starting point up to the ending point.
Note: [: and :]are used when you want to include the beginning and ending characters in
the returned value.
Use slicing and concatenation to combine schoolNamewith the park'sshapefile extension (".shp")
and assign it to a variable named school.
schoolName = "ND_Schools454"
print school
Step 6d: Index and slice variables.
These are a few examples of variable manipulation using concatenation, indexing, and slicing.
The script's next task (indicated in the pseudocode) is to create a list of shapefile variables.
In the script window, move your cursor to line 7, under #Create a list of shapefile
variables.
Press Enter.
In this line of code, shapeListis a variable assigned to a list of values. The list is indicated with the
bracket closures [], and each item in the list is separated by a comma.
How would you change the shapeListvariable to create a list of the shapefile literals,
instead of their variables?
Note: Like strings, lists can be indexed. Each item in the list has an index value—starting
with 0, moving left to right.
The ingredients have been included in the script. You will use this script later to update the list of
shapefile variables.
If you have time to complete the next exercise, keep the script open in PythonWin. Otherwise, exit
PythonWin.