The Chuffer

I just completed my second ever air engine: The Chuffer. With this project I decided that I’d go for complexity, rather than the simplistic approach I took when I built The Wobbler earlier this year.

The Chuffer is a twin-cylinder double acting engine – which means each of its two cylinders both push and pull the piston. This means it operates much more smoothly and can self-start as there’s always a piston on a power stroke, regardless of where the flywheel is.

Unlike its predecessor, the Chuffer has fixed cylinders and airflow is controlled by two spool valves, operated by eccentrics next to the flywheel via some pushrods and a hopelessly over-engineered lever mechanism.

And of course everything was made by yours truly, in the garage, from chunks of brass, steel and aluminium bar and flat stock I bought on eBay. Needless to say, I have learned a lot since I started the project this February!

What’s next? Well, maybe a Watt Governor or a little generator

Mapserver and Leaflet

Leaflet

Leaflet is a very simple but incredibly powerful Javascript mapping library that lets you add interactive maps to your website very easily. Try scrolling and zooming around this one:

For example, to add that map to this page, all I did was add the following code (after reading the Quick-Start Guide):

<div id="danMap" style="height: 200px;"></div>

<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.css" />
<script type="text/javascript" src="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.js"></script>

<script type="text/javascript">
var map = L.map('danMap').setView([51.4, -1.25], 13);	
L.tileLayer('http://{s}.tiles.mapbox.com/v3/YOUR.MAP.KEY/{z}/{x}/{y}.png', 
{
  attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="http://mapbox.com">Mapbox</a>',
  maxZoom: 18,
}).addTo(map);
</script>

You can add points, polygons and visualise all kinds of live data using a simple web service that returns some GeoJson data. It works like a charm on mobile devices too!

Why combine Leaflet with Mapserver?

I have a couple of use-cases that meant I needed to look at combining Leaflet with Mapserver. This turns out to be easy enough as Leaflet can hook up to any tile provider and Mapserver can be set up to serve images as a Web Map Service (WMS).

The first thing I wanted to do is serve up some map data when not connected to the internet. Imagine I am in the middle of nowhere, connected to the Raspberry Pi in the back of the Land Rover via WiFi to my phone or tablet. I have a GPS signal but I don’t have any connection to a map imagery server as there’s no mobile coverage. I need to use mapserver to render some local map data so I can see where I am. This use case has a boring work-related benefit too – it enables you to serve up maps in a web-based mapping application behind a strict corporate firewall.

The other use case is simple: raster data. Lots of the data we deal with where I work is served up as raster data by Mapserver. Imagine it as heat-maps of some KPI value layered on top of a street map.

Setting up Mapserver

There are a couple of things you need to do to get Mapserver to act as a WMS. The first is to add a projection and web metadata element to the root of the map file (as below). After a large amount of head-scratching, wailing and gnashing of teeth I found that the de-facto standard projection for all “internet” map data is EPSG 3857. Make sure you use that EPSG at the root of your map file.

PROJECTION
  "init=epsg:3857"
END
	
WEB
  METADATA
    "wms_title" "Dans Layers and Stuff"
    "wms_onlineresource" "http://192.168.2.164/cgi-bin/mapserv.exe?"
    "wms_enable_request" "*"
    "wms_srs" "EPSG:3857"
    "wms_feature_info_mime_type" "text/html"
    "wms_format" "image/png"
  END
END

The next thing to do is add some extra stuff to every layer in your map. You need to set the STATUS field to ‘on’; add a METADATA element and set the ‘wms_title’ to something sensible; and finally add a projection, specifying the projection the layer data is stored in. As I am using the OS VectorMap District dataset, which is on the OSGB projection I used EPSG 27700.

LAYER
  NAME         Woodland
  DATA         Woodland
  PROJECTION
    "init=epsg:27700"
  END
  METADATA
    "wms_title" "Woodland"
  END
  STATUS       on
  TYPE         POLYGON
  CLASS	
    STYLE
      COLOR 20 40 20
    END
  END
END 

Connecting it Together

You can then add a new layer to the Leaflet map, connected to your Mapserver. Here I’m using ms4w, the Windows version of Mapserver and hooking it up to a map file in my Dropbox folder. The map file I am using is the one I created for a previous post on mapserver.

L.tileLayer.wms("http://localhost:8001/cgi-bin/mapserv.exe?map=D:\\Dropbox\\Data\\Mapfiles\\leaflet.map", {
			layers: 'Roads,MotorwayJunctions',
			format: 'image/png',
			transparent: true,
			attribution: "Dan's Amazing Roads",
			maxZoom: 18,
			minZoom: 12,
		}).addTo(map);

Sadly I don’t have a mapserver instance on the internet, so all I can show here is a couple of screenshots. You’ll just have to take my word for it – it works brilliantly!

mapserverLeaflet2

mapserverLeaflet1

A sheared bolt and a mini adventure

