Package: map-uk
map-ukmap-ukis a Python package to help you quickly create maps of UK geographies such asLocal Authority Districts. The package will automatically download relevantgeojsonfiles so all that is required is a dataset with two columns: one containing yourgeocodeand one with the relevant values to plot on a map. Data is shown on a map usingfolium.Currently only Local Authority Districta (2023) are supported, further development will allow users to specify the geography type they want plotting (e.g. specific year of Local Authority Districts, Lower Super Output Areas, Combined Authorities) andmap-ukwill handle everything else.Credit toFlorian Maasfor the idea and initial codebase from theirmap-nlpackage.UsageChoropleth mapsYou can useChoroplethMapUKto create quick choropleth maps of 2023 Local Authority Districts using the below:importpandasaspdfrommap_ukimportChoroplethMapUK,constantsdata=pd.read_csv(constants.Paths.DATA_DIR/"data.csv")m=ChoroplethMapUK().plot(data,geocode_column="mnemonic",value_column_name="Median",legend_name="Median earnings (£)")m.save(constants.Paths.STATIC_DIR/"map1_basic.html")Other keyword-arguments passed toplot()are passed ontofolium.Choropleth. For example, we can change the colour palette and use the Jenks Natural Breaks algorithm:m=ChoroplethMapUK().plot(data,geocode_column="mnemonic",value_column_name="Median",legend_name="Median earnings (£)",fill_color="viridis",use_jenks=True,)m.save(constants.Paths.STATIC_DIR/"map2_basic.html")Custom mapsYou can useMapUKto create custom LAD 2023 maps of the UK.importfoliumimportpandasaspdfrommap_ukimportMapUK,constantsdefget_color(value,limit):"""Example function that specifies colour based on condition."""ifnotvalue:return"grey"ifvalue>limit:return"green"else:return"blue"defstyle(feature):"""Example function that specifies colour style to be used in folium map."""return{"fillColor":get_color(feature.get("properties").get("Median"),limit=35000)}data=pd.read_csv(constants.Paths.DATA_DIR/"data.csv")m=MapUK().plot(data,geocode_column="mnemonic",value_column_name="Median",style_function=style,name="Median earnings (£) value",)m.save(constants.Paths.STATIC_DIR/"map3_custom.html")The above example will colour any LADs with a median annual salary above 35000 green, blue if it is below 35000, and grey if no median value is found.As inChoroplethMapUK, you can pass keyword arguments to theplot()function. For example, you can modify the default tooltip and define your own:tooltip=folium.GeoJsonTooltip(fields=["geocode","ename","Median"],aliases=["Geography Code:","LAD Name:","Median Earnings:"],localize=True,sticky=False,labels=True,style="""background-color: #F0EFEF;border: 3px solid black;border-radius: 10px;box-shadow: 10px;""",max_width=800,)m=MapUK().plot(data,geocode_column="mnemonic",value_column_name="Median",style_function=style,name="Median earnings (£) value",tooltip=tooltip,)m.save(constants.Paths.STATIC_DIR/"map4_tooltip.html")InstallFor non-DAP users, this assumes you have installedpyenvandpoetry.First ensure you have a supported Python version installed on your machine:pyenvinstall3.10.*Any supported version is>=3.9,and<3.12.Set the local environment and install dependencies:Note: you may get an error running the 'env use python' command. Instead, just try running poetry installpyenvlocal3.10.*
poetryenvusepython
poetryinstallFinally, reload the window withCmd + R(or via the command paletteCmd + Shift + P>Developer: Reload window), then activate the newly created virtual environment:Note: if the command doesn't work, you can open the command palette, search 'python' and select 'Select Python interpreter' and choose the Poetry environment there - then reload the window.source$(poetryenvinfo--path)/bin/activate