0% found this document useful (0 votes)
69 views29 pages

Mapserver Postgis CC

The document describes how to use MapServer with PostgreSQL/PostGIS. It discusses: 1. How to connect to a PostgreSQL database using pgAdmin III and browse the database structure. 2. How to create tables, add geometry columns, and spatial indexes to enable spatial functionality. 3. How to populate tables with data, convert tables to shapefiles, and shapefiles to tables for use in MapServer.
Copyright
© © All Rights Reserved
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
0% found this document useful (0 votes)
69 views29 pages

Mapserver Postgis CC

The document describes how to use MapServer with PostgreSQL/PostGIS. It discusses: 1. How to connect to a PostgreSQL database using pgAdmin III and browse the database structure. 2. How to create tables, add geometry columns, and spatial indexes to enable spatial functionality. 3. How to populate tables with data, convert tables to shapefiles, and shapefiles to tables for use in MapServer.
Copyright
© © All Rights Reserved
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/ 29

GIS (A) - 2006/2007

Using MapServer
with
PostgreSQL / PostGIS

1
(Creative Commons by-nc-sa, 3.0)

D.Magni, Using

MapServer with PostgreSQL / PostGIS

GIS (A) - 2006/2007

1. PostgreSQL

ORDBMS : Object oriented Relational DataBase Management System


Link : http://www.postgresql.org/
Release for exercises : 8.2 for Windows
with PostGIS
with pgAdmin III
OpenSource License : BSD

2
(Creative Commons by-nc-sa, 3.0)

D.Magni, Using

MapServer with PostgreSQL / PostGIS

GIS (A) - 2006/2007

2. PostGIS
PostGIS adds support for geographic objects to the PostgreSQL
object-relational database.
In effect, PostGIS "spatially enables" the PostgreSQL server,
allowing it to be used as a backend spatial database
for geographic information systems (GIS) (citation from PostGIS website)

Link : http://postgis.refractions.net/
Release for exercises : 8.2 for Windows
OpenSource License : GPL (General Public License)

3
(Creative Commons by-nc-sa, 3.0)

D.Magni, Using

MapServer with PostgreSQL / PostGIS

GIS (A) - 2006/2007

2. PostGIS
Allows to geo-refer tables of PostgreSQL (tables become spatial tables)
This is possible by adding a Geometry Column to tables
The Geometry Column (type=geometry) contains data with the syntax:
SRID=<crs_id>;<GEOMETRY_TYPE>(<COORDINATES>,<COORDINATES>)

Example:
SRID=3003;LINESTRING(1503032.67 5071234.09,1503052.55 5073234.21)

For Gauss-Boaga (West) SRID=3003; if the CRS is not specified, set SRID=-1

4
(Creative Commons by-nc-sa, 3.0)

D.Magni, Using

MapServer with PostgreSQL / PostGIS

GIS (A) - 2006/2007

2. PostGIS
<GEOMETRY_TYPE> can be:
POINT(0 0)
LINESTRING(0 0,1 1,1 2)
POLYGON((0 0,4 0,4 4,0 4,0 0),(1 1, 2 1, 2 2, 1 2,1 1))
MULTIPOINT(0 0,1 2)
MULTILINESTRING((0 0,1 1,1 2),(2 3,3 2,5 4))
MULTIPOLYGON(((0 0,4 0,4 4,0 4,0 0),(1 1,2 1,2 2,1 2,1 1)),
((-1 -1,-1 -2,-2 -2,-2 -1,-1 -1)))
GEOMETRYCOLLECTION(POINT(2 3),LINESTRING((2 3,3 4)))

5
(Creative Commons by-nc-sa, 3.0)

D.Magni, Using

MapServer with PostgreSQL / PostGIS

GIS (A) - 2006/2007

2. PostGIS
A DB spatially enabled by PostGIS has two special tables:
geometry_columns: contains a row for each spatial table of that DB
Name of geometry column
Geometry dimensions

Name of the spatial table

CRS (SRID)

Geometry type

spatial_ref_sys: is a list of CRSs, like the EPSG file seen for PROJ library

6
(Creative Commons by-nc-sa, 3.0)

D.Magni, Using

MapServer with PostgreSQL / PostGIS

GIS (A) - 2006/2007

3. pgAdmin III
Frontend application for working with PostgreSQL without command line
Link : http://www.pgadmin.org/
Release for exercises : 1.6.2 for Windows

(Creative Commons by-nc-sa, 3.0)

License : http://www.pgadmin.org/licence.php

GIS (A) - 2006/2007

