Hex Artifact Content
Not logged in

Artifact 111e279bced74dfbf866726ee847a2534ca6d0fc:

Wiki page [about DE-9IM] by sandro 2017-04-18 13:00:31.
0000: 44 20 32 30 31 37 2d 30 34 2d 31 38 54 31 33 3a  D 2017-04-18T13:
0010: 30 30 3a 33 31 2e 34 33 31 0a 4c 20 61 62 6f 75  00:31.431.L abou
0020: 74 5c 73 44 45 2d 39 49 4d 0a 50 20 64 32 38 34  t\sDE-9IM.P d284
0030: 31 37 61 61 64 39 62 66 63 34 61 64 34 39 66 35  17aad9bfc4ad49f5
0040: 32 34 33 39 37 64 35 38 30 33 34 66 31 63 37 64  24397d58034f1c7d
0050: 61 66 34 32 0a 55 20 73 61 6e 64 72 6f 0a 57 20  af42.U sandro.W 
0060: 38 35 38 38 0a 3c 68 32 3e 43 6f 72 72 65 73 70  8588.<h2>Corresp
0070: 6f 6e 64 65 6e 63 65 73 20 62 65 74 77 65 65 6e  ondences between
0080: 20 53 51 4c 20 46 75 6e 63 74 69 6f 6e 73 20 61   SQL Functions a
0090: 6e 64 20 44 45 2d 39 49 4d 20 49 6e 74 65 72 73  nd DE-9IM Inters
00a0: 65 63 74 69 6f 6e 20 4d 61 74 72 69 63 65 73 3c  ection Matrices<
00b0: 2f 68 32 3e 0d 0a 51 75 69 63 6b 20 72 65 63 61  /h2>..Quick reca
00c0: 6c 6c 3a 0d 0a 3c 6f 6c 3e 0d 0a 3c 6c 69 3e 54  ll:..<ol>..<li>T
00d0: 68 65 20 73 74 61 6e 64 61 72 64 20 53 70 61 74  he standard Spat
00e0: 69 61 6c 20 53 51 4c 20 6d 6f 64 65 6c 20 64 65  ial SQL model de
00f0: 66 69 6e 65 73 20 73 65 76 65 72 61 6c 20 66 75  fines several fu
0100: 6e 63 74 69 6f 6e 73 20 28 61 73 20 65 2e 67 2e  nctions (as e.g.
0110: 20 3c 62 3e 53 54 5f 45 71 75 61 6c 73 28 29 3c   <b>ST_Equals()<
0120: 2f 62 3e 2c 20 3c 62 3e 53 54 5f 49 6e 74 65 72  /b>, <b>ST_Inter
0130: 73 65 63 74 73 28 29 3c 2f 62 3e 2c 20 3c 62 3e  sects()</b>, <b>
0140: 53 54 5f 54 6f 75 63 68 65 73 28 29 3c 2f 62 3e  ST_Touches()</b>
0150: 20 61 6e 64 20 61 6c 69 6b 65 29 20 69 6e 74 65   and alike) inte
0160: 6e 64 65 64 20 74 6f 20 74 65 73 74 20 69 66 20  nded to test if 
0170: 61 20 67 69 76 65 6e 20 73 70 61 74 69 61 6c 20  a given spatial 
0180: 72 65 6c 61 74 69 6f 6e 73 68 69 70 20 65 78 69  relationship exi
0190: 73 74 73 20 62 65 74 77 65 65 6e 20 61 20 70 61  sts between a pa
01a0: 69 72 20 6f 66 20 47 65 6f 6d 65 74 72 69 65 73  ir of Geometries
01b0: 2e 3c 2f 6c 69 3e 0d 0a 3c 6c 69 3e 41 6c 6c 20  .</li>..<li>All 
01c0: 73 75 63 68 20 53 51 4c 20 66 75 6e 63 74 69 6f  such SQL functio
01d0: 6e 73 20 61 72 65 20 73 69 6d 70 6c 79 20 63 6f  ns are simply co
01e0: 6e 76 65 6e 69 65 6e 63 65 20 77 72 61 70 70 65  nvenience wrappe
01f0: 72 73 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20  rs based on the 
0200: 6d 6f 73 74 20 67 65 6e 65 72 61 6c 20 44 45 2d  most general DE-
0210: 39 49 4d 20 6d 6f 64 65 6c 2e 3c 2f 6c 69 3e 0d  9IM model.</li>.
0220: 0a 3c 6c 69 3e 41 6e 20 61 6c 74 65 72 6e 61 74  .<li>An alternat
0230: 69 76 65 20 28 6d 6f 72 65 20 66 6c 65 78 69 62  ive (more flexib
0240: 6c 65 2c 20 6d 6f 72 65 20 67 65 6e 65 72 69 63  le, more generic
0250: 20 61 6e 64 20 6d 6f 72 65 20 65 66 66 69 63 69   and more effici
0260: 65 6e 74 29 20 61 70 70 72 6f 61 63 68 20 66 6f  ent) approach fo
0270: 72 20 65 76 61 6c 75 61 74 69 6e 67 20 73 70 61  r evaluating spa
0280: 74 69 61 6c 20 72 65 6c 61 74 69 6f 6e 73 68 69  tial relationshi
0290: 70 73 20 63 6f 75 6c 64 20 62 65 20 62 61 73 65  ps could be base
02a0: 64 20 6f 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69  d on the followi
02b0: 6e 67 20 74 77 6f 20 3c 62 3e 3c 69 3e 61 64 76  ng two <b><i>adv
02c0: 61 6e 63 65 64 3c 2f 69 3e 3c 2f 62 3e 20 53 51  anced</i></b> SQ
02d0: 4c 20 66 75 6e 63 74 69 6f 6e 73 3a 0d 0a 3c 75  L functions:..<u
02e0: 6c 3e 0d 0a 3c 6c 69 3e 3c 62 3e 53 54 5f 52 65  l>..<li><b>ST_Re
02f0: 6c 61 74 65 20 28 3c 2f 62 3e 20 3c 69 3e 67 31  late (</b> <i>g1
0300: 3c 2f 69 3e 20 47 65 6f 6d 65 74 72 79 20 2c 20  </i> Geometry , 
0310: 3c 69 3e 67 32 3c 2f 69 3e 20 47 65 6f 6d 65 74  <i>g2</i> Geomet
0320: 72 79 20 3c 62 3e 29 20 3a 3c 2f 62 3e 20 3c 69  ry <b>) :</b> <i
0330: 3e 69 6e 74 65 72 73 65 63 74 69 6f 6e 5f 6d 61  >intersection_ma
0340: 74 72 69 78 3c 2f 69 3e 20 54 65 78 74 3c 62 72  trix</i> Text<br
0350: 3e 0d 0a 54 68 69 73 20 66 69 72 73 74 20 66 75  >..This first fu
0360: 6e 63 74 69 6f 6e 20 77 69 6c 6c 20 65 76 61 6c  nction will eval
0370: 75 61 74 65 20 62 6f 74 68 20 47 65 6f 6d 65 74  uate both Geomet
0380: 72 69 65 73 2c 20 74 68 65 6e 20 72 65 74 75 72  ries, then retur
0390: 6e 69 6e 67 20 61 20 54 65 78 74 20 73 74 72 69  ning a Text stri
03a0: 6e 67 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  ng corresponding
03b0: 20 74 6f 20 61 20 6e 69 6e 65 09 64 69 67 69 74   to a nine.digit
03c0: 73 20 73 65 72 69 61 6c 69 7a 65 64 20 44 45 2d  s serialized DE-
03d0: 39 49 4d 20 3c 62 3e 69 6e 74 65 72 73 65 63 74  9IM <b>intersect
03e0: 69 6f 6e 5f 6d 61 74 72 69 78 3c 2f 62 3e 20 61  ion_matrix</b> a
03f0: 73 20 65 2e 67 2e 20 3c 62 3e 27 30 46 31 46 46  s e.g. <b>'0F1FF
0400: 30 46 46 32 27 3c 2f 62 3e 20 6f 72 20 3c 62 3e  0FF2'</b> or <b>
0410: 27 32 31 32 31 30 31 32 31 32 27 3c 2f 62 3e 20  '212101212'</b> 
0420: 77 69 74 68 20 74 68 65 20 66 6f 6c 6c 6f 77 69  with the followi
0430: 6e 67 20 69 6e 74 65 72 70 72 65 74 61 74 69 6f  ng interpretatio
0440: 6e 3a 0d 0a 3c 75 6c 3e 0d 0a 3c 6c 69 3e 3c 62  n:..<ul>..<li><b
0450: 3e 46 3c 2f 62 3e 3a 20 74 68 69 73 20 73 74 61  >F</b>: this sta
0460: 6e 64 73 20 66 6f 72 20 3c 62 3e 46 41 4c 53 45  nds for <b>FALSE
0470: 3c 2f 62 3e 2c 20 69 2e 65 2e 20 74 68 65 20 63  </b>, i.e. the c
0480: 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 69 6e 74  orresponding int
0490: 65 72 73 65 63 74 69 6f 6e 20 64 6f 65 73 6e 27  ersection doesn'
04a0: 74 20 6f 63 63 75 72 73 2e 3c 2f 6c 69 3e 0d 0a  t occurs.</li>..
04b0: 3c 6c 69 3e 3c 62 3e 30 3c 2f 62 3e 3a 20 74 68  <li><b>0</b>: th
04c0: 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  e corresponding 
04d0: 69 6e 74 65 72 73 65 63 74 69 6f 6e 20 69 73 20  intersection is 
04e0: 61 20 50 6f 69 6e 74 2c 20 69 2e 65 2e 20 61 20  a Point, i.e. a 
04f0: 7a 65 72 6f 2d 64 69 6d 65 6e 73 69 6f 6e 73 20  zero-dimensions 
0500: 47 65 6f 6d 65 74 72 79 2e 3c 2f 6c 69 3e 0d 0a  Geometry.</li>..
0510: 3c 6c 69 3e 3c 62 3e 31 3c 2f 62 3e 3a 20 74 68  <li><b>1</b>: th
0520: 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  e corresponding 
0530: 69 6e 74 65 72 73 65 63 74 69 6f 6e 20 69 73 20  intersection is 
0540: 61 20 4c 69 6e 65 73 74 72 69 6e 67 2c 20 69 2e  a Linestring, i.
0550: 65 2e 20 61 20 20 6f 6e 65 2d 64 69 6d 65 6e 73  e. a  one-dimens
0560: 69 6f 6e 20 47 65 6f 6d 65 74 72 79 2e 3c 2f 6c  ion Geometry.</l
0570: 69 3e 0d 0a 3c 6c 69 3e 3c 62 3e 32 3c 2f 62 3e  i>..<li><b>2</b>
0580: 3a 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64  : the correspond
0590: 69 6e 67 20 69 6e 74 65 72 73 65 63 74 69 6f 6e  ing intersection
05a0: 20 69 73 20 61 20 50 6f 6c 79 67 6f 6e 2c 20 69   is a Polygon, i
05b0: 2e 65 2e 20 61 20 74 77 6f 2d 64 69 6d 65 6e 73  .e. a two-dimens
05c0: 69 6f 6e 73 20 47 65 6f 6d 65 74 72 79 2e 3c 2f  ions Geometry.</
05d0: 6c 69 3e 0d 0a 3c 2f 75 6c 3e 3c 2f 6c 69 3e 0d  li>..</ul></li>.
05e0: 0a 3c 6c 69 3e 3c 62 3e 53 54 5f 52 65 6c 61 74  .<li><b>ST_Relat
05f0: 65 4d 61 74 63 68 20 28 3c 2f 62 3e 20 3c 69 3e  eMatch (</b> <i>
0600: 69 6e 74 65 72 73 65 63 74 69 6f 6e 5f 6d 61 74  intersection_mat
0610: 72 69 78 3c 2f 69 3e 20 54 65 78 74 20 2c 20 3c  rix</i> Text , <
0620: 69 3e 72 65 66 65 72 65 6e 63 65 5f 70 61 74 74  i>reference_patt
0630: 65 72 6e 3c 2f 69 3e 20 54 65 78 74 20 3c 62 3e  ern</i> Text <b>
0640: 29 20 3a 3c 2f 62 3e 20 3c 69 3e 74 72 75 65 5f  ) :</b> <i>true_
0650: 6f 72 5f 66 61 6c 73 65 3c 2f 69 3e 20 42 6f 6f  or_false</i> Boo
0660: 6c 65 61 6e 3c 62 72 3e 0d 0a 54 68 69 73 20 73  lean<br>..This s
0670: 65 63 6f 6e 64 20 66 75 6e 63 74 69 6f 6e 20 69  econd function i
0680: 73 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 65 76  s intended to ev
0690: 61 6c 75 61 74 65 20 61 6e 20 3c 62 3e 69 6e 74  aluate an <b>int
06a0: 65 72 73 65 63 74 69 6f 6e 20 6d 61 74 72 69 78  ersection matrix
06b0: 3c 2f 62 3e 20 72 65 74 75 72 6e 65 64 20 62 79  </b> returned by
06c0: 20 61 20 70 72 65 76 69 6f 75 73 20 63 61 6c 6c   a previous call
06d0: 20 74 6f 20 3c 62 3e 53 54 5f 52 65 6c 61 74 65   to <b>ST_Relate
06e0: 28 29 3c 2f 62 3e 20 61 67 61 69 6e 73 74 20 61  ()</b> against a
06f0: 20 67 69 76 65 6e 20 3c 62 3e 72 65 66 65 72 65   given <b>refere
0700: 6e 63 65 5f 70 61 74 74 65 72 6e 3c 2f 62 3e 2c  nce_pattern</b>,
0710: 20 74 68 65 6e 20 72 65 74 75 72 6e 69 6e 67 20   then returning 
0720: 3c 62 3e 54 52 55 45 3c 2f 62 3e 20 6f 72 20 3c  <b>TRUE</b> or <
0730: 62 3e 46 41 4c 53 45 3c 2f 62 3e 2e 3c 62 72 3e  b>FALSE</b>.<br>
0740: 0d 0a 41 20 3c 62 3e 72 65 66 65 72 65 6e 63 65  ..A <b>reference
0750: 5f 70 61 74 74 65 72 6e 3c 2f 62 3e 20 61 73 20  _pattern</b> as 
0760: 65 2e 67 2e 20 3c 62 3e 27 54 2a 46 2a 2a 46 46  e.g. <b>'T*F**FF
0770: 46 2a 27 3c 2f 62 3e 20 6f 72 20 3c 62 3e 27 30  F*'</b> or <b>'0
0780: 2a 2a 2a 2a 2a 2a 2a 2a 27 3c 2f 62 3e 20 20 73  ********'</b>  s
0790: 69 6d 70 6c 79 20 69 73 20 79 65 74 20 61 6e 6f  imply is yet ano
07a0: 74 68 65 72 20 6e 69 6e 65 20 64 69 67 69 74 73  ther nine digits
07b0: 20 73 65 72 69 61 6c 69 7a 65 64 20 44 45 2d 39   serialized DE-9
07c0: 49 4d 20 6d 61 74 72 69 78 20 61 64 6f 70 74 69  IM matrix adopti
07d0: 6e 67 20 61 20 73 6c 69 67 68 74 6c 79 20 63 68  ng a slightly ch
07e0: 61 6e 67 65 64 20 73 79 6e 74 61 78 3a 0d 0a 3c  anged syntax:..<
07f0: 75 6c 3e 0d 0a 3c 6c 69 3e 3c 62 3e 46 3c 2f 62  ul>..<li><b>F</b
0800: 3e 2c 20 3c 62 3e 30 3c 2f 62 3e 2c 20 3c 62 3e  >, <b>0</b>, <b>
0810: 31 3c 2f 62 3e 20 61 6e 64 20 3c 62 3e 32 3c 2f  1</b> and <b>2</
0820: 62 3e 3a 20 73 61 6d 65 20 69 6e 74 65 72 70 72  b>: same interpr
0830: 65 74 61 74 69 6f 6e 20 61 73 20 62 65 66 6f 72  etation as befor
0840: 65 2e 3c 2f 6c 69 3e 0d 0a 3c 6c 69 3e 3c 62 3e  e.</li>..<li><b>
0850: 54 3c 2f 62 3e 3a 20 74 68 69 73 20 73 74 61 6e  T</b>: this stan
0860: 64 73 20 66 6f 72 20 3c 62 3e 54 52 55 45 3c 2f  ds for <b>TRUE</
0870: 62 3e 2c 20 69 2e 65 2e 20 74 68 65 20 63 6f 72  b>, i.e. the cor
0880: 72 65 73 70 6f 6e 64 69 6e 67 20 69 6e 74 65 72  responding inter
0890: 73 65 63 74 69 6f 6e 20 69 73 20 65 78 70 65 63  section is expec
08a0: 74 65 64 20 74 6f 20 6f 63 63 75 72 2c 20 62 75  ted to occur, bu
08b0: 74 20 69 74 73 20 73 70 65 63 69 66 69 63 20 6e  t its specific n
08c0: 61 74 75 72 65 20 64 6f 65 73 6e 27 74 20 6d 61  ature doesn't ma
08d0: 74 74 65 72 73 2e 3c 2f 6c 69 3e 0d 0a 3c 6c 69  tters.</li>..<li
08e0: 3e 3c 62 3e 2a 3c 2f 62 3e 3a 20 61 6e 20 61 73  ><b>*</b>: an as
08f0: 74 65 72 69 73 6b 20 69 73 20 61 20 3c 62 3e 6d  terisk is a <b>m
0900: 61 73 6b 20 73 69 67 6e 3c 2f 62 3e 20 61 6e 64  ask sign</b> and
0910: 20 61 6c 77 61 79 73 20 6d 65 61 6e 73 20 3c 62   always means <b
0920: 3e 3c 69 3e 69 67 6e 6f 72 65 20 2f 20 64 6f 6e  ><i>ignore / don
0930: 27 74 20 63 61 72 65 20 2f 20 73 6b 69 70 3c 2f  't care / skip</
0940: 69 3e 3c 2f 62 3e 20 74 68 69 73 20 6d 61 74 72  i></b> this matr
0950: 69 78 20 63 65 6c 6c 2e 3c 2f 6c 69 3e 0d 0a 3c  ix cell.</li>..<
0960: 2f 75 6c 3e 3c 2f 6c 69 3e 0d 0a 3c 6c 69 3e 3c  /ul></li>..<li><
0970: 75 3e 4e 6f 74 65 3c 2f 75 3e 3a 20 3c 62 3e 53  u>Note</u>: <b>S
0980: 54 5f 52 65 6c 61 74 65 28 29 3c 2f 62 3e 20 69  T_Relate()</b> i
0990: 73 20 61 20 63 6f 6d 70 75 74 61 74 69 6f 6e 61  s a computationa
09a0: 6c 6c 79 20 68 65 61 76 79 20 66 75 6e 63 74 69  lly heavy functi
09b0: 6f 6e 20 28 65 78 61 63 74 6c 79 20 20 61 73 20  on (exactly  as 
09c0: 3c 62 3e 53 54 5f 45 71 75 61 6c 73 28 29 3c 2f  <b>ST_Equals()</
09d0: 62 3e 2c 20 3c 62 3e 53 54 5f 43 72 6f 73 73 65  b>, <b>ST_Crosse
09e0: 73 28 29 3c 2f 62 3e 20 61 6e 64 20 61 6c 69 6b  s()</b> and alik
09f0: 65 20 61 72 65 29 3b 20 09 09 09 6f 6e 20 74 68  e are); ...on th
0a00: 65 20 6f 74 68 65 72 20 68 61 6e 64 20 3c 62 3e  e other hand <b>
0a10: 53 54 5f 52 65 6c 61 74 65 4d 61 74 63 68 28 29  ST_RelateMatch()
0a20: 3c 2f 62 3e 20 69 73 20 61 20 76 65 72 79 20 6c  </b> is a very l
0a30: 69 67 68 74 20 61 6e 64 20 71 75 69 63 6b 20 66  ight and quick f
0a40: 75 6e 63 74 69 6f 6e 2e 3c 62 72 3e 0d 0a 53 6f  unction.<br>..So
0a50: 20 77 68 65 6e 20 79 6f 75 20 68 61 76 65 20 74   when you have t
0a60: 6f 20 63 68 65 63 6b 20 66 6f 72 20 6d 6f 72 65  o check for more
0a70: 20 74 68 61 6e 20 61 20 73 69 6e 67 6c 65 20 72   than a single r
0a80: 65 6c 61 74 69 6f 6e 73 68 69 70 20 61 74 20 74  elationship at t
0a90: 68 65 20 73 61 6d 65 20 74 69 6d 65 2c 20 63 61  he same time, ca
0aa0: 6c 6c 69 6e 67 20 3c 62 3e 53 54 5f 52 65 6c 61  lling <b>ST_Rela
0ab0: 74 65 28 29 3c 2f 62 3e 20 6a 75 73 74 20 6f 6e  te()</b> just on
0ac0: 63 65 20 61 6e 64 20 74 68 65 6e 20 65 76 61 6c  ce and then eval
0ad0: 75 61 74 69 6e 67 20 65 61 63 68 20 73 70 61 74  uating each spat
0ae0: 69 61 6c 20 72 65 6c 61 74 69 6f 6e 73 68 69 70  ial relationship
0af0: 20 62 79 20 63 61 6c 6c 69 6e 67 20 3c 62 3e 53   by calling <b>S
0b00: 54 5f 52 65 6c 61 74 65 4d 61 74 63 68 28 29 3c  T_RelateMatch()<
0b10: 2f 62 3e 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d  /b> multiple tim
0b20: 65 73 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 65  es will always e
0b30: 6e 73 75 72 65 20 6e 6f 74 69 63 65 61 62 6c 79  nsure noticeably
0b40: 20 66 61 73 74 65 72 20 70 65 72 66 6f 72 6d 61   faster performa
0b50: 6e 63 65 73 2e 3c 2f 6c 69 3e 0d 0a 3c 6c 69 3e  nces.</li>..<li>
0b60: 3c 75 3e 43 6f 6e 63 6c 75 73 69 6f 6e 3c 2f 75  <u>Conclusion</u
0b70: 3e 3a 20 75 73 69 6e 67 20 74 68 65 20 71 75 69  >: using the qui
0b80: 72 6b 79 20 61 6e 64 20 61 77 6b 77 61 72 64 20  rky and awkward 
0b90: 44 45 2d 39 49 4d 20 6d 6f 64 65 6c 20 73 75 72  DE-9IM model sur
0ba0: 65 6c 79 20 69 73 20 6e 65 69 74 68 65 72 20 65  ely is neither e
0bb0: 61 73 79 20 6f 72 20 69 6e 74 75 69 74 69 76 65  asy or intuitive
0bc0: 2e 3c 62 72 3e 0d 0a 54 68 65 20 66 6f 6c 6c 6f  .<br>..The follo
0bd0: 77 69 6e 67 20 74 61 62 6c 65 20 69 73 20 73 70  wing table is sp
0be0: 65 63 69 66 69 63 61 6c 6c 79 20 69 6e 74 65 6e  ecifically inten
0bf0: 64 65 64 20 74 6f 20 6d 61 6b 65 20 74 68 65 20  ded to make the 
0c00: 75 73 61 67 65 20 6f 66 20 62 6f 74 68 20 3c 62  usage of both <b
0c10: 3e 53 54 5f 52 65 6c 61 74 65 28 29 3c 2f 62 3e  >ST_Relate()</b>
0c20: 20 61 6e 64 20 3c 62 3e 53 54 5f 52 65 6c 61 74   and <b>ST_Relat
0c30: 65 4d 61 74 63 68 28 29 3c 2f 62 3e 20 65 61 73  eMatch()</b> eas
0c40: 69 65 72 2c 20 69 6e 20 61 20 72 65 61 73 6f 6e  ier, in a reason
0c50: 61 62 6c 79 20 70 61 69 6e 6c 65 73 73 20 77 61  ably painless wa
0c60: 79 2e 0d 0a 3c 2f 75 6c 3e 3c 2f 6c 69 3e 0d 0a  y...</ul></li>..
0c70: 3c 2f 6f 6c 3e 0d 0a 3c 74 61 62 6c 65 20 63 65  </ol>..<table ce
0c80: 6c 6c 73 70 61 63 69 6e 67 3d 22 34 22 20 63 65  llspacing="4" ce
0c90: 6c 6c 70 61 64 64 69 6e 67 3d 22 36 22 20 62 6f  llpadding="6" bo
0ca0: 72 64 65 72 3d 22 31 22 20 62 67 63 6f 6c 6f 72  rder="1" bgcolor
0cb0: 3d 22 23 66 30 66 66 66 30 22 3e 0d 0a 3c 74 72  ="#f0fff0">..<tr
0cc0: 3e 3c 74 68 20 62 67 63 6f 6c 6f 72 3d 22 23 66  ><th bgcolor="#f
0cd0: 66 63 30 30 30 22 3e 53 51 4c 20 66 75 6e 63 74  fc000">SQL funct
0ce0: 69 6f 6e 3c 2f 74 68 3e 3c 74 68 20 62 67 63 6f  ion</th><th bgco
0cf0: 6c 6f 72 3d 22 23 66 66 63 30 30 30 22 3e 52 65  lor="#ffc000">Re
0d00: 66 65 72 65 6e 63 65 20 50 61 74 74 65 72 6e 3c  ference Pattern<
0d10: 2f 74 68 3e 3c 2f 74 72 3e 0d 0a 3c 74 72 3e 3c  /th></tr>..<tr><
0d20: 74 64 3e 3c 62 3e 53 54 5f 45 71 75 61 6c 73 28  td><b>ST_Equals(
0d30: 20 3c 2f 62 3e 3c 69 3e 67 31 3c 2f 69 3e 2c 20   </b><i>g1</i>, 
0d40: 3c 69 3e 67 32 3c 2f 69 3e 20 3c 62 3e 29 3c 2f  <i>g2</i> <b>)</
0d50: 62 3e 3c 2f 74 64 3e 3c 74 64 20 61 6c 69 67 6e  b></td><td align
0d60: 3d 22 63 65 6e 74 65 72 22 3e 3c 62 3e 54 2a 46  ="center"><b>T*F
0d70: 2a 2a 46 46 46 2a 3c 2f 62 3e 3c 2f 74 64 3e 3c  **FFF*</b></td><
0d80: 2f 74 72 3e 0d 0a 3c 74 72 3e 3c 74 64 3e 3c 62  /tr>..<tr><td><b
0d90: 3e 53 54 5f 44 69 73 6a 6f 69 6e 74 28 20 3c 2f  >ST_Disjoint( </
0da0: 62 3e 3c 69 3e 67 31 3c 2f 69 3e 2c 20 3c 69 3e  b><i>g1</i>, <i>
0db0: 67 32 3c 2f 69 3e 20 3c 62 3e 29 3c 2f 62 3e 3c  g2</i> <b>)</b><
0dc0: 2f 74 64 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 63  /td><td align="c
0dd0: 65 6e 74 65 72 22 3e 3c 62 3e 46 46 2a 46 46 2a  enter"><b>FF*FF*
0de0: 2a 2a 2a 3c 2f 62 3e 3c 2f 74 64 3e 3c 2f 74 72  ***</b></td></tr
0df0: 3e 0d 0a 3c 74 72 3e 3c 74 64 20 72 6f 77 73 70  >..<tr><td rowsp
0e00: 61 6e 3d 22 33 22 3e 3c 62 3e 53 54 5f 54 6f 75  an="3"><b>ST_Tou
0e10: 63 68 65 73 28 20 3c 2f 62 3e 3c 69 3e 67 31 3c  ches( </b><i>g1<
0e20: 2f 69 3e 2c 20 3c 69 3e 67 32 3c 2f 69 3e 20 3c  /i>, <i>g2</i> <
0e30: 62 3e 29 3c 2f 62 3e 3c 2f 74 64 3e 3c 74 64 20  b>)</b></td><td 
0e40: 61 6c 69 67 6e 3d 22 63 65 6e 74 65 72 22 3e 3c  align="center"><
0e50: 62 3e 46 54 2a 2a 2a 2a 2a 2a 2a 3c 2f 62 3e 3c  b>FT*******</b><
0e60: 2f 74 64 3e 3c 2f 74 72 3e 0d 0a 3c 74 72 3e 3c  /td></tr>..<tr><
0e70: 74 64 20 61 6c 69 67 6e 3d 22 63 65 6e 74 65 72  td align="center
0e80: 22 3e 3c 62 3e 46 2a 2a 54 2a 2a 2a 2a 2a 3c 2f  "><b>F**T*****</
0e90: 62 3e 3c 2f 74 64 3e 3c 2f 74 72 3e 0d 0a 3c 74  b></td></tr>..<t
0ea0: 72 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 63 65 6e  r><td align="cen
0eb0: 74 65 72 22 3e 3c 62 3e 46 2a 2a 2a 54 2a 2a 2a  ter"><b>F***T***
0ec0: 2a 3c 2f 62 3e 3c 2f 74 64 3e 3c 2f 74 72 3e 0d  *</b></td></tr>.
0ed0: 0a 3c 74 72 3e 3c 74 64 3e 3c 62 3e 53 54 5f 57  .<tr><td><b>ST_W
0ee0: 69 74 68 69 6e 28 20 3c 2f 62 3e 3c 69 3e 67 31  ithin( </b><i>g1
0ef0: 3c 2f 69 3e 2c 20 3c 69 3e 67 32 3c 2f 69 3e 20  </i>, <i>g2</i> 
0f00: 3c 62 3e 29 3c 2f 62 3e 3c 2f 74 64 3e 3c 74 64  <b>)</b></td><td
0f10: 20 61 6c 69 67 6e 3d 22 63 65 6e 74 65 72 22 3e   align="center">
0f20: 3c 62 3e 54 2a 46 2a 2a 46 2a 2a 2a 3c 2f 62 3e  <b>T*F**F***</b>
0f30: 3c 2f 74 64 3e 3c 2f 74 72 3e 0d 0a 3c 74 72 3e  </td></tr>..<tr>
0f40: 3c 74 64 20 72 6f 77 73 70 61 6e 3d 22 32 22 3e  <td rowspan="2">
0f50: 3c 62 3e 53 54 5f 4f 76 65 72 6c 61 70 73 28 20  <b>ST_Overlaps( 
0f60: 3c 2f 62 3e 3c 69 3e 67 31 3c 2f 69 3e 2c 20 3c  </b><i>g1</i>, <
0f70: 69 3e 67 32 3c 2f 69 3e 20 3c 62 3e 29 3c 2f 62  i>g2</i> <b>)</b
0f80: 3e 3c 2f 74 64 3e 3c 74 64 20 61 6c 69 67 6e 3d  ></td><td align=
0f90: 22 63 65 6e 74 65 72 22 3e 3c 62 3e 54 2a 54 2a  "center"><b>T*T*
0fa0: 2a 2a 54 2a 2a 3c 2f 62 3e 3c 2f 74 64 3e 3c 2f  **T**</b></td></
0fb0: 74 72 3e 0d 0a 3c 74 72 3e 3c 74 64 20 61 6c 69  tr>..<tr><td ali
0fc0: 67 6e 3d 22 63 65 6e 74 65 72 22 3e 3c 62 3e 31  gn="center"><b>1
0fd0: 2a 54 2a 2a 2a 54 2a 2a 3c 2f 62 3e 3c 2f 74 64  *T***T**</b></td
0fe0: 3e 3c 2f 74 72 3e 0d 0a 3c 74 72 3e 3c 74 64 20  ></tr>..<tr><td 
0ff0: 72 6f 77 73 70 61 6e 3d 22 33 22 3e 3c 62 3e 53  rowspan="3"><b>S
1000: 54 5f 43 72 6f 73 73 65 73 28 20 3c 2f 62 3e 3c  T_Crosses( </b><
1010: 69 3e 67 31 3c 2f 69 3e 2c 20 3c 69 3e 67 32 3c  i>g1</i>, <i>g2<
1020: 2f 69 3e 20 3c 62 3e 29 3c 2f 62 3e 3c 2f 74 64  /i> <b>)</b></td
1030: 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 63 65 6e 74  ><td align="cent
1040: 65 72 22 3e 3c 62 3e 54 2a 54 2a 2a 2a 2a 2a 2a  er"><b>T*T******
1050: 3c 2f 62 3e 3c 2f 74 64 3e 3c 2f 74 72 3e 0d 0a  </b></td></tr>..
1060: 3c 74 72 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 63  <tr><td align="c
1070: 65 6e 74 65 72 22 3e 3c 62 3e 54 2a 2a 2a 2a 2a  enter"><b>T*****
1080: 54 2a 2a 3c 2f 62 3e 3c 2f 74 64 3e 3c 2f 74 72  T**</b></td></tr
1090: 3e 0d 0a 3c 74 72 3e 3c 74 64 20 61 6c 69 67 6e  >..<tr><td align
10a0: 3d 22 63 65 6e 74 65 72 22 3e 3c 62 3e 30 2a 2a  ="center"><b>0**
10b0: 2a 2a 2a 2a 2a 2a 3c 2f 62 3e 3c 2f 74 64 3e 3c  ******</b></td><
10c0: 2f 74 72 3e 0d 0a 3c 74 72 3e 3c 74 64 20 72 6f  /tr>..<tr><td ro
10d0: 77 73 70 61 6e 3d 22 34 22 3e 3c 62 3e 53 54 5f  wspan="4"><b>ST_
10e0: 49 6e 74 65 72 73 65 63 74 73 28 20 3c 2f 62 3e  Intersects( </b>
10f0: 3c 69 3e 67 31 3c 2f 69 3e 2c 20 3c 69 3e 67 32  <i>g1</i>, <i>g2
1100: 3c 2f 69 3e 20 3c 62 3e 29 3c 2f 62 3e 3c 2f 74  </i> <b>)</b></t
1110: 64 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 63 65 6e  d><td align="cen
1120: 74 65 72 22 3e 3c 62 3e 54 2a 2a 2a 2a 2a 2a 2a  ter"><b>T*******
1130: 2a 3c 2f 62 3e 3c 2f 74 64 3e 3c 2f 74 72 3e 0d  *</b></td></tr>.
1140: 0a 3c 74 72 3e 3c 74 64 20 61 6c 69 67 6e 3d 22  .<tr><td align="
1150: 63 65 6e 74 65 72 22 3e 3c 62 3e 2a 54 2a 2a 2a  center"><b>*T***
1160: 2a 2a 2a 2a 3c 2f 62 3e 3c 2f 74 64 3e 3c 2f 74  ****</b></td></t
1170: 72 3e 0d 0a 3c 74 72 3e 3c 74 64 20 61 6c 69 67  r>..<tr><td alig
1180: 6e 3d 22 63 65 6e 74 65 72 22 3e 3c 62 3e 2a 2a  n="center"><b>**
1190: 2a 54 2a 2a 2a 2a 2a 3c 2f 62 3e 3c 2f 74 64 3e  *T*****</b></td>
11a0: 3c 2f 74 72 3e 0d 0a 3c 74 72 3e 3c 74 64 20 61  </tr>..<tr><td a
11b0: 6c 69 67 6e 3d 22 63 65 6e 74 65 72 22 3e 3c 62  lign="center"><b
11c0: 3e 2a 2a 2a 2a 54 2a 2a 2a 2a 3c 2f 62 3e 3c 2f  >****T****</b></
11d0: 74 64 3e 3c 2f 74 72 3e 0d 0a 3c 74 72 3e 3c 74  td></tr>..<tr><t
11e0: 64 3e 3c 62 3e 53 54 5f 43 6f 6e 74 61 69 6e 73  d><b>ST_Contains
11f0: 28 20 3c 2f 62 3e 3c 69 3e 67 31 3c 2f 69 3e 2c  ( </b><i>g1</i>,
1200: 20 3c 69 3e 67 32 3c 2f 69 3e 20 3c 62 3e 29 3c   <i>g2</i> <b>)<
1210: 2f 62 3e 3c 2f 74 64 3e 3c 74 64 20 61 6c 69 67  /b></td><td alig
1220: 6e 3d 22 63 65 6e 74 65 72 22 3e 3c 62 3e 54 2a  n="center"><b>T*
1230: 2a 2a 2a 2a 46 46 2a 3c 2f 62 3e 3c 2f 74 64 3e  ****FF*</b></td>
1240: 3c 2f 74 72 3e 0d 0a 3c 74 72 3e 3c 74 64 20 72  </tr>..<tr><td r
1250: 6f 77 73 70 61 6e 3d 22 34 22 3e 3c 62 3e 53 54  owspan="4"><b>ST
1260: 5f 43 6f 76 65 72 73 28 20 3c 2f 62 3e 3c 69 3e  _Covers( </b><i>
1270: 67 31 3c 2f 69 3e 2c 20 3c 69 3e 67 32 3c 2f 69  g1</i>, <i>g2</i
1280: 3e 20 3c 62 3e 29 3c 2f 62 3e 3c 2f 74 64 3e 3c  > <b>)</b></td><
1290: 74 64 20 61 6c 69 67 6e 3d 22 63 65 6e 74 65 72  td align="center
12a0: 22 3e 3c 62 3e 54 2a 2a 2a 2a 2a 46 46 2a 3c 2f  "><b>T*****FF*</
12b0: 62 3e 3c 2f 74 64 3e 3c 2f 74 72 3e 0d 0a 3c 74  b></td></tr>..<t
12c0: 72 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 63 65 6e  r><td align="cen
12d0: 74 65 72 22 3e 3c 62 3e 2a 54 2a 2a 2a 2a 46 46  ter"><b>*T****FF
12e0: 2a 3c 2f 62 3e 3c 2f 74 64 3e 3c 2f 74 72 3e 0d  *</b></td></tr>.
12f0: 0a 3c 74 72 3e 3c 74 64 20 61 6c 69 67 6e 3d 22  .<tr><td align="
1300: 63 65 6e 74 65 72 22 3e 3c 62 3e 2a 2a 2a 54 2a  center"><b>***T*
1310: 2a 46 46 2a 3c 2f 62 3e 3c 2f 74 64 3e 3c 2f 74  *FF*</b></td></t
1320: 72 3e 0d 0a 3c 74 72 3e 3c 74 64 20 61 6c 69 67  r>..<tr><td alig
1330: 6e 3d 22 63 65 6e 74 65 72 22 3e 3c 62 3e 2a 2a  n="center"><b>**
1340: 2a 2a 54 2a 46 46 2a 3c 2f 62 3e 3c 2f 74 64 3e  **T*FF*</b></td>
1350: 3c 2f 74 72 3e 0d 0a 3c 74 72 3e 3c 74 64 20 72  </tr>..<tr><td r
1360: 6f 77 73 70 61 6e 3d 22 34 22 3e 3c 62 3e 53 54  owspan="4"><b>ST
1370: 5f 43 6f 76 65 72 65 64 42 79 28 20 3c 2f 62 3e  _CoveredBy( </b>
1380: 3c 69 3e 67 31 3c 2f 69 3e 2c 20 3c 69 3e 67 32  <i>g1</i>, <i>g2
1390: 3c 2f 69 3e 20 3c 62 3e 29 3c 2f 62 3e 3c 2f 74  </i> <b>)</b></t
13a0: 64 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 63 65 6e  d><td align="cen
13b0: 74 65 72 22 3e 3c 62 3e 54 2a 46 2a 2a 46 2a 2a  ter"><b>T*F**F**
13c0: 2a 3c 2f 62 3e 3c 2f 74 64 3e 3c 2f 74 72 3e 0d  *</b></td></tr>.
13d0: 0a 3c 74 72 3e 3c 74 64 20 61 6c 69 67 6e 3d 22  .<tr><td align="
13e0: 63 65 6e 74 65 72 22 3e 3c 62 3e 2a 54 46 2a 2a  center"><b>*TF**
13f0: 46 2a 2a 2a 3c 2f 62 3e 3c 2f 74 64 3e 3c 2f 74  F***</b></td></t
1400: 72 3e 0d 0a 3c 74 72 3e 3c 74 64 20 61 6c 69 67  r>..<tr><td alig
1410: 6e 3d 22 63 65 6e 74 65 72 22 3e 3c 62 3e 2a 2a  n="center"><b>**
1420: 46 54 2a 46 2a 2a 2a 3c 2f 62 3e 3c 2f 74 64 3e  FT*F***</b></td>
1430: 3c 2f 74 72 3e 0d 0a 3c 74 72 3e 3c 74 64 20 61  </tr>..<tr><td a
1440: 6c 69 67 6e 3d 22 63 65 6e 74 65 72 22 3e 3c 62  lign="center"><b
1450: 3e 2a 2a 46 2a 54 46 2a 2a 2a 3c 2f 62 3e 3c 2f  >**F*TF***</b></
1460: 74 64 3e 3c 2f 74 72 3e 0d 0a 3c 2f 74 61 62 6c  td></tr>..</tabl
1470: 65 3e 3c 62 72 3e 0d 0a 46 75 72 74 68 65 72 20  e><br>..Further 
1480: 75 73 65 66 75 6c 20 72 65 61 64 69 6e 67 73 3a  useful readings:
1490: 0d 0a 3c 75 6c 3e 0d 0a 3c 6c 69 3e 61 20 67 65  ..<ul>..<li>a ge
14a0: 6e 65 72 61 6c 20 69 6e 74 72 6f 64 75 63 74 69  neral introducti
14b0: 6f 6e 20 74 6f 20 3c 61 20 68 72 65 66 3d 22 68  on to <a href="h
14c0: 74 74 70 73 3a 2f 2f 65 6e 2e 77 69 6b 69 70 65  ttps://en.wikipe
14d0: 64 69 61 2e 6f 72 67 2f 77 69 6b 69 2f 44 45 2d  dia.org/wiki/DE-
14e0: 39 49 4d 22 3e 44 45 2d 39 49 4d 3c 2f 61 3e 3c  9IM">DE-9IM</a><
14f0: 2f 6c 69 3e 0d 0a 3c 6c 69 3e 61 20 77 65 6c 6c  /li>..<li>a well
1500: 2d 77 72 69 74 74 65 6e 20 64 6f 63 75 6d 65 6e  -written documen
1510: 74 61 74 69 6f 6e 20 66 72 6f 6d 20 61 20 3c 61  tation from a <a
1520: 20 68 72 65 66 3d 22 68 74 74 70 3a 2f 2f 65 64   href="http://ed
1530: 6e 64 6f 63 2e 65 73 72 69 2e 63 6f 6d 2f 61 72  ndoc.esri.com/ar
1540: 63 73 64 65 2f 39 2e 30 2f 67 65 6e 65 72 61 6c  csde/9.0/general
1550: 5f 74 6f 70 69 63 73 2f 75 6e 64 65 72 73 74 61  _topics/understa
1560: 6e 64 5f 73 70 61 74 69 61 6c 5f 72 65 6c 61 74  nd_spatial_relat
1570: 69 6f 6e 73 2e 68 74 6d 22 3e 50 72 6f 70 72 69  ions.htm">Propri
1580: 65 74 61 72 79 20 53 57 20 56 65 6e 64 6f 72 3c  etary SW Vendor<
1590: 2f 61 3e 3c 2f 6c 69 3e 0d 0a 3c 2f 75 6c 3e 3c  /a></li>..</ul><
15a0: 62 72 3e 0d 0a 3c 68 72 3e 0d 0a 3c 68 33 3e 41  br>..<hr>..<h3>A
15b0: 20 70 72 61 63 74 69 63 61 6c 20 65 78 61 6d 70   practical examp
15c0: 6c 65 20 28 61 6e 64 20 72 65 6c 61 74 65 64 20  le (and related 
15d0: 6f 62 6a 65 63 74 69 76 65 20 74 69 6d 69 6e 67  objective timing
15e0: 73 29 3c 2f 68 33 3e 0d 0a 3c 76 65 72 62 61 74  s)</h3>..<verbat
15f0: 69 6d 3e 0d 0a 53 45 4c 45 43 54 20 6d 2e 6e 61  im>..SELECT m.na
1600: 6d 65 20 41 53 20 6d 75 6e 69 63 69 70 61 6c 69  me AS municipali
1610: 74 79 2c 20 70 2e 6e 61 6d 65 20 41 53 20 70 72  ty, p.name AS pr
1620: 6f 76 69 6e 63 65 2c 0d 0a 20 20 20 20 53 54 5f  ovince,..    ST_
1630: 44 69 73 6a 6f 69 6e 74 28 6d 2e 67 65 6f 6d 2c  Disjoint(m.geom,
1640: 20 70 2e 67 65 6f 6d 29 20 41 53 20 64 69 73 6a   p.geom) AS disj
1650: 6f 69 6e 74 2c 0d 0a 20 20 20 20 53 54 5f 54 6f  oint,..    ST_To
1660: 75 63 68 65 73 28 6d 2e 67 65 6f 6d 2c 20 70 2e  uches(m.geom, p.
1670: 67 65 6f 6d 29 20 41 53 20 74 6f 75 63 68 65 73  geom) AS touches
1680: 2c 0d 0a 20 20 20 20 53 54 5f 57 69 74 68 69 6e  ,..    ST_Within
1690: 28 6d 2e 67 65 6f 6d 2c 20 70 2e 67 65 6f 6d 29  (m.geom, p.geom)
16a0: 20 41 53 20 77 69 74 68 69 6e 2c 0d 0a 20 20 20   AS within,..   
16b0: 20 53 54 5f 49 6e 74 65 72 73 65 63 74 73 28 6d   ST_Intersects(m
16c0: 2e 67 65 6f 6d 2c 20 70 2e 67 65 6f 6d 29 20 41  .geom, p.geom) A
16d0: 53 20 69 6e 74 65 72 73 65 63 74 73 2c 0d 0a 20  S intersects,.. 
16e0: 20 20 20 53 54 5f 4f 76 65 72 6c 61 70 73 28 6d     ST_Overlaps(m
16f0: 2e 67 65 6f 6d 2c 20 70 2e 67 65 6f 6d 29 20 41  .geom, p.geom) A
1700: 53 20 6f 76 65 72 6c 61 70 73 2c 0d 0a 20 20 20  S overlaps,..   
1710: 20 53 54 5f 43 6f 76 65 72 65 64 42 79 28 6d 2e   ST_CoveredBy(m.
1720: 67 65 6f 6d 2c 20 70 2e 67 65 6f 6d 29 20 41 53  geom, p.geom) AS
1730: 20 63 6f 76 65 72 65 64 5f 62 79 0d 0a 46 52 4f   covered_by..FRO
1740: 4d 20 6d 75 6e 69 63 69 70 61 6c 69 74 69 65 73  M municipalities
1750: 20 41 53 20 6d 2c 20 70 72 6f 76 69 6e 63 65 73   AS m, provinces
1760: 20 41 53 20 70 3b 0d 0a 3c 2f 76 65 72 62 61 74   AS p;..</verbat
1770: 69 6d 3e 0d 0a 49 6e 20 74 68 69 73 20 66 69 72  im>..In this fir
1780: 73 74 20 74 65 73 74 20 49 27 76 65 20 66 6f 6c  st test I've fol
1790: 6c 6f 77 65 64 20 74 68 65 20 6d 6f 73 74 20 3c  lowed the most <
17a0: 62 3e 63 6c 61 73 73 69 63 61 6c 20 61 70 70 72  b>classical appr
17b0: 6f 61 63 68 3c 2f 62 3e 20 74 6f 20 64 65 74 65  oach</b> to dete
17c0: 72 6d 69 6e 65 20 73 65 76 65 72 61 6c 20 73 70  rmine several sp
17d0: 61 74 69 61 6c 20 72 65 6c 61 74 69 6f 6e 73 68  atial relationsh
17e0: 69 70 73 20 69 6e 74 65 72 63 75 72 72 69 6e 67  ips intercurring
17f0: 20 62 65 74 77 65 65 6e 3a 0d 0a 3c 75 6c 3e 0d   between:..<ul>.
1800: 0a 3c 6c 69 3e 74 68 65 20 3c 62 3e 6d 75 6e 69  .<li>the <b>muni
1810: 63 69 70 61 6c 69 74 69 65 73 3c 2f 62 3e 20 74  cipalities</b> t
1820: 61 62 6c 65 20 28 4d 75 6c 74 69 50 6f 6c 79 67  able (MultiPolyg
1830: 6f 6e 73 29 2c 20 63 6f 6e 74 61 69 6e 69 6e 67  ons), containing
1840: 20 74 68 65 20 3c 62 3e 32 37 36 3c 2f 62 3e 20   the <b>276</b> 
1850: 4d 75 6e 69 63 69 70 61 6c 69 74 65 73 20 6f 66  Municipalites of
1860: 20 54 75 73 63 61 6e 79 2e 3c 2f 6c 69 3e 0d 0a   Tuscany.</li>..
1870: 3c 6c 69 3e 61 6e 64 20 74 68 65 20 3c 62 3e 70  <li>and the <b>p
1880: 72 6f 76 69 6e 63 65 73 3c 2f 62 3e 20 74 61 62  rovinces</b> tab
1890: 6c 65 20 28 4d 75 6c 74 69 50 6f 6c 79 67 6f 6e  le (MultiPolygon
18a0: 73 29 2c 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  s), containing t
18b0: 68 65 20 3c 62 3e 31 30 3c 2f 62 3e 20 50 72 6f  he <b>10</b> Pro
18c0: 76 69 6e 63 65 73 20 6f 66 20 54 75 73 63 61 6e  vinces of Tuscan
18d0: 79 2e 3c 2f 6c 69 3e 0d 0a 3c 6c 69 3e 74 68 69  y.</li>..<li>thi
18e0: 73 20 69 73 20 61 6e 20 75 6e 73 6f 70 68 69 73  s is an unsophis
18f0: 74 69 63 61 74 65 64 20 71 75 65 72 79 2c 20 61  ticated query, a
1900: 6e 64 20 74 68 65 20 72 65 73 75 6c 74 69 6e 67  nd the resulting
1910: 20 3c 62 3e 63 61 72 74 65 73 69 61 6e 20 70 72   <b>cartesian pr
1920: 6f 64 75 63 74 3c 2f 62 3e 20 6f 66 20 62 6f 74  oduct</b> of bot
1930: 68 20 74 61 62 6c 65 73 20 63 6f 6e 74 61 69 6e  h tables contain
1940: 73 20 3c 62 3e 32 2e 37 36 30 3c 2f 62 3e 20 72  s <b>2.760</b> r
1950: 6f 77 73 2e 3c 2f 6c 69 3e 0d 0a 3c 2f 75 6c 3e  ows.</li>..</ul>
1960: 0d 0a 3c 75 3e 4d 65 61 73 75 72 65 64 20 74 69  ..<u>Measured ti
1970: 6d 69 6e 67 3c 2f 75 3e 3a 20 3c 62 3e 36 37 20  ming</u>: <b>67 
1980: 73 65 63 73 3c 2f 62 3e 0d 0a 3c 62 72 3e 3c 62  secs</b>..<br><b
1990: 72 3e 3c 62 72 3e 0d 0a 3c 76 65 72 62 61 74 69  r><br>..<verbati
19a0: 6d 3e 0d 0a 43 52 45 41 54 45 20 54 45 4d 50 4f  m>..CREATE TEMPO
19b0: 52 41 52 59 20 54 41 42 4c 45 20 74 6d 70 5f 72  RARY TABLE tmp_r
19c0: 65 6c 61 74 65 20 41 53 0d 0a 53 45 4c 45 43 54  elate AS..SELECT
19d0: 20 6d 2e 6e 61 6d 65 20 41 53 20 6d 75 6e 69 63   m.name AS munic
19e0: 69 70 61 6c 69 74 79 2c 20 70 2e 6e 61 6d 65 20  ipality, p.name 
19f0: 41 53 20 70 72 6f 76 69 6e 63 65 2c 20 53 54 5f  AS province, ST_
1a00: 52 65 6c 61 74 65 28 6d 2e 67 65 6f 6d 2c 20 70  Relate(m.geom, p
1a10: 2e 67 65 6f 6d 29 20 41 53 20 6d 61 74 72 69 78  .geom) AS matrix
1a20: 0d 0a 46 52 4f 4d 20 6d 75 6e 69 63 69 70 61 6c  ..FROM municipal
1a30: 69 74 69 65 73 20 41 53 20 6d 2c 20 70 72 6f 76  ities AS m, prov
1a40: 69 6e 63 65 73 20 41 53 20 70 3b 0d 0a 0d 0a 53  inces AS p;....S
1a50: 45 4c 45 43 54 20 6d 75 6e 69 63 69 70 61 6c 69  ELECT municipali
1a60: 74 79 2c 20 70 72 6f 76 69 6e 63 65 2c 20 6d 61  ty, province, ma
1a70: 74 72 69 78 2c 0d 0a 20 20 20 20 53 54 5f 52 65  trix,..    ST_Re
1a80: 6c 61 74 65 4d 61 74 63 68 28 6d 61 74 72 69 78  lateMatch(matrix
1a90: 2c 20 27 46 46 2a 46 46 2a 2a 2a 2a 27 29 20 41  , 'FF*FF****') A
1aa0: 53 20 64 69 73 6a 6f 69 6e 74 2c 0d 0a 20 20 20  S disjoint,..   
1ab0: 20 53 54 5f 52 65 6c 61 74 65 4d 61 74 63 68 28   ST_RelateMatch(
1ac0: 6d 61 74 72 69 78 2c 20 27 46 54 2a 2a 2a 2a 2a  matrix, 'FT*****
1ad0: 2a 2a 27 29 20 4f 52 0d 0a 20 20 20 20 53 54 5f  **') OR..    ST_
1ae0: 52 65 6c 61 74 65 4d 61 74 63 68 28 6d 61 74 72  RelateMatch(matr
1af0: 69 78 2c 20 27 46 2a 2a 54 2a 2a 2a 2a 2a 27 29  ix, 'F**T*****')
1b00: 20 4f 52 0d 0a 20 20 20 20 53 54 5f 52 65 6c 61   OR..    ST_Rela
1b10: 74 65 4d 61 74 63 68 28 6d 61 74 72 69 78 2c 20  teMatch(matrix, 
1b20: 27 46 2a 2a 2a 54 2a 2a 2a 2a 27 29 20 41 53 20  'F***T****') AS 
1b30: 74 6f 75 63 68 65 73 2c 0d 0a 20 20 20 20 53 54  touches,..    ST
1b40: 5f 52 65 6c 61 74 65 4d 61 74 63 68 28 6d 61 74  _RelateMatch(mat
1b50: 72 69 78 2c 20 27 54 2a 46 2a 2a 46 2a 2a 2a 27  rix, 'T*F**F***'
1b60: 29 20 41 53 20 77 69 74 68 69 6e 2c 0d 0a 20 20  ) AS within,..  
1b70: 20 20 53 54 5f 52 65 6c 61 74 65 4d 61 74 63 68    ST_RelateMatch
1b80: 28 6d 61 74 72 69 78 2c 20 27 54 2a 2a 2a 2a 2a  (matrix, 'T*****
1b90: 2a 2a 2a 27 29 20 4f 52 0d 0a 20 20 20 20 53 54  ***') OR..    ST
1ba0: 5f 52 65 6c 61 74 65 4d 61 74 63 68 28 6d 61 74  _RelateMatch(mat
1bb0: 72 69 78 2c 20 27 2a 54 2a 2a 2a 2a 2a 2a 2a 27  rix, '*T*******'
1bc0: 29 20 4f 52 0d 0a 20 20 20 20 53 54 5f 52 65 6c  ) OR..    ST_Rel
1bd0: 61 74 65 4d 61 74 63 68 28 6d 61 74 72 69 78 2c  ateMatch(matrix,
1be0: 20 27 2a 2a 2a 54 2a 2a 2a 2a 2a 27 29 20 4f 52   '***T*****') OR
1bf0: 0d 0a 20 20 20 20 53 54 5f 52 65 6c 61 74 65 4d  ..    ST_RelateM
1c00: 61 74 63 68 28 6d 61 74 72 69 78 2c 20 27 2a 2a  atch(matrix, '**
1c10: 2a 2a 54 2a 2a 2a 2a 27 29 20 41 53 20 69 6e 74  **T****') AS int
1c20: 65 72 73 65 63 74 73 2c 0d 0a 20 20 20 20 53 54  ersects,..    ST
1c30: 5f 52 65 6c 61 74 65 4d 61 74 63 68 28 6d 61 74  _RelateMatch(mat
1c40: 72 69 78 2c 20 27 54 2a 54 2a 2a 2a 54 2a 2a 27  rix, 'T*T***T**'
1c50: 29 20 41 53 20 6f 76 65 72 6c 61 70 73 2c 0d 0a  ) AS overlaps,..
1c60: 20 20 20 20 53 54 5f 52 65 6c 61 74 65 4d 61 74      ST_RelateMat
1c70: 63 68 28 6d 61 74 72 69 78 2c 20 27 54 2a 46 2a  ch(matrix, 'T*F*
1c80: 2a 46 2a 2a 2a 27 29 20 4f 52 0d 0a 20 20 20 20  *F***') OR..    
1c90: 53 54 5f 52 65 6c 61 74 65 4d 61 74 63 68 28 6d  ST_RelateMatch(m
1ca0: 61 74 72 69 78 2c 20 27 2a 54 46 2a 2a 46 2a 2a  atrix, '*TF**F**
1cb0: 2a 27 29 20 4f 52 0d 0a 20 20 20 20 53 54 5f 52  *') OR..    ST_R
1cc0: 65 6c 61 74 65 4d 61 74 63 68 28 6d 61 74 72 69  elateMatch(matri
1cd0: 78 2c 20 27 2a 2a 46 54 2a 46 2a 2a 2a 27 29 20  x, '**FT*F***') 
1ce0: 4f 52 0d 0a 20 20 20 20 53 54 5f 52 65 6c 61 74  OR..    ST_Relat
1cf0: 65 4d 61 74 63 68 28 6d 61 74 72 69 78 2c 20 27  eMatch(matrix, '
1d00: 2a 2a 46 2a 54 46 2a 2a 2a 27 29 20 41 53 20 63  **F*TF***') AS c
1d10: 6f 76 65 72 65 64 5f 62 79 0d 0a 46 52 4f 4d 20  overed_by..FROM 
1d20: 74 6d 70 5f 72 65 6c 61 74 65 3b 0d 0a 3c 2f 76  tmp_relate;..</v
1d30: 65 72 62 61 74 69 6d 3e 0d 0a 55 73 69 6e 67 20  erbatim>..Using 
1d40: 74 68 65 20 73 61 6d 65 20 74 61 62 6c 65 73 20  the same tables 
1d50: 61 73 20 62 65 66 6f 72 65 20 69 6e 20 74 68 69  as before in thi
1d60: 73 20 73 65 63 6f 6e 64 20 74 65 73 74 2c 20 62  s second test, b
1d70: 75 74 20 74 68 69 73 20 74 69 6d 65 20 49 27 76  ut this time I'v
1d80: 65 20 61 64 6f 70 74 65 64 20 74 68 65 20 61 6c  e adopted the al
1d90: 74 65 72 6e 61 74 69 76 65 20 61 70 70 72 6f 61  ternative approa
1da0: 63 68 20 62 61 73 65 64 20 6f 6e 20 3c 62 3e 53  ch based on <b>S
1db0: 54 5f 52 65 6c 61 74 65 28 29 3c 2f 62 3e 20 61  T_Relate()</b> a
1dc0: 6e 64 20 3c 62 3e 53 54 5f 52 65 6c 61 74 65 4d  nd <b>ST_RelateM
1dd0: 61 74 63 68 28 29 3c 2f 62 3e 2e 3c 62 72 3e 0d  atch()</b>.<br>.
1de0: 0a 41 66 74 65 72 20 73 6f 6d 65 20 70 72 65 6c  .After some prel
1df0: 69 6d 69 6e 61 72 79 20 74 65 73 74 73 2c 20 69  iminary tests, i
1e00: 74 20 71 75 69 63 6b 6c 79 20 65 6d 65 72 67 65  t quickly emerge
1e10: 64 20 74 68 61 74 20 74 68 65 20 53 51 4c 69 74  d that the SQLit
1e20: 65 20 6f 70 74 69 6d 69 7a 65 72 20 64 6f 65 73  e optimizer does
1e30: 6e 27 74 20 6c 69 6b 65 20 74 68 65 20 6d 69 78  n't like the mix
1e40: 74 75 72 65 20 6f 66 20 61 6e 20 69 6e 6e 65 72  ture of an inner
1e50: 20 71 75 65 72 79 20 74 6f 67 65 74 68 65 72 20   query together 
1e60: 77 69 74 68 20 66 75 6e 63 74 69 6f 6e 20 63 61  with function ca
1e70: 6c 6c 73 20 6f 6e 20 74 68 65 20 73 61 6d 65 20  lls on the same 
1e80: 6f 76 65 72 61 6c 6c 20 71 75 65 72 79 20 28 72  overall query (r
1e90: 65 73 75 6c 74 69 6e 67 20 69 6e 20 61 77 66 75  esulting in awfu
1ea0: 6c 20 74 69 6d 69 6e 67 73 29 2c 20 73 6f 20 49  l timings), so I
1eb0: 20 64 75 6c 79 20 73 77 69 74 63 68 65 64 20 74   duly switched t
1ec0: 6f 20 61 6e 20 69 6e 64 69 72 65 63 74 20 3c 62  o an indirect <b
1ed0: 3e 74 77 6f 2d 73 74 65 70 73 20 61 70 70 72 6f  >two-steps appro
1ee0: 61 63 68 3c 2f 62 3e 3a 0d 0a 3c 75 6c 3e 0d 0a  ach</b>:..<ul>..
1ef0: 3c 6c 69 3e 3c 62 3e 73 74 65 70 20 23 31 3c 2f  <li><b>step #1</
1f00: 62 3e 3a 20 63 6f 6d 70 75 74 69 6e 67 20 3c 62  b>: computing <b
1f10: 3e 53 54 5f 52 65 6c 61 74 65 64 28 29 3c 2f 62  >ST_Related()</b
1f20: 3e 20 61 6e 64 20 73 61 76 69 6e 67 20 74 68 65  > and saving the
1f30: 20 77 68 6f 6c 65 20 72 65 73 75 6c 74 73 65 74   whole resultset
1f40: 20 69 6e 74 6f 20 61 20 3c 62 3e 74 65 6d 70 6f   into a <b>tempo
1f50: 72 61 72 79 20 74 61 62 6c 65 3c 2f 62 3e 2e 3c  rary table</b>.<
1f60: 2f 6c 69 3e 0d 0a 3c 6c 69 3e 3c 62 3e 73 74 65  /li>..<li><b>ste
1f70: 70 20 23 32 3c 2f 62 3e 3a 20 63 6f 6d 70 6c 65  p #2</b>: comple
1f80: 74 69 6e 67 20 74 68 65 20 74 61 73 6b 20 62 79  ting the task by
1f90: 20 63 61 6c 6c 69 6e 67 20 3c 62 3e 53 54 5f 52   calling <b>ST_R
1fa0: 65 6c 61 74 65 4d 61 74 63 68 3c 2f 62 3e 20 6f  elateMatch</b> o
1fb0: 6e 20 74 68 65 20 61 62 6f 76 65 20 74 65 6d 70  n the above temp
1fc0: 6f 72 61 72 79 20 74 61 62 6c 65 2e 3c 2f 6c 69  orary table.</li
1fd0: 3e 0d 0a 3c 2f 75 6c 3e 0d 0a 3c 75 3e 4d 65 61  >..</ul>..<u>Mea
1fe0: 73 75 72 65 64 20 74 69 6d 69 6e 67 3c 2f 75 3e  sured timing</u>
1ff0: 3a 20 3c 62 3e 32 32 20 73 65 63 73 3c 2f 62 3e  : <b>22 secs</b>
2000: 3c 62 72 3e 3c 62 72 3e 0d 0a 3c 75 3e 4e 6f 74  <br><br>..<u>Not
2010: 65 3c 2f 75 3e 3a 20 74 68 65 20 73 65 63 6f 6e  e</u>: the secon
2020: 64 20 71 75 65 72 79 20 28 53 54 5f 52 65 6c 61  d query (ST_Rela
2030: 74 65 4d 61 74 63 68 29 20 6f 6e 6c 79 20 72 65  teMatch) only re
2040: 71 75 69 72 65 64 20 61 20 66 65 77 20 6d 69 6c  quired a few mil
2050: 6c 69 73 65 63 6f 6e 64 73 20 74 6f 20 63 6f 6d  liseconds to com
2060: 70 6c 65 74 65 3b 20 74 68 65 20 72 65 61 6c 20  plete; the real 
2070: 63 6f 6d 70 75 74 61 74 69 6f 6e 61 6c 20 6c 6f  computational lo
2080: 61 64 20 77 61 73 20 65 6e 74 69 72 65 6c 79 20  ad was entirely 
2090: 63 6f 6e 66 69 6e 65 64 20 77 69 74 68 69 6e 20  confined within 
20a0: 74 68 65 20 66 69 72 73 74 20 71 75 65 72 79 20  the first query 
20b0: 28 53 54 5f 52 65 6c 61 74 65 29 2e 3c 62 72 3e  (ST_Relate).<br>
20c0: 3c 62 72 3e 0d 0a 3c 75 3e 46 69 6e 61 6c 20 63  <br>..<u>Final c
20d0: 6f 6e 63 6c 75 73 69 6f 6e 3c 2f 75 3e 3a 20 64  onclusion</u>: d
20e0: 69 72 65 63 74 6c 79 20 75 73 69 6e 67 20 74 68  irectly using th
20f0: 65 20 61 77 6b 77 61 72 64 20 44 45 2d 39 49 4d  e awkward DE-9IM
2100: 20 6d 6f 64 65 6c 20 69 73 20 69 6e 20 73 6f 6d   model is in som
2110: 65 77 61 79 73 20 64 69 66 66 69 63 75 6c 74 2c  eways difficult,
2120: 20 62 75 74 20 69 74 20 64 6f 65 73 20 65 6e 73   but it does ens
2130: 75 72 65 20 61 6e 20 61 73 74 6f 6e 69 73 68 69  ure an astonishi
2140: 6e 67 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 62  ng performance b
2150: 6f 6f 73 74 2e 3c 62 72 3e 0d 0a 49 74 27 73 20  oost.<br>..It's 
2160: 75 73 61 67 65 20 69 73 20 68 69 67 68 6c 79 20  usage is highly 
2170: 72 65 63 6f 6d 6d 65 6e 64 65 64 20 77 68 65 6e  recommended when
2180: 20 79 6f 75 20 61 72 65 20 72 65 71 75 69 72 65   you are require
2190: 64 20 74 6f 20 63 68 65 63 6b 20 6d 6f 72 65 20  d to check more 
21a0: 74 68 61 6e 20 61 20 73 69 6e 67 6c 65 20 73 70  than a single sp
21b0: 61 74 69 61 6c 20 72 65 6c 61 74 69 6f 6e 73 68  atial relationsh
21c0: 69 70 20 62 65 74 77 65 65 6e 20 74 68 65 20 73  ip between the s
21d0: 61 6d 65 20 70 61 69 72 20 6f 66 20 47 65 6f 6d  ame pair of Geom
21e0: 65 74 72 69 65 73 2e 0d 0a 3c 62 72 3e 3c 62 72  etries...<br><br
21f0: 3e 0a 5a 20 32 61 61 30 34 62 30 35 63 62 34 31  >.Z 2aa04b05cb41
2200: 64 38 37 33 64 34 61 30 39 34 31 34 63 66 62 64  d873d4a09414cfbd
2210: 66 33 33 66 0a                                   f33f.