Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
For 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
Ordnance Survey Ireland - Grid Reference Conversion
String Handling example from a Well House Consultants training course
More on String Handling [link]

This example is described in the following article(s):
   • Conversion of OSI grid references to Eastings and Northings - [link]
   • Learning Python - many new example programs - [link]

Source code: irish_grid Module: Y108
import re
import sys

""" A script to take Irish grid reference and
validate them / convert them to eastings and

Ordnance Surver Ireland use an Easting and
Northing grid system within 100 km squares
which are lettered, across the island as per
the table included in this file. There are
then a number of formats that the data can
be provided in.

This Python script takes some sample references
and validates them / reduces them to a standard
form. """

# Table of grid reference letters:


# O00850,74150 Newgrange Neothlothic Tomb
# or 300850,274150
# or O 008 741
# or O008741

# C95850,45450 Giant's Causeway, County Antrim

lookup = { "A":[0,4], "B":[1,4], "C":[2,4], "D":[3,4], "E":[4,4],
   "F": [0,3], "G":[1,3], "H":[2,3], "J":[3,3], "K":[4,3],
   "L": [0,2], "M":[1,2], "N":[2,2], "O":[3,2], "P":[4,2],
   "Q": [0,1], "R":[1,1], "S":[2,1], "T":[3,1], "U":[4,1],
   "V": [0,0], "W":[1,0], "X":[2,0], "Y":[3,0], "Z":[4,0]}

proper = re.compile(r'^(\d{1,6}),(\d{1,6})$')
lettered = re.compile(r'^([A-Za-z])(\d{1,5}),(\d{1,5})$')
short = re.compile(r'^([A-Za-z])\s*(\d{3})\s*(\d{3})$')

# No parameters - run some sample

gridrefs = ("123456,232313","117736,55432","H42314,53442",

# With parameters - use them

if len(sys.argv) > 1:
        gridrefs = sys.argv[1:]

for posn in gridrefs:
        got = proper.findall(posn)
        if got:
                easting = "%06d" % int(got[0][0])
                northing = "%06d" % int(got[0][1])
                got = lettered.findall(posn)
                if got:
                        fareast = lookup[got[0][0].upper()][0] * 100000
                        farnorth = lookup[got[0][0].upper()][1] * 100000
                        easting = "%06d" % (int(got[0][1]) + fareast)
                        northing = "%06d" % (int(got[0][2]) + farnorth)
                        got = short.findall(posn)
                        if got:
                                fareast = lookup[got[0][0].upper()][0] * 100000
                                farnorth = lookup[got[0][0].upper()][1] * 100000
                                easting = "%06d" % ((100 * int(got[0][1])) + fareast)
                                northing = "%06d" % ((100 * int(got[0][2])) + farnorth)

        if got:
                print(easting, northing, posn)

""" Sample Outout
WomanWithCat:y108 grahamellis$ python3 irish_grid
123456 232313 123456,232313
117736 055432 117736,55432
242314 353442 H42314,53442
534232 662313 534232,662313
nah hello
300850 274150 O00850,74150
295850 445450 C95850,45450
300800 274100 O008741
WomanWithCat:y108 grahamellis$ python2 irish_grid
('123456', '232313', '123456,232313')
('117736', '055432', '117736,55432')
('242314', '353442', 'H42314,53442')
('534232', '662313', '534232,662313')
('nah', 'hello')
('300850', '274150', 'O00850,74150')
('295850', '445450', 'C95850,45450')
('300800', '274100', 'O008741')
WomanWithCat:y108 grahamellis$


Learn about this subject
This module and example are covered on the following public courses:
 * Learning to program in Python
 * Python Programming
 * Intermediate Python
Also available on on site courses for larger groups

Books covering this topic
Yes. We have over 700 books in our library. Books covering Python are listed here and when you've selected a relevant book we'll link you on to Amazon to order.

Other Examples
This example comes from our "String Handling" training module. You'll find a description of the topic and some other closely related examples on the "String Handling" module index page.

Full description of the source code
You can learn more about this example on the training courses listed on this page, on which you'll be given a full set of training notes.

Many other training modules are available for download (for limited use) from our download centre under an Open Training Notes License.

Other resources
• Our Solutions centre provides a number of longer technical articles.
• Our Opentalk forum archive provides a question and answer centre.
The Horse's mouth provides a daily tip or thought.
• Further resources are available via the resources centre.
• All of these resources can be searched through through our search engine
• And there's a global index here.

Purpose of this website
This is a sample program, class demonstration or answer from a training course. It's main purpose is to provide an after-course service to customers who have attended our public private or on site courses, but the examples are made generally available under conditions described below.

Web site author
This web site is written and maintained by Well House Consultants.

Conditions of use
Past attendees on our training courses are welcome to use individual examples in the course of their programming, but must check the examples they use to ensure that they are suitable for their job. Remember that some of our examples show you how not to do things - check in your notes. Well House Consultants take no responsibility for the suitability of these example programs to customer's needs.

This program is copyright Well House Consultants Ltd. You are forbidden from using it for running your own training courses without our prior written permission. See our page on courseware provision for more details.

Any of our images within this code may NOT be reused on a public URL without our prior permission. For Bona Fide personal use, we will often grant you permission provided that you provide a link back. Commercial use on a website will incur a license fee for each image used - details on request.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2021: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/resources/ex.php • PAGE BUILT: Sun Oct 11 14:50:09 2020 • BUILD SYSTEM: JelliaJamb