On Thursday night last week my charge light didn’t go out. Since it was very wet I just assumed it was down to a slippy fan belt and decided to fix it at the weekend, provided the never-ending rains let up.
2014-02-16 15.02.07
After a huge fry-up at the farm, daughter and I set about fixing the problem – which turned out to be caused by the alternator completely snapping off! The 200Di conversion involved making a custom alternator bracket. Though the bracket itself is made from 10mm sheet steel welded to a chunky steel tube I made on the lathe, the weak spot is the M8 bolt which runs through it. This bolt had sheared off, leaving the alternator hanging loose. Luckily the fan belt didn’t fall off on the way home and I didn’t drive far enough to overheat the engine (the water pump must have stopped working as a result of the belt going slack).
2014-02-16 15.18.37
In the end I used a length of threaded bar instead of the stainless set screw that Emma is holding in the photo. Hopefully this will last a bit longer; stainless steel being notoriously brittle.
2014-02-16 16.39.27
I wasn’t allowed to start the engine “unless we’re going on a journey” so, once the fix was in, we headed up the road to Bucklebury Common. After the weeks of heavy rain and strong winds the common was pretty muddy and there were fallen trees and branches everywhere. I think the daughter enjoyed her introduction to Green Laning.
2014-02-16 16.39.30
In fact she took it so seriously she started compiling some pace notes!

The Wobbler

I made a Wobbler! A single acting, oscillating air engine, to be precise. I was inspired by the amazing Tubalcain, who’s YouTube channel has been the source of many top tips and the cause of many late nights. Though the design of the wobbler wasn’t new to me, the old Mamod steam engine I had when I was a kid used the same principle.

The wobbler is probably the simplest engine design there is. I tried to to a nice job of this one, though didn’t go overboard with the look of the thing – I don’t see the point of pretending it’s not made from some bits and bobs I has knocking about in the garage! Here are some photos:

2014-01-23 21.07.06

2014-01-23 22.04.21

2014-01-27 21.31.38

2014-01-27 22.03.00

2014-01-28 22.51.55

2014-01-29 22.14.48-1

2014-02-01 22.38.52

2014-02-01 22.39.06

Fixing the gear stick

For the last couple of weeks, gear changes in The Duke have been very hard work. The tiny pin which stops the stick spinning sheared off, adding a new element of drama to my commute.

image

The original pin had sheared and was stuck in the channel in the ball at the base of the stick.

image

So I had to strip the whole thing down.

image

The pin and the thread on the hole were totally ruined, so I had to think if something new.

image

I drilled out the hole

image

Then tapped it with an M8 thread. Much bigger and stronger than the old one.

image

Then turned down a set screw on the lathe

image

And heat treated it with case hardening compound, which will hopefully stop it getting squashed and bent.

image

I doubt there are many Land Rovers on the road with the original rubber blob on the base of the great stick. It’s meant to prevent vibrations but was totally worn away.

image

So I turned a replacement out of nylon and hammered it on.

image

At 19.3mm diameter is a snug fit on my spare gearbox and should help prevent rattles and make gears a bit easier to find.

image

All in all I’m very happy with my evening’s work. Just need the sun to come up so I can put it all back together again!

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="MSSQL:Server=tcp:yourserver.database.windows.net;Database=danTest;Uid=usernname@yourserver.database.windows.net;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.

PolygonsInSqlServer

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.

polygonsInKml

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

SET SqlConnectionString="MSSQL:Server=tcp:yourserver.database.windows.net;Database=danTest;Uid=usernname@yourserver.database.windows.net;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.

Combining Shape Files

This is one of those things that’s easy when you know how. Just so I don’t forget, here’s how to combine shape files using ogr2ogr.

I wrote it as a batch file to combine all the OSGB grid squares from the OS VectorMap District dataset into a single large data file for use with MapServer.

echo off

set OGR2OGR="C:\ms4w\tools\gdal-ogr\ogr2ogr"
set inputdir="D:\Dropbox\Data\OS VectorMap"
set outputdir="D:\Dropbox\Data\OS VectorMap Big"

set tiles=(HP HT HU HW HX HY HZ NA NB NC ND NF NG NH NJ NK NL NM NN NO NR NS NT NU NW NX NY NZ OV SC SD SE TA SH SJ SK TF TG SM SN SO SP TL TM SR SS ST TU TQ TR SV SW SX SY SZ TV)

set layers=(Airport AdministrativeBoundary Building ElectricityTransmissionLine Foreshore GlassHouse HeritageSite Land MotorwayJunction NamedPlace PublicAmenity RailwayStation RailwayTrack Road RoadTunnel SpotHeight SurfaceWater_Area SurfaceWater_Line TidalBoundary TidalWater Woodland)

del /Q %outputdir%\*.*

FOR %%L IN %layers% DO (
%OGR2OGR% %outputdir%\%%L.shp %inputdir%\SU_%%L.shp
)

FOR %%T IN %tiles% DO FOR %%L IN %layers% DO (
%OGR2OGR% -update -append %outputdir%\%%L.shp %inputdir%\%%T_%%L.shp -nln %%L
)

After a little map file jiggery-pokery I can now render a huge map of the UK or tiles with smaller maps without the many layer definitions needed to use ~20 shape file sets.

london-zoomed

london-big

uk-big