100% found this document useful (1 vote)
67 views25 pages

Working With Postgis: in This Module You Will

This document provides instructions for installing PostgreSQL and PostGIS, loading spatial data into a PostGIS database, and configuring GeoServer to interact with the PostGIS database. It walks through installing PostgreSQL and PostGIS, loading building, hydrography, and roads shapefiles into PostGIS tables, and adding the PostGIS data source and a feature type to GeoServer.

Uploaded by

Kamal Bakari
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
67 views25 pages

Working With Postgis: in This Module You Will

This document provides instructions for installing PostgreSQL and PostGIS, loading spatial data into a PostGIS database, and configuring GeoServer to interact with the PostGIS database. It walks through installing PostgreSQL and PostGIS, loading building, hydrography, and roads shapefiles into PostGIS tables, and adding the PostGIS data source and a feature type to GeoServer.

Uploaded by

Kamal Bakari
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

MODULE 3: Working with PostGIS

Module 3
Working with PostGIS
This module focuses on how to use the PostGIS spatial database with
GeoServer. The reader will begin by walking through the installation process, and
continue on to learn how to load spatial data into PostGIS.

In this module you will:

• Install PostgreSQL and PostGIS.


• Learn how to load spatial data into PostGIS.
• Learn how to configure GeoServer to interact with a
PostGIS database.
• Learn how to convert a shapefile into a PostGIS table.

Before you start:

• Ensure that Module 2: The Basics have been


completed.
• Ensure that the OpenGEO USB drive is inserted.

1
MODULE 3: Working with PostGIS

Section 1: Installing PostgreSQL

1. Run the PostgreSQL installer by double-clicking on the “Postgres


install” on the welcome page (or software\postgresql-8.2.msi).

2. Begin the installer by selecting the language of your choice and


clicking Start.

2
MODULE 3: Working with PostGIS
3. On the Welcome screen click Next.

4. Accept the License Agreement / Installation Notes and click Next.

5. On the Installation Options screen accept all the defaults and click
Next.

The PostGIS extensions


should be left disabled. They
will be installed in the next
section.

3
MODULE 3: Working with PostGIS

6. On the Service Configuration screen, enter the password


“postgres2008” and click Next. When asked to create a new account,
click Yes.

For security
reasons PostgreSQL
requires a non-
privileged account to
be installed. The
installer will create
one for you. This
account can be
removed later.

Any warnings about


weak passwords can be
ignored.

4
MODULE 3: Working with PostGIS

7. On the Initialize Database Cluster screen, enter the password


“postgres” (note that this is different from the password in the previous
step), and click Next.

The database super


user is different from the
windows user account
created in the previous
step.

8. Continue through the rest of the installer accepting the defaults.

5
MODULE 3: Working with PostGIS

6
MODULE 3: Working with PostGIS

9. Finalize the installation and exit the installer.

7
MODULE 3: Working with PostGIS

Section 2: Installing PostGIS

1. Run the PostGIS installer by double-clicking the “PostGIS


install” link on the welcome page (or software\postgis-
pg82-setup-1.3.2-1.exe).

2. Accept the License Agreement and click Next.

8
MODULE 3: Working with PostGIS

3. Continue through the next three screens accepting the


defaults.

9
MODULE 3: Working with PostGIS

4. On the Database Connection screen specify the


password “postgres” and click Next.

5. Accept the default database name “postgis”, and click


Install.

10
MODULE 3: Working with PostGIS

6. Finalize the installation and exit the installer.

11
MODULE 3: Working with PostGIS

Section 3: Loading Spatial Data

1. Open the PostgresSQL Administration Console by opening the Start


Menu and navigating to PostgreSQL 8.2→pgAdmin III.

2. In pgAdmin connect to the database by right-clicking on PostgreSQL


Database Server 8.2 (localhost:5432) and selecting connect.

12
MODULE 3: Working with PostGIS

