Installing PostGIS - Google Code

cuttlefishblueΔιαχείριση Δεδομένων

16 Δεκ 2012 (πριν από 4 χρόνια και 6 μήνες)

215 εμφανίσεις

1
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
.
MODULE 3: Working with PostGIS
2
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
.
MODULE 3: Working with PostGIS
3
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
.
MODULE 3: Working with PostGIS
The PostGIS extensions

should be left
disabled
. They

will be installed in the next

section.
4
6.
On the
Service Configuration
screen, enter the password

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

click
Yes
.
MODULE 3: Working with PostGIS
Any warnings about

weak passwords can be

ignored.
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.
5
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
.

8.
Continue through the rest of the installer accepting the defaults.
MODULE 3: Working with PostGIS
The database super

user is
different
from the

windows user account

created in the previous

step.
6
MODULE 3: Working with PostGIS
7
9.
Finalize the installation and exit the installer.
MODULE 3: Working with PostGIS
8
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
.
MODULE 3: Working with PostGIS
9
3.
Continue through the next three screens accepting the

defaults.
MODULE 3: Working with PostGIS
10
4.
On the
Database Connection
screen specify the

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

Install
.
MODULE 3: Working with PostGIS
11
6.
Finalize the installation and exit the installer.
MODULE 3: Working with PostGIS
12
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
.
MODULE 3: Working with PostGIS
13
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.
MODULE 3: Working with PostGIS
14
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.
MODULE 3: Working with PostGIS
The
buildings.sql

file contains the SQL

statements to create a

table named roads and

populate it with data.
15
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.
MODULE 3: Working with PostGIS
16
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.
MODULE 3: Working with PostGIS
17
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
c.
Enter “postgres” in the
password
field
d.
Click
Submit
at the bottom of the page
MODULE 3: Working with PostGIS
The editor page is

used to enter all the

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.
18
6.
Click
Apply
and then
Save
located in the top left corner of the page.
MODULE 3: Working with PostGIS
19
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
.
MODULE 3: Working with PostGIS
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.
20
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.
MODULE 3: Working with PostGIS
21
8.
View the new layer from the
Map Preview
and clicking the

topp:buildings
link.
MODULE 3: Working with PostGIS
22
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.
MODULE 3: Working with PostGIS
23
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
.
MODULE 3: Working with PostGIS
24
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
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
MODULE 3: Working with PostGIS

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.
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.
25
7.
Using skills learned in the previous section, publish the
sights
table

created in the previous step.
MODULE 3: Working with PostGIS