Open Charge Map APIΒΆ

import requests
import collections
# assign API key to variable

ocm_key = '****'
# test query for GB (Great Britain), 100 results, as single url

call = 'https://api.openchargemap.io/v3/poi/?key=ocm_key&output=json&countrycode=GB&maxresults=100&compact=true&verbose=false'
req = requests.get(call)
req
<Response [200]>
# all charging points in a 20 mile radius from Jomec

params = {"key": ocm_key,
          "countrycode": "GB", 
          "output": "json", 
          "latitude": 51.476629314550514, 
          "longitude": -3.1808810080300978, 
          "distance":20,
          "distanceunit": 'Miles',
          "compact": True, 
          "verbose": False, 
          "maxresults": 2_000}

endpoint = 'https://api.openchargemap.io/v3/poi/?'

req = requests.get(endpoint, params = params)
print(req)
<Response [200]>
# how many items were returned?

len(req.json())
180
# assign to variable and examine first item

data = req.json()
data[0]
{'IsRecentlyVerified': True,
 'DateLastVerified': '2022-08-23T11:08:00Z',
 'ID': 200352,
 'UUID': '001E7150-DAB4-4035-81DA-440500D3D0D8',
 'DataProviderID': 18,
 'DataProvidersReference': 'beca63abda9d65595fafd17918307c03',
 'OperatorID': 3341,
 'UsageTypeID': 5,
 'AddressInfo': {'ID': 200711,
  'Title': 'Mardy Street',
  'AddressLine1': 'Mardy Street',
  'Town': 'Cardiff',
  'Postcode': 'CF11 6RD',
  'CountryID': 1,
  'Latitude': 51.474088,
  'Longitude': -3.185956,
  'AccessComments': '',
  'RelatedURL': 'http://www.aptcontrols.co.uk',
  'Distance': 0.2802499324048852,
  'DistanceUnit': 2},
 'Connections': [{'ID': 334239,
   'ConnectionTypeID': 25,
   'Reference': 'SEC20034',
   'StatusTypeID': 50,
   'LevelID': 2,
   'Amps': 32,
   'Voltage': 230,
   'PowerKW': 7.0,
   'CurrentTypeID': 10},
  {'ID': 334240,
   'ConnectionTypeID': 25,
   'Reference': 'SEC20034a',
   'StatusTypeID': 50,
   'LevelID': 2,
   'Amps': 32,
   'Voltage': 230,
   'PowerKW': 7.0,
   'CurrentTypeID': 10}],
 'StatusTypeID': 0,
 'DateLastStatusUpdate': '2022-08-23T11:08:00Z',
 'MetadataValues': [{'ID': 20774,
   'MetadataFieldID': 4,
   'ItemValue': 'Contains public sector information licensed under the Open Government Licence v2.0. http://www.nationalarchives.gov.uk/doc/open-government-licence/version/2/'}],
 'DataQualityLevel': 3,
 'DateCreated': '2022-08-23T11:08:00Z',
 'SubmissionStatusTypeID': 100}
# assemble list of postcodes

pc_list = []

for x in data:
    
    pc = x['AddressInfo']['Postcode']
    pc_list.append(pc)

print(len(pc_list))
180
# 20 mile radius reaches over to Bristol - bit far!

for x in pc_list:
    if x.startswith('BS') == True:
        print(x)
BS22 9UH
BS23 1JP
BS23 1UA
BS23 1UA
BS23 3PT
BS23 3YN
BS23 3YX
BS22 6AQ
BS21 7RH
BS21 7TU
BS21 6RR
BS24 8EE
BS24 7DX
BS21 6LH
BS22 7XN
BS22 6DB
BS20 8JJ
BS49 5AD
BS49 5AD
BS240JB
BS20 7DB
BS20 7AJ
BS20 7DE
BS20 7GD
BS24 0JL
BS26 2UF
BS48 4AH
BS48 1AQ
BS20 7TN
# how many postcodes are CF, Cardiff?
        
len([x for x in pc_list if x.startswith('CF') == True])
91
# Try 200 queries on the UK

params = {"key": ocm_key,
          "countrycode": "GB", 
          "output": "json", 
          "compact": True, 
          "verbose": False, 
          "maxresults": 200}

endpoint = 'https://api.openchargemap.io/v3/poi/?'

req = requests.get(endpoint, params = params)
data = req.json()
print(req)
<Response [200]>
# empty lists to collect cities, and postcodes (and the IDs of problem entries)

city_list = []
pc_list = []
problem_list = []

for x in data:
    
    try:
        town = x['AddressInfo']['Town']
        pc = x['AddressInfo']['Postcode']

        city_list.append(town)
        pc_list.append(pc)
    
    except:
        problem_list.append(x['ID'])
        pass

# build a frequency distribution dictionary for cities, then postcodes
counter = collections.Counter(city_list)
city_freq_dict = dict(counter)

