You are on page 1of 10

How

to use PostGIS with Postgres Plus Advanced Server

How to use PostGIS with Postgres Plus Advanced Server A Postgres Evaluation Quick Tutorial From EnterpriseDB
June 29, 2010

(R)

EnterpriseDB Corporation, 235 Littleton Road, Westford, MA 01866, USA T +1 978 589 5700 F +1 978 589 5701 E info@enterprisedb.com www.enterprisedb.com

Copyright 2010 EnterpriseDB Corporation. All rights reserved.

How to use PostGIS with Postgres Plus Advanced Server

Introduction
This EnterpriseDB Quick Tutorial helps you get started with the Postgres Plus Advanced Server database in a Linux or Windows environment. It is assumed that you have already downloaded and installed Postgres Plus Advanced Server on your desktop or laptop computer. This Quick Tutorial is designed to help you expedite your Technical Evaluation of Postgres Plus Advanced Server. In this Quick Tutorial you will learn: How to install and configure PostGIS in a Postgres Plus Advanced Server environment. How to create a PostGIS database with spatially enabled tables and indexes. How to add and display spatial data in an Advanced Server database.

Usage Note: While the examples in this tutorial are demonstrated in a Windows environment, the steps are the same for the Linux 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. This tutorial is a step-by-step guide to getting started with PostGIS and Postgres Plus Advanced Server. After reading through this tutorial, you will be able to setup up your environment and deploy a PostGIS database. For more informational assets on conducting your evaluation of Postgres Plus, visit the self-service web site, Postgres Plus Open Source Adoption.

Using a PostGIS database


A Geographic Information System (GIS) object is a defined unit of geographical data that represents a specific spatial object. GIS technology has many applications, including: Resource Management Emergency Planning Surveying and Cartography Infrastructure Planning and Maintenance GPS Applications

PostGIS is an extension to Postgres Plus Advanced Server that stores GIS objects. PostGIS supports storage of the following GIS object types:
POINT LINE

Copyright 2010 EnterpriseDB Corporation. All rights reserved.

How to use PostGIS with Postgres Plus Advanced Server


POLYGON MULTIPOINT MULTILINE MULTIPOLYGON GEOMETRYCOLLECTION

In addition to adding GIS storage capabilities to Advanced 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: Calculate the surface area of a given object. Find the distance between two objects. Locate the Geometric center of a given object.

The support that PostGIS provides for GIS objects to Advanced Server is comparable to support added by Oracle Spatial, DB2 Spatial, and SQL Server Spatial to their associated databases.

PostGIS Installation
PostGIS is included with the standard distribution of the Postgres Plus Advanced Server installer, available at the EnterpriseDB website at: www.enterprisedb.com/products/downloads.do. After downloading the installer, start the installation wizard; the wizards dialogs will guide you through the installation of Advanced Server and PostGIS. The installation process creates a database called template_postgis that contains the PostGIS functions and data types; use the template_postgis database as a template from which to create other databases.

Viewing PostGIS Database Objects with Postgres Studio


Postgres Studio can display the database objects that belong to a Postgres Plus Advanced Server database. To open Postgres Studio, navigate through the Start menu to the Postgres Plus Advanced Server 8.4 menu, and choose Postgres Studio. Right click on the Postgres Plus Advanced Server 8.4 node (in the tree control), and choose Connect from the drop-down menu. After connecting to
Copyright 2010 EnterpriseDB Corporation. All rights reserved. 3

How to use PostGIS with Postgres Plus Advanced Server

Advanced Server, use the tree control to navigate through the Databases node to the template_postgis node; you should see 780 functions, 2 tables and 2 Trigger Functions beneath the template_postgis database public schema.

Copying the Template Database


The examples in this tutorial use a table (named roads) that stores geographical coordinates in the roadmaps database. You can use the CREATE DATABASE command at the EDB_PSQL command line to make a copy of the template_postgis database. To open the command line, navigate through the Start menu to Postgres Plus Advanced Studio, select Run SQL Command Line from the menu and click EDB-PSQL. At the EDB_PSQL command line enter:
CREATE DATABASE roadmaps TEMPLATE=template_postgis;

The new database (roadmaps) will contain all of the PostGIS types and functions that were installed in template_postgis.

Copyright 2010 EnterpriseDB Corporation. All rights reserved.

How to use PostGIS with Postgres Plus Advanced Server You can connect to the new database with the command:
\c roadmaps;

View the objects owned by user enterprisedb in the roadmaps database by entering the following command:
SELECT * FROM user_objects;

Creating Spatial Tables and Indexes


You can use the EDB-PSQL command line to create spatial tables and indexes. The following example creates a table named roads that will hold sample GIS data of the type, GEOMETRY. Creating a table that contains spatial data is a two-step process. First, create the table, omitting the columns that will hold the GIS data; then add the GEOMETRY columns with the PostGIS AddGeometryColumn() function.
CREATE TABLE roads (id INT4, name VARCHAR(128)); SELECT AddGeometryColumn('roads', 'geom', -1, 'GEOMETRY', 2);

Postgres Plus Advanced Server confirms that the table has been created, and contains a column of type GEOMETRY:

Copyright 2010 EnterpriseDB Corporation. All rights reserved.

How to use PostGIS with Postgres Plus Advanced Server

A GEOMETRY column can hold any type of GIS data; you must 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:
CREATE INDEX roads_index ON roads USING GIST(geom);

Now, optimize the roads table and collect performance statistics with the Postgres VACUUM command:

Copyright 2010 EnterpriseDB Corporation. All rights reserved.

How to use PostGIS with Postgres Plus Advanced Server


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; Advanced Server requires a table lock to perform the task. Adding the VERBOSE parameter to the command line instructs Advanced 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.

Viewing PostGIS Data


You can view the data in the roads table with the PostGIS AsText() function; without the AsText() function, the GEOMETRY data is returned in binary format. Use the command:

Copyright 2010 EnterpriseDB Corporation. All rights reserved.

How to use PostGIS with Postgres Plus Advanced Server


SELECT id, AsText(geom) AS geom, name FROM roads ORDER BY id;

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

Advanced Server returns a result of:

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.:
SELECT id, name FROM roads WHERE geom ~= GeomFromText(LINESTRING(0 10,0 0), -1) ORDER BY id;

Advanced Server returns a result of:


Copyright 2010 EnterpriseDB Corporation. All rights reserved. 8

How to use PostGIS with Postgres Plus Advanced Server

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 Advanced Server database. For additional information about using PostGIS please visit: http://postgis.refractions.net.

Conclusion
In this Tutorial, we demonstrated how to set up and use a PostGIS database in Postgres Plus Advanced Server.

Copyright 2010 EnterpriseDB Corporation. All rights reserved.

How to use PostGIS with Postgres Plus Advanced Server

If you havent checked out EnterpriseDBs Evaluation Guide, please do so; it may help you move onto the next step in your Postgres Plus evaluation. The guide can be accessed at: http://www.enterprisedb.com/learning/whitepapers.do You should now be able to proceed with a technical evaluation of Postgres Plus Advanced Server. The following resources should help you move on with this step: Postgres Plus getting started resources Postgres Plus tutorials Postgres Plus forums Postgres Plus documentation Postgres Plus webinars

Copyright 2010 EnterpriseDB Corporation. All rights reserved.

10

You might also like