Академический Документы
Профессиональный Документы
Культура Документы
1
(Creative Commons by-nc-sa, 3.0)
D.Magni, Using
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
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
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
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
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
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
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)
Database
Then, OK
10
D.Magni, Using
Then, OK
11
(Creative Commons by-nc-sa, 3.0)
D.Magni, Using
12
(Creative Commons by-nc-sa, 3.0)
D.Magni, Using
12
D.Magni, Using
13
14
(Creative Commons by-nc-sa, 3.0)
D.Magni, Using
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
5. Check result
16
(Creative Commons by-nc-sa, 3.0)
D.Magni, Using
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)
Table columns
GiST index
18
(Creative Commons by-nc-sa, 3.0)
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
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:
20
Web Server
Architecture
Map file
CGI
2 4.b 3.a 5
Template file (HTML)
BROWSER
Data
PostgreSQL
3.b 4.a
21
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,
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)
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 :
24
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)
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
26
6. Open Free SQL Box 7. Load your .sql file (A) 8. Run the SQL statement (B)
27
(Creative Commons by-nc-sa, 3.0)
28
(Creative Commons by-nc-sa, 3.0)
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
29