ArcPy Script Dialog Controls

A simple example of a control

The Add Script wizard provides some user interface tools for your scripts

In this example, fred is a parameter that gets its data from an input file browser

This makes life easier for the user who doesn�t want to type in a path to a file

When you run the script, this dialog appears:

In an accompanying script, �fred� would be represented by parameter 0

Various Python functions could be used to read data from the selected file

What exactly is a file?

In most modern operating systems, a file is considered to be a �backing store� or data source

Files are often implemented as coded sectors of disk or flash memory

Files can be local to a particular machine or remote

Remote data access is controlled by various protocols

Two of the most common are

Transmission Control Protocol (TCP)

Standard protocol for most internet data transfer

File Transfer Protocol (FTP)

Common protocol for transferring static files

We care more about how to read and write this data than how the storage technology works

Therefore, modern operating systems and input/output (IO) libraries provide high-level tools to hide the underlying complexity

For the programmer, reading data from a local file or from a remote website is quite similar

Common Python file access functions

For local backing stores, specify a file resource with open()

Windows example (creates a new file for writing (stuff.txt) in c:\temp):

>>> myFile = open('c:\\temp\\stuff.txt', 'wt')

Mac and Linux systems example (creates stuff.txt in the user�s home directory):

>>> import os

>>> myHome = os.path.expanduser('~/')

>>> myFile = open(myHome + '~/stuff.txt', 'wt')

You can write to the file (in any OS) like this:

>>> myFile.write('Howdy\nJim')

Always remember to close a file when you�re done:

>>> myFile.close()

Try opening stuff.txt in a text editor to see what happened

To read from a file, you must open it in read mode

If a file is currently open for writing, you must close it before opening it for reading

>>> myFile = open('c:\\temp\\stuff.txt', 'rt')

>>> print myFile.readline() # read line of code ending in newline

Howdy

>>> print myFile.readline()

Jim

>>> myFile.close()

You can open and read all the lines of a file at once like this:

>>> myFile = open('c:\\temp\\stuff.txt', 'rt')

>>> # read() dumps all the file data to a string

>>> # optional read argument specifies number of bytes to read

>>> # readlines() each data item to a list object

>>> contentString = myFile.read() # Good�only 1 IO operation

>>> myFile.seek(0)��������������� # Reposition file pointer to start

>>> contentList = myFile.readlines()

>>> print contentString

Howdy

Jim

>>> print contentList

['Howdy\n', 'Jim']

The possibilities are now endless�here is a mini �od� (octal dump) that prints out Unicode char values:

>>> print contentList

['Howdy\n', 'Jim']

>>> for c in contentString:

...���� print c, ord(c)

...

H 72

o 111

w 119

d 100

y 121

 

10

J 74

i 105

m 109

Reading from a web resource

You can open a URL, read its data, and print it in 5 lines of code:

>>> import urllib as ur # module for HTTP communication

>>> url = 'https://www.albany.edu' # specify URL

>>> conn = ur.urlopen(url)������� # request a connection

>>> data = conn.read()����������� # read data into a string

>>> print data������������������� # print the string

ArcPy provides multiple tools for reading specific types of files

ESRI�s shapefile format has been around since the 1990s

Example: create a script that opens a shapefile and then zooms to its extent

1.       In ArcMap, open an existing mxd or create a new one on your flash drive

2.       Add a script to your toolbox using the Add�Script dialog (if this is a new mxd file, you will need to first create a new toolbox)

3.       In the Add Script dialog, use these values:
Name: loadMapLayer
Label: Load Map Layer

4.       Click Next. Create a new file, LoadMapLayer.py, in a convenient location on your flash drive. Click Yes if asked to create this file

5.       In the next Add Script frame, use these values:
Display Name: My Map
Data Type: select Shapefile from dropdown list

6.       Click Finish

7.       Edit LoadMapLayer.py and add the text below:

import arcpy

mxd = arcpy.mapping.MapDocument("CURRENT")

df = arcpy.mapping.ListDataFrames(mxd, '')[0]

# Get selected shapefile and open it as a Layer

newlayer = arcpy.mapping.Layer(arcpy.GetParameterAsText(0))

# Add the layer to the data frame

arcpy.mapping.AddLayer(df, newlayer,"BOTTOM")

# Get the extent of newlayer

ext = newlayer.getExtent()

# Set the data frame�s extent to newlayer�s extent

df.extent = ext

#Refresh Table of Contents

arcpy.RefreshTOC()

#Refresh Active View

arcpy.RefreshActiveView()

#sys.exit(0)

8.       Save LoadMapLayer.py and then run the script. You should see your selected shapefile centered in the data frame.

We will explore more parameter options as we move on