{ "cells": [ { "cell_type": "markdown", "id": "5694c920-c12c-40b6-bf05-c7a48ea9598e", "metadata": {}, "source": [ "# Simple examples - Extraction\n", "\n", "This notebook is just a small reference on how to request OSM data via the ohsome-API:\n", "\n", "- features for single timestamp\n", "- collections for singe timestamp\n", "- collections members for single timestamp\n", "- contributions for time range" ] }, { "cell_type": "code", "execution_count": 1, "id": "2ecc8374-d973-4ee5-857b-926065328367", "metadata": {}, "outputs": [], "source": [ "import os\n", "from io import BytesIO\n", "\n", "import geopandas as gpd\n", "import httpx" ] }, { "cell_type": "code", "execution_count": 2, "id": "634573b8-f011-4b89-a7e7-725372497e9e", "metadata": {}, "outputs": [], "source": [ "OHSOME_API_URL = os.getenv(\"OHSOME_API_URL\", \"https://api.heigit.org/ohsome-api/v2-rc\")\n", "OHSOME_API_KEY = os.getenv(\n", " \"OHSOME_API_KEY\", \"\"\n", ") # insert your api key as env or default value here" ] }, { "cell_type": "markdown", "id": "e3e87aea-86c7-47f3-8f1a-3da90fceecc0", "metadata": {}, "source": [ "## Features for single timestamp\n", "A simple example to extract all bus stops in Heidelberg." ] }, { "cell_type": "code", "execution_count": 31, "id": "569c29de-26ce-4879-b781-933eb4606690", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "response = httpx.post(\n", " OHSOME_API_URL + \"/extraction/features.parquet\",\n", " json={\n", " \"aoi\": [8.6275437, 49.3727921, 8.7545042, 49.4385023],\n", " \"filter\": \"highway=bus_stop and geometry:point\",\n", " \"time\": \"2026-06-01\",\n", " },\n", " headers={\"authorization\": OHSOME_API_KEY},\n", ")\n", "\n", "gdf = gpd.read_parquet(\n", " BytesIO(response.content),\n", " to_pandas_kwargs={\"maps_as_pydicts\": \"strict\"},\n", ")\n", "\n", "\n", "gdf.explore(\n", " tiles=\"CartoDB positron\",\n", " tooltip_kwds={\"style\": \"max-width: 250px; white-space: normal;\"},\n", ")" ] }, { "cell_type": "markdown", "id": "4d7a25d1-1359-4c7b-9aaa-afd484adc4dd", "metadata": {}, "source": [ "## Collections for single Timestamp\n", "We display the OSM relation \"Buslinie 37\" in Heidelberg." ] }, { "cell_type": "code", "execution_count": 32, "id": "c187edb2-80f4-4c70-aa52-f8d454f00e21", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "response = httpx.post(\n", " OHSOME_API_URL + \"/extraction/collections.parquet\",\n", " json={\n", " \"aoi\": [8.6275437, 49.3727921, 8.7545042, 49.4385023],\n", " \"filter\": \"type=route and route=bus and ref=37\",\n", " \"time\": \"latest\",\n", " \"clip\": False,\n", " },\n", " headers={\"authorization\": OHSOME_API_KEY},\n", ")\n", "\n", "gdf = gpd.read_parquet(\n", " BytesIO(response.content),\n", " to_pandas_kwargs={\"maps_as_pydicts\": \"strict\"},\n", ")\n", "\n", "gdf.explore(\n", " tiles=\"CartoDB positron\",\n", " tooltip_kwds={\"style\": \"max-width: 250px; white-space: normal;\"},\n", ")" ] }, { "cell_type": "markdown", "id": "ecf4e4b4-e211-438e-8b12-46acc2e108b8", "metadata": {}, "source": [ "## Collections Members for single Timestamp\n", "Here, we take a look at the members of the OSM relation \"Buslinie 37\".\n", "\n", "We inspect the tags of the members to display the stop name. This name is part of the member and not of the parent relation." ] }, { "cell_type": "code", "execution_count": 33, "id": "e7b1123a-71ca-4c4d-ba81-1a33fd5a24c5", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "response = httpx.post(\n", " OHSOME_API_URL + \"/extraction/collections_members.parquet\",\n", " json={\n", " \"aoi\": [8.6275437, 49.3727921, 8.7545042, 49.4385023],\n", " \"filter\": \"type=route and route=bus and ref=37\",\n", " \"member_filter\": \"highway=bus_stop and geometry:point\",\n", " \"time\": \"latest\",\n", " \"clip\": False,\n", " },\n", " headers={\"authorization\": OHSOME_API_KEY},\n", ")\n", "\n", "gdf = gpd.read_parquet(\n", " BytesIO(response.content),\n", " to_pandas_kwargs={\"maps_as_pydicts\": \"strict\"},\n", ")\n", "\n", "gdf[\"stop_name\"] = gdf[\"tags\"].apply(lambda x: x.get(\"name\"))\n", "\n", "gdf.explore(\n", " \"stop_name\",\n", " tiles=\"CartoDB positron\",\n", " tooltip_kwds={\"style\": \"max-width: 250px; white-space: normal;\"},\n", ")" ] }, { "cell_type": "markdown", "id": "8a3b4c55-10cb-4145-be78-b9cfba3de3f9", "metadata": {}, "source": [ "## Contributions for Time Range\n", "Here, we inspect all changes related to bus stops in Heidelberg between 2026-01 and 2026-06.\n", "\n", "For each contribution we look at the OSM editor used. This properties comes from the changeset tags." ] }, { "cell_type": "code", "execution_count": 34, "id": "69c6ac90-cdfb-4a29-9fbe-9548ca90245b", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "response = httpx.post(\n", " OHSOME_API_URL + \"/extraction/contributions.parquet\",\n", " json={\n", " \"aoi\": [8.6275437, 49.3727921, 8.7545042, 49.4385023],\n", " \"filter\": \"highway=bus_stop and geometry:point\",\n", " \"time\": {\"start\": \"2026-01-01\", \"end\": \"2026-06-01\"},\n", " },\n", " headers={\"authorization\": OHSOME_API_KEY},\n", ")\n", "\n", "gdf = gpd.read_parquet(\n", " BytesIO(response.content),\n", " to_pandas_kwargs={\"maps_as_pydicts\": \"strict\"},\n", ")\n", "\n", "gdf[\"editor\"] = gdf[\"changeset_tags\"].apply(lambda x: x.get(\"created_by\"))\n", "\n", "gdf.explore(\n", " \"editor\",\n", " tiles=\"CartoDB positron\",\n", " tooltip_kwds={\"style\": \"max-width: 250px; white-space: normal;\"},\n", ")" ] }, { "cell_type": "code", "execution_count": null, "id": "8a516e9c-ba02-45aa-891d-84469b400cfa", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.14.6" } }, "nbformat": 4, "nbformat_minor": 5 }