14. Ejercicios de Joins espaciales¶
Aquí tienes un recordatorio de algunas de las funciones que hemos visto. Pista: ¡serán útiles para los ejercicios!
suma(expresión): función de agregación para devolver una suma de un conjunto de registros
count(expression): devolver la cantidad de filas (registros) que retorna una consulta
ST_Area(geometría) devuelve el área de la geometría
ST_AsText(geometry) devuelve la representación de geometry en formato WKT
ST_Contiene(geometría A, geometría B) devuelve verdadero si la geometría A contiene a la geometría B
ST_Distancia(geometría A, geometría B) devuelve la distancia mínima entre la geometría A y la geometría B
ST_DWithin(geometría A, geometría B, radio) devuelve verdadero si la geometría A está a una distancia dentro del “radio” de la geometría B
ST_GeomFromText(text) retorna un objeto
geometry
desde una definición WKTST_Intersects(geometry A, geometry B) returns the true if geometry A intersects geometry B
ST_Length(linestring) retorna la longitud de la línea
ST_Touches(geometry A, geometry B) returns the true if the boundary of geometry A touches geometry B
ST_Within(geometry A, geometry B) returns the true if geometry A is within geometry B
También recuerde las tablas que tenemos disponibles:
nyc_census_blocks
name, popn_total, boroname, geom
nyc_streets
name, type, geom
nyc_subway_stations
name, routes, geom
nyc_neighborhoods
name, boroname, geom
14.1. Ejercicios¶
What subway station is in “Little Italy”? What subway route is it on?
SELECT s.name, s.routes FROM nyc_subway_stations AS s JOIN nyc_neighborhoods AS n ON ST_Contains(n.geom, s.geom) WHERE n.name = 'Little Italy';
name | routes -----------+-------- Spring St | 6
What are all the neighborhoods served by the 6-train? (Hint: The
routes
column in thenyc_subway_stations
table has values like “B,D,6,V” and “C,6”)SELECT DISTINCT n.name, n.boroname FROM nyc_subway_stations AS s JOIN nyc_neighborhoods AS n ON ST_Contains(n.geom, s.geom) WHERE strpos(s.routes,'6') > 0;
name | boroname --------------------+----------- Midtown | Manhattan Hunts Point | The Bronx Gramercy | Manhattan Little Italy | Manhattan Financial District | Manhattan South Bronx | The Bronx Yorkville | Manhattan Murray Hill | Manhattan Mott Haven | The Bronx Upper East Side | Manhattan Chinatown | Manhattan East Harlem | Manhattan Greenwich Village | Manhattan Parkchester | The Bronx Soundview | The Bronx
Nota
We used the
DISTINCT
keyword to remove duplicate values from our result set where there were more than one subway station in a neighborhood.After 9/11, the “Battery Park” neighborhood was off limits for several days. How many people had to be evacuated?
SELECT Sum(popn_total) FROM nyc_neighborhoods AS n JOIN nyc_census_blocks AS c ON ST_Intersects(n.geom, c.geom) WHERE n.name = 'Battery Park';
17153
What neighborhood has the highest population density (persons/km2)?
SELECT n.name, Sum(c.popn_total) / (ST_Area(n.geom) / 1000000.0) AS popn_per_sqkm FROM nyc_census_blocks AS c JOIN nyc_neighborhoods AS n ON ST_Intersects(c.geom, n.geom) GROUP BY n.name, n.geom ORDER BY popn_per_sqkm DESC LIMIT 2;
name | popn_per_sqkm -------------------+------------------ North Sutton Area | 68435.13283772678 East Village | 50404.48341332535