{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Segment provinces\n", "\n", "\n", "Create wijk and gemeente level segmentations for all Dutch provinces and save as geojson and Gabmap KML.\n", "\n", "All is based on CBS data.\n", "\n", "For Friesland, several wijken are merged.\n", "\n", "Note: only applied to Groningen and Friesland, because other provinces give gemetry errors." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "%load_ext autoreload\n", "\n", "%autoreload 2" ] }, { "cell_type": "code", "execution_count": 7, "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": 12, "metadata": {}, "outputs": [], "source": [ "for province in ['Groningen', 'Friesland']:\n", " wijken_geojson = gwb_in_province(province, 'wijk', 2018)\n", " gemeente_geojson = gwb_in_province(province, 'gem', 2018)\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'))" ] } ], "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 }