3 Spatial SQL I
3.1 Introduction
In this lesson, you will learn how to use SQL to query spatial data. You will learn how to use the duckdb
Python library to connect to a DuckDB database and run SQL queries. You will also learn how to use the leafmap
Python library to visualize spatial data.
3.2 Learning Objectives
By the end of this lesson, you will be able to:
- Connect to a DuckDB database using Python
- Run SQL queries to query spatial data
- Visualize spatial data using Leafmap
- Run spatial SQL queries using PgAdmin
3.3 Materials
To get started, work your way through the following two notebooks:
Clicking the link will take you to Google Colab, where you can run the notebook in your browser. These workbooks use the duckdb
Python library to connect to a DuckDB database and run SQL queries. They also use the leafmap
Python library to visualize spatial data. Most of the spatial functions and syntax are the same or very similar to their PostGIS equivalents.
Once you’ve completed these you can test your knowledge by answering the questions in the lab below. To complete this lab, open PgAdmin and connect to the nyc
database. Then, open a new query window and write your SQL queries there.
4 Geometry Exercises
Here's a reminder of all the functions we have seen so far. They should be useful for the exercises!
sum(expression)
aggregate to return a sum for a set of recordscount(expression)
aggregate to return the size of a set of recordsST_GeometryType(geometry)
returns the type of the geometryST_NDims(geometry)
returns the number of dimensions of the geometryST_SRID(geometry)
returns the spatial reference identifier number of the geometryST_X(point)
returns the X ordinateST_Y(point)
returns the Y ordinateST_Length(linestring)
returns the length of the linestringST_StartPoint(geometry)
returns the first coordinate as a pointST_EndPoint(geometry)
returns the last coordinate as a pointST_NPoints(geometry)
returns the number of coordinates in the linestringST_Area(geometry)
returns the area of the polygonsST_NRings(geometry)
returns the number of rings (usually 1, more if there are holes)ST_ExteriorRing(polygon)
returns the outer ring as a linestringST_InteriorRingN(polygon, integer)
returns a specified interior ring as a linestringST_Perimeter(geometry)
returns the length of all the ringsST_NumGeometries(multi/geomcollection)
returns the number of parts in the collectionST_GeometryN(geometry, integer)
returns the specified part of the collectionST_GeomFromText(text)
returnsgeometry
ST_AsText(geometry)
returns WKTtext
ST_AsEWKT(geometry)
returns EWKTtext
ST_GeomFromWKB(bytea)
returnsgeometry
ST_AsBinary(geometry)
returns WKBbytea
ST_AsEWKB(geometry)
returns EWKBbytea
ST_GeomFromGML(text)
returnsgeometry
ST_AsGML(geometry)
returns GMLtext
ST_GeomFromKML(text)
returnsgeometry
ST_AsKML(geometry)
returns KMLtext
ST_AsGeoJSON(geometry)
returns JSONtext
ST_AsSVG(geometry)
returns SVGtext
Also remember the tables we have available:
nyc_census_blocks
- blkid, popn_total, boroname, geom
nyc_streets
- name, type, geom
nyc_subway_stations
- name, geom
nyc_neighborhoods
- name, boroname, geom
4.1 Exercises
What is the area of the 'West Village' neighborhood? (Hint: The area is given in square meters. To get an area in hectares, divide by 10000. To get an area in acres, divide by 4047.)
What is the geometry type of ‘Pelham St’? The length?
What is the GeoJSON representation of the 'Broad St' subway station?
What is the total length of streets (in kilometers) in New York City? (Hint: The units of measurement of the spatial data are meters, there are 1000 meters in a kilometer.)
What is the area of Manhattan in acres? (Hint: both
nyc_census_blocks
andnyc_neighborhoods
have aboroname
in them.)What is the most westerly subway station?
How long is 'Columbus Cir' (aka Columbus Circle)?
What is the length of streets in New York City, summarized by type?
Answers (only check after you’ve given it your best shot!)
5 Spatial Relationships Exercises
Here's a reminder of the functions we saw in the last section. They should be useful for the exercises!
sum(expression)
aggregate to return a sum for a set of recordscount(expression)
aggregate to return the size of a set of recordsST_Contains(geometry A, geometry B)
returns true if geometry A contains geometry BST_Crosses(geometry A, geometry B)
returns true if geometry A crosses geometry BST_Disjoint(geometry A , geometry B)
returns true if the geometries do not "spatially intersect"ST_Distance(geometry A, geometry B)
returns the minimum distance between geometry A and geometry BST_DWithin(geometry A, geometry B, radius)
returns true if geometry A is radius distance or less from geometry BST_Equals(geometry A, geometry B)
returns true if geometry A is the same as geometry BST_Intersects(geometry A, geometry B)
returns true if geometry A intersects geometry BST_Overlaps(geometry A, geometry B)
returns true if geometry A and geometry B share space, but are not completely contained by each other.ST_Touches(geometry A, geometry B)
returns true if the boundary of geometry A touches geometry BST_Within(geometry A, geometry B)
returns true if geometry A is within geometry B
Also remember the tables we have available:
nyc_census_blocks
- blkid, popn_total, boroname, geom
nyc_streets
- name, type, geom
nyc_subway_stations
- name, geom
nyc_neighborhoods
- name, boroname, geom
5.1 Exercises
What is the geometry value for the street named 'Atlantic Commons'?
What neighborhood and borough is Atlantic Commons in?
What streets does Atlantic Commons join with?
Approximately how many people live on (within 50 meters of) Atlantic Commons?
Answers (only check after you’ve given it your best shot!)