Customization - GIS and Mapping - Lecture Notes, Study notes of Geology

In these Lecture notes, the following main points were discussed by the Lecturer : Customization, Customisation In Arcgis, The Graphical User Interface, Programming Options, Visual Basic For Applications, Python, Basic For Applications, Language Basics, Subs And Functions, Visual Basic Editor

Typology: Study notes

2012/2013

Uploaded on 07/23/2013

ramkumar
ramkumar 🇮🇳

4.4

(11)

89 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
GIS04
- 1 -
CUSTOMISATION
Last day we looked at computer programming in general terms; today we will look at how even a little
knowledge of programming can be used to great advantage.
The Graphical User Interface (GUI) in most GIS applications is generally sufficient for most user’s needs.
However, in some situations it may be useful either to change the user interface or to extend the program to
provide additional functionality. These changes are collectively referred to as customisation.Some of these
changes require knowledge of a programming language, but some applications provide facilities for customising
the GUI without any knowledge of programming.
The class today will focus upon customising ArcGIS, but similar principles apply to other applications.
TYPES OF CUSTOMISATION IN ARCGIS
Customising The Graphical User Interface
The ArcGIS GUI may be customised quite extensively, without any knowledge either of programming or the
underlying software components or data structures, by tapping into the program’s existing functionality using
drag and drop operations. Such changes generally involve moving or deleting objects on the GUI. The changes
are initiated using the Tools | Customise menu option in ArcMap or ArcCatalog. Examples of the types of
customisation possible will be demonstrated in the practical exercises.
Programming Options
ArcGIS is constructed from a library of software components known as ArcObjects.These objects are
accessible through the menus and buttons on the GUI, but they may also be programmed to provide additional
functionality via the objects’ interfaces.
Extensions to the functionality of ArcGIS may be accomplished in two ways:
1. You can write your own code using a high level language, or
2. You can import code that has already been written by someone else. Scripts for many useful operations may
be downloaded from the web and integrated into ArcGIS.
There are two broad approaches to adding functionality:
1. Internal.Changes are made within ArcGIS. The general procedure is to compile the code and then attach it
to a button or menu option in the GUI. When the end user clicks the new button, or the selects the new menu
option, an event is generated that causes the compiled code to be executed.
2. External.The ArcObjects may be used to build a standalone application outside of ArcGIS using any COM-
compliant or .NET-compliant high level programming language.
Internal modifications can be made using Visual Basic for Applications (VBA). VBA may be thought of as a
trimmed down version of Visual Basic. A Visual Basic Editor (VBE) is embedded in the ArcMap and
ArcCatalog applications to provide an integrated development environment (IDE). The built-in editor may be
used either to write or to import a Visual Basic (VB) ‘macro’ (program), compile and debug it, and then test it in
ArcMap or ArcCatalog, before saving it for future use. The saved VBA macro can either be run from the VBE or
attached to a button on the desktop for easier access.
Internal modifications can also be made using a scripting language such as VBScript, Jscript or Python. Support
for scripting languages was introduced in ArcGIS 9 using a new ArcObjects component called the geoprocessor.
This manages all the geoprocessing functions available within ArcGIS and provides access to more than
400 tools. Scripts can provide a lot of versatility by incorporating loops for batch processing and/or if statements
for conditional control. Scripts can be used to either create new tools or to create a geoprocessing model to
automate work flows (i.e. a series of operations the user might normally undertake using the GUI). The scripts
and models can be added to the Toolbox. ArcGIS also provides facilities to create models without programming
using graphical methods. W e will look at geoprocessing again later in the course.
Docsity.com
pf3
pf4

Partial preview of the text

Download Customization - GIS and Mapping - Lecture Notes and more Study notes Geology in PDF only on Docsity!

CUSTOMISATION

Last day we looked at computer programming in general terms; today we will look at how even a little knowledge of programming can be used to great advantage.

The Graphical User Interface (GUI) in most GIS applications is generally sufficient for most user’s needs. However, in some situations it may be useful either to change the user interface or to extend the program to provide additional functionality. These changes are collectively referred to as customisation. Some of these changes require knowledge of a programming language, but some applications provide facilities for customising the GUI without any knowledge of programming.

The class today will focus upon customising ArcGIS, but similar principles apply to other applications.

TYPES OF CUSTOMISATION IN ARCGIS

Customising The Graphical User Interface

The ArcGIS GUI may be customised quite extensively, without any knowledge either of programming or the underlying software components or data structures, by tapping into the program’s existing functionality using drag and drop operations. Such changes generally involve moving or deleting objects on the GUI. The changes are initiated using the Tools | Customise menu option in ArcMap or ArcCatalog. Examples of the types of customisation possible will be demonstrated in the practical exercises.

Programming Options

ArcGIS is constructed from a library of software components known as ArcObjects. These objects are accessible through the menus and buttons on the GUI, but they may also be programmed to provide additional functionality via the objects’ interfaces.

Extensions to the functionality of ArcGIS may be accomplished in two ways:

  1. You can write your own code using a high level language, or
  2. You can import code that has already been written by someone else. Scripts for many useful operations may be downloaded from the web and integrated into ArcGIS.

There are two broad approaches to adding functionality:

  1. Internal. Changes are made within ArcGIS. The general procedure is to compile the code and then attach it to a button or menu option in the GUI. When the end user clicks the new button, or the selects the new menu option, an event is generated that causes the compiled code to be executed.
  2. External. The ArcObjects may be used to build a standalone application outside of ArcGIS using any COM- compliant or .NET-compliant high level programming language.

