Adventures in GIS

wow… over a full month since I posted anything 🙂
well what can I say, i’ve been quite busy.
Over the May bank holiday weekend however, I’ve been playing with some GIS stuff.  To those who don’t know what that is, it’s digital mapping and no that doesn’t only mean Google Earth or Google Maps, but they do play a part of it.

So what have I been upto then, well first off I started by setting up a database server that was able to handle GIS operations, for me this was Postgres with the PostGIS extensions.  I’ll not go into the exact setup routines as the documentation at the Postgres and Postgis web sites is easy enough.
Once i had the server up and running and configured correctly, I then needed to insert some data into it.  For those of you in the UK, the British Ordanance Survey have started releasing some of thier mapping data for free, you can find out more about whats available at the Ordanance Survey Website.
To make adding the data easy I used the free GIS program Quantum GIS, this is one of the easiest GIS programs I’ve ever used, and considering it’s open source it’s a great bit of software.  Quantum also supports more file & database formats than you can shake a stick at, and can handle many others (Including file conversions) thanks to it’s many plugins.
To get the data into PostGIS I used the "Split shape file to Postgres database" plugin, initially I had a few problems, but this was mostly due to the format of the data rather than anything else.  Once I’d tidyed the shape files up a little I tried again and into postgres they went.
Once I had all the data in that I wanted, it was time to explore.
I have some rather large raster base maps of the UK, and Qauntum was able to handle these exceptionally easy, and plot different layers of data from my database over them, I was able to scroll around and view many aspects of the data I had available, and perform lots of analysis.
I figured out some intresting patterns & relationships between postcodes and the town where I live and the surrounding area.
So was this the end of it?
Not by a long shot, from there I went onto actually exploring GIS-SQL.
just like a regular GIS database, postgis as a command line and understands SQL (Structured Query Language) , those of you who have previous DB experience my recognise things like:
INSERT INTO names (fname,sname,nickname) VALUES(‘peter’,’shaw’,’shawty’)
GIS-SQL is no different, in fact it’s an extention to regular SQL and adds a number of new things such as geometry columns, spatial analysis functions and lots of other things.
In my case the data I’d inserted was for the most part regular tabular data, apart from the co-ordinates for each point in the set.  The co-ordinates are held in a MULTIPOINT geometry type, which to the casual observer just looks like a normal BLOB (Binary Large OBject) field, however if you try using some of the extended functionality, EG:
SELECT name,admin_name,AsText(geometry) FROM settlements
you’ll get somthing like
‘consett’,’county durham’,’MULTIPOINT(41100 55085)’
returned.  Without ‘AsText’ you’d only see somthing like
‘consett’,’county durham’,’0E16A8724B893246241…………’
to add data tot he database is just as easy.
INSERT INTO settlements (name,admin_name,geometry) VALUES(‘consett’,’county durham’,GeomFromText(‘MULTIPOINT(41100 55085)’))
there are also ‘POLYLINES‘, ‘MULTILINES‘, ‘POLYGONS‘ and quite a few other types, all documented at the relevent web-sites.
A geometry column can only hold one type of geometry at a time, so you can’t mix points/polys/lines.  We can also perform operations that make desicions based on criteria return by queries, just as we can in ordinary SQL.  Imagine we have a series of polygons that show the boundries of different towns, we could do somthing like:
SELECT name FROM town_boundries WHERE contains(geometry,GeomFromText(‘MULTIPOINT(41100 55085)’)
Which will return the name fields of all town_boundries where the specified MULTIPOINT is contained fully within.
and so we come to the question on everyones lips.  Thats all well and good, but how on earth do I use all of this with my favorite mapping program ‘Google Earth’, well Quantum can load GE geometry natively.  Just save anything you’ve drawn as a KML file, then load it into Qauntum, from there you can choose to save it as shape files, load it into a database and man other things.
You can also come back the other way, by using Quantums "OGR Converter" plugin.  this can take many different input formats, and produce many different output, among which is Google Earth’s KML format.  This means that we can take the symbology for our settlements (or even just a subset) and export them to be viewable in GE quite easily, that however would take the fun away.  🙂
GE’s KML format is exceptionaly simple to use, it’s just a custom XML format.  The only thing you need to be aware of is that go-ordinates MUST be in decimal longtiude & latitude, and not in any thing like UK-OSGB36.  The co-ordinate conversion must be "WGS84" with E/W 0 at greenwich meridian.  Unfortunately I don’t have the space here to give you a full run down of co-ordinate systems (There’s hundreds of them) , if however your dealing with UK data then there are only 2 systems you really need to know.
OSGB36 – This is the standard UK northings & eastings positioning system, measured in meters from the lower left corner of the "Isles of Scilly" and going NOrth/East in a positive direction, the SRID Value for this is 27700
WGS84 – This is the standard linear world coordinate system, with point 0,0 being just off the coast of Africa, where the Prime meridian meets the Equator.  NOrth of the Equator is positive, South is Negative where as West of the Meridian is Negative and East is Positive, the SRID value for this is 4326
You’ll need the SRID values when setting your data up, as it is used to tell PostGIS what co-ordinate system your data is in, my settlement data was all UK mainland, and so was in the normal co-ordinate system for the UK OSGB36, to export for Google Earth you need WGS84.
So now we come to the final part, the KML data itself, you can find full details in the KML Developers Guide at Google, I’m just going to show you here how to create a simple point using C# and .NET
XDocument createKmlPoint(double longtitude,double latitude, String name,String description)
  XDocument myDoc = XDocument.Parse("
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="">
    <name>" + name + "</name>
    <description>" + description + "</description>
      <coordinates>" + longtitude.ToString() + "," + latitude.ToString() + ",0</coordinates>
  Return myDoc;
I’ve not actually tested this code segment, and to be honest it could be done much better by using nested XElement and Attribute addition, for my experiments I actually wrote it in PHP, but that also required a lot more code, particulaly in the use of "Jonathan Stotts excellent PHP co-ordinate class (Download here)" which is all too long to repeat in this post.  I was however quite able to get a web page up and running that allowed me to query for a settlement name, and have the webpage launch Google Earth and display the location of the requested settlement.
Thats all for now…
Happy Mapping.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s