.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.40) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Geo::Postcode 3pm" .TH Geo::Postcode 3pm "2021-01-07" "perl v5.32.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Geo::Postcode \- UK Postcode validation and location .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 2 \& use Geo::Postcode; \& my $postcode = Geo::Postcode\->new(\*(AqSW1 1AA\*(Aq); \& \& return unless $postcode\->valid; \& my ($n, $e) = ($postcode\->gridn, $postcode\->gride); \& \& # is the same as \& \& my ($n, $e) = $postcode\->coordinates; \& \& # and alternative to \& \& my @location = ($postcode\->lat, $postcode\->long); \& \& # or the impatient can skip the construction step: \& \& my ($n, $e) = Geo::Postcode\->coordinates(\*(AqSW1 1AA\*(Aq); \& \& my $clean_postcode = Geo::Postcode\->valid( $postcode ); \& \& my ($unit, $sector, $district, $area) = Geo::Postcode\->analyse(\*(AqSW1 1AA\*(Aq); .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" Geo::Postcode will accept full or partial \s-1UK\s0 postcodes, validate them against the official spec, separate them into their significant parts, translate them into map references or co-ordinates and calculate distances between them. .PP It does not check whether the supplied postcode exists: only whether it is well-formed according to British Standard 7666, which you can find here: .PP .Vb 1 \& http://www.govtalk.gov.uk/gdsc/html/frames/PostCode.htm .Ve .PP Geo::Postcode will also work with partial codes, ie areas, districts and sectors. They won't validate, but you can test them for legitimacy with a call to \f(CW\*(C`valid_fragment\*(C'\fR, and you can still turn them into grid references. .PP To work with \s-1US\s0 zipcodes, you need Geo::Postalcode instead. .SH "GRID REFERENCES AND DATA FILES" .IX Header "GRID REFERENCES AND DATA FILES" Any postcode, whether fully or partly specified, can be turned into a grid reference. The Post Office calls it a centroid, and it marks the approximate centre of the area covered by the code. .PP Unfortunately, and inexplicably, this information is not public domain: unless you're prepared to work at a very crude level, you have to buy location data either from the Post Office or a data shop. .PP This module does not come with any postcode data of its own, therefore, and so the coordinate and distance calculation facilities are unavailable unless you plug in your own database. .PP See the \s-1POD\s0 for Geo::Delivery::Location for how to override the standard data set something more comprehensive. .SH "INTERFACE" .IX Header "INTERFACE" This is a mostly vanilla \s-1OOP\s0 module, but for quick and dirty work you can skip the object construction step and call a method directly with a postcode string. It will build the necessary object behind the scenes and return the result of the operation. .PP .Vb 2 \& my @coordinates = Geo::Postcode\->coordinates(\*(AqLA23 3PA\*(Aq); \& my $postcode = Geo::Postcode\->valid($input\->param(\*(Aqpostcode\*(Aq)); .Ve .PP The object will not be available for any more requests, of course. .SH "INTERNALS" .IX Header "INTERNALS" The main Geo::Postcode object is very simple blessed hashref. The postcode information is stored as a four-element listref in \f(CW$self\fR\->{postcode}. Location information is retrieved by the separate Geo::Postcode::Location, which by default uses SQLite but can easily be overridden to use the database or other source of your choice. The location machinery is not loaded until it's needed, so you can validate and parse postcodes very cheaply. .SH "CONSTRUCTION" .IX Header "CONSTRUCTION" .SS "new ( postcode_string, location_class )" .IX Subsection "new ( postcode_string, location_class )" Constructs and returns the very simple postcode object. All other processing and loading is deferred. .PP You can also pass in a couple of parameters up front, as a hashref after the postcode: .PP .Vb 4 \& my $postcode = Geo::Postcode\->new(\*(AqSW1 1AA\*(Aq, { \& location_class => \*(AqMy::Location::Data::Class\*(Aq, \& distance_units => \*(Aqmiles\*(Aq, \& }) .Ve .PP This list will probably grow. .SS "postcode_string ( )" .IX Subsection "postcode_string ( )" Always returns the (uppercased) postcode string with which the object was constructed. Cannot be set after construction. .SS "fragments ( )" .IX Subsection "fragments ( )" Breaks the postcode into its significant parts, eg: .PP .Vb 1 \& EC1R 8DH \-\-> | EC | 1R | 8 | DH | .Ve .PP then stores the parts for later reference and returns them as a listref. Most other methods in this class call \f(CW\*(C`fragments()\*(C'\fR first to get their raw material. .SH "LOCATION" .IX Header "LOCATION" The first call to a location-related method of Geo::Postcode will cause the location class \- normally Geo::Postcode::Location \- to be loaded along with its data file, and a location object to be associated with this postcode object. We then pass all location-related queries on to the location object. .PP The accuracy of the information returned by location methods depends on the resolution of the location data file: see the \s-1POD\s0 for Geo::Postcode::Location for how to supply your own dataset instead of using the crude set that comes with this module. .SS "location ()" .IX Subsection "location ()" Returns \- and if necessary, creates \- the location object associated with this postcode object. This operation is avoided until explicitly requested, so that simple postcode-validation can be as economical as possible. The location object does all the work of looking up map reference data, calculating distances and translating into other forms. .SS "location_class ()" .IX Subsection "location_class ()" Sets and/or returns the full name of the class that should be called to get a location object. Calling \f(CW\*(C`location_class\*(C'\fR after a location object has been constructed will cause that object to be destroyed, so that the next call to a location-dependent method constructs a new object of the newly-specified class. .SS "default_location_class ()" .IX Subsection "default_location_class ()" Returns the name of the location class we'll use if no other is specified. The default default is Geo::Postcode::Location, but if you're subclassing you will probably want to replace that with one of your own. .SS "gridn () gride ()" .IX Subsection "gridn () gride ()" Return the \s-1OS\s0 grid reference coordinates of the centre of this postcode. .SS "gridref ()" .IX Subsection "gridref ()" Return the proper \s-1OS\s0 grid reference for this postcode, in classic \s-1AA123456\s0 style. .SS "lat () long ()" .IX Subsection "lat () long ()" Return the latitude and longitude of the centre of this postcode. .SS "placename () ward () nhsarea ()" .IX Subsection "placename () ward () nhsarea ()" These return information from other fields that may or may not be present in your dataset. The default set supplied with this module doesn't have these extra fields but a set derived from the \s-1PAF\s0 normally will. .SS "coordinates ()" .IX Subsection "coordinates ()" Return the grid reference x, y coordinates of this postcode as two separate values. The grid reference we use here are completely numerical: the usual \s-1OS\s0 prefix is omitted and an absolute coordinate value returned unless you get a stringy version from \f(CW\*(C`gridref()\*(C'\fR. .SS "distance_from ( postcode object or string, unit )" .IX Subsection "distance_from ( postcode object or string, unit )" Accepts a postcode object or string, and returns the distance from here to there. .PP As usual, you can call this method directly (ie without first constructing an object), or with any combination of postcode strings and objects: .PP .Vb 3 \& my $distance = Geo::Postcode\->distance_from(\*(AqLA23 3PA\*(Aq, \*(AqEC1Y 8PQ\*(Aq); \& my $distance = Geo::Postcode\->distance_from($postcode, \*(AqEC1Y 8PQ\*(Aq); \& my $distance = Geo::Postcode\->distance_from(\*(AqEC1Y 8PQ\*(Aq, $postcode); .Ve .PP Will do what you would expect, and the last two should be exactly the same. \f(CW\*(C`distance_between\*(C'\fR is provided as a synonym of \f(CW\*(C`distance_from\*(C'\fR to make that read more sensibly: .PP .Vb 1 \& my $distance = Geo::Postcode\->distance_between(\*(AqLA23 3PA\*(Aq, \*(AqEC1Y 8PQ\*(Aq); .Ve .PP In any of these cases you can supply an additional parameter dictating the units of distance: the options are currently 'miles', 'm' or 'km' (the default). .PP .Vb 1 \& my $distance = Geo::Postcode\->distance_between(\*(AqLA23 3PA\*(Aq, \*(AqEC1Y 8PQ\*(Aq, \*(Aqmiles\*(Aq); .Ve .PP The same thing can be accomplished by supplying a 'distance_units' parameter at construction time or, if you don't mind acting global, by setting \f(CW$Geo::Postcode::Location::units\fR. .SS "bearing_to ( postcode objects or strings)" .IX Subsection "bearing_to ( postcode objects or strings)" Accepts a list of postcode objects and/or strings, and returns a corresponding list of the bearings from here to there, as degrees clockwise from grid North. .SS "friendly_bearing_to ( postcode objects or strings)" .IX Subsection "friendly_bearing_to ( postcode objects or strings)" Accepts a list of postcode objects and/or strings, and returns a corresponding list of rough directions from here to there. '\s-1NW\s0', '\s-1ESE\s0', that sort of thing. .PP .Vb 2 \& print "That\*(Aqs " . $postcode1\->distance_to($postcode2) . " km " . \& $postcode1\->friendly_bearing_to($postcode2) . " of here."; .Ve .SH "VALIDATION" .IX Header "VALIDATION" Postcodes are checked against \s-1BS7666,\s0 which specifies the various kinds of sequences allowed and the characters which may appear in each position. .SS "valid ()" .IX Subsection "valid ()" If the postcode is well-formed and complete, this method returns true (in the useful form of the postcode itself, properly formatted). Otherwise, returns false. .SS "valid_fragment ()" .IX Subsection "valid_fragment ()" A looser check that doesn't mind incomplete postcodes. It will test that area, district or sector codes follow the rules for valid characters in that part of the postcode, and return true unless it finds anything that's not allowed. .SH "SEGMENTATION" .IX Header "SEGMENTATION" These methods provide the various sector, area and district codes that can be derived from a full postcode, each of which identifies a larger area that encloses the postcode area. .SH "analyse ()" .IX Header "analyse ()" Returns a list of all the codes present in this postcode, in descending order of specificity. So: .PP .Vb 1 \& Geo::Postcode\->analyse(\*(AqEC1Y8PQ\*(Aq); .Ve .PP will return: .PP .Vb 1 \& (\*(AqEC1Y 8PQ\*(Aq, \*(AqEC1Y 8\*(Aq, \*(AqEC1Y\*(Aq, \*(AqEC\*(Aq) .Ve .PP which is useful mostly for dealing with situations where you don't know what resolution will be available and need to try alternatives. We do this when location-finding, so as to be able to work with data of unpredictable or variable specificity (ie we are cheap and only buy very rough data sets, but people enter exact postcodes). .SH "area ()" .IX Header "area ()" Returns the area code part of this postcode. This is the broadest area of all and is identified by the first one or two letters of the code: 'E' or '\s-1EC\s0' or '\s-1LA\s0' or whatever. .SH "district ()" .IX Header "district ()" Returns the district code part of this postcode. This is also called the 'outward' part, by the post office: it consists of the first two or three characters and identifies the delivery office for this address. It will look like '\s-1LA23\s0' or '\s-1EC1Y\s0'. .SH "sector ()" .IX Header "sector ()" Returns the sector code part of this postcode. This is getting more local: it includes the first part of the code and the first digit of the second part, and is apparent used by the delivery office to sort the package. It will look something like '\s-1EC1Y 8\s0' or 'E1 7', and note that the space *is* meaningful. 'E1 7' and 'E17' are not the same thing. .SH "unit ()" .IX Header "unit ()" Returns the whole postcode, properly formatted (ie in caps and with a space in the right place, regardless of how it came in). .PP This is similar to what you get just by stringifying the postcode object, with the important difference that \fBunit()\fR will only work for a well-formed postcode: .PP .Vb 4 \& print Geo::Postcode\->unit(\*(AqLA233PA\*(Aq); # prints LA23 3PA \& print Geo::Postcode\->new(\*(AqLA233PA\*(Aq); # prints LA23 3PA \& print Geo::Postcode\->unit(\*(AqLA23333\*(Aq); # prints nothing \& print Geo::Postcode\->new(\*(AqLA23333\*(Aq); # prints LA23 .Ve .PP Whereas normal stringification \- which calls \f(CW\*(C`_as_string\*(C'\fR will print all the valid parts of a postcode. .SH "special_cases ()" .IX Header "special_cases ()" Returns a list of known valid but non-conformist postcodes. The only official one is 'G1R 0AA', the old girobank address, but you can override this method to extend the list. .SH "PLANS" .IX Header "PLANS" The next majorish version of this module will support (but not require) the interface offered by Geo::Postalcode, so that one can be dropped into the place of the other. Some methods will not be relevant, but I'll try and keep as close a match as I can. .SH "AUTHOR" .IX Header "AUTHOR" William Ross, wross@cpan.org .PP Development of this library is kindly supported by Amnesty International \s-1UK,\s0 who are pleased to see it distributed for public use but should not be held responsible for any shortcomings (or inadvertent copyright violations :). .SH "COPYRIGHT" .IX Header "COPYRIGHT" Copyright 2004 William Ross, spanner ltd. .PP This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.