Starting With Python

July 9, 2007

I have been writing a few geoprocessing scripts with Python recently, and have now even started to write scripts with more than one function..

I know I should sit down and read all the basics of a language first before using it, but where is the fun in that? I had written my main script, which called a couple of functions declared in the same .py file. I kept running into the following error:

“NameError: name ‘getMyFileTimes’ is not defined”

After looking a few times at how to declare functions, I realised eventually that they have to be declared before calling them. E.g.

def hello (what):
     text = “Hello, ” + what + “!”
     print text
hello (“World”)

I guess I am still getting used to writing in a scripting language in what amounts to little more than notepad, and it makes sense that if it is not compiled and read line by line then the function would not be “seen.” Still I miss my autosensing, and Visual Studio debugging tools a lot…


MS Access and Application.ExportXML

June 23, 2007

Just spent a few hours bieng frustated by the error message “reserved error 2950” when trying to export a query from MS Access 2002.

Turns out the problem was with one of the field names being “Size”, once renamed everything ran without problems. I guess this could apply to all reserved words.

Adding Coordinates to a Map Layout

May 17, 2007

A recent request by an ArcMap was user was how to add the current coordinates of the dataframe to the map layout in ArcMap. I messed around with grids and labelling for a while before resorting to a VBA script.

Coordinate Text

The code below should copied and pasted into the VBA Editor in ArcMap, and run when the current view is a page layout. The number of decimal places and distances of the text labels from the grid can be changed easily by modifying the relevant variable.

    1 Public Sub AddCornerText()


    3 Dim pMxDoc As IMxDocument

    4 Dim pActiveView As IActiveView

    5 Dim pLayoutExtent As IEnvelope

    6 Dim strText As String

    7 Dim pMapFrame As IFrameElement

    8 Dim pGraphic As IElement

    9 Dim dblOffset As Double

   10 Dim lngRound As Long


   12 ‘this is the offset used so the text does not overlap the map

   13 ‘modify according to needs

   14 dblOffset = 0.2


   16 ‘this is the number of decimal places used for each coordinate value

   17 lngRound = 0


   19 Set pMxDoc = ThisDocument

   20 Set pActiveView = pMxDoc.FocusMap


   22 ‘find the map data frame in the layout


   24 Set pMapFrame = pMxDoc.ActiveView.GraphicsContainer.FindFrame(pMxDoc.ActiveView.FocusMap)

   25 Set pGraphic = pMapFrame

   26 Set pLayoutExtent = pGraphic.Geometry.Envelope


   28 ‘create the bottom left point and text

   29 strText = Round(pActiveView.Extent.XMin, lngRound) & _

   30 “:” & Round(pActiveView.Extent.YMin, lngRound)

   31 AddPoint pLayoutExtent.XMin – dblOffset, pLayoutExtent.YMin – dblOffset, strText


   33 ‘create the top left point and text

   34 strText = Round(pActiveView.Extent.XMin, lngRound) & _

   35 “:” & Round(pActiveView.Extent.YMax, lngRound)

   36 AddPoint pLayoutExtent.XMin – dblOffset, pLayoutExtent.YMax + dblOffset, strText


   38 ‘create the bottom right point and text

   39 strText = Round(pActiveView.Extent.XMax, lngRound) & _

   40 “:” & Round(pActiveView.Extent.YMin, lngRound)

   41 AddPoint pLayoutExtent.XMax + dblOffset, pLayoutExtent.YMin – dblOffset, strText


   43 ‘create the top right point and text

   44 strText = Round(pActiveView.Extent.XMax, lngRound) & _

   45 “:” & Round(pActiveView.Extent.YMax, lngRound)

   46 AddPoint pLayoutExtent.XMax + dblOffset, pLayoutExtent.YMax + dblOffset, strText



   49 pMxDoc.ActiveView.Refresh


   51 End Sub


   53 Private Sub AddPoint(dblX As Double, dblY As Double, strText As String)


   55 Dim pMxDoc As IMxDocument

   56 Dim pPoint As IPoint

   57 Dim pTextElement As ITextElement

   58 Dim pPageLayout As IPageLayout

   59 Dim pElement As IElement

   60 Dim pGContainer As IGraphicsContainer


   62 Set pMxDoc = ThisDocument


   64 ‘create the location for the text


   66 Set pPoint = New Point

   67 pPoint.X = dblX

   68 pPoint.Y = dblY


   70 ‘create the text element


   72 Set pTextElement = New TextElement

   73 pTextElement.Text = strText


   75 ‘add the element to the layout


   77 Set pPageLayout = pMxDoc.PageLayout

   78 Set pElement = pTextElement

   79 pElement.Geometry = pPoint

   80 Set pGContainer = pPageLayout

   81 pGContainer.AddElement pElement, 0


   83 End Sub

