{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Segment provinces\n", "\n", "\n", "Create wijk and gemeente level segmentations for two Dutch provinces, Groningen and Friesland, and save as geojson and Gabmap KML.\n", "\n", "All is based on [CBS data](https://www.cbs.nl/nl-nl/dossier/nederland-regionaal/geografische%20data/wijk-en-buurtkaart-2017)\n", "\n", "For Friesland, several wijken are merged, in particular those of the municipalities Ameland, Harlingen, Schiermonnikoog, Terschelling and Vlieland, and those of Leeuwarden with centroid above 53.167. These neighborhoods are small in area and hence we decided to merge, to avoid a " ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "%load_ext autoreload\n", "\n", "%autoreload 2" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import sys\n", "sys.path.append('../')\n", "\n", "from stimmen.latitude_longitude import reverse_latitude_longitude\n", "from stimmen.shapefile import shapefiles_to_geojson\n", "from stimmen.cbs import gwb_in_province, get_available_provinces\n", "from stimmen.geojson import merge_features\n", "from gabmap import as_gabmap_kml\n", "\n", "from shapely.geometry import shape, box, mapping\n", "\n", "import json\n", "import folium\n", "import pickle\n", "\n", "from collections import defaultdict" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Groningen\n", "0\n", "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "Friesland\n", "0\n", "1\n", "2\n", "3\n", "4\n", "5\n", "6\n" ] } ], "source": [ "for province in ['Groningen', 'Friesland']:\n", " wijken_geojson = gwb_in_province(province, 'wijk', 2018, polygon_simplification=None)\n", " gemeente_geojson = gwb_in_province(province, 'gem', 2018, polygon_simplification=None)\n", " \n", " if province == 'Friesland':\n", " for gemeente in {'Ameland', 'Harlingen', 'Schiermonnikoog', 'Terschelling', 'Vlieland'}:\n", " merged_geojson = merge_features(\n", " wijken_geojson.copy(),\n", " condition=lambda feature: feature['properties']['GM_NAAM'] == gemeente,\n", " aggregate={'WK_NAAM': ' '.join}\n", " )\n", "\n", " merge_leeuwarden_only_above_latitude = 53.167\n", " wijken_geojson = merge_features(\n", " wijken_geojson,\n", " condition=lambda feature: (\n", " feature['properties']['GM_NAAM'] == 'Leeuwarden' and\n", " shape(feature['geometry']).centroid.y > merge_leeuwarden_only_above_latitude\n", " ),\n", " aggregate={'WK_NAAM': ' '.join}\n", " )\n", " \n", " #Some gemeentes appear twice in the cbs data.\n", " for gemeente in [feature['properties']['GM_NAAM'] for feature in gemeente_geojson['features']]:\n", " gemeente_geojson = merge_features(\n", " gemeente_geojson, condition=lambda feature: feature['properties']['GM_NAAM'] == gemeente)\n", " \n", " for feature in wijken_geojson['features']:\n", " feature['properties']['gemeente_en_wijk_naam'] = (\n", " feature['properties']['GM_NAAM'] +\n", " ', ' +\n", " feature['properties'].get('WK_NAAM', '')\n", " ).replace('&', 'en').replace('/', ' ').replace('\"', ' ').replace(\"'\", ' ')\n", " \n", " for feature in gemeente_geojson['features']:\n", " feature['properties']['gemeente_naam'] = (\n", " feature['properties']['GM_NAAM']\n", " ).replace('&', 'en').replace('/', ' ').replace('\"', ' ').replace(\"'\", ' ')\n", " \n", " with open('../data/{}_wijken.geojson'.format(province), 'w') as f:\n", " json.dump(wijken_geojson, f)\n", " with open('../data/{}_gemeentes.geojson'.format(province), 'w') as f:\n", " json.dump(gemeente_geojson, f)\n", " \n", " with open('../data/{}_wijken.kml'.format(province), 'w') as f:\n", " f.write(as_gabmap_kml(wijken_geojson, name_property='gemeente_en_wijk_naam'))\n", " with open('../data/{}_gemeentes.kml'.format(province), 'w') as f:\n", " f.write(as_gabmap_kml(gemeente_geojson, name_property='gemeente_naam'))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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 }