4. Exercises
4.1 - Connect a remote PostgreSQL server
1. Open pgAdmin III on your local PC (Start J Programs J PostgreSQL J pgAdmin III)
2. Connect the PostgreSQL remote server
File J Aggiungi server
File J Add server
A
B
A - Service name: GIS course
B - Host name: 192.168.157.30
C
D
C Manager DB: gis
E
D - User name: ugisNR
E - Password: ugisNR
3. Click OK
NR is the same number of your PSF;
Example: psf34 J ugis34
D.Magni, Using
(Creative Commons by-nc-sa, 3.0)

MapServer with PostgreSQL / PostGIS

GIS (A) - 2006/2007

4. Browse the server tree

Database

geometry_columns
Tables
spatial_ref_sys
9
(Creative Commons by-nc-sa, 3.0)

GIS (A) - 2006/2007

4.2 Create a table


NR is your
PSF number

1. Tabelle J Nuova tabella


Tables J New table
Properties

Name = tableNR
Holder = your user

Then, OK
10
D.Magni, Using

MapServer with PostgreSQL / PostGIS

(Creative Commons by-nc-sa, 3.0)

With OID (select checkbox)

GIS (A) - 2006/2007

4.2 Create a table


Columns

Column name = id
Column type = integer

Is not null (select checkbox)

Then, OK
11
(Creative Commons by-nc-sa, 3.0)

D.Magni, Using

MapServer with PostgreSQL / PostGIS

GIS (A) - 2006/2007

4.2 Create a table


Columns

Insert a new column:


Name = name
Type = character varying
Length = 25

12
(Creative Commons by-nc-sa, 3.0)

D.Magni, Using

MapServer with PostgreSQL / PostGIS

12

GIS (A) - 2006/2007

4.2 Create a table


3) Choose columns tab

Constraints

1) Choose a primary key

2) Click Aggiungi (Add)

4) Select id column
as primary key

5) Click Aggiungi and13


OK
(Creative Commons by-nc-sa, 3.0)

D.Magni, Using

MapServer with PostgreSQL / PostGIS

13

GIS (A) - 2006/2007

4.2 Create a table

Correspondent SQL statements


Confirm table creation with OK

14
(Creative Commons by-nc-sa, 3.0)

D.Magni, Using

MapServer with PostgreSQL / PostGIS

GIS (A) - 2006/2007

4.3 Add a Geometry Column


1. Open Free SQL Box

2. Write the SQL statement:


SELECT AddGeometryColumn(<dbName>,<tableName>,
<geometryColumnName>,<SRID>,<geometryType>,
<geometryDimension>);
3. For the table just created write:
SELECT AddGeometryColumn(gis,tableNR,coord,3003,POINT,2);

15
(Creative Commons by-nc-sa, 3.0)

D.Magni, Using

MapServer with PostgreSQL / PostGIS

GIS (A) - 2006/2007

4.3 Add a Geometry Column

4. Confirm with Run button

5. Check result
16
(Creative Commons by-nc-sa, 3.0)

D.Magni, Using

MapServer with PostgreSQL / PostGIS

GIS (A) - 2006/2007

4.4 Add a GiST index


1. Open Free SQL Box
2. Write the SQL statement:
CREATE INDEX <indexName>
ON <tableName>
USING GIST ( <geometryColumnName> );
Here:
CREATE INDEX mygistNR ON tableNR USING GIST ( coord );
3. Confirm the SQL statement

17
(Creative Commons by-nc-sa, 3.0)

GIS (A) - 2006/2007

Table columns

Primary key
Geometry column indexes

GiST index

18
(Creative Commons by-nc-sa, 3.0)

GIS (A) - 2006/2007

4.5 Populate a table


1. Select the table
2. Right click on the table name
and select Visualizza
dati (View data)
3. Choose if display all rows
(Mostra tutte le
righe), first 100 rows
(Visualizza le prime
100 righe) or filtered rows
(Visualizza righe
filtrate...)

A B

or
1. Select the table
2. Click on A to display all rows
or B to display filtered rows
(Creative Commons by-nc-sa, 3.0)

19

GIS (A) - 2006/2007

4.5 Populate a table


4. Fill in the rows (id,name,coord) as shown in the example

5. Dont fill in oid: PostgreSQL does it automatically


6. Press Enter to confirm
Its possible to populated tables by SQL directly (standard mode)
Open Free SQL Box and write
INSERT INTO <tableName>(<column1>,<column2>,...)
VALUES (<value1>,<value2>,...);
If all columns are populated, <column1>,<column2>,... specification can be omitted
X Here write:

INSERT INTO table1 VALUES (2,Monte Tre Croci,


SRID=3003;POINT(1507871 5070661));
(Creative Commons by-nc-sa, 3.0)

20

GIS (A) - 2006/2007

4.6 Display a PostgreSQL spatial table with MapServer

Web Server

Architecture
Map file

CGI
Data

2
4.b

3.a
5

Template file
(HTML)
BROWSER

