Interpreter constrained for the form "Location" returns coordinates of a geographic location.

For example

Interpreter["Location"]["Mount Everest"]
GeoPosition[{27.9881, 86.9253}]

That seemly works for some UK Postcodes

Interpreter["Location"] /@ {"NW6 1PG", "UB6 0HY", "W6 0RF"}
 { GeoPosition[{51.5472, -0.191049}],
   GeoPosition[{51.5558, -0.334926}],  
   GeoPosition[{51.4941, -0.234571}] }

but strangely not for others

Interpreter["Location"] /@ {"NW6 1EH", "NW6 1UE", "NW6 5AE"}

enter image description here

What alternative methods are there to get a GeoPosition from a UK Postcode?


1 Answer 1


The problem

This problem has been acknowledged by Wolfram Technical Support [CASE:3913717] (July 2017)

"[...] at this point in time, they haven't optimized interpreter to handle these inputs [in Mathematica 11.1]. As such it isn't surprising that some may work, but we are unable to guarantee that they all will.[...] Further conversion of these postal codes to GeoPositions would require a custom interpreter. "

A solution

There is a Mathematica package by Arnoud Buzing that uses a web API from api.postcodes.io. The packages is available at github.com/arnoudbuzing/postcode.

A representative function of the package (with some modifications) is

postcodes[codes : List[_String ..]] := Module[
  {request, response},
  request = HTTPRequest[
    , <|
     Method -> "POST"
     , "Body" -> {"postcodes" -> codes}
     , "Headers" -> {"content-type" -> "application/x-www-form-urlencoded;charset=UTF-8", 
       "user-agent" -> "Wolfram HTTPClient 11.1"}
  response = URLRead[request];
  ImportString[response["Body"], {"RawJSON"}]["result"]]

The API allows list of only 100 code per query, so this needs to be modified to divide longer lists in smaller chunks. Several other related functionalities are also available.


Interpreter["Location"] /@ {"NW65AR", "OX49 5NU"}
{GeoPosition[{51.5313, -0.195886}], GeoPosition[{51.6595, -1.07229}]}
Query[All, "result", GeoPosition[#["latitude"], #["longitude"]] &][
 postcodes[{"NW65AR", "OX49 5NU"}]]
{GeoPosition[51.6559, -1.06977], GeoPosition[51.533, -0.197077]}


Beware that the GeoPosition obtained by different methods are not identical. Interpreter["Location"] provides incorrect coordinates.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.