Shape Files and SQL Server

Over the last couple of weeks I have been doing a lot of work importing polygons into an SQL server database, using them for some data processing tasks and then exporting the results as KML for display. I thought it’d be worth a post to record how I did it.

Inserting polygons (or any other geometry type) from a shape file to the database can be done with the ogr2ogr tool which ships with the gdal libraries (and with Mapserver for Windows). I knocked up a little batch file to do it:

SET InputShapeFile="D:\Dropbox\Data\SingleView\Brazillian Polygons\BRA_adm3.shp"

SET SqlConnectionString=";Database=danTest;;Pwd=yourpassword;"

SET TEMPFILE="D:\Dropbox\Data\Temp.shp"
SET OGR2OGR="C:\ms4w\tools\gdal-ogr\ogr2ogr.exe"
SET TABLENAME="TestPolygons"

%OGR2OGR% -overwrite -simplify 0.01 %TEMPFILE% %InputShapeFile% -progress

%OGR2OGR% -lco "SHPT=POLYGON" -f "MSSQLSpatial" %SqlConnectionString% %TEMPFILE% -nln %TABLENAME% -progress

The first ogr2ogr call is used to simplify the polygons. The value 0.01 is the minimum length of an edge (in degrees in this case) to be stored. Results of this command are pushed to a temporary shape file set. The second call to ogr2ogr pushes the polygons from the temp file up to a database in Windows Azure. The same code would work for a local SQL Server, you just need to tweak the connection string.

You can use SQL Server Management Studio to show the spatial results of your query, which is nice! Here I just did a “select * from testPolygons” to see the first 5000 polygons from my file.


Sql Server contains all sorts of interesting data processing options, which I’ll look at another time. Here I’ll just skip to the final step – exporting the polygon data from the database to a local KML file.


SET KmlFile="D:\Dropbox\Data\Brazil.kml"

SET SqlConnectionString=";Database=danTest;;Pwd=yourpassword;"

SET TEMPFILE="D:\Dropbox\Data\Temp.shp"
SET OGR2OGR="C:\ms4w\tools\gdal-ogr\ogr2ogr.exe"
SET SQL="select * from TestPolygons"

%OGR2OGR% -lco "SHPT=POLYGON" -f "KML" %KmlFile% -sql %SQL% %SqlConnectionString%  -progress

Obviously you can make the SQL in that command as complex as you like.

Polygons here are from this site which allows you to download various polygon datasets for various countries.

Serial on Raspberry Pi Arch Linux

So the new version of Arch Linux doesn’t have runlevels, rc.d or any of that nonsense any more. It just has systemd. Super simple if you know how to use it, but a right pain in the backside if you don’t.

I have a little serial GPS module hooked up to my Raspberry Pi via the hardware serial port (ttyAMA0). My old instructions for getting this to work aren’t much use any more. Here’s the new procedure for getting serial data with the minimum of fuss:

1. Disable serial output during boot

Edit /boot/cmdline.txt using your favourite editor. I like nano these days.

sudo nano /boot/cmdline.txt

Remove all chunks of text that mention ttyAMA0 but leave the rest of the line intact. Bits to remove look like:

console=ttyAMA0,115200 kgdboc=ttyAMA0,115200

2. Disable the console on the serial port

This was the new bit for me. The process used to involve commenting out a line in /etc/innitab but that file is long gone.

Systemd uses links in /etc to decide what to start up, so once you find the right one, removing it is easy. You can find the files associated with consoles by doing:

ls /etc/systemd/system/

One of the entries clearly refers to ttyAMA0. It can be removed using the following command:

sudo systemd disable serial-getty@ttyAMA0.service

3. Check you’re getting data

I used minicom for this as it’s very simple to use. First of all, make sure you plug in your device (with the power off, if you’re as clumsy as me!).

sudo pacman -S minicom
minicom -b 4800 -o -D /dev/ttyAMA0

You should see a lovely stream of data. I my case it was a screen full of NMEA sentences. Great stuff!

WordPress: Oh deary deary me!

This evening I was innocently setting up a wireless dongle for my darling wife. I casually typed in the address of this very web page into her browser to check it was working, only to find that all the posts were missing!