Spatially DBMS enabler


(Creative Commons by-nc-sa, 3.0)

PostgreSQL

3.b
4.a
21

GIS (A) - 2006/2007

4.6 Display a PostgreSQL spatial table with MapServer


1. Open your map file
2. Write a new layer to load PostgreSQL/PostGIS data:
LAYER
NAME "layername"
TYPE point
STATUS default
Connection by PostGIS
Connection parameters
CONNECTIONTYPE postgis
CONNECTION "dbname=<db> host=<host> port=5432 user=<user> password=<pwd>"
DATA "<geometryColumn> from <tableName>"
Geometry column from table
CLASS
NAME "voice of legend"
STYLE
COLOR 255 0 255
SYMBOL "45rotated_cross"
END
END
END
If PostgreSQL and MapServer share the same host,

its possible to write host=127.0.0.1 or host=localhost


22
(Creative Commons by-nc-sa, 3.0)

GIS (A) - 2006/2007

4.6 Display a PostgreSQL spatial table with MapServer


3. Write a layer for tableNR data
4. Check that the layer is correctly drawn in the map
5. Insert other 5 rows in your tableNR and see the result on the map

23
(Creative Commons by-nc-sa, 3.0)

GIS (A) - 2006/2007

4.7 PostgreSQL table to shapefile conversion


Its possible to convert a PostgreSQL/PostGIS table to a shapefile

Open an MS-DOS command prompt


Enter the folder of your local PostgreSQL installation (refer to it as
$PostgreSQL), by cd command
Enter 8.2\bin\ folder
Run the pgsql2shp command, with the following syntax:
pgsql2shp -h [hostName] -u [PostgreSQLUserName] -P
[password] -f [path\NewShapefileName] [dbName]
[tableName]

5. Check that the output message is :

Number of table rows converted to shapefile features


(Creative Commons by-nc-sa, 3.0)

24

GIS (A) - 2006/2007

4.7 PostgreSQL table to shapefile conversion


6. Convert your tableNR to a new shapefile:
pgsql2shp -h [hostName] -u [PostgreSQLUserName] -P
[password] -f C:\Temp\tableNR gis tableNR
7. Open target folder and check that the new shapefile has been created
correctly (.shp, .shx, and .dbf files)
8. Open it in a desktop GIS (e.g. ArcGIS) and verify its structure

25
(Creative Commons by-nc-sa, 3.0)

(Creative Commons by-nc-sa, 3.0)

GIS (A) - 2006/2007

4.8 Shapefile to PostgreSQL table conversion


Its also possible to convert a shapefile to a PostgreSQL/PostGIS table.

Open an MS-DOS command prompt


Enter the folder of your local PostgreSQL installation (refer to it as
$PostgreSQL), by cd command
Enter 8.2\bin\ folder
Run the shp2pgsql command, with the following syntax:
shp2pgsql -c -s [SRID] -g [geometryColumnName] -I
[path\ShapefileName.shp] [newTableName] >
[path\outputFilename.sql]
-c means that the table is created and populated (other options are: -a: append
shapefile into a current table; -d: drop the table and recreate it with shapefile data;
-p: only create the table, without populating it.
-I creates a GiST index for the new table

5. Check that the output message is :

26

GIS (A) - 2006/2007

4.8 Shapefile to PostgreSQL table conversion

6. Open Free SQL Box


7. Load your .sql file (A)
8. Run the SQL statement (B)

9. Check that the table has been created properly

27
(Creative Commons by-nc-sa, 3.0)

GIS (A) - 2006/2007

Documentation and References


PostgreSQL documentation: http://www.postgresql.org/docs/
PostGIS documentation: http://postgis.refractions.net/documentation/
gdAdmin III documentation: http://www.pgadmin.org/docs/
Loading PostgreSQL/PostGIS tables in MapServer:
http://mapserver.gis.umn.edu/docs/reference/mapfile/layer
http://postgis.refractions.net/docs/ch04.html#id2879503
Atzeni P., Ceri S., Peraboschi S., Torlone R. (1999). Basi di dati - McGraw-Hill

28
(Creative Commons by-nc-sa, 3.0)

GIS (A) - 2006/2007

License
This document is released under the following license:

Creative Commons , Attribution Noncommercial - Share Alike , 3.0


Creative Commons , Attribuzione - Non commerciale - Condividi allo stesso modo , 3.0
More information
X Use conditions
http://creativecommons.org/licenses/by-nc-sa/3.0
http://creativecommons.org/licenses/by-nc-sa/3.0/deed.it
X Legal Code (the full license)
http://creativecommons.org/licenses/by-nc-sa/3.0/legalcode

Last update: 24/10/2007


(Creative Commons by-nc-sa, 3.0)

29

You might also like