Geo::C...::Grid(3pm)
okay bestie, welcome to Geo::C...::Grid(3pm) β that's the manual name, from the User Contributed Perl Documentation side of the internet. She's giving British National Grid references, and she's lowkey iconic. π
β¨
π NAME π
Geo::Coordinates::OSGB::Grid β Format and parse British National Grid references. fr fr, that's the whole vibe.
β VERSION β
2.20 β periodt, that's the version bestie.
π¦ SYNOPSIS π¦
use Geo::Coordinates::OSGB::Grid qw/parse_grid format_grid/;
my ($e,$n) = parse_grid('TQ 23451 09893');
my $gr = format_grid($e, $n); # "TQ 234 098"
β¨ DESCRIPTION β¨
okay so this module is literally your go-to for parsing and formatting OSGB grid references. Useful functions? Obsessed. π«Ά
If you want the deep lore, some detailed background is given in "background.pod" and on the OS web site. iykyk. π
β¨ SUBROUTINES AND METHODS β¨
π format_grid(e, n) π
okay so bestie, format_grid is literally the moment you take an (easting, northing) pair and turn it into the classic full national grid reference β two letters and two sets of three numbers, like SU 387 147. it's giving map nerd, and we love that for us. π
$gridref = format_grid(438710.908, 114792.248); # SU 387 147
if you want the individual components, just call it in list context, bestie:
($sq, $e, $n) = format_grid(438710.908, 114792.248); # ('SU', 387, 147)
no cap, the easting and northing are not rounded β they're truncated to hectometres (because the OS system said so), so the grid reference points to the lower left corner of that 100m square. the system is described below the legend on all OS Landranger maps. periodt.
the routine takes an optional third argument to control the form of grid reference returned. it should be a hash reference with one or more of the keys below (defaults included).
format_grid(e, n, {form => 'SS EEE NNN', maps => 0, series => 'ABCHJ'})
πΈ Options for format_grid πΈ
form β this controls the format of the grid reference. with $e, $n set as above:
Format produces Format produces
----------------------------------------------------------------
'SS' SU
'SSEN' SU31 'SS E N' SU 3 1
'SSEENN' SU3814 'SS EE NN' SU 38 14
'SSEEENNN' SU387147 'SS EEE NNN' SU 387 147
'SSEEEENNNN' SU38711479 'SS EEEE NNNN' SU 3871 1479
'SSEEEEENNNNN' SU3871014792 'SS EEEEE NNNNN' SU 38710 14792
you can't leave out the SS, you can't put N before E, and the number of Es and Ns has to match. fr fr.
there are two other special formats:
form => 'TRAD' is equivalent to form => 'SS EEE NNN'
form => 'GPS' is equivalent to form => 'SS EEEEE NNNNN'
in a list context, this option means the individual components come back appropriately truncated as shown. so with SS EEE NNN you get ('SU', 387, 147) and not ('SU', 387.10908, 147.92248). the format can be upper case, lower case, or a mix β she's flexible. if you just want the local easting and northing without the grid square, grab the individual parts in list context and format them yourself:
my $gr = sprintf('Grid ref %2$s %3$s on Sheet %4$s', format_grid_landranger($e, $n))
# returns: Grid ref 387 147 on Sheet 196
maps β controls whether to include a list of map sheets after the grid reference. set it to 1 (or any true value) to include the list, and to 0 (or any false value) to leave it out. default is maps => 0.
scalar context gives you a string like this:
SU 387 147 on A:196, B:OL22E, C:180
list context gives you a list like this:
('SU', 387, 147, A:196, B:OL22E, C:180)
series β only used when maps is true. it controls which series of maps to include in the list of sheets. currently the series included are:
"A": OS Landranger 1:50000 maps"B": OS Explorer 1:25000 maps (some of these are designated asOutdoor Leisuremaps)"C": OS Seventh Series One-Inch 1:63360 maps"H": Harvey British Mountain maps - mainly at 1:40000"J": Harvey Super Walker maps - mainly at 1:25000
so if you only want Explorer maps use: series => 'B', and if you want only Explorers and Landrangers use: series => 'AB', and so on. you get the vibe.
note that the numbers returned for the Harvey maps have been invented for the purposes of this module. they do not appear on the maps themselves; instead the maps have titles. you can use the numbers returned as an index to the data in Geo::Coordinates::OSGB::Maps to find the appropriate title. π
π« format_grid_trad(e,n) π«
equivalent to format_grid(e,n, { form => 'trad' }). she's just a little alias, bestie.
π« format_grid_GPS(e,n) π«
equivalent to format_grid(e,n, { form => 'gps' }).
π« format_grid_map(e,n) π«
equivalent to format_grid(e,n, { maps => 1 }).
π« format_grid_landranger(e,n) π«
equivalent to
format_grid(e,n,{ form => 'ss eee nnn', maps => 1, series => 'A' })
except that the leading A: will be stripped from any sheet names returned, and you get a slightly fancier set of phrases in a scalar context depending on how many map numbers are in the list of sheets. fancy, right? β¨
π parse_grid π
the parse_grid routine extracts an (easting, northing) pair from a string or a list of arguments representing a grid reference. the pair returned are in metres from the false origin of the grid, so you can pass them to format_grid or grid_to_ll. iconic.
the arguments should be in one of these forms:
- a single string representing a grid reference
String -> interpreted as
--------------------------------------------------
parse_grid("TA 123 678") -> (512300, 467800)
parse_grid("TA 12345 67890") -> (512345, 467890)
spaces are optional in all cases, bestie. you can also refer to a 100km square as TA which returns (500000,400000), a 10km square as TA16 which returns (510000, 460000), or a kilometre square as TA1267 which gives (512000, 467000). for completeness you can also use TA 1234 6789 to refer to a decametre square (512340, 467890) but honestly you might struggle to find a use for that one. not me saying that though. π
- a list representing a grid reference
List -> interpreted as
-----------------------------------------------------
parse_grid('TA', 0, 0) -> (500000, 400000)
parse_grid('TA', 123, 678) -> (512300, 467800)
parse_grid('TA', 12345, 67890) -> (512345, 467890)
parse_grid('TA', '123 678') -> (512300, 467800)
parse_grid('TA', '12345 67890') -> (512345, 467890)
if you're processing grid references from some external data source, beware: if you use a list with bare numbers you may lose any leading zeros for grid references close to the SW corner of a grid square. this can lead to some ambiguity. either make the numbers into strings to preserve the leading digits, or supply a hash of options as a fourth argument with the figs option to define how many figures are supposed to be in each easting and northing. like this:
List -> interpreted as
-------------------------------------------------------------
parse_grid('TA', 123, 8) -> (512300, 400800)
parse_grid('TA', 123, 8, { figs => 5 }) -> (500123, 400008)
the default setting of figs is 3, which assumes you're using hectometres as in a traditional grid reference.
- a string or list representing a map sheet and a grid reference on that sheet
Map input -> interpreted as
----------------------------------------------------
parse_grid('A:164/352194') -> (435200, 219400)
parse_grid('B:OL43E/914701') -> (391400, 570100)
parse_grid('B:OL43E 914 701') -> (391400, 570100)
parse_grid('B:OL43E','914701') -> (391400, 570100)
parse_grid('B:OL43E',914,701) -> (391400, 570100)
again spaces are optional, but you need some non-digit between the map identifier and the grid reference. there are also some constraints: the map identifier must be one defined in Geo::Coordinates::OSGB::Maps; and the following grid reference must actually be on the given sheet. note also that you need to supply a specific sheet for a map that has more than one. the given example would fail if the map was given as B:OL43, since that map has two sheets: B:OL43E and B:OL43W. so specific, bestie.
if you give the identifier as just a number, it's assumed you wanted a Landranger map;
parse_grid('176/224711') -> (522400, 171100)
parse_grid(164,513,62) -> (451300, 206200)
parse_grid will croak of you pass it a sheet identifier that is not defined in Geo::Coordinates::OSGB::Maps. it will also croak if the supplied easting and northing are not actually on the sheet. so don't test her. π
if you just want the corner of a particular map, just pass the sheet name:
parse_grid('A:82') -> (195000, 530000)
parse_grid(161) -> (309000, 205000)
again, it's assumed that you want a Landranger map. the grid reference returned is the SW corner of the particular sheet. this is usually obvious, but less so for some of the oddly shaped 1:25000 sheets, or Harvey's maps. what you actually get is the first point defined in the maps polygon, as defined in Maps. if in doubt you should work directly with the data in Geo::Coordinates::OSGB::Maps. periodt.
π«Ά parse_trad_grid(grid_ref) π«Ά
this is included only for backward compatibility. it's now just a synonym for parse_grid. she's legacy but still cute.
π«Ά parse_GPS_grid(grid_ref) π«Ά
this is included only for backward compatibility. it's now just a synonym for parse_grid.
π«Ά parse_landranger_grid(sheet, e, n) π«Ά
this is included only for backward compatibility. it's now just a synonym for parse_grid.
π«Ά parse_map_grid(sheet, e, n) π«Ά
this is included only for backward compatibility. it's now just a synonym for parse_grid.
π² random_grid([sheet1, sheet2, ...]) π²
takes an optional list of map sheet identifiers, and returns a random easting and northing for some place covered by one of the maps. there's no guarantee that the point will not be in the sea, but it will be within the bounding box of one of the maps. so it's a vibe, not a guarantee.
- if you omit the list of sheets, then one of map sheets defined in
Geo::Coordinates::OSGB::Mapswill be picked at random. - as a convenience whole numbers in the range 1..204 will be interpreted as Landranger sheets, as if you had written
A:1,A:2, etc. - any sheet identifiers in the list that are not defined in
Geo::Coordinates::OSGB::Mapswill be (silently) ignored. - the easting and northing are returned as meters from the grid origin, so that they are suitable for input to the
format_gridroutines.
β¨ EXAMPLES β¨
okay bestie, this is the part where we actually do stuff. watch and learn π
use Geo::Coordinates::OSGB::Grid
qw/parse_grid
format_grid
format_grid_landranger/;
# Get full coordinates in metres from GR
my ($e,$n) = parse_grid('TQ 23451 09893');
# Reading and writing grid references
# Format full easting and northing into traditional formats
my $gr1 = format_grid($e, $n); # "TQ 234 098"
my $gr2 = format_grid($e, $n, { form => 'SSEEENNN' } ); # "TQ234098"
my $gr3 = format_grid($e, $n, { form => 'SSEEEEENNNNN'} ); # "TQ 23451 09893"
my $gr4 = format_grid($e, $n, { form => 'gps'} ); # "TQ 23451 09893"
my $gr5 = format_grid_landranger($e, $n);# "TQ 234 098 on Landranger sheet 198"
# or call in list context to get the individual parts
my ($sq, $ee, $nn) = format_grid($e, $n); # ('TQ', 234, 98)
# parse routines to convert from these formats to full e,n
($e,$n) = parse_grid('TQ 234 098');
($e,$n) = parse_grid('TQ234098'); # spaces optional
($e,$n) = parse_grid('TQ',234,98); # or even as a list
($e,$n) = parse_grid('TQ 23451 09893'); # as above..
# You can also get grid refs from individual maps.
# Sheet between 1..204; gre & grn must be 3 or 5 digits long
($e,$n) = parse_grid(176,123,994);
# put leading zeros in quotes
($e,$n) = parse_grid(196,636,'024');
For more examples of parsing and formatting look at the test files. π
π BUGS AND LIMITATIONS π
ngl the useful area of these routines is confined to the British Isles, not including Ireland or the Channel Islands. it's giving "we made a choice" π
but wait β very little range checking is done, so you can absolutely generate pseudo grid references for points that are some way outside this useful area. like, for example we have St Peter Port in Guernsey at "XD 611 506" and Rockall at "MC 035 165". yes fr fr.
the working area runs from square "AA" in the far north west to "ZZ" in the far south east. in WGS84 terms the corners run from:
- 64.75N 32.33W (Iceland)
- 65.8N 22.65E (Norway)
- 44.5N 11.8E (Venice)
- 44N 19.5W (the Western Approaches)
so yeah, this is something of a geodesy toy rather than a useful function. no cap. π§Έ
π©Ί DIAGNOSTICS π©Ί
Messages from format_grid
in case of error format_grid will die with a message. π΅ possible messages are:
Format ... was not recognizedβ the format code you supplied with{ form => ... }did not match any of the expected patterns.Too far off the grid: ...β the (easting, northing) pair you supplied are too far away from the OS grid to be formatted with a valid grid square letter combination.
Messages from parse_grid
in case of error parse_grid will die with one of the following messages:
No easting or northing foundβ this means you passed something more than a 2-letter grid square but there were no numbers found in the latter part of the string.Easting and northing have different lengths in ...β the easting and northing you supply must have same length to avoid ambiguity.Too many digits in ...β you have supplied more than 10 digits. girl that's too many digits πGrid reference .... is not on sheet ...β you can get this if you pass a map sheet identifier and a short grid ref, but the grid ref is not actually on that particular sheet.Failed to parse a grid reference from ...β this is the catch all message issued if none of the patterns matches your input.
π«Ά If you get an unexpected result from any of these subroutines, please generate a test case to reproduce your result and get in touch to ask me about it.
βοΈ CONFIGURATION AND ENVIRONMENT βοΈ
there is no configuration required either of these modules or your environment. it should work on any recent version of Perl better than 5.10, on any platform. iconic, honestly. β¨
π¦ DEPENDENCIES π¦
Perl 5.10 or better. that's it. that's the list. π
π« INCOMPATIBILITIES π«
none known. we love a clean slate π
π LICENSE AND COPYRIGHT π
Copyright (C) 2002-2017 Toby Thurston
OSTN02 transformation data included in this module is freely available from the Ordnance Survey but remains Crown Copyright (C) 2002
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
π AUTHOR π
Toby Thurston -- 30 Jul 2017
toby@cpan.org
π SEE ALSO π
See Geo::Coordinates::OSGB.
aaaand that's a wrap bestie!! π this page is from perl v5.36.0 and it was last touched on 2022-12-06, so go forth and format those grid refs. slay, bye!! πβ¨π¦
π¦ where this page comes from
- debian bookworm: libgeo-coordinates-osgb-perl 2.20-2 Β· GPL-2.0-or-later
- debian trixie: libgeo-coordinates-osgb-perl 2.20-2 Β· GPL-2.0-or-later
- ubuntu noble: libgeo-coordinates-osgb-perl 2.20-2 Β· GPL-2.0-or-later
- ubuntu resolute: libgeo-coordinates-osgb-perl 2.20-2 Β· GPL-2.0-or-later
- ubuntu stonking: libgeo-coordinates-osgb-perl 2.20-2 Β· GPL-2.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms