Getting started

Feature Collections

The main argument required by many Geoplot functions feature_collection is a Python dictionary containing the contents of a GeoJSON FeatureCollection. You can customise which property to use as the index. The default is 'id'.

GeoJSON FeatureCollections for various UK geographies are available infaculty_extras.opendata. To list available GeoJSONs:

[1]:
from faculty_extras import opendata

opendata.ls("uk_statistical_boundaries/geojson")
[1]:
['/input/geojson/',
 '/input/geojson/local_authorities.json',
 '/input/geojson/lower_super_output_areas.json',
 '/input/geojson/middle_super_output_areas.json',
 '/input/geojson/output_areas.json',
 '/input/geojson/parliamentary_constituencies.json']

and to load, for example, the boundaries of th UK parliamentary constituencies:

[2]:
pcon_geojson = opendata.load(
    "uk_statistical_boundaries/geojson/parliamentary_constituencies.json"
)

Create a map

Now let’s use geoplotz to make a folium map centred on the feature collection:

[3]:
from faculty_extras import geoplot

folium_map = geoplot.centred_map(pcon_geojson)

We can generate a data point for each feature in the dataset:

Generate sample data

[4]:
import numpy as np


def lat(feature):
    coords = feature["geometry"]["coordinates"]
    while not isinstance(coords[0], (int, float)):
        coords = coords[0]
    return coords[1]


areas = np.array([feat["properties"]["id"] for feat in pcon_geojson["features"]])
data = np.array([lat(feat) for feat in pcon_geojson["features"]])

Plot a single-colour map

[5]:
geoplot.single_colour(folium_map, pcon_geojson, "red")
folium_map
[5]:

Plot only selected features

[6]:
folium_map = geoplot.centred_map(pcon_geojson)
geoplot.single_colour(
    folium_map, pcon_geojson, "red", feature_selection=areas[data < 54]
)
folium_map
[6]:

Plot choropleth maps

[7]:
folium_map = geoplot.centred_map(pcon_geojson)
geoplot.choropleth(folium_map, pcon_geojson, areas, data, bins=10)
folium_map
[7]:

Alternative colours

[8]:
folium_map = geoplot.centred_map(pcon_geojson, zoom_start=5)
geoplot.choropleth(folium_map, pcon_geojson, areas, data, bins=10, colourmap="rainbow")
folium_map
[8]:

Save

[9]:
folium_map.save("map.html")