Main Content
Conversion of OSI grid references to Eastings and Northings Archive - Originally posted on "The Horse's Mouth" - 2009-01-28 07:46:29 - Graham Ellis
In both Ireland and the UK, the Ordnance survey divides down the country into a series of lettered 100km x 100km squares, represented by 1 letter (Ireland) or 2 (UK mainland), and then measures / reports and Easting / Northing within that square.
Example:
ST645332 (UK, box ST, 64.5 km east, 33.2 km north)
J665543 (Ireland, box J, 66.5 km east, 54.3 km north)
For Graphic Information systems and for other comparisons, there's a need to remove the letter and add extra numbers onto the Easting and Northing to produce a pair of system wide co-ordinates, and due to the lack of a pattern in the letters, that's typically done with a lookup table.
Yesterday, I wrote a python program to do the conversion ...
Dorothy:d2 grahamellis$ ./irish_grid G552844
155200 384400 G552844
Dorothy:d2 grahamellis$
... which also handles a whole load of other formats (and reports to two more significant figures)
You are welcome to view / copy / use that program [source code here] which is an excellent expression of how regular expressions are used and captured in Python, and of course you are very welcome to come to our Python Course if you want to learn more about how to do this sort of thing yourself!
Illustration - Trim Castle [link]
P.S. Answer to a totally different question - look here for the class that lets you read Excel spread sheets into Python (Answer for course delegate!)
Some other articles
Y110 - File Handling with in Python - examples of use, and of defining your own context Scons - a build system in Python - building hello world Easy data to object mapping (csv and Python) Command line parameter handling in Python via the argparse module Running an operating system command from your Python program - the new way with the subprocess module Loving programming in Python - and ready to teach YOU how Shell, Awk, Perl of Python? Python or Lua - which should I use / learn? How can I do an FTP transfer in Python? A demonstration of how many Python facilities work together Python - fresh examples from recent courses Old prices - what would the equivalent price have been in 1966? Checking robots.txt from Python This article Reading a file multiple times - file pointers The elegance of Python Relative or absolute milkman Y105 - Functions, Modules and Packages From and Import in Python - where is the module loaded from? Embedding more complex code into a named block Nesting decorators Recursion in Python - the classic example What are callbacks? Why use them? An example in Python What is the difference between a function and a method? Reading command line parameters in Python A good example of recursion - a real use in Python Python - even named code blocks are objects Multiple yields and no loops in a Python generator? Python functions - an introduction to how they work Python varables - checking existance, and call by name or by value? Exception, Lambda, Generator, Slice, Dict - examples in one Python program vargs in Python - how to call a method with unknown number of parameters Optional positional and named parameters in Python Default local - a good choice by the author of Python Static variables in Python? Python timing - when to use a list, and when to use a generator Functions are first class variables in Lua and Python Finding all the unique lines in a file, using Python or Perl Python Packages - groupings of modules. An introduction Static variables in functions - and better ways using objects Passing optional and named parameters to python methods Catching the fishes first? Passing parameters to Python functions - the options you have Returning multiple values from a function call in various languages - a comparison Using an exception to initialise a static variable in a Python function / method Python - some common questions answered in code examples Passing a variable number of parameters in to a function / method Program for reliability and efficiency - do not duplicate, but rather share and re-use Optional and named parameters to Python functions/methods Python - access to variables in the outer scope Global and Enable - two misused words! Good example of recursion in Python - analyse an RSS feed Sample code with errors in it on our web site Optional parameters to Python functions Multiple returns from a function in Python This article Dynamic code - Python Optional and named parameters in Python What to do with a huge crop of apples Anonymous functions (lambdas) and map in Python Sharing variables with functions, but keeping them local too - Python Global - Tcl, PHP, Python Python Script - easy examples of lots of basics Returning multiple values from a function (Perl, PHP, Python) A better alternative to cutting and pasting code Function / method parameters with * and ** in Python It's the 1st, not the 1nd 1rd or 1th. Sludge off the mountain, and Python and PHP Python - A list of methods Recursion in Python Python - function v method Dynamic functions and names - Python Do not duplicate your code Cottage industry or production line data handling methods Python modules. The distribution, The Cheese Shop and the Vaults of Parnassus. Python - block insets help with documentation Python's Generator functions Difference between import and from in Python What is a callback? Code and code maintainance efficiency Call by name v call by value Lambdas in Python Python generator functions, lambdas, and iterators Distance Learning Variable Scope