{
"cells": [
{
"cell_type": "markdown",
"id": "116fcdbc",
"metadata": {},
"source": [
"# Working with Geometries\n",
"\n",
"**Setting up the conda env:**\n",
"\n",
"```\n",
"conda create -n sql python\n",
"conda activate sql\n",
"conda install ipython-sql sqlalchemy psycopg2 notebook pandas -c conda-forge\n",
"```\n",
"\n",
"**Sample dataset:**\n",
"- [nyc_data.zip](https://github.com/giswqs/postgis/raw/master/data/nyc_data.zip) (Watch this [video](https://youtu.be/fROzLrjNDrs) to load data into PostGIS)\n",
"\n",
"**References**:\n",
"- [Introduction to PostGIS](https://postgis.net/workshops/postgis-intro)\n",
"- [Using SQL with Geodatabases](https://desktop.arcgis.com/en/arcmap/latest/manage-data/using-sql-with-gdbs/sql-and-enterprise-geodatabases.htm)"
]
},
{
"cell_type": "markdown",
"id": "b43030e6",
"metadata": {},
"source": [
"## Connecting to the database"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "30a969e8",
"metadata": {},
"outputs": [],
"source": [
"%load_ext sql"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "4a222a24",
"metadata": {},
"outputs": [],
"source": [
"import os"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "df2880de",
"metadata": {},
"outputs": [],
"source": [
"host = \"localhost\"\n",
"database = \"nyc\"\n",
"user = os.getenv('SQL_USER')\n",
"password = os.getenv('SQL_PASSWORD')"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "dc4f36b1",
"metadata": {},
"outputs": [],
"source": [
"connection_string = f\"postgresql://{user}:{password}@{host}/{database}\""
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "c74c78db",
"metadata": {},
"outputs": [],
"source": [
"%sql $connection_string"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "6bd2a33c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" * postgresql://postgres:***@localhost/nyc\n",
"0 rows affected.\n"
]
},
{
"data": {
"text/html": [
"
\n",
" \n",
" srid | \n",
" auth_name | \n",
" auth_srid | \n",
" srtext | \n",
" proj4text | \n",
"
\n",
" \n",
" 3819 | \n",
" EPSG | \n",
" 3819 | \n",
" GEOGCS["HD1909",DATUM["Hungarian_Datum_1909",SPHEROID["Bessel 1841",6377397.155,299.1528128,AUTHORITY["EPSG","7004"]],TOWGS84[595.48,121.69,515.35,4.115,-2.9383,0.853,-3.408],AUTHORITY["EPSG","1024"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","3819"]] | \n",
" +proj=longlat +ellps=bessel +towgs84=595.48,121.69,515.35,4.115,-2.9383,0.853,-3.408 +no_defs | \n",
"
\n",
" \n",
" 3821 | \n",
" EPSG | \n",
" 3821 | \n",
" GEOGCS["TWD67",DATUM["Taiwan_Datum_1967",SPHEROID["GRS 1967 Modified",6378160,298.25,AUTHORITY["EPSG","7050"]],AUTHORITY["EPSG","1025"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","3821"]] | \n",
" +proj=longlat +ellps=aust_SA +no_defs | \n",
"
\n",
" \n",
" 3824 | \n",
" EPSG | \n",
" 3824 | \n",
" GEOGCS["TWD97",DATUM["Taiwan_Datum_1997",SPHEROID["GRS 1980",6378137,298.257222101,AUTHORITY["EPSG","7019"]],TOWGS84[0,0,0,0,0,0,0],AUTHORITY["EPSG","1026"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","3824"]] | \n",
" +proj=longlat +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +no_defs | \n",
"
\n",
" \n",
" 3889 | \n",
" EPSG | \n",
" 3889 | \n",
" GEOGCS["IGRS",DATUM["Iraqi_Geospatial_Reference_System",SPHEROID["GRS 1980",6378137,298.257222101,AUTHORITY["EPSG","7019"]],TOWGS84[0,0,0,0,0,0,0],AUTHORITY["EPSG","1029"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","3889"]] | \n",
" +proj=longlat +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +no_defs | \n",
"
\n",
" \n",
" 3906 | \n",
" EPSG | \n",
" 3906 | \n",
" GEOGCS["MGI 1901",DATUM["MGI_1901",SPHEROID["Bessel 1841",6377397.155,299.1528128,AUTHORITY["EPSG","7004"]],TOWGS84[682,-203,480,0,0,0,0],AUTHORITY["EPSG","1031"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","3906"]] | \n",
" +proj=longlat +ellps=bessel +towgs84=682,-203,480,0,0,0,0 +no_defs | \n",
"
\n",
" \n",
" 4001 | \n",
" EPSG | \n",
" 4001 | \n",
" GEOGCS["Unknown datum based upon the Airy 1830 ellipsoid",DATUM["Not_specified_based_on_Airy_1830_ellipsoid",SPHEROID["Airy 1830",6377563.396,299.3249646,AUTHORITY["EPSG","7001"]],AUTHORITY["EPSG","6001"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4001"]] | \n",
" +proj=longlat +ellps=airy +no_defs | \n",
"
\n",
" \n",
" 4002 | \n",
" EPSG | \n",
" 4002 | \n",
" GEOGCS["Unknown datum based upon the Airy Modified 1849 ellipsoid",DATUM["Not_specified_based_on_Airy_Modified_1849_ellipsoid",SPHEROID["Airy Modified 1849",6377340.189,299.3249646,AUTHORITY["EPSG","7002"]],AUTHORITY["EPSG","6002"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4002"]] | \n",
" +proj=longlat +ellps=mod_airy +no_defs | \n",
"
\n",
" \n",
" 4003 | \n",
" EPSG | \n",
" 4003 | \n",
" GEOGCS["Unknown datum based upon the Australian National Spheroid",DATUM["Not_specified_based_on_Australian_National_Spheroid",SPHEROID["Australian National Spheroid",6378160,298.25,AUTHORITY["EPSG","7003"]],AUTHORITY["EPSG","6003"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4003"]] | \n",
" +proj=longlat +ellps=aust_SA +no_defs | \n",
"
\n",
" \n",
" 4004 | \n",
" EPSG | \n",
" 4004 | \n",
" GEOGCS["Unknown datum based upon the Bessel 1841 ellipsoid",DATUM["Not_specified_based_on_Bessel_1841_ellipsoid",SPHEROID["Bessel 1841",6377397.155,299.1528128,AUTHORITY["EPSG","7004"]],AUTHORITY["EPSG","6004"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4004"]] | \n",
" +proj=longlat +ellps=bessel +no_defs | \n",
"
\n",
" \n",
" 4005 | \n",
" EPSG | \n",
" 4005 | \n",
" GEOGCS["Unknown datum based upon the Bessel Modified ellipsoid",DATUM["Not_specified_based_on_Bessel_Modified_ellipsoid",SPHEROID["Bessel Modified",6377492.018,299.1528128,AUTHORITY["EPSG","7005"]],AUTHORITY["EPSG","6005"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4005"]] | \n",
" +proj=longlat +a=6377492.018 +b=6356173.508712696 +no_defs | \n",
"
\n",
"
"
],
"text/plain": [
"[(3819, 'EPSG', 3819, 'GEOGCS[\"HD1909\",DATUM[\"Hungarian_Datum_1909\",SPHEROID[\"Bessel 1841\",6377397.155,299.1528128,AUTHORITY[\"EPSG\",\"7004\"]],TOWGS84[595.48,121.69,515.35,4. ... (34 characters truncated) ... Y[\"EPSG\",\"1024\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"3819\"]]', '+proj=longlat +ellps=bessel +towgs84=595.48,121.69,515.35,4.115,-2.9383,0.853,-3.408 +no_defs '),\n",
" (3821, 'EPSG', 3821, 'GEOGCS[\"TWD67\",DATUM[\"Taiwan_Datum_1967\",SPHEROID[\"GRS 1967 Modified\",6378160,298.25,AUTHORITY[\"EPSG\",\"7050\"]],AUTHORITY[\"EPSG\",\"1025\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"3821\"]]', '+proj=longlat +ellps=aust_SA +no_defs '),\n",
" (3824, 'EPSG', 3824, 'GEOGCS[\"TWD97\",DATUM[\"Taiwan_Datum_1997\",SPHEROID[\"GRS 1980\",6378137,298.257222101,AUTHORITY[\"EPSG\",\"7019\"]],TOWGS84[0,0,0,0,0,0,0],AUTHORITY[\"EPSG\",\"1026\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"3824\"]]', '+proj=longlat +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +no_defs '),\n",
" (3889, 'EPSG', 3889, 'GEOGCS[\"IGRS\",DATUM[\"Iraqi_Geospatial_Reference_System\",SPHEROID[\"GRS 1980\",6378137,298.257222101,AUTHORITY[\"EPSG\",\"7019\"]],TOWGS84[0,0,0,0,0,0,0],AU ... (6 characters truncated) ... Y[\"EPSG\",\"1029\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"3889\"]]', '+proj=longlat +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +no_defs '),\n",
" (3906, 'EPSG', 3906, 'GEOGCS[\"MGI 1901\",DATUM[\"MGI_1901\",SPHEROID[\"Bessel 1841\",6377397.155,299.1528128,AUTHORITY[\"EPSG\",\"7004\"]],TOWGS84[682,-203,480,0,0,0,0],AUTHORITY[\"EPSG\",\"1031\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"3906\"]]', '+proj=longlat +ellps=bessel +towgs84=682,-203,480,0,0,0,0 +no_defs '),\n",
" (4001, 'EPSG', 4001, 'GEOGCS[\"Unknown datum based upon the Airy 1830 ellipsoid\",DATUM[\"Not_specified_based_on_Airy_1830_ellipsoid\",SPHEROID[\"Airy 1830\",6377563.396,299.324 ... (39 characters truncated) ... Y[\"EPSG\",\"6001\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4001\"]]', '+proj=longlat +ellps=airy +no_defs '),\n",
" (4002, 'EPSG', 4002, 'GEOGCS[\"Unknown datum based upon the Airy Modified 1849 ellipsoid\",DATUM[\"Not_specified_based_on_Airy_Modified_1849_ellipsoid\",SPHEROID[\"Airy Modifie ... (66 characters truncated) ... Y[\"EPSG\",\"6002\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4002\"]]', '+proj=longlat +ellps=mod_airy +no_defs '),\n",
" (4003, 'EPSG', 4003, 'GEOGCS[\"Unknown datum based upon the Australian National Spheroid\",DATUM[\"Not_specified_based_on_Australian_National_Spheroid\",SPHEROID[\"Australian N ... (67 characters truncated) ... Y[\"EPSG\",\"6003\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4003\"]]', '+proj=longlat +ellps=aust_SA +no_defs '),\n",
" (4004, 'EPSG', 4004, 'GEOGCS[\"Unknown datum based upon the Bessel 1841 ellipsoid\",DATUM[\"Not_specified_based_on_Bessel_1841_ellipsoid\",SPHEROID[\"Bessel 1841\",6377397.155,2 ... (45 characters truncated) ... Y[\"EPSG\",\"6004\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4004\"]]', '+proj=longlat +ellps=bessel +no_defs '),\n",
" (4005, 'EPSG', 4005, 'GEOGCS[\"Unknown datum based upon the Bessel Modified ellipsoid\",DATUM[\"Not_specified_based_on_Bessel_Modified_ellipsoid\",SPHEROID[\"Bessel Modified\",6 ... (57 characters truncated) ... Y[\"EPSG\",\"6005\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4005\"]]', '+proj=longlat +a=6377492.018 +b=6356173.508712696 +no_defs ')]"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%%sql \n",
"\n",
"SELECT * FROM spatial_ref_sys LIMIT 10"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "f189ec99",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" * postgresql://postgres:***@localhost/nyc\n",
"6 rows affected.\n"
]
},
{
"data": {
"text/html": [
"