Difference between revisions of "PostGIS Installation"

From edegan.com
Jump to navigation Jump to search
(()
Line 56: Line 56:
 
> select count(*) from nyu_2451_34505;
 
> select count(*) from nyu_2451_34505;
 
> \q</nowiki>
 
> \q</nowiki>
 +
 +
== Translating Table names to corresponding States ==
 +
tl_2016_01_place --> Alabama
 +
tl_2016_02_place --> Alaska
 +
(...    03 ...  ) --> N/A
 +
tl_2016_04_place --> Arizona
 +
tl_2016_05_place --> Arkansas
 +
tl_2016_06_place --> California
 +
(...    07 ...  ) -->  N/A
 +
tl_2016_01_place --> Alabama
 +
tl_2016_01_place --> Alabama
 +
tl_2016_01_place --> Alabama
 +
tl_2016_01_place --> Alabama
 +
tl_2016_01_place --> Alabama
 +
tl_2016_01_place --> Alabama
 +
tl_2016_01_place --> Alabama
 +
tl_2016_01_place --> Alabama
 +
tl_2016_01_place --> Alabama
 +
tl_2016_01_place --> Alabama
 +
tl_2016_01_place --> Alabama
 +
tl_2016_01_place --> Alabama
 +
tl_2016_01_place --> Alabama
 +
tl_2016_01_place --> Alabama

Revision as of 10:51, 26 September 2017

PostGIS is a spatial database extender for PostgreSQL object-relational database. It adds support for geographic objects allowing location queries to be run in SQL.


Installation Commands

Going off of http://trac.osgeo.org/postgis/wiki/UsersWikiPostGIS23UbuntuPGSQL96Apt

$ lsb_release --codename
Codename:       trusty
$ sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt trusty-pgdg main" >> /etc/apt/sources.list'
$ wget --quiet -O - http://apt.postgresql.org/pub/repos/apt/ACCC4CF8.asc | sudo apt-key add -
$ sudo apt-get update
$ psql -V
psql (PostgreSQL) 9.5.4
$ sudo apt install postgresql-9.5-postgis-2.3
$ sudo apt-get install --no-install-recommends postgis

Note the last line. Without the --no-install-recommends line postgres assumes you're using the default configuration (in this case, postgis for postgresql 9.6). Since we're using a different flavor of postgis, we do not want this.

The postgis apt package is not the extension to postgres. It is a collection of command line utilities for importing and exporting data to a postgis db.

Creating a Postgis DB

Initial Setup

$ createdb --username researcher tigertest
$ psql --username researcher --dbname tigertest
> create extension postgis;
> select postgis_full_version(); -- sanity test and make sure installed and enabled
> \q

Bulk Download TIGER Shapefiles

For example, say we want all of the state-level place data. The first step is to find a programmatic URL that we can use. You can inspect the HTML on the HTML interface to place data to get the correct mapping of states/territories to two digit integer. Note that they are not necessarily sequential and there is not strictly fifty.

$ for i in $(seq -f "%02g" 1 80); do
    wget "https://www2.census.gov/geo/tiger/TIGER2016/PLACE/tl_2016_${i}_place.zip";
    sleep 3 # not necessary but a good guy scraper move
done;
$ for f in *.zip; do unzip "$f"; done
$ for f in *.shp; do shp2pgsql -I "$f" | psql -U researcher -d tigertest; done
$ psql --username researcher --dbname tigertest
> select count(*) from tl_2016_01_place; -- 585

Also say we want to separate New York City, New York into a more granular by-borough set of polygons. Using City University of New York data, we will import this shapefile.

$ curl --insecure https://archive.nyu.edu/retrieve/74704/nyu_2451_34505.zip > /tmp/boroughs.zip
$ cd /tmp
$ unzip boroughs.zip
$ shp2pgsql -I nyu_2451_34505/nyu_2451_34505.shp | psql -U researcher -d tigertest
$ psql --username researcher --dbname tigertest
> select count(*) from nyu_2451_34505;
> \q

Translating Table names to corresponding States

tl_2016_01_place --> Alabama
tl_2016_02_place --> Alaska
(...    03 ...  ) --> N/A
tl_2016_04_place --> Arizona
tl_2016_05_place --> Arkansas
tl_2016_06_place --> California
(...    07 ...  ) -->  N/A
tl_2016_01_place --> Alabama
tl_2016_01_place --> Alabama
tl_2016_01_place --> Alabama
tl_2016_01_place --> Alabama
tl_2016_01_place --> Alabama
tl_2016_01_place --> Alabama
tl_2016_01_place --> Alabama
tl_2016_01_place --> Alabama
tl_2016_01_place --> Alabama
tl_2016_01_place --> Alabama
tl_2016_01_place --> Alabama
tl_2016_01_place --> Alabama
tl_2016_01_place --> Alabama
tl_2016_01_place --> Alabama