Thursday, July 24, 2008

Terrain in GeoMedia WebMap Mashup

In addition to the Satellite, street map, and hybrid map types, Google Maps also publishes a terrain map type. If you use the default GeoMedia WebMap 6.1 Publisher template to create a new Google Maps mashup, you will notice that there is no terrain button in the web page; only the Map, Satellite, Hybrid, and No Backdrop buttons are available. See the screen shot below.

To enable the display of terrain shaded relief data from Google, you need to add in the terrain map type to the Google Maps javascript code. Here are the steps to do it.
  1. Go to the GeoMedia WebMap Publisher created mashup folder, e.g. C:\GeoMediaWebMapProjects\mymashup\
  2. Change directory to the System folder.
  3. Use a text editor to open the file gm.js.
  4. Inside the text editor, locate the function initView.
  5. Look for the following lines in the function.



    objMapViewEle = document.getElementById("map");
    //testoutput("creating new custom map type");
    var tileLayers = [new GTileLayer(new GCopyrightCollection(), 0, 18)];

    var custommap = new GMapType(tileLayers, G_NORMAL_MAP.getProjection(), "No Backdrop",{errorMessage:"No Data Available"});
    //testoutput("creating GMap2");
    objMapView = new GMap2(objMapViewEle);

    objMapView.addControl(new GLargeMapControl());

    objMapView.addMapType(custommap);

    objMapView.addControl(new GMapTypeControl());

    gOverviewMapControl = new GOverviewMapControl(new GSize(150,150));
    objMapView.addControl(gOverviewMapControl);
    objMapView.addControl(new GScaleControl());


  6. Add in the following line in bold:

    objMapView.addMapType(custommap); objMapView.addMapType(G_PHYSICAL_MAP); objMapView.addControl(new GMapTypeControl());

  7. Save and close the file gm.js.
  8. Now, open up the mashup web page in an Internet browser. You should be able to see the Terrain button. Clicking on the button will display the terrain data in the map window as shown in the screen shot below.



There you go. Just remember, that the change in gm.js will be overwritten if you decide to republish the mashup again from GeoMedia WebMap Publisher.

Thursday, July 10, 2008

Query Oracle Spatial in Microsoft Excel

Oracle has been building spatial query tools inside the database engine for many years now since Oracle 8i. But I have not seen many examples of Microsoft Office applications that directly query a Oracle Spatial database for spatially related information without having to use any GIS vendors' software.

A simple example that came to my mind is an Office application that generates water usage demand reports by geographic regions. Here, I outline the steps I took to create a simple Excel VBA to query an Oracle Spatial database. The VBA will look for all point geometries INSIDE a polygon geometry to calculate the total demand by usage type and the results printed to an Excel sheet.

Create Sample Oracle Data
For my convenience, I used Intergraph's GeoMedia Professional 6.1 to create a couple of Oracle tables CUSTDATA and DEMANDPOLYGON with GEOMETRY fields. However, you can use any tool of your choice to create the spatial tables. Even plain SQL statements through the Oracle SQL*PLUS prompt is fine.

CREATE TABLE CUSTDATA
(
ID INTEGER,
POSTCODE VARCHAR2(6),
BPNAME VARCHAR2(255),
TELNO VARCHAR2(10),
ACCTCLASS VARCHAR2(255),
PREMISENUM VARCHAR2(50),
PREMISEADDR VARCHAR2(255),
AVGBILL FLOAT(126),
X FLOAT(126),
Y FLOAT(126),
GEOMETRY SDO_GEOMETRY
);

CREATE TABLE DEMANDPOLYGON
(
ID INTEGER,
DESCRIPTION VARCHAR2(255),
GEOMETRY SDO_GEOMETRY
);

CUSTDATA holds point geometry data while DEMANDPOLYGON stores polygon geometries. After creating the tables, I inserted a few records inside the newly created tables. My sample data looks like the screen shot below.



Create an ODBC Data Source
If you don't have an ODBC Data Source Name pointing to the Oracle schema, then you can use the Control Panel's ODBC Data Source Administrator to define one as shown in the two figures below.




Create a New VBA in Microsoft Excel
  1. Bring up Microsoft Excel and create a new Excel Workbook. Then click Tools > Macros > Visual Basic Editor to bring up the VBA Editor.



  2. Select Tools > References. Select Microsoft ActiveX Data Objects 2.1 library to reference.



  3. Select Insert > UserForm to create a new form. By default, it is named as UserForm1.
  4. Using the IDE's toolbox, add in the following controls: Label, ComboBox, and 2 CommandButtons. Arrange the controls as shown in the figure below. Name the CommandButton controls as cmdOK and cmdCancel.



  5. Select View > Code to enter the VBA code for the UserForm1. Enter the following code.

    Option Explicit

    Dim mobjConnection As ADODB.Connection

    Private Sub cmdOK_Click()
    Dim sql As String
    Dim objRs As ADODB.Recordset
    Dim demandType As String
    Dim demand As Double
    Dim excelRow As Integer

    'Form the Oracle Spatial query statement
    sql = "SELECT A.ACCTCLASS,SUM(A.AVGBILL) AS DEMAND FROM CUSTDATA A, DEMANDPOLYGON B "
    sql = sql & "WHERE "
    sql = sql & "B.DESCRIPTION='" & ComboBox1.Text & "' "
    sql = sql & "AND "
    sql = sql & "SDO_RELATE(A.GEOMETRY,B.GEOMETRY,'MASK=INSIDE')='TRUE' "
    sql = sql & "GROUP BY A.ACCTCLASS "
    sql = sql & "ORDER BY A.ACCTCLASS ASC "

    'Print out the report title and heading on the Excel sheet
    With Sheet1
    .Range("A1").Value = "REPORT OF WATER DEMAND FOR " & ComboBox1.Text
    .Range("A2").Value = "TYPE"
    .Range("B2").Value = "TOTAL DEMAND"
    End With

    'Submit the query to Oracle and loop through the recordset.
    'Print out the results in the Excel sheet
    excelRow = 3
    Set objRs = New ADODB.Recordset
    objRs.Open sql, mobjConnection
    While Not objRs.EOF
    demandType = objRs.Fields(0)
    demand = objRs.Fields(1)
    Sheet1.Range("A" & excelRow).Value = demandType
    Sheet1.Range("B" & excelRow).Value = demand
    excelRow = excelRow + 1
    objRs.MoveNext
    Wend
    End Sub

    Private Sub UserForm_Initialize()
    Dim username As String
    Dim password As String
    Dim dataSourceName As String
    Dim connString As String

    username = "demo"
    password = "demo"
    dataSourceName = "demo"

    ' Open a ADO database connection to the Oracle Spatial Schema
    connString = "Data Source=" & dataSourceName & ";User ID=" & username & ";Password=" & password & ";"
    Set mobjConnection = New ADODB.Connection
    With mobjConnection
    .ConnectionString = connString
    .ConnectionTimeout = 10
    .CursorLocation = adUseClient
    .Open
    End With

    'Query the DEMANDPOLYGON table for the names of all the polygons
    'Add the names to the combo box for selection
    Dim sql As String
    Dim objRs As ADODB.Recordset
    sql = "SELECT DISTINCT DESCRIPTION FROM DEMANDPOLYGON ORDER BY DESCRIPTION ASC"
    Set objRs = New ADODB.Recordset
    objRs.Open sql, mobjConnection
    ComboBox1.Clear
    While Not objRs.EOF
    ComboBox1.AddItem objRs.Fields(0)
    objRs.MoveNext
    Wend
    ComboBox1.ListIndex = 0
    objRs.Close
    Set objRs = Nothing

    End Sub

  6. In the VBA IDE' project pane, right click on the ThisWorkbook object and select View Code. Add in the following code.

    Private Sub Workbook_Open()
    UserForm1.Show
    End Sub

  7. Now save and close everything.

Running the Excel VBA
Double click the Excel file you created previously. The Excel Workbook should open and you would be prompted with the following:



Choose a region from the drop down list and press OK. The Excel VBA would submit a spatial query to Oracle to find all the CUSTDATA records inside the selected demand polygon. The results are grouped, tabulated and displayed in the Excel spreadsheet as shown below.



Notes
The example code shown is quite unsophisticated. I have hardcoded the Oracle login, password and ODBC data source name. There is also no error handling at all. These are intentional as the example is just meant to illustrate a technique.

Wednesday, July 2, 2008

GeoMedia WebMap Publisher 6.1 Permission Denied Error