404 errors on every page but the front page. Poo! I desperately dived in to the wordpress settings, everything was set up fine. I updated wordpress but it made no difference. In the end, I went back to the post URL settings and clicked “Apply” again. It fixed the problem!

Looking at the stats, Logical Genetics seems to have been off the air since Independence Day. Almost a month. Miserable.

Back now though, and soon to be posting an article on my Build Status Traffic Lights.

Using a BufferBlock to Read and process in Parallel

Wrote an app this week – top secret of course – to load data from a database and process the contents. The reading from the database is the slow part and the processing takes slightly less time. I decided it might help if I could read a batch of results into memory and process it while loading the next batch.

Batching was dead easy, I found an excellent extension method on the internet that batches up an enumerable and yields you a sequence of arrays. The code looks like this, in case you can’t be bothered to click the link:

public static IEnumerable<T[]> Batch<T>(this IEnumerable<T> sequence, int batchSize)
    var batch = new List<T>(batchSize);

    foreach (var item in sequence)

        if (batch.Count >= batchSize)
            yield return batch.ToArray();

    if (batch.Count > 0)
        yield return batch.ToArray();

That works really well, but it doesn’t give me the parallel read and process I’m looking for. After a large amount of research, some help from an esteemed colleague and quite a bit of inappropriate language, I ended up with the following. It uses the BufferBlock class which is a new thing from Microsoft’s new Dataflow Pipeline libraries (which provide all sorts of very useful stuff which I may well write an article on at a later date). The BufferBlock marshals data over thread boundaries in a very clean and simple way.

public static IEnumerable<T[]> BatchAsync<T>(this IEnumerable<T> sequence, int batchSize)
    BufferBlock<T[]> buffer = new BufferBlock<T[]>();

    var reader = new Thread(() =>
            foreach (var batch in sequence.Batch(batchSize))
        }) { Name = "Batch Reader Async" };

    T[] blocktoProcess;
    while ((blocktoProcess = buffer.Receive()) != null)
        yield return blocktoProcess;

The database read is done on a new thread and data is pulled back to the calling thread in batches. This makes for nice clean code on the consumer side!

Tracking Kanban with TFS

Kanban is a great way to manage your bug backlog.  It’s much better than Scrum simply because of the nature of bugs as compared to user stories. Scrum is all about making firm commitments based on estimates but bugs are very hard to estimate up-front. Generally when you’ve looked hard enough into the code to find the problem, you are in a position to fix it very quickly. Bug fixing is essentially a research task – like a spike – so time-boxing the work makes much more sense.

Set up a prioritised backlog and blast off the top as many bugs as possible in the time you’ve set aside – Kanban Style.  This works very well but, as with most agile approaches, it leaves old fashioned managers a bit grumpy.  They want to track your productivity and it’s fair to say that you should too because that’s how you spot impediments (plus it’s always good to show off).

Scrum-style burn downs don’t work with Kanban because they track progress against some committed target.  The answer is the Cumulative Flow Diagram:


So I did some tweaking to my Information Radiator to add a page showing the CFD for the last 60 days of one of our projects.  The data comes out of TFS via the C# API and a WIQL query – which has a very nice historical query feature which I’ll explain below.

Cumulative Flow Diagrams Explained

Cumulative flow diagrams couldn’t be simpler.  Like a burn-up chart they show a running total of the bugs fixed over time.  Since bugs aren’t estimated, the Y axis shows the bug count.  In the chart above the X axis is in days but I guess you could do weeks or even hours if you like.  In addition to the “fixed bugs” series, there are also stacked series for other states: “committed”, “in development” and “in QA”.

The benefit of showing the other issue states is that it gives you a readout on how the process is working.  The QA and development series should generally be the same thickness.  If the QA area gets fatter than the development area then you have a bottleneck in QA.  If the development series gets too fat then you’re spread too thinly – you have an impediment in development or need to think about your Kanban limit.