Another Visual Studio Macro

April 4, 2007

My start up program when working with ArcObjects is nearly always set to ArcMap.exe As my ArcMap installation had recently moved from an F: drive to a C: drive I had to update this for over a dozen projects. There had to be an easier way (!) so I spent some time trying to find out how to accomplish this with a Visual Studio macro. This can easily be altered to set the start up program for a set of projects in a solution, or to another .exe.

    1 Imports System

    2 Imports EnvDTE

    3 Imports VSLangProj

    4 Imports VSLangProj2

    5 Imports VSLangProj80


    7 Public Module ArcGISMacros


    9     Sub SetStartProgram()


   11         Dim proj As Project

   12         Dim config As Configuration

   13         Dim configProps As Properties

   14         Dim prop As [Property]


   16         For Each proj In DTE.Solution.Projects

   17             If TypeOf proj.Object Is VSLangProj.VSProject Then ‘loop through projects

   18                 config = proj.ConfigurationManager.ActiveConfiguration

   19                 configProps = config.Properties

   20                 prop = configProps.Item(“StartProgram”)

   21                 If prop.Value.ToString() Like “*ArcMap*” Then

   22                     prop.Value = “C:\Program Files\ArcGIS\Bin\ArcMap.exe”

   23                 End If

   24             End If

   25         Next

   26     End Sub

   27 End Module

Migrating .NET Projects from ArcGIS 9.1 to 9.2

April 4, 2007

My Windows XP recently decided to fall apart, so after a few days reinstalling everything I decided it would be a good time to switch from ArcGIS 9.1 to 9.2. The installation went smoothly enough, and after a few more hours I had Visual Studio 2005 up and running as well. With some trepidation I opened up my largest ArcGIS VB solution…1030 errors, let alone warnings! The solution had been developed for ArcGIS 9.1 so I had expected some issues..

After some investigation it became apparent the cause of most errors were:

1. None of the ESRI 9.1 DLLs that my projects referenced were present on my machine. These had all been updated to 9.2

2. The ESRI.ArcGIS.Utility library has been deprecated, and its existing functionality moved to the ESRI.ArcGIS.ADF library.

With regards to the first issue all the libraries still had the same name, but were different versions. Changing the “Specific Name” property of the reference allowed VS to find the library and removed the error. I must have several 100 of these references for several projects so I decided to try out the VS macros to automate this task. To edit and create macros manually go to Tools >> Macros > Macros IDE in Visual Studio (2005). All the subs in this post were created in the same module, and require the following references listed below. Some of these had to be added manually to the “MyMacros” project via the References in the Macros IDE Project Explorer. The VSLangProj80 and VSLangProj2 libraries are in C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\PublicAssemblies\

    1 Option Strict On

    2 Option Explicit On


    4 Imports EnvDTE

    5 Imports VSLangProj

    6 Imports VSLangProj2

    7 Imports VSLangProj80

The following procedure loops through every project in the solution, and then through every reference in the project. If the reference starts with the text ESRI then the “Specific Version” property is set to false.




    5         Dim projectItem As ProjectItem

    6         Dim myCodeProject As VSLangProj.VSProject

    7         Dim proj As Project

    8         Dim ref As Reference3


   10         For Each proj In DTE.Solution.Projects

   11             If  TypeOf  proj.Object Is VSLangProj.VSProject  Then ‘loop through projects

   12                 myCodeProject = CType(proj.Object, VSProject)

   13                 For Each ref In myCodeProject.References

   14                     If ref.Name Like “ESRI.*” Then

   15                         ref.SpecificVersion = False

   16                     End If

   17                 Next

   18             End If

   19         Next


   21     End Sub

