Created
May 22, 2026 16:43
-
-
Save walkermatt/203b95166e592614e28501e9a1ac44c0 to your computer and use it in GitHub Desktop.
Convert an OSGB grid ref with a quadrant to one without (TQ27SE to TQ2570)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def quad2num(ref): | |
| ref = ref.upper() | |
| grid = ref[:2] | |
| assert grid.isalpha() | |
| nums = ref[2:-2] | |
| if nums: | |
| assert nums.isdigit() | |
| quad = ref[-2:] | |
| assert quad.isalpha() | |
| num_nums = len(nums) | |
| assert num_nums % 2 == 0 | |
| nums_x = nums[:int(num_nums/2)] | |
| nums_y = nums[int(num_nums/2):] | |
| quad_lookup = {"N": "5", "E": "5", "S": "0", "W": "0"} | |
| quad_x = quad_lookup[quad[1]] | |
| quad_y = quad_lookup[quad[0]] | |
| return grid + nums_x + quad_x + nums_y + quad_y | |
| if __name__ == '__main__': | |
| ref = input().strip() | |
| print(quad2num(ref), end="") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment