{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import requests\n", "\n", "import pandas as pd\n", "import geopandas as gpd\n", "from shapely import wkt\n", "from shapely.geometry import shape\n", "\n", "from google.oauth2 import service_account\n", "from google.cloud import bigquery\n", "\n", "from lets_plot import *" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Getting started with BigQuery GIS #\n", "Remake of the example https://cloud.google.com/bigquery/docs/gis-getting-started with lets_plot library" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "NYC_DISTRICT_IDS = [9691750, 8398124, 9691819, 9691948, 9691916]\n", "NYC_XMIN, NYC_XMAX, NYC_YMIN, NYC_YMAX = -74.05, -73.9, 40.65, 40.85" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def osm_boundaries(osm_id):\n", " response = requests.get('http://polygons.openstreetmap.fr/get_geojson.py?id={0}¶ms=0'.format(osm_id))\n", " return gpd.GeoDataFrame(geometry=[ shape(response.json()['geometries'][0]) ])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "credentials = service_account.Credentials.from_service_account_file(\n", " 'bigquery_account_credentials.json',\n", " scopes=['https://www.googleapis.com/auth/cloud-platform'],\n", ")\n", "client = bigquery.Client(credentials=credentials, project=credentials.project_id)\n", "query_job = client.query('''\n", " SELECT ST_GeogPoint(longitude, latitude) as WKT, num_bikes_available\n", " FROM `bigquery-public-data.new_york.citibike_stations`\n", " WHERE num_bikes_available > 30\n", "''')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df = query_job.result().to_dataframe()\n", "df['WKT'] = df['WKT'].apply(wkt.loads)\n", "gdf = gpd.GeoDataFrame(df, geometry='WKT')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "nyc_gdf = pd.concat([osm_boundaries(osm_id) for osm_id in NYC_DISTRICT_IDS])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ggplot() + \\\n", " geom_rect(xmin=NYC_XMIN, xmax=NYC_XMAX, ymin=NYC_YMIN, ymax=NYC_YMAX, fill='#deebf7') + \\\n", " geom_polygon(data=nyc_gdf, color='#3182bd', fill='#f1f1f1') + \\\n", " geom_point(aes(size='num_bikes_available'), data=gdf, color='#0000FF', alpha=.5) + \\\n", " scale_size(range=[4, 8], name='Available bikes') + \\\n", " coord_cartesian(xlim=[NYC_XMIN, NYC_XMAX], ylim=[NYC_YMIN, NYC_YMAX]) + \\\n", " theme(axis_title='blank', axis_text='blank', axis_ticks='blank', axis_line='blank') + \\\n", " ggsize(800, 600)" ] } ], "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.7.10" }, "pycharm": { "stem_cell": { "cell_type": "raw", "metadata": { "collapsed": false }, "source": [] } } }, "nbformat": 4, "nbformat_minor": 2 }