Getting ready

To get started, perform the following steps:

  1. Be sure that you have PostGIS topology support enabled in your database instance. This support is packaged as a separate extension and, if you are using PostgreSQL 9.1 or newer versions, you can install it using the following SQL CREATE EXTENSION command:
      postgis_cookbook=# CREATE EXTENSION postgis_topology;
  1. Download the administrative area archive for Hungary from the gadm.org website at http://gadm.org/country (or use the copy included in the code bundle provided with this book).
  2. Extract the HUN_adm1.shp shapefile from the archive to your working directory, working/chp03.

 

  1. Import the shapefile to PostGIS using a tool such as ogr2ogr or shp2pgsql, as follows:
      ogr2ogr -f PostgreSQL -t_srs EPSG:3857 -nlt MULTIPOLYGON 
-lco GEOMETRY_NAME=the_geom -nln chp03.hungary
PG:"dbname='postgis_cookbook' user='me'
password='mypassword'" HUN_adm1.shp
  1. After the import process is completed, you can check the count using the following command; note that this spatial table consists of 20 multipolygons, each representing one administrative area in Hungary:
      postgis_cookbook=# SELECT COUNT(*) FROM chp03.hungary; 
      (1 row)