Database Reference
In-Depth Information
Exporting data to the shapefile with the
pgsql2shp PostGIS command
In this recipe, you will export a PostGIS table to a shapefile using the
pgsql2shp
command that is shipped with any PostGIS distribution.
How to do it...
The steps you need to followto complete this recipe are as follows:
1. Incaseyoustillhaven'tdoneit,exportthecountriesshapefiletoPostGISus-
ing the
ogr2ogr
or the
shp2pgsql
commands. The
shp2pgsql
approach
is as shown:
$ shp2pgsql -I -d -s 4326 -W LATIN1 -g
the_geom countries.shp chp01.countries >
countries.sql
$ psql -U me -d postgis_cookbook -f
countries.sql
2. The
ogr2ogr
approach is as follows:
$ ogr2ogr -f PostgreSQL
PG:"dbname='postgis_cookbook' user='me'
password='mypassword'" -lco SCHEMA=chp01
countries.shp -nlt MULTIPOLYGON -lco
OVERWRITE=YES -lco GEOMETRY_NAME=the_geom
3. Now,queryPostGISinordertogetalistofcountriesgroupedbythe
subre-
gion
field.Forthispurpose,youwillmergethegeometriesforfeatureshaving
thesame
subregion
codeusingthe
ST_Union
PostGISgeometricprocess-
ing function:
postgis_cookbook=> SELECT MIN(subregion)
AS subregion,ST_Union(the_geom) AS
the_geom, SUM(pop2005) AS pop2005FROM
chp01.countries GROUP BY subregion;