{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# NYC Subway With BigQuery #" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import geopandas as gpd\n", "from shapely import wkt\n", "from google.oauth2 import service_account\n", "from google.cloud import bigquery\n", "\n", "from lets_plot import *\n", "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "def load_gdf(client, *, geometry_name, query):\n", " df = client.query(query).result().to_dataframe()\n", " df[geometry_name] = df[geometry_name].apply(wkt.loads)\n", " return gpd.GeoDataFrame(df, geometry=geometry_name)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# You should add your own bigquery_credentials.json file\n", "# See https://cloud.google.com/docs/authentication/getting-started\n", "credentials = service_account.Credentials.from_service_account_file(\n", " '../data/bigquery_credentials.json',\n", " scopes=['https://www.googleapis.com/auth/cloud-platform'],\n", ")\n", "client = bigquery.Client(credentials=credentials, project=credentials.project_id)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "boroughs_gdf = load_gdf(client, geometry_name='borough_geom', query='''\n", " SELECT borough_name, borough_geom\n", " FROM `bigquery-public-data.new_york_subway.geo_nyc_borough_boundaries`\n", "''')" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "stations_gdf = load_gdf(client, geometry_name='station_geom', query='''\n", " SELECT station_name, line, station_geom\n", " FROM `bigquery-public-data.new_york_subway.stations`\n", "''')" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "lines_gdf = load_gdf(client, geometry_name='line_geom', query='''\n", " SELECT ny_s.line, ST_MakeLine(ARRAY(\n", " SELECT station_geom\n", " FROM `bigquery-public-data.new_york_subway.stations`\n", " WHERE line = ny_s.line\n", " )) AS line_geom\n", " FROM(\n", " SELECT line \n", " FROM `bigquery-public-data.new_york_subway.stations`\n", " ) AS ny_s\n", " GROUP BY line\n", "''')" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ], "text/plain": [ "