{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Getting started" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Feature Collections\n", "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'`.\n", "\n", "GeoJSON FeatureCollections for various UK geographies are available in``faculty_extras.opendata``. To list available GeoJSONs:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['/input/geojson/',\n", " '/input/geojson/local_authorities.json',\n", " '/input/geojson/lower_super_output_areas.json',\n", " '/input/geojson/middle_super_output_areas.json',\n", " '/input/geojson/output_areas.json',\n", " '/input/geojson/parliamentary_constituencies.json']" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from faculty_extras import opendata\n", "\n", "opendata.ls(\"uk_statistical_boundaries/geojson\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and to load, for example, the boundaries of th UK parliamentary constituencies:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "pcon_geojson = opendata.load(\n", " \"uk_statistical_boundaries/geojson/parliamentary_constituencies.json\"\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create a map\n", "\n", "Now let's use `geoplotz` to make a [folium](https://folium.readthedocs.io/en/latest/) map centred on the feature collection:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "from faculty_extras import geoplot\n", "\n", "folium_map = geoplot.centred_map(pcon_geojson)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can generate a data point for each feature in the dataset:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Generate sample data" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "\n", "\n", "def lat(feature):\n", " coords = feature[\"geometry\"][\"coordinates\"]\n", " while not isinstance(coords[0], (int, float)):\n", " coords = coords[0]\n", " return coords[1]\n", "\n", "\n", "areas = np.array([feat[\"properties\"][\"id\"] for feat in pcon_geojson[\"features\"]])\n", "data = np.array([lat(feat) for feat in pcon_geojson[\"features\"]])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Plot a single-colour map" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "geoplot.single_colour(folium_map, pcon_geojson, \"red\")\n", "folium_map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Plot only selected features" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "folium_map = geoplot.centred_map(pcon_geojson)\n", "geoplot.single_colour(\n", " folium_map, pcon_geojson, \"red\", feature_selection=areas[data < 54]\n", ")\n", "folium_map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Plot choropleth maps" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "folium_map = geoplot.centred_map(pcon_geojson)\n", "geoplot.choropleth(folium_map, pcon_geojson, areas, data, bins=10)\n", "folium_map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Alternative colours" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "folium_map = geoplot.centred_map(pcon_geojson, zoom_start=5)\n", "geoplot.choropleth(folium_map, pcon_geojson, areas, data, bins=10, colourmap=\"rainbow\")\n", "folium_map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Save" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": true }, "outputs": [], "source": [ "folium_map.save(\"map.html\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.5" } }, "nbformat": 4, "nbformat_minor": 2 }