Difference between revisions of "PostGIS Installation"

From edegan.com
Jump to navigation Jump to search
Line 81: Line 81:
 
| California
 
| California
 
|-
 
|-
| tl_2016_06_place
+
| tl_2016_07_place
| California
+
|  
 
|-
 
|-
 
| tl_2016_08_place
 
| tl_2016_08_place
Line 100: Line 100:
 
|-
 
|-
 
| tl_2016_13_place
 
| tl_2016_13_place
 +
| Georgia
 +
|-
 +
| tl_2016_14_place
 
| Georgia
 
| Georgia
 
|-
 
|-
Line 108: Line 111:
 
| Idaho
 
| Idaho
 
|-
 
|-
| tl_2016_06_place
+
| tl_2016_17_place
| California
+
| Illinois
 
|-
 
|-
| tl_2016_06_place
+
| tl_2016_18_place
| California
+
| Indiana
 
|-
 
|-
| tl_2016_06_place
+
| tl_2016_19_place
| California
+
| Iowa
 
|-
 
|-
| tl_2016_06_place
+
| tl_2016_20_place
| California
+
| Kansas
 
|-
 
|-
| tl_2016_06_place
+
| tl_2016_21_place
| California
+
| Kentucky
 
|-
 
|-
 
| tl_2016_06_place
 
| tl_2016_06_place

Revision as of 11:09, 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

Table Name Corresponding State
tl_2016_01_place Alabama
tl_2016_02_place Alaska
tl_2016_03_place
tl_2016_04_place Arizona
tl_2016_05_place Arkansas
tl_2016_06_place California
tl_2016_07_place
tl_2016_08_place Colorado
tl_2016_09_place Missouri
tl_2016_10_place Delaware
tl_2016_11_place District of Columbia
tl_2016_12_place Florida
tl_2016_13_place Georgia
tl_2016_14_place Georgia
tl_2016_15_place Hawaii
tl_2016_16_place Idaho
tl_2016_17_place Illinois
tl_2016_18_place Indiana
tl_2016_19_place Iowa
tl_2016_20_place Kansas
tl_2016_21_place Kentucky
tl_2016_06_place California
tl_2016_06_place California