Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Exercise: Create data types

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.

Estimated completion time: 20 minutes

Step 1: Download the data

To complete the exercise, you need to download the data. If you have already downloaded and installed
the data, continue to the next step.

Step 2: Set up script environment

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 PythonWin, click the New button .

Step 2a: Set up script environment.

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.

Tile the Script and Interactive Window.

Remind me how
From the Window menu, choose Tile.

From the File menu, choose Save As.

In the Save As window, navigate to your C:\Student\PythEveryone10_1\CreatingScripts folder.

Name the new script BismarckShapefiles.py.

Click Save.

The scripting environment is now set up. You are ready to start writing your script.

Step 3: Write pseudocode for the 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?

In the script window, type the following pseudocode:

#Assign variables to the shapefiles

Step 3a: Write pseudocode for the script.

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.

Press the Enter key twice to move to line 3 of the script.

On line 3, type the following pseudocode, and then press Enter:

#Create a list of shapefile variables

Step 3b: Write pseudocode for the script.

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.

Step 4: Assign the variables

After writing the pseudocode, the next step is to write the script.

In the script window, click next to line 2.

Type the following:

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.

Step 4a: Assign the variables.

Is the nd_park520.shp literal a string or number?

Assign ND_schools454.shpto a variable called school, and then press Enter.

Step 4b: Assign the variables.

Assign nd_sew454.shpto a variable named sewer, and then press Enter.

Step 4c: Assign the variables.

You now have variables for each of the shapefiles.

Step 5: Concatenate variables

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.

In the Interactive Window, type the following line of code:

parkName = "nd_park"

Press Enter.

In the Interactive Window, pressing Enter runs the line of code that you just wrote.

Type and run the following code:


print parkName

Step 5a: C oncatenate variables.

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

Step 5b: C oncatenate variables.

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.

Step 5c: C oncatenate variables.

Type and run the following line of code (it will return an error):
parkName + parkZip + shapeExt

Step 5d: C oncatenate variables.

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.

Type and run the following code:

park = parkName + str(parkZip) + shapeExt

Print the park variable.

Step 5e: C oncatenate variables.

You successfully recreated the park variable by using concatenation to manipulate variable values.

Step 6: Index and slice variables

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]

Step 6a: Index and slice variables.


In this example, you indexed the first character in the string nd_park520.shp, which is the character 'n'.

What line of code would you use to return the 'k'?

You can also index characters from right to left; you can do this by starting with -1 and moving left.

Type and run the following line of code:

park[-1]

Step 6b: Index and slice variables.

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]

Step 6c: Index and slice variables.

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.

What line of code would you use to return ".shp"?

In the Interactive Window, assign "ND_Schools454"to a variable named schoolName.

Use slicing and concatenation to combine schoolNamewith the park'sshapefile extension (".shp")
and assign it to a variable named school.

Print schoolto verify that it returns ND_Schools454.shp.

schoolName = "ND_Schools454"

school = schoolName + park[-4:]

print school
Step 6d: Index and slice variables.

These are a few examples of variable manipulation using concatenation, indexing, and slicing.

Step 7: Create a list

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.

Type the following line of code:

shapeList = [park, school, sewer]

Press Enter.

Step 7a: C reate a list.

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.

Why would you use a list instead of a tuple?

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.

Save the script.

If you have time to complete the next exercise, keep the script open in PythonWin. Otherwise, exit
PythonWin.

You might also like