From 1cd5a6f3ccbe2f313d5f8750bc79b7efff12cd20 Mon Sep 17 00:00:00 2001 From: vikineema Date: Tue, 20 Feb 2024 20:37:58 +0000 Subject: [PATCH] Add notebook demonstrating how to add polygons to the database --- notebooks/WritePolygonstoDB.ipynb | 140 ++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 notebooks/WritePolygonstoDB.ipynb diff --git a/notebooks/WritePolygonstoDB.ipynb b/notebooks/WritePolygonstoDB.ipynb new file mode 100644 index 0000000..347e81d --- /dev/null +++ b/notebooks/WritePolygonstoDB.ipynb @@ -0,0 +1,140 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "01363705-ba78-40ce-a70d-6c519d94ddaa", + "metadata": {}, + "outputs": [], + "source": [ + "import os\n", + "# These are the default AWS configurations for the Analysis Sandbox.\n", + "# that are set in the environmnet variables. \n", + "aws_default_config = {\n", + " #'AWS_NO_SIGN_REQUEST': 'YES', \n", + " 'AWS_SECRET_ACCESS_KEY': 'fake',\n", + " 'AWS_ACCESS_KEY_ID': 'fake',\n", + "}\n", + "\n", + "# To access public bucket, need to remove the AWS credentials in \n", + "# the environment variables or the following error will occur.\n", + "# PermissionError: The AWS Access Key Id you provided does not exist in our records.\n", + "\n", + "for key in aws_default_config.keys():\n", + " if key in os.environ:\n", + " del os.environ[key]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f5f80385-ffe6-4508-b11a-bd1e13671585", + "metadata": {}, + "outputs": [], + "source": [ + "from sqlalchemy import create_engine\n", + "import deafrica_conflux.db\n", + "from deafrica_conflux.cli.logs import logging_setup\n", + "import geopandas as gpd" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "22f27ebd-4c23-44d1-9c3f-de88302a30f4", + "metadata": {}, + "outputs": [], + "source": [ + "# Set up logger.\n", + "logging_setup(1)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c14c5a92-de32-4545-8cd1-c7714fdc3727", + "metadata": {}, + "outputs": [], + "source": [ + "# Get the engine\n", + "username = \"waterbodies_writer\"\n", + "password = \"xxxx\"\n", + "host = \"db-writer\"\n", + "port = 5432\n", + "database_name = \"waterbodies\"\n", + "\n", + "# identifying name of the SQLAlchemy dialect\n", + "dialect = \"postgresql\"\n", + "# name of the DBAPI to be used to connect to the database\n", + "driver = \"psycopg2\"\n", + "# dialect+driver://username:password@host:port/database\n", + "database_url = f\"{dialect}+{driver}://{username}:{password}@{host}:{port}/{database_name}\"\n", + "engine = create_engine(database_url, future=True)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "addfece4-625f-437a-8e15-fcc50b7eaaa2", + "metadata": {}, + "outputs": [], + "source": [ + "deafrica_conflux.db.add_waterbody_polygons_to_db(\n", + " engine=engine,\n", + " drop_table=False,\n", + " update_rows=True,\n", + " waterbodies_polygons_fp=\"s3://deafrica-waterbodies-dev/waterbodies/v0.0.2/senegal_basin/historical_extent/waterbodies.parquet\",\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c6100db6-975c-4b88-97fe-6ab3cc63ea56", + "metadata": {}, + "outputs": [], + "source": [ + "%%time\n", + "# Define your SQL query\n", + "sql_query = \"SELECT * FROM waterbodies\"\n", + "\n", + "# Load GeoDataFrame from SQL database\n", + "waterbodies = gpd.read_postgis(sql_query, con=engine, geom_col=\"geometry\")\n", + "\n", + "# Print the GeoDataFrame\n", + "len(waterbodies)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "015f870a-e65d-42eb-9b62-0acb56fe4753", + "metadata": {}, + "outputs": [], + "source": [ + "waterbodies" + ] + } + ], + "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.12" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}