3. In the Connect To Server dialog, enter the password “postgres”, check


off Store Password, and click OK.

4. In the Object Browser navigate to Databases→postgis, and click the


SQL Query button in the toolbar.

13
MODULE 3: Working with PostGIS

5. In the Query window, open the File menu and select


Open…

6. In the File Explorer navigate to the data directory on the USB drive and
open the buildings.sql file.

The buildings.sql
file contains the SQL
statements to create a
table named roads and
populate it with data.

14
MODULE 3: Working with PostGIS

7. Execute the SQL by clicking the Execute Query button in the toolbar.

8. Repeat steps 5 through 7 with the hydrography.sql and roads.sql files.

15
MODULE 3: Working with PostGIS

Section 4: Adding a PostGIS Database

1. Open a web browser and navigate to the GeoServer Welcome Page at


http://localhost:8080/geoserver.

2. From the Welcome Page navigate to Config→Data.

3. Select the DataStores link and click New.

16
MODULE 3: Working with PostGIS

4. Select Postgis from the dropdown list and enter “postgis” in the text
box.

5. On the Feature Data Set Editor page:

a. Enter “postgis” in the database field


b. Enter “postgres” in the user field The editor page is
c. Enter “postgres” in the password field used to enter all the
d. Click Submit at the bottom of the page database connection
information. It also
provides a number of
parameters that can be
used to tune database
performance such as
connection pool size,
which specifies how
many open database
connections can exist
concurrently.

17
MODULE 3: Working with PostGIS

6. Click Apply and then Save located in the top left corner of the page.

18
MODULE 3: Working with PostGIS

Section 5: Adding a PostGIS Table

1. From the GeoServer Welcome page navigate to


Config→Data→FeatureTypes.

2. Click New.

3. Select postgis:::buildings from drop down list and click New.

The dropdown list


contains a list of the
tables which are available
to be published. When a
table is chosen from the
list a new Feature Type is
created from it.

19
MODULE 3: Working with PostGIS

4. On the FeatureType Editor page change the style by selecting polygon


from the Style dropdown list.

5. Generate the bounds for the layer by clicking Generate located on the
middle of the page.

6. Scroll to the bottom of the page and click Submit.

7. Apply and Save changes.

20
MODULE 3: Working with PostGIS

8. View the new layer from the Map Preview and clicking the
topp:buildings link.

21
MODULE 3: Working with PostGIS

9. Add the roads table repeating steps 1 through 7. Remember to:

a. Set the Style dropdown to line


b. Set the bounds with the Generate button
c. Click Apply after clicking Submit

10. Add the hydrography table repeating steps 1 through 7.

22
MODULE 3: Working with PostGIS

Extra Credit: Importing Shapefile Data


1. Navigate to the data directory on the USB drive.

2. Copy the following files onto the desktop.

sights.shp
sights.shx
sights.dbf
sights.prj

3. From the Start menu, open a Command Line Prompt.

23
MODULE 3: Working with PostGIS

4. Change directory to the desktop by executing the command line:

cd Desktop

5. Convert the sights.shp file to SQL with the shp2pgsql utility by


executing the command line:

“C:\Program Files\PostgreSQL\8.2\bin\shp2pgsql.exe”
-s 4326 sights.shp sights > sights.sql
Note the double
quotes “” around the
path to shp2pgsql.
They are necessary
because of the space
in the file path.

shp2pgsql is a
utility program used
to convert a shapefile
into a database table.
It comes with every
PostGIS installation.

6. Execute the sights.sql script against the database by


executing the command line:

“C:\Program Files\PostgreSQL\8.2\bin\psql.exe”
–U postgres postgis < sights.sql

If prompted for a
password remember
that the password is
“postgres”.

psql is a
program used to
interact with a
Postgres database
from the command
line.
24
MODULE 3: Working with PostGIS

7. Using skills learned in the previous section, publish the sights table
created in the previous step.

25

You might also like