Note how there are a couple of “steps” on the left of my graph.  Those correspond to the first couple of sprints in which we used TFS. The team weren’t familiar with it, so work item states were generally changed at the end of the sprint.  As time went on we got better at updating the system and the steps turned into a nice looking slope.

Historical Queries in TFS 2012

It’s not every day that I openly applaud Microsoft for doing something brilliant and until now I’ve never been that cheerful about TFS.  But… the historical querying in WIQL (work item query language) is bloody brilliant!

Drawing a CFD chart depends on an ability to get the historical state of any issue in the system at a specified point in time.  In WIQL this is done using the “AsOf” keyword:

Select [ID], [Title], [Effort - Microsoft Visual Studio Scrum 2_0], [Assigned To]
From WorkItems
  [Team Project] = 'Project'
  [Work Item Type] = 'Bug'
  [Iteration Path] under 'Project\Release'
AsOf '21/01/2013'

So the algorithm for drawing the CFD is pretty simple:

  • Grab the sprints for the project in question and use them to get the start and end dates for your chart
  • For each day on the X axis
    • Run a WIQL statement to get the state of all the bugs in the project on that date
    • Use linq to count issues in the various states you’re showing on the graph series
    • Populate a list of view model/data objects (one for each X value)
  • Throw the values at the chart

The only complications were the fact that the WPF Toolkit chart doesn’t support stacked area series (so I had to do it myself in the view model) and that getting data on group membership from TFS is very hard and very slow (so I build a cache of dev and QA group members up front and do comparisons on the display name).

Circular Polarisation the Easy Way

Antennas?  Antennae?  I’m not an expert on pluralisation, but I know how to search the internet.  Seems English authors don’t differentiate between metallic apparatus and sensory appendages, so antennae it is.

Anyway, a couple of months ago I carefully soldered together a couple of circular polarised FPV antennae.  I followed the guide on, carefully measured some 0.8mm welding wire and fiddled for hours getting it soldered together.  The end result was a pair of incredibly fragile antennae in which I had very little confidence.  At 5.8GHz the tolerances are incredibly small and even the smallest blob or errant solder can cause issues, not to mention bashing into the ground and ruining everything.


The answer?  Buy something!  These fantastic plastic-potted circular polarised antennae are built to plug into Fat Shark goggles but can be made to fit the HobbyKing FPV stuff with a very cheap adapter that can be found on eBay for about £1 each (search for an SMA female to RP-SMA male adapter).

HobbyKing just added a version that doesn’t need the adapter too!  I found it while looking for a link for the previous paragraph.  I’m not too fussed that I missed out though – the ones I got are great!


These little antennae are much smaller than I expected.  They are also very light and very tough.  The flexible but stiff cable allows them to be bent into the right position for your model too.

Can’t wait to give these a try – when it stops raining.

Double Tricopter Test Flight

So what happens if you take two Tricopters and bolt all the bits together?  This week I extended the legs of my old frame and made a six-motor tricopter.


After the early morning Tricoptering mission on top of Beacon Hill last week I was a bit miserable. The extra weight of the GoPro and high winds made the ‘copter sluggish and nasty to fly.  Performance has been getting worse recently, probably due to ageing batteries as I haven’t changed the design.

The only answer, other than ordering some new batteries, is to make the frame lighter… or increase the power!


The old 3-motor frame weighed about 1kg without batteries.  The new one weighs about 1.4kg but has twice as many motors!  The legs are 500mm long, so it’s less portable than before.  Tested in the garden today and it seems much more powerful.


Most of the complexity is in the cabling.  Plus it needs two batteries now, which adds a fair bit of weight.  I also beefed up the servo which tilts the back leg.  This chunky metal gear servo weighs something like 64g but will hopefully survive the crashes better than the smaller and lighter Corona I used before.


Very happy indeed with the new landing gear.  Slices of PVC pipe attached with cable ties. They really absorb the bumps and cost very little.

It flies about OK.  More power than the tricopter of old – but not really as much as I’d like.  I’m sure it’ll handle the extra weight of the GoPro easily, I’m just not sure it’s worth the huge size.  Doubt it’ll fit in the car for holidays!


Need to have a test flight in the wild to really get to grips with it… watch this space!