This greatly reduced the number of errors, and was so satisfying I continued playing around with macros and the macro recorder available in VS. OK it may have taken longer than doing all this manually..but where is the fun in that..

The following macros work as follows:

UpdateReferences – runs all the sub macros, passing parameters where appropriate.

ReplaceReference – removes a reference tat is no longer needed, and automatically adds in the new reference. In this example I use it to replace all references (sic) to the deprecated ESRI.ArcGIS.Utility reference with the new ESRI.ArcGIS.ADF reference.

ReplaceNameSpaces – this opens the find / replace dialog with parameters already filled. I commented out the automated execution of this replacement as I sometimes received dialogs while replacing that cause the macro to crash. In this example I replaced all Import declarations with the new ESRI.ArcGIS.ADF reference.

   35     Sub UpdateReferences()


   37         ChangeSpecificVersions()

   38         ReplaceNameSpaces(“ESRI.ArcGIS.Utility”, “ESRI.ArcGIS.ADF”)

   39         ReplaceReference(“ESRI.ArcGIS.Utility”, “C:\Program Files\ArcGIS\DotNet\ESRI.ArcGIS.ADF.dll”)


   41     End Sub

   42     Sub ReplaceReference(ByVal strOrigRefName As String, ByVal strNewRefPath As String)



   45         Dim projectItem As ProjectItem

   46         Dim myCodeProject As VSLangProj.VSProject

   47         Dim proj As Project

   48         Dim ref As Reference3


   50         For Each proj In DTE.Solution.Projects

   51             If TypeOf proj.Object Is VSLangProj.VSProject Then ‘loop through projects

   52                 myCodeProject = CType(proj.Object, VSProject)

   53                 For Each ref In myCodeProject.References

   54                     If ref.Name = strOrigRefName Then

   55                         ref.Remove()

   56                         myCodeProject.References.Add(strNewRefPath)

   57                     End If

   58                 Next

   59             End If

   60         Next


   62     End Sub


   64     Sub ReplaceNameSpaces(ByVal strOldNameSpace As String, ByVal strNewNameSpace As String)


   66         DTE.ExecuteCommand(“Edit.Find”)

   67         DTE.ExecuteCommand(“Edit.SwitchtoReplaceInFiles”)

   68         DTE.Find.Target = vsFindTarget.vsFindTargetFiles

   69         DTE.Find.FindWhat = strOldNameSpace

   70         DTE.Find.ReplaceWith = strNewNameSpace

   71         DTE.Find.MatchCase = False

   72         DTE.Find.MatchWholeWord = True

   73         DTE.Find.MatchInHiddenText = False

   74         DTE.Find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxLiteral

   75         DTE.Find.SearchPath = “Entire Solution”

   76         DTE.Find.SearchSubfolders = True

   77         DTE.Find.KeepModifiedDocumentsOpen = False

   78         DTE.Find.FilesOfType = “*.vb”

   79         DTE.Find.ResultsLocation = vsFindResultsLocation.vsFindResults1

   80         DTE.Find.Action = vsFindAction.vsFindActionReplaceAll

   81         ‘If (DTE.Find.Execute() = vsFindResult.vsFindResultNotFound) Then

   82         ‘Throw New System.Exception(“vsFindResultNotFound”)

   83         ‘End If

   84         ‘DTE.Windows.Item(“{CF2DDC32-8CAD-11D2-9302-005345000000}”).Close()


   86     End Sub

Web Accessibility and Online GIS

March 19, 2007

Is it impossible?!

Online maps are visual, there is no getting round this basic premise. The use of a summary attribute for a map element in a page is the only thing I can think of that would be of any use. This works in the same was as an alt attribute for an image – screen readers will read this text out to users who have problems visual impairments.

As the majority of online mapping servers generate images as output there could probably be further development in customising the ALT attributes of these images to provide more detailed information of the current map state, so rather than alt=”A Map”, alt=”Map showing towns and major roads.” This alt text could be derived from visible layers, and the centre coordinates of the current map location.

I tried running one of my map URLs on the online validator at which tests “single pages of web content for quality, accessibility, and privacy issues.” It timed out… performs similar checks..what can best be described as a mess was returned along with lots of “page not found errors.”

Fortunately clients do not seem to demand that accessibility standards are met for mapping pages, but with increasing legislation in many countries for websites to be usable by people with disabilites it is definitely an area that needs further work.

