Python
Using SDS AddressBase API as a UK-wide Geocoder

Update

Query the API using Python Requests

Lookup a single UPRN - try this out in python console to see how it works:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

# import the requests moduleimportrequests# set the endpoint url to the apiurl='https://address.digitalservices.surreyi.gov.uk/addresses'# set the header to include authorization method and the api_key headers={"Authorization":"Bearer my_secret_api_key"}# set parameters to add to urlparams={"format":"all","query":"all","uprn":10007088276}# make the requestr=requests.get(url,params=params,headers=headers)# view the response using requests json methodprint(r.json())# returns an unsorted list of data

Dealing with the response

To sort the returned json into something more readable use the python json module:

# first parse the response json so we can access the keysjson_parsed=json.loads(json.dumps(r.json()))# set keys to variablesuprn=json_parsed[0]['uprn']easting=json_parsed[0]['location']['easting']northing=json_parsed[0]['location']['northing']# print the valuesprint(uprn,easting,northing)# returns:# 10007088276 501423.0 152276.0

Automate Geocoding by UPRN

Here’s a script ready to go which will geocode a csv list of UPRNs, adding the easting & northing values, and appending the successful results into another csv.

What Next? Some ideas of how to use this

Use the UPRN to join values extracted from the geocoding back to your data.

Instead of writing to csv create a function to update the results back into your data. To connect python to PostGIS use psycopg2.

With QGIS 2.16 you can run this script direct from the python console. The requests, json, and csv modules are installed by default. Really easy for GIS users to have a UK wide UPRN AddressBase Geocoder now.