{ "cells": [ { "cell_type": "markdown", "id": "1bff0133", "metadata": {}, "source": [ "# Schools Count Exercise in Python" ] }, { "cell_type": "markdown", "id": "a0aa1a72", "metadata": {}, "source": [ "### Count Schools in Berlin using Overpass API through Python\n", "This analysis uses the [OSMPythonTools](https://github.com/mocnik-science/osm-python-tools) to query the Overpass API. In addition [Nominatim](https://nominatim.org/) is queried first to define the area of interest." ] }, { "cell_type": "code", "execution_count": 1, "id": "ec53e00f", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "[nominatim] downloading data: search\n", "[overpass] downloading data: [timeout:25][out:json];area(3600062422)->.searchArea;(node[\"amenity\"=\"school\"](area.searchArea);way[\"amenity\"=\"school\"](area.searchArea);relation[\"amenity\"=\"school\"](area.searchArea);); out body;\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Nodes: 131\n", "Ways: 932\n", "Relations: 21\n" ] } ], "source": [ "from OSMPythonTools.overpass import Overpass\n", "from OSMPythonTools.overpass import overpassQueryBuilder\n", "from OSMPythonTools.nominatim import Nominatim\n", "\n", "nominatim = Nominatim()\n", "overpass = Overpass()\n", "\n", "query = overpassQueryBuilder(\n", " area=nominatim.query('Berlin').areaId(),\n", " elementType=['node', 'way', 'relation'],\n", " selector=['\"amenity\"=\"school\"']\n", ")\n", "\n", "result = overpass.query(query)\n", "\n", "print(f\"Nodes: {len(result.nodes())}\")\n", "print(f\"Ways: {len(result.ways())}\")\n", "print(f\"Relations: {len(result.relations())}\")" ] }, { "cell_type": "markdown", "id": "49708877", "metadata": {}, "source": [ "### Count Schools per District using ohsome-py package\n", "Here we use the [ohsome-py](https://github.com/GIScience/ohsome-py) package to query the ohsome API. We use the `elements/count/groupBy/boundary` endpoint. \n", "\n", "For this analysis we assume that you already downloaded the district boundaries for Berlin. If not do this via [OSM Boundaries](https://osm-boundaries.com/)." ] }, { "cell_type": "code", "execution_count": 5, "id": "5302441a", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | \n", " | value | \n", "
---|---|---|
boundary | \n", "timestamp | \n", "\n", " |
feature1 | \n", "2022-11-02 09:00:00+00:00 | \n", "74.0 | \n", "
feature10 | \n", "2022-11-02 09:00:00+00:00 | \n", "107.0 | \n", "
feature11 | \n", "2022-11-02 09:00:00+00:00 | \n", "78.0 | \n", "
feature12 | \n", "2022-11-02 09:00:00+00:00 | \n", "84.0 | \n", "
feature2 | \n", "2022-11-02 09:00:00+00:00 | \n", "105.0 | \n", "
feature3 | \n", "2022-11-02 09:00:00+00:00 | \n", "133.0 | \n", "
feature4 | \n", "2022-11-02 09:00:00+00:00 | \n", "76.0 | \n", "
feature5 | \n", "2022-11-02 09:00:00+00:00 | \n", "80.0 | \n", "
feature6 | \n", "2022-11-02 09:00:00+00:00 | \n", "87.0 | \n", "
feature7 | \n", "2022-11-02 09:00:00+00:00 | \n", "92.0 | \n", "
feature8 | \n", "2022-11-02 09:00:00+00:00 | \n", "75.0 | \n", "
feature9 | \n", "2022-11-02 09:00:00+00:00 | \n", "100.0 | \n", "