Lecture 4

advertisement
Customisation
• The GUI in most GIS applications is sufficient for most
needs. However, situations arise where you want either to:
– Modify the interface, or
– Add new functionality
• We will focus on ArcGIS, but similar principles apply to
other systems.
GUI Changes
• Modifying the interface can be achieved without any
programming.
• Basically involves adding, deleting or moving objects in
the GUI using drag and drop operations.
• The changes are initiated from the Tools | Customize
menu.
• We will look at examples in the practical class.
Programming Options
• ArcGIS is constructed from several hundred ArcObjects.
• These can be programmed to add functionality.
• You can either:
– Write your own code, or
– Download code via the Internet
• Two approaches:
– Internal – i.e. modify ArcGIS itself
– External – i.e. write a standalone program using
ArcObjects
Internal Changes
• ArcGIS can be modified in several ways:
– Visual Basic for Applications (VBA) macro. The
macro can either be run from the built-in Visual Basic
Editor (VBE) - which provides an Integrated
Development Environment (IDE), or attached to a
button on the desktop.
– Scripting languages such as VBScript, JScript or
Python can be used to create a geoprocessing script.
The script could either be a new tool or several tools
linked together to automate a workflow as a
geoprocessing model.
External Programs
• ArcObject functionality can also be incorporated into your
own programs in an external environment.
• Any COM-compliant or .NET-compliant high level
programming language, such as Visual Basic, Visual C++
or Python may be used.
• Programs require a licence.
Python
• Python is arguably the language of choice.
• It is an interpreted high-level language – simple to learn,
yet very versatile.
• It is object-orientated, yet lends itself to procedural
programming.
• In the ArcGIS context it can be used both for scripting and
writing standalone applications.
• It is open source and available for free from the Internet.
• ArcGIS 9.x comes with Python 2.5.1, but this is not
compatible with Python 3.x.
Visual Basic for Applications
• We will use some VBA macros in the practical class, so it
may be helpful to say something about VB.
• A block of code that can perform a specific task is referred
to as a procedure. A collection of procedures is known as
a module and a collection of modules is referred to as a
project.
• Procedures may be private or public. A private procedure
can only be called or used by another procedure in the
same module; public procedures are available to
procedures in other modules in the same project.
Procedures
• There are three types of procedure: events, subs and
functions.
• Event procedures are associated with controls such as
menus and buttons.
• Subs and functions are not associated with controls, but
are called by other procedures; the difference between
them is that functions return a value whereas a sub does
not.
Type Declarations
• Variable type must be declared before use using a Dim
statement, e.g.
Dim n As Integer
• Objects must be declared and then instantiated, e.g.
Dim pField As IFieldEdit
Set pField = New Field
The first line creates a variable to hold the object, the
second creates the object. All references to an object are
through an interface.
Dot Notation
• The properties of an object are indicated using an
object.property dot notation. For example:
pObjectnameName.PropertyName = value
• A method for an object is specified in a similar manner:
Set pObjectName.PropertyName = pObjectName.MethodName (params)
• Comments may be added using an inverted comma:
‘ Everything after the inverted comma is a comment
Structure
• VBA supports branches:
If ... Then ... Else ... End If
Select Case ...Case ...Case ... End Select
• And loops:
Do While ... Loop
Do Until ... Loop
For ... Next
The Visual Basic Editor (VBE)
• The VBE contains various windows:
– Code window
– Project window / explorer
– Properties window
– Immediate window
Error Handling
• There are three main types of error:
– Compile
– Run-time
– Logic
• Error trapping:
– Breakpoints / Debug.Print
– Step Into / Step Over / Step Out
– Run To Cursor
– The Err object
Download