Wiki page
[about DE-9IM] by
sandro
2017-04-18 09:20:05.
0000: 44 20 32 30 31 37 2d 30 34 2d 31 38 54 30 39 3a D 2017-04-18T09:
0010: 32 30 3a 30 35 2e 38 32 30 0a 4c 20 61 62 6f 75 20:05.820.L abou
0020: 74 5c 73 44 45 2d 39 49 4d 0a 55 20 73 61 6e 64 t\sDE-9IM.U sand
0030: 72 6f 0a 57 20 38 35 38 31 0a 3c 68 32 3e 43 6f ro.W 8581.<h2>Co
0040: 72 72 65 73 70 6f 6e 64 65 6e 63 65 73 20 62 65 rrespondences be
0050: 74 77 65 65 6e 20 53 51 4c 20 46 75 6e 63 74 69 tween SQL Functi
0060: 6f 6e 73 20 61 6e 64 20 44 45 2d 39 49 4d 20 49 ons and DE-9IM I
0070: 6e 74 65 72 73 65 63 74 69 6f 6e 20 4d 61 74 72 ntersection Matr
0080: 69 63 65 73 3c 2f 68 32 3e 0d 0a 51 75 69 63 6b ices</h2>..Quick
0090: 20 72 65 63 61 6c 6c 3a 0d 0a 3c 6f 6c 3e 0d 0a recall:..<ol>..
00a0: 3c 6c 69 3e 54 68 65 20 73 74 61 6e 64 61 72 64 <li>The standard
00b0: 20 53 70 61 74 69 61 6c 20 53 51 4c 20 6d 6f 64 Spatial SQL mod
00c0: 65 6c 20 64 65 66 69 6e 65 73 20 73 65 76 65 72 el defines sever
00d0: 61 6c 20 66 75 6e 63 74 69 6f 6e 73 20 28 61 73 al functions (as
00e0: 20 65 2e 67 2e 20 3c 62 3e 53 54 5f 45 71 75 61 e.g. <b>ST_Equa
00f0: 6c 73 28 29 3c 2f 62 3e 2c 20 3c 62 3e 53 54 5f ls()</b>, <b>ST_
0100: 49 6e 74 65 72 73 65 63 74 73 28 29 3c 2f 62 3e Intersects()</b>
0110: 2c 20 3c 62 3e 53 54 5f 54 6f 75 63 68 65 73 28 , <b>ST_Touches(
0120: 29 3c 2f 62 3e 20 61 6e 64 20 61 6c 69 6b 65 29 )</b> and alike)
0130: 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 74 65 73 intended to tes
0140: 74 20 69 66 20 61 20 67 69 76 65 6e 20 73 70 61 t if a given spa
0150: 74 69 61 6c 20 72 65 6c 61 74 69 6f 6e 73 68 69 tial relationshi
0160: 70 20 65 78 69 73 74 73 20 62 65 74 77 65 65 6e p exists between
0170: 20 61 20 70 61 69 72 20 6f 66 20 47 65 6f 6d 65 a pair of Geome
0180: 74 72 69 65 73 2e 3c 2f 6c 69 3e 0d 0a 3c 6c 69 tries.</li>..<li
0190: 3e 41 6c 6c 20 73 75 63 68 20 53 51 4c 20 66 75 >All such SQL fu
01a0: 6e 63 74 69 6f 6e 73 20 73 69 6d 70 6c 79 20 61 nctions simply a
01b0: 72 65 20 63 6f 6e 76 65 6e 69 65 6e 63 65 20 77 re convenience w
01c0: 72 61 70 70 65 72 73 20 62 61 73 65 64 20 6f 6e rappers based on
01d0: 20 74 68 65 20 6d 6f 73 74 20 67 65 6e 65 72 61 the most genera
01e0: 6c 20 44 45 2d 39 49 4d 20 6d 6f 64 65 6c 2e 3c l DE-9IM model.<
01f0: 2f 6c 69 3e 0d 0a 3c 6c 69 3e 41 6e 20 61 6c 74 /li>..<li>An alt
0200: 65 72 6e 61 74 69 76 65 20 28 6d 6f 72 65 20 66 ernative (more f
0210: 6c 65 78 69 62 6c 65 2c 20 6d 6f 72 65 20 67 65 lexible, more ge
0220: 6e 65 72 69 63 20 61 6e 64 20 6d 6f 72 65 20 65 neric and more e
0230: 66 66 69 63 69 65 6e 74 29 20 61 70 70 72 6f 61 fficient) approa
0240: 63 68 20 66 6f 72 20 65 76 61 6c 75 61 74 69 6e ch for evaluatin
0250: 67 20 73 70 61 74 69 61 6c 20 72 65 6c 61 74 69 g spatial relati
0260: 6f 6e 73 68 69 70 73 20 63 6f 75 6c 64 20 62 65 onships could be
0270: 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20 66 6f based on the fo
0280: 6c 6c 6f 77 69 6e 67 20 74 77 6f 20 3c 62 3e 3c llowing two <b><
0290: 69 3e 61 64 76 61 6e 63 65 64 3c 2f 69 3e 3c 2f i>advanced</i></
02a0: 62 3e 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 b> SQL functions
02b0: 3a 0d 0a 3c 75 6c 3e 0d 0a 3c 6c 69 3e 3c 62 3e :..<ul>..<li><b>
02c0: 53 54 5f 52 65 6c 61 74 65 20 28 3c 2f 62 3e 20 ST_Relate (</b>
02d0: 3c 69 3e 67 31 3c 2f 69 3e 20 47 65 6f 6d 65 74 <i>g1</i> Geomet
02e0: 72 79 20 2c 20 3c 69 3e 67 32 3c 2f 69 3e 20 47 ry , <i>g2</i> G
02f0: 65 6f 6d 65 74 72 79 20 3c 62 3e 29 20 3a 3c 2f eometry <b>) :</
0300: 62 3e 20 3c 69 3e 69 6e 74 65 72 73 65 63 74 69 b> <i>intersecti
0310: 6f 6e 5f 6d 61 74 72 69 78 3c 2f 69 3e 20 54 65 on_matrix</i> Te
0320: 78 74 3c 62 72 3e 0d 0a 54 68 69 73 20 66 69 72 xt<br>..This fir
0330: 73 74 20 66 75 6e 63 74 69 6f 6e 20 77 69 6c 6c st function will
0340: 20 65 76 61 6c 75 61 74 65 20 62 6f 74 68 20 47 evaluate both G
0350: 65 6f 6d 65 74 72 69 65 73 2c 20 74 68 65 6e 20 eometries, then
0360: 72 65 74 75 72 6e 69 6e 67 20 61 20 54 65 78 74 returning a Text
0370: 20 73 74 72 69 6e 67 20 63 6f 72 72 65 73 70 6f string correspo
0380: 6e 64 69 6e 67 20 74 6f 20 61 20 6e 69 6e 65 09 nding to a nine.
0390: 64 69 67 69 74 73 20 73 65 72 69 61 6c 69 7a 65 digits serialize
03a0: 64 20 44 45 2d 39 49 4d 20 3c 62 3e 69 6e 74 65 d DE-9IM <b>inte
03b0: 72 73 65 63 74 69 6f 6e 5f 6d 61 74 72 69 78 3c rsection_matrix<
03c0: 2f 62 3e 20 61 73 20 65 2e 67 2e 20 3c 62 3e 27 /b> as e.g. <b>'
03d0: 30 46 31 46 46 30 46 46 32 27 3c 2f 62 3e 20 6f 0F1FF0FF2'</b> o
03e0: 72 20 3c 62 3e 27 32 31 32 31 30 31 32 31 32 27 r <b>'212101212'
03f0: 3c 2f 62 3e 20 77 69 74 68 20 74 68 65 20 66 6f </b> with the fo
0400: 6c 6c 6f 77 69 6e 67 20 69 6e 74 65 72 70 72 65 llowing interpre
0410: 74 61 74 69 6f 6e 3a 0d 0a 3c 75 6c 3e 0d 0a 3c tation:..<ul>..<
0420: 6c 69 3e 3c 62 3e 46 3c 2f 62 3e 3a 20 74 68 69 li><b>F</b>: thi
0430: 73 20 73 74 61 6e 64 73 20 66 6f 72 20 3c 62 3e s stands for <b>
0440: 46 41 4c 53 45 3c 2f 62 3e 2c 20 69 2e 65 2e 20 FALSE</b>, i.e.
0450: 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e the correspondin
0460: 67 20 69 6e 74 65 72 73 65 63 74 69 6f 6e 20 64 g intersection d
0470: 6f 65 73 6e 27 74 20 6f 63 63 75 72 73 2e 3c 2f oesn't occurs.</
0480: 6c 69 3e 0d 0a 3c 6c 69 3e 3c 62 3e 30 3c 2f 62 li>..<li><b>0</b
0490: 3e 3a 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e >: the correspon
04a0: 64 69 6e 67 20 69 6e 74 65 72 73 65 63 74 69 6f ding intersectio
04b0: 6e 20 69 73 20 61 20 50 6f 69 6e 74 2c 20 69 2e n is a Point, i.
04c0: 65 2e 20 61 20 7a 65 72 6f 2d 64 69 6d 65 6e 73 e. a zero-dimens
04d0: 69 6f 6e 73 20 47 65 6f 6d 65 74 72 79 2e 3c 2f ions Geometry.</
04e0: 6c 69 3e 0d 0a 3c 6c 69 3e 3c 62 3e 31 3c 2f 62 li>..<li><b>1</b
04f0: 3e 3a 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e >: the correspon
0500: 64 69 6e 67 20 69 6e 74 65 72 73 65 63 74 69 6f ding intersectio
0510: 6e 20 69 73 20 61 20 4c 69 6e 65 73 74 72 69 6e n is a Linestrin
0520: 67 2c 20 69 2e 65 2e 20 61 20 20 6f 6e 65 2d 64 g, i.e. a one-d
0530: 69 6d 65 6e 73 69 6f 6e 20 47 65 6f 6d 65 74 72 imension Geometr
0540: 79 2e 3c 2f 6c 69 3e 0d 0a 3c 6c 69 3e 3c 62 3e y.</li>..<li><b>
0550: 32 3c 2f 62 3e 3a 20 74 68 65 20 63 6f 72 72 65 2</b>: the corre
0560: 73 70 6f 6e 64 69 6e 67 20 69 6e 74 65 72 73 65 sponding interse
0570: 63 74 69 6f 6e 20 69 73 20 61 20 50 6f 6c 79 67 ction is a Polyg
0580: 6f 6e 2c 20 69 2e 65 2e 20 61 20 74 77 6f 2d 64 on, i.e. a two-d
0590: 69 6d 65 6e 73 69 6f 6e 73 20 47 65 6f 6d 65 74 imensions Geomet
05a0: 72 79 2e 3c 2f 6c 69 3e 0d 0a 3c 2f 75 6c 3e 3c ry.</li>..</ul><
05b0: 2f 6c 69 3e 0d 0a 3c 6c 69 3e 3c 62 3e 53 54 5f /li>..<li><b>ST_
05c0: 52 65 6c 61 74 65 4d 61 74 63 68 20 28 3c 2f 62 RelateMatch (</b
05d0: 3e 20 3c 69 3e 69 6e 74 65 72 73 65 63 74 69 6f > <i>intersectio
05e0: 6e 5f 6d 61 74 72 69 78 3c 2f 69 3e 20 54 65 78 n_matrix</i> Tex
05f0: 74 20 2c 20 3c 69 3e 72 65 66 65 72 65 6e 63 65 t , <i>reference
0600: 5f 70 61 74 74 65 72 6e 3c 2f 69 3e 20 54 65 78 _pattern</i> Tex
0610: 74 20 3c 62 3e 29 20 3a 3c 2f 62 3e 20 3c 69 3e t <b>) :</b> <i>
0620: 74 72 75 65 5f 6f 72 5f 66 61 6c 73 65 3c 2f 69 true_or_false</i
0630: 3e 20 42 6f 6f 6c 65 61 6e 3c 62 72 3e 0d 0a 54 > Boolean<br>..T
0640: 68 69 73 20 73 65 63 6f 6e 64 20 66 75 6e 63 74 his second funct
0650: 69 6f 6e 20 69 73 20 69 6e 74 65 6e 64 65 64 20 ion is intended
0660: 74 6f 20 65 76 61 6c 75 61 74 65 20 61 6e 20 3c to evaluate an <
0670: 62 3e 69 6e 74 65 72 73 65 63 74 69 6f 6e 20 6d b>intersection m
0680: 61 74 72 69 78 3c 2f 62 3e 20 72 65 74 75 72 6e atrix</b> return
0690: 65 64 20 62 79 20 61 20 70 72 65 76 69 6f 75 73 ed by a previous
06a0: 20 63 61 6c 6c 20 74 6f 20 3c 62 3e 53 54 5f 52 call to <b>ST_R
06b0: 65 6c 61 74 65 28 29 3c 2f 62 3e 20 61 67 61 69 elate()</b> agai
06c0: 6e 73 74 20 61 20 67 69 76 65 6e 20 3c 62 3e 72 nst a given <b>r
06d0: 65 66 65 72 65 6e 63 65 5f 70 61 74 74 65 72 6e eference_pattern
06e0: 3c 2f 62 3e 2c 20 74 68 65 6e 20 72 65 74 75 72 </b>, then retur
06f0: 6e 69 6e 67 20 3c 62 3e 54 52 55 45 3c 2f 62 3e ning <b>TRUE</b>
0700: 20 6f 72 20 3c 62 3e 46 41 4c 53 45 3c 2f 62 3e or <b>FALSE</b>
0710: 2e 3c 62 72 3e 0d 0a 41 20 3c 62 3e 72 65 66 65 .<br>..A <b>refe
0720: 72 65 6e 63 65 5f 70 61 74 74 65 72 6e 3c 2f 62 rence_pattern</b
0730: 3e 20 61 73 20 65 2e 67 2e 20 3c 62 3e 27 54 2a > as e.g. <b>'T*
0740: 46 2a 2a 46 46 46 2a 27 3c 2f 62 3e 20 6f 72 20 F**FFF*'</b> or
0750: 3c 62 3e 27 30 2a 2a 2a 2a 2a 2a 2a 2a 27 3c 2f <b>'0********'</
0760: 62 3e 20 20 73 69 6d 70 6c 79 20 69 73 20 79 65 b> simply is ye
0770: 74 20 61 6e 6f 74 68 65 72 20 6e 69 6e 65 20 64 t another nine d
0780: 69 67 69 74 73 20 73 65 72 69 61 6c 69 7a 65 64 igits serialized
0790: 20 44 45 2d 39 49 4d 20 6d 61 74 72 69 78 20 61 DE-9IM matrix a
07a0: 64 6f 70 74 69 6e 67 20 61 20 73 6c 69 67 68 74 dopting a slight
07b0: 6c 79 20 63 68 61 6e 67 65 64 20 73 79 6e 74 61 ly changed synta
07c0: 78 3a 0d 0a 3c 75 6c 3e 0d 0a 3c 6c 69 3e 3c 62 x:..<ul>..<li><b
07d0: 3e 46 3c 2f 62 3e 2c 20 3c 62 3e 30 3c 2f 62 3e >F</b>, <b>0</b>
07e0: 2c 20 3c 62 3e 31 3c 2f 62 3e 20 61 6e 64 20 3c , <b>1</b> and <
07f0: 62 3e 32 3c 2f 62 3e 3a 20 73 61 6d 65 20 69 6e b>2</b>: same in
0800: 74 65 72 70 72 65 74 61 74 69 6f 6e 20 61 73 20 terpretation as
0810: 62 65 66 6f 72 65 2e 3c 2f 6c 69 3e 0d 0a 3c 6c before.</li>..<l
0820: 69 3e 3c 62 3e 54 3c 2f 62 3e 3a 20 74 68 69 73 i><b>T</b>: this
0830: 20 73 74 61 6e 64 73 20 66 6f 72 20 3c 62 3e 54 stands for <b>T
0840: 52 55 45 3c 2f 62 3e 2c 20 69 2e 65 2e 20 74 68 RUE</b>, i.e. th
0850: 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 e corresponding
0860: 69 6e 74 65 72 73 65 63 74 69 6f 6e 20 69 73 20 intersection is
0870: 65 78 70 65 63 74 65 64 20 74 6f 20 6f 63 63 75 expected to occu
0880: 72 2c 20 62 75 74 20 69 74 73 20 73 70 65 63 69 r, but its speci
0890: 66 69 63 20 6e 61 74 75 72 65 20 64 6f 65 73 6e fic nature doesn
08a0: 27 74 20 6d 61 74 74 65 72 73 2e 3c 2f 6c 69 3e 't matters.</li>
08b0: 0d 0a 3c 6c 69 3e 3c 62 3e 2a 3c 2f 62 3e 3a 20 ..<li><b>*</b>:
08c0: 61 6e 20 61 73 74 65 72 69 73 6b 20 69 73 20 61 an asterisk is a
08d0: 20 3c 62 3e 6d 61 73 6b 20 73 69 67 6e 3c 2f 62 <b>mask sign</b
08e0: 3e 20 61 6e 64 20 61 6c 77 61 79 73 20 6d 65 61 > and always mea
08f0: 6e 73 20 3c 62 3e 3c 69 3e 69 67 6e 6f 72 65 20 ns <b><i>ignore
0900: 2f 20 64 6f 6e 27 74 20 63 61 72 65 20 2f 20 73 / don't care / s
0910: 6b 69 70 3c 2f 69 3e 3c 2f 62 3e 20 74 68 69 73 kip</i></b> this
0920: 20 6d 61 74 72 69 78 20 63 65 6c 6c 2e 3c 2f 6c matrix cell.</l
0930: 69 3e 0d 0a 3c 2f 75 6c 3e 3c 2f 6c 69 3e 0d 0a i>..</ul></li>..
0940: 3c 6c 69 3e 3c 75 3e 4e 6f 74 65 3c 2f 75 3e 3a <li><u>Note</u>:
0950: 20 3c 62 3e 53 54 5f 52 65 6c 61 74 65 28 29 3c <b>ST_Relate()<
0960: 2f 62 3e 20 69 73 20 61 20 63 6f 6d 70 75 74 61 /b> is a computa
0970: 74 69 6f 6e 61 6c 6c 79 20 68 65 61 76 79 20 66 tionally heavy f
0980: 75 6e 63 74 69 6f 6e 20 28 65 78 61 63 74 6c 79 unction (exactly
0990: 20 20 61 73 20 3c 62 3e 53 54 5f 45 71 75 61 6c as <b>ST_Equal
09a0: 73 28 29 3c 2f 62 3e 2c 20 3c 62 3e 53 54 5f 43 s()</b>, <b>ST_C
09b0: 72 6f 73 73 65 73 28 29 3c 2f 62 3e 20 61 6e 64 rosses()</b> and
09c0: 20 61 6c 69 6b 65 20 61 72 65 29 3b 20 09 09 09 alike are); ...
09d0: 6f 6e 20 74 68 65 20 6f 74 68 65 72 20 68 61 6e on the other han
09e0: 64 20 3c 62 3e 53 54 5f 52 65 6c 61 74 65 4d 61 d <b>ST_RelateMa
09f0: 74 63 68 28 29 3c 2f 62 3e 20 69 73 20 61 20 76 tch()</b> is a v
0a00: 65 72 79 20 6c 69 67 68 74 20 61 6e 64 20 71 75 ery light and qu
0a10: 69 63 6b 20 66 75 6e 63 74 69 6f 6e 2e 3c 62 72 ick function.<br
0a20: 3e 0d 0a 53 6f 20 77 68 65 6e 20 79 6f 75 20 68 >..So when you h
0a30: 61 76 65 20 74 6f 20 63 68 65 63 6b 20 66 6f 72 ave to check for
0a40: 20 6d 6f 72 65 20 74 68 61 6e 20 61 20 73 69 6e more than a sin
0a50: 67 6c 65 20 72 65 6c 61 74 69 6f 6e 73 68 69 70 gle relationship
0a60: 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d at the same tim
0a70: 65 2c 20 63 61 6c 6c 69 6e 67 20 6a 75 73 74 20 e, calling just
0a80: 6f 6e 63 65 20 3c 62 3e 53 54 5f 52 65 6c 61 74 once <b>ST_Relat
0a90: 65 28 29 3c 2f 62 3e 20 61 6e 64 20 74 68 65 6e e()</b> and then
0aa0: 20 65 76 61 6c 75 61 74 69 6e 67 20 65 61 63 68 evaluating each
0ab0: 20 73 70 61 74 69 61 6c 20 72 65 6c 61 74 69 6f spatial relatio
0ac0: 6e 73 68 69 70 20 62 79 20 63 61 6c 6c 69 6e 67 nship by calling
0ad0: 20 6d 6f 72 65 20 74 69 6d 65 73 20 3c 62 3e 53 more times <b>S
0ae0: 54 5f 52 65 6c 61 74 65 4d 61 74 63 68 28 29 3c T_RelateMatch()<
0af0: 2f 62 3e 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 /b> will always
0b00: 65 6e 73 75 72 65 20 6e 6f 74 69 63 65 61 62 6c ensure noticeabl
0b10: 79 20 66 61 73 74 65 72 20 70 65 72 66 6f 72 6d y faster perform
0b20: 61 6e 63 65 73 2e 3c 2f 6c 69 3e 0d 0a 3c 6c 69 ances.</li>..<li
0b30: 3e 3c 75 3e 43 6f 6e 63 6c 75 73 69 6f 6e 3c 2f ><u>Conclusion</
0b40: 75 3e 3a 20 75 73 69 6e 67 20 74 68 65 20 71 75 u>: using the qu
0b50: 69 72 6b 79 20 61 6e 64 20 61 77 6b 77 61 72 64 irky and awkward
0b60: 20 44 45 2d 39 49 4d 20 6d 6f 64 65 6c 20 73 75 DE-9IM model su
0b70: 72 65 6c 79 20 69 73 20 6e 65 69 74 68 65 72 20 rely is neither
0b80: 65 61 73 79 20 6f 72 20 69 6e 74 75 69 74 69 76 easy or intuitiv
0b90: 65 2e 3c 62 72 3e 0d 0a 54 68 65 20 66 6f 6c 6c e.<br>..The foll
0ba0: 6f 77 69 6e 67 20 74 61 62 6c 65 20 69 73 20 73 owing table is s
0bb0: 70 65 63 69 66 69 63 61 6c 6c 79 20 69 6e 74 65 pecifically inte
0bc0: 6e 64 65 64 20 74 6f 20 6d 61 6b 65 20 74 68 65 nded to make the
0bd0: 20 75 73 61 67 65 20 6f 66 20 62 6f 74 68 20 3c usage of both <
0be0: 62 3e 53 54 5f 52 65 6c 61 74 65 28 29 3c 2f 62 b>ST_Relate()</b
0bf0: 3e 20 61 6e 64 20 3c 62 3e 53 54 5f 52 65 6c 61 > and <b>ST_Rela
0c00: 74 65 4d 61 74 63 68 28 29 3c 2f 62 3e 20 65 61 teMatch()</b> ea
0c10: 73 69 65 72 2c 20 69 6e 20 61 20 72 65 61 73 6f sier, in a reaso
0c20: 6e 61 62 6c 79 20 70 61 69 6e 6c 65 73 73 20 77 nably painless w
0c30: 61 79 2e 0d 0a 3c 2f 75 6c 3e 3c 2f 6c 69 3e 0d ay...</ul></li>.
0c40: 0a 3c 2f 6f 6c 3e 0d 0a 3c 74 61 62 6c 65 20 63 .</ol>..<table c
0c50: 65 6c 6c 73 70 61 63 69 6e 67 3d 22 34 22 20 63 ellspacing="4" c
0c60: 65 6c 6c 70 61 64 64 69 6e 67 3d 22 36 22 20 62 ellpadding="6" b
0c70: 6f 72 64 65 72 3d 22 31 22 20 62 67 63 6f 6c 6f order="1" bgcolo
0c80: 72 3d 22 23 66 30 66 66 66 30 22 3e 0d 0a 3c 74 r="#f0fff0">..<t
0c90: 72 3e 3c 74 68 20 62 67 63 6f 6c 6f 72 3d 22 23 r><th bgcolor="#
0ca0: 66 66 63 30 30 30 22 3e 53 51 4c 20 66 75 6e 63 ffc000">SQL func
0cb0: 74 69 6f 6e 3c 2f 74 68 3e 3c 74 68 20 62 67 63 tion</th><th bgc
0cc0: 6f 6c 6f 72 3d 22 23 66 66 63 30 30 30 22 3e 52 olor="#ffc000">R
0cd0: 65 66 65 72 65 6e 63 65 20 50 61 74 74 65 72 6e eference Pattern
0ce0: 3c 2f 74 68 3e 3c 2f 74 72 3e 0d 0a 3c 74 72 3e </th></tr>..<tr>
0cf0: 3c 74 64 3e 3c 62 3e 53 54 5f 45 71 75 61 6c 73 <td><b>ST_Equals
0d00: 28 20 3c 2f 62 3e 3c 69 3e 67 31 3c 2f 69 3e 2c ( </b><i>g1</i>,
0d10: 20 3c 69 3e 67 32 3c 2f 69 3e 20 3c 62 3e 29 3c <i>g2</i> <b>)<
0d20: 2f 62 3e 3c 2f 74 64 3e 3c 74 64 20 61 6c 69 67 /b></td><td alig
0d30: 6e 3d 22 63 65 6e 74 65 72 22 3e 3c 62 3e 54 2a n="center"><b>T*
0d40: 46 2a 2a 46 46 46 2a 3c 2f 62 3e 3c 2f 74 64 3e F**FFF*</b></td>
0d50: 3c 2f 74 72 3e 0d 0a 3c 74 72 3e 3c 74 64 3e 3c </tr>..<tr><td><
0d60: 62 3e 53 54 5f 44 69 73 6a 6f 69 6e 74 28 20 3c b>ST_Disjoint( <
0d70: 2f 62 3e 3c 69 3e 67 31 3c 2f 69 3e 2c 20 3c 69 /b><i>g1</i>, <i
0d80: 3e 67 32 3c 2f 69 3e 20 3c 62 3e 29 3c 2f 62 3e >g2</i> <b>)</b>
0d90: 3c 2f 74 64 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 </td><td align="
0da0: 63 65 6e 74 65 72 22 3e 3c 62 3e 46 46 2a 46 46 center"><b>FF*FF
0db0: 2a 2a 2a 2a 3c 2f 62 3e 3c 2f 74 64 3e 3c 2f 74 ****</b></td></t
0dc0: 72 3e 0d 0a 3c 74 72 3e 3c 74 64 20 72 6f 77 73 r>..<tr><td rows
0dd0: 70 61 6e 3d 22 33 22 3e 3c 62 3e 53 54 5f 54 6f pan="3"><b>ST_To
0de0: 75 63 68 65 73 28 20 3c 2f 62 3e 3c 69 3e 67 31 uches( </b><i>g1
0df0: 3c 2f 69 3e 2c 20 3c 69 3e 67 32 3c 2f 69 3e 20 </i>, <i>g2</i>
0e00: 3c 62 3e 29 3c 2f 62 3e 3c 2f 74 64 3e 3c 74 64 <b>)</b></td><td
0e10: 20 61 6c 69 67 6e 3d 22 63 65 6e 74 65 72 22 3e align="center">
0e20: 3c 62 3e 46 54 2a 2a 2a 2a 2a 2a 2a 3c 2f 62 3e <b>FT*******</b>
0e30: 3c 2f 74 64 3e 3c 2f 74 72 3e 0d 0a 3c 74 72 3e </td></tr>..<tr>
0e40: 3c 74 64 20 61 6c 69 67 6e 3d 22 63 65 6e 74 65 <td align="cente
0e50: 72 22 3e 3c 62 3e 46 2a 2a 54 2a 2a 2a 2a 2a 3c r"><b>F**T*****<
0e60: 2f 62 3e 3c 2f 74 64 3e 3c 2f 74 72 3e 0d 0a 3c /b></td></tr>..<
0e70: 74 72 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 63 65 tr><td align="ce
0e80: 6e 74 65 72 22 3e 3c 62 3e 46 2a 2a 2a 54 2a 2a nter"><b>F***T**
0e90: 2a 2a 3c 2f 62 3e 3c 2f 74 64 3e 3c 2f 74 72 3e **</b></td></tr>
0ea0: 0d 0a 3c 74 72 3e 3c 74 64 3e 3c 62 3e 53 54 5f ..<tr><td><b>ST_
0eb0: 57 69 74 68 69 6e 28 20 3c 2f 62 3e 3c 69 3e 67 Within( </b><i>g
0ec0: 31 3c 2f 69 3e 2c 20 3c 69 3e 67 32 3c 2f 69 3e 1</i>, <i>g2</i>
0ed0: 20 3c 62 3e 29 3c 2f 62 3e 3c 2f 74 64 3e 3c 74 <b>)</b></td><t
0ee0: 64 20 61 6c 69 67 6e 3d 22 63 65 6e 74 65 72 22 d align="center"
0ef0: 3e 3c 62 3e 54 2a 46 2a 2a 46 2a 2a 2a 3c 2f 62 ><b>T*F**F***</b
0f00: 3e 3c 2f 74 64 3e 3c 2f 74 72 3e 0d 0a 3c 74 72 ></td></tr>..<tr
0f10: 3e 3c 74 64 20 72 6f 77 73 70 61 6e 3d 22 32 22 ><td rowspan="2"
0f20: 3e 3c 62 3e 53 54 5f 4f 76 65 72 6c 61 70 73 28 ><b>ST_Overlaps(
0f30: 20 3c 2f 62 3e 3c 69 3e 67 31 3c 2f 69 3e 2c 20 </b><i>g1</i>,
0f40: 3c 69 3e 67 32 3c 2f 69 3e 20 3c 62 3e 29 3c 2f <i>g2</i> <b>)</
0f50: 62 3e 3c 2f 74 64 3e 3c 74 64 20 61 6c 69 67 6e b></td><td align
0f60: 3d 22 63 65 6e 74 65 72 22 3e 3c 62 3e 54 2a 54 ="center"><b>T*T
0f70: 2a 2a 2a 54 2a 2a 3c 2f 62 3e 3c 2f 74 64 3e 3c ***T**</b></td><
0f80: 2f 74 72 3e 0d 0a 3c 74 72 3e 3c 74 64 20 61 6c /tr>..<tr><td al
0f90: 69 67 6e 3d 22 63 65 6e 74 65 72 22 3e 3c 62 3e ign="center"><b>
0fa0: 31 2a 54 2a 2a 2a 54 2a 2a 3c 2f 62 3e 3c 2f 74 1*T***T**</b></t
0fb0: 64 3e 3c 2f 74 72 3e 0d 0a 3c 74 72 3e 3c 74 64 d></tr>..<tr><td
0fc0: 20 72 6f 77 73 70 61 6e 3d 22 33 22 3e 3c 62 3e rowspan="3"><b>
0fd0: 53 54 5f 43 72 6f 73 73 65 73 28 20 3c 2f 62 3e ST_Crosses( </b>
0fe0: 3c 69 3e 67 31 3c 2f 69 3e 2c 20 3c 69 3e 67 32 <i>g1</i>, <i>g2
0ff0: 3c 2f 69 3e 20 3c 62 3e 29 3c 2f 62 3e 3c 2f 74 </i> <b>)</b></t
1000: 64 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 63 65 6e d><td align="cen
1010: 74 65 72 22 3e 3c 62 3e 54 2a 54 2a 2a 2a 2a 2a ter"><b>T*T*****
1020: 2a 3c 2f 62 3e 3c 2f 74 64 3e 3c 2f 74 72 3e 0d *</b></td></tr>.
1030: 0a 3c 74 72 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 .<tr><td align="
1040: 63 65 6e 74 65 72 22 3e 3c 62 3e 54 2a 2a 2a 2a center"><b>T****
1050: 2a 54 2a 2a 3c 2f 62 3e 3c 2f 74 64 3e 3c 2f 74 *T**</b></td></t
1060: 72 3e 0d 0a 3c 74 72 3e 3c 74 64 20 61 6c 69 67 r>..<tr><td alig
1070: 6e 3d 22 63 65 6e 74 65 72 22 3e 3c 62 3e 30 2a n="center"><b>0*
1080: 2a 2a 2a 2a 2a 2a 2a 3c 2f 62 3e 3c 2f 74 64 3e *******</b></td>
1090: 3c 2f 74 72 3e 0d 0a 3c 74 72 3e 3c 74 64 20 72 </tr>..<tr><td r
10a0: 6f 77 73 70 61 6e 3d 22 34 22 3e 3c 62 3e 53 54 owspan="4"><b>ST
10b0: 5f 49 6e 74 65 72 73 65 63 74 73 28 20 3c 2f 62 _Intersects( </b
10c0: 3e 3c 69 3e 67 31 3c 2f 69 3e 2c 20 3c 69 3e 67 ><i>g1</i>, <i>g
10d0: 32 3c 2f 69 3e 20 3c 62 3e 29 3c 2f 62 3e 3c 2f 2</i> <b>)</b></
10e0: 74 64 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 63 65 td><td align="ce
10f0: 6e 74 65 72 22 3e 3c 62 3e 54 2a 2a 2a 2a 2a 2a nter"><b>T******
1100: 2a 2a 3c 2f 62 3e 3c 2f 74 64 3e 3c 2f 74 72 3e **</b></td></tr>
1110: 0d 0a 3c 74 72 3e 3c 74 64 20 61 6c 69 67 6e 3d ..<tr><td align=
1120: 22 63 65 6e 74 65 72 22 3e 3c 62 3e 2a 54 2a 2a "center"><b>*T**
1130: 2a 2a 2a 2a 2a 3c 2f 62 3e 3c 2f 74 64 3e 3c 2f *****</b></td></
1140: 74 72 3e 0d 0a 3c 74 72 3e 3c 74 64 20 61 6c 69 tr>..<tr><td ali
1150: 67 6e 3d 22 63 65 6e 74 65 72 22 3e 3c 62 3e 2a gn="center"><b>*
1160: 2a 2a 54 2a 2a 2a 2a 2a 3c 2f 62 3e 3c 2f 74 64 **T*****</b></td
1170: 3e 3c 2f 74 72 3e 0d 0a 3c 74 72 3e 3c 74 64 20 ></tr>..<tr><td
1180: 61 6c 69 67 6e 3d 22 63 65 6e 74 65 72 22 3e 3c align="center"><
1190: 62 3e 2a 2a 2a 2a 54 2a 2a 2a 2a 3c 2f 62 3e 3c b>****T****</b><
11a0: 2f 74 64 3e 3c 2f 74 72 3e 0d 0a 3c 74 72 3e 3c /td></tr>..<tr><
11b0: 74 64 3e 3c 62 3e 53 54 5f 43 6f 6e 74 61 69 6e td><b>ST_Contain
11c0: 73 28 20 3c 2f 62 3e 3c 69 3e 67 31 3c 2f 69 3e s( </b><i>g1</i>
11d0: 2c 20 3c 69 3e 67 32 3c 2f 69 3e 20 3c 62 3e 29 , <i>g2</i> <b>)
11e0: 3c 2f 62 3e 3c 2f 74 64 3e 3c 74 64 20 61 6c 69 </b></td><td ali
11f0: 67 6e 3d 22 63 65 6e 74 65 72 22 3e 3c 62 3e 54 gn="center"><b>T
1200: 2a 2a 2a 2a 2a 46 46 2a 3c 2f 62 3e 3c 2f 74 64 *****FF*</b></td
1210: 3e 3c 2f 74 72 3e 0d 0a 3c 74 72 3e 3c 74 64 20 ></tr>..<tr><td
1220: 72 6f 77 73 70 61 6e 3d 22 34 22 3e 3c 62 3e 53 rowspan="4"><b>S
1230: 54 5f 43 6f 76 65 72 73 28 20 3c 2f 62 3e 3c 69 T_Covers( </b><i
1240: 3e 67 31 3c 2f 69 3e 2c 20 3c 69 3e 67 32 3c 2f >g1</i>, <i>g2</
1250: 69 3e 20 3c 62 3e 29 3c 2f 62 3e 3c 2f 74 64 3e i> <b>)</b></td>
1260: 3c 74 64 20 61 6c 69 67 6e 3d 22 63 65 6e 74 65 <td align="cente
1270: 72 22 3e 3c 62 3e 54 2a 2a 2a 2a 2a 46 46 2a 3c r"><b>T*****FF*<
1280: 2f 62 3e 3c 2f 74 64 3e 3c 2f 74 72 3e 0d 0a 3c /b></td></tr>..<
1290: 74 72 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 63 65 tr><td align="ce
12a0: 6e 74 65 72 22 3e 3c 62 3e 2a 54 2a 2a 2a 2a 46 nter"><b>*T****F
12b0: 46 2a 3c 2f 62 3e 3c 2f 74 64 3e 3c 2f 74 72 3e F*</b></td></tr>
12c0: 0d 0a 3c 74 72 3e 3c 74 64 20 61 6c 69 67 6e 3d ..<tr><td align=
12d0: 22 63 65 6e 74 65 72 22 3e 3c 62 3e 2a 2a 2a 54 "center"><b>***T
12e0: 2a 2a 46 46 2a 3c 2f 62 3e 3c 2f 74 64 3e 3c 2f **FF*</b></td></
12f0: 74 72 3e 0d 0a 3c 74 72 3e 3c 74 64 20 61 6c 69 tr>..<tr><td ali
1300: 67 6e 3d 22 63 65 6e 74 65 72 22 3e 3c 62 3e 2a gn="center"><b>*
1310: 2a 2a 2a 54 2a 46 46 2a 3c 2f 62 3e 3c 2f 74 64 ***T*FF*</b></td
1320: 3e 3c 2f 74 72 3e 0d 0a 3c 74 72 3e 3c 74 64 20 ></tr>..<tr><td
1330: 72 6f 77 73 70 61 6e 3d 22 34 22 3e 3c 62 3e 53 rowspan="4"><b>S
1340: 54 5f 43 6f 76 65 72 65 64 42 79 28 20 3c 2f 62 T_CoveredBy( </b
1350: 3e 3c 69 3e 67 31 3c 2f 69 3e 2c 20 3c 69 3e 67 ><i>g1</i>, <i>g
1360: 32 3c 2f 69 3e 20 3c 62 3e 29 3c 2f 62 3e 3c 2f 2</i> <b>)</b></
1370: 74 64 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 63 65 td><td align="ce
1380: 6e 74 65 72 22 3e 3c 62 3e 54 2a 46 2a 2a 46 2a nter"><b>T*F**F*
1390: 2a 2a 3c 2f 62 3e 3c 2f 74 64 3e 3c 2f 74 72 3e **</b></td></tr>
13a0: 0d 0a 3c 74 72 3e 3c 74 64 20 61 6c 69 67 6e 3d ..<tr><td align=
13b0: 22 63 65 6e 74 65 72 22 3e 3c 62 3e 2a 54 46 2a "center"><b>*TF*
13c0: 2a 46 2a 2a 2a 3c 2f 62 3e 3c 2f 74 64 3e 3c 2f *F***</b></td></
13d0: 74 72 3e 0d 0a 3c 74 72 3e 3c 74 64 20 61 6c 69 tr>..<tr><td ali
13e0: 67 6e 3d 22 63 65 6e 74 65 72 22 3e 3c 62 3e 2a gn="center"><b>*
13f0: 2a 46 54 2a 46 2a 2a 2a 3c 2f 62 3e 3c 2f 74 64 *FT*F***</b></td
1400: 3e 3c 2f 74 72 3e 0d 0a 3c 74 72 3e 3c 74 64 20 ></tr>..<tr><td
1410: 61 6c 69 67 6e 3d 22 63 65 6e 74 65 72 22 3e 3c align="center"><
1420: 62 3e 2a 2a 46 2a 54 46 2a 2a 2a 3c 2f 62 3e 3c b>**F*TF***</b><
1430: 2f 74 64 3e 3c 2f 74 72 3e 0d 0a 3c 2f 74 61 62 /td></tr>..</tab
1440: 6c 65 3e 3c 62 72 3e 0d 0a 46 75 72 74 68 65 72 le><br>..Further
1450: 20 75 73 65 66 75 6c 20 72 65 61 64 69 6e 67 73 useful readings
1460: 3a 0d 0a 3c 75 6c 3e 0d 0a 3c 6c 69 3e 61 20 67 :..<ul>..<li>a g
1470: 65 6e 65 72 61 6c 20 69 6e 74 72 6f 64 75 63 74 eneral introduct
1480: 69 6f 6e 20 74 6f 20 3c 61 20 68 72 65 66 3d 22 ion to <a href="
1490: 68 74 74 70 73 3a 2f 2f 65 6e 2e 77 69 6b 69 70 https://en.wikip
14a0: 65 64 69 61 2e 6f 72 67 2f 77 69 6b 69 2f 44 45 edia.org/wiki/DE
14b0: 2d 39 49 4d 22 3e 44 45 2d 39 49 4d 3c 2f 61 3e -9IM">DE-9IM</a>
14c0: 3c 2f 6c 69 3e 0d 0a 3c 6c 69 3e 61 20 77 65 6c </li>..<li>a wel
14d0: 6c 2d 77 72 69 74 74 65 6e 20 64 6f 63 75 6d 65 l-written docume
14e0: 6e 74 61 74 69 6f 6e 20 66 72 6f 6d 20 61 20 3c ntation from a <
14f0: 61 20 68 72 65 66 3d 22 68 74 74 70 3a 2f 2f 65 a href="http://e
1500: 64 6e 64 6f 63 2e 65 73 72 69 2e 63 6f 6d 2f 61 dndoc.esri.com/a
1510: 72 63 73 64 65 2f 39 2e 30 2f 67 65 6e 65 72 61 rcsde/9.0/genera
1520: 6c 5f 74 6f 70 69 63 73 2f 75 6e 64 65 72 73 74 l_topics/underst
1530: 61 6e 64 5f 73 70 61 74 69 61 6c 5f 72 65 6c 61 and_spatial_rela
1540: 74 69 6f 6e 73 2e 68 74 6d 22 3e 50 72 6f 70 72 tions.htm">Propr
1550: 69 65 74 61 72 79 20 53 57 20 56 65 6e 64 6f 72 ietary SW Vendor
1560: 3c 2f 61 3e 3c 2f 6c 69 3e 0d 0a 3c 2f 75 6c 3e </a></li>..</ul>
1570: 3c 62 72 3e 0d 0a 3c 68 72 3e 0d 0a 3c 68 33 3e <br>..<hr>..<h3>
1580: 41 20 70 72 61 63 74 69 63 61 6c 20 65 78 61 6d A practical exam
1590: 70 6c 65 20 28 61 6e 64 20 72 65 6c 61 74 65 64 ple (and related
15a0: 20 6f 62 6a 65 63 74 69 76 65 20 74 69 6d 69 6e objective timin
15b0: 67 73 29 3c 2f 68 33 3e 0d 0a 3c 76 65 72 62 61 gs)</h3>..<verba
15c0: 74 69 6d 3e 0d 0a 53 45 4c 45 43 54 20 6d 2e 6e tim>..SELECT m.n
15d0: 61 6d 65 20 41 53 20 6d 75 6e 69 63 69 70 61 6c ame AS municipal
15e0: 69 74 79 2c 20 70 2e 6e 61 6d 65 20 41 53 20 70 ity, p.name AS p
15f0: 72 6f 76 69 6e 63 65 2c 0d 0a 20 20 20 20 53 54 rovince,.. ST
1600: 5f 44 69 73 6a 6f 69 6e 74 28 6d 2e 67 65 6f 6d _Disjoint(m.geom
1610: 2c 20 70 2e 67 65 6f 6d 29 20 41 53 20 64 69 73 , p.geom) AS dis
1620: 6a 6f 69 6e 74 2c 0d 0a 20 20 20 20 53 54 5f 54 joint,.. ST_T
1630: 6f 75 63 68 65 73 28 6d 2e 67 65 6f 6d 2c 20 70 ouches(m.geom, p
1640: 2e 67 65 6f 6d 29 20 41 53 20 74 6f 75 63 68 65 .geom) AS touche
1650: 73 2c 0d 0a 20 20 20 20 53 54 5f 57 69 74 68 69 s,.. ST_Withi
1660: 6e 28 6d 2e 67 65 6f 6d 2c 20 70 2e 67 65 6f 6d n(m.geom, p.geom
1670: 29 20 41 53 20 77 69 74 68 69 6e 2c 0d 0a 20 20 ) AS within,..
1680: 20 20 53 54 5f 49 6e 74 65 72 73 65 63 74 73 28 ST_Intersects(
1690: 6d 2e 67 65 6f 6d 2c 20 70 2e 67 65 6f 6d 29 20 m.geom, p.geom)
16a0: 41 53 20 69 6e 74 65 72 73 65 63 74 73 2c 0d 0a AS intersects,..
16b0: 20 20 20 20 53 54 5f 4f 76 65 72 6c 61 70 73 28 ST_Overlaps(
16c0: 6d 2e 67 65 6f 6d 2c 20 70 2e 67 65 6f 6d 29 20 m.geom, p.geom)
16d0: 41 53 20 6f 76 65 72 6c 61 70 73 2c 0d 0a 20 20 AS overlaps,..
16e0: 20 20 53 54 5f 43 6f 76 65 72 65 64 42 79 28 6d ST_CoveredBy(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 63 6f 76 65 72 65 64 5f 62 79 0d 0a 46 52 S covered_by..FR
1710: 4f 4d 20 6d 75 6e 69 63 69 70 61 6c 69 74 69 65 OM municipalitie
1720: 73 20 41 53 20 6d 2c 20 70 72 6f 76 69 6e 63 65 s AS m, province
1730: 73 20 41 53 20 70 3b 0d 0a 3c 2f 76 65 72 62 61 s AS p;..</verba
1740: 74 69 6d 3e 0d 0a 49 6e 20 74 68 69 73 20 66 69 tim>..In this fi
1750: 72 73 74 20 74 65 73 74 20 49 27 76 65 20 66 6f rst test I've fo
1760: 6c 6c 6f 77 65 64 20 74 68 65 20 6d 6f 73 74 20 llowed the most
1770: 3c 62 3e 63 6c 61 73 73 69 63 61 6c 20 61 70 70 <b>classical app
1780: 72 6f 61 63 68 3c 2f 62 3e 20 74 6f 20 64 65 74 roach</b> to det
1790: 65 72 6d 69 6e 65 20 73 65 76 65 72 61 6c 20 73 ermine several s
17a0: 70 61 74 69 61 6c 20 72 65 6c 61 74 69 6f 6e 73 patial relations
17b0: 68 69 70 73 20 69 6e 74 65 72 63 75 72 72 69 6e hips intercurrin
17c0: 67 20 62 65 74 77 65 65 6e 3a 0d 0a 3c 75 6c 3e g between:..<ul>
17d0: 0d 0a 3c 6c 69 3e 74 68 65 20 3c 62 3e 6d 75 6e ..<li>the <b>mun
17e0: 69 63 69 70 61 6c 69 74 69 65 73 3c 2f 62 3e 20 icipalities</b>
17f0: 74 61 62 6c 65 2c 20 28 4d 75 6c 74 69 50 6f 6c table, (MultiPol
1800: 79 67 6f 6e 73 29 20 63 6f 6e 74 61 69 6e 69 6e ygons) containin
1810: 67 20 74 68 65 20 3c 62 3e 32 37 36 3c 2f 62 3e g the <b>276</b>
1820: 20 4d 75 6e 69 63 69 70 61 6c 69 74 65 73 20 6f Municipalites o
1830: 66 20 54 75 73 63 61 6e 79 2e 3c 2f 6c 69 3e 0d f Tuscany.</li>.
1840: 0a 3c 6c 69 3e 61 6e 64 20 74 68 65 20 3c 62 3e .<li>and the <b>
1850: 70 72 6f 76 69 6e 63 65 73 3c 2f 62 3e 20 74 61 provinces</b> ta
1860: 62 6c 65 2c 20 28 4d 75 6c 74 69 50 6f 6c 79 67 ble, (MultiPolyg
1870: 6f 6e 73 29 20 63 6f 6e 74 61 69 6e 69 6e 67 20 ons) containing
1880: 74 68 65 20 3c 62 3e 31 30 3c 2f 62 3e 20 50 72 the <b>10</b> Pr
1890: 6f 76 69 6e 63 65 73 20 6f 66 20 54 75 73 63 61 ovinces of Tusca
18a0: 6e 79 2e 3c 2f 6c 69 3e 0d 0a 3c 6c 69 3e 74 68 ny.</li>..<li>th
18b0: 69 73 20 69 73 20 61 6e 20 75 6e 73 6f 70 68 69 is is an unsophi
18c0: 73 74 69 63 61 74 65 64 20 71 75 65 72 79 2c 20 sticated query,
18d0: 61 6e 64 20 74 68 65 20 72 65 73 75 6c 74 69 6e and the resultin
18e0: 67 20 3c 62 3e 63 61 72 74 65 73 69 61 6e 20 70 g <b>cartesian p
18f0: 72 6f 64 75 63 74 3c 2f 62 3e 20 6f 66 20 62 6f roduct</b> of bo
1900: 74 68 20 74 61 62 6c 65 73 20 63 6f 6e 74 61 69 th tables contai
1910: 6e 73 20 3c 62 3e 32 2e 37 36 30 3c 2f 62 3e 20 ns <b>2.760</b>
1920: 72 6f 77 73 2e 3c 2f 6c 69 3e 0d 0a 3c 2f 75 6c rows.</li>..</ul
1930: 3e 0d 0a 3c 75 3e 4d 65 61 73 75 72 65 64 20 74 >..<u>Measured t
1940: 69 6d 69 6e 67 3c 2f 75 3e 3a 20 3c 62 3e 36 37 iming</u>: <b>67
1950: 20 73 65 63 73 3c 2f 62 3e 0d 0a 3c 62 72 3e 3c secs</b>..<br><
1960: 62 72 3e 3c 62 72 3e 0d 0a 3c 76 65 72 62 61 74 br><br>..<verbat
1970: 69 6d 3e 0d 0a 43 52 45 41 54 45 20 54 45 4d 50 im>..CREATE TEMP
1980: 4f 52 41 52 59 20 54 41 42 4c 45 20 74 6d 70 5f ORARY TABLE tmp_
1990: 72 65 6c 61 74 65 20 41 53 0d 0a 53 45 4c 45 43 relate AS..SELEC
19a0: 54 20 6d 2e 6e 61 6d 65 20 41 53 20 6d 75 6e 69 T m.name AS muni
19b0: 63 69 70 61 6c 69 74 79 2c 20 70 2e 6e 61 6d 65 cipality, p.name
19c0: 20 41 53 20 70 72 6f 76 69 6e 63 65 2c 20 53 54 AS province, ST
19d0: 5f 52 65 6c 61 74 65 28 6d 2e 67 65 6f 6d 2c 20 _Relate(m.geom,
19e0: 70 2e 67 65 6f 6d 29 20 41 53 20 6d 61 74 72 69 p.geom) AS matri
19f0: 78 0d 0a 46 52 4f 4d 20 6d 75 6e 69 63 69 70 61 x..FROM municipa
1a00: 6c 69 74 69 65 73 20 41 53 20 6d 2c 20 70 72 6f lities AS m, pro
1a10: 76 69 6e 63 65 73 20 41 53 20 70 3b 0d 0a 0d 0a vinces AS p;....
1a20: 53 45 4c 45 43 54 20 6d 75 6e 69 63 69 70 61 6c SELECT municipal
1a30: 69 74 79 2c 20 70 72 6f 76 69 6e 63 65 2c 20 6d ity, province, m
1a40: 61 74 72 69 78 2c 0d 0a 20 20 20 20 53 54 5f 52 atrix,.. ST_R
1a50: 65 6c 61 74 65 4d 61 74 63 68 28 6d 61 74 72 69 elateMatch(matri
1a60: 78 2c 20 27 46 46 2a 46 46 2a 2a 2a 2a 27 29 20 x, 'FF*FF****')
1a70: 41 53 20 64 69 73 6a 6f 69 6e 74 2c 0d 0a 20 20 AS disjoint,..
1a80: 20 20 53 54 5f 52 65 6c 61 74 65 4d 61 74 63 68 ST_RelateMatch
1a90: 28 6d 61 74 72 69 78 2c 20 27 46 54 2a 2a 2a 2a (matrix, 'FT****
1aa0: 2a 2a 2a 27 29 20 4f 52 0d 0a 20 20 20 20 53 54 ***') OR.. ST
1ab0: 5f 52 65 6c 61 74 65 4d 61 74 63 68 28 6d 61 74 _RelateMatch(mat
1ac0: 72 69 78 2c 20 27 46 2a 2a 54 2a 2a 2a 2a 2a 27 rix, 'F**T*****'
1ad0: 29 20 4f 52 0d 0a 20 20 20 20 53 54 5f 52 65 6c ) OR.. ST_Rel
1ae0: 61 74 65 4d 61 74 63 68 28 6d 61 74 72 69 78 2c ateMatch(matrix,
1af0: 20 27 46 2a 2a 2a 54 2a 2a 2a 2a 27 29 20 41 53 'F***T****') AS
1b00: 20 74 6f 75 63 68 65 73 2c 0d 0a 20 20 20 20 53 touches,.. S
1b10: 54 5f 52 65 6c 61 74 65 4d 61 74 63 68 28 6d 61 T_RelateMatch(ma
1b20: 74 72 69 78 2c 20 27 54 2a 46 2a 2a 46 2a 2a 2a trix, 'T*F**F***
1b30: 27 29 20 41 53 20 77 69 74 68 69 6e 2c 0d 0a 20 ') AS within,..
1b40: 20 20 20 53 54 5f 52 65 6c 61 74 65 4d 61 74 63 ST_RelateMatc
1b50: 68 28 6d 61 74 72 69 78 2c 20 27 54 2a 2a 2a 2a h(matrix, 'T****
1b60: 2a 2a 2a 2a 27 29 20 4f 52 0d 0a 20 20 20 20 53 ****') OR.. S
1b70: 54 5f 52 65 6c 61 74 65 4d 61 74 63 68 28 6d 61 T_RelateMatch(ma
1b80: 74 72 69 78 2c 20 27 2a 54 2a 2a 2a 2a 2a 2a 2a trix, '*T*******
1b90: 27 29 20 4f 52 0d 0a 20 20 20 20 53 54 5f 52 65 ') OR.. ST_Re
1ba0: 6c 61 74 65 4d 61 74 63 68 28 6d 61 74 72 69 78 lateMatch(matrix
1bb0: 2c 20 27 2a 2a 2a 54 2a 2a 2a 2a 2a 27 29 20 4f , '***T*****') O
1bc0: 52 0d 0a 20 20 20 20 53 54 5f 52 65 6c 61 74 65 R.. ST_Relate
1bd0: 4d 61 74 63 68 28 6d 61 74 72 69 78 2c 20 27 2a Match(matrix, '*
1be0: 2a 2a 2a 54 2a 2a 2a 2a 27 29 20 41 53 20 69 6e ***T****') AS in
1bf0: 74 65 72 73 65 63 74 73 2c 0d 0a 20 20 20 20 53 tersects,.. S
1c00: 54 5f 52 65 6c 61 74 65 4d 61 74 63 68 28 6d 61 T_RelateMatch(ma
1c10: 74 72 69 78 2c 20 27 54 2a 54 2a 2a 2a 54 2a 2a trix, 'T*T***T**
1c20: 27 29 20 41 53 20 6f 76 65 72 6c 61 70 73 2c 0d ') AS overlaps,.
1c30: 0a 20 20 20 20 53 54 5f 52 65 6c 61 74 65 4d 61 . ST_RelateMa
1c40: 74 63 68 28 6d 61 74 72 69 78 2c 20 27 54 2a 46 tch(matrix, 'T*F
1c50: 2a 2a 46 2a 2a 2a 27 29 20 4f 52 0d 0a 20 20 20 **F***') OR..
1c60: 20 53 54 5f 52 65 6c 61 74 65 4d 61 74 63 68 28 ST_RelateMatch(
1c70: 6d 61 74 72 69 78 2c 20 27 2a 54 46 2a 2a 46 2a matrix, '*TF**F*
1c80: 2a 2a 27 29 20 4f 52 0d 0a 20 20 20 20 53 54 5f **') OR.. ST_
1c90: 52 65 6c 61 74 65 4d 61 74 63 68 28 6d 61 74 72 RelateMatch(matr
1ca0: 69 78 2c 20 27 2a 2a 46 54 2a 46 2a 2a 2a 27 29 ix, '**FT*F***')
1cb0: 20 4f 52 0d 0a 20 20 20 20 53 54 5f 52 65 6c 61 OR.. ST_Rela
1cc0: 74 65 4d 61 74 63 68 28 6d 61 74 72 69 78 2c 20 teMatch(matrix,
1cd0: 27 2a 2a 46 2a 54 46 2a 2a 2a 27 29 20 41 53 20 '**F*TF***') AS
1ce0: 63 6f 76 65 72 65 64 5f 62 79 0d 0a 46 52 4f 4d covered_by..FROM
1cf0: 20 74 6d 70 5f 72 65 6c 61 74 65 3b 0d 0a 3c 2f tmp_relate;..</
1d00: 76 65 72 62 61 74 69 6d 3e 0d 0a 55 73 69 6e 67 verbatim>..Using
1d10: 20 74 68 65 20 73 61 6d 65 20 74 61 62 6c 65 73 the same tables
1d20: 20 61 73 20 62 65 66 6f 72 65 20 69 6e 20 74 68 as before in th
1d30: 69 73 20 73 65 63 6f 6e 64 20 74 65 73 74 2c 20 is second test,
1d40: 62 75 74 20 74 68 69 73 20 74 69 6d 65 20 49 27 but this time I'
1d50: 76 65 20 61 64 6f 70 74 65 64 20 74 68 65 20 61 ve adopted the a
1d60: 6c 74 65 72 6e 61 74 69 76 65 20 61 70 70 72 6f lternative appro
1d70: 61 63 68 20 62 61 73 65 64 20 6f 6e 20 3c 62 3e ach based on <b>
1d80: 53 54 5f 52 65 6c 61 74 65 28 29 3c 2f 62 3e 20 ST_Relate()</b>
1d90: 61 6e 64 20 3c 62 3e 53 54 5f 52 65 6c 61 74 65 and <b>ST_Relate
1da0: 4d 61 74 63 68 28 29 3c 2f 62 3e 2e 3c 62 72 3e Match()</b>.<br>
1db0: 0d 0a 41 66 74 65 72 20 73 6f 6d 65 20 70 72 65 ..After some pre
1dc0: 6c 69 6d 69 6e 61 72 79 20 74 65 73 74 73 2c 20 liminary tests,
1dd0: 69 74 20 71 75 69 63 6b 6c 79 20 65 6d 65 72 67 it quickly emerg
1de0: 65 64 20 74 68 61 74 20 74 68 65 20 53 51 4c 69 ed that the SQLi
1df0: 74 65 20 6f 70 74 69 6d 69 7a 65 72 20 64 6f 65 te optimizer doe
1e00: 73 6e 27 74 20 6c 69 6b 65 20 74 68 65 20 6d 69 sn't like the mi
1e10: 78 74 75 72 65 20 6f 66 20 61 6e 20 69 6e 6e 65 xture of an inne
1e20: 72 20 71 75 65 72 79 20 74 6f 67 65 74 68 65 72 r query together
1e30: 20 77 69 74 68 20 66 75 6e 63 74 69 6f 6e 20 63 with function c
1e40: 61 6c 6c 73 20 6f 6e 20 74 68 65 20 73 61 6d 65 alls on the same
1e50: 20 6f 76 65 72 61 6c 6c 20 71 75 65 72 79 20 28 overall query (
1e60: 72 65 73 75 6c 74 69 6e 67 20 69 6e 20 61 77 66 resulting in awf
1e70: 75 6c 20 74 69 6d 69 6e 67 73 29 2c 20 73 6f 20 ul timings), so
1e80: 49 20 64 75 6c 79 20 73 77 69 74 63 68 65 64 20 I duly switched
1e90: 74 6f 20 61 6e 20 69 6e 64 69 72 65 63 74 20 3c to an indirect <
1ea0: 62 3e 74 77 6f 2d 73 74 65 70 73 20 61 70 70 72 b>two-steps appr
1eb0: 6f 61 63 68 3c 2f 62 3e 3a 0d 0a 3c 75 6c 3e 0d oach</b>:..<ul>.
1ec0: 0a 3c 6c 69 3e 3c 62 3e 73 74 65 70 20 23 31 3c .<li><b>step #1<
1ed0: 2f 62 3e 3a 20 63 6f 6d 70 75 74 69 6e 67 20 3c /b>: computing <
1ee0: 62 3e 53 54 5f 52 65 6c 61 74 65 64 28 29 3c 2f b>ST_Related()</
1ef0: 62 3e 20 61 6e 64 20 73 61 76 69 6e 67 20 74 68 b> and saving th
1f00: 65 20 77 68 6f 6c 65 20 72 65 73 75 6c 74 73 65 e whole resultse
1f10: 74 20 69 6e 74 6f 20 61 20 3c 62 3e 74 65 6d 70 t into a <b>temp
1f20: 6f 72 61 72 79 20 74 61 62 6c 65 3c 2f 62 3e 2e orary table</b>.
1f30: 3c 2f 6c 69 3e 0d 0a 3c 6c 69 3e 3c 62 3e 73 74 </li>..<li><b>st
1f40: 65 70 20 23 32 3c 2f 62 3e 3a 20 63 6f 6d 70 6c ep #2</b>: compl
1f50: 65 74 69 6e 67 20 74 68 65 20 74 61 73 6b 20 62 eting the task b
1f60: 79 20 63 61 6c 6c 69 6e 67 20 3c 62 3e 53 54 5f y calling <b>ST_
1f70: 52 65 6c 61 74 65 4d 61 74 63 68 3c 2f 62 3e 20 RelateMatch</b>
1f80: 6f 6e 20 74 68 65 20 61 62 6f 76 65 20 74 65 6d on the above tem
1f90: 70 6f 72 61 72 79 20 74 61 62 6c 65 2e 3c 2f 6c porary table.</l
1fa0: 69 3e 0d 0a 3c 2f 75 6c 3e 0d 0a 3c 75 3e 4d 65 i>..</ul>..<u>Me
1fb0: 61 73 75 72 65 64 20 74 69 6d 69 6e 67 3c 2f 75 asured timing</u
1fc0: 3e 3a 20 3c 62 3e 32 32 20 73 65 63 73 3c 2f 62 >: <b>22 secs</b
1fd0: 3e 3c 62 72 3e 3c 62 72 3e 0d 0a 3c 75 3e 4e 6f ><br><br>..<u>No
1fe0: 74 65 3c 2f 75 3e 3a 20 74 68 65 20 73 65 63 6f te</u>: the seco
1ff0: 6e 64 20 71 75 65 72 79 20 28 53 54 5f 52 65 6c nd query (ST_Rel
2000: 61 74 65 4d 61 74 63 68 29 20 6f 6e 6c 79 20 72 ateMatch) only r
2010: 65 71 75 69 72 65 64 20 61 20 66 65 77 20 6d 69 equired a few mi
2020: 6c 6c 69 73 65 63 6f 6e 64 73 20 74 6f 20 63 6f lliseconds to co
2030: 6d 70 6c 65 74 65 3b 20 74 68 65 20 72 65 61 6c mplete; the real
2040: 20 63 6f 6d 70 75 74 61 74 69 6f 6e 61 6c 20 6c computational l
2050: 6f 61 64 20 77 61 73 20 65 6e 74 69 72 65 6c 79 oad was entirely
2060: 20 63 6f 6e 66 69 6e 65 64 20 77 69 74 68 69 6e confined within
2070: 20 74 68 65 20 66 69 72 73 74 20 71 75 65 72 79 the first query
2080: 20 28 53 54 5f 52 65 6c 61 74 65 29 2e 3c 62 72 (ST_Relate).<br
2090: 3e 3c 62 72 3e 0d 0a 3c 75 3e 46 69 6e 61 6c 20 ><br>..<u>Final
20a0: 63 6f 6e 63 6c 75 73 69 6f 6e 3c 2f 75 3e 3a 20 conclusion</u>:
20b0: 64 69 72 65 63 74 6c 79 20 75 73 69 6e 67 20 74 directly using t
20c0: 68 65 20 61 77 6b 77 61 72 64 20 44 45 2d 39 49 he awkward DE-9I
20d0: 4d 20 6d 6f 64 65 6c 20 69 73 20 73 6f 6d 65 77 M model is somew
20e0: 61 79 73 20 64 69 66 66 69 63 75 6c 74 2c 20 62 ays difficult, b
20f0: 75 74 20 69 74 20 64 6f 65 73 20 65 6e 73 75 72 ut it does ensur
2100: 65 20 61 6e 20 61 73 74 6f 6e 69 73 68 69 6e 67 e an astonishing
2110: 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 62 6f 6f performance boo
2120: 73 74 2e 3c 62 72 3e 0d 0a 49 74 27 73 20 75 73 st.<br>..It's us
2130: 61 67 65 20 69 73 20 68 69 67 68 6c 79 20 72 65 age is highly re
2140: 63 6f 6d 6d 65 6e 64 65 64 20 77 68 65 6e 20 79 commended when y
2150: 6f 75 20 61 72 65 20 72 65 71 75 69 72 65 64 20 ou are required
2160: 74 6f 20 63 68 65 63 6b 20 6d 6f 72 65 20 74 68 to check more th
2170: 61 6e 20 61 20 73 69 6e 67 6c 65 20 73 70 61 74 an a single spat
2180: 69 61 6c 20 72 65 6c 61 74 69 6f 6e 73 68 69 70 ial relationship
2190: 20 62 65 74 77 65 65 6e 20 74 68 65 20 73 61 6d between the sam
21a0: 65 20 70 61 69 72 20 6f 66 20 47 65 6f 6d 65 74 e pair of Geomet
21b0: 72 69 65 73 2e 0d 0a 3c 62 72 3e 3c 62 72 3e 0a ries...<br><br>.
21c0: 5a 20 32 33 61 61 64 66 38 38 32 64 62 65 33 32 Z 23aadf882dbe32
21d0: 33 66 39 38 35 39 35 33 38 65 34 63 66 37 38 65 3f9859538e4cf78e
21e0: 61 33 0a a3.