Database Reference
In-Depth Information
Importing and exporting data with the
ogr2ogr GDAL command
In this recipe, you will use the popular
ogr2ogr
GDAL command for importing and
exporting vector data from PostGIS.
Firstly,youwillimportashapefileinPostGISusingthemostsignificantoptionsofthe
ogr2ogr
command.Then,stillusing
ogr2ogr
,youwillexporttheresultsofaspatial
query performed in PostGIS to a couple of GDAL-supported vector formats.
How to do it...
The steps you need to follow to complete this recipe are as follows:
1. Unzip the
TM_WORLD_BORDERS-0.3.zip
archive to your working directory.
You can find this archive in the topic's dataset.
2. Import the world countries shapefile (
TM_WORLD_BORDERS-0.3.shp
) in
PostGISusingthe
ogr2ogr
command.Usingsomeofthe
ogr2ogr
options,
you will import only the features from
SUBREGION=2
(Africa), and the
ISO2
and
NAME
attributes, and rename the feature class to
africa_countries
:
$ ogr2ogr -f PostgreSQL -sql "SELECT ISO2,
NAME AS country_name FROM
'TM_WORLD_BORDERS-0.3' WHERE REGION=2"
-nlt MULTIPOLYGON
PG:"dbname='postgis_cookbook' user='me'
password='mypassword'" -nln
africa_countries -lco SCHEMA=chp01 -lco
GEOMETRY_NAME=the_geom
TM_WORLD_BORDERS-0.3.shp
3. CheckiftheshapefilewascorrectlyimportedinPostGIS,queryingthespatial
table in the database or displaying it in aDesktop GIS.
4. Query PostGIS to get a list of the 50 active hotspots with the highest bright-
ness temperature (the
bright_t31
field) from the
global_24h
table cre-
ated in the previous recipe: