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!
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.
"wms_title" "Dans Layers and Stuff"
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.
COLOR 20 40 20
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.
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.
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).
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.
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.
In fact she took it so seriously she started compiling some pace notes!
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:
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"
%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 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.
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.
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.