Using PostGIS – the Geographical Information System extension to PostgreSQL

1. Load PostGIS database with geographical data

PostGIS works well with SRI shape data (.shp, .dbf files). The track data download from public data sites are mostly zipped shape data files.

1.1 convert shape data to postGIS sql data
shp2pgsql [options] shapefile [schema.]table

e.g.
shp2pgsql tl_2013_us_zcta510 public.tl_2013_us_zcta510 > tl_2013_us_zcta510.sql
shp2pgsql -s 26986 tl_2013_25_tract.shp tl_2013_25_tract.dbf > tl_2013_25_tract.sql

SRID can be found by querying postGIS via, e.g.

select srid, srtext, proj4text from spatial_ref_sys where srtext ILIKE '%Massachusetts%'

This example is to convert the MA tract shape data into postGIS data, where 26986 is the MA SRID, which is the Spatial Reference System Identifier (SRID) is a unique value used to unambiguously identify projected, unprojected, and local spatial coordinate system definitions. These coordinate systems form the heart of all GIS applications.

1.2 convert GEOJSON file to postGIS sql data

GEOJSON is a widely used JSON format for geographical data. Importing GEOJSON data into postGIS database can be done in two ways:

a. convert GEOJSON to shape data and convert shape data to postGIS sql data.

e.g.
ogr2ogr -f "ESRI Shapefile" cities.shp cities.geojson OGRGeoJSON

b. uses PostGIS ST_GeomFromGeoJSON function to convert GEOJSON to PostGIS geometry object (JSON-C >= 0.9 is required).

e.g.
SELECT ST_AsText(ST_GeomFromGeoJSON('{"type":"Point","coordinates":[-48.23456,20.12345]}')) As wkt;
wkt
------
POINT(-48.23456 20.12345)

1.3 load the postGIS sql data into database

psql -d database-name -h localhost -U user-name -f towns.sql

1.4 load DEM (digital elevation model) file into postGIS

DEM (digital elevation model) file contains the elevation data of an area that provides the 3rd dimension of the geographical characteristics. Importing a .dem file data can be done in three steps:

a. convert the .dem file into contour shapefile
e.g.
3d version: gdal_contour -3d greeley-w.dem 3d-contour/greeley-w.shp -i 2.0
2d version: gdal_contour greeley-w.dem 3d-contour/greeley-w.shp -i 2.0

where: -i 2.0 specifies the elevation interval between contours.

b. convert the shape file into postGIS sql file

e.g.
shp2pgsql -s 26953 -I greeley-w dem.greeley-w > greeley-w.sql

c. import the postGIS sql file into database

The above procedures will import the contour data as multiline string geometry objects into postGIS database.