counter = collections.Counter(pc_list)
pc_freq_dict = dict(counter)
# check the number of towns and postcodes gathered, from the 200 entries

print(len(city_list))
print(len(pc_list))

# and the number of problems
print(len(problem_list))
198
198
2
# from a (not at all random) sample of 200 entries, how are UK cities represented?

dict(sorted(city_freq_dict.items(), key=lambda item: item[1], reverse = True))
{'Cherwell': 8,
 'Edinburgh': 5,
 'Aberdeen': 4,
 'Swindon': 3,
 'West Oxfordshire': 3,
 'Banbury': 3,
 'Bicester': 3,
 'London': 3,
 'Liverpool': 3,
 'Glasgow': 3,
 'Dundee': 3,
 'Birmingham': 3,
 'Chippenham': 3,
 'Manchester': 2,
 'Thirsk': 2,
 'Bristol': 2,
 'Stirling': 2,
 'Yeovil': 2,
 'Colchester': 2,
 'Renfrew': 2,
 'Bradford': 2,
 'Wyre': 2,
 'Fylde': 2,
 'Stamford': 2,
 'Saint Helens': 2,
 'Llangefni': 2,
 'Montrose': 2,
 'Isle of North Uist': 2,
 'Portree': 2,
 'Alexandria': 2,
 'Dalkeith': 2,
 'Bathgate': 2,
 'Paisley': 2,
 'Bellshill': 1,
 'Barnstaple': 1,
 'Kinross': 1,
 'Busher Walk': 1,
 'Slough': 1,
 'Stranraer': 1,
 'Bentham': 1,
 'Knottingley': 1,
 'Broadstairs': 1,
 'Nuneaton': 1,
 'Bruton': 1,
 'Cleckheaton': 1,
 'Portsoy': 1,
 'North Devon': 1,
 'Darlington': 1,
 'Basingstoke': 1,
 'Trafford': 1,
 'Talbot Green': 1,
 'Pengam Green': 1,
 'Exeter': 1,
 'Stow-on-the-Wold': 1,
 'Marlborough': 1,
 'Eastville': 1,
 'Bordon': 1,
 'Hungerford': 1,
 'Ingleton': 1,
 'Towyn Road': 1,
 'Crosshills': 1,
 'Gargrave': 1,
 'Priory Way': 1,
 'Burslem': 1,
 'Avenue Two': 1,
 'Spring Gardens': 1,
 'Brixton Hill': 1,
 'Chesterfield': 1,
 'Langley Moor': 1,
 'Painters Forstal': 1,
 'Llanwnda': 1,
 'Brixton': 1,
 'Wick': 1,
 'Hawick': 1,
 'South Cambridgeshire': 1,
 'Salford': 1,
 'Balsall Heath': 1,
 'Bispham': 1,
 'Lytham Saint Annes': 1,
 'Newark': 1,
 'South Cliff': 1,
 '3rd Floor': 1,
 'Houghton Regis': 1,
 'Droitwich Road': 1,
 'Tiverton': 1,
 'Sudbury': 1,
 'Hayling Island': 1,
 'Robertsbridge': 1,
 'Isleworth': 1,
 'Retail Park': 1,
 'Foleshill': 1,
 'Squires Lane': 1,
 'Battle': 1,
 'Tunstall': 1,
 'Watton': 1,
 'Weybridge': 1,
 'Pontardawe': 1,
 'Stockton-on-Tees': 1,
 ' Gosport': 1,
 'Hereford': 1,
 'Bracknell': 1,
 'Worcester': 1,
 'Radcliffe': 1,
 'Darnell': 1,
 'Aberdare': 1,
 'Milford on Sea': 1,
 'Penparcau': 1,
 'Cardiff': 1,
 'Middlesbrough': 1,
 'Seaham': 1,
 'Pwllheli': 1,
 'Amlwch': 1,
 'Holyhead': 1,
 'Menai Bridge': 1,
 'Bournemouth': 1,
 'Lower Village': 1,
 'Ellon': 1,
 'Kirkwall': 1,
 'Lairg': 1,
 'Thurso': 1,
 'The Links, Golspie Business Pa': 1,
 'Isle of Harris': 1,
 'Fraserburgh': 1,
 'Inverness': 1,
 'Aberfeldy': 1,
 'Perth': 1,
 'Lochearnhead': 1,
 'Blair Drummond': 1,
 'Kirkcaldy': 1,
 'North Berwick': 1,
 'Whitekirk': 1,
 'Brightons ': 1,
 'Falkirk': 1,
 'Dunbar': 1,
 'Longniddry': 1,
 'Croy Park and Ride': 1,
 'Prestonpans': 1,
 'Haddington': 1,
 'Macmerry': 1,
 'Newbridge': 1,
 'Inveresk': 1,
 'Clydebank': 1,
 'Johnstone': 1,
 ' Berwick-upon-Tweed': 1}