{ "cells": [ { "cell_type": "markdown", "id": "bed33e12", "metadata": {}, "source": [ "# Download OpenStreetMap POI data for Brighton & Hove\n", "\n", "This tutorial shows how you can download Points of Interest (POI) data for Brighton and Hove from OpenStreetMap. " ] }, { "cell_type": "code", "execution_count": 5, "id": "1c686f74", "metadata": {}, "outputs": [], "source": [ "import osmnx as ox\n", "import geopandas as gpd\n", "from shapely.geometry import box" ] }, { "cell_type": "markdown", "id": "ae76c628", "metadata": {}, "source": [ "### Define the area of interest \n", "\n", "Let's create a GeoDataFrame with Polygon that defines the area of interest for us. We will only collect data from this area. " ] }, { "cell_type": "code", "execution_count": 6, "id": "5a5a2e54", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Data extent for Brighton\n", "# minx, miny, maxx, maxy\n", "bounds = (-0.49975, 50.73, 0.3469234, 50.98)\n", "\n", "# Create a GeoDataFrame\n", "gdf = gpd.GeoDataFrame(geometry=[box(*bounds)], crs=\"EPSG:4326\")\n", "\n", "# Plot to map\n", "gdf.explore()" ] }, { "cell_type": "markdown", "id": "497831fb", "metadata": {}, "source": [ "## Extract information about restaurants" ] }, { "cell_type": "code", "execution_count": 15, "id": "8f0fdfb3", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/tmp/ipykernel_493210/3658294838.py:6: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.\n", "\n", " restaurants[\"geometry\"] = restaurants.centroid\n" ] }, { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Download restaurants using Osmnx\n", "tags = {\"amenity\": \"restaurant\"}\n", "restaurants = ox.geometries_from_polygon(gdf.geometry.values[0], tags)\n", "\n", "# Use centroid as geometry (we ignore the shape of the building)\n", "restaurants[\"geometry\"] = restaurants.centroid\n", "\n", "# Plot to map\n", "restaurants.explore()" ] }, { "cell_type": "markdown", "id": "4abfb54c", "metadata": {}, "source": [ "## Extract information about schools" ] }, { "cell_type": "code", "execution_count": 14, "id": "e5ebdf01", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/tmp/ipykernel_493210/808073114.py:6: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.\n", "\n", " schools[\"geometry\"] = schools.centroid\n" ] }, { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Download schools\n", "tags = {\"amenity\": \"school\"}\n", "schools = ox.geometries_from_polygon(gdf.geometry.values[0], tags)\n", "\n", "# Use centroid as geometry (we ignore the shape of the building)\n", "schools[\"geometry\"] = schools.centroid\n", "\n", "# Plot to map\n", "schools.explore()" ] }, { "cell_type": "markdown", "id": "b2674eb2", "metadata": {}, "source": [ "## Extract information about healthcare facilities\n" ] }, { "cell_type": "code", "execution_count": 13, "id": "5fadd547", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/tmp/ipykernel_493210/1219259598.py:6: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.\n", "\n", " healthcare[\"geometry\"] = healthcare.centroid\n" ] }, { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Download schools\n", "tags = {\"amenity\": [\"clinic\", \"doctors\", \"hospital\"]}\n", "healthcare = ox.geometries_from_polygon(gdf.geometry.values[0], tags)\n", "\n", "# Use centroid as geometry (we ignore the shape of the building)\n", "healthcare[\"geometry\"] = healthcare.centroid\n", "\n", "# Plot to map\n", "healthcare.explore()" ] }, { "cell_type": "markdown", "id": "5e95896d", "metadata": {}, "source": [ "## Save data to disk\n" ] }, { "cell_type": "markdown", "id": "429cd79f", "metadata": {}, "source": [ "### Restaurants" ] }, { "cell_type": "code", "execution_count": 33, "id": "063a02f6", "metadata": {}, "outputs": [], "source": [ "# GeoDataFrame has MultiLevel index - We only want to keep the second one and drop the first \n", "restaurants[\"id\"] = restaurants.index.droplevel(level=0)\n", "\n", "# Restaurants (store only specific columns)\n", "restaurants[[\"id\", \"name\", \"geometry\"]].to_file(\"data/Brighton/Brighton_restaurants.gpkg\")" ] }, { "cell_type": "markdown", "id": "0f234a8f", "metadata": {}, "source": [ "### Schools" ] }, { "cell_type": "code", "execution_count": 32, "id": "14319ba3", "metadata": {}, "outputs": [], "source": [ "# GeoDataFrame has MultiLevel index - We only want to keep the second one and drop the first \n", "schools[\"id\"] = schools.index.droplevel(level=0)\n", "\n", "# Restaurants (store only specific columns)\n", "schools[[\"id\", \"name\", \"geometry\"]].to_file(\"data/Brighton/Brighton_schools.gpkg\")" ] }, { "cell_type": "markdown", "id": "4f198339", "metadata": {}, "source": [ "### Healthcare facilities" ] }, { "cell_type": "code", "execution_count": 31, "id": "5ec646ca", "metadata": {}, "outputs": [], "source": [ "# GeoDataFrame has MultiLevel index - We only want to keep the second one and drop the first \n", "healthcare[\"id\"] = healthcare.index.droplevel(level=0)\n", "\n", "# Restaurants (store only specific columns)\n", "healthcare[[\"id\", \"name\", \"geometry\"]].to_file(\"data/Brighton/Brighton_healthcare.gpkg\")" ] } ], "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.10.8" } }, "nbformat": 4, "nbformat_minor": 5 }