{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "from sqlalchemy import create_engine\n", "import geopandas as gpd\n", "\n", "import os\n", "\n", "# Database connection details from zshrc environment variables\n", "db_name = 'colorado_spills'\n", "user = os.getenv('DB_USER')\n", "password = os.getenv('DB_PASSWORD')\n", "host = os.getenv('DB_HOST')\n", "port = os.getenv('DB_PORT')\n", "\n", "\n", "# Create an engine to connect to the PostgreSQL database\n", "engine = create_engine(f'postgresql+psycopg2://{user}:{password}@{host}:{port}/{db_name}')\n", "\n", "# Read in the spills_with_demographics as spills\n", "spills = pd.read_sql_table('spills_with_demographics', engine)\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# use longitude and latitude to create a GeoDataFrame from spills data\n", "spills['geometry'] = gpd.points_from_xy(spills['Longitude'], spills['Latitude'])" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# Create a GeoDataFrame from spills\n", "spills_gdf = gpd.GeoDataFrame(spills, crs='EPSG:4326') \n", "\n", "# Write the GeoDataFrame to a new table in the database\n", "spills_gdf.to_postgis('spills_with_demographics_geog', engine, if_exists='replace')\n", "\n", "# Close the database connection\n", "engine.dispose()\n", "\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
| \n", " | Spill Description | \n", "Sentiment | \n", "
|---|---|---|
| 0 | \n", "Crews working on 6/11/14 in area of former pro... | \n", "0.000000 | \n", "
| 1 | \n", "Historical release discovered during removal o... | \n", "0.000000 | \n", "
| 2 | \n", "Historical release discovered during removal o... | \n", "0.000000 | \n", "
| 3 | \n", "The night operator noticed a high level alarm ... | \n", "-0.046667 | \n", "
| 4 | \n", "On May 24, 2014, in anticipation of potential ... | \n", "0.151786 | \n", "