You are on page 1of 14

Introduction to the

Dimensionally Extended 9 Intersection Model (DE-9IM)


in PostgreSQL/PostIS
!utorial
Germn Carrillo
g carrillo @ uni-muenster .de
geotux_tuxman@linuxmail.org
"#$ecti%es
Following this tutorial you will be able to:
Create spatial databases
Create spatial tables (alanumeric tables ! geometry"
Chec# topological relations on geometries
$un spatial operators on geometries (spatial analysis"
Create spatial tables rom spatial operators
&e'uirements
%ata &ase 'anager (ystem (%&'(" )ostgre(*+ and its spatial extension )ostG,( installed.
)ostgre(*+ client pg-dmin installed.
.dit permissions on )ostgre(*+ (to create and modiy database ob/ects".
0ptionally1 a pg-dmin2s plugin installed to display geometries. 3he plugin is called
4)ostG,( 5iewer6.
(*+ basic #nowledge.
Let(s start)
*+ ,onnecting -g.dmin to the ser%er
0pen pgAdmin3.
,n linux you can press -lt!F7 and write pgadmin8.
,n 9indows you can ind it in the )ostgre(*+ older.
)g-dmin can act as client or a number o )ostgre(*+ ser5ers at the same time. -t least you
need one ser5er1 it may be your own ser5er (i )ostgre(*+ is installed in your computer" or the
ser5er pre5iously conigured or the tutorial (as# or the connection details".
:ou may need to add a ser5er:
Germn Carrillo1 7;<<.
3he %&'( has a superuser1 which is a role with all the permissions o5er the ser5er (so1 be careul
with it=". ,n the conigured ser5er1 the superuser is postgres and its password is postgres.
0nce the ser5er is added1 you can connect to it by double-clic#ing o5er its name in the tree on
the let.
:ou will get the databases1 tablespaces and roles (users" o that ser5er.
/+ ,reating your s-atial data#ase
$ight clic# on the item Databases(2) and select New Database... :ou will get this window:
Fill the ields according to the igure. %on2t introduce spaces or non--(C,, characters.
Germn Carrillo1 7;<<.
Clic# on 0#.
, e5erything is o#1 you ha5e your own database in the ser5er. 3hat database (geodb_german) is
also a spatial database since you used a )ostG,( template database (template_postgis" to create
it.
0+ Introducing PostgreSQL data#ases
%atabases are core ob/ects in %&'( such as )ostgre(*+. 3hey allow us to store1 >uery and
manage 5ast amounts o related data. For e5ery ob/ect in your data model you may create a
table in )ostgre(*+.
3o organi?e the data1 )ostgre(*+ uses Schemas, which are data containers such as the
directories in your computer2s ile system. 3he dierence is that (chemas can not be nested1 i.e.
you can not create (chemas inside (chemas.
)ostgre(*+ uses a deault (chema called public1 where all the database ob/ects1 such as tables1
will be created (you can change that by creating your own schemas".
3hus1 we will be wor#ing on the public schema o your database.
1+ Introducing PostIS
)ostgre(*+ is not intended to wor# with spatial ob/ects. ,t actually can wor# with basic
geometries but it does not understand things li#e (patial $eerence (ystems (($(".
)ostG,( is a spatial extension to )ostgre(*+ allowing it to deal with a wide 5ariety o geometry
types (more than )oints1 +ines and )olygons"1 understand ($( and pro5ide unctions to >uery1
combine and modiy geometries.
3he way )ostG,( stores geometries and ($(1 and names unctions is gi5en by the Simple Feature
Access or S!" technical speciication rom the 0pen Geospatial Consortium (0GC".
Go to the public schema and explore its tables. 3hose are tables created by )ostG,( to store
geometries and ($(.
Germn Carrillo1 7;<<.
Go to the unctions section in the public schema. 3hose are )ostG,( unctions to create1 drop1
edit1 >uery1 process1 and relate geometries. -re you amiliar with some o those unctions@
2+ ,reating ta#les
9e will use pg-dmin to create a table by means o a Graphical Aser ,nterace (you could also
create it using purely (*+ instructions".
$ight clic# on the #ables (2) tree item and select $reate #able... :ou will get this window (only
clic# on the %& button when it is indicated":
Fill the 'roperties tab as you see in the igure1 the table will be called points_table. Bow let2s
deine the columns (ields" o the table. 3o do so1 clic# on the $olumns tab and Add a new
column1 this way:
Germn Carrillo1 7;<<.
-dd another column to the table and name it attribute1 with %ata type character and lenght 8.
Bow we need to set a ield as )$,'-$: C.:1 which acts as a uni>ue code to distinguish the
registers o the table (rows" and access them. - table with no )$,'-$: C.: can not be edited.
Clic# on the $onstraints tab. 9hen you add a primary #ey you need to speciy the name o the
constraint1 name it p(_points_table)
Clic# on the $olumns tab and add the gid column. :ou get something li#e this:
Germn Carrillo1 7;<<.
Clic# on %( to conirm the constraints and clic# on %( to conirm the creation o the table.
Bow you ha5e a table called points_table in your database.
Dust or your inormation1 the e>ui5alent set o (*+ instructions (to create the same table with
the same ield deinition" would be:
CREATE TABLE points_table (
gid serial PRIMARY KEY,
attribute char(3)
);
9hich way do you preer (GA, or (*+"@
9e ha5e almost inished1 but as you may ha5e noticed1 we ha5e no spatial capabilities on the
table. 3o achie5e this1 we need to add a new column to store the geometry. 9e will see how to
do it in the next step o the tutorial.
3+ .dding s-atial ca-a#ilities to the ta#le
3o add geometry columns we can use a 'ost*+S unction called Add*eometr,$olumn(". 3ry to
ind it in the unctions list and identiy the arguments o such a unction.
$ight1 there are three unctions on the list called Add*eometr,$olumn(". 3he dierence is the
set o arguments they recei5e. -s we are wor#ing on the public schema1 we can use the unction
that recei5es the smallest number o arguments. 3he arguments o that unction are:
ta#le4name: Bame o the table you want to modiy by including the geometry column.
column4name: Bame o the column that will hold the geometry. Asually called geom.
srid: .)(G code o the ($( (Get a complete list o ($( descriptions at
http:EEspatialreerence.org". Find the .)(G code o the 9G(FG ($(.
ty-e: Geometry type o the table. )0,B31 +,B.(3$,BG or )0+:G0B1 among others.
dimension: %imension o the ($( used (number o coordinates you use to deine a point".
-s we are going to wor# with +atitude and +ongitude1 the dimension will be 7.
For a better comprehension o these arguments1 you can chec# the Simple Feature Access or
S!" speciication1 it has been gi5en to you in the same ?ip ile o this tutorial.
Germn Carrillo1 7;<<.
3o run the )ostG,( unctions you need the (*+ *uery .ditor. Clic# on the button which loo#s li#e
this:
,n the (*+ .ditor &ox type:
SELECT AddGeomer!Col"m#($points_table$,$geom$,4326,$P%I&T$,2);
-nd run it by pressing 52.
, e5erything went ine1 your table has spatial capabilities1 i.e. it can store geographic data.
Chec# the geometr,_columns table1 now it includes your table as a geometry table
(geometr,_columns is a metadata table1 it tells you which o the database tables ha5e spatial
capabilities".

6+ .dding s-atial data to the ta#le
3o add geographic data to the table you can use a 5ariety o geometry ormats. 0ne o
them is -ell &nown #e.t (9C3"1 in which e5ery geometry is deined in a human-readable
way1 here are some examples:
'POINT(0 0)'
'LIN!T"IN#($ $% 2 2% 3 4)'
'POL&#ON((0 0% 0 $% $ $% $ 0% 0 0))'
-ccording to the geometry type you need to use a )ostG,( unction to read the
geometry: S#_'ointFrom#e.t1 S#_"ineFrom#e.t or S#_'ol,gonFrom#e.t1 among others.
-dd data to the table using the common (*+ instruction +NS/0# +N#%:

I&SERT I&T% points_table (geom%attribute) 'AL(ES
(!T_Point'romTe(t('POINT()$0 *)'%4326)%'Pt$')+
I&SERT I&T% points_table (geom%attribute) 'AL(ES
(!T_Point'romTe(t('POINT(* *)'%4326)%'Pt2')+
I&SERT I&T% points_table (geom%attribute) 'AL(ES
(!T_Point'romTe(t('POINT()20 20)'%4326)%'Pt3')+
Germn Carrillo1 7;<<.
7+ ,reate line and -olygon data
Following the steps to create the points_table1 create a lines_table and a pol,gons_table. :ou
need to create the gid (serial" and the attribute (character1 8" ield or the table. -lso add the
geometry ield by using the -ddGeometryColumn unction (be careul with the arguments you
pro5ide to the unction".
Bow add line and polygon data to your tables. 3he 9C3 representation or each geometry is:
89! eometry .ttri#ute
'LIN!T"IN#()$0 *% * *)'
2+i<2
'LIN!T"IN#()$0 )20% $0 )20% 20 2*% 40 $0% 40 )*)'
2+i72
'POL&#ON(()20 $0% $0 $0% $0 )20% )20 $0))'
2)o<2
'POL&#ON((0 0% 0 30% 30 0% 0 0))'
2)o72
:ou ha5e to construct the +NS/0# +N#% (*+ instructions to load the data to your line and polygon
tables.
0nce you ha5e inished this point you will be able to start using )ostG,( unctions to perorm
spatial analysis on your data.
9+ Dis-laying your s-atial data ("-tional ste-)
&asically you could use any thic# client (*uantum G,(1 Cosmo1 g5(,G1 u%ig1 among others" to
display your )ostG,( data. Howe5er1 we will use another approach1 a pg-dmin2s plugin.
:ou can display your spatial data in pg-dmin by using the 'ost*+S 1iewer plugin. :ou can ind the
installation instructions at:
http:EEgeotux.tuxamily.orgEindex.php@optionIcom_myblogJtas#I5iewJidI7KFJ,temidILMJlangIen
3o display a spatial table1 select it in the main tree and clic# on the plugin.
Germn Carrillo1 7;<<.
:ou will get this window:
3o add more spatial tables to the same map repeat the process (select the table and clic# on the
plugin" with each table.
3ry to identiy each point1 line and polygon you ha5e loaded and which attribute corresponds to
it. For instance1 according to the coordinates the point 2)t82 is in the upper let part o the map.
*:+ !o-ological -redicates (;rom DE-9IM)
3opological predicates are unctions to chec# topological relations between two geometries. -s
they chec# relations1 they /ust return boolean 5alues1 i.e. 3rue (t" or False (".
%.-M,' deines the ollowing topological predicates:
!o-ological Predicate Meaning
.>uals 3he Geometries are topologically e>ual
%is/oint 3he Geometries ha5e no point in common
,ntersects 3he Geometries ha5e at least one point in common (the in5erse o %is/oint"
3ouches 3he Geometries ha5e at least one boundary point in common1 but no
interior points
Crosses 3he Geometries share some but not all interior points1 and the dimension o
the intersection is less than that o at least one o the Geometries
05erlaps 3he Geometries share some but not all points in common1 and the
intersection has the same dimension as the Geometries themsel5es
9ithin Geometry - lies in the interior o Geometry &
Contains Geometry & lies in the interior o Geometry - (the in5erse o 9ithin"
Germn Carrillo1 7;<<.
+et2s run one o the unctions:
SELECT p,attribute% l,attribute% ST)I#ersecs(p,geom% l,geom)
*R%M points_table p% lines_table l+
3he >uery extracts the attribute ield o two tables (points_table and lines_table) as well as
whether their geometries are related through an intersection. -ter pressing FL you will get:
3ry to iner what would be the result o the #ouches predicate o5er the pol,gons_table and the
lines_table. Bow build a >uery to ind the answer. :ou should get something li#e this:

Can you apply the %2erlaps predicate on the two polygons rom the pol,gons_table@ :ou should
get they o5erlap.
Bow apply the %2erlaps predicate on the two lines o the lines_table.
*:+a+ !he DE-9IM matrix
-ccording to the Simple Feature Access or S!" speciication: Nthe basic approach to comparing
two geometries is to ma#e pair-wise tests o the intersections #et<een the Interiors=
>oundaries and Exteriors o; the t<o geometries and to classiy the relationship between the
two geometries based on the entries in the resulting 2intersection2 matrix.N 3he 2intersection
matrix2 is #nown as %.-M,' matrix.
3here is one )ostG,( unction that returns the %.-M,' matrix: S#_0elate.
SELECT p,attribute% l,attribute% ST)Relae(p,geom% l,geom)
*R%M points_table p% lines_table l+
3he result is:
Germn Carrillo1 7;<<.
3he irst row1 which is the relation between the point < ()t<" and the line < (+i<"1 can be read
as:
Li1
I l Bl El
Pt1
I p
B p
E p
[
F 0 F
F F F
1 0 2
]
9here +() is the interior1 3() the boundary and /() the exterior1 and p and l are the point and the
line respecti5ely. &oundary1 interior and exterior are deined as ollows ((trobl1 7;;K":
>oundary o a geometry ob/ect: ,s a set o geometries o the next lower dimension.
Interior o a geometry ob/ect? Consists o those points that are let (inside" when the boundary
points are remo5ed.
Exterior o a geometry ob/ect: Consists o points not in the interior or boundary.
3he characters returned by the unction (3_$elate may ha5e the ollowing 5alues:
: : )oint (3he intersection has a dimension ;"
* : +ine
/ : -rea
! : O;1<17P (3he intersection may ha5e a dimension ;1 < or 7"
5 : OQP (3he intersection is an empty set"
@ : 4%on2t care6
3hus1 the F;FFFF<;7 relation is telling us that the point < is one o the endpoints o the line<
(loo# at the irst row and the second column o the matrix." Bote that the point 7 has the same
relation with the line < since it is the other endpoint.
:ou can get the %.-M,' matrix or e5ery pair o geometries that you pro5ide in a (.+.C3 >uery.
**+ S-atial o-erators (etting ne< geometries)
(patial operators are unctions that construct geometries rom other geometries.
-ccording to the Simple Feature Access or S!" speciication1 these are: ,ntersection1
%ierence1 Anion1 (ymetrical %ierence1 &uer and Con5ex Hull.
5unction Descri-tion
Intersection
(g< Geometry1 g7 Geometry"
return a geometric ob/ect that is the intersection o
geometric ob/ects g< and g7
Di;;erence
(g< Geometry1 g7 Geometry"
return a geometric ob/ect that is the closure o the set
dierence o g< and g7
Anion return a geometric ob/ect that is the set union o g< and
Germn Carrillo1 7;<<.
(g< Geometry1 g7 Geometry" g7
SymDi;;erence
(g< Geometry1 g7 Geometry"
return a geometric ob/ect that is the closure o the set
symmetric dierence o g< and g7 (logical R0$ o space"
>u;;er
(g< Geometry1 d %ouble
)recision"
return a geometric ob/ect deined by buering a distance
d around g<1 where d is in the distance units or the
(patial $eerence o g<
,on%exBull
(g< Geometry"
return a geometric ob/ect that is the con5ex hull o g<
+et2s apply the 4nion operator o5er the polygons:
SELECT p$,gid% p$,attribute as att$% p2,attribute as att2%
ST)(#io#(p$,geom% p2,geom)
*R%M pol-gons_table p$% pol-gons_table p2
+,ERE p$,gid . p2,gid+
-s you can see1 you are getting new geometries but you see them as binary data1 actually1 as
9ell Cnown &inary (9C&".
3o display these geometries we need to create a table1 but wait= 9e can create tables rom
S/"/$# statements1 so it will not ta#e too much time.
*/+ ,reating s-atial ta#les ;rom s-atial o-erators
9e can create tables rom a (.+.C3 statement using the word -( (note that this is /ust or
displaying purposes as we are not deining any primary #ey":
CREATE TABLE pol-gons_union AS
SELECT p$,gid% p$,attribute as att$% p2,attribute as att2%
ST)(#io#(p$,geom% p2,geom)
*R%M pol-gons_table p$% pol-gons_table p2
+,ERE p$,gid . p2,gid+
3his should loo# li#e this (loo# at the polygon":
Germn Carrillo1 7;<<.
- buer example rom the points:
CREATE TABLE points_bu//er AS
SELECT p,gid% p,attribute% ST)B"--er(p,geom% $0)
*R%M points_table p+
*0+ Asing to-ological -redicates in 8BE&E clauses
9e ha5e seen one o the lines touches the pol,gons_table. 9hat i you need a buer around
that speciic line@ :ou would li#e to ilter somehow your lines beore applying the buer.
9e can use topological predicates as a constraint to apply spatial operators.
CREATE TABLE line_bu//er AS
SELECT l,gid% l,attribute% ST)B"--er(l,geom% *)
*R%M lines_table l% pol-gons_table p
+,ERE ST)To"ches(l,geom% p,geom)+
3his is the result (yellow polygon":
Germn Carrillo1 7;<<.
:ou could also use the unction S#_0elate to ilter a >uery by means o the 9H.$. clause. Asing
the same example as the step <;.a.1 to apply a buer around the line that holds the relation
F;FFFF<;7 with the points o the point_table you would write:
CREATE TABLE line$_bu//er AS
SELECT l,gid% l,attribute as line% p,attribute as point% ST)B"--er(l,geom% *)
*R%M points_table p% lines_table l
+,ERE ST)Relae(p,geom% l,geom% ''0''''$02')+
-nd this is what you get (red polygon":
!hat(s it)
&e;erences
$amsey1 )aul. #ips or power users. -5ailable at
http:EE7;<;.ossGg.orgEpresentations_show.php@idI88SM
$amsey1 )aul. +ntroduction to 'ost*+S, +nstallation 5 #utorial 5 /.ercises. $eractions
$esearch. Canada1 7;;K.
'ost*+S manual. -5ailable at http:EEpostgis.reractions.netEdocumentationEmanual-
<.L(TBEch;G.htmlU%.-M,'
0pen Geospatial Consortium (0GC". Simple Feature Access or S!". 7;;L.
(trobl1 Christian. Dimensionall, /.tended Nine6+ntersection 7odel (D/68+7). 7;;K.
Germn Carrillo1 7;<<.

You might also like