Tutorial All PPSS PostGIS
Tutorial All PPSS PostGIS
Introduction
PostGIS is a powerful spatial database extension you can use with Postgres Plus Standard
Server. This tutorial shows how quickly you can setup and begin using a spatially enabled
database that can be applied to a variety of applications.
This EnterpriseDB Quick Tutorial helps you get started with the Postgres Plus Standard
Server database in a Linux or Windows environment. It is assumed that you have already
downloaded and installed Postgres Plus Standard Server on your desktop or laptop
computer.
This Quick Tutorial is designed to help you expedite your Technical Evaluation of
Postgres Plus Standard Server or Postgres Plus Advanced Server. For more informational
assets on conducting your evaluation of Postgres Plus, visit the self-service web site,
Postgres Plus Open Source Adoption.
In this Quick Tutorial you will learn how to do the following:
insert test data into your tables and view the data
Usage Note: While the examples in this tutorial demonstrated in a Linux environment,
the steps are the same for the Windows and Mac environments. You will notice slight
variations between the operating systems; there are differences in the tools used (e.g.
terminal windows and text editors), the use of forward slashes vs. back slashes in path
specifications, and the installation directory locations.
Resource Management
Emergency Planning
Surveying and Cartography
Infrastructure Planning and Maintenance
GPS Applications
POINT
LINE
POLYGON
MULTIPOINT
MULTILINE
MULTIPOLYGON
GEOMETRYCOLLECTION
In addition to adding GIS storage capabilities to Postgres Plus Standard Server, PostGIS
also adds support for GiST-based R-tree spatial indexes; these indexes allow you to
quickly find GIS data based on criteria specified in terms of spatial relationship. An
example of such a query would be to SELECT all Chinese restaurants within X miles of a
given geographical point.
PostGIS also includes numerous functions you can use to analyze GIS objects; the
functions provide features such as:
The support that PostGIS provides for GIS objects to Standard Server is comparable to
support added by Oracle Spatial, DB2 Spatial, and SQL Server Spatial to their associated
databases.
Tutorial Steps
PostGIS Installation and Setup
PostGIS is included with the Postgres Plus Standard Server distribution, available from
the EnterpriseDB website at: www.enterprisedb.com/products/downloads.do. After
downloading the installer, double-click the icon to start the installation wizard.
When the installation wizard displays the Select Components dialog, confirm that the
box next to PostGIS is checked to include PostGIS in the installation process. Click
Next to continue. The installation wizard completes the installation of Postgres Plus
Standard Server and creates a PostGIS enabled database called template_postgis.
You can use StackBuilder Plus (distributed with Postgres Plus Standard Server) to add
PostGIS functionality to an existing Standard Server installation. To start StackBuilder
Plus, open the Start menu and navigate to the Postgres Plus Standard Server
menu. Select StackBuilder Plus to start the StackBuilder Plus wizard.
Copyright 2009 EnterpriseDB Corporation. All rights reserved.
Expand the Spatial Extensions node, and check the box next to PostGIS 1.4
for PostgreSQL 8.4. Click Next to continue. After the StackBuilder Plus wizard
downloads the PostGIS files, use the PostGIS Setup wizard to complete the
installation process.
StackBuilder Plus alleviates the need to manually:
Postgres Plus confirms that the new role has been created:
The options that follow the CREATE ROLE command grant privileges to the new role:
Postgres Plus confirms that the new database has been created:
Creating a table that contains spatial data is a two-step process. First, you create the
table, omitting the columns that will hold the GIS data; then you add the GEOMETRY
columns with the PostGIS AddGeometryColumn() function.
CREATE TABLE roads (id INT4, name VARCHAR(128));
Copyright 2009 EnterpriseDB Corporation. All rights reserved.
Standard Server confirms that the table has been created, and contains a column of type
GEOMETRY:
A GEOMETRY column can hold any type of GIS data; you specify the GIS type when
adding data to the table. The following series of SQL commands loads sample data of the
type, LINESTRING into the table, roads. LINESTRING data describes a line connecting
two points.
INSERT INTO roads (id, geom, name)
VALUES (1, GeomFromText('LINESTRING(0 10,0 0)', -1),
'Beacon Road');
INSERT INTO roads (id, geom, name)
VALUES (2, GeomFromText('LINESTRING(0 0,0 10)', -1),
'Violet Road');
INSERT INTO roads (id, geom, name)
VALUES (3, GeomFromText('LINESTRING(0 0,10 0)', -1),
'Skelton Street');
INSERT INTO roads (id, geom, name)
VALUES (4, GeomFromText('LINESTRING(0 0,10 10)', -1),
'Fifth Avenue');
INSERT INTO roads (id, geom, name)
VALUES (5, GeomFromText('LINESTRING(10 0,0 0)', -1),
'Lipton Street');
To improve performance on the roads table, create an index on the geom column. The
following code creates an index called roads_index:
Copyright 2009 EnterpriseDB Corporation. All rights reserved.
Now, optimize the roads table and collect performance statistics with the Postgres
VACUUM command:
VACUUM FULL VERBOSE ANALYZE roads;
The VACUUM command reclaims storage vacated by deleted tuples. Using the VACUUM
command without additional options performs the optimization without requiring a full
lock on the table, and can be performed during normal read/write functions.
Adding the FULL parameter to the command performs a more thorough optimization of
the table that may include moving tuples across data blocks; Standard Server requires a
table lock to perform the task.
Adding the VERBOSE parameter to the command line instructs Standard Server to print a
detailed report of the vacuum-related activity on the table. The ANALYZE parameter
updates system statistics used by the query optimizer to determine the most efficient
query plan.
Postgres Plus Standard Server displays the data in the roads table, in order by id:
PostGIS includes functions that perform spatial calculations. The following query uses
the DISTANCE() function to find all entries in the roads table that are within 5 units of
a geographic point.
SELECT id, name AsText(geom) FROM roads
WHERE DISTANCE(geom, GeomFromText(POINT(5 5), -1)) < 5;
The PostGIS ~= operator returns any data that matches the value specified in the query;
the following query identifies any row with a geom column with a value of (0 10,0
0), -1.:
The && operator is a Boolean operator that evaluates to TRUE if the first value in an
argument overlaps the second value. You can use the && operator to find all of the roads
that intersect a given point:
SELECT name, AsText(geom) FROM roads,
WHERE geom && ST_MakePoint(10, 10);
The WHERE clause tells Postgres to find each road where the geometry column (geom)
overlaps the given point (the point 10, 10).
This document is an introduction to using PostGIS with an Standard Server database. For
additional information about using PostGIS please visit:
http://postgis.refractions.net.
Conclusion
In this Tutorial, we demonstrated how to set up a PostGIS database in Postgres Plus
Standard Server.
Copyright 2009 EnterpriseDB Corporation. All rights reserved.
10
11