Combining it all to create the maps#

In the previous page the decisions made to produce the individual countries of UK were shown.

In this part I will combine the results of the previous part as well as:

  • Create a map of the UK including all four individual countries

  • Create a way to map postcodes to an area in this map

from MapUtils import *
Traceback (most recent call last):

  File ~\anaconda3\envs\ons-env\lib\site-packages\IPython\core\interactiveshell.py:3433 in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)

  Cell In[1], line 1
    from MapUtils import *

  File ~\Documents\GitHub\PesticideDocs\MapUtils.py:65
    postcode_df.to_csv(postcode_to_region.csv")
                                               ^
SyntaxError: EOL while scanning string literal

Create the combined json file for all UK countries#

url_data_paths = ['https://www.doogal.co.uk/kml/counties/Counties.kml',
                  '.\\_data\\scotland_preg_2011.KML',
                  '.\\_data\\WalesDistrict.kml',
                  'https://www.doogal.co.uk/kml/UkPostcodes.kml']
                  
country=['England', 'Scotland', 'Wales', 'Northern Ireland']

m, df_map_pc =url_KML_map(url_data_paths, 'combined_json', doScotWales=country)

Create a dataframe of UK postcodes to match map regions#

def load_pcode_csvs():
    """
    Loads the csv files for poscodes in different countries in UK
    Runs _postcodeDF_additionalProcessing for additional processing
    The main goal is to have a file with all postcodes each having a region name 
        that can be plotted
        
    Args: None (could add column to load instead of Population?)
    Returns: 
        A pandas dataframe with values for each postcode and an area name to plot to
    
    """

    import re

    file_paths=["C:\\Users\\44781\\Documents\\Github\\PesticideDocs\\_data\\England1 postcodes.csv",
                "C:\\Users\\44781\\Documents\\Github\\PesticideDocs\\_data\\England2 postcodes.csv",
                "C:\\Users\\44781\\Documents\\Github\\PesticideDocs\\_data\\Wales postcodes.csv",
                "C:\\Users\\44781\\Documents\\Github\\PesticideDocs\\_data\\NRScotland-SmallUser.csv", #has column for maps
    #             ".\\_data\\Scotland postcodes.csv",
                "C:\\Users\\44781\\Documents\\Github\\PesticideDocs\\_data\\BT postcodes.csv",]

    postcode_df = pd.DataFrame()
    for file in file_paths:
        print(file)
        if re.search(r"NRScotland-SmallUser",file):
            df_temp = pd.read_csv(file, 
                     usecols=['Postcode','ScottishParliamentaryRegion2021Code','CensusPopulationCount2011'])
            df_temp.rename(columns={'ScottishParliamentaryRegion2021Code':'District Code',
                                   'CensusPopulationCount2011':'Population'},inplace=True)
            df_temp['Country'] = 'Scotland'

        else:
            df_temp = pd.read_csv(file, 
                     usecols=['Postcode','Country','District','District Code','County','Population'])

        postcode_df = pd.concat([postcode_df, df_temp])
        
        # call function for additional procs
    postcode_df = _postcodeDF_additionalProcessing(postcode_df)
        
    return postcode_df

def _postcodeDF_additionalProcessing(postcode_df):

    cols_to_use = ['Postcode','Country','mapArea','Population']
    
    # For England counties match maps
    postcode_df['mapArea']=postcode_df['County']

    # For Northern Ireland group together
    country = 'Northern Ireland'
    postcode_df.loc[postcode_df['Country']==country,'mapArea'] = country

    # For Wales use the dict defined before
    country = 'Wales'
    postcode_df.loc[postcode_df['Country']==country,'mapArea'] = postcode_df.loc[postcode_df['Country']==country,'District']
    postcode_df.mapArea = postcode_df.mapArea.replace(wales_region_dict)

    # For Scotland uses ScottishParliamentaryRegion2021Code from https://www.nrscotland.gov.uk/statistics-and-data/geography/nrs-postcode-extract
    # and Scottish (Holyrood) Parliamentary Regions, 2011 from https://borders.ukdataservice.ac.uk/easy_download_data.html?data=Scotland_preg_2011
    # slight difference in code numbers but checked location of areas in Kaggle and seems correct
    country = 'Scotland'
    dict_scotDistr = {
         'S17000020':'Glasgow',
         'S17000019' : 'Central Scotland',
         'S17000018' : 'West Scotland', 
         'S17000015' : 'South Scotland', 
         'S17000014' : 'North East Scotland', 
         'S17000013' : 'Mid Scotland and Fife', 
         'S17000012' : 'Lothian', 
         'S17000011' : 'Highlands and Islands', 
        }

    postcode_df.loc[postcode_df['Country']==country,'mapArea'] = postcode_df.loc[postcode_df['Country']==country,'District Code']
    postcode_df.mapArea = postcode_df.mapArea.replace(dict_scotDistr)

    # Select some columns only
    postcode_df = postcode_df[cols_to_use]
    
    return postcode_df
postcode_df = load_pcode_csvs()

Plot of population in regions#

df_grouped = postcode_df.groupby('mapArea',as_index=False).sum(numeric_only=True)
df_grouped['Population'] = np.log10( df_grouped['Population'] )
plot_map(df_grouped, 'Population', 'mapArea','.//_data/combined_json.json')
Make this Notebook Trusted to load map: File -> Trust Notebook

So now we have a map of the UK seperated by regions and a way to translate postcodes to one of these regions.

In the above map the population in each of these regions is shown. Note I have plotted log10(sum of Population) so a value of 6 means \(10^6\) people or 1million and 4 means \(10^4\) people or 10,000.

Go back

Next page