ogr2ogr is the GDAL's vector transform utility. It is - not without reason - considered a Swiss Army knife for vector transformations. Despite its size, ogr2ogr can handle a wide range of formats and this makes it a really worthy tool.
We'll use ogr2ogr to import a few data formats other than SHP, although ogr2ogr can obviously import SHP too. For this scenario, we'll use some data downloaded earlier:
- OS GB address base in GML format
- OS GB code point polygons in MapInof MIF & TAB formats
- USGS earthquakes in KML format
Some of the most common ogr2ogr params are:
- -f: The format of the output (when importing to PostGIS it will be PostgreSQL).
- -nln: Assigns a name to the layer. In the case of importing the data to PostGIS this will be the table name.
- -select: Lets you specify a comma separated list of columns to pick.
- -where: Lets you specify a sql like query to filter out the data.
- append: Appends data to the output dataset.
- overwrite: Overwrites the output datasource - in case of PostgreSQL it will drop and re-create a table.
- s_srs: Specifies the input SRID.
- t_srs: Transforms coordinates to the specified SRID.
- a_srs: Specifies the output SRID.
- lco NAME=VALUE: Layer creation options - these are driver specific; for pgsql options, see http://www.gdal.org/drv_pg.html. The most commonly used layer creation options are:
- LAUNDER: This defaults to YES. It is responsible for converting column names into pgsql compatible ones (lower case, underscores).
- PRECISION: This defaults to YES. It is responsible for using numeric and char types over float and varchar.
- GEOMETRY_NAME: Defaults to wkb_geometry.
Ogr2ogr has an accompanying utility called ogrinfo. This tool lets one inspect the metadata of a dataset. Verifying the metadata of any dataset prior to working with it is considered good practice and one should get into the habit of always using it before importing or exporting the data.