Some Notes on Web Standards and XHTML

March 19, 2007

I am starting to develop more and more online GIS and mapping applications, and to stop all the wiggly lines in Visual Studio I am trying to implement appropriate web standards. A good introductory article can be found here:

My summary…

 There are several versions of XHTML:

  • XHTML 1.0 Transitional – most similar to HTML
  • XHTML 1.0 Strict – most similar to XML
  • XHTML 1.0 Frameset – to allow the use of frames
  • XHTML 1.1 – allows additional elements in languages such as SVG
  • XHTML 2.0 – is apparently on the way
  • Depending on which language is used the page should contain the correct DOCTYPE e.g.

    <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN” “”&gt;

    In ASP.NET 2.0 every control renders valid XHTML 1.0 transitional output by default (not strict XHTML..ASP 1.1 controls do not even meet the transitional criteria) . As code is used to generate XHTML then pages cannot be validated at design time. However a URL can be validated at the following site – this checks the actual XHTML output seen by the browser.

    When using JavaScript in a web page, the script shold be enclosed in a CDATA (character data)  section. This stops the browser interpreting < or > as XML tags.  The CDATA section start and end should be enclosed in JavaScript comment tags to avoid errors:

    /* <![CDATA[ */

    /* ]]> */

    No New Objects Added…

    March 9, 2007

     If only Visual Studio would open in the same state that it was closed..

    Excluded DLLs in set up packages seem to re-include themselves, projects in solutions disappear, and today an ArcObects command seemed to have disappeared.

    All had previously been running without problems in debugging mode, opening ArcMap and then stopping at various breakpoints. However today no breakpoints were hit. Not even on the OnCreate or New functions of the BaseCommand. The button in ArcMap had disappeared. Adding it manually from the .tlb gave the “No New Objects Added” message.

    Some time later after much blood, sweat, (and possibly tears), I delved into the registry. I searched for the class ID in key values and names. The class ID can be found in the class code file of the tool:

    Public ConstClassId As String = “480d624c-3d4c-4210-ae11-0c775dddc35d”

    Then I took the rather risky step of deleting all these keys. If anyone does the same I’d recommend making a backup of the registry first – not that I did myself..
    Anyway I think the class must have been registered twice, or there was an error in the registry, as the next time I started the project with debugging everything had started working again =)

    Some ArcToolbox Commands

    March 6, 2007

    A few sample ArcToolbox commands I’ve been using recently. These can be run from the command line available in ArcCatalog (in the menu select Window >> Command Line).

    workspace C:\MyPath\MyGeoDatabase.mdb
    Intersect (featureClass1”;’featureDataset1\featureClass2′ ”) my_output_featureClass ALL # INPUT

    This automates the Intersect tool, found in ArcToolbox under “Analysis Tools >> Overlay >> Intersect” and is useful if this is a process that has to be run several times, and can be part of a batch process. The first line sets the workspace to a geodatabase containing the two input classes, and the second line runs the intersect, outputting all fields to my_output_featureClass.

    workspace C:\MyPath\MyGeoDatabase.mdb
    delete myFeatureClass

    This command deletes a feature class in the geodatabase.

    More samples can be found in the ArcGIS Desktop Help under the “Geoprocessing tool reference” section.


    Microsoft Access Queries

    March 2, 2007

    With the arrival of MS Access as the “personal” (i.e. restricted…) geodatabase of choice, I along with many others have had to get familiar with using SQL and Access. The graphical query generator (I am currently using MS Access 2002) seems to like to make the statements as verbose as possible, even thoigh relatively simple SQL still runs. Anyway below are some of my often used queries.

    • To copy data from one table to another (where both fields in both tables match). This is useful when two identical datasets cover two different geographical areas but need to be combined into a larger datasets.

    INSERT INTO Table1( Field1, Field2)
    SELECT Field1, Field2
    FROM Table2;

    • To create a new field in a table using a SQL statement (useful if you have to update many tables).


    • To combine the results of two queries with the same fields (the top 15 values from one subquery, and a specific record from another).

    SELECT TOP 15 Field1, Field2
    FROM Subquery1
    SELECT Field1, Field2
    FROM Subquery2
    WHERE Field1 = 272;

    More to follow (they get harder to explain!)…