Documente Academic
Documente Profesional
Documente Cultură
I. Getting Started
Lot of stuff is going to come here
I.1. How to start a postgis server?
pg_ctl is a utility to start, stop, restart, reload configuration files, or report the
status of a PostgreSQL server.
To get help on pg_ctl, type the following command at your unix prompt
o % pg_ctl help
o The contents of pg_ctl help are shown below
postgres@flounder (~) pg_ctl --help
pg_ctl is a utility to start, stop, restart, reload configuration files,
or report the status of a PostgreSQL server.
Usage:
pg_ctl start
pg_ctl stop
pg_ctl restart
pg_ctl reload
pg_ctl status
Common options:
-D DATADIR Location of the database storage area
-s
Only print errors, no informational messages
-w
Wait until operation completes
-W
Do not wait until operation completes
(The default is to wait for shutdown, but not for start or restart.)
If the -D option is omitted, the environment variable PGDATA is used.
Options for start or restart:
-l FILENAME
Write (or append) server log to FILENAME. The
use of this option is highly recommended.
-o OPTIONS
Command line options to pass to the postmaster
(PostgreSQL server executable)
-p PATH-TO-POSTMASTER
Normally not necessary
Options for stop or restart:
-m SHUTDOWN-MODE
By default, a database with the same name as the current user is created.
with various GIS database layers. Before that let us get familiarize
ourselves with simple SQL commands.
I.3. How to connect to the new database and use SQL?
Any database can be accessed is several ways. For now let us start with interactive
session.
Now we are connected to the county database and ready to do some serious
business with the database.
o Now one must be wondering what this database contains? So how do
we find out whats in there, or in the first place is there anything in this
new database?
o Let us start with typing \? at the county=# prompt
o The output is provided in the following table. Did you see any
interesting commands? Now its time to explore what some of these
commands do?
country=# \?
\a
toggle between unaligned and aligned output mode
\c[onnect] [DBNAME|- [USER]] connect to new database (currently "country")
\C TITLE
set table title
\cd [DIRNAME]
change the current working directory
\copy ...
perform SQL COPY with data stream to the client host
\copyright
show PostgreSQL usage and distribution terms
\d TABLE
describe table (or view, index, sequence)
\d{t|i|s|v}...
list tables/indexes/sequences/views
\d{p|S|l}
list access privileges, system tables, or large objects
\da
list aggregate functions
\dd NAME
show comment for table, type, function, or operator
\df
list functions
\do
list operators
\dT
list data types
\e FILENAME
edit the current query buffer or file with external editor
\echo TEXT
write text to standard output
\encoding ENCODING
set client encoding
\f STRING
set field separator
\g FILENAME
send SQL command to server (and write results to file or |pipe)
\h NAME
help on syntax of SQL commands, * for all commands
\H
toggle HTML output mode (currently off)
\i FILENAME
execute commands from file
\l
list all databases
\lo_export, \lo_import, \lo_list, \lo_unlink
large object operations
\o FILENAME
send all query results to file or |pipe
\p
show the content of the current query buffer
\pset VAR
set table output option (VAR := {format|border|expanded|
fieldsep|null|recordsep|tuples_only|title|tableattr|pager})
\q
quit psql
\qecho TEXT
write text to query output stream (see \o)
\r
reset (clear) the query buffer
\s FILENAME
print history or save it to file
\set NAME VALUE
set internal variable
\t
show only rows (currently on)
\T TEXT
set HTML table tag attributes
\unset NAME
unset (delete) internal variable
\w FILENAME
write current query buffer to file
\x
toggle expanded output (currently off)
\z
list table access privileges
\! [COMMAND]
execute command in shell or start interactive shell
OpenGIS Functions
(http://postgis.refractions.net/docs/c664.html#AEN667)
ExteriorRing(geometry)
Return the exterior ring of the first polygon in the geometry. Return
NULL if there is no polygon in the geometry.
NumInteriorRings(geometry)
Return the number of interior rings of the first polygon in the
geometry. Return NULL if there is no polygon in the geometry.
InteriorRingN(geometry,integer)
Return the N'th interior ring of the first polygon in the geometry.
Return NULL if there is no polygon in the geometry.
IsClosed(geometry)
Returns true of the geometry start and end points are coincident.
NumGeometries(geometry)
If geometry is a GEOMETRYCOLLECTION return the number of
geometries, otherwise return NULL.
GeometryN(geometry,int)
Return the N'th geometry if the geometry is a
GEOMETRYCOLLECTION, MULTIPOINT, MULTILINESTRING or
MULTIPOLYGON. Otherwise, return NULL.
Distance(geometry,geometry)
Return the cartesian distance between two geometries in projected
units.
AsText(geometry)
Return the Well-Known Text representation of the geometry. For
example: POLYGON(0 0,0 1,1 1,1 0,0 0)
SRID(geometry)
Returns the integer SRID number of the spatial reference system of
the geometry.
GeometryFromText(varchar, integer)
Syntax: GeometryFromText(<geometry>,<SRID>) Convert a WellKnown Text representation of a geometry into a geometry object.
GeomFromText(varchar, integer)
As above. A synonym for GeometryFromText.
SetSRID(geometry)
Set the SRID on a geometry to a particular integer value. Useful in
constructing bounding boxes for queries.
EndPoint(geometry)
Returns the last point of the geometry as a point.
StartPoint(geometry)
Returns the first point of the geometry as a point.
Centroid(geometry)
Returns the centroid of the geometry as a point.
| not null
| not null
| not null
| not null
| not null
| not null
| not null
|
|
|
| countries
| geom
2 | -1 | GEOMETRY | 429823 |
3|
(WKT) format for describing geometries. Here is an example of how to insert various
geometry types into our countries table.
country=# \d countries
id
| integer
name
| character varying(20)
geom
| geometry
| not null
| not null
|
// Now let us insert a point (assuming a country can be represented as point at certain
scale)
country=# insert into countries (id, name, geom)
values (1, 'Sri Lanka', GeometryFromText('POINT(30 50)', -1));
INSERT 429830 1
// Note that the GeometryFromText function takes a WKT string and spatial reference
id (SRID) as input and returns a geometry type.
// Now let us insert a polygon (assuming a country can be represented as a polygon at
the same scale).
country=# insert into countries (id, name, geom)
values (2, 'India', GeometryFromText('POLYGON (
(25 70, 20 65, 30 55, 20 65, 25 70)
)', -1)
);
INSERT 429831 1
Before importing these shape files, first create a fresh database using the
procedure described above. Name the database as your group name (for
example, sdblab#). Once the database is created, we can directly import the
external data into database (called bulkload) using the appropriate utility, in
our case it is called shp2pgsql. Here is the help on shp2pgsql:
postgres@flounder (~) pg_ctl start -D /data2/pgsql/data/ -l tmplog
postmaster successfully started
// create database using the above described procedure
..
..
// import shape files into the newly created database
postgres@flounder (~) shp2pgsql -s 4326 country conuntrytbl sdblab | psql sdblab
CREATE
addgeometrycolumn
----------------------------------------------------------------------------------------------Geometry column the_geom added to table conuntrytbl WITH a SRID of 4326
and type MULTIPOLYGON
(1 row)
BEGIN
INSERT 477366 1
INSERT 477368 1
..
..
COMMIT
postgres@flounder (~)
Now import rest of the shape files (cities, mjroads, and mjwater) into your
sdblab# database (replace # with your group number).
Once the database is populated, we can do spatial queries through SQL. In the reminder
of this section we provide sample queries to get familiarize with the PostGIS. Please note
that a given query can be written in several ways in SQL, some times one is the better
than the other. Also query syntax will be different from one system to the other. However,
the semantics are same as long as the system conforms to OGCs simple feature
specification for SQL standard.
List all the countries and the country they belong to in the CITIESTBL table.
// First connect to the database (once till you quit)
postgres@flounder (/data2/sdb-data/eu-data) psql sdblab
Welcome to psql, the PostgreSQL interactive terminal.
Type:
sdblab=#
Now let us look at some spatial queries. Here are couple of examples:
1. Find area of all countries in the Europe database.
2. Find all neighboring states of Minnesota (use USA database).
3. Find all states that I35 passes through.
Correlate these queries with the similar examples provided in the text book.
sdblab=# select cntry_name, area(the_geom) from cbndtbl;
cntry_name
|
area
------------------------------+-----------------------Germany
|
45.872
France
|
64.348
sdblab=# select s2.state from states s1, states s2 where s1.state = Minnesota and
touches(s1.the_geom, s2.the_geom);
State
--------Minnesota
North Dakota
Michigan
Wisconsin
South Dakota
Iowa
sdblab=# select s.state from states s, roads r where r.name = Interstate Route 35
and intersects(r.the_geom, s.the_geom);
State
-------Minnesota
Iowa
Missouri
Kansas
Oklahoma
Texas