Lab 04: Spatial Relationship
Due date: Thursday, Feb 20 submitted as PDF or HTML to Canvas Lab04 link. This lab counts 9 % toward your total grade.
Objectives:
- Understand Spatial Relationships.
- Implement Subqueries.
For each task:
- Provide the SQL Query
- Plot the output.
In [ ]:
Copied!
import matplotlib.pyplot as plt
from sqlalchemy import create_engine
from sqlalchemy import inspect
from sqlalchemy import text
import os
import geopandas as gpd
import matplotlib.pyplot as plt
from sqlalchemy import create_engine
from sqlalchemy import inspect
from sqlalchemy import text
import os
import geopandas as gpd
In [ ]:
Copied!
host = 'localhost'
database = 'Your database'
user = 'postgres'
port = '5432'
password = os.getenv('SQL_PASSWORD')
host = 'localhost'
database = 'Your database'
user = 'postgres'
port = '5432'
password = os.getenv('SQL_PASSWORD')
In [ ]:
Copied!
connection_string = f"postgresql://{user}:{password}@{host}:{port}/{database}"
connection_string = f"postgresql://{user}:{password}@{host}:{port}/{database}"
In [ ]:
Copied!
engine = create_engine(connection_string)
insp = inspect(engine)
insp.get_table_names()
engine = create_engine(connection_string)
insp = inspect(engine)
insp.get_table_names()
In [ ]:
Copied!
query = """
SELECT __________
FROM _________
WHERE ________;
"""
subway_stations = gpd.read_postgis(query, engine, geom_col='geom')
print(subway_stations)
query02 = """
________________
"""
all_neighborhood = gpd.read_postgis(query02, engine, geom_col='geom')
# Plotting all_neighborhood as the background, and pinpoint the station
fig, ax = plt.subplots(figsize=(10, 8))
all_neighborhood.plot(ax=ax, color = 'none')
subway_stations.plot(ax=ax, color='', label='')
plt.legend()
plt.title('')
plt.show()
query = """
SELECT __________
FROM _________
WHERE ________;
"""
subway_stations = gpd.read_postgis(query, engine, geom_col='geom')
print(subway_stations)
query02 = """
________________
"""
all_neighborhood = gpd.read_postgis(query02, engine, geom_col='geom')
# Plotting all_neighborhood as the background, and pinpoint the station
fig, ax = plt.subplots(figsize=(10, 8))
all_neighborhood.plot(ax=ax, color = 'none')
subway_stations.plot(ax=ax, color='', label='')
plt.legend()
plt.title('')
plt.show()
In [ ]:
Copied!
query = """
SELECT __________
FROM ch05.streets AS street, ch05.neighborhoods AS nbh
WHERE ___________;
"""
query = """
SELECT __________
FROM ch05.streets AS street, ch05.neighborhoods AS nbh
WHERE ___________;
"""
In [ ]:
Copied!
query = """
SELECT _________________
FROM _________________
WHERE _____________________________;
"""
query = """
SELECT _________________
FROM _________________
WHERE _____________________________;
"""
In [ ]:
Copied!
query = """
SELECT _________
FROM _________
JOIN __________
ON _________
AND __________;
"""
query = """
SELECT _________
FROM _________
JOIN __________
ON _________
AND __________;
"""
In [ ]:
Copied!
query = """
"""
query = """
"""
In [ ]:
Copied!
query = """
"""
query = """
"""
Task 1¶
Select subway stations with the colors RED and GREY, then plot them in two different colors on the map.
In [ ]:
Copied!
query = """
"""
query = """
"""
Task 2: ST_Crosses¶
Using ST_Crosses to identify the streets that cross with the 100 m buffer around each station.
In [ ]:
Copied!
query = """
"""
query = """
"""
Task 3: ST_Centroid¶
Calculate the centroid of each neighborhood polygon using ST_Centroid and visualize them on the map.
In [ ]:
Copied!
query = """
"""
query = """
"""
Task 4. Nearest subway station to each centroid¶
Use these centroids to find the subway station closest to each neighborhood center.
MIN(ST_Distance()): The subquery is looking for the minimum distance between the neighborhood's centroid and all subway stations.
In [ ]:
Copied!
query = """
"""
query = """
"""