Internal modifications can be made using Visual Basic for Applications (VBA). VBA may be thought of as a trimmed down version of Visual Basic. A Visual Basic Editor (VBE) is embedded in the ArcMap and ArcCatalog applications to provide an integrated development environment (IDE). The built-in editor may be used either to write or to import a Visual Basic (VB) ‘macro’ (program), compile and debug it, and then test it in ArcMap or ArcCatalog, before saving it for future use. The saved VBA macro can either be run from the VBE or attached to a button on the desktop for easier access.

Internal modifications can also be made using a scripting language such as VBScript, Jscript or Python. Support for scripting languages was introduced in ArcGIS 9 using a new ArcObjects component called the geoprocessor. This manages all the geoprocessing functions available within ArcGIS and provides access to more than 400 tools. Scripts can provide a lot of versatility by incorporating loops for batch processing and/or if statements for conditional control. Scripts can be used to either create new tools or to create a geoprocessing model to automate work flows (i.e. a series of operations the user might normally undertake using the GUI). The scripts and models can be added to the Toolbox. ArcGIS also provides facilities to create models without programming using graphical methods. We will look at geoprocessing again later in the course.

ArcObject functionality can also be incorporated into your own programs in an external environment. This approach generally requires you to create a project, reference the ArcObjects type libraries required, implement ArcObjects interfaces, add code, and then compile the source into a binary file. Any COM-compliant or .NET- compliant high level programming language, such as Visual Basic, Visual C++ or Python may be used.

Python

Python would appear to be the language of choice. Python is an interpreted object-oriented programming language which may be used both for scripting and also to write standalone applications. It is distributed under a GNU General Public Licence and may be downloaded from the web. Version 2.6 is bundled with ArcGIS, but version 2.6 is being phased out in favour of Python 3.x.

VISUAL BASIC FOR APPLICATIONS

Language Basics

It is not feasible to explain the full VBA language here, but a few basic ideas may help you decipher a VBA script.

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. Applications built using VBA are often referred to as a macro – a macro is functionally similar to a procedure.

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.

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.

Variable type must be declared before the variable is used. This is most commonly done using the keyword Dim. Thus, for example, the variable n could be declared as an integer using the statement:

Dim n As Integer

VBA is an object-orientated language. If you want a variable to refer to an existing class, it must be declared by pointing to an interface supported by the class. This is because the properties and methods of the class are hidden according to the principle of encapsulation, so when programming you must always work through the interface. The type declaration takes the form:

Dim pField As IFieldEdit Set pField = New Field

where the first statement declares a variable pField to hold the object by pointing to an interface IFieldEdit (supported in this example by the Field class). The second statement (instantiation) creates a new Field object by making pField an instance of the Field class. pField inherits the properties and methods of the Field class. Set and New are keywords.

By convention interface names begin with a capital I, whilst the objects by convention have names beginning with a lower case p (for pointer). The rest of the name uses upper and lower case letters for readability.

The properties of an object are indicated using an object.property dot notation. For example:

pObjectnameName.PropertyName = value

indicates that the property or attribute called PropertyName in object pObjectName should be set to value (where value might, for example, be a specific number or a text string).

A similar object.method notation is used for methods. For example:

Set pObjectName.PropertyName = pObjectName.MethodName (parameters)

The File menu contains options for importing and exporting procedures in text file format. The Debug menu contains options for compiling and debugging macros. The macros may be run from the Run menu or the Run icon.

Debugging Code

There are three types of error possible in VBA. Compile errors are caused by syntax problems within the code. They cause the compiler to stop compiling so that the problem can be fixed. Run-time errors are more difficult to fix. They arise if a successfully compiled macro instructs the computer to do something it is unable to (e.g. to divide by zero). Logic errors are even more difficult to detect. The macro may compile and run okay, but give incorrect results.

When debugging the code, it may be useful to set breakpoints at certain locations. These cause the program to stop running. If you include Debug.Print statements within your program, the current values of the specified variables will be displayed in the Immediate window. The program can then be continued from the point at which it stopped using the Run button. The Debug menu and the debug toolbar contain options for adding or removing breakpoints, as well as other options such as Step Into (i.e. get the program to stop at every statement), Step Over (i.e. get the program to run the current procedure as a unit) and Step Out (i.e. execute all the remaining code in the procedure as a single statement). The Run To Cursor command allows you to select a statement to stop the program at.

VBA has a built-in object called Err , which contains properties Err.Number and Err.Description. By adding an On Error statement to your code, you can include code to display the error number and its description in the event of a run-time error.

SAMPLES

One of the best ways to teach yourself a programming language is to examine programs and scripts written by others. Finding sample scripts used to be simple, but it seems to take more digging these days to find them. The following lists some of the places you might try:

 ArcGIS Help. From the Help menu in ArcGIS, enter VBA in the Index tab. Select Sample Code.  ArcScripts. ESRI have a web site at http://arcscripts.esri.com/ which contains thousands of scripts in a couple of dozen different languages.  Geoprocessing Scripts. From the main ESRI homepage at http://www.esri.com follow the links for Support, Downloads, Geoprocessing.  EDN Code Exchange. Click on the Resources link at http://edn.esri.com/