Skip to main content

Quickly create maps of the UK in Python

Project description

map uk

map-uk

map-uk is a Python package to help you quickly create maps of UK geographies such as Local Authority Districts. The package will automatically download relevant geojson files so all that is required is a dataset with two columns: one containing your geocode and one with the relevant values to plot on a map. Data is shown on a map using folium.

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) and map-uk will handle everything else.

Credit to Florian Maas for the idea and initial codebase from their map-nl package.

Usage

Choropleth maps

You can use ChoroplethMapUK to create quick choropleth maps of 2023 Local Authority Districts using the below:

import pandas as pd
from map_uk import ChoroplethMapUK, constants

data = 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 to plot() are passed onto folium.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 maps

You can use MapUK to create custom LAD 2023 maps of the UK.

import folium
import pandas as pd

from map_uk import MapUK, constants


def get_color(value, limit):
    """Example function that specifies colour based on condition."""
    if not value:
        return "grey"
    if value > limit:
        return "green"
    else:
        return "blue"

def style(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 in ChoroplethMapUK, you can pass keyword arguments to the plot() 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")

Install

For non-DAP users, this assumes you have installed pyenv and poetry.

First ensure you have a supported Python version installed on your machine:

pyenv install 3.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 install

pyenv local 3.10.*
poetry env use python
poetry install

Finally, reload the window with Cmd + R (or via the command palette Cmd + 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 $(poetry env info --path)/bin/activate

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

map_uk-0.0.0.tar.gz (12.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

map_uk-0.0.0-py3-none-any.whl (11.8 kB view details)

Uploaded Python 3

File details

Details for the file map_uk-0.0.0.tar.gz.

File metadata

  • Download URL: map_uk-0.0.0.tar.gz
  • Upload date:
  • Size: 12.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.12.1 Darwin/21.6.0

File hashes

Hashes for map_uk-0.0.0.tar.gz
Algorithm Hash digest
SHA256 3063d7441ded0faad90f9d0db6b3f460b3b65b8dbf019af71f2f7cb7bef179ca
MD5 9cbfe4cdf859b55a0039f3bb68e7a15b
BLAKE2b-256 23361fdb17f1577d6bf92b7e054e8e465d4803c23928a71820e72164ead315ba

See more details on using hashes here.

File details

Details for the file map_uk-0.0.0-py3-none-any.whl.

File metadata

  • Download URL: map_uk-0.0.0-py3-none-any.whl
  • Upload date:
  • Size: 11.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.12.1 Darwin/21.6.0

File hashes

Hashes for map_uk-0.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8de7c198e70df15a74c115c2eed29f9c5ef0109af56aa1e5b115a83dc896c3a7
MD5 5c44de2fc5c1fb2165679ecb3c91b38f
BLAKE2b-256 62bfb1eb1e8aec0566af7cabaeec394650c9cb9b6370630cdc841db5928614a1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page