Showing posts with label OSGB. Show all posts
Showing posts with label OSGB. Show all posts

2024-01-22

Finding a postcode

My GPS logger is going well (available here) and it logs journeys when back on home WiFi.

One of the requirements that someone requested was to list postcodes visited. I think it was a district nurse, and they need this for expense claims.

So, not a hard task, surely, making it find postcode where you are...

Postcodes are odd shapes

Sadly postcodes are not simple, and odd shapes, but the best I could easily do is find nearest postcode based on the centre for each postcode, which is published free by Ordnance Survey.

Notably the Dutch government does the same but with a bounding box which may help identify when there is ambiguity.

Microcontroller

The challenge is that this is a small micro controller. I cannot just "install mysql". I need a way to look up postcodes and finding the nearest. I cannot sensibly scan 1.7 million UK postcodes each time I want to log a postcode.

The "trick" is simple, make a grid. I picked 1km squares on the OSGB E/N grid. So not quite 1km but close enough. More on that later. That meant I could make a grid for the whole UK, and for each grid square I can make a list of possible postcodes with there centre.

That was simple enough, well, except...

Converting lat/lon to OSGB

First issue is, on device, converting GPS lat/lon to E/N OSGB. There are simple algorithms for this, I have seen them, but some are way off, and ultimately the "official" way to do it is make a guess and check a huge look up table of surveyed points (I think each 10km).

I did not want to do this on device, so my solution is convert the postcode list to lat/lon. This runs on linux and can use the official look up table, but gives me a postcode database which uses lat/lon.

Searching one cell

The next step was a simplification for search. I make each grid square (now based on lat/lon) have a list of postcodes with centre location to find the closest.

The problem is that, at the edge of a square, the closest may be in the next square. So I scan the edges of the squares, binary dividing to find all "near" postcodes for all points along it, whether in the target square or not. I then have all that are in the target square.

This means for each square I have a list of all postcodes that could be the closest postcode for any point in the square. This means some duplication from square to square but makes the lookup simple.

The whole lots ends up in one file. Look up the grid to find the list of postcodes, and scan the list for the closest. Simple.

2022-08-24

Grid reference

The UK, like many countries, have a simple system based on UTM for "grid reference" to say where you are. In the UK this is two letters to say the main map square and then digits. How many digits depends on how exact you want it. All digits (two letters and ten digits, so no worse than a phone number) gets you to the metre, ish.

Wikipedia covers this well (here).

One of the issues is correctly conveying the letters and numbers over an audio link. If you have any form of data, SMS, etc, you can convey an exact latitude and longitude, and that should always be preferred, but conveyed over a call, or radio, map references are all about people reading and typing the sequence of letters and numbers.

For example: SO 29902 14197

The sequence is no harder to convey than a phone number, which works well, but would help if there was a way of checking what you heard was right. There are alternative ways to convey location, but not with a proper check character, surprisingly.

So what would be really cool is if Ordnance Survey, or some international body, defined a check character for such things. Then apps, and the like could add that.

Make it SO 29902 14197 Q or some such where Q is the check. This could be a simple mod 23 of a sum/multiplier of the letters/numbers, or some such. But the idea is you can quote a location with a check. As a letter on the end (avoid O and I and Z to avoid confusion with digits) you have an optional, and obvious, check.

Please can they define this - it is simple, and no cost to do so, but means we could have apps and devices quoting grid references with an integrity check.


OK, I am going to go ahead and define a standard - it would be nice if Ordnance Survey approved it.

  • Each character (ignoring spaces) is assigned a value. For the letters is is 0 to 24 for the position in the 25 letters that are valid (all normal letters except I). Each digit has the value of the digit 0-9.
  • The value of each character is multiplied by the position (ignoring spaces), starting with the first character (letter) being 1, then 2, etc.
  • The result is added and then modulo 23 (a prime).
  • The check character is the position 0-22 in all letters (except I, O, and Z).

Some simple Eufymake tips 3D print over print

My Eufymake UV ink printer has issues, and so does dealing with Eufymake as a company, as I have blogged, but today, let's look at it wo...