This must be a beginner's mistake but it stumped me for a couple of days. On a Windows XP SP2 notebook, I tried to run GeoMedia WebMap Publisher 6.1's Server Configuration Utility - but I kept getting a permission denied error message as shown below.

I suspected the DCOM Config Access Permissions may not be granted with the correct permissions but I could not see it. I thought I had set all permissions as per according to the Web Map Publisher instructions. But then some kind soul from the GeoMedia Developer's forum pointed to me that the INTERACTIVE user needs to be granted access permission also in the Component Services.


Once I did that, I had no problems bringing up the Publisher Server Configuration Utility.

Wednesday, June 25, 2008

GeoMedia Terrain's Unintuitive Terrain Models Dialog Box

The first time I launched the Terrain Models command, I was dumbfounded for a while. Quite a while. I was supposed to build a terrain model using this dialog but there are no obvious indicators to guide me along. The layout and visual arrangement of the buttons, list boxes and labels do not tell me which item I should click on first.

How was I to know that you have to select a Geomorphic data store or a grid data store label and drag the item and drop it on one of the labels: Grid Terrain model or TIN terrain model? Only after reading the online document Working with GeoMedia Terrain that I learn about how to do it. Intergraph need to redesign this dialog box for better user interaction.

Tuesday, June 24, 2008

GeoMedia and PostGIS

PostGIS is an open source software that spatially enables the PostgreSQL database. I have been using PostGIS for several years and it has served me well personally as well as in a couple of GIS implementation projects. It is easy to use and I really like being able to create spatial tables and perform spatial queries and analysis all from the SQL prompt. I could string a number of the statements into a script for automated batch processing. An example PostGIS spatial query is shown below:

create table demand as
select
a.postcode, a.telno, a.acctclass, a.premisenumber, a.premiseaddress, a.avgbill, a.acctclass, a.x, a.y, a.demandtype, b.polygonid
from custdata a, demandpolygon b
where (a.the_geom && b.the_geom) and
intersects(a.the_geom,b.the_geom)
order by b.polygonid

The field the_geom is the geometry field of the tables; the operator ' &&' is similar to Oracle's first pass rough filter while intersects is a spatial operator.

One of the problems that I face is the lack of a good graphical user interface for viewing and manipulating spatial data in PostGIS. I have to export data in Shape file format using the PostGIS utility pgsql2shp for viewing in GeoMedia.

However, I have just seen the Intergraph 2008 GeoMedia Product Updates presentation and I am pleased to see that a PostGIS Data Server will be developed as part of a minor release of GeoMedia 6.1. It is not known whether it will be a Read only or Read/Write data server. Let's hope it is R/W. Here is a crop of a slide below:

Thursday, June 19, 2008

Firefox 3.0 "Awesome" Bar

The new Mozilla Firefox 3.0 has been newly installed on my work computer and from the word go it is an amazing piece of work. It feels snappier and web pages seem to load and render faster than the previous version 2.0. But the most jarring change is with the url bar (some people call it the Awesome bar or Smart bar). Unlike version 2.0, it comes with a smarter auto-complete algorithm and rich text display (see the figure below); the auto-complete is no longer limited to the first characters of the url.

At first, using the new url bar felt awkward but I can see benefits of using it, so I'm going to keep it. There is a Firefox extension - the OldBar here at https://addons.mozilla.org/en-US/firefox/addon/6227 that you can install if you really don't like the new url bar and prefer how the urls look in the old way.

Wednesday, June 18, 2008

Batch Resize Image Files with ImageMagick

There is a useful open source software for manipulating image files. No, I'm not talking about GIMP but rather another software suite called ImageMagick. ImageMagick can read, convert and write images in a variety of formats including JPEG, PNG and even PDF. It comes with command line utilities like convert, mogrify, identify, composite, etc. The main reason I am mentioning ImageMagick is that I like to use its mogrify utility to resize a large number of image files for display on my Palm Tungsten device. High resolution images cannot be displayed on my PDA due to insufficient memory - so I have to resize the images to a lower resolution. Photoshop has a graphical resize tool but Photoshop is not free and it takes a longer time to fire up. With ImageMagick, all I have to do is open up a Command Window and type in the following commands:

C:\> mogrify -resize 90% *.jpg

The above command will resize all files with the extension JPG to 90% of their original resolutions.

Note: Make copies of your files before using mogrify as it will make the adjustments on the actual files.