100% found this document useful (1 vote)
99 views11 pages

Tutorial All PPSS PostGIS

Tutorial All PPSS PostGIS

Uploaded by

shareeff3779
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
100% found this document useful (1 vote)
99 views11 pages

Tutorial All PPSS PostGIS

Tutorial All PPSS PostGIS

Uploaded by

shareeff3779
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/ 11

How to use PostGIS with Postgres Plus Standard Server

How to Setup and Use PostGIS with


Postgres Plus(R) Standard Server
A Postgres Evaluation Quick Tutorial
From EnterpriseDB
December 22, 2009

EnterpriseDB Corporation, 235 Littleton Road, Westford, MA 01866, USA


T +1 978 589 5700 F +1 978 589 5701 E [email protected] www.enterprisedb.com

Copyright 2009 EnterpriseDB Corporation. All rights reserved.

How to use PostGIS with Postgres Plus Standard Server

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:

install and set up a PostGIS enabled environment

deploy a PostGIS database

view PostGIS database objects in pgAdmin

create spatial tables and indexes

insert test data into your tables and view the data

apply spatial functions against your PostGIS 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.

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

Copyright 2009 EnterpriseDB Corporation. All rights reserved.

How to use PostGIS with Postgres Plus Standard Server


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

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:

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

How to use PostGIS with Postgres Plus Standard Server


When the Stack Builder Plus wizard opens, choose your installation of Standard Server
from the drop down listbox and click Next to continue.
The StackBuilder Plus dialog opens, displaying the Categories tree control (shown
below):

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:

copy the PostGIS files into place.


restart the server (on Linux).
execute the SQL files that install the PostGIS functions.

Getting started with a PostGIS database


Before creating a new PostGIS database, create a superuser to manage the database.
Open the Start menu and navigate to the Postgres Plus Standard Server
Copyright 2009 EnterpriseDB Corporation. All rights reserved.

How to use PostGIS with Postgres Plus Standard Server


menu and choose SQL Shell (psql) to open the psql command line. After
connecting to Standard Server, use the following command to create a role named
gisadmin, with a password of mypassword:
CREATE ROLE gisadmin PASSWORD 'mypassword'
LOGIN SUPERUSER
INHERIT CREATEDB;

Postgres Plus confirms that the new role has been created:

The options that follow the CREATE ROLE command grant privileges to the new role:

LOGIN gives the new role the right to login.


CREATEDB grants the new role the right to create new databases.
SUPERUSER grants the new role superuser privileges.
INHERIT specifies that the new role inherits any privileges assigned to

any role it is a member of.


The following command creates a Standard Server database named roadmaps,
owned by the user, gisadmin. At the psql command line, enter:
CREATE DATABASE roadmaps
WITH OWNER = gisadmin
TEMPLATE = template_postgis;

Postgres Plus confirms that the new database has been created:

Viewing PostGIS Database Objects


Use the pgAdmin utility to view the database objects that belong to a Postgres Plus
Standard Server database. To open pgAdmin, open the Start menu, navigate to the
Postgres Plus Standard Server menu and choose pgAdmin from the pullCopyright 2009 EnterpriseDB Corporation. All rights reserved.

How to use PostGIS with Postgres Plus Standard Server


beside menu. If you start pgAdmin and navigate to the roadmaps node, you should
now see 685 functions, 2 tables and 3 Trigger Functions beneath the roadmaps database
public schema.

Creating Spatial Tables and Indexes


The following example creates a table named roads that will hold sample GIS data of
the type, GEOMETRY. To create the roads table in the roadmaps database, change your
username and database with the command:
\c roadmaps gisadmin;

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.

How to use PostGIS with Postgres Plus Standard Server


SELECT AddGeometryColumn('roads', 'geom', -1, 'GEOMETRY',
2);

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.

How to use PostGIS with Postgres Plus Standard Server


CREATE INDEX roads_index ON roads USING GIST(geom);

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.

Viewing PostGIS Data


You can view the data in the roads table with the PostGIS AsText() function; without
Copyright 2009 EnterpriseDB Corporation. All rights reserved.

How to use PostGIS with Postgres Plus Standard Server


the AsText() function, the GEOMETRY data is returned in binary format. Use the
command:
SELECT id, AsText(geom) AS geom, name FROM roads
ORDER BY id;

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;

Postgres Plus Standard 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.:

Copyright 2009 EnterpriseDB Corporation. All rights reserved.

How to use PostGIS with Postgres Plus Standard 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 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

How to use PostGIS with Postgres Plus Standard 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 Standard
Server.
The following resources should help you move on with this step:

Postgres Plus Technical Evaluation Guide


Postgres Plus Getting Started resources
Postgres Plus Quick Tutorials
Postgres Plus User Forums
Postgres Plus Documentation
Postgres Plus Webinars

Copyright 2009 EnterpriseDB Corporation. All rights reserved.

11

You might also like