Hex Artifact Content
Not logged in

Artifact 22f4f550841c61347fc9eddb86d9cdbc1fe4201c:

Wiki page [About ST_Subdivide()] by sandro 2019-02-16 10:48:06.
0000: 44 20 32 30 31 39 2d 30 32 2d 31 36 54 31 30 3a  D 2019-02-16T10:
0010: 34 38 3a 30 36 2e 37 31 39 0a 4c 20 41 62 6f 75  48:06.719.L Abou
0020: 74 5c 73 53 54 5f 53 75 62 64 69 76 69 64 65 28  t\sST_Subdivide(
0030: 29 0a 50 20 66 63 38 34 32 39 37 38 62 61 30 64  ).P fc842978ba0d
0040: 37 62 39 35 30 36 38 35 39 66 36 38 64 34 65 34  7b9506859f68d4e4
0050: 65 32 64 62 38 36 32 38 64 63 33 61 0a 55 20 73  e2db8628dc3a.U s
0060: 61 6e 64 72 6f 0a 57 20 31 32 36 30 38 0a 3c 74  andro.W 12608.<t
0070: 61 62 6c 65 20 63 65 6c 6c 73 70 61 63 69 6e 67  able cellspacing
0080: 3d 22 31 32 22 20 77 69 64 74 68 3d 22 31 30 30  ="12" width="100
0090: 25 22 3e 0d 0a 3c 74 72 3e 3c 74 64 20 63 6f 6c  %">..<tr><td col
00a0: 73 70 61 6e 3d 22 32 22 3e 0d 0a 3c 74 61 62 6c  span="2">..<tabl
00b0: 65 20 77 69 64 74 68 3d 22 31 30 30 25 22 20 62  e width="100%" b
00c0: 67 63 6f 6c 6f 72 3d 22 23 66 30 66 30 66 38 22  gcolor="#f0f0f8"
00d0: 3e 0d 0a 3c 74 72 3e 3c 74 64 20 61 6c 69 67 6e  >..<tr><td align
00e0: 3d 22 63 65 6e 74 65 72 22 3e 0d 0a 3c 68 31 3e  ="center">..<h1>
00f0: 53 54 5f 53 75 62 64 69 76 69 64 65 28 29 3a 20  ST_Subdivide(): 
0100: 61 20 71 75 69 63 6b 20 69 6e 74 72 6f 3c 2f 68  a quick intro</h
0110: 31 3e 0d 0a 3c 2f 74 64 3e 3c 2f 74 72 3e 3c 2f  1>..</td></tr></
0120: 74 61 62 6c 65 3e 0d 0a 3c 74 61 62 6c 65 20 77  table>..<table w
0130: 69 64 74 68 3d 22 31 30 30 25 22 3e 3c 74 72 3e  idth="100%"><tr>
0140: 0d 0a 3c 74 64 20 77 69 64 74 68 3d 22 33 33 25  ..<td width="33%
0150: 22 20 61 6c 69 67 6e 3d 22 6c 65 66 74 22 3e 3c  " align="left"><
0160: 2f 74 64 3e 0d 0a 3c 74 64 20 61 6c 69 67 6e 3d  /td>..<td align=
0170: 22 63 65 6e 74 65 72 22 3e 3c 61 20 68 72 65 66  "center"><a href
0180: 3d 22 68 74 74 70 73 3a 2f 2f 77 77 77 2e 67 61  ="https://www.ga
0190: 69 61 2d 67 69 73 2e 69 74 2f 66 6f 73 73 69 6c  ia-gis.it/fossil
01a0: 2f 6c 69 62 73 70 61 74 69 61 6c 69 74 65 2f 77  /libspatialite/w
01b0: 69 6b 69 3f 6e 61 6d 65 3d 34 2e 33 2e 30 2b 64  iki?name=4.3.0+d
01c0: 6f 63 22 3e 62 61 63 6b 20 74 6f 20 69 6e 64 65  oc">back to inde
01d0: 78 3c 2f 61 3e 3c 2f 74 64 3e 0d 0a 3c 74 64 20  x</a></td>..<td 
01e0: 77 69 64 74 68 3d 22 33 33 25 22 20 61 6c 69 67  width="33%" alig
01f0: 6e 3d 22 72 69 67 68 74 22 3e 3c 2f 74 64 3e 0d  n="right"></td>.
0200: 0a 3c 2f 74 72 3e 3c 2f 74 61 62 6c 65 3e 3c 62  .</tr></table><b
0210: 72 3e 0d 0a 3c 68 32 3e 41 62 6f 75 74 20 53 54  r>..<h2>About ST
0220: 5f 53 75 62 64 69 76 69 64 65 28 29 3c 2f 68 32  _Subdivide()</h2
0230: 3e 0d 0a 53 69 6e 63 65 20 76 65 72 73 69 6f 6e  >..Since version
0240: 20 3c 62 3e 35 2e 30 2e 30 3c 2f 62 3e 20 53 70   <b>5.0.0</b> Sp
0250: 61 74 69 61 4c 69 74 65 20 73 75 70 70 6f 72 74  atiaLite support
0260: 73 20 3c 62 3e 53 54 5f 53 75 62 64 69 76 69 64  s <b>ST_Subdivid
0270: 65 28 29 3c 2f 62 3e 2c 20 61 6e 20 61 64 76 61  e()</b>, an adva
0280: 6e 63 65 64 20 53 51 4c 20 66 75 6e 63 74 69 6f  nced SQL functio
0290: 6e 20 61 6c 72 65 61 64 79 20 61 76 61 69 6c 61  n already availa
02a0: 62 6c 65 20 6f 6e 20 3c 61 20 68 72 65 66 3d 22  ble on <a href="
02b0: 68 74 74 70 73 3a 2f 2f 70 6f 73 74 67 69 73 2e  https://postgis.
02c0: 6e 65 74 2f 64 6f 63 73 2f 53 54 5f 53 75 62 64  net/docs/ST_Subd
02d0: 69 76 69 64 65 2e 68 74 6d 6c 22 3e 50 6f 73 74  ivide.html">Post
02e0: 47 49 53 3c 2f 61 3e 2e 3c 62 72 3e 0d 0a 54 68  GIS</a>.<br>..Th
02f0: 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
0300: 20 69 73 20 73 74 72 69 63 74 6c 79 20 73 69 6d   is strictly sim
0310: 69 6c 61 72 20 69 6e 20 62 6f 74 68 20 53 70 61  ilar in both Spa
0320: 74 69 61 6c 20 44 42 4d 53 65 73 20 62 65 63 61  tial DBMSes beca
0330: 75 73 65 20 6f 6e 20 50 6f 73 74 47 49 53 20 74  use on PostGIS t
0340: 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 62  he function is b
0350: 75 69 6c 74 20 6f 6e 20 74 68 65 20 74 6f 70 20  uilt on the top 
0360: 6f 66 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20  of the internal 
0370: 3c 62 3e 6c 77 67 65 6f 6d 3c 2f 62 3e 20 6c 69  <b>lwgeom</b> li
0380: 62 72 61 72 79 2c 20 61 6e 64 20 6f 6e 20 53 70  brary, and on Sp
0390: 61 74 69 61 4c 69 74 65 20 69 73 20 62 75 69 6c  atiaLite is buil
03a0: 74 20 6f 6e 20 74 68 65 20 74 6f 70 20 6f 66 20  t on the top of 
03b0: 3c 62 3e 6c 69 62 72 74 74 6f 70 6f 3c 2f 62 3e  <b>librttopo</b>
03c0: 20 74 68 61 74 20 73 69 6d 70 6c 79 20 69 73 20   that simply is 
03d0: 61 20 6d 6f 72 65 20 75 6e 69 76 65 72 73 61 6c  a more universal
03e0: 20 70 6f 72 74 69 6e 67 20 6f 66 20 6c 77 67 65   porting of lwge
03f0: 6f 6d 20 6f 75 74 73 69 64 65 20 50 6f 73 74 47  om outside PostG
0400: 49 53 2e 3c 62 72 3e 3c 62 72 3e 0d 0a 41 20 73  IS.<br><br>..A s
0410: 68 6f 72 74 20 72 61 74 69 6f 6e 61 6c 65 3a 20  hort rationale: 
0420: 70 72 6f 63 65 73 73 69 6e 67 20 68 75 67 65 20  processing huge 
0430: 67 65 6f 6d 65 74 72 69 65 73 20 68 61 76 69 6e  geometries havin
0440: 67 20 61 6e 20 69 6d 70 72 65 73 73 69 76 65 20  g an impressive 
0450: 6e 75 6d 62 65 72 20 6f 66 20 56 65 72 74 69 63  number of Vertic
0460: 65 73 20 28 6d 61 6e 79 20 74 68 6f 75 73 61 6e  es (many thousan
0470: 64 73 20 6f 72 20 65 76 65 6e 20 6d 6f 72 65 29  ds or even more)
0480: 20 69 73 20 61 6e 20 69 6e 74 72 69 6e 73 69 63   is an intrinsic
0490: 61 6c 6c 79 20 73 6c 6f 77 20 70 72 6f 63 65 73  ally slow proces
04a0: 73 2e 3c 62 72 3e 0d 0a 53 75 62 64 69 76 69 64  s.<br>..Subdivid
04b0: 69 6e 67 20 74 68 65 6d 20 69 6e 74 6f 20 6d 61  ing them into ma
04c0: 6e 79 20 73 6d 61 6c 6c 65 72 20 70 61 72 74 73  ny smaller parts
04d0: 20 28 73 74 69 6c 6c 20 70 72 65 73 65 72 76 69   (still preservi
04e0: 6e 67 20 66 75 6c 6c 20 74 6f 70 6f 6c 6f 67 69  ng full topologi
04f0: 63 61 6c 20 63 6f 6e 73 69 73 74 65 6e 63 79 29  cal consistency)
0500: 20 75 73 75 61 6c 6c 79 20 68 65 6c 70 73 20 74   usually helps t
0510: 6f 20 72 65 73 74 6f 72 65 20 61 20 73 61 74 69  o restore a sati
0520: 73 66 79 69 6e 67 20 70 72 6f 63 65 73 73 69 6e  sfying processin
0530: 67 20 73 70 65 65 64 2e 0d 0a 54 68 69 73 20 69  g speed...This i
0540: 73 20 65 78 61 63 74 6c 79 20 74 68 65 20 69 6e  s exactly the in
0550: 74 65 6e 64 65 64 20 73 63 6f 70 65 20 6f 66 20  tended scope of 
0560: 3c 62 3e 53 54 5f 53 75 62 64 69 76 69 64 65 28  <b>ST_Subdivide(
0570: 29 3c 2f 62 3e 3a 0d 0a 3c 75 6c 3e 0d 0a 3c 6c  )</b>:..<ul>..<l
0580: 69 3e 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  i>this function 
0590: 77 69 6c 6c 20 72 65 63 65 69 76 65 20 61 6e 20  will receive an 
05a0: 69 6e 70 75 74 20 67 65 6f 6d 65 74 72 79 20 28  input geometry (
05b0: 6d 61 79 20 77 65 6c 6c 20 62 65 20 61 20 76 65  may well be a ve
05c0: 72 79 20 68 75 67 65 20 6f 6e 65 29 2e 3c 2f 6c  ry huge one).</l
05d0: 69 3e 0d 0a 3c 6c 69 3e 65 61 63 68 20 3c 62 3e  i>..<li>each <b>
05e0: 4c 69 6e 65 73 74 72 69 6e 67 3c 2f 62 3e 20 6f  Linestring</b> o
05f0: 72 20 3c 62 3e 50 6f 6c 79 67 6f 6e 3c 2f 62 3e  r <b>Polygon</b>
0600: 20 66 6f 75 6e 64 20 77 69 74 68 69 6e 20 74 68   found within th
0610: 65 20 69 6e 70 75 74 20 67 65 6f 6d 65 74 72 79  e input geometry
0620: 20 77 69 6c 6c 20 62 65 20 74 68 65 6e 20 70 72   will be then pr
0630: 6f 63 65 73 73 65 64 3a 0d 0a 3c 75 6c 3e 0d 0a  ocessed:..<ul>..
0640: 3c 6c 69 3e 61 6c 6c 20 4c 69 6e 65 73 74 72 69  <li>all Linestri
0650: 6e 67 73 20 6f 72 20 50 6f 6c 79 67 6f 6e 73 20  ngs or Polygons 
0660: 75 73 69 6e 67 20 61 20 6e 75 6d 62 65 72 20 6f  using a number o
0670: 66 20 56 65 72 74 69 63 65 73 20 6c 65 73 73 65  f Vertices lesse
0680: 72 20 6f 72 20 65 71 75 61 6c 20 74 68 61 6e 20  r or equal than 
0690: 74 68 65 20 67 69 76 65 6e 20 74 68 72 65 73 68  the given thresh
06a0: 6f 6c 64 20 77 69 6c 6c 20 62 65 20 72 65 74 75  old will be retu
06b0: 72 6e 65 64 20 61 73 20 74 68 65 79 20 61 72 65  rned as they are
06c0: 2e 3c 2f 6c 69 3e 0d 0a 3c 6c 69 3e 62 75 74 20  .</li>..<li>but 
06d0: 61 6c 6c 20 4c 69 6e 65 73 74 72 69 6e 67 73 20  all Linestrings 
06e0: 6f 72 20 50 6f 6c 79 67 6f 6e 73 20 75 73 69 6e  or Polygons usin
06f0: 67 20 61 6e 20 65 78 63 65 65 64 69 6e 67 20 6e  g an exceeding n
0700: 75 6d 62 65 72 20 6f 66 20 56 65 72 74 69 63 65  umber of Vertice
0710: 73 20 77 69 6c 6c 20 62 65 20 72 65 63 75 72 73  s will be recurs
0720: 69 76 65 6c 79 20 73 70 6c 69 74 2c 20 75 6e 74  ively split, unt
0730: 69 6c 20 74 68 65 79 27 6c 6c 20 62 65 20 72 65  il they'll be re
0740: 70 72 65 73 65 6e 74 65 64 20 62 79 20 61 20 3c  presented by a <
0750: 62 3e 63 6f 6c 6c 65 63 74 69 6f 6e 3c 2f 62 3e  b>collection</b>
0760: 20 6f 66 20 65 6c 65 6d 65 6e 74 61 72 79 20 3c   of elementary <
0770: 62 3e 70 61 72 74 73 3c 2f 62 3e 20 75 73 69 6e  b>parts</b> usin
0780: 67 20 6e 6f 20 6d 6f 72 65 20 74 68 61 6e 20 74  g no more than t
0790: 68 65 20 72 65 71 75 69 72 65 64 20 6e 75 6d 62  he required numb
07a0: 65 72 20 6f 66 20 76 65 72 74 69 63 65 73 2e 3c  er of vertices.<
07b0: 2f 6c 69 3e 0d 0a 3c 2f 75 6c 3e 3c 2f 6c 69 3e  /li>..</ul></li>
07c0: 0d 0a 3c 6c 69 3e 61 74 20 74 68 65 20 65 6e 64  ..<li>at the end
07d0: 20 6f 66 20 74 68 65 20 70 72 6f 63 65 73 73 20   of the process 
07e0: 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 77 69 6c  the function wil
07f0: 6c 20 72 65 74 75 72 6e 20 61 20 63 6f 6c 6c 65  l return a colle
0800: 63 74 69 6f 6e 20 63 6f 6e 74 61 69 6e 69 6e 67  ction containing
0810: 20 61 6c 6c 20 65 6c 65 6d 65 6e 74 61 72 79 20   all elementary 
0820: 70 61 72 74 73 20 28 61 20 3c 62 3e 4d 75 6c 74  parts (a <b>Mult
0830: 69 4c 69 6e 65 73 74 72 69 6e 67 3c 2f 62 3e 20  iLinestring</b> 
0840: 6f 72 20 61 20 3c 62 3e 4d 75 6c 74 69 50 6f 6c  or a <b>MultiPol
0850: 79 67 6f 6e 3c 2f 62 3e 20 64 65 70 65 6e 64 69  ygon</b> dependi
0860: 6e 67 20 6f 6e 20 74 68 65 20 6e 61 74 75 72 65  ng on the nature
0870: 20 6f 66 20 74 68 65 20 69 6e 70 75 74 20 67 65   of the input ge
0880: 6f 6d 65 74 72 79 29 2e 3c 2f 6c 69 3e 0d 0a 3c  ometry).</li>..<
0890: 2f 75 6c 3e 0d 0a 3c 68 32 3e 42 61 73 69 63 20  /ul>..<h2>Basic 
08a0: 45 78 61 6d 70 6c 65 73 3c 2f 68 32 3e 0d 0a 54  Examples</h2>..T
08b0: 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 78 61  he following exa
08c0: 6d 70 6c 65 73 20 61 72 65 20 62 61 73 65 64 20  mples are based 
08d0: 6f 6e 20 74 68 65 20 3c 62 3e 61 6d 5f 72 65 67  on the <b>am_reg
08e0: 5f 6d 75 6c 74 69 70 61 72 74 3c 2f 62 3e 20 53  _multipart</b> S
08f0: 68 61 70 65 66 69 6c 65 20 66 72 65 65 6c 79 20  hapefile freely 
0900: 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 64 6f  available for do
0910: 77 6e 6c 6f 61 64 20 66 72 6f 6d 20 3c 61 20 68  wnload from <a h
0920: 72 65 66 3d 22 68 74 74 70 3a 2f 2f 77 77 77 35  ref="http://www5
0930: 30 32 2e 72 65 67 69 6f 6e 65 2e 74 6f 73 63 61  02.regione.tosca
0940: 6e 61 2e 69 74 2f 67 65 6f 73 63 6f 70 69 6f 2f  na.it/geoscopio/
0950: 63 61 72 74 6f 74 65 63 61 2e 68 74 6d 6c 22 3e  cartoteca.html">
0960: 68 65 72 65 3c 2f 61 3e 20 28 73 65 61 72 63 68  here</a> (search
0970: 20 66 6f 72 20 3c 62 3e 41 6d 62 69 74 69 20 41   for <b>Ambiti A
0980: 6d 6d 69 6e 69 73 74 72 61 74 69 76 69 3c 2f 62  mministrativi</b
0990: 3e 20 2d 20 3c 69 3e 41 64 6d 69 6e 69 73 74 72  > - <i>Administr
09a0: 61 74 69 76 65 20 42 6f 75 6e 64 61 72 69 65 73  ative Boundaries
09b0: 3c 2f 69 3e 29 2e 3c 62 72 3e 3c 62 72 3e 0d 0a  </i>).<br><br>..
09c0: 54 68 69 73 20 6f 6e 65 20 73 75 70 70 6f 72 74  This one support
09d0: 73 20 61 20 76 65 72 79 20 61 63 63 75 72 61 74  s a very accurat
09e0: 65 20 61 6e 64 20 70 72 65 63 69 73 65 20 6d 61  e and precise ma
09f0: 70 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e  p representation
0a00: 2c 20 61 6e 64 20 74 68 65 20 6d 61 69 6e 20 65  , and the main e
0a10: 78 74 65 72 69 6f 72 20 72 69 6e 67 20 68 61 73  xterior ring has
0a20: 20 6d 6f 72 65 20 74 68 61 6e 20 31 30 30 2c 30   more than 100,0
0a30: 30 30 2b 20 56 65 72 74 69 63 65 73 2e 3c 62 72  00+ Vertices.<br
0a40: 3e 0d 0a 49 74 27 73 20 61 20 76 65 72 79 20 67  >..It's a very g
0a50: 6f 6f 64 20 65 78 61 6d 70 6c 65 20 6f 66 20 61  ood example of a
0a60: 20 47 65 6f 6d 65 74 72 79 20 73 6f 20 68 75 67   Geometry so hug
0a70: 65 20 74 6f 20 6d 61 6b 65 20 65 78 74 72 65 6d  e to make extrem
0a80: 65 6c 79 20 73 6c 6f 77 20 63 6f 6d 70 75 74 69  ely slow computi
0a90: 6e 67 20 61 6e 79 20 53 70 61 74 69 61 6c 20 6f  ng any Spatial o
0aa0: 70 65 72 61 74 6f 72 20 28 61 73 20 65 2e 67 2e  perator (as e.g.
0ab0: 20 53 54 5f 49 6e 74 65 72 73 65 63 74 73 2c 20   ST_Intersects, 
0ac0: 53 54 5f 54 6f 75 63 68 65 73 2c 20 53 54 5f 43  ST_Touches, ST_C
0ad0: 6f 76 65 72 73 20 61 6e 64 20 73 6f 20 6f 6e 29  overs and so on)
0ae0: 2e 3c 62 72 3e 3c 62 72 3e 0d 0a 3c 74 61 62 6c  .<br><br>..<tabl
0af0: 65 20 62 67 63 6f 6c 6f 72 3d 22 23 65 30 66 66  e bgcolor="#e0ff
0b00: 65 30 22 20 63 65 6c 6c 73 70 61 63 69 6e 67 3d  e0" cellspacing=
0b10: 22 38 22 20 63 65 6c 6c 70 61 64 64 69 6e 67 3d  "8" cellpadding=
0b20: 22 36 22 20 62 6f 72 64 65 72 3d 22 31 22 3e 0d  "6" border="1">.
0b30: 0a 3c 74 72 3e 3c 74 68 3e 53 51 4c 20 71 75 65  .<tr><th>SQL que
0b40: 72 79 3c 2f 74 68 3e 3c 74 68 3e 56 69 73 75 61  ry</th><th>Visua
0b50: 6c 20 53 61 6d 70 6c 65 3c 2f 74 68 3e 3c 2f 74  l Sample</th></t
0b60: 72 3e 0d 0a 3c 74 72 3e 0d 0a 3c 74 64 3e 0d 0a  r>..<tr>..<td>..
0b70: 3c 76 65 72 62 61 74 69 6d 3e 0d 0a 53 45 4c 45  <verbatim>..SELE
0b80: 43 54 20 53 54 5f 53 75 62 64 69 76 69 64 65 28  CT ST_Subdivide(
0b90: 67 65 6f 6d 65 74 72 79 29 20 46 52 4f 4d 20 74  geometry) FROM t
0ba0: 75 73 63 61 6e 79 3b 0d 0a 3c 2f 76 65 72 62 61  uscany;..</verba
0bb0: 74 69 6d 3e 0d 0a 69 6e 20 74 68 69 73 20 66 69  tim>..in this fi
0bc0: 72 73 74 20 63 61 6c 6c 20 6e 6f 20 6f 70 74 69  rst call no opti
0bd0: 6f 6e 61 6c 20 61 72 67 75 6d 65 6e 74 20 69 73  onal argument is
0be0: 20 73 70 65 63 69 66 69 65 64 2c 20 61 6e 64 20   specified, and 
0bf0: 74 68 75 73 20 3c 62 3e 53 54 5f 53 75 62 64 69  thus <b>ST_Subdi
0c00: 76 69 64 65 28 29 3c 2f 62 3e 20 77 69 6c 6c 20  vide()</b> will 
0c10: 69 6d 70 6c 69 63 69 74 6c 79 20 61 73 73 75 6d  implicitly assum
0c20: 65 20 74 68 65 20 73 74 61 6e 64 61 72 64 20 74  e the standard t
0c30: 68 72 65 73 68 6f 6c 64 20 6f 66 20 3c 62 3e 6d  hreshold of <b>m
0c40: 61 78 20 31 32 38 3c 2f 62 3e 20 56 65 72 74 69  ax 128</b> Verti
0c50: 63 65 73 2e 3c 62 72 3e 3c 62 72 3e 0d 0a 41 73  ces.<br><br>..As
0c60: 20 74 68 65 20 73 69 64 65 20 66 69 67 75 72 65   the side figure
0c70: 20 73 68 6f 77 73 2c 20 74 68 65 20 72 65 74 75   shows, the retu
0c80: 72 6e 65 64 20 72 65 73 75 6c 74 20 69 73 20 61  rned result is a
0c90: 20 63 6f 6c 6c 65 63 74 69 6f 6e 20 6f 66 20 61   collection of a
0ca0: 62 6f 75 74 20 34 2c 33 30 30 2b 20 65 6c 65 6d  bout 4,300+ elem
0cb0: 65 6e 74 61 72 79 20 70 61 72 74 73 2e 0d 0a 3c  entary parts...<
0cc0: 2f 74 64 3e 0d 0a 3c 74 64 3e 0d 0a 3c 69 6d 67  /td>..<td>..<img
0cd0: 20 73 72 63 3d 22 68 74 74 70 73 3a 2f 2f 77 77   src="https://ww
0ce0: 77 2e 67 61 69 61 2d 67 69 73 2e 69 74 2f 67 61  w.gaia-gis.it/ga
0cf0: 69 61 2d 73 69 6e 73 2f 73 75 62 64 69 76 69 64  ia-sins/subdivid
0d00: 65 2f 73 75 62 31 32 38 2e 70 6e 67 22 20 61 6c  e/sub128.png" al
0d10: 74 3d 22 6d 61 78 3d 31 32 38 22 3e 0d 0a 3c 2f  t="max=128">..</
0d20: 74 64 3e 0d 0a 3c 2f 74 72 3e 0d 0a 3c 74 72 3e  td>..</tr>..<tr>
0d30: 0d 0a 3c 74 64 3e 0d 0a 3c 76 65 72 62 61 74 69  ..<td>..<verbati
0d40: 6d 3e 0d 0a 53 45 4c 45 43 54 20 53 54 5f 53 75  m>..SELECT ST_Su
0d50: 62 64 69 76 69 64 65 28 67 65 6f 6d 65 74 72 79  bdivide(geometry
0d60: 2c 20 35 31 32 29 20 46 52 4f 4d 20 74 75 73 63  , 512) FROM tusc
0d70: 61 6e 79 3b 0d 0a 3c 2f 76 65 72 62 61 74 69 6d  any;..</verbatim
0d80: 3e 0d 0a 69 6e 20 74 68 69 73 20 73 65 63 6f 6e  >..in this secon
0d90: 64 20 63 61 6c 6c 20 74 68 65 20 6f 70 74 69 6f  d call the optio
0da0: 6e 61 6c 20 61 72 67 75 6d 65 6e 74 20 69 73 20  nal argument is 
0db0: 65 78 70 6c 69 63 69 74 6c 79 20 73 65 74 2c 20  explicitly set, 
0dc0: 61 6e 64 20 3c 62 3e 53 54 5f 53 75 62 64 69 76  and <b>ST_Subdiv
0dd0: 69 64 65 28 29 3c 2f 62 3e 20 69 73 20 6e 6f 77  ide()</b> is now
0de0: 20 61 73 73 75 6d 69 6e 67 20 61 20 74 68 72 65   assuming a thre
0df0: 73 68 6f 6c 64 20 6f 66 20 3c 62 3e 6d 61 78 20  shold of <b>max 
0e00: 35 31 32 3c 2f 62 3e 20 56 65 72 74 69 63 65 73  512</b> Vertices
0e10: 2e 3c 62 72 3e 3c 62 72 3e 0d 0a 41 73 20 74 68  .<br><br>..As th
0e20: 65 20 73 69 64 65 20 66 69 67 75 72 65 20 73 68  e side figure sh
0e30: 6f 77 73 2c 20 74 68 65 20 72 65 74 75 72 6e 65  ows, the returne
0e40: 64 20 72 65 73 75 6c 74 20 69 73 20 61 20 63 6f  d result is a co
0e50: 6c 6c 65 63 74 69 6f 6e 20 6f 66 20 61 62 6f 75  llection of abou
0e60: 74 20 31 2c 38 30 30 2b 20 65 6c 65 6d 65 6e 74  t 1,800+ element
0e70: 61 72 79 20 70 61 72 74 73 2e 0d 0a 3c 2f 74 64  ary parts...</td
0e80: 3e 0d 0a 3c 74 64 3e 0d 0a 3c 69 6d 67 20 73 72  >..<td>..<img sr
0e90: 63 3d 22 68 74 74 70 73 3a 2f 2f 77 77 77 2e 67  c="https://www.g
0ea0: 61 69 61 2d 67 69 73 2e 69 74 2f 67 61 69 61 2d  aia-gis.it/gaia-
0eb0: 73 69 6e 73 2f 73 75 62 64 69 76 69 64 65 2f 73  sins/subdivide/s
0ec0: 75 62 35 31 32 2e 70 6e 67 22 20 61 6c 74 3d 22  ub512.png" alt="
0ed0: 6d 61 78 3d 35 31 32 22 3e 0d 0a 3c 2f 74 64 3e  max=512">..</td>
0ee0: 0d 0a 3c 2f 74 72 3e 0d 0a 3c 74 72 3e 0d 0a 3c  ..</tr>..<tr>..<
0ef0: 74 64 3e 0d 0a 3c 76 65 72 62 61 74 69 6d 3e 0d  td>..<verbatim>.
0f00: 0a 53 45 4c 45 43 54 20 53 54 5f 53 75 62 64 69  .SELECT ST_Subdi
0f10: 76 69 64 65 28 67 65 6f 6d 65 74 72 79 2c 20 32  vide(geometry, 2
0f20: 30 34 38 29 20 46 52 4f 4d 20 74 75 73 63 61 6e  048) FROM tuscan
0f30: 79 3b 0d 0a 3c 2f 76 65 72 62 61 74 69 6d 3e 0d  y;..</verbatim>.
0f40: 0a 69 6e 20 74 68 69 73 20 74 68 69 72 64 20 61  .in this third a
0f50: 6e 64 20 66 69 6e 61 6c 20 63 61 6c 6c 20 74 68  nd final call th
0f60: 65 20 74 68 72 65 73 68 6f 6c 64 20 69 73 20 73  e threshold is s
0f70: 65 74 20 74 6f 20 3c 62 3e 6d 61 78 20 32 2c 30  et to <b>max 2,0
0f80: 34 38 3c 2f 62 3e 20 56 65 72 74 69 63 65 73 2e  48</b> Vertices.
0f90: 3c 62 72 3e 3c 62 72 3e 0d 0a 41 73 20 73 68 6f  <br><br>..As sho
0fa0: 77 6e 20 62 79 20 74 68 65 20 66 69 67 75 72 65  wn by the figure
0fb0: 2c 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 72  , the returned r
0fc0: 65 73 75 6c 74 20 69 73 20 6e 6f 77 20 61 20 63  esult is now a c
0fd0: 6f 6c 6c 65 63 74 69 6f 6e 20 63 6f 6e 74 61 69  ollection contai
0fe0: 6e 69 6e 67 20 6a 75 73 74 20 31 2c 32 30 30 2b  ning just 1,200+
0ff0: 20 65 6c 65 6d 65 6e 74 61 72 79 20 70 61 72 74   elementary part
1000: 73 2e 0d 0a 3c 68 33 3e 53 68 6f 72 74 20 63 6f  s...<h3>Short co
1010: 6e 63 6c 75 73 69 6f 6e 3c 2f 68 33 3e 0d 0a 3c  nclusion</h3>..<
1020: 62 3e 53 54 5f 53 75 62 64 69 76 69 64 65 28 29  b>ST_Subdivide()
1030: 3c 2f 62 3e 20 69 73 20 65 66 66 65 63 74 69 76  </b> is effectiv
1040: 65 6c 79 20 63 61 70 61 62 6c 65 20 74 6f 20 73  ely capable to s
1050: 75 62 64 69 76 69 64 65 20 73 6f 6d 65 20 6e 61  ubdivide some na
1060: 73 74 79 20 68 75 67 65 20 47 65 6f 6d 65 74 72  sty huge Geometr
1070: 79 20 69 6e 74 6f 20 61 20 63 6f 6c 6c 65 63 74  y into a collect
1080: 69 6f 6e 20 6f 66 20 6d 61 6e 79 20 73 6d 61 6c  ion of many smal
1090: 6c 65 72 20 70 61 72 74 73 20 6d 75 63 68 20 6d  ler parts much m
10a0: 6f 72 65 20 72 65 61 73 6f 6e 61 62 6c 65 20 74  ore reasonable t
10b0: 6f 20 62 65 20 68 61 6e 64 6c 65 64 2e 3c 62 72  o be handled.<br
10c0: 3e 0d 0a 41 6e 64 20 69 74 27 73 20 72 65 61 73  >..And it's reas
10d0: 6f 6e 61 62 6c 79 20 66 6c 65 78 69 62 6c 65 20  onably flexible 
10e0: 61 6e 64 20 65 61 73 79 20 74 6f 20 62 65 20 63  and easy to be c
10f0: 75 73 74 6f 6d 69 7a 65 64 20 61 73 20 72 65 71  ustomized as req
1100: 75 69 72 65 64 3b 20 79 6f 75 20 6a 75 73 74 20  uired; you just 
1110: 68 61 76 65 20 74 6f 20 73 65 74 20 74 68 65 20  have to set the 
1120: 6d 6f 73 74 20 61 70 70 72 6f 70 72 69 61 74 65  most appropriate
1130: 20 3c 62 3e 6d 61 78 2e 20 56 65 72 74 69 63 65   <b>max. Vertice
1140: 73 3c 2f 62 3e 20 74 68 72 65 73 68 6f 6c 64 2e  s</b> threshold.
1150: 0d 0a 3c 2f 74 64 3e 0d 0a 3c 74 64 3e 0d 0a 3c  ..</td>..<td>..<
1160: 69 6d 67 20 73 72 63 3d 22 68 74 74 70 73 3a 2f  img src="https:/
1170: 2f 77 77 77 2e 67 61 69 61 2d 67 69 73 2e 69 74  /www.gaia-gis.it
1180: 2f 67 61 69 61 2d 73 69 6e 73 2f 73 75 62 64 69  /gaia-sins/subdi
1190: 76 69 64 65 2f 73 75 62 32 30 34 38 2e 70 6e 67  vide/sub2048.png
11a0: 22 20 61 6c 74 3d 22 6d 61 78 3d 32 30 34 38 22  " alt="max=2048"
11b0: 3e 0d 0a 3c 2f 74 64 3e 0d 0a 3c 2f 74 72 3e 0d  >..</td>..</tr>.
11c0: 0a 3c 2f 74 61 62 6c 65 3e 0d 0a 3c 62 72 3e 0d  .</table>..<br>.
11d0: 0a 48 6f 77 65 76 65 72 2c 20 61 20 3c 62 3e 62  .However, a <b>b
11e0: 69 67 20 63 61 76 65 61 74 3c 2f 62 3e 20 73 68  ig caveat</b> sh
11f0: 6f 75 6c 64 20 62 65 20 61 6c 77 61 79 73 20 63  ould be always c
1200: 6f 6e 73 69 64 65 72 65 64 20 61 62 6f 75 74 20  onsidered about 
1210: 74 68 65 20 6d 75 6c 74 69 2d 70 61 72 74 20 63  the multi-part c
1220: 6f 6c 6c 65 63 74 69 6f 6e 73 20 72 65 74 75 72  ollections retur
1230: 6e 65 64 20 62 79 20 3c 62 3e 53 54 5f 53 75 62  ned by <b>ST_Sub
1240: 64 69 76 69 64 65 28 29 3c 2f 62 3e 2e 3c 62 72  divide()</b>.<br
1250: 3e 0d 0a 41 74 20 6c 65 61 73 74 20 69 6e 20 74  >..At least in t
1260: 68 65 20 63 61 73 65 20 6f 66 20 3c 62 3e 4d 75  he case of <b>Mu
1270: 6c 74 69 50 6f 6c 79 67 6f 6e 73 3c 2f 62 3e 20  ltiPolygons</b> 
1280: 61 6e 79 20 63 6f 6c 6c 65 63 74 69 6f 6e 20 72  any collection r
1290: 65 74 75 72 6e 65 64 20 62 79 20 53 54 5f 53 75  eturned by ST_Su
12a0: 62 64 69 76 69 64 65 28 29 20 69 73 20 69 6e 68  bdivide() is inh
12b0: 65 72 65 6e 74 6c 79 20 69 6e 76 61 6c 69 64 2c  erently invalid,
12c0: 20 61 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   as the followin
12d0: 67 20 53 51 4c 20 71 75 65 72 79 20 64 65 6d 6f  g SQL query demo
12e0: 6e 73 74 72 61 74 65 73 3a 0d 0a 3c 76 65 72 62  nstrates:..<verb
12f0: 61 74 69 6d 3e 0d 0a 53 45 4c 45 43 54 20 53 54  atim>..SELECT ST
1300: 5f 49 73 56 61 6c 69 64 28 53 54 5f 53 75 62 64  _IsValid(ST_Subd
1310: 69 76 69 64 65 28 67 65 6f 6d 65 74 72 79 29 29  ivide(geometry))
1320: 20 46 52 4f 4d 20 74 75 73 63 61 6e 79 3b 0d 0a   FROM tuscany;..
1330: 2d 2d 2d 2d 2d 2d 2d 2d 2d 0d 0a 30 0d 0a 3c 2f  ---------..0..</
1340: 76 65 72 62 61 74 69 6d 3e 0d 0a 49 74 27 73 20  verbatim>..It's 
1350: 6e 6f 74 20 61 74 20 61 6c 6c 20 64 69 66 66 69  not at all diffi
1360: 63 75 6c 74 20 75 6e 64 65 72 73 74 61 6e 64 69  cult understandi
1370: 6e 67 20 77 68 79 20 74 68 69 73 20 68 61 70 70  ng why this happ
1380: 65 6e 73 2e 3c 62 72 3e 0d 0a 41 63 63 6f 72 64  ens.<br>..Accord
1390: 69 6e 67 6c 79 20 74 6f 20 73 74 61 6e 64 61 72  ingly to standar
13a0: 64 20 3c 62 3e 4f 47 43 2f 53 46 53 3c 2f 62 3e  d <b>OGC/SFS</b>
13b0: 20 72 75 6c 65 73 20 74 77 6f 20 73 69 6e 67 6c   rules two singl
13c0: 65 2d 70 61 72 74 20 50 6f 6c 79 67 6f 6e 73 20  e-part Polygons 
13d0: 62 65 6c 6f 6e 67 69 6e 67 20 74 6f 20 74 68 65  belonging to the
13e0: 20 73 61 6d 65 20 4d 75 6c 74 69 50 6f 6c 79 67   same MultiPolyg
13f0: 6f 6e 20 61 72 65 20 61 6c 77 61 79 73 20 66 6f  on are always fo
1400: 72 62 69 64 64 65 6e 20 74 6f 20 72 65 63 69 70  rbidden to recip
1410: 72 6f 63 61 6c 6c 79 20 74 6f 75 63 68 2e 3c 62  rocally touch.<b
1420: 72 3e 0d 0a 4d 6f 72 65 20 70 72 65 63 69 73 65  r>..More precise
1430: 6c 79 3a 20 74 68 65 79 20 63 61 6e 20 6f 6e 6c  ly: they can onl
1440: 79 20 74 6f 75 63 68 20 6f 6e 20 73 70 65 63 69  y touch on speci
1450: 66 69 63 20 70 6f 69 6e 74 28 73 29 2c 20 62 75  fic point(s), bu
1460: 74 20 74 68 65 79 20 63 61 6e 20 6e 65 76 65 72  t they can never
1470: 20 73 68 61 72 65 20 61 20 63 6f 6d 6d 6f 6e 20   share a common 
1480: 62 6f 75 6e 64 61 72 79 2e 3c 62 72 3e 3c 62 72  boundary.<br><br
1490: 3e 0d 0a 3c 74 61 62 6c 65 20 62 67 63 6f 6c 6f  >..<table bgcolo
14a0: 72 3d 22 23 66 66 64 30 64 30 22 20 63 65 6c 6c  r="#ffd0d0" cell
14b0: 73 70 61 63 69 6e 67 3d 22 38 22 20 63 65 6c 6c  spacing="8" cell
14c0: 70 61 64 64 69 6e 67 3d 22 36 22 3e 0d 0a 3c 74  padding="6">..<t
14d0: 72 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 63 65 6e  r><td align="cen
14e0: 74 65 72 22 3e 3c 68 33 3e 43 61 76 65 61 74 20  ter"><h3>Caveat 
14f0: 21 21 21 3c 2f 68 33 3e 3c 2f 74 64 3e 3c 2f 74  !!!</h3></td></t
1500: 72 3e 0d 0a 3c 74 72 3e 3c 74 64 3e 3c 62 3e 4e  r>..<tr><td><b>N
1510: 65 76 65 72 20 65 76 65 72 3c 2f 62 3e 20 64 69  ever ever</b> di
1520: 72 65 63 74 6c 79 20 70 61 73 73 20 61 20 47 65  rectly pass a Ge
1530: 6f 6d 65 74 72 79 20 6f 66 20 74 68 69 73 20 6b  ometry of this k
1540: 69 6e 64 20 74 6f 20 61 6e 79 20 53 70 61 74 69  ind to any Spati
1550: 61 6c 20 6f 70 65 72 61 74 6f 72 20 73 75 63 68  al operator such
1560: 20 61 73 20 53 54 5f 49 6e 74 65 72 73 65 63 74   as ST_Intersect
1570: 73 2c 20 53 54 5f 54 6f 75 63 68 65 73 2c 20 53  s, ST_Touches, S
1580: 54 5f 43 6f 76 65 72 73 20 61 6e 64 20 61 6c 69  T_Covers and ali
1590: 6b 65 2e 3c 62 72 3e 3c 62 72 3e 0d 0a 54 68 69  ke.<br><br>..Thi
15a0: 73 20 77 69 6c 6c 20 63 65 72 74 61 69 6e 6c 79  s will certainly
15b0: 20 6d 61 6b 65 20 63 72 61 7a 79 20 74 68 65 20   make crazy the 
15c0: 3c 62 3e 47 45 4f 53 3c 2f 62 3e 20 6c 69 62 72  <b>GEOS</b> libr
15d0: 61 72 79 2e 3c 62 72 3e 0d 0a 49 6e 63 6f 72 72  ary.<br>..Incorr
15e0: 65 63 74 20 72 65 73 75 6c 74 73 20 6d 61 79 20  ect results may 
15f0: 65 61 73 69 6c 79 20 66 6f 6c 6c 6f 77 2e 3c 62  easily follow.<b
1600: 72 3e 0d 0a 41 6e 64 20 69 6e 20 74 68 65 20 77  r>..And in the w
1610: 6f 72 73 74 20 63 61 73 65 20 73 6f 6d 65 20 75  orst case some u
1620: 6e 65 78 70 65 63 74 65 64 20 63 72 61 73 68 20  nexpected crash 
1630: 63 6f 75 6c 64 20 65 76 65 6e 74 75 61 6c 6c 79  could eventually
1640: 20 6f 63 63 75 72 2e 0d 0a 3c 68 33 3e 59 6f 75   occur...<h3>You
1650: 20 68 61 76 65 20 62 65 65 6e 20 77 61 72 6e 65   have been warne
1660: 64 20 21 21 21 3c 2f 68 33 3e 0d 0a 3c 2f 74 64  d !!!</h3>..</td
1670: 3e 0d 0a 3c 2f 74 72 3e 0d 0a 3c 2f 74 61 62 6c  >..</tr>..</tabl
1680: 65 3e 0d 0a 3c 62 72 3e 3c 62 72 3e 0d 0a 3c 68  e>..<br><br>..<h
1690: 72 3e 0d 0a 3c 68 32 3e 41 64 76 61 6e 63 65 64  r>..<h2>Advanced
16a0: 20 45 78 61 6d 70 6c 65 73 3c 2f 68 32 3e 0d 0a   Examples</h2>..
16b0: 4e 6f 77 20 77 65 27 6c 6c 20 67 6f 20 74 6f 20  Now we'll go to 
16c0: 65 78 70 6c 6f 72 65 20 74 68 65 20 72 65 61 6c  explore the real
16d0: 20 65 66 66 65 63 74 69 76 65 6e 65 73 73 20 6f   effectiveness o
16e0: 66 20 3c 62 3e 53 54 5f 53 75 62 64 69 76 69 64  f <b>ST_Subdivid
16f0: 65 28 29 3c 2f 62 3e 20 69 6e 20 6f 6e 65 20 6f  e()</b> in one o
1700: 66 20 74 68 65 20 6d 6f 73 74 20 63 6f 6d 6d 6f  f the most commo
1710: 6e 20 53 70 61 74 69 61 6c 20 50 72 6f 62 6c 65  n Spatial Proble
1720: 6d 73 3a 20 69 64 65 6e 74 69 66 79 69 6e 67 20  ms: identifying 
1730: 61 6c 6c 20 69 6e 74 65 72 73 65 63 74 69 6f 6e  all intersection
1740: 73 20 62 65 74 77 65 65 6e 20 61 20 64 61 74 61  s between a data
1750: 73 65 74 20 6f 66 20 74 68 65 20 50 4f 49 4e 54  set of the POINT
1760: 20 74 79 70 65 20 61 6e 64 20 61 20 68 75 67 65   type and a huge
1770: 20 50 6f 6c 79 67 6f 6e 2f 4d 75 6c 74 69 50 6f   Polygon/MultiPo
1780: 6c 79 67 6f 6e 2e 3c 62 72 3e 3c 62 72 3e 0d 0a  lygon.<br><br>..
1790: 44 75 72 69 6e 67 20 74 68 69 73 20 73 65 63 6f  During this seco
17a0: 6e 64 20 74 65 73 74 20 77 65 27 6c 6c 20 75 73  nd test we'll us
17b0: 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  e the following 
17c0: 64 61 74 61 73 65 74 73 3a 0d 0a 3c 75 6c 3e 0d  datasets:..<ul>.
17d0: 0a 3c 6c 69 3e 6f 6e 63 65 20 61 67 61 69 6e 2c  .<li>once again,
17e0: 20 74 68 65 20 73 61 6d 65 20 61 64 6d 69 6e 69   the same admini
17f0: 73 74 72 61 74 69 76 65 20 62 6f 75 6e 64 61 72  strative boundar
1800: 79 20 6f 66 20 54 75 73 63 61 6e 79 20 77 65 27  y of Tuscany we'
1810: 76 65 20 61 6c 72 65 61 64 79 20 75 73 65 64 20  ve already used 
1820: 62 65 66 6f 72 65 2e 3c 2f 6c 69 3e 0d 0a 3c 6c  before.</li>..<l
1830: 69 3e 69 6e 20 74 68 65 20 72 6f 6c 65 20 6f 66  i>in the role of
1840: 20 74 68 65 20 50 4f 49 4e 54 20 64 61 74 61 73   the POINT datas
1850: 65 74 20 77 65 27 6c 6c 20 75 73 65 20 74 68 65  et we'll use the
1860: 20 3c 62 3e 63 69 76 69 63 69 3c 2f 62 3e 20 53   <b>civici</b> S
1870: 68 61 70 65 66 69 6c 65 20 28 3c 69 3e 48 6f 75  hapefile (<i>Hou
1880: 73 65 20 4e 75 6d 62 65 72 73 3c 2f 69 3e 29 20  se Numbers</i>) 
1890: 66 72 65 65 6c 79 20 61 76 61 69 6c 61 62 6c 65  freely available
18a0: 20 66 6f 72 20 64 6f 77 6e 6c 6f 61 64 20 66 72   for download fr
18b0: 6f 6d 20 3c 61 20 68 72 65 66 3d 22 68 74 74 70  om <a href="http
18c0: 3a 2f 2f 77 77 77 35 30 32 2e 72 65 67 69 6f 6e  ://www502.region
18d0: 65 2e 74 6f 73 63 61 6e 61 2e 69 74 2f 67 65 6f  e.toscana.it/geo
18e0: 73 63 6f 70 69 6f 2f 63 61 72 74 6f 74 65 63 61  scopio/cartoteca
18f0: 2e 68 74 6d 6c 22 3e 68 65 72 65 3c 2f 61 3e 20  .html">here</a> 
1900: 28 73 65 61 72 63 68 20 66 6f 72 20 3c 62 3e 47  (search for <b>G
1910: 72 61 66 6f 20 53 74 72 61 64 61 6c 65 3c 2f 62  rafo Stradale</b
1920: 3e 20 2d 20 3c 69 3e 52 6f 61 64 20 4e 65 74 77  > - <i>Road Netw
1930: 6f 72 6b 3c 2f 69 3e 29 2e 3c 62 72 3e 0d 0a 54  ork</i>).<br>..T
1940: 68 69 73 20 74 6f 6f 20 69 73 20 61 20 68 75 67  his too is a hug
1950: 65 20 64 61 74 61 73 65 74 20 63 6f 6e 74 61 69  e dataset contai
1960: 6e 69 6e 67 20 61 62 6f 75 74 20 31 2e 35 20 6d  ning about 1.5 m
1970: 69 6c 6c 69 6f 6e 20 70 6f 69 6e 74 73 2e 0d 0a  illion points...
1980: 3c 2f 75 6c 3e 0d 0a 3c 68 33 3e 41 20 66 69 72  </ul>..<h3>A fir
1990: 73 74 20 6e 61 69 76 65 20 61 74 74 65 6d 70 74  st naive attempt
19a0: 3c 2f 68 33 3e 0d 0a 3c 76 65 72 62 61 74 69 6d  </h3>..<verbatim
19b0: 3e 0d 0a 53 45 4c 45 43 54 20 43 6f 75 6e 74 28  >..SELECT Count(
19c0: 2a 29 0d 0a 46 52 4f 4d 20 63 69 76 69 63 69 20  *)..FROM civici 
19d0: 41 53 20 63 0d 0a 4c 45 46 54 20 4a 4f 49 4e 20  AS c..LEFT JOIN 
19e0: 74 75 73 63 61 6e 79 20 41 53 20 74 20 4f 4e 20  tuscany AS t ON 
19f0: 28 53 54 5f 49 6e 74 65 72 73 65 63 74 73 28 63  (ST_Intersects(c
1a00: 2e 67 65 6f 6d 65 74 72 79 2c 20 74 2e 67 65 6f  .geometry, t.geo
1a10: 6d 65 74 72 79 29 20 3d 20 31 29 3b 0d 0a 0d 0a  metry) = 1);....
1a20: 0d 0a 53 45 4c 45 43 54 20 43 6f 75 6e 74 28 2a  ..SELECT Count(*
1a30: 29 0d 0a 46 52 4f 4d 20 63 69 76 69 63 69 20 41  )..FROM civici A
1a40: 53 20 63 0d 0a 4c 45 46 54 20 4a 4f 49 4e 20 74  S c..LEFT JOIN t
1a50: 75 73 63 61 6e 79 20 41 53 20 74 20 4f 4e 20 28  uscany AS t ON (
1a60: 53 54 5f 49 6e 74 65 72 73 65 63 74 73 28 63 2e  ST_Intersects(c.
1a70: 67 65 6f 6d 65 74 72 79 2c 20 74 2e 67 65 6f 6d  geometry, t.geom
1a80: 65 74 72 79 29 20 3d 20 31 20 0d 0a 20 20 20 20  etry) = 1 ..    
1a90: 20 41 4e 44 20 63 2e 72 6f 77 69 64 20 49 4e 20   AND c.rowid IN 
1aa0: 28 0d 0a 20 20 20 20 20 20 20 20 20 53 45 4c 45  (..         SELE
1ab0: 43 54 20 72 6f 77 69 64 20 46 52 4f 4d 20 53 70  CT rowid FROM Sp
1ac0: 61 74 69 61 6c 49 6e 64 65 78 0d 0a 20 20 20 20  atialIndex..    
1ad0: 20 20 20 20 20 57 48 45 52 45 20 66 5f 74 61 62       WHERE f_tab
1ae0: 6c 65 5f 6e 61 6d 65 20 3d 20 27 63 69 76 69 63  le_name = 'civic
1af0: 69 27 20 41 4e 44 20 73 65 61 72 63 68 5f 66 72  i' AND search_fr
1b00: 61 6d 65 20 3d 20 74 2e 67 65 6f 6d 65 74 72 79  ame = t.geometry
1b10: 29 29 3b 0d 0a 3c 2f 76 65 72 62 61 74 69 6d 3e  ));..</verbatim>
1b20: 0d 0a 54 68 65 20 6f 6e 6c 79 20 64 69 66 66 65  ..The only diffe
1b30: 72 65 6e 63 65 20 62 65 74 77 65 65 6e 20 74 68  rence between th
1b40: 65 20 61 62 6f 76 65 20 74 77 6f 20 53 51 4c 20  e above two SQL 
1b50: 71 75 65 72 69 65 73 20 69 73 20 69 6e 20 74 68  queries is in th
1b60: 61 74 20 74 68 65 20 73 65 63 6f 6e 64 20 6f 6e  at the second on
1b70: 65 20 65 78 70 6c 69 63 69 74 6c 79 20 63 61 6c  e explicitly cal
1b80: 6c 73 20 74 68 65 20 53 70 61 74 69 61 6c 20 49  ls the Spatial I
1b90: 6e 64 65 78 2e 3c 62 72 3e 3c 62 72 3e 0d 0a 3c  ndex.<br><br>..<
1ba0: 62 3e 41 63 74 75 61 6c 20 66 69 6e 64 69 6e 67  b>Actual finding
1bb0: 73 3c 2f 62 3e 3a 0d 0a 3c 75 6c 3e 0d 0a 3c 6c  s</b>:..<ul>..<l
1bc0: 69 3e 49 20 77 61 73 20 66 6f 72 63 65 64 20 74  i>I was forced t
1bd0: 6f 20 70 72 65 6d 61 74 75 72 65 6c 79 20 61 62  o prematurely ab
1be0: 6f 72 74 20 74 68 65 20 66 69 72 73 74 20 74 65  ort the first te
1bf0: 73 74 20 77 68 65 6e 20 49 20 64 69 73 63 6f 76  st when I discov
1c00: 65 72 65 64 20 74 68 61 74 20 64 75 72 69 6e 67  ered that during
1c10: 20 74 68 65 20 66 69 72 73 74 20 68 61 6c 66 20   the first half 
1c20: 61 6e 20 68 6f 75 72 20 69 74 20 68 61 64 20 6d  an hour it had m
1c30: 61 64 65 20 6f 6e 6c 79 20 61 20 76 65 72 79 20  ade only a very 
1c40: 6c 69 74 74 6c 65 20 70 72 6f 67 72 65 73 73 2e  little progress.
1c50: 3c 62 72 3e 0d 0a 41 63 63 6f 72 64 69 6e 67 6c  <br>..Accordingl
1c60: 79 20 74 6f 20 6d 79 20 65 73 74 69 6d 61 74 65  y to my estimate
1c70: 64 20 65 78 74 72 61 70 6f 6c 61 74 69 6f 6e 2c  d extrapolation,
1c80: 20 77 61 69 74 69 6e 67 20 66 6f 72 20 63 6f 6d   waiting for com
1c90: 70 6c 65 74 69 6f 6e 20 77 69 6c 6c 20 68 61 64  pletion will had
1ca0: 20 70 72 65 73 75 6d 61 62 6c 79 20 72 65 71 75   presumably requ
1cb0: 69 72 65 64 20 62 65 74 77 65 65 6e 20 34 20 61  ired between 4 a
1cc0: 6e 64 20 36 20 68 6f 75 72 73 2e 3c 62 72 3e 0d  nd 6 hours.<br>.
1cd0: 0a 44 65 66 69 6e 69 74 65 6c 79 20 74 68 69 73  .Definitely this
1ce0: 20 69 73 6e 27 74 20 61 20 70 72 61 63 74 69 63   isn't a practic
1cf0: 61 6c 20 73 6f 6c 75 74 69 6f 6e 20 66 6f 72 20  al solution for 
1d00: 6f 75 72 20 70 72 6f 62 6c 65 6d 2e 3c 2f 6c 69  our problem.</li
1d10: 3e 0d 0a 3c 6c 69 3e 74 68 65 20 73 65 63 6f 6e  >..<li>the secon
1d20: 64 20 74 65 73 74 20 77 61 73 2c 20 69 66 20 70  d test was, if p
1d30: 6f 73 73 69 62 6c 65 2c 20 65 76 65 6e 20 73 6c  ossible, even sl
1d40: 6f 77 65 72 20 74 68 61 6e 20 74 68 65 20 66 69  ower than the fi
1d50: 72 73 74 20 6f 6e 65 2e 3c 62 72 3e 0d 0a 41 6e  rst one.<br>..An
1d60: 64 20 74 68 65 72 65 20 69 73 20 76 65 72 79 20  d there is very 
1d70: 67 6f 6f 64 20 72 65 61 73 6f 6e 20 65 78 70 6c  good reason expl
1d80: 61 69 6e 69 6e 67 20 66 6f 72 20 74 68 69 73 3b  aining for this;
1d90: 20 61 6c 6c 20 50 6f 69 6e 74 73 20 74 6f 20 62   all Points to b
1da0: 65 20 63 68 65 63 6b 65 64 20 66 61 6c 6c 20 69  e checked fall i
1db0: 6e 73 69 64 65 20 74 68 65 20 42 42 4f 58 20 6f  nside the BBOX o
1dc0: 66 20 54 75 73 63 61 6e 79 2e 3c 62 72 3e 0d 0a  f Tuscany.<br>..
1dd0: 53 6f 20 74 68 65 20 53 70 61 74 69 61 6c 20 49  So the Spatial I
1de0: 6e 64 65 78 20 69 73 6e 27 74 20 6f 66 20 61 6e  ndex isn't of an
1df0: 79 20 70 6f 73 73 69 62 6c 65 20 68 65 6c 70 2e  y possible help.
1e00: 3c 62 72 3e 0d 0a 41 6e 64 20 65 76 65 6e 20 77  <br>..And even w
1e10: 6f 72 73 74 2c 20 71 75 65 72 79 69 6e 67 20 74  orst, querying t
1e20: 68 65 20 53 70 61 74 69 61 6c 20 49 6e 64 65 78  he Spatial Index
1e30: 20 69 6d 70 6f 73 65 73 20 61 20 66 75 72 74 68   imposes a furth
1e40: 65 72 20 6f 76 65 72 68 65 61 64 2c 20 74 68 75  er overhead, thu
1e50: 73 20 72 65 71 75 69 72 69 6e 67 20 61 20 6c 6f  s requiring a lo
1e60: 6e 67 65 72 20 74 69 6d 65 20 66 6f 72 20 61 62  nger time for ab
1e70: 73 6f 6c 75 74 65 6c 79 20 6e 6f 74 68 69 6e 67  solutely nothing
1e80: 2e 3c 2f 6c 69 3e 0d 0a 3c 6c 69 3e 3c 62 3e 53  .</li>..<li><b>S
1e90: 68 6f 72 74 20 63 6f 6e 63 6c 75 73 69 6f 6e 3c  hort conclusion<
1ea0: 2f 62 3e 3a 20 63 6f 6d 70 75 74 69 6e 67 20 6d  /b>: computing m
1eb0: 61 6e 79 20 6d 69 6c 6c 69 6f 6e 20 74 69 6d 65  any million time
1ec0: 73 20 74 68 65 20 69 6e 74 65 72 73 65 63 74 69  s the intersecti
1ed0: 6f 6e 73 20 62 65 74 77 65 65 6e 20 61 20 50 6f  ons between a Po
1ee0: 69 6e 74 20 61 6e 64 20 61 20 50 6f 6c 79 67 6f  int and a Polygo
1ef0: 6e 20 64 65 66 69 6e 65 64 20 62 79 20 73 6f 6d  n defined by som
1f00: 65 20 68 75 6e 64 72 65 64 74 68 20 74 68 6f 75  e hundredth thou
1f10: 73 61 6e 64 73 20 56 65 72 74 69 63 65 73 20 69  sands Vertices i
1f20: 73 20 61 20 64 61 6d 6e 20 68 61 72 64 20 63 6f  s a damn hard co
1f30: 6d 70 75 74 61 74 69 6f 6e 61 6c 20 70 72 6f 62  mputational prob
1f40: 6c 65 6d 2e 3c 62 72 3e 20 0d 0a 41 6e 64 20 63  lem.<br> ..And c
1f50: 6f 6e 73 65 71 75 65 6e 74 6c 79 20 69 74 20 72  onsequently it r
1f60: 65 71 75 69 72 65 73 20 61 20 76 65 72 79 20 6c  equires a very l
1f70: 6f 6e 67 20 74 69 6d 65 2c 20 73 6f 20 6c 6f 6e  ong time, so lon
1f80: 67 20 74 6f 20 62 65 20 61 62 73 6f 6c 75 74 65  g to be absolute
1f90: 6c 79 20 75 6e 70 72 61 63 74 69 63 61 6c 2e 3c  ly unpractical.<
1fa0: 2f 6c 69 3e 0d 0a 3c 2f 75 6c 3e 0d 0a 3c 68 33  /li>..</ul>..<h3
1fb0: 3e 41 20 73 65 63 6f 6e 64 20 61 74 74 65 6d 70  >A second attemp
1fc0: 74 20 62 61 73 65 64 20 6f 6e 20 53 54 5f 53 75  t based on ST_Su
1fd0: 62 64 69 76 69 64 65 28 29 20 61 6e 64 20 56 69  bdivide() and Vi
1fe0: 72 74 75 61 6c 45 6c 65 6d 65 6e 74 61 72 79 3c  rtualElementary<
1ff0: 2f 68 33 3e 0d 0a 3c 76 65 72 62 61 74 69 6d 3e  /h3>..<verbatim>
2000: 0d 0a 43 52 45 41 54 45 20 54 41 42 4c 45 20 78  ..CREATE TABLE x
2010: 78 20 28 69 64 20 49 4e 54 45 47 45 52 20 50 52  x (id INTEGER PR
2020: 49 4d 41 52 59 20 4b 45 59 29 3b 0d 0a 53 45 4c  IMARY KEY);..SEL
2030: 45 43 54 20 41 64 64 47 65 6f 6d 65 74 72 79 43  ECT AddGeometryC
2040: 6f 6c 75 6d 6e 28 27 78 78 27 2c 20 27 67 65 6f  olumn('xx', 'geo
2050: 6d 27 2c 20 33 30 30 33 2c 20 27 4d 55 4c 54 49  m', 3003, 'MULTI
2060: 50 4f 4c 59 47 4f 4e 27 2c 20 27 58 59 27 29 3b  POLYGON', 'XY');
2070: 0d 0a 49 4e 53 45 52 54 20 49 4e 54 4f 20 78 78  ..INSERT INTO xx
2080: 20 56 41 4c 55 45 53 20 28 31 2c 20 4e 55 4c 4c   VALUES (1, NULL
2090: 29 3b 0d 0a 3c 2f 76 65 72 62 61 74 69 6d 3e 0d  );..</verbatim>.
20a0: 0a 57 65 27 6c 6c 20 73 74 61 72 74 20 66 69 72  .We'll start fir
20b0: 73 74 20 62 79 20 63 72 65 61 74 69 6e 67 20 61  st by creating a
20c0: 20 63 6f 6e 76 65 6e 69 65 6e 63 65 20 54 61 62   convenience Tab
20d0: 6c 65 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 74  le intended to t
20e0: 65 6d 70 6f 72 61 72 69 6c 79 20 73 74 6f 72 65  emporarily store
20f0: 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 53   the result of S
2100: 54 5f 53 75 62 64 69 76 69 64 65 28 29 2e 20 54  T_Subdivide(). T
2110: 68 69 73 20 54 61 62 6c 65 20 69 73 20 76 65 72  his Table is ver
2120: 79 20 73 69 6d 70 6c 65 3a 0d 0a 3c 75 6c 3e 0d  y simple:..<ul>.
2130: 0a 3c 6c 69 3e 69 74 20 68 61 73 20 6a 75 73 74  .<li>it has just
2140: 20 74 77 6f 20 63 6f 6c 75 6d 6e 73 2c 20 61 20   two columns, a 
2150: 50 72 69 6d 61 72 79 20 4b 65 79 20 61 6e 64 20  Primary Key and 
2160: 61 20 4d 75 6c 74 69 50 6f 6c 79 67 6f 6e 20 47  a MultiPolygon G
2170: 65 6f 6d 65 74 72 79 2e 3c 2f 6c 69 3e 0d 0a 3c  eometry.</li>..<
2180: 6c 69 3e 61 6e 64 20 69 74 27 73 20 69 6e 74 65  li>and it's inte
2190: 6e 64 65 64 20 74 6f 20 73 74 6f 72 65 20 6a 75  nded to store ju
21a0: 73 74 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20  st a single row 
21b0: 69 64 65 6e 74 69 66 69 65 64 20 62 79 20 3c 62  identified by <b
21c0: 3e 69 64 3d 31 3c 2f 62 3e 3c 2f 6c 69 3e 0d 0a  >id=1</b></li>..
21d0: 3c 2f 75 6c 3e 0d 0a 3c 76 65 72 62 61 74 69 6d  </ul>..<verbatim
21e0: 3e 0d 0a 55 50 44 41 54 45 20 78 78 20 53 45 54  >..UPDATE xx SET
21f0: 20 67 65 6f 6d 20 3d 20 28 53 45 4c 45 43 54 20   geom = (SELECT 
2200: 53 54 5f 53 75 62 64 69 76 69 64 65 28 67 65 6f  ST_Subdivide(geo
2210: 6d 65 74 72 79 2c 20 32 30 34 38 29 20 46 52 4f  metry, 2048) FRO
2220: 4d 20 74 75 73 63 61 6e 79 29 20 57 48 45 52 45  M tuscany) WHERE
2230: 20 69 64 20 3d 20 31 3b 0d 0a 53 45 4c 45 43 54   id = 1;..SELECT
2240: 20 43 6f 75 6e 74 28 2a 29 0d 0a 46 52 4f 4d 20   Count(*)..FROM 
2250: 45 6c 65 6d 65 6e 74 61 72 79 47 65 6f 6d 65 74  ElementaryGeomet
2260: 72 69 65 73 20 41 53 20 74 0d 0a 4a 4f 49 4e 20  ries AS t..JOIN 
2270: 63 69 76 69 63 69 20 41 53 20 63 20 4f 4e 20 28  civici AS c ON (
2280: 53 54 5f 49 6e 74 65 72 73 65 63 74 73 28 63 2e  ST_Intersects(c.
2290: 67 65 6f 6d 65 74 72 79 2c 20 74 2e 67 65 6f 6d  geometry, t.geom
22a0: 65 74 72 79 29 20 3d 20 31 0d 0a 20 20 20 20 20  etry) = 1..     
22b0: 41 4e 44 20 63 2e 72 6f 77 69 64 20 49 4e 20 28  AND c.rowid IN (
22c0: 0d 0a 20 20 20 20 20 20 20 20 20 53 45 4c 45 43  ..         SELEC
22d0: 54 20 72 6f 77 69 64 20 46 52 4f 4d 20 53 70 61  T rowid FROM Spa
22e0: 74 69 61 6c 49 6e 64 65 78 0d 0a 20 20 20 20 20  tialIndex..     
22f0: 20 20 20 20 57 48 45 52 45 20 66 5f 74 61 62 6c      WHERE f_tabl
2300: 65 5f 6e 61 6d 65 20 3d 20 27 63 69 76 69 63 69  e_name = 'civici
2310: 27 20 41 4e 44 20 73 65 61 72 63 68 5f 66 72 61  ' AND search_fra
2320: 6d 65 20 3d 20 74 2e 67 65 6f 6d 65 74 72 79 29  me = t.geometry)
2330: 29 0d 0a 57 48 45 52 45 20 66 5f 74 61 62 6c 65  )..WHERE f_table
2340: 5f 6e 61 6d 65 20 3d 20 27 78 78 27 20 41 4e 44  _name = 'xx' AND
2350: 20 6f 72 69 67 69 6e 5f 72 6f 77 69 64 20 3d 20   origin_rowid = 
2360: 31 3b 0d 0a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  1;..------------
2370: 2d 2d 2d 2d 0d 0a 31 34 38 30 39 39 31 20 28 69  ----..1480991 (i
2380: 6e 20 33 38 2e 30 39 34 20 73 65 63 6f 6e 64 73  n 38.094 seconds
2390: 29 0d 0a 0d 0a 0d 0a 55 50 44 41 54 45 20 78 78  )......UPDATE xx
23a0: 20 53 45 54 20 67 65 6f 6d 20 3d 20 28 53 45 4c   SET geom = (SEL
23b0: 45 43 54 20 53 54 5f 53 75 62 64 69 76 69 64 65  ECT ST_Subdivide
23c0: 28 67 65 6f 6d 65 74 72 79 2c 20 35 31 32 29 20  (geometry, 512) 
23d0: 46 52 4f 4d 20 74 75 73 63 61 6e 79 29 20 57 48  FROM tuscany) WH
23e0: 45 52 45 20 69 64 20 3d 20 31 3b 0d 0a 53 45 4c  ERE id = 1;..SEL
23f0: 45 43 54 20 43 6f 75 6e 74 28 2a 29 0d 0a 46 52  ECT Count(*)..FR
2400: 4f 4d 20 45 6c 65 6d 65 6e 74 61 72 79 47 65 6f  OM ElementaryGeo
2410: 6d 65 74 72 69 65 73 20 41 53 20 74 0d 0a 4a 4f  metries AS t..JO
2420: 49 4e 20 63 69 76 69 63 69 20 41 53 20 63 20 4f  IN civici AS c O
2430: 4e 20 28 53 54 5f 49 6e 74 65 72 73 65 63 74 73  N (ST_Intersects
2440: 28 63 2e 67 65 6f 6d 65 74 72 79 2c 20 74 2e 67  (c.geometry, t.g
2450: 65 6f 6d 65 74 72 79 29 20 3d 20 31 0d 0a 20 20  eometry) = 1..  
2460: 20 20 20 41 4e 44 20 63 2e 72 6f 77 69 64 20 49     AND c.rowid I
2470: 4e 20 28 0d 0a 20 20 20 20 20 20 20 20 20 53 45  N (..         SE
2480: 4c 45 43 54 20 72 6f 77 69 64 20 46 52 4f 4d 20  LECT rowid FROM 
2490: 53 70 61 74 69 61 6c 49 6e 64 65 78 0d 0a 20 20  SpatialIndex..  
24a0: 20 20 20 20 20 20 20 57 48 45 52 45 20 66 5f 74         WHERE f_t
24b0: 61 62 6c 65 5f 6e 61 6d 65 20 3d 20 27 63 69 76  able_name = 'civ
24c0: 69 63 69 27 20 41 4e 44 20 73 65 61 72 63 68 5f  ici' AND search_
24d0: 66 72 61 6d 65 20 3d 20 74 2e 67 65 6f 6d 65 74  frame = t.geomet
24e0: 72 79 29 29 0d 0a 57 48 45 52 45 20 66 5f 74 61  ry))..WHERE f_ta
24f0: 62 6c 65 5f 6e 61 6d 65 20 3d 20 27 78 78 27 20  ble_name = 'xx' 
2500: 41 4e 44 20 6f 72 69 67 69 6e 5f 72 6f 77 69 64  AND origin_rowid
2510: 20 3d 20 31 3b 0d 0a 2d 2d 2d 2d 2d 2d 2d 2d 2d   = 1;..---------
2520: 2d 2d 2d 2d 2d 2d 2d 0d 0a 31 34 38 30 39 39 31  -------..1480991
2530: 20 28 69 6e 20 31 32 2e 36 31 37 20 73 65 63 6f   (in 12.617 seco
2540: 6e 64 73 29 0d 0a 0d 0a 0d 0a 55 50 44 41 54 45  nds)......UPDATE
2550: 20 78 78 20 53 45 54 20 67 65 6f 6d 20 3d 20 28   xx SET geom = (
2560: 53 45 4c 45 43 54 20 53 54 5f 53 75 62 64 69 76  SELECT ST_Subdiv
2570: 69 64 65 28 67 65 6f 6d 65 74 72 79 29 20 46 52  ide(geometry) FR
2580: 4f 4d 20 74 75 73 63 61 6e 79 29 20 57 48 45 52  OM tuscany) WHER
2590: 45 20 69 64 20 3d 20 31 3b 0d 0a 53 45 4c 45 43  E id = 1;..SELEC
25a0: 54 20 43 6f 75 6e 74 28 2a 29 0d 0a 46 52 4f 4d  T Count(*)..FROM
25b0: 20 45 6c 65 6d 65 6e 74 61 72 79 47 65 6f 6d 65   ElementaryGeome
25c0: 74 72 69 65 73 20 41 53 20 74 0d 0a 4a 4f 49 4e  tries AS t..JOIN
25d0: 20 63 69 76 69 63 69 20 41 53 20 63 20 4f 4e 20   civici AS c ON 
25e0: 28 53 54 5f 49 6e 74 65 72 73 65 63 74 73 28 63  (ST_Intersects(c
25f0: 2e 67 65 6f 6d 65 74 72 79 2c 20 74 2e 67 65 6f  .geometry, t.geo
2600: 6d 65 74 72 79 29 20 3d 20 31 0d 0a 20 20 20 20  metry) = 1..    
2610: 20 41 4e 44 20 63 2e 72 6f 77 69 64 20 49 4e 20   AND c.rowid IN 
2620: 28 0d 0a 20 20 20 20 20 20 20 20 20 53 45 4c 45  (..         SELE
2630: 43 54 20 72 6f 77 69 64 20 46 52 4f 4d 20 53 70  CT rowid FROM Sp
2640: 61 74 69 61 6c 49 6e 64 65 78 0d 0a 20 20 20 20  atialIndex..    
2650: 20 20 20 20 20 57 48 45 52 45 20 66 5f 74 61 62       WHERE f_tab
2660: 6c 65 5f 6e 61 6d 65 20 3d 20 27 63 69 76 69 63  le_name = 'civic
2670: 69 27 20 41 4e 44 20 73 65 61 72 63 68 5f 66 72  i' AND search_fr
2680: 61 6d 65 20 3d 20 74 2e 67 65 6f 6d 65 74 72 79  ame = t.geometry
2690: 29 29 0d 0a 57 48 45 52 45 20 66 5f 74 61 62 6c  ))..WHERE f_tabl
26a0: 65 5f 6e 61 6d 65 20 3d 20 27 78 78 27 20 41 4e  e_name = 'xx' AN
26b0: 44 20 6f 72 69 67 69 6e 5f 72 6f 77 69 64 20 3d  D origin_rowid =
26c0: 20 31 3b 0d 0a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d   1;..-----------
26d0: 2d 2d 2d 2d 2d 0d 0a 31 34 38 30 39 39 31 20 28  -----..1480991 (
26e0: 69 6e 20 39 2e 35 33 34 20 73 65 63 6f 6e 64 73  in 9.534 seconds
26f0: 29 0d 0a 3c 2f 76 65 72 62 61 74 69 6d 3e 0d 0a  )..</verbatim>..
2700: 3c 62 3e 51 75 69 63 6b 20 65 76 61 6c 75 61 74  <b>Quick evaluat
2710: 69 6f 6e 3c 2f 62 3e 3a 0d 0a 3c 75 6c 3e 0d 0a  ion</b>:..<ul>..
2720: 3c 6c 69 3e 3c 62 3e 53 54 5f 53 75 62 64 69 76  <li><b>ST_Subdiv
2730: 69 64 65 3c 2f 62 3e 20 64 65 66 69 6e 69 74 65  ide</b> definite
2740: 6c 79 20 69 73 20 74 68 65 20 74 6f 6f 6c 20 6f  ly is the tool o
2750: 66 20 74 68 65 20 74 72 61 64 65 20 77 65 20 77  f the trade we w
2760: 65 72 65 20 73 65 61 72 63 68 69 6e 67 20 66 6f  ere searching fo
2770: 72 2e 3c 62 72 3e 0d 0a 49 74 20 65 66 66 65 63  r.<br>..It effec
2780: 74 69 76 65 6c 79 20 61 6c 6c 6f 77 73 20 74 6f  tively allows to
2790: 20 72 65 73 6f 6c 76 65 20 61 20 63 6f 6d 70 75   resolve a compu
27a0: 74 61 74 69 6f 6e 61 6c 6c 79 20 68 61 72 64 20  tationally hard 
27b0: 70 72 6f 62 6c 65 6d 20 69 6e 20 61 20 73 75 72  problem in a sur
27c0: 70 72 69 73 69 6e 67 6c 79 20 71 75 69 63 6b 20  prisingly quick 
27d0: 74 69 6d 65 2e 3c 62 72 3e 0d 0a 50 61 73 73 69  time.<br>..Passi
27e0: 6e 67 20 66 72 6f 6d 20 6d 61 6e 79 20 6c 6f 6e  ng from many lon
27f0: 67 20 68 6f 75 72 73 20 74 6f 20 6a 75 73 74 20  g hours to just 
2800: 61 20 68 61 6e 64 66 75 6c 20 6f 66 20 73 65 63  a handful of sec
2810: 6f 6e 64 73 20 69 73 20 61 20 73 69 6d 70 6c 79  onds is a simply
2820: 20 61 73 74 6f 6e 69 73 68 69 6e 67 20 69 6d 70   astonishing imp
2830: 72 6f 76 65 6d 65 6e 74 2e 3c 62 72 3e 3c 2f 6c  rovement.<br></l
2840: 69 3e 0d 0a 3c 6c 69 3e 73 75 62 64 69 76 69 64  i>..<li>subdivid
2850: 69 6e 67 20 74 68 65 20 69 6e 69 74 69 61 6c 20  ing the initial 
2860: 68 75 67 65 20 50 6f 6c 79 67 6f 6e 20 69 6e 74  huge Polygon int
2870: 6f 20 6d 61 6e 79 20 73 69 6d 70 6c 65 72 2f 73  o many simpler/s
2880: 6d 61 6c 6c 65 72 20 70 61 72 74 73 20 68 61 73  maller parts has
2890: 20 73 65 76 65 72 61 6c 20 62 65 6e 65 66 69 63   several benefic
28a0: 69 61 6c 20 65 66 66 65 63 74 73 3a 0d 0a 3c 75  ial effects:..<u
28b0: 6c 3e 0d 0a 3c 6c 69 3e 74 68 65 20 74 69 6d 65  l>..<li>the time
28c0: 20 72 65 71 75 69 72 65 64 20 74 6f 20 63 6f 6d   required to com
28d0: 70 75 74 65 20 61 20 70 6f 69 6e 74 2d 74 6f 2d  pute a point-to-
28e0: 70 6f 6c 79 67 6f 6e 20 69 6e 74 65 72 73 65 63  polygon intersec
28f0: 74 69 6f 6e 20 71 75 69 63 6b 6c 79 20 69 6e 63  tion quickly inc
2900: 72 65 61 73 65 73 20 77 69 74 68 20 74 68 65 20  reases with the 
2910: 6e 75 6d 62 65 72 20 6f 66 20 56 65 72 74 69 63  number of Vertic
2920: 65 73 2e 3c 62 72 3e 0d 0a 53 6d 61 6c 6c 65 72  es.<br>..Smaller
2930: 20 70 6f 6c 79 67 6f 6e 73 20 61 72 65 20 6f 62   polygons are ob
2940: 76 69 6f 75 73 6c 79 20 6d 75 63 68 20 6d 6f 72  viously much mor
2950: 65 20 66 61 73 74 65 72 20 74 6f 20 62 65 20 65  e faster to be e
2960: 76 61 6c 75 61 74 65 64 20 74 68 61 6e 20 62 69  valuated than bi
2970: 67 67 65 72 20 6f 6e 65 73 2e 3c 2f 6c 69 3e 0d  gger ones.</li>.
2980: 0a 3c 6c 69 3e 73 6d 61 6c 6c 65 72 20 70 6f 6c  .<li>smaller pol
2990: 79 67 6f 6e 73 20 64 6f 65 73 6e 27 74 20 73 68  ygons doesn't sh
29a0: 61 64 6f 77 20 74 68 65 20 53 70 61 74 69 61 6c  adow the Spatial
29b0: 20 49 6e 64 65 78 2c 20 74 68 61 74 20 63 61 6e   Index, that can
29c0: 20 62 65 20 65 66 66 69 63 69 65 6e 74 6c 79 20   be efficiently 
29d0: 71 75 65 72 69 65 64 2e 3c 2f 6c 69 3e 0d 0a 3c  queried.</li>..<
29e0: 2f 75 6c 3e 3c 2f 6c 69 3e 0d 0a 3c 6c 69 3e 75  /ul></li>..<li>u
29f0: 73 69 6e 67 20 3c 62 3e 56 69 72 74 75 61 6c 45  sing <b>VirtualE
2a00: 6c 65 6d 65 6e 74 61 72 79 3c 2f 62 3e 20 69 6e  lementary</b> in
2a10: 20 6f 72 64 65 72 20 74 6f 20 70 72 6f 63 65 73   order to proces
2a20: 73 20 61 6c 6c 20 73 75 62 2d 70 61 72 74 73 20  s all sub-parts 
2a30: 6f 6e 65 20 62 79 20 6f 6e 65 20 73 75 72 65 6c  one by one surel
2a40: 79 20 69 6d 70 6f 73 65 73 20 73 6f 6d 65 20 66  y imposes some f
2a50: 75 72 74 68 65 72 20 63 6f 6d 70 6c 65 78 69 74  urther complexit
2a60: 79 20 74 6f 20 6f 75 72 20 53 51 4c 20 71 75 65  y to our SQL que
2a70: 72 69 65 73 2e 3c 62 72 3e 0d 0a 42 75 74 20 61  ries.<br>..But a
2a80: 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  t the end of the
2a90: 20 64 61 79 20 69 74 20 63 6f 6e 66 69 72 6d 73   day it confirms
2aa0: 20 74 6f 20 62 65 20 61 20 76 65 72 79 20 65 66   to be a very ef
2ab0: 66 69 63 69 65 6e 74 20 6d 65 63 68 61 6e 69 73  ficient mechanis
2ac0: 6d 2e 3c 2f 6c 69 3e 0d 0a 3c 6c 69 3e 3c 62 3e  m.</li>..<li><b>
2ad0: 4e 6f 74 65 3c 2f 62 3e 3a 20 53 70 61 74 69 61  Note</b>: Spatia
2ae0: 4c 69 74 65 20 69 73 20 6e 6f 74 20 50 6f 73 74  Lite is not Post
2af0: 47 49 53 2c 20 61 6e 64 20 53 51 4c 69 74 65 33  GIS, and SQLite3
2b00: 20 69 73 20 6e 6f 74 20 50 6f 73 74 67 72 65 53   is not PostgreS
2b10: 51 4c 3b 20 74 68 65 69 72 20 72 65 73 70 65 63  QL; their respec
2b20: 74 69 76 65 20 69 6e 74 65 72 6e 61 6c 20 61 72  tive internal ar
2b30: 63 68 69 74 65 63 74 75 72 65 73 20 73 69 67 6e  chitectures sign
2b40: 69 66 69 63 61 6e 74 6c 79 20 64 69 66 66 65 72  ificantly differ
2b50: 20 75 6e 64 65 72 20 6d 61 6e 79 20 61 73 70 65   under many aspe
2b60: 63 74 73 2e 3c 62 72 3e 0d 0a 4d 6f 72 65 20 73  cts.<br>..More s
2b70: 70 65 63 69 66 69 63 61 6c 6c 79 2c 20 53 51 4c  pecifically, SQL
2b80: 69 74 65 20 69 73 20 63 6f 6d 70 6c 65 74 65 6c  ite is completel
2b90: 79 20 75 6e 61 62 6c 65 20 74 6f 20 73 75 70 70  y unable to supp
2ba0: 6f 72 74 20 61 72 72 61 79 73 20 6f 72 20 6d 75  ort arrays or mu
2bb0: 6c 74 69 2d 72 6f 77 20 72 65 73 75 6c 74 73 2c  lti-row results,
2bc0: 20 73 6f 20 75 73 69 6e 67 20 56 69 72 74 75 61   so using Virtua
2bd0: 6c 45 6c 65 6d 65 6e 74 61 72 79 20 69 73 20 61  lElementary is a
2be0: 6e 20 61 62 73 6f 6c 75 74 65 6c 79 20 6e 65 63  n absolutely nec
2bf0: 65 73 73 61 72 79 20 77 6f 72 6b 61 72 6f 75 6e  essary workaroun
2c00: 64 2e 3c 2f 6c 69 3e 0d 0a 3c 2f 75 6c 3e 0d 0a  d.</li>..</ul>..
2c10: 3c 68 33 3e 41 20 66 69 6e 61 6c 20 65 78 70 65  <h3>A final expe
2c20: 72 69 6d 65 6e 74 61 6c 20 61 74 74 65 6d 70 74  rimental attempt
2c30: 20 62 61 73 65 64 20 6f 6e 20 61 20 70 75 72 65   based on a pure
2c40: 20 53 51 4c 20 61 70 70 72 6f 61 63 68 3c 2f 68   SQL approach</h
2c50: 33 3e 0d 0a 54 68 65 72 65 20 69 73 20 61 6e 20  3>..There is an 
2c60: 61 6c 74 65 72 6e 61 74 69 76 65 20 6d 65 63 68  alternative mech
2c70: 61 6e 69 73 6d 20 62 61 73 65 64 20 6f 6e 20 70  anism based on p
2c80: 75 72 65 20 53 51 4c 20 61 6e 64 20 6e 6f 74 20  ure SQL and not 
2c90: 72 65 71 75 69 72 69 6e 67 20 74 6f 20 75 73 65  requiring to use
2ca0: 20 6e 6f 72 20 45 6c 65 6d 65 6e 74 61 72 79 47   nor ElementaryG
2cb0: 65 6f 6d 65 74 72 69 65 73 20 6e 65 69 74 68 65  eometries neithe
2cc0: 72 20 61 20 63 6f 6e 76 65 6e 69 65 6e 63 65 20  r a convenience 
2cd0: 54 61 62 6c 65 3a 0d 0a 3c 76 65 72 62 61 74 69  Table:..<verbati
2ce0: 6d 3e 0d 0a 57 49 54 48 20 52 45 43 55 52 53 49  m>..WITH RECURSI
2cf0: 56 45 20 6d 61 67 69 63 28 6e 2c 20 67 65 6f 6d  VE magic(n, geom
2d00: 29 20 41 53 20 28 0d 0a 20 20 20 20 56 41 4c 55  ) AS (..    VALU
2d10: 45 53 28 31 2c 20 28 53 45 4c 45 43 54 20 53 54  ES(1, (SELECT ST
2d20: 5f 53 75 62 64 69 76 69 64 65 28 67 65 6f 6d 65  _Subdivide(geome
2d30: 74 72 79 2c 20 32 30 34 38 29 20 46 52 4f 4d 20  try, 2048) FROM 
2d40: 74 75 73 63 61 6e 79 29 29 0d 0a 20 20 20 20 55  tuscany))..    U
2d50: 4e 49 4f 4e 20 41 4c 4c 0d 0a 20 20 20 20 53 45  NION ALL..    SE
2d60: 4c 45 43 54 20 6e 20 2b 20 31 2c 20 67 65 6f 6d  LECT n + 1, geom
2d70: 0d 0a 20 20 20 20 46 52 4f 4d 20 67 65 6f 6d 5f  ..    FROM geom_
2d80: 69 6e 64 0d 0a 20 20 20 20 57 48 45 52 45 20 53  ind..    WHERE S
2d90: 54 5f 47 65 6f 6d 65 74 72 79 4e 28 67 65 6f 6d  T_GeometryN(geom
2da0: 2c 20 6e 29 20 49 53 20 4e 4f 54 20 4e 55 4c 4c  , n) IS NOT NULL
2db0: 0d 0a 29 0d 0a 53 45 4c 45 43 54 20 6e 2c 20 53  ..)..SELECT n, S
2dc0: 54 5f 47 65 6f 6d 65 74 72 79 4e 28 67 65 6f 6d  T_GeometryN(geom
2dd0: 2c 20 6e 29 20 46 52 4f 4d 20 6d 61 67 69 63 3b  , n) FROM magic;
2de0: 0d 0a 3c 2f 76 65 72 62 61 74 69 6d 3e 0d 0a 3c  ..</verbatim>..<
2df0: 75 6c 3e 0d 0a 3c 6c 69 3e 77 65 20 73 69 6d 70  ul>..<li>we simp
2e00: 6c 79 20 68 61 76 65 20 74 6f 20 77 72 69 74 65  ly have to write
2e10: 20 61 20 3c 62 3e 52 65 63 75 72 73 69 76 65 20   a <b>Recursive 
2e20: 53 51 4c 20 51 75 65 72 79 3c 2f 62 3e 20 69 6e  SQL Query</b> in
2e30: 20 6f 72 64 65 72 20 74 6f 20 67 65 74 20 61 20   order to get a 
2e40: 6d 75 6c 74 69 2d 72 6f 77 20 72 65 73 75 6c 74  multi-row result
2e50: 73 65 74 2c 20 77 69 74 68 20 61 20 64 69 73 74  set, with a dist
2e60: 69 6e 63 74 20 72 6f 77 20 66 6f 72 20 65 61 63  inct row for eac
2e70: 68 20 65 6c 65 6d 65 6e 74 61 72 79 20 70 61 72  h elementary par
2e80: 74 2e 3c 2f 6c 69 3e 0d 0a 3c 6c 69 3e 69 74 20  t.</li>..<li>it 
2e90: 65 66 66 65 63 74 69 76 65 6c 79 20 77 6f 72 6b  effectively work
2ea0: 73 2c 20 62 75 74 20 73 65 65 6d 73 20 74 6f 20  s, but seems to 
2eb0: 62 65 20 73 6c 6f 77 65 72 20 74 68 61 6e 20 45  be slower than E
2ec0: 6c 65 6d 65 6e 74 61 72 79 47 65 6f 6d 65 74 72  lementaryGeometr
2ed0: 69 65 73 2e 3c 2f 6c 69 3e 0d 0a 3c 6c 69 3e 61  ies.</li>..<li>a
2ee0: 6e 64 20 65 76 65 6e 20 6d 6f 72 65 20 72 65 6c  nd even more rel
2ef0: 65 76 61 6e 74 2c 20 49 20 77 61 73 20 75 6e 61  evant, I was una
2f00: 62 6c 65 20 74 6f 20 77 72 69 74 65 20 61 20 63  ble to write a c
2f10: 6f 6d 70 6c 65 74 65 20 51 75 65 72 79 20 64 65  omplete Query de
2f20: 74 65 72 6d 69 6e 69 6e 67 20 61 6c 6c 20 69 6e  termining all in
2f30: 74 65 72 73 65 63 74 69 6f 6e 73 20 62 65 74 77  tersections betw
2f40: 65 65 6e 20 50 6f 69 6e 74 73 20 61 6e 64 20 50  een Points and P
2f50: 6f 6c 79 67 6f 6e 73 20 69 6e 20 61 20 72 65 61  olygons in a rea
2f60: 73 6f 6e 61 62 6c 65 20 74 69 6d 65 2e 3c 62 72  sonable time.<br
2f70: 3e 0d 0a 49 20 63 61 6e 20 73 69 6d 70 6c 79 20  >..I can simply 
2f80: 73 75 70 70 6f 73 65 20 74 68 61 74 20 74 68 65  suppose that the
2f90: 20 51 75 65 72 79 20 4f 70 74 69 6d 69 7a 65 72   Query Optimizer
2fa0: 20 6f 66 20 53 51 4c 69 74 65 20 77 69 6c 6c 20   of SQLite will 
2fb0: 65 6e 64 20 75 70 20 69 6e 20 69 67 6e 6f 72 69  end up in ignori
2fc0: 6e 67 20 74 68 65 20 53 70 61 74 69 61 6c 20 49  ng the Spatial I
2fd0: 6e 64 65 78 2c 20 74 68 75 73 20 63 6f 6d 70 6c  ndex, thus compl
2fe0: 65 74 65 6c 79 20 76 61 6e 69 73 68 69 6e 67 20  etely vanishing 
2ff0: 74 68 65 20 75 73 65 66 75 6c 6e 65 73 73 20 6f  the usefulness o
3000: 66 20 74 68 69 73 20 61 70 70 72 6f 61 63 68 2e  f this approach.
3010: 3c 62 72 3e 0d 0a 4d 61 79 20 77 65 6c 6c 20 62  <br>..May well b
3020: 65 20 74 68 61 74 20 73 6f 6d 65 6f 6e 65 20 65  e that someone e
3030: 6c 73 65 20 63 6f 75 6c 64 20 73 75 63 63 65 65  lse could succee
3040: 64 20 77 68 65 72 65 20 49 20 66 61 69 6c 65 64  d where I failed
3050: 3b 20 61 6e 79 20 66 75 72 74 68 65 72 20 74 68  ; any further th
3060: 69 72 64 2d 70 61 72 74 79 20 69 6e 76 65 73 74  ird-party invest
3070: 69 67 61 74 69 6f 6e 20 6f 6e 20 74 68 69 73 20  igation on this 
3080: 73 70 65 63 69 66 69 63 20 61 72 65 61 20 69 73  specific area is
3090: 20 73 75 72 65 6c 79 20 77 65 6c 63 6f 6d 65 2e   surely welcome.
30a0: 3c 2f 6c 69 3e 0d 0a 3c 2f 75 6c 3e 0d 0a 3c 62  </li>..</ul>..<b
30b0: 72 3e 3c 62 72 3e 0d 0a 3c 68 72 3e 0d 0a 3c 62  r><br>..<hr>..<b
30c0: 72 3e 3c 62 72 3e 0d 0a 3c 74 61 62 6c 65 20 77  r><br>..<table w
30d0: 69 64 74 68 3d 22 31 30 30 25 22 3e 3c 74 72 3e  idth="100%"><tr>
30e0: 0d 0a 3c 74 64 20 77 69 64 74 68 3d 22 33 33 25  ..<td width="33%
30f0: 22 20 61 6c 69 67 6e 3d 22 6c 65 66 74 22 3e 3c  " align="left"><
3100: 2f 74 64 3e 0d 0a 3c 74 64 20 61 6c 69 67 6e 3d  /td>..<td align=
3110: 22 63 65 6e 74 65 72 22 3e 3c 61 20 68 72 65 66  "center"><a href
3120: 3d 22 68 74 74 70 73 3a 2f 2f 77 77 77 2e 67 61  ="https://www.ga
3130: 69 61 2d 67 69 73 2e 69 74 2f 66 6f 73 73 69 6c  ia-gis.it/fossil
3140: 2f 6c 69 62 73 70 61 74 69 61 6c 69 74 65 2f 77  /libspatialite/w
3150: 69 6b 69 3f 6e 61 6d 65 3d 34 2e 33 2e 30 2b 64  iki?name=4.3.0+d
3160: 6f 63 22 3e 62 61 63 6b 20 74 6f 20 69 6e 64 65  oc">back to inde
3170: 78 3c 2f 61 3e 3c 2f 74 64 3e 0d 0a 3c 74 64 20  x</a></td>..<td 
3180: 77 69 64 74 68 3d 22 33 33 25 22 20 61 6c 69 67  width="33%" alig
3190: 6e 3d 22 72 69 67 68 74 22 3e 3c 2f 74 64 3e 0d  n="right"></td>.
31a0: 0a 3c 2f 74 72 3e 3c 2f 74 61 62 6c 65 3e 0a 5a  .</tr></table>.Z
31b0: 20 66 63 33 38 32 61 38 62 39 35 30 34 61 30 38   fc382a8b9504a08
31c0: 34 65 65 66 33 63 38 65 31 62 38 30 30 35 64 66  4eef3c8e1b8005df
31d0: 33 0a                                            3.