Вы находитесь на странице: 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 Name of the spatial table Geometry dimensions

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 License : http://www.pgadmin.org/licence.php

(Creative Commons by-nc-sa, 3.0)

GIS (A) - 2006/2007

4. Exercises
4.1 - Connect a remote PostgreSQL server 1. Open pgAdmin III on your local PC (Start 2. Connect the PostgreSQL remote server File Aggiungi server File 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 ugis34
D.Magni, Using
(Creative Commons by-nc-sa, 3.0)

Programs

PostgreSQL

pgAdmin III)

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 1. Tabelle Tables


Properties

Nuova tabella New table

NR is your PSF number

Name = tableNR Holder = your user With OID (select checkbox)

Then, OK
10
D.Magni, Using

MapServer with PostgreSQL / PostGIS

(Creative Commons by-nc-sa, 3.0)

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


Constraints

3) Choose columns tab

1) Choose a primary key 4) Select id column as primary key

2) Click Aggiungi (Add)

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...) 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)

A B

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 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

2 4.b 3.a 5
Template file (HTML)
BROWSER

Data

PostgreSQL

3.b 4.a
21

Spatially DBMS enabler


(Creative Commons by-nc-sa, 3.0)

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)

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

(Creative Commons by-nc-sa, 3.0)

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 Use conditions http://creativecommons.org/licenses/by-nc-sa/3.0 http://creativecommons.org/licenses/by-nc-sa/3.0/deed.it 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

Вам также может понравиться