Database Reference
In-Depth Information
Importing shapefiles with shp2pgsql
If you need to import a shapefile in PostGIS, you have at least a couple of options
suchasthe ogr2ogr GDALcommand,asyouhaveseenpreviously,orthe shp2p-
gsql PostGIS command.
In this recipe, you will load a shapefile in the database using the shp2pgsql com-
mand, analyze it with the ogrinfo command, and display it in the QGIS Desktop
software.
How to do it...
The steps you need to follow to complete this recipe are as follows:
1. Createashapefilefromthevirtualdrivercreatedinthepreviousrecipeusing
the ogr2ogr command(notethatinthiscase,youdonotneedtospecifythe
-f option,astheshapefileisthedefaultoutputformatforthe ogr2ogr com-
mand):
$ ogr2ogr global_24h.shp global_24h.vrt
2. Generate the SQL dump file for the shapefile using the shp2pgsql com-
mand.Youaregoingtousethe -G optiontogenerateaPostGISspatialtable
usingthegeographytype,andthe -I optiontogeneratethespatialindexon
the geometric column:
$ shp2pgsql -G -I global_24h.shp
chp01.global_24h_geographic >
global_24h.sql
3. Analyze the global_24h.sql file (in Windows, use a text editor such as
Notepad):
$ head -n 20 global_24h.sql
SET CLIENT_ENCODING TO UTF8;
SET STANDARD_CONFORMING_STRINGS TO ON;
BEGIN;
Search WWH ::




Custom Search