/usr/local/var/postgres ☺ pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start • create a DB ☺ createdb nyc • and connect to it ☺ psql -h localhost nyc psql (9.1.3) Type "help" for help. nyc=#
nyc=# SELECT name FROM nyc_neighborhoods; name ----------------------------- Bensonhurst East Village West Village Throggs Neck Wakefield-Williamsbridge ...
housing unit in NYC? nyc=# SELECT Sum(popn_total)/Sum(hous_total) AS popn_per_house FROM nyc_census_blocks; popn_per_house -------------------- 2.6503540522400804 (1 row)
type? nyc=# SELECT type, Sum(ST_Length(the_geom)) AS length FROM nyc_streets GROUP BY type ORDER BY length DESC; type | length -------------------------------------------------- residential | 8629870.33786606 motorway | 403622.478126363 tertiary | 360394.879051303 motorway_link | 294261.419479668 ...
neighborhoods.boroname AS borough FROM nyc_neighborhoods AS neighborhoods JOIN nyc_subway_stations AS subways ON ST_Contains(neighborhoods.the_geom, subways.the_geom) WHERE subways.name = 'Broad St'; subway_name | neighborhood_name | borough -------------+--------------------+----------- Broad St | Financial District | Manhattan (1 row)
neigh. of Manhattan? nyc=# SELECT neighborhoods.name AS neighborhood_name, Sum(census.popn_total) AS population, Round(100.0 * Sum(census.popn_white) / Sum(census.popn_total),1) AS white_pct, Round(100.0 * Sum(census.popn_black) / Sum(census.popn_total),1) AS black_pct FROM nyc_neighborhoods AS neighborhoods JOIN nyc_census_blocks AS census ON ST_Intersects(neighborhoods.the_geom, census.the_geom) WHERE neighborhoods.boroname = 'Manhattan' GROUP BY neighborhoods.name ORDER BY black_pct DESC;
several days. How many people had to be evacuated? nyc=# SELECT Sum(popn_total) FROM nyc_neighborhoods AS n JOIN nyc_census_blocks AS c ON ST_Intersects(n.the_geom, c.the_geom) WHERE n.name = 'Battery Park'; sum ------ 9928 (1 row)
to use a spatial DB for large data • Without it one would need to sequentially scan all of the data • This is especially true when joining two large tables, since without indexing it would be extremely slow
nyc_census_blocks blocks ON ST_Intersects(neighborhoods.the_geom, blocks.the_geom) WHERE neighborhoods.name = 'West Village'; sum ------- 27141 (1 row)
of the Earth onto #at paper or screen • SRID encapsulates this information • We will need to know a data’s SRID when importing • We will also need to re-project and transform form one system to another