Hex Artifact Content
Not logged in

Artifact 0383ed4bde1b810757943a8d87105847f5d984e9:

Wiki page [KNN] by sandro 2015-12-13 15:14:05.
0000: 44 20 32 30 31 35 2d 31 32 2d 31 33 54 31 35 3a  D 2015-12-13T15:
0010: 31 34 3a 30 35 2e 37 39 34 0a 4c 20 4b 4e 4e 0a  14:05.794.L KNN.
0020: 50 20 35 30 62 65 30 30 64 63 65 32 62 66 62 30  P 50be00dce2bfb0
0030: 33 39 38 33 62 65 37 30 61 63 34 36 30 66 61 36  3983be70ac460fa6
0040: 62 34 61 36 38 32 33 64 33 32 0a 55 20 73 61 6e  b4a6823d32.U san
0050: 64 72 6f 0a 57 20 31 39 31 37 30 0a 3c 74 61 62  dro.W 19170.<tab
0060: 6c 65 20 63 65 6c 6c 73 70 61 63 69 6e 67 3d 22  le cellspacing="
0070: 31 32 22 20 77 69 64 74 68 3d 22 31 30 30 25 22  12" width="100%"
0080: 3e 0d 0a 3c 74 72 3e 3c 74 64 20 63 6f 6c 73 70  >..<tr><td colsp
0090: 61 6e 3d 22 32 22 3e 0d 0a 3c 74 61 62 6c 65 20  an="2">..<table 
00a0: 77 69 64 74 68 3d 22 31 30 30 25 22 20 62 67 63  width="100%" bgc
00b0: 6f 6c 6f 72 3d 22 23 66 30 66 30 66 38 22 3e 0d  olor="#f0f0f8">.
00c0: 0a 3c 74 72 3e 3c 74 64 20 61 6c 69 67 6e 3d 22  .<tr><td align="
00d0: 63 65 6e 74 65 72 22 3e 0d 0a 3c 68 31 3e 56 69  center">..<h1>Vi
00e0: 72 74 75 61 6c 4b 4e 4e 3a 20 61 20 71 75 69 63  rtualKNN: a quic
00f0: 6b 20 69 6e 74 72 6f 3c 2f 68 31 3e 0d 0a 3c 2f  k intro</h1>..</
0100: 74 64 3e 3c 2f 74 72 3e 3c 2f 74 61 62 6c 65 3e  td></tr></table>
0110: 0d 0a 3c 74 61 62 6c 65 20 77 69 64 74 68 3d 22  ..<table width="
0120: 31 30 30 25 22 3e 3c 74 72 3e 0d 0a 3c 74 64 20  100%"><tr>..<td 
0130: 77 69 64 74 68 3d 22 33 33 25 22 20 61 6c 69 67  width="33%" alig
0140: 6e 3d 22 6c 65 66 74 22 3e 3c 2f 74 64 3e 0d 0a  n="left"></td>..
0150: 3c 74 64 20 61 6c 69 67 6e 3d 22 63 65 6e 74 65  <td align="cente
0160: 72 22 3e 3c 61 20 68 72 65 66 3d 22 68 74 74 70  r"><a href="http
0170: 73 3a 2f 2f 77 77 77 2e 67 61 69 61 2d 67 69 73  s://www.gaia-gis
0180: 2e 69 74 2f 66 6f 73 73 69 6c 2f 6c 69 62 73 70  .it/fossil/libsp
0190: 61 74 69 61 6c 69 74 65 2f 77 69 6b 69 3f 6e 61  atialite/wiki?na
01a0: 6d 65 3d 6d 69 73 63 2d 64 6f 63 73 22 3e 62 61  me=misc-docs">ba
01b0: 63 6b 20 74 6f 20 69 6e 64 65 78 3c 2f 61 3e 3c  ck to index</a><
01c0: 2f 74 64 3e 0d 0a 3c 74 64 20 77 69 64 74 68 3d  /td>..<td width=
01d0: 22 33 33 25 22 20 61 6c 69 67 6e 3d 22 72 69 67  "33%" align="rig
01e0: 68 74 22 3e 3c 2f 74 64 3e 0d 0a 3c 2f 74 72 3e  ht"></td>..</tr>
01f0: 3c 2f 74 61 62 6c 65 3e 3c 62 72 3e 0d 0a 3c 74  </table><br>..<t
0200: 61 62 6c 65 20 62 67 63 6f 6c 6f 72 3d 22 23 64  able bgcolor="#d
0210: 30 65 30 66 66 22 20 77 69 64 74 68 3d 22 31 30  0e0ff" width="10
0220: 30 25 22 3e 0d 0a 3c 74 72 3e 3c 74 64 20 61 6c  0%">..<tr><td al
0230: 69 67 6e 3d 22 63 65 6e 74 65 72 22 3e 0d 0a 3c  ign="center">..<
0240: 62 3e 43 72 65 64 69 74 73 3c 2f 62 3e 3c 62 72  b>Credits</b><br
0250: 3e 0d 0a 44 65 76 65 6c 6f 70 6d 65 6e 74 20 6f  >..Development o
0260: 66 20 3c 62 3e 3c 69 3e 56 69 72 74 75 61 6c 4b  f <b><i>VirtualK
0270: 4e 4e 3c 2f 69 3e 3c 2f 62 3e 20 68 61 73 20 62  NN</i></b> has b
0280: 65 65 6e 20 65 6e 74 69 72 65 6c 79 20 66 75 6e  een entirely fun
0290: 64 65 64 20 62 79 0d 0a 3c 61 20 68 72 65 66 3d  ded by..<a href=
02a0: 22 68 74 74 70 3a 2f 2f 65 6e 2e 77 69 6b 69 70  "http://en.wikip
02b0: 65 64 69 61 2e 6f 72 67 2f 77 69 6b 69 2f 54 75  edia.org/wiki/Tu
02c0: 73 63 61 6e 79 22 3e 54 75 73 63 61 6e 79 20 52  scany">Tuscany R
02d0: 65 67 69 6f 6e 3c 2f 61 3e 20 2d 20 54 65 72 72  egion</a> - Terr
02e0: 69 74 6f 72 69 61 6c 20 61 6e 64 20 45 6e 76 69  itorial and Envi
02f0: 72 6f 6e 6d 65 6e 74 61 6c 20 49 6e 66 6f 72 6d  ronmental Inform
0300: 61 74 69 6f 6e 20 53 79 73 74 65 6d 3c 62 72 3e  ation System<br>
0310: 0d 0a 3c 61 20 68 72 65 66 3d 22 68 74 74 70 3a  ..<a href="http:
0320: 2f 2f 77 77 77 2e 72 65 67 69 6f 6e 65 2e 74 6f  //www.regione.to
0330: 73 63 61 6e 61 2e 69 74 2f 61 6d 62 69 65 6e 74  scana.it/ambient
0340: 65 65 74 65 72 72 69 74 6f 72 69 6f 2f 67 65 6f  eeterritorio/geo
0350: 67 72 61 66 69 61 67 65 6f 6c 6f 67 69 61 2f 69  grafiageologia/i
0360: 6e 64 65 78 2e 68 74 6d 6c 22 3e 52 65 67 69 6f  ndex.html">Regio
0370: 6e 65 20 54 6f 73 63 61 6e 61 3c 2f 61 3e 20 2d  ne Toscana</a> -
0380: 20 53 65 74 74 6f 72 65 20 53 69 73 74 65 6d 61   Settore Sistema
0390: 20 49 6e 66 6f 72 6d 61 74 69 76 6f 20 54 65 72   Informativo Ter
03a0: 72 69 74 6f 72 69 61 6c 65 20 65 64 20 41 6d 62  ritoriale ed Amb
03b0: 69 65 6e 74 61 6c 65 2e 0d 0a 3c 2f 74 64 3e 3c  ientale...</td><
03c0: 2f 74 72 3e 3c 2f 74 61 62 6c 65 3e 3c 62 72 3e  /tr></table><br>
03d0: 0d 0a 3c 68 72 3e 3c 62 72 3e 0d 0a 3c 74 61 62  ..<hr><br>..<tab
03e0: 6c 65 20 77 69 64 74 68 3d 22 31 30 30 25 22 20  le width="100%" 
03f0: 62 67 63 6f 6c 6f 72 3d 22 23 66 66 66 66 64 66  bgcolor="#ffffdf
0400: 22 20 63 65 6c 6c 73 70 61 63 69 6e 67 3d 22 34  " cellspacing="4
0410: 22 20 63 65 6c 6c 70 61 64 64 69 6e 67 3d 22 38  " cellpadding="8
0420: 22 3e 0d 0a 3c 74 72 3e 3c 74 64 3e 3c 62 3e 57  ">..<tr><td><b>W
0430: 68 61 74 20 69 73 20 74 68 65 20 4b 4e 4e 20 28  hat is the KNN (
0440: 3c 69 3e 4b 2d 4e 65 61 72 65 73 74 20 4e 65 69  <i>K-Nearest Nei
0450: 67 68 62 6f 72 73 3c 2f 69 3e 29 20 70 72 6f 62  ghbors</i>) prob
0460: 6c 65 6d 20 3f 3c 2f 62 3e 3c 2f 74 64 3e 3c 2f  lem ?</b></td></
0470: 74 72 3e 0d 0a 3c 74 72 3e 3c 74 64 3e 49 6d 61  tr>..<tr><td>Ima
0480: 67 69 6e 65 20 61 20 73 65 74 20 6f 66 20 61 72  gine a set of ar
0490: 62 69 74 72 61 72 79 20 67 65 6f 6d 65 74 72 69  bitrary geometri
04a0: 65 73 3b 20 77 68 69 63 68 20 6d 61 79 20 77 65  es; which may we
04b0: 6c 6c 20 62 65 2c 20 61 20 76 65 72 79 20 68 75  ll be, a very hu
04c0: 67 65 20 64 61 74 61 73 65 74 20 63 6f 6e 74 61  ge dataset conta
04d0: 69 6e 69 6e 67 20 73 6f 6d 65 20 6d 69 6c 6c 69  ining some milli
04e0: 6f 6e 73 20 6f 66 20 66 65 61 74 75 72 65 73 2e  ons of features.
04f0: 3c 62 72 3e 0d 0a 4e 6f 77 20 69 6d 61 67 69 6e  <br>..Now imagin
0500: 65 20 74 68 61 74 20 66 6f 72 20 77 68 61 74 65  e that for whate
0510: 76 65 72 20 72 65 61 73 6f 6e 20 79 6f 75 20 61  ver reason you a
0520: 72 65 20 69 6e 74 65 72 65 73 74 65 64 20 69 6e  re interested in
0530: 20 71 75 69 63 6b 6c 79 20 69 64 65 6e 74 69 66   quickly identif
0540: 79 69 6e 67 20 61 6c 6c 20 66 65 61 74 75 72 65  ying all feature
0550: 73 20 77 69 74 68 69 6e 20 61 20 20 63 6c 6f 73  s within a  clos
0560: 65 20 73 70 61 74 69 61 6c 20 70 72 6f 78 69 6d  e spatial proxim
0570: 69 74 79 20 74 6f 20 61 6e 20 61 72 62 69 74 72  ity to an arbitr
0580: 61 72 79 20 6c 6f 63 61 74 69 6f 6e 2e 3c 62 72  ary location.<br
0590: 3e 0d 0a 54 68 69 73 20 69 73 20 74 68 65 20 74  >..This is the t
05a0: 79 70 69 63 61 6c 20 3c 62 3e 4b 4e 4e 3c 2f 62  ypical <b>KNN</b
05b0: 3e 20 70 72 6f 62 6c 65 6d 2e 3c 2f 74 64 3e 3c  > problem.</td><
05c0: 2f 74 72 3e 0d 0a 3c 74 72 3e 3c 74 64 3e 4a 75  /tr>..<tr><td>Ju
05d0: 73 74 20 66 65 77 20 70 72 61 63 74 69 63 61 6c  st few practical
05e0: 20 72 65 61 6c 20 77 6f 72 6c 64 20 65 78 61 6d   real world exam
05f0: 70 6c 65 73 3a 0d 0a 3c 75 6c 3e 0d 0a 3c 6c 69  ples:..<ul>..<li
0600: 3e 53 75 70 70 6f 73 65 20 77 68 69 6c 65 20 75  >Suppose while u
0610: 73 69 6e 67 20 61 20 73 6d 61 72 74 70 68 6f 6e  sing a smartphon
0620: 65 20 61 70 70 3a 20 79 6f 75 72 20 63 75 72 72  e app: your curr
0630: 65 6e 74 20 67 65 6f 67 72 61 70 68 69 63 20 6c  ent geographic l
0640: 6f 63 61 74 69 6f 6e 20 69 73 20 77 65 6c 6c 20  ocation is well 
0650: 6b 6e 6f 77 6e 20 62 79 20 74 68 65 20 69 6e 74  known by the int
0660: 65 67 72 61 74 65 64 20 47 50 53 20 73 65 6e 73  egrated GPS sens
0670: 6f 72 2c 20 73 6f 20 79 6f 75 20 61 72 65 20 73  or, so you are s
0680: 69 6d 70 6c 79 20 65 78 70 65 63 74 69 6e 67 20  imply expecting 
0690: 74 68 61 74 20 74 68 65 20 61 70 70 20 77 69 6c  that the app wil
06a0: 6c 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74 65  l immediately te
06b0: 6c 6c 20 77 68 65 72 65 20 79 6f 75 20 61 72 65  ll where you are
06c0: 20 28 61 6e 64 20 74 68 65 20 65 78 61 63 74 20   (and the exact 
06d0: 6c 6f 63 61 74 69 6f 6e 20 6f 66 29 20 74 68 65  location of) the
06e0: 20 6e 65 61 72 65 73 74 20 66 75 65 6c 20 73 74   nearest fuel st
06f0: 61 74 69 6f 6e 73 20 28 68 6f 74 65 6c 73 2c 20  ations (hotels, 
0700: 72 65 73 74 61 75 72 61 6e 74 73 20 65 74 63 29  restaurants etc)
0710: 2e 3c 2f 6c 69 3e 0d 0a 3c 6c 69 3e 53 75 70 70  .</li>..<li>Supp
0720: 6f 73 65 20 61 20 7a 6f 6f 6c 6f 67 69 63 61 6c  ose a zoological
0730: 2f 65 63 6f 6c 6f 67 69 63 61 6c 20 73 74 75 64  /ecological stud
0740: 79 3a 20 79 6f 75 20 6b 6e 6f 77 20 77 68 65 72  y: you know wher
0750: 65 20 61 72 65 20 6c 6f 63 61 74 65 64 20 74 68  e are located th
0760: 65 20 6e 65 73 74 69 6e 67 20 61 72 65 61 73 20  e nesting areas 
0770: 6f 66 20 73 6f 6d 65 20 72 61 72 65 20 77 61 74  of some rare wat
0780: 65 72 20 62 69 72 64 20 73 70 65 63 69 65 73 2c  er bird species,
0790: 20 61 6c 73 6f 20 74 68 65 20 6c 6f 63 61 74 69   also the locati
07a0: 6f 6e 20 6f 66 20 61 6c 6c 20 72 69 76 65 72 73  on of all rivers
07b0: 2c 20 6c 61 6b 65 73 2c 20 70 6f 6e 64 73 20 61  , lakes, ponds a
07c0: 6e 64 20 6d 61 72 73 68 6c 61 6e 64 73 20 69 6e  nd marshlands in
07d0: 20 74 68 65 20 61 72 65 61 20 79 6f 75 20 61 72   the area you ar
07e0: 65 20 69 6e 74 65 72 65 73 74 65 64 20 69 6e 2e  e interested in.
07f0: 3c 62 72 3e 0d 0a 4e 6f 77 20 79 6f 75 20 61 72  <br>..Now you ar
0800: 65 20 61 74 74 65 6d 70 74 69 6e 67 20 74 6f 20  e attempting to 
0810: 67 65 74 20 61 20 72 65 61 73 6f 6e 61 62 6c 65  get a reasonable
0820: 20 61 73 73 6f 63 69 61 74 69 6f 6e 20 62 65 74   association bet
0830: 77 65 65 6e 20 6e 65 73 74 69 6e 67 20 61 72 65  ween nesting are
0840: 61 73 20 61 6e 64 20 66 65 65 64 69 6e 67 20 7a  as and feeding z
0850: 6f 6e 65 73 20 62 61 73 65 64 20 6f 6e 20 6d 69  ones based on mi
0860: 6e 69 6d 61 6c 20 64 69 73 74 61 6e 63 65 20 63  nimal distance c
0870: 72 69 74 65 72 69 61 2e 3c 2f 6c 69 3e 0d 0a 3c  riteria.</li>..<
0880: 2f 75 6c 3e 0d 0a 3c 2f 74 64 3e 3c 2f 74 72 3e  /ul>..</td></tr>
0890: 0d 0a 3c 74 72 3e 3c 74 64 3e 57 68 65 72 65 62  ..<tr><td>Whereb
08a0: 79 2c 20 69 6e 20 62 6f 74 68 20 63 61 73 65 73  y, in both cases
08b0: 20 79 6f 75 20 63 61 6e 27 74 20 73 61 66 65 6c   you can't safel
08c0: 79 20 69 64 65 6e 74 69 66 79 20 61 20 72 65 61  y identify a rea
08d0: 73 6f 6e 61 62 6c 65 20 3c 69 3e 3c 75 3e 6d 69  sonable <i><u>mi
08e0: 6e 69 6d 61 6c 20 72 61 64 69 75 73 3c 2f 75 3e  nimal radius</u>
08f0: 3c 2f 69 3e 20 73 6f 20 74 6f 20 75 73 65 66 75  </i> so to usefu
0900: 6c 6c 79 20 72 65 73 74 72 69 63 74 20 79 6f 75  lly restrict you
0910: 72 20 73 65 61 72 63 68 20 62 79 2c 20 73 69 6e  r search by, sin
0920: 63 65 20 79 6f 75 20 63 61 6e 27 74 20 6d 61 6b  ce you can't mak
0930: 65 20 61 6e 79 20 76 61 6c 69 64 20 61 73 73 75  e any valid assu
0940: 6d 70 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65  mption about the
0950: 20 67 65 6f 67 72 61 70 68 69 63 20 64 69 73 70   geographic disp
0960: 65 72 73 69 6f 6e 20 6f 66 20 79 6f 75 72 20 74  ersion of your t
0970: 61 72 67 65 74 73 2e 3c 62 72 3e 0d 0a 54 68 65  argets.<br>..The
0980: 20 64 69 73 74 61 6e 63 65 73 20 6d 61 79 20 6e   distances may n
0990: 6f 74 69 63 65 61 62 6c 79 20 76 61 72 79 20 66  oticeably vary f
09a0: 72 6f 6d 20 63 61 73 65 20 74 6f 20 63 61 73 65  rom case to case
09b0: 2c 20 61 6e 64 20 79 6f 75 20 68 61 76 65 6e 27  , and you haven'
09c0: 74 20 61 6e 79 20 69 64 65 61 20 61 62 6f 75 74  t any idea about
09d0: 20 74 68 69 73 2c 20 73 69 6e 63 65 20 69 74 20   this, since it 
09e0: 73 74 72 69 63 74 6c 79 20 64 65 70 65 6e 64 73  strictly depends
09f0: 20 6f 6e 20 74 68 65 20 61 63 74 75 61 6c 20 6c   on the actual l
0a00: 6f 63 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73  ocation of the s
0a10: 65 61 72 63 68 20 6f 72 69 67 69 6e 20 61 6e 64  earch origin and
0a20: 20 6f 6e 20 74 68 65 20 73 74 61 74 69 73 74 69   on the statisti
0a30: 63 61 6c 20 64 69 73 70 65 72 73 69 6f 6e 20 6f  cal dispersion o
0a40: 66 20 74 68 65 20 74 61 72 67 65 74 73 20 74 6f  f the targets to
0a50: 20 62 65 20 73 65 61 72 63 68 65 64 2e 3c 2f 74   be searched.</t
0a60: 64 3e 3c 2f 74 72 3e 0d 0a 3c 2f 74 61 62 6c 65  d></tr>..</table
0a70: 3e 3c 62 72 3e 0d 0a 3c 68 72 3e 3c 62 72 3e 0d  ><br>..<hr><br>.
0a80: 0a 3c 74 61 62 6c 65 20 77 69 64 74 68 3d 22 31  .<table width="1
0a90: 30 30 25 22 3e 0d 0a 3c 74 72 3e 3c 74 64 20 76  00%">..<tr><td v
0aa0: 61 6c 69 67 6e 3d 22 6d 69 64 64 6c 65 22 3e 0d  align="middle">.
0ab0: 0a 59 6f 75 20 63 61 6e 20 3c 61 20 68 72 65 66  .You can <a href
0ac0: 3d 22 68 74 74 70 73 3a 2f 2f 77 77 77 2e 67 61  ="https://www.ga
0ad0: 69 61 2d 67 69 73 2e 69 74 2f 67 61 69 61 2d 73  ia-gis.it/gaia-s
0ae0: 69 6e 73 2f 6b 6e 6e 2f 61 69 72 70 6f 72 74 73  ins/knn/airports
0af0: 2e 37 7a 22 3e 64 6f 77 6e 6c 6f 61 64 3c 2f 61  .7z">download</a
0b00: 3e 20 61 20 73 61 6d 70 6c 65 20 64 62 2d 66 69  > a sample db-fi
0b10: 6c 65 20 65 78 61 63 74 6c 79 20 63 6f 72 72 65  le exactly corre
0b20: 73 70 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65 20  sponding to the 
0b30: 65 78 61 6d 70 6c 65 73 20 77 65 27 6c 6c 20 75  examples we'll u
0b40: 73 65 20 69 6e 20 74 68 69 73 20 66 69 72 73 74  se in this first
0b50: 20 74 75 74 6f 72 69 61 6c 2e 3c 62 72 3e 3c 62   tutorial.<br><b
0b60: 72 3e 0d 0a 54 68 69 73 20 64 61 74 61 73 65 74  r>..This dataset
0b70: 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20   corresponds to 
0b80: 61 20 63 6f 6c 6c 65 63 74 69 6f 6e 20 6f 66 20  a collection of 
0b90: 61 62 6f 75 74 20 3c 62 3e 33 30 2e 30 30 30 20  about <b>30.000 
0ba0: 61 69 72 70 6f 72 74 73 3c 2f 62 3e 20 73 63 61  airports</b> sca
0bb0: 74 74 65 72 65 64 20 61 6c 6c 20 6f 76 65 72 20  ttered all over 
0bc0: 74 68 65 20 77 6f 72 6c 64 3b 20 74 68 65 20 6f  the world; the o
0bd0: 72 69 67 69 6e 61 6c 20 69 6e 70 75 74 20 73 68  riginal input sh
0be0: 61 70 65 66 69 6c 65 20 77 61 73 20 64 6f 77 6e  apefile was down
0bf0: 6c 6f 61 64 65 64 20 66 72 6f 6d 20 3c 61 20 68  loaded from <a h
0c00: 72 65 66 3d 22 68 74 74 70 73 3a 2f 2f 77 77 77  ref="https://www
0c10: 2e 73 68 61 72 65 67 65 6f 2e 61 63 2e 75 6b 2f  .sharegeo.ac.uk/
0c20: 68 61 6e 64 6c 65 2f 31 30 36 37 32 2f 33 30 33  handle/10672/303
0c30: 22 3e 68 65 72 65 3c 2f 61 3e 3c 62 72 3e 0d 0a  ">here</a><br>..
0c40: 54 68 65 20 72 65 66 65 72 65 6e 63 65 20 73 79  The reference sy
0c50: 73 74 65 6d 20 69 73 20 3c 62 3e 34 33 32 36 20  stem is <b>4326 
0c60: 57 47 53 38 34 3c 2f 62 3e 20 28 62 61 73 65 64  WGS84</b> (based
0c70: 20 6f 6e 20 3c 69 3e 6c 6f 6e 67 69 74 75 64 65   on <i>longitude
0c80: 3c 2f 69 3e 20 61 6e 64 20 3c 69 3e 6c 61 74 69  </i> and <i>lati
0c90: 74 75 64 65 3c 2f 69 3e 20 61 6e 67 6c 65 73 29  tude</i> angles)
0ca0: 2e 0d 0a 3c 62 72 3e 3c 62 72 3e 3c 62 72 3e 3c  ...<br><br><br><
0cb0: 62 72 3e 0d 0a 54 68 65 20 6d 61 70 20 6f 6e 20  br>..The map on 
0cc0: 74 68 65 20 72 69 67 68 74 20 73 68 6f 77 73 20  the right shows 
0cd0: 74 68 65 20 67 65 6f 67 72 61 70 68 69 63 20 63  the geographic c
0ce0: 6f 6e 74 65 78 74 20 6f 66 20 74 68 69 73 20 66  ontext of this f
0cf0: 69 72 73 74 20 4b 4e 4e 20 65 78 61 6d 70 6c 65  irst KNN example
0d00: 2e 3c 2f 74 64 3e 0d 0a 3c 74 64 3e 3c 69 6d 67  .</td>..<td><img
0d10: 20 73 72 63 3d 22 68 74 74 70 73 3a 2f 2f 77 77   src="https://ww
0d20: 77 2e 67 61 69 61 2d 67 69 73 2e 69 74 2f 67 61  w.gaia-gis.it/ga
0d30: 69 61 2d 73 69 6e 73 2f 6b 6e 6e 2f 74 79 72 72  ia-sins/knn/tyrr
0d40: 68 65 6e 69 61 6e 2d 73 65 61 2e 70 6e 67 22 20  henian-sea.png" 
0d50: 61 6c 74 3d 22 54 79 72 72 68 65 6e 69 61 6e 20  alt="Tyrrhenian 
0d60: 53 65 61 22 3e 3c 2f 74 64 3e 0d 0a 3c 2f 74 61  Sea"></td>..</ta
0d70: 62 6c 65 3e 3c 62 72 3e 0d 0a 3c 68 32 3e 41 20  ble><br>..<h2>A 
0d80: 66 69 72 73 74 2c 20 73 69 6d 70 6c 65 20 61 6e  first, simple an
0d90: 64 20 76 65 72 79 20 69 6e 65 66 66 69 63 69 65  d very inefficie
0da0: 6e 74 2c 20 61 70 70 72 6f 61 63 68 3c 2f 68 32  nt, approach</h2
0db0: 3e 0d 0a 3c 76 65 72 62 61 74 69 6d 3e 0d 0a 53  >..<verbatim>..S
0dc0: 45 4c 45 43 54 20 67 65 6f 6e 61 6d 65 69 64 2c  ELECT geonameid,
0dd0: 20 6e 61 6d 65 2c 20 63 6f 75 6e 74 72 79 2c 20   name, country, 
0de0: 0d 0a 20 20 20 20 4d 69 6e 28 53 54 5f 44 69 73  ..    Min(ST_Dis
0df0: 74 61 6e 63 65 28 4d 61 6b 65 50 6f 69 6e 74 28  tance(MakePoint(
0e00: 31 30 2c 20 34 33 29 2c 20 67 65 6f 6d 2c 20 31  10, 43), geom, 1
0e10: 29 29 20 2f 20 31 30 30 30 2e 30 20 41 53 20 64  )) / 1000.0 AS d
0e20: 69 73 74 5f 6b 6d 0d 0a 46 52 4f 4d 20 61 69 72  ist_km..FROM air
0e30: 70 6f 72 74 73 3b 0d 0a 2d 2d 2d 2d 2d 2d 2d 2d  ports;..--------
0e40: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0e50: 2d 2d 2d 2d 2d 2d 2d 2d 0d 0a 36 32 39 39 36 32  --------..629962
0e60: 33 09 7c 20 4d 61 72 69 6e 61 20 44 69 20 43 61  3.| Marina Di Ca
0e70: 6d 70 6f 20 7c 20 49 54 20 7c 20 33 33 2e 30 34  mpo | IT | 33.04
0e80: 33 33 32 30 0d 0a 0d 0a 53 45 4c 45 43 54 20 67  3320....SELECT g
0e90: 65 6f 6e 61 6d 65 69 64 2c 20 6e 61 6d 65 2c 20  eonameid, name, 
0ea0: 63 6f 75 6e 74 72 79 2c 20 0d 0a 20 20 20 20 4d  country, ..    M
0eb0: 69 6e 28 53 54 5f 44 69 73 74 61 6e 63 65 28 4d  in(ST_Distance(M
0ec0: 61 6b 65 50 6f 69 6e 74 28 31 30 2c 20 34 33 29  akePoint(10, 43)
0ed0: 2c 20 67 65 6f 6d 2c 20 31 29 29 20 2f 20 31 30  , geom, 1)) / 10
0ee0: 30 30 2e 30 20 41 53 20 64 69 73 74 5f 6b 6d 0d  00.0 AS dist_km.
0ef0: 0a 46 52 4f 4d 20 61 69 72 70 6f 72 74 73 0d 0a  .FROM airports..
0f00: 57 48 45 52 45 20 67 65 6f 6e 61 6d 65 69 64 20  WHERE geonameid 
0f10: 4e 4f 54 20 49 4e 20 28 36 32 39 39 36 32 33 29  NOT IN (6299623)
0f20: 3b 0d 0a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ;..-------------
0f30: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0f40: 2d 2d 2d 2d 0d 0a 36 32 39 39 33 39 32 20 7c 20  ----..6299392 | 
0f50: 42 61 73 74 69 61 2d 50 6f 72 65 74 74 61 20 7c  Bastia-Poretta |
0f60: 20 46 52 20 7c 20 36 35 2e 32 32 36 35 37 33 0d   FR | 65.226573.
0f70: 0a 0d 0a 53 45 4c 45 43 54 20 67 65 6f 6e 61 6d  ...SELECT geonam
0f80: 65 69 64 2c 20 6e 61 6d 65 2c 20 63 6f 75 6e 74  eid, name, count
0f90: 72 79 2c 0d 0a 20 20 20 20 4d 69 6e 28 53 54 5f  ry,..    Min(ST_
0fa0: 44 69 73 74 61 6e 63 65 28 4d 61 6b 65 50 6f 69  Distance(MakePoi
0fb0: 6e 74 28 31 30 2c 20 34 33 29 2c 20 67 65 6f 6d  nt(10, 43), geom
0fc0: 2c 20 31 29 29 20 2f 20 31 30 30 30 2e 30 20 41  , 1)) / 1000.0 A
0fd0: 53 20 64 69 73 74 5f 6b 6d 0d 0a 46 52 4f 4d 20  S dist_km..FROM 
0fe0: 61 69 72 70 6f 72 74 73 0d 0a 57 48 45 52 45 20  airports..WHERE 
0ff0: 67 65 6f 6e 61 6d 65 69 64 20 4e 4f 54 20 49 4e  geonameid NOT IN
1000: 20 28 36 32 39 39 36 32 33 2c 20 36 32 39 39 33   (6299623, 62993
1010: 39 32 29 3b 0d 0a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  92);..----------
1020: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1030: 2d 2d 2d 2d 2d 2d 2d 0d 0a 36 32 39 39 36 32 38  -------..6299628
1040: 20 7c 20 50 69 73 61 20 2f 20 53 2e 20 47 69 75   | Pisa / S. Giu
1050: 73 74 6f 20 7c 20 49 54 09 7c 20 38 32 2e 33 38  sto | IT.| 82.38
1060: 37 30 31 34 0d 0a 0d 0a 53 45 4c 45 43 54 20 67  7014....SELECT g
1070: 65 6f 6e 61 6d 65 69 64 2c 20 6e 61 6d 65 2c 20  eonameid, name, 
1080: 63 6f 75 6e 74 72 79 2c 20 0d 0a 20 20 20 20 4d  country, ..    M
1090: 69 6e 28 53 54 5f 44 69 73 74 61 6e 63 65 28 4d  in(ST_Distance(M
10a0: 61 6b 65 50 6f 69 6e 74 28 31 30 2c 20 34 33 29  akePoint(10, 43)
10b0: 2c 20 67 65 6f 6d 2c 20 31 29 29 20 2f 20 31 30  , geom, 1)) / 10
10c0: 30 30 2e 30 20 41 53 20 64 69 73 74 5f 6b 6d 0d  00.0 AS dist_km.
10d0: 0a 46 52 4f 4d 20 61 69 72 70 6f 72 74 73 0d 0a  .FROM airports..
10e0: 57 48 45 52 45 20 67 65 6f 6e 61 6d 65 69 64 20  WHERE geonameid 
10f0: 4e 4f 54 20 49 4e 20 28 36 32 39 39 36 32 33 2c  NOT IN (6299623,
1100: 20 36 32 39 39 33 39 32 2c 20 36 32 39 39 36 32   6299392, 629962
1110: 38 29 3b 0d 0a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  8);..-----------
1120: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1130: 2d 2d 2d 2d 2d 2d 0d 0a 36 32 39 39 36 33 30 20  ------..6299630 
1140: 7c 20 47 72 6f 73 73 65 74 6f 20 41 69 72 70 6f  | Grosseto Airpo
1150: 72 74 20 7c 20 49 54 20 7c 20 39 31 2e 35 34 39  rt | IT | 91.549
1160: 37 37 33 0d 0a 0d 0a 53 45 4c 45 43 54 20 67 65  773....SELECT ge
1170: 6f 6e 61 6d 65 69 64 2c 20 6e 61 6d 65 2c 20 63  onameid, name, c
1180: 6f 75 6e 74 72 79 2c 20 0d 0a 20 20 20 20 4d 69  ountry, ..    Mi
1190: 6e 28 53 54 5f 44 69 73 74 61 6e 63 65 28 4d 61  n(ST_Distance(Ma
11a0: 6b 65 50 6f 69 6e 74 28 31 30 2c 20 34 33 29 2c  kePoint(10, 43),
11b0: 20 67 65 6f 6d 2c 20 31 29 29 20 2f 20 31 30 30   geom, 1)) / 100
11c0: 30 2e 30 20 41 53 20 64 69 73 74 5f 6b 6d 0d 0a  0.0 AS dist_km..
11d0: 46 52 4f 4d 20 61 69 72 70 6f 72 74 73 0d 0a 57  FROM airports..W
11e0: 48 45 52 45 20 67 65 6f 6e 61 6d 65 69 64 20 4e  HERE geonameid N
11f0: 4f 54 20 49 4e 20 28 36 32 39 39 36 32 33 2c 20  OT IN (6299623, 
1200: 36 32 39 39 33 39 32 2c 20 36 32 39 39 36 32 38  6299392, 6299628
1210: 2c 20 36 32 39 39 36 33 30 29 3b 0d 0a 2d 2d 2d  , 6299630);..---
1220: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1230: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0d 0a  --------------..
1240: 36 36 39 34 34 39 35 20 7c 20 43 6f 72 74 65 20  6694495 | Corte 
1250: 7c 20 46 52 20 7c 20 31 30 32 2e 38 31 39 37 37  | FR | 102.81977
1260: 38 0d 0a 3c 2f 76 65 72 62 61 74 69 6d 3e 0d 0a  8..</verbatim>..
1270: 57 65 20 63 6f 75 6c 64 20 65 66 66 65 63 74 69  We could effecti
1280: 76 65 6c 79 20 65 78 65 63 75 74 65 20 61 20 66  vely execute a f
1290: 69 72 73 74 20 53 51 4c 20 71 75 65 72 79 20 75  irst SQL query u
12a0: 73 69 6e 67 20 74 68 65 20 3c 62 3e 4d 69 6e 28  sing the <b>Min(
12b0: 29 3c 2f 62 3e 20 61 67 67 72 65 67 61 74 65 20  )</b> aggregate 
12c0: 66 75 6e 63 74 69 6f 6e 20 69 6e 20 6f 72 64 65  function in orde
12d0: 72 20 74 6f 20 69 64 65 6e 74 69 66 79 20 74 68  r to identify th
12e0: 65 20 63 6c 6f 73 65 73 74 20 61 69 72 70 6f 72  e closest airpor
12f0: 74 20 74 6f 20 61 6e 20 61 72 62 69 74 72 61 72  t to an arbitrar
1300: 79 20 70 6f 73 69 74 69 6f 6e 20 28 3c 69 3e 6c  y position (<i>l
1310: 61 74 69 74 75 64 65 3c 2f 69 3e 3d 3c 62 3e 34  atitude</i>=<b>4
1320: 33 2e 30 3c 2f 62 3e 2c 20 3c 69 3e 6c 6f 6e 67  3.0</b>, <i>long
1330: 69 74 75 64 65 3c 2f 69 3e 3d 3c 62 3e 31 30 2e  itude</i>=<b>10.
1340: 30 3c 2f 62 3e 29 20 6c 6f 63 61 74 65 64 20 69  0</b>) located i
1350: 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20  n the middle of 
1360: 74 68 65 20 54 79 72 72 68 65 6e 69 61 6e 20 53  the Tyrrhenian S
1370: 65 61 2e 3c 62 72 3e 0d 0a 54 68 65 6e 20 77 65  ea.<br>..Then we
1380: 20 63 6f 75 6c 64 20 63 6f 6e 74 69 6e 75 61 6c   could continual
1390: 6c 79 20 72 65 70 65 61 74 20 74 68 65 20 69 64  ly repeat the id
13a0: 65 6e 74 69 63 61 6c 20 71 75 65 72 79 2c 20 65  entical query, e
13b0: 61 63 68 20 74 69 6d 65 20 65 78 63 6c 75 64 69  ach time excludi
13c0: 6e 67 20 61 6c 6c 20 61 69 72 70 6f 72 74 73 20  ng all airports 
13d0: 77 65 27 76 65 20 61 6c 72 65 61 64 79 20 69 64  we've already id
13e0: 65 6e 74 69 66 69 65 64 20 69 6e 20 74 68 65 20  entified in the 
13f0: 70 72 65 76 69 6f 75 73 20 70 72 6f 63 65 73 73  previous process
1400: 65 64 20 73 74 65 70 73 2e 3c 62 72 3e 0d 0a 54  ed steps.<br>..T
1410: 68 65 20 66 69 6e 61 6c 20 72 65 73 75 6c 74 20  he final result 
1420: 77 69 6c 6c 20 62 65 20 61 20 66 75 6c 6c 20 6c  will be a full l
1430: 69 73 74 20 6f 66 20 74 68 65 20 66 69 72 73 74  ist of the first
1440: 20 66 69 76 65 20 61 69 72 70 6f 72 74 73 20 63   five airports c
1450: 6c 6f 73 65 73 74 20 74 6f 20 74 68 65 20 72 65  losest to the re
1460: 66 65 72 65 6e 63 65 20 6c 6f 63 61 74 69 6f 6e  ference location
1470: 20 6f 72 64 65 72 65 64 20 62 79 20 69 6e 63 72   ordered by incr
1480: 65 61 73 69 6e 67 20 64 69 73 74 61 6e 63 65 2e  easing distance.
1490: 0d 0a 3c 62 72 3e 3c 62 72 3e 0d 0a 3c 74 61 62  ..<br><br>..<tab
14a0: 6c 65 20 62 67 63 6f 6c 6f 72 3d 22 23 64 30 66  le bgcolor="#d0f
14b0: 66 62 30 22 20 63 65 6c 6c 73 70 61 63 69 6e 67  fb0" cellspacing
14c0: 3d 22 34 22 20 63 65 6c 6c 70 61 64 64 69 6e 67  ="4" cellpadding
14d0: 3d 22 34 22 20 62 6f 72 64 65 72 3d 22 31 22 3e  ="4" border="1">
14e0: 0d 0a 3c 74 72 3e 3c 74 64 20 62 67 63 6f 6c 6f  ..<tr><td bgcolo
14f0: 72 3d 22 23 63 30 64 64 61 6f 22 3e 3c 62 3e 72  r="#c0ddao"><b>r
1500: 61 6e 6b 3c 2f 62 3e 3c 2f 74 64 3e 3c 74 64 20  ank</b></td><td 
1510: 62 67 63 6f 6c 6f 72 3d 22 23 63 30 64 64 61 6f  bgcolor="#c0ddao
1520: 22 3e 3c 62 3e 67 65 6f 6e 61 6d 65 69 64 3c 2f  "><b>geonameid</
1530: 62 3e 3c 2f 74 64 3e 3c 74 64 20 62 67 63 6f 6c  b></td><td bgcol
1540: 6f 72 3d 22 23 63 30 64 64 61 6f 22 3e 3c 62 3e  or="#c0ddao"><b>
1550: 6e 61 6d 65 3c 2f 62 3e 3c 2f 74 64 3e 3c 74 64  name</b></td><td
1560: 20 62 67 63 6f 6c 6f 72 3d 22 23 63 30 64 64 61   bgcolor="#c0dda
1570: 6f 22 3e 3c 62 3e 63 6f 75 6e 74 72 79 3c 2f 62  o"><b>country</b
1580: 3e 3c 2f 74 64 3e 3c 74 64 20 62 67 63 6f 6c 6f  ></td><td bgcolo
1590: 72 3d 22 23 63 30 64 64 61 6f 22 3e 3c 62 3e 64  r="#c0ddao"><b>d
15a0: 69 73 74 5f 6b 6d 3c 2f 62 3e 3c 2f 74 64 3e 3c  ist_km</b></td><
15b0: 2f 74 72 3e 0d 0a 3c 74 72 3e 3c 74 64 20 61 6c  /tr>..<tr><td al
15c0: 69 67 6e 3d 22 72 69 67 68 74 22 3e 31 3c 2f 74  ign="right">1</t
15d0: 64 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69 67  d><td align="rig
15e0: 68 74 22 3e 36 32 39 39 36 32 33 3c 2f 74 64 3e  ht">6299623</td>
15f0: 3c 74 64 3e 4d 61 72 69 6e 61 20 44 69 20 43 61  <td>Marina Di Ca
1600: 6d 70 6f 3c 2f 74 64 3e 3c 74 64 3e 49 54 3c 2f  mpo</td><td>IT</
1610: 74 64 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69  td><td align="ri
1620: 67 68 74 22 3e 33 33 2e 30 34 33 33 32 30 3c 2f  ght">33.043320</
1630: 74 64 3e 3c 2f 74 72 3e 0d 0a 3c 74 72 3e 3c 74  td></tr>..<tr><t
1640: 64 20 61 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e  d align="right">
1650: 32 3c 2f 74 64 3e 3c 74 64 20 61 6c 69 67 6e 3d  2</td><td align=
1660: 22 72 69 67 68 74 22 3e 36 32 39 39 33 39 32 3c  "right">6299392<
1670: 2f 74 64 3e 3c 74 64 3e 42 61 73 74 69 61 2d 50  /td><td>Bastia-P
1680: 6f 72 65 74 74 61 3c 2f 74 64 3e 3c 74 64 3e 46  oretta</td><td>F
1690: 52 3c 2f 74 64 3e 3c 74 64 20 61 6c 69 67 6e 3d  R</td><td align=
16a0: 22 72 69 67 68 74 22 3e 36 35 2e 32 32 36 35 37  "right">65.22657
16b0: 33 3c 2f 74 64 3e 3c 2f 74 72 3e 0d 0a 3c 74 72  3</td></tr>..<tr
16c0: 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69 67 68  ><td align="righ
16d0: 74 22 3e 33 3c 2f 74 64 3e 3c 74 64 20 61 6c 69  t">3</td><td ali
16e0: 67 6e 3d 22 72 69 67 68 74 22 3e 36 32 39 39 36  gn="right">62996
16f0: 32 38 3c 2f 74 64 3e 3c 74 64 3e 50 69 73 61 20  28</td><td>Pisa 
1700: 2f 20 53 2e 20 47 69 75 73 74 6f 3c 2f 74 64 3e  / S. Giusto</td>
1710: 3c 74 64 3e 49 54 3c 2f 74 64 3e 3c 74 64 20 61  <td>IT</td><td a
1720: 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e 38 32 2e  lign="right">82.
1730: 33 38 37 30 31 34 3c 2f 74 64 3e 3c 2f 74 72 3e  387014</td></tr>
1740: 0d 0a 3c 74 72 3e 3c 74 64 20 61 6c 69 67 6e 3d  ..<tr><td align=
1750: 22 72 69 67 68 74 22 3e 34 3c 2f 74 64 3e 3c 74  "right">4</td><t
1760: 64 20 61 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e  d align="right">
1770: 36 32 39 39 36 33 30 3c 2f 74 64 3e 3c 74 64 3e  6299630</td><td>
1780: 47 72 6f 73 73 65 74 6f 20 41 69 72 70 6f 72 74  Grosseto Airport
1790: 3c 2f 74 64 3e 3c 74 64 3e 49 54 3c 2f 74 64 3e  </td><td>IT</td>
17a0: 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69 67 68 74  <td align="right
17b0: 22 3e 39 31 2e 35 34 39 37 37 33 3c 2f 74 64 3e  ">91.549773</td>
17c0: 3c 2f 74 72 3e 0d 0a 3c 74 72 3e 3c 74 64 20 61  </tr>..<tr><td a
17d0: 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e 35 3c 2f  lign="right">5</
17e0: 74 64 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69  td><td align="ri
17f0: 67 68 74 22 3e 36 36 39 34 34 39 35 3c 2f 74 64  ght">6694495</td
1800: 3e 3c 74 64 3e 43 6f 72 74 65 3c 2f 74 64 3e 3c  ><td>Corte</td><
1810: 74 64 3e 46 52 3c 2f 74 64 3e 3c 74 64 20 61 6c  td>FR</td><td al
1820: 69 67 6e 3d 22 72 69 67 68 74 22 3e 31 30 32 2e  ign="right">102.
1830: 38 31 39 37 37 38 3c 2f 74 64 3e 3c 2f 74 72 3e  819778</td></tr>
1840: 0d 0a 3c 2f 74 61 62 6c 65 3e 3c 62 72 3e 0d 0a  ..</table><br>..
1850: 53 75 63 68 20 61 20 73 69 6d 70 6c 65 20 61 70  Such a simple ap
1860: 70 72 6f 61 63 68 20 69 73 20 68 69 67 68 6c 79  proach is highly
1870: 20 69 6d 70 72 61 63 74 69 63 61 6c 2c 20 62 65   impractical, be
1880: 63 61 75 73 65 20 69 74 20 73 74 72 69 63 74 6c  cause it strictl
1890: 79 20 72 65 71 75 69 72 65 73 20 74 6f 20 68 61  y requires to ha
18a0: 6e 64 2d 77 72 69 74 65 20 73 65 76 65 72 61 6c  nd-write several
18b0: 20 3c 69 3e 61 64 20 68 6f 63 3c 2f 69 3e 20 53   <i>ad hoc</i> S
18c0: 51 4c 20 71 75 65 72 69 65 73 20 6f 6e 65 20 62  QL queries one b
18d0: 79 20 6f 6e 65 2e 3c 62 72 3e 0d 0a 57 72 69 74  y one.<br>..Writ
18e0: 69 6e 67 20 61 20 66 75 6c 6c 79 20 61 75 74 6f  ing a fully auto
18f0: 6d 61 74 65 64 20 53 51 4c 20 73 63 72 69 70 74  mated SQL script
1900: 20 69 73 6e 27 74 20 61 74 20 61 6c 6c 20 61 20   isn't at all a 
1910: 73 69 6d 70 6c 65 20 74 61 73 6b 2c 20 61 6e 64  simple task, and
1920: 20 73 6f 6d 65 20 6b 69 6e 64 20 6f 66 20 68 69   some kind of hi
1930: 67 68 65 72 20 6c 65 76 65 6c 20 73 63 72 69 70  gher level scrip
1940: 74 69 6e 67 20 28 65 2e 67 2e 20 50 79 74 68 6f  ting (e.g. Pytho
1950: 6e 29 20 77 6f 75 6c 64 20 62 65 20 65 61 73 69  n) would be easi
1960: 6c 79 20 72 65 71 75 69 72 65 64 20 69 6e 20 61  ly required in a
1970: 6e 79 20 72 65 61 6c 69 73 74 69 63 20 73 63 65  ny realistic sce
1980: 6e 61 72 69 6f 2e 3c 62 72 3e 0d 0a 54 68 65 20  nario.<br>..The 
1990: 61 75 74 6f 6d 61 74 69 6f 6e 20 6f 66 20 61 6c  automation of al
19a0: 6c 20 72 65 71 75 69 72 65 64 20 53 51 4c 20 71  l required SQL q
19b0: 75 65 72 69 65 73 20 69 73 6e 27 74 20 74 68 65  ueries isn't the
19c0: 20 6d 6f 73 74 20 63 72 69 74 69 63 61 6c 20 69   most critical i
19d0: 73 73 75 65 20 77 65 20 68 61 76 65 20 74 6f 20  ssue we have to 
19e0: 66 61 63 65 3b 20 74 68 65 20 72 65 61 6c 2c 20  face; the real, 
19f0: 63 72 69 74 69 63 61 6c 2c 20 62 6f 74 74 6c 65  critical, bottle
1a00: 6e 65 63 6b 20 77 69 74 68 20 74 68 69 73 20 73  neck with this s
1a10: 69 6d 70 6c 65 20 61 70 70 72 6f 61 63 68 2c 20  imple approach, 
1a20: 69 73 20 74 68 61 74 20 65 61 63 68 20 6f 66 20  is that each of 
1a30: 74 68 65 73 65 20 71 75 65 72 69 65 73 20 77 69  these queries wi
1a40: 6c 6c 20 63 61 75 73 65 20 61 6e 20 61 77 66 75  ll cause an awfu
1a50: 6c 20 3c 61 20 68 72 65 66 3d 22 68 74 74 70 73  l <a href="https
1a60: 3a 2f 2f 65 6e 2e 77 69 6b 69 70 65 64 69 61 2e  ://en.wikipedia.
1a70: 6f 72 67 2f 77 69 6b 69 2f 46 75 6c 6c 5f 74 61  org/wiki/Full_ta
1a80: 62 6c 65 5f 73 63 61 6e 22 3e 66 75 6c 6c 20 74  ble_scan">full t
1a90: 61 62 6c 65 20 73 63 61 6e 3c 2f 61 3e 3c 62 72  able scan</a><br
1aa0: 3e 0d 0a 54 68 69 73 20 77 69 6c 6c 20 62 65 20  >..This will be 
1ab0: 74 68 65 20 63 61 75 73 65 20 6f 66 20 61 6e 20  the cause of an 
1ac0: 69 6e 74 6f 6c 65 72 61 62 6c 79 20 73 6c 75 67  intolerably slug
1ad0: 67 69 73 68 20 70 65 72 66 6f 72 6d 61 6e 63 65  gish performance
1ae0: 2c 20 61 6e 64 20 74 68 65 20 6f 76 65 72 61 6c  , and the overal
1af0: 6c 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 77 69  l performance wi
1b00: 6c 6c 20 71 75 69 63 6b 6c 79 20 62 65 63 6f 6d  ll quickly becom
1b10: 65 20 77 6f 72 73 65 20 61 73 20 74 68 65 20 64  e worse as the d
1b20: 61 74 61 73 65 74 20 70 72 6f 67 72 65 73 73 69  ataset progressi
1b30: 76 65 6c 79 20 69 6e 63 72 65 61 73 65 73 20 69  vely increases i
1b40: 6e 20 73 69 7a 65 2e 0d 0a 3c 62 72 3e 3c 62 72  n size...<br><br
1b50: 3e 0d 0a 3c 62 3e 43 6f 6e 63 6c 75 73 69 6f 6e  >..<b>Conclusion
1b60: 3c 2f 62 3e 3a 3c 62 72 3e 0d 0a 53 6f 6d 65 20  </b>:<br>..Some 
1b70: 65 61 73 69 65 72 20 61 6e 64 20 73 6d 61 72 74  easier and smart
1b80: 65 72 20 61 70 70 72 6f 61 63 68 20 69 73 20 72  er approach is r
1b90: 65 71 75 69 72 65 64 3a 20 70 6f 73 73 69 62 6c  equired: possibl
1ba0: 79 20 6f 6e 65 20 74 61 6b 69 6e 67 20 66 75 6c  y one taking ful
1bb0: 6c 20 70 72 6f 66 69 74 20 66 72 6f 6d 20 61 6e  l profit from an
1bc0: 20 3c 62 3e 52 2a 54 52 65 65 20 53 70 61 74 69   <b>R*TRee Spati
1bd0: 61 6c 20 49 6e 64 65 78 3c 2f 62 3e 20 73 75 70  al Index</b> sup
1be0: 70 6f 72 74 69 6e 67 20 74 68 65 20 47 65 6f 6d  porting the Geom
1bf0: 65 74 72 69 65 73 20 74 6f 20 62 65 20 73 65 61  etries to be sea
1c00: 72 63 68 65 64 2e 3c 62 72 3e 3c 62 72 3e 0d 0a  rched.<br><br>..
1c10: 3c 68 72 3e 0d 0a 3c 68 32 3e 56 69 72 74 75 61  <hr>..<h2>Virtua
1c20: 6c 4b 4e 4e 3c 2f 68 32 3e 0d 0a 53 74 61 72 74  lKNN</h2>..Start
1c30: 69 6e 67 20 77 69 74 68 20 76 65 72 73 69 6f 6e  ing with version
1c40: 20 3c 62 3e 34 2e 34 2e 30 3c 2f 62 3e 20 53 70   <b>4.4.0</b> Sp
1c50: 61 74 69 61 4c 69 74 65 20 73 75 70 70 6f 72 74  atiaLite support
1c60: 73 20 61 20 3c 62 3e 56 69 72 74 75 61 6c 4b 4e  s a <b>VirtualKN
1c70: 4e 3c 2f 62 3e 20 3c 61 20 68 72 65 66 3d 22 68  N</b> <a href="h
1c80: 74 74 70 73 3a 2f 2f 77 77 77 2e 73 71 6c 69 74  ttps://www.sqlit
1c90: 65 2e 6f 72 67 2f 76 74 61 62 2e 68 74 6d 6c 22  e.org/vtab.html"
1ca0: 3e 56 69 72 74 75 61 6c 20 54 61 62 6c 65 3c 2f  >Virtual Table</
1cb0: 61 3e 20 73 70 65 63 69 66 69 63 61 6c 6c 79 20  a> specifically 
1cc0: 69 6e 74 65 6e 64 65 64 20 61 73 20 61 20 63 6f  intended as a co
1cd0: 6d 70 6c 65 74 65 20 61 6e 64 20 68 69 67 68 6c  mplete and highl
1ce0: 79 20 65 66 66 69 63 69 65 6e 74 20 73 6f 6c 75  y efficient solu
1cf0: 74 69 6f 6e 20 74 6f 20 74 68 65 20 3c 62 3e 4b  tion to the <b>K
1d00: 4e 4e 3c 2f 62 3e 20 70 72 6f 62 6c 65 6d 2e 0d  NN</b> problem..
1d10: 0a 3c 76 65 72 62 61 74 69 6d 3e 0d 0a 43 52 45  .<verbatim>..CRE
1d20: 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 4c  ATE VIRTUAL TABL
1d30: 45 20 6b 6e 6e 20 55 53 49 4e 47 20 56 69 72 74  E knn USING Virt
1d40: 75 61 6c 4b 4e 4e 28 29 3b 0d 0a 3c 2f 76 65 72  ualKNN();..</ver
1d50: 62 61 74 69 6d 3e 0d 0a 45 76 65 72 79 20 6e 65  batim>..Every ne
1d60: 77 20 64 62 2d 66 69 6c 65 20 62 65 69 6e 67 20  w db-file being 
1d70: 63 72 65 61 74 65 64 20 77 69 74 68 20 3c 62 3e  created with <b>
1d80: 34 2e 34 2e 30 3c 2f 62 3e 20 77 69 6c 6c 20 61  4.4.0</b> will a
1d90: 6c 77 61 79 73 20 61 75 74 6f 6d 61 74 69 63 61  lways automatica
1da0: 6c 6c 79 20 64 65 66 69 6e 65 20 61 20 3c 62 3e  lly define a <b>
1db0: 4b 4e 4e 3c 2f 62 3e 20 76 69 72 74 75 61 6c 20  KNN</b> virtual 
1dc0: 74 61 62 6c 65 2e 3c 62 72 3e 0d 0a 4f 6e 20 61  table.<br>..On a
1dd0: 6e 79 20 65 61 72 6c 69 65 72 20 76 65 72 73 69  ny earlier versi
1de0: 6f 6e 20 6f 66 20 61 20 53 70 61 74 69 61 4c 69  on of a SpatiaLi
1df0: 74 65 20 64 62 2d 66 69 6c 65 2c 20 79 6f 75 20  te db-file, you 
1e00: 63 61 6e 20 61 64 64 20 74 68 65 20 4b 4e 4e 20  can add the KNN 
1e10: 73 75 70 70 6f 72 74 20 62 79 20 6d 61 6e 75 61  support by manua
1e20: 6c 6c 79 20 65 78 65 63 75 74 69 6e 67 20 74 68  lly executing th
1e30: 65 20 61 62 6f 76 65 20 53 51 4c 20 73 74 61 74  e above SQL stat
1e40: 65 6d 65 6e 74 2e 3c 62 72 3e 0d 0a 3c 62 3e 4e  ement.<br>..<b>N
1e50: 6f 74 65 3c 2f 62 3e 3a 20 56 69 72 74 75 61 6c  ote</b>: Virtual
1e60: 4b 4e 4e 20 6e 65 63 65 73 73 61 72 69 6c 79 20  KNN necessarily 
1e70: 72 65 71 75 69 72 65 73 20 34 2e 34 2e 30 20 62  requires 4.4.0 b
1e80: 69 6e 61 72 79 20 73 75 70 70 6f 72 74 2c 20 73  inary support, s
1e90: 6f 20 61 6e 79 20 61 74 74 65 6d 70 74 20 74 6f  o any attempt to
1ea0: 20 6f 70 65 6e 20 61 20 64 62 2d 66 69 6c 65 20   open a db-file 
1eb0: 69 6e 63 6c 75 64 69 6e 67 20 61 20 56 69 72 74  including a Virt
1ec0: 75 61 6c 4b 4e 4e 20 74 61 62 6c 65 20 62 79 20  ualKNN table by 
1ed0: 75 73 69 6e 67 20 73 6f 6d 65 20 70 72 65 76 69  using some previ
1ee0: 6f 75 73 20 76 65 72 73 69 6f 6e 20 28 3c 3d 20  ous version (<= 
1ef0: 3c 62 3e 34 2e 33 2e 30 61 3c 2f 62 3e 29 20 77  <b>4.3.0a</b>) w
1f00: 69 6c 6c 20 73 75 72 65 6c 79 20 72 61 69 73 65  ill surely raise
1f10: 20 61 6e 20 65 72 72 6f 72 20 63 6f 6e 64 69 74   an error condit
1f20: 69 6f 6e 2e 0d 0a 3c 68 33 3e 41 20 66 69 72 73  ion...<h3>A firs
1f30: 74 20 62 61 73 69 63 61 6c 6c 79 20 73 69 6d 70  t basically simp
1f40: 6c 65 20 4b 4e 4e 20 71 75 65 72 79 3c 2f 68 33  le KNN query</h3
1f50: 3e 0d 0a 3c 76 65 72 62 61 74 69 6d 3e 0d 0a 53  >..<verbatim>..S
1f60: 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 6b 6e 6e  ELECT * FROM knn
1f70: 0d 0a 57 48 45 52 45 20 66 5f 74 61 62 6c 65 5f  ..WHERE f_table_
1f80: 6e 61 6d 65 20 3d 20 27 61 69 72 70 6f 72 74 73  name = 'airports
1f90: 27 20 41 4e 44 20 72 65 66 5f 67 65 6f 6d 65 74  ' AND ref_geomet
1fa0: 72 79 20 3d 20 4d 61 6b 65 50 6f 69 6e 74 28 31  ry = MakePoint(1
1fb0: 30 2c 20 34 33 29 3b 0d 0a 3c 2f 76 65 72 62 61  0, 43);..</verba
1fc0: 74 69 6d 3e 0d 0a 3c 74 61 62 6c 65 20 62 67 63  tim>..<table bgc
1fd0: 6f 6c 6f 72 3d 22 23 64 30 66 66 62 30 22 20 63  olor="#d0ffb0" c
1fe0: 65 6c 6c 73 70 61 63 69 6e 67 3d 22 34 22 20 63  ellspacing="4" c
1ff0: 65 6c 6c 70 61 64 64 69 6e 67 3d 22 34 22 20 62  ellpadding="4" b
2000: 6f 72 64 65 72 3d 22 31 22 3e 0d 0a 3c 74 72 3e  order="1">..<tr>
2010: 3c 74 64 20 62 67 63 6f 6c 6f 72 3d 22 23 63 30  <td bgcolor="#c0
2020: 64 64 61 6f 22 3e 3c 62 3e 66 5f 74 61 62 6c 65  ddao"><b>f_table
2030: 5f 6e 61 6d 65 3c 2f 62 3e 3c 2f 74 64 3e 3c 74  _name</b></td><t
2040: 64 20 62 67 63 6f 6c 6f 72 3d 22 23 63 30 64 64  d bgcolor="#c0dd
2050: 61 6f 22 3e 3c 62 3e 66 5f 67 65 6f 6d 65 74 72  ao"><b>f_geometr
2060: 79 5f 63 6f 6c 75 6d 6e 3c 2f 62 3e 3c 2f 74 64  y_column</b></td
2070: 3e 3c 74 64 20 62 67 63 6f 6c 6f 72 3d 22 23 63  ><td bgcolor="#c
2080: 30 64 64 61 6f 22 3e 3c 62 3e 72 65 66 5f 67 65  0ddao"><b>ref_ge
2090: 6f 6d 65 74 72 79 3c 2f 62 3e 3c 2f 74 64 3e 3c  ometry</b></td><
20a0: 74 64 20 62 67 63 6f 6c 6f 72 3d 22 23 63 30 64  td bgcolor="#c0d
20b0: 64 61 6f 22 3e 3c 62 3e 6d 61 78 5f 69 74 65 6d  dao"><b>max_item
20c0: 73 3c 2f 62 3e 3c 2f 74 64 3e 3c 74 64 20 62 67  s</b></td><td bg
20d0: 63 6f 6c 6f 72 3d 22 23 63 30 64 64 61 6f 22 3e  color="#c0ddao">
20e0: 3c 62 3e 70 6f 73 3c 2f 62 3e 3c 2f 74 64 3e 3c  <b>pos</b></td><
20f0: 74 64 20 62 67 63 6f 6c 6f 72 3d 22 23 63 30 64  td bgcolor="#c0d
2100: 64 61 6f 22 3e 3c 62 3e 66 69 64 3c 2f 62 3e 3c  dao"><b>fid</b><
2110: 2f 74 64 3e 3c 74 64 20 62 67 63 6f 6c 6f 72 3d  /td><td bgcolor=
2120: 22 23 63 30 64 64 61 6f 22 3e 3c 62 3e 64 69 73  "#c0ddao"><b>dis
2130: 74 61 6e 63 65 3c 2f 62 3e 3c 2f 74 64 3e 3c 2f  tance</b></td></
2140: 74 72 3e 0d 0a 3c 74 72 3e 3c 74 64 3e 61 69 72  tr>..<tr><td>air
2150: 70 6f 72 74 73 3c 2f 74 64 3e 3c 74 64 3e 67 65  ports</td><td>ge
2160: 6f 6d 3c 2f 74 64 3e 3c 74 64 3e 42 4c 4f 42 20  om</td><td>BLOB 
2170: 73 7a 3d 36 30 20 47 45 4f 4d 45 54 52 59 3c 2f  sz=60 GEOMETRY</
2180: 74 64 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69  td><td align="ri
2190: 67 68 74 22 3e 33 3c 2f 74 64 3e 3c 74 64 20 61  ght">3</td><td a
21a0: 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e 31 3c 2f  lign="right">1</
21b0: 74 64 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69  td><td align="ri
21c0: 67 68 74 22 3e 36 32 39 39 36 32 33 3c 2f 74 64  ght">6299623</td
21d0: 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69 67 68  ><td align="righ
21e0: 74 22 3e 33 33 30 34 33 2e 33 31 39 35 32 30 3c  t">33043.319520<
21f0: 2f 74 64 3e 3c 2f 74 72 3e 0d 0a 3c 74 72 3e 3c  /td></tr>..<tr><
2200: 74 64 3e 61 69 72 70 6f 72 74 73 3c 2f 74 64 3e  td>airports</td>
2210: 3c 74 64 3e 67 65 6f 6d 3c 2f 74 64 3e 3c 74 64  <td>geom</td><td
2220: 3e 42 4c 4f 42 20 73 7a 3d 36 30 20 47 45 4f 4d  >BLOB sz=60 GEOM
2230: 45 54 52 59 3c 2f 74 64 3e 3c 74 64 20 61 6c 69  ETRY</td><td ali
2240: 67 6e 3d 22 72 69 67 68 74 22 3e 33 3c 2f 74 64  gn="right">3</td
2250: 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69 67 68  ><td align="righ
2260: 74 22 3e 32 3c 2f 74 64 3e 3c 74 64 20 61 6c 69  t">2</td><td ali
2270: 67 6e 3d 22 72 69 67 68 74 22 3e 36 32 39 39 33  gn="right">62993
2280: 39 32 3c 2f 74 64 3e 3c 74 64 20 61 6c 69 67 6e  92</td><td align
2290: 3d 22 72 69 67 68 74 22 3e 36 35 32 32 36 2e 35  ="right">65226.5
22a0: 37 33 31 34 39 3c 2f 74 64 3e 3c 2f 74 72 3e 0d  73149</td></tr>.
22b0: 0a 3c 74 72 3e 3c 74 64 3e 61 69 72 70 6f 72 74  .<tr><td>airport
22c0: 73 3c 2f 74 64 3e 3c 74 64 3e 67 65 6f 6d 3c 2f  s</td><td>geom</
22d0: 74 64 3e 3c 74 64 3e 42 4c 4f 42 20 73 7a 3d 36  td><td>BLOB sz=6
22e0: 30 20 47 45 4f 4d 45 54 52 59 3c 2f 74 64 3e 3c  0 GEOMETRY</td><
22f0: 74 64 20 61 6c 69 67 6e 3d 22 72 69 67 68 74 22  td align="right"
2300: 3e 33 3c 2f 74 64 3e 3c 74 64 20 61 6c 69 67 6e  >3</td><td align
2310: 3d 22 72 69 67 68 74 22 3e 33 3c 2f 74 64 3e 3c  ="right">3</td><
2320: 74 64 20 61 6c 69 67 6e 3d 22 72 69 67 68 74 22  td align="right"
2330: 3e 36 32 39 39 36 32 38 3c 2f 74 64 3e 3c 74 64  >6299628</td><td
2340: 20 61 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e 38   align="right">8
2350: 32 33 38 37 2e 30 31 34 30 32 38 3c 2f 74 64 3e  2387.014028</td>
2360: 3c 2f 74 72 3e 0d 0a 3c 2f 74 61 62 6c 65 3e 3c  </tr>..</table><
2370: 62 72 3e 0d 0a 3c 75 6c 3e 0d 0a 3c 6c 69 3e 61  br>..<ul>..<li>a
2380: 20 56 69 72 74 75 61 6c 4b 4e 4e 20 71 75 65 72   VirtualKNN quer
2390: 79 20 63 6c 6f 73 65 6c 79 20 72 65 73 65 6d 62  y closely resemb
23a0: 6c 65 73 20 61 20 56 69 72 74 75 61 6c 53 70 61  les a VirtualSpa
23b0: 74 69 61 6c 49 6e 64 65 78 20 71 75 65 72 79 2e  tialIndex query.
23c0: 0d 0a 54 68 69 73 20 69 73 20 6e 6f 74 20 73 75  ..This is not su
23d0: 72 70 72 69 73 69 6e 67 2c 20 73 69 6e 63 65 20  rprising, since 
23e0: 62 6f 74 68 20 6f 66 20 74 68 65 6d 20 61 72 65  both of them are
23f0: 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 62 65 20   intended to be 
2400: 77 72 61 70 70 65 64 20 61 72 6f 75 6e 64 20 61  wrapped around a
2410: 6e 20 75 6e 64 65 72 6c 79 69 6e 67 20 3c 62 3e  n underlying <b>
2420: 52 2a 54 72 65 65 20 53 70 61 74 69 61 6c 20 49  R*Tree Spatial I
2430: 6e 64 65 78 3c 2f 62 3e 2e 3c 2f 6c 69 3e 20 0d  ndex</b>.</li> .
2440: 0a 3c 6c 69 3e 41 6e 79 20 76 61 6c 69 64 20 56  .<li>Any valid V
2450: 69 72 74 75 61 6c 4b 4e 4e 20 71 75 65 72 79 20  irtualKNN query 
2460: 6d 75 73 74 20 75 73 65 20 61 20 66 6f 72 6d 20  must use a form 
2470: 73 75 63 68 20 61 73 3a 0d 0a 3c 62 3e 57 48 45  such as:..<b>WHE
2480: 52 45 20 3c 69 3e 6b 6e 6e 2d 63 6f 6c 75 6d 6e  RE <i>knn-column
2490: 3c 2f 69 3e 20 3d 20 3c 69 3e 76 61 6c 75 65 3c  </i> = <i>value<
24a0: 2f 69 3e 20 41 4e 44 20 3c 69 3e 6b 6e 6e 2d 63  /i> AND <i>knn-c
24b0: 6f 6c 75 6d 6e 3c 2f 69 3e 20 3d 20 3c 69 3e 76  olumn</i> = <i>v
24c0: 61 6c 75 65 3c 2f 69 3e 20 2e 2e 2e 3c 2f 62 3e  alue</i> ...</b>
24d0: 3c 2f 6c 69 3e 0d 0a 3c 6c 69 3e 74 68 65 20 69  </li>..<li>the i
24e0: 6e 70 75 74 20 63 6f 6c 75 6d 6e 73 20 28 74 68  nput columns (th
24f0: 65 20 6f 6e 65 73 20 79 6f 75 20 63 61 6e 20 72  e ones you can r
2500: 65 66 65 72 65 6e 63 65 20 69 6e 74 6f 20 61 20  eference into a 
2510: 3c 62 3e 57 48 45 52 45 3c 2f 62 3e 20 63 6c 61  <b>WHERE</b> cla
2520: 75 73 65 29 20 61 72 65 3a 0d 0a 3c 75 6c 3e 0d  use) are:..<ul>.
2530: 0a 3c 6c 69 3e 3c 62 3e 66 5f 74 61 62 6c 65 5f  .<li><b>f_table_
2540: 6e 61 6d 65 3c 2f 62 3e 20 28 3c 69 3e 6d 61 6e  name</b> (<i>man
2550: 64 61 74 6f 72 79 3c 2f 69 3e 29 3c 62 72 3e 0d  datory</i>)<br>.
2560: 0a 6e 61 6d 65 20 6f 66 20 74 68 65 20 47 65 6f  .name of the Geo
2570: 54 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67  Table containing
2580: 20 74 68 65 20 47 65 6f 6d 65 74 72 69 65 73 20   the Geometries 
2590: 74 6f 20 62 65 20 73 65 61 72 63 68 65 64 2e 3c  to be searched.<
25a0: 2f 6c 69 3e 0d 0a 3c 6c 69 3e 3c 62 3e 66 5f 67  /li>..<li><b>f_g
25b0: 65 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e 3c 2f  eometry_column</
25c0: 62 3e 20 28 3c 69 3e 6f 70 74 69 6f 6e 61 6c 3c  b> (<i>optional<
25d0: 2f 69 3e 29 3c 62 72 3e 0d 0a 6e 61 6d 65 20 6f  /i>)<br>..name o
25e0: 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6f 66 20  f the column of 
25f0: 74 68 65 20 61 62 6f 76 65 20 74 61 62 6c 65 20  the above table 
2600: 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 47  containing the G
2610: 65 6f 6d 65 74 72 69 65 73 20 74 6f 20 62 65 20  eometries to be 
2620: 73 65 61 72 63 68 65 64 2e 0d 0a 3c 6f 6c 3e 0d  searched...<ol>.
2630: 0a 3c 6c 69 3e 49 66 20 74 68 65 20 74 61 62 6c  .<li>If the tabl
2640: 65 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 20  e identified by 
2650: 3c 62 3e 66 5f 74 61 62 6c 65 5f 6e 61 6d 65 3c  <b>f_table_name<
2660: 2f 62 3e 20 6a 75 73 74 20 63 6f 6e 74 61 69 6e  /b> just contain
2670: 73 20 61 20 73 69 6e 67 6c 65 20 47 65 6f 6d 65  s a single Geome
2680: 74 72 79 20 63 6f 6c 75 6d 6e 20 79 6f 75 20 63  try column you c
2690: 61 6e 20 73 61 66 65 6c 79 20 6f 6d 69 74 20 74  an safely omit t
26a0: 68 65 20 3c 62 3e 66 5f 67 65 6f 6d 65 74 72 79  he <b>f_geometry
26b0: 5f 63 6f 6c 75 6d 6e 3c 2f 62 3e 20 61 72 67 75  _column</b> argu
26c0: 6d 65 6e 74 20 28 69 74 20 77 69 6c 6c 20 62 65  ment (it will be
26d0: 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 73   automatically s
26e0: 65 74 29 2e 3c 2f 6c 69 3e 0d 0a 3c 6c 69 3e 49  et).</li>..<li>I
26f0: 66 2c 20 68 6f 77 65 76 65 72 2c 20 74 68 65 20  f, however, the 
2700: 74 61 62 6c 65 20 69 64 65 6e 74 69 66 69 65 64  table identified
2710: 20 62 79 20 3c 62 3e 66 5f 74 61 62 6c 65 5f 6e   by <b>f_table_n
2720: 61 6d 65 3c 2f 62 3e 20 63 6f 6e 74 61 69 6e 73  ame</b> contains
2730: 20 74 77 6f 20 28 6f 72 20 6d 6f 72 65 29 20 47   two (or more) G
2740: 65 6f 6d 65 74 72 79 20 63 6f 6c 75 6d 6e 73 20  eometry columns 
2750: 65 78 70 6c 69 63 69 74 6c 79 20 73 70 65 63 69  explicitly speci
2760: 66 79 69 6e 67 20 74 68 65 20 3c 62 3e 66 5f 67  fying the <b>f_g
2770: 65 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e 3c 2f  eometry_column</
2780: 62 3e 20 61 72 67 75 6d 65 6e 74 20 69 73 20 73  b> argument is s
2790: 74 72 69 63 74 6c 79 20 72 65 71 75 69 72 65 64  trictly required
27a0: 2c 20 74 6f 20 61 76 6f 69 64 20 61 6e 20 61 6d  , to avoid an am
27b0: 62 69 67 75 6f 75 73 20 64 65 66 69 6e 69 74 69  biguous definiti
27c0: 6f 6e 20 6f 66 20 74 68 65 20 73 65 61 72 63 68  on of the search
27d0: 20 63 6f 6e 74 65 78 74 2e 3c 2f 6c 69 3e 0d 0a   context.</li>..
27e0: 3c 6c 69 3e 49 6e 20 62 6f 74 68 20 63 61 73 65  <li>In both case
27f0: 73 20 3c 62 3e 66 5f 74 61 62 6c 65 5f 6e 61 6d  s <b>f_table_nam
2800: 65 3c 2f 62 3e 20 61 6e 64 20 3c 62 3e 66 5f 67  e</b> and <b>f_g
2810: 65 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e 3c 2f  eometry_column</
2820: 62 3e 20 6d 75 73 74 20 65 78 61 63 74 6c 79 20  b> must exactly 
2830: 6d 61 74 63 68 20 61 20 70 72 6f 70 65 72 6c 79  match a properly
2840: 20 64 65 66 69 6e 65 64 20 47 65 6f 6d 65 74 72   defined Geometr
2850: 79 20 63 6f 6c 75 6d 6e 20 73 75 70 70 6f 72 74  y column support
2860: 65 64 20 62 79 20 61 20 63 6f 72 72 65 73 70 6f  ed by a correspo
2870: 6e 64 69 6e 67 20 53 70 61 74 69 61 6c 20 49 6e  nding Spatial In
2880: 64 65 78 2e 3c 2f 6c 69 3e 0d 0a 3c 2f 6f 6c 3e  dex.</li>..</ol>
2890: 3c 2f 6c 69 3e 0d 0a 3c 6c 69 3e 3c 62 3e 72 65  </li>..<li><b>re
28a0: 66 5f 67 65 6f 6d 65 74 72 79 3c 2f 62 3e 20 28  f_geometry</b> (
28b0: 3c 69 3e 6d 61 6e 64 61 74 6f 72 79 3c 2f 69 3e  <i>mandatory</i>
28c0: 29 3c 62 72 3e 0d 0a 61 6e 79 20 61 72 62 69 74  )<br>..any arbit
28d0: 72 61 72 79 20 47 65 6f 6d 65 74 72 79 20 28 3c  rary Geometry (<
28e0: 62 3e 50 4f 49 4e 54 3c 2f 62 3e 2c 20 3c 62 3e  b>POINT</b>, <b>
28f0: 4c 49 4e 45 53 54 52 49 4e 47 3c 2f 62 3e 2c 20  LINESTRING</b>, 
2900: 3c 62 3e 50 4f 4c 59 47 4f 4e 3c 2f 62 3e 20 6f  <b>POLYGON</b> o
2910: 72 20 77 68 61 74 65 76 65 72 20 65 6c 73 65 29  r whatever else)
2920: 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 72 65 70   intended to rep
2930: 72 65 73 65 6e 74 20 74 68 65 20 6f 72 69 67 69  resent the origi
2940: 6e 20 6f 66 20 74 68 65 20 4b 4e 4e 20 73 65 61  n of the KNN sea
2950: 72 63 68 2e 3c 62 72 3e 0d 0a 54 68 69 73 20 47  rch.<br>..This G
2960: 65 6f 6d 65 74 72 79 20 6d 75 73 74 20 63 6f 6e  eometry must con
2970: 74 61 69 6e 20 74 68 65 20 73 61 6d 65 20 53 52  tain the same SR
2980: 49 44 20 61 73 20 74 68 65 20 74 61 72 67 65 74  ID as the target
2990: 20 47 65 6f 6d 65 74 72 69 65 73 20 74 6f 20 62   Geometries to b
29a0: 65 20 73 65 61 72 63 68 65 64 2e 3c 2f 6c 69 3e  e searched.</li>
29b0: 0d 0a 3c 6c 69 3e 3c 62 3e 6d 61 78 5f 69 74 65  ..<li><b>max_ite
29c0: 6d 73 3c 2f 62 3e 20 28 3c 69 3e 6f 70 74 69 6f  ms</b> (<i>optio
29d0: 6e 61 6c 3c 2f 69 3e 29 3c 62 72 3e 0d 0a 6d 61  nal</i>)<br>..ma
29e0: 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20  ximum number of 
29f0: 72 6f 77 73 20 74 6f 20 62 65 20 72 65 74 75 72  rows to be retur
2a00: 6e 65 64 20 69 6e 74 6f 20 74 68 65 20 72 65 73  ned into the res
2a10: 75 6c 74 73 65 74 2e 3c 62 72 3e 0d 0a 54 68 65  ultset.<br>..The
2a20: 20 76 61 6c 69 64 20 72 61 6e 67 65 20 69 73 20   valid range is 
2a30: 66 72 6f 6d 20 3c 62 3e 31 3c 2f 62 3e 20 74 6f  from <b>1</b> to
2a40: 20 3c 62 3e 31 30 32 34 3c 2f 62 3e 20 28 68 69   <b>1024</b> (hi
2a50: 67 68 65 72 20 76 61 6c 75 65 73 20 77 69 6c 6c  gher values will
2a60: 20 72 65 71 75 69 72 65 20 61 20 6c 6f 6e 67 65   require a longe
2a70: 72 20 74 69 6d 65 20 74 6f 20 62 65 20 70 72 6f  r time to be pro
2a80: 63 65 73 73 65 64 29 2e 3c 62 72 3e 0d 0a 42 79  cessed).<br>..By
2a90: 20 64 65 66 61 75 6c 74 20 6f 6e 6c 79 20 74 68   default only th
2aa0: 65 20 66 69 72 73 74 20 3c 62 3e 33 3c 2f 62 3e  e first <b>3</b>
2ab0: 20 72 65 73 75 6c 74 73 20 77 69 6c 6c 20 62 65   results will be
2ac0: 20 72 65 74 75 72 6e 65 64 2e 3c 2f 6c 69 3e 0d   returned.</li>.
2ad0: 0a 3c 2f 75 6c 3e 3c 2f 6c 69 3e 0d 0a 3c 6c 69  .</ul></li>..<li
2ae0: 3e 74 68 65 20 6f 75 74 70 75 74 20 63 6f 6c 75  >the output colu
2af0: 6d 6e 73 20 28 74 68 65 20 6f 6e 65 73 20 63 6f  mns (the ones co
2b00: 6e 74 61 69 6e 69 6e 67 20 76 61 6c 75 65 73 20  ntaining values 
2b10: 72 65 74 72 69 65 76 65 64 20 62 79 20 74 68 65  retrieved by the
2b20: 20 4b 4e 4e 20 73 65 61 72 63 68 29 20 61 72 65   KNN search) are
2b30: 3a 0d 0a 3c 75 6c 3e 0d 0a 3c 6c 69 3e 3c 62 3e  :..<ul>..<li><b>
2b40: 70 6f 73 3c 2f 62 3e 20 28 3c 69 3e 49 4e 54 45  pos</b> (<i>INTE
2b50: 47 45 52 3c 2f 69 3e 29 3c 62 72 3e 0d 0a 72 65  GER</i>)<br>..re
2b60: 6c 61 74 69 76 65 20 72 61 6e 6b 20 28 73 6f 72  lative rank (sor
2b70: 74 65 64 20 62 79 20 64 69 73 74 61 6e 63 65 29  ted by distance)
2b80: 3a 20 74 68 65 20 63 6c 6f 73 65 73 74 20 69 74  : the closest it
2b90: 65 6d 20 77 69 6c 6c 20 62 65 20 3c 62 3e 23 31  em will be <b>#1
2ba0: 3c 2f 62 3e 2c 20 74 68 65 20 73 65 63 6f 6e 64  </b>, the second
2bb0: 20 63 6c 6f 73 65 73 74 20 69 74 65 6d 20 77 69   closest item wi
2bc0: 6c 6c 20 62 65 20 3c 62 3e 23 32 3c 2f 62 3e 20  ll be <b>#2</b> 
2bd0: 61 6e 64 20 73 6f 20 6f 6e 2e 3c 2f 6c 69 3e 0d  and so on.</li>.
2be0: 0a 3c 6c 69 3e 3c 62 3e 66 69 64 3c 2f 62 3e 20  .<li><b>fid</b> 
2bf0: 28 3c 69 3e 49 4e 54 45 47 45 52 3c 2f 69 3e 29  (<i>INTEGER</i>)
2c00: 3c 62 72 3e 0d 0a 74 68 65 20 75 6e 69 71 75 65  <br>..the unique
2c10: 20 3c 62 3e 52 4f 57 49 44 3c 2f 62 3e 20 76 61   <b>ROWID</b> va
2c20: 6c 75 65 20 6f 66 20 74 68 65 20 63 6f 72 72 65  lue of the corre
2c30: 73 70 6f 6e 64 69 6e 67 20 66 65 61 74 75 72 65  sponding feature
2c40: 20 69 6e 74 6f 20 74 68 65 20 73 65 61 72 63 68   into the search
2c50: 65 64 20 47 65 6f 54 61 62 6c 65 2e 3c 2f 6c 69  ed GeoTable.</li
2c60: 3e 0d 0a 3c 6c 69 3e 3c 62 3e 64 69 73 74 61 6e  >..<li><b>distan
2c70: 63 65 3c 2f 62 3e 20 28 3c 69 3e 44 4f 55 42 4c  ce</b> (<i>DOUBL
2c80: 45 3c 2f 69 3e 29 3c 62 72 3e 0d 0a 74 68 65 20  E</i>)<br>..the 
2c90: 6d 69 6e 69 6d 75 6d 20 64 69 73 74 61 6e 63 65  minimum distance
2ca0: 20 62 65 74 77 65 65 6e 20 74 68 65 20 63 6f 72   between the cor
2cb0: 72 65 73 70 6f 6e 64 69 6e 67 20 66 65 61 74 75  responding featu
2cc0: 72 65 20 69 6e 74 6f 20 74 68 65 20 73 65 61 72  re into the sear
2cd0: 63 68 65 64 20 47 65 6f 54 61 62 6c 65 20 61 6e  ched GeoTable an
2ce0: 64 20 74 68 65 20 6f 72 69 67 69 6e 20 64 65 66  d the origin def
2cf0: 69 6e 65 64 20 62 79 20 3c 62 3e 72 65 66 5f 67  ined by <b>ref_g
2d00: 65 6f 6d 65 74 72 79 3c 2f 62 3e 2e 0d 0a 3c 6f  eometry</b>...<o
2d10: 6c 3e 0d 0a 3c 6c 69 3e 57 68 65 6e 20 74 68 65  l>..<li>When the
2d20: 20 64 65 66 69 6e 65 64 20 53 52 49 44 20 75 73   defined SRID us
2d30: 65 73 20 61 20 3c 62 3e 70 6c 61 6e 61 72 3c 2f  es a <b>planar</
2d40: 62 3e 20 28 61 6b 61 20 3c 69 3e 70 72 6f 6a 65  b> (aka <i>proje
2d50: 63 74 65 64 3c 2f 69 3e 29 20 72 65 66 65 72 65  cted</i>) refere
2d60: 6e 63 65 20 73 79 73 74 65 6d 2c 20 61 6c 6c 20  nce system, all 
2d70: 64 69 73 74 61 6e 63 65 73 20 77 69 6c 6c 20 62  distances will b
2d80: 65 20 72 65 74 75 72 6e 65 64 20 69 6e 20 74 68  e returned in th
2d90: 65 20 75 6e 69 74 73 20 64 65 66 69 6e 65 64 20  e units defined 
2da0: 62 79 20 74 68 65 20 70 72 6f 6a 65 63 74 69 6f  by the projectio
2db0: 6e 20 28 6d 65 74 65 72 73 2c 20 66 65 65 74 2c  n (meters, feet,
2dc0: 20 63 68 61 69 6e 73 20 65 74 63 2e 29 2e 3c 2f   chains etc.).</
2dd0: 6c 69 3e 0d 0a 3c 6c 69 3e 57 68 65 6e 20 75 73  li>..<li>When us
2de0: 69 6e 67 20 61 20 3c 62 3e 67 65 6f 67 72 61 70  ing a <b>geograp
2df0: 68 69 63 3c 2f 62 3e 20 74 79 70 65 20 28 3c 69  hic</b> type (<i
2e00: 3e 6c 6f 6e 67 69 74 75 64 65 3c 2f 69 3e 20 61  >longitude</i> a
2e10: 6e 64 20 3c 69 3e 6c 61 74 69 74 75 64 65 3c 2f  nd <i>latitude</
2e20: 69 3e 20 3c 62 3e 64 65 67 72 65 65 73 3c 2f 62  i> <b>degrees</b
2e30: 3e 29 2c 20 61 6c 6c 20 64 69 73 74 61 6e 63 65  >), all distance
2e40: 73 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 62 65  s will always be
2e50: 20 6d 65 61 73 75 72 65 64 20 69 6e 20 3c 62 3e   measured in <b>
2e60: 6d 65 74 65 72 73 3c 2f 62 3e 2c 20 77 69 74 68  meters</b>, with
2e70: 20 74 68 65 20 6d 6f 73 74 20 70 72 65 63 69 73   the most precis
2e80: 65 20 67 65 6f 64 65 74 69 63 20 66 6f 72 6d 75  e geodetic formu
2e90: 6c 61 73 20 62 65 69 6e 67 20 61 75 74 6f 6d 61  las being automa
2ea0: 74 69 63 61 6c 6c 79 20 61 70 70 6c 69 65 64 2e  tically applied.
2eb0: 3c 2f 6c 69 3e 0d 0a 3c 2f 6f 6c 3e 3c 2f 6c 69  </li>..</ol></li
2ec0: 3e 0d 0a 3c 2f 75 6c 3e 3c 2f 6c 69 3e 0d 0a 3c  >..</ul></li>..<
2ed0: 2f 75 6c 3e 3c 62 72 3e 0d 0a 3c 68 72 3e 0d 0a  /ul><br>..<hr>..
2ee0: 3c 68 33 3e 41 20 73 65 63 6f 6e 64 20 73 61 6d  <h3>A second sam
2ef0: 70 6c 65 20 6f 66 20 61 20 6d 6f 72 65 20 73 6f  ple of a more so
2f00: 70 68 69 73 74 69 63 61 74 65 64 20 4b 4e 4e 20  phisticated KNN 
2f10: 71 75 65 72 79 3c 2f 68 33 3e 0d 0a 3c 76 65 72  query</h3>..<ver
2f20: 62 61 74 69 6d 3e 0d 0a 53 45 4c 45 43 54 20 61  batim>..SELECT a
2f30: 2e 70 6f 73 2c 20 62 2e 6e 61 6d 65 2c 20 62 2e  .pos, b.name, b.
2f40: 63 6f 75 6e 74 72 79 2c 20 61 2e 64 69 73 74 61  country, a.dista
2f50: 6e 63 65 20 2f 20 31 30 30 30 2e 30 20 41 53 20  nce / 1000.0 AS 
2f60: 64 69 73 74 5f 6b 6d 0d 0a 46 52 4f 4d 20 6b 6e  dist_km..FROM kn
2f70: 6e 20 41 53 20 61 0d 0a 4a 4f 49 4e 20 61 69 72  n AS a..JOIN air
2f80: 70 6f 72 74 73 20 41 53 20 62 20 4f 4e 20 28 62  ports AS b ON (b
2f90: 2e 67 65 6f 6e 61 6d 65 69 64 20 3d 20 61 2e 66  .geonameid = a.f
2fa0: 69 64 29 0d 0a 57 48 45 52 45 20 66 5f 74 61 62  id)..WHERE f_tab
2fb0: 6c 65 5f 6e 61 6d 65 20 3d 20 27 61 69 72 70 6f  le_name = 'airpo
2fc0: 72 74 73 27 20 41 4e 44 20 72 65 66 5f 67 65 6f  rts' AND ref_geo
2fd0: 6d 65 74 72 79 20 3d 20 4d 61 6b 65 50 6f 69 6e  metry = MakePoin
2fe0: 74 28 31 30 2c 20 34 33 29 20 41 4e 44 20 6d 61  t(10, 43) AND ma
2ff0: 78 5f 69 74 65 6d 73 20 3d 20 35 3b 0d 0a 3c 2f  x_items = 5;..</
3000: 76 65 72 62 61 74 69 6d 3e 0d 0a 3c 74 61 62 6c  verbatim>..<tabl
3010: 65 20 62 67 63 6f 6c 6f 72 3d 22 23 64 30 66 66  e bgcolor="#d0ff
3020: 62 30 22 20 63 65 6c 6c 73 70 61 63 69 6e 67 3d  b0" cellspacing=
3030: 22 34 22 20 63 65 6c 6c 70 61 64 64 69 6e 67 3d  "4" cellpadding=
3040: 22 34 22 20 62 6f 72 64 65 72 3d 22 31 22 3e 0d  "4" border="1">.
3050: 0a 3c 74 72 3e 3c 74 64 20 62 67 63 6f 6c 6f 72  .<tr><td bgcolor
3060: 3d 22 23 63 30 64 64 61 6f 22 3e 3c 62 3e 72 61  ="#c0ddao"><b>ra
3070: 6e 6b 3c 2f 62 3e 3c 2f 74 64 3e 3c 74 64 20 62  nk</b></td><td b
3080: 67 63 6f 6c 6f 72 3d 22 23 63 30 64 64 61 6f 22  gcolor="#c0ddao"
3090: 3e 3c 62 3e 67 65 6f 6e 61 6d 65 69 64 3c 2f 62  ><b>geonameid</b
30a0: 3e 3c 2f 74 64 3e 3c 74 64 20 62 67 63 6f 6c 6f  ></td><td bgcolo
30b0: 72 3d 22 23 63 30 64 64 61 6f 22 3e 3c 62 3e 6e  r="#c0ddao"><b>n
30c0: 61 6d 65 3c 2f 62 3e 3c 2f 74 64 3e 3c 74 64 20  ame</b></td><td 
30d0: 62 67 63 6f 6c 6f 72 3d 22 23 63 30 64 64 61 6f  bgcolor="#c0ddao
30e0: 22 3e 3c 62 3e 63 6f 75 6e 74 72 79 3c 2f 62 3e  "><b>country</b>
30f0: 3c 2f 74 64 3e 3c 74 64 20 62 67 63 6f 6c 6f 72  </td><td bgcolor
3100: 3d 22 23 63 30 64 64 61 6f 22 3e 3c 62 3e 64 69  ="#c0ddao"><b>di
3110: 73 74 5f 6b 6d 3c 2f 62 3e 3c 2f 74 64 3e 3c 2f  st_km</b></td></
3120: 74 72 3e 0d 0a 3c 74 72 3e 3c 74 64 20 61 6c 69  tr>..<tr><td ali
3130: 67 6e 3d 22 72 69 67 68 74 22 3e 31 3c 2f 74 64  gn="right">1</td
3140: 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69 67 68  ><td align="righ
3150: 74 22 3e 36 32 39 39 36 32 33 3c 2f 74 64 3e 3c  t">6299623</td><
3160: 74 64 3e 4d 61 72 69 6e 61 20 44 69 20 43 61 6d  td>Marina Di Cam
3170: 70 6f 3c 2f 74 64 3e 3c 74 64 3e 49 54 3c 2f 74  po</td><td>IT</t
3180: 64 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69 67  d><td align="rig
3190: 68 74 22 3e 33 33 2e 30 34 33 33 32 30 3c 2f 74  ht">33.043320</t
31a0: 64 3e 3c 2f 74 72 3e 0d 0a 3c 74 72 3e 3c 74 64  d></tr>..<tr><td
31b0: 20 61 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e 32   align="right">2
31c0: 3c 2f 74 64 3e 3c 74 64 20 61 6c 69 67 6e 3d 22  </td><td align="
31d0: 72 69 67 68 74 22 3e 36 32 39 39 33 39 32 3c 2f  right">6299392</
31e0: 74 64 3e 3c 74 64 3e 42 61 73 74 69 61 2d 50 6f  td><td>Bastia-Po
31f0: 72 65 74 74 61 3c 2f 74 64 3e 3c 74 64 3e 46 52  retta</td><td>FR
3200: 3c 2f 74 64 3e 3c 74 64 20 61 6c 69 67 6e 3d 22  </td><td align="
3210: 72 69 67 68 74 22 3e 36 35 2e 32 32 36 35 37 33  right">65.226573
3220: 3c 2f 74 64 3e 3c 2f 74 72 3e 0d 0a 3c 74 72 3e  </td></tr>..<tr>
3230: 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69 67 68 74  <td align="right
3240: 22 3e 33 3c 2f 74 64 3e 3c 74 64 20 61 6c 69 67  ">3</td><td alig
3250: 6e 3d 22 72 69 67 68 74 22 3e 36 32 39 39 36 32  n="right">629962
3260: 38 3c 2f 74 64 3e 3c 74 64 3e 50 69 73 61 20 2f  8</td><td>Pisa /
3270: 20 53 2e 20 47 69 75 73 74 6f 3c 2f 74 64 3e 3c   S. Giusto</td><
3280: 74 64 3e 49 54 3c 2f 74 64 3e 3c 74 64 20 61 6c  td>IT</td><td al
3290: 69 67 6e 3d 22 72 69 67 68 74 22 3e 38 32 2e 33  ign="right">82.3
32a0: 38 37 30 31 34 3c 2f 74 64 3e 3c 2f 74 72 3e 0d  87014</td></tr>.
32b0: 0a 3c 74 72 3e 3c 74 64 20 61 6c 69 67 6e 3d 22  .<tr><td align="
32c0: 72 69 67 68 74 22 3e 34 3c 2f 74 64 3e 3c 74 64  right">4</td><td
32d0: 20 61 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e 36   align="right">6
32e0: 32 39 39 36 33 30 3c 2f 74 64 3e 3c 74 64 3e 47  299630</td><td>G
32f0: 72 6f 73 73 65 74 6f 20 41 69 72 70 6f 72 74 3c  rosseto Airport<
3300: 2f 74 64 3e 3c 74 64 3e 49 54 3c 2f 74 64 3e 3c  /td><td>IT</td><
3310: 74 64 20 61 6c 69 67 6e 3d 22 72 69 67 68 74 22  td align="right"
3320: 3e 39 31 2e 35 34 39 37 37 33 3c 2f 74 64 3e 3c  >91.549773</td><
3330: 2f 74 72 3e 0d 0a 3c 74 72 3e 3c 74 64 20 61 6c  /tr>..<tr><td al
3340: 69 67 6e 3d 22 72 69 67 68 74 22 3e 35 3c 2f 74  ign="right">5</t
3350: 64 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69 67  d><td align="rig
3360: 68 74 22 3e 36 36 39 34 34 39 35 3c 2f 74 64 3e  ht">6694495</td>
3370: 3c 74 64 3e 43 6f 72 74 65 3c 2f 74 64 3e 3c 74  <td>Corte</td><t
3380: 64 3e 46 52 3c 2f 74 64 3e 3c 74 64 20 61 6c 69  d>FR</td><td ali
3390: 67 6e 3d 22 72 69 67 68 74 22 3e 31 30 32 2e 38  gn="right">102.8
33a0: 31 39 37 37 38 3c 2f 74 64 3e 3c 2f 74 72 3e 0d  19778</td></tr>.
33b0: 0a 3c 2f 74 61 62 6c 65 3e 3c 62 72 3e 0d 0a 54  .</table><br>..T
33c0: 68 69 73 20 73 65 63 6f 6e 64 20 4b 4e 4e 20 71  his second KNN q
33d0: 75 65 72 79 20 77 69 6c 6c 20 72 65 74 75 72 6e  uery will return
33e0: 20 74 68 65 20 73 61 6d 65 20 69 64 65 6e 74 69   the same identi
33f0: 63 61 6c 20 72 65 73 75 6c 74 73 20 77 65 20 61  cal results we a
3400: 6c 72 65 61 64 79 20 72 65 63 65 69 76 65 64 20  lready received 
3410: 62 65 66 6f 72 65 20 62 79 20 75 73 69 6e 67 20  before by using 
3420: 74 68 65 20 73 69 6d 70 6c 65 20 61 70 70 72 6f  the simple appro
3430: 61 63 68 2e 3c 62 72 3e 0d 0a 54 68 65 20 73 74  ach.<br>..The st
3440: 72 69 6b 69 6e 67 20 64 69 66 66 65 72 65 6e 63  riking differenc
3450: 65 20 69 73 20 74 68 61 74 20 74 68 69 73 20 4b  e is that this K
3460: 4e 4e 20 71 75 65 72 79 20 73 75 63 63 65 73 73  NN query success
3470: 66 75 6c 6c 79 20 63 6f 6d 70 6c 65 74 65 73 20  fully completes 
3480: 69 6e 20 6a 75 73 74 20 61 20 66 72 61 63 74 69  in just a fracti
3490: 6f 6e 20 6f 66 20 61 20 73 65 63 6f 6e 64 2c 20  on of a second, 
34a0: 77 68 69 6c 65 20 65 78 65 63 75 74 69 6e 67 20  while executing 
34b0: 74 68 65 20 66 69 72 73 74 20 69 6e 65 66 66 69  the first ineffi
34c0: 63 69 65 6e 74 20 71 75 65 72 69 65 73 20 72 65  cient queries re
34d0: 71 75 69 72 65 64 20 6d 61 6e 79 20 73 65 63 6f  quired many seco
34e0: 6e 64 73 20 28 61 6e 64 20 74 68 61 74 20 77 69  nds (and that wi
34f0: 74 68 20 61 20 72 65 6c 61 74 69 76 65 6c 79 20  th a relatively 
3500: 73 6d 61 6c 6c 20 64 61 74 61 73 65 74 29 2e 3c  small dataset).<
3510: 62 72 3e 3c 62 72 3e 0d 0a 3c 62 3e 53 75 6d 6d  br><br>..<b>Summ
3520: 61 72 79 3c 2f 62 3e 3a 20 4b 4e 4e 20 71 75 65  ary</b>: KNN que
3530: 72 69 65 73 20 61 72 65 20 72 65 61 6c 6c 79 20  ries are really 
3540: 65 66 66 69 63 69 65 6e 74 20 61 6e 64 20 66 61  efficient and fa
3550: 73 74 2c 20 62 65 63 61 75 73 65 20 74 68 65 79  st, because they
3560: 20 64 69 72 65 63 74 6c 79 20 69 6e 74 65 72 61   directly intera
3570: 63 74 20 77 69 74 68 20 74 68 65 20 6c 6f 77 65  ct with the lowe
3580: 72 6d 6f 73 74 20 6c 65 76 65 6c 73 20 6f 66 20  rmost levels of 
3590: 74 68 65 20 52 2a 54 72 65 65 20 53 70 61 74 69  the R*Tree Spati
35a0: 61 6c 20 49 6e 64 65 78 20 69 6d 70 6c 65 6d 65  al Index impleme
35b0: 6e 74 61 74 69 6f 6e 2e 3c 62 72 3e 3c 62 72 3e  ntation.<br><br>
35c0: 0d 0a 3c 68 72 3e 0d 0a 3c 68 33 3e 41 20 74 68  ..<hr>..<h3>A th
35d0: 69 72 64 20 73 61 6d 70 6c 65 20 6f 66 20 4b 4e  ird sample of KN
35e0: 4e 20 71 75 65 72 79 3c 2f 68 33 3e 0d 0a 3c 76  N query</h3>..<v
35f0: 65 72 62 61 74 69 6d 3e 0d 0a 53 45 4c 45 43 54  erbatim>..SELECT
3600: 20 61 2e 70 6f 73 20 41 53 20 72 61 6e 6b 2c 20   a.pos AS rank, 
3610: 62 2e 67 65 6f 6e 61 6d 65 69 64 2c 20 62 2e 6e  b.geonameid, b.n
3620: 61 6d 65 2c 20 62 2e 63 6f 75 6e 74 72 79 2c 20  ame, b.country, 
3630: 61 2e 64 69 73 74 61 6e 63 65 20 2f 20 31 30 30  a.distance / 100
3640: 30 2e 30 20 41 53 20 64 69 73 74 5f 6b 6d 0d 0a  0.0 AS dist_km..
3650: 46 52 4f 4d 20 6b 6e 6e 20 41 53 20 61 0d 0a 4a  FROM knn AS a..J
3660: 4f 49 4e 20 61 69 72 70 6f 72 74 73 20 41 53 20  OIN airports AS 
3670: 62 20 4f 4e 20 28 62 2e 67 65 6f 6e 61 6d 65 69  b ON (b.geonamei
3680: 64 20 3d 20 61 2e 66 69 64 29 0d 0a 57 48 45 52  d = a.fid)..WHER
3690: 45 20 66 5f 74 61 62 6c 65 5f 6e 61 6d 65 20 3d  E f_table_name =
36a0: 20 27 61 69 72 70 6f 72 74 73 27 20 41 4e 44 20   'airports' AND 
36b0: 72 65 66 5f 67 65 6f 6d 65 74 72 79 20 3d 20 4d  ref_geometry = M
36c0: 61 6b 65 50 6f 69 6e 74 28 2d 31 37 2e 33 2c 20  akePoint(-17.3, 
36d0: 2d 34 34 29 20 41 4e 44 20 6d 61 78 5f 69 74 65  -44) AND max_ite
36e0: 6d 73 20 3d 20 35 3b 0d 0a 3c 2f 76 65 72 62 61  ms = 5;..</verba
36f0: 74 69 6d 3e 0d 0a 3c 74 61 62 6c 65 20 62 67 63  tim>..<table bgc
3700: 6f 6c 6f 72 3d 22 23 64 30 66 66 62 30 22 20 63  olor="#d0ffb0" c
3710: 65 6c 6c 73 70 61 63 69 6e 67 3d 22 34 22 20 63  ellspacing="4" c
3720: 65 6c 6c 70 61 64 64 69 6e 67 3d 22 34 22 20 62  ellpadding="4" b
3730: 6f 72 64 65 72 3d 22 31 22 3e 0d 0a 3c 74 72 3e  order="1">..<tr>
3740: 3c 74 64 20 62 67 63 6f 6c 6f 72 3d 22 23 63 30  <td bgcolor="#c0
3750: 64 64 61 6f 22 3e 3c 62 3e 72 61 6e 6b 3c 2f 62  ddao"><b>rank</b
3760: 3e 3c 2f 74 64 3e 3c 74 64 20 62 67 63 6f 6c 6f  ></td><td bgcolo
3770: 72 3d 22 23 63 30 64 64 61 6f 22 3e 3c 62 3e 67  r="#c0ddao"><b>g
3780: 65 6f 6e 61 6d 65 69 64 3c 2f 62 3e 3c 2f 74 64  eonameid</b></td
3790: 3e 3c 74 64 20 62 67 63 6f 6c 6f 72 3d 22 23 63  ><td bgcolor="#c
37a0: 30 64 64 61 6f 22 3e 3c 62 3e 6e 61 6d 65 3c 2f  0ddao"><b>name</
37b0: 62 3e 3c 2f 74 64 3e 3c 74 64 20 62 67 63 6f 6c  b></td><td bgcol
37c0: 6f 72 3d 22 23 63 30 64 64 61 6f 22 3e 3c 62 3e  or="#c0ddao"><b>
37d0: 63 6f 75 6e 74 72 79 3c 2f 62 3e 3c 2f 74 64 3e  country</b></td>
37e0: 3c 74 64 20 62 67 63 6f 6c 6f 72 3d 22 23 63 30  <td bgcolor="#c0
37f0: 64 64 61 6f 22 3e 3c 62 3e 64 69 73 74 5f 6b 6d  ddao"><b>dist_km
3800: 3c 2f 62 3e 3c 2f 74 64 3e 3c 2f 74 72 3e 0d 0a  </b></td></tr>..
3810: 3c 74 72 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72  <tr><td align="r
3820: 69 67 68 74 22 3e 31 3c 2f 74 64 3e 3c 74 64 20  ight">1</td><td 
3830: 61 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e 37 36  align="right">76
3840: 36 38 34 33 33 3c 2f 74 64 3e 3c 74 64 3e 43 61  68433</td><td>Ca
3850: 62 6f 20 46 72 69 6f 20 41 69 72 70 6f 72 74 3c  bo Frio Airport<
3860: 2f 74 64 3e 3c 74 64 3e 42 52 3c 2f 74 64 3e 3c  /td><td>BR</td><
3870: 74 64 20 61 6c 69 67 6e 3d 22 72 69 67 68 74 22  td align="right"
3880: 3e 33 32 35 36 2e 35 38 39 39 36 34 3c 2f 74 64  >3256.589964</td
3890: 3e 3c 2f 74 72 3e 0d 0a 3c 74 72 3e 3c 74 64 20  ></tr>..<tr><td 
38a0: 61 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e 32 3c  align="right">2<
38b0: 2f 74 64 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72  /td><td align="r
38c0: 69 67 68 74 22 3e 33 33 35 39 30 30 31 3c 2f 74  ight">3359001</t
38d0: 64 3e 3c 74 64 3e 59 6f 75 6e 67 73 66 69 65 6c  d><td>Youngsfiel
38e0: 64 3c 2f 74 64 3e 3c 74 64 3e 5a 41 3c 2f 74 64  d</td><td>ZA</td
38f0: 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69 67 68  ><td align="righ
3900: 74 22 3e 33 32 36 32 2e 32 37 31 30 32 37 3c 2f  t">3262.271027</
3910: 74 64 3e 3c 2f 74 72 3e 0d 0a 3c 74 72 3e 3c 74  td></tr>..<tr><t
3920: 64 20 61 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e  d align="right">
3930: 33 3c 2f 74 64 3e 3c 74 64 20 61 6c 69 67 6e 3d  3</td><td align=
3940: 22 72 69 67 68 74 22 3e 37 37 33 30 31 35 33 3c  "right">7730153<
3950: 2f 74 64 3e 3c 74 64 3e 55 6d 62 65 72 74 6f 20  /td><td>Umberto 
3960: 4d 6f 64 69 61 6e 6f 20 41 69 72 70 6f 72 74 3c  Modiano Airport<
3970: 2f 74 64 3e 3c 74 64 3e 42 52 3c 2f 74 64 3e 3c  /td><td>BR</td><
3980: 74 64 20 61 6c 69 67 6e 3d 22 72 69 67 68 74 22  td align="right"
3990: 3e 33 32 36 32 2e 36 39 33 30 33 32 3c 2f 74 64  >3262.693032</td
39a0: 3e 3c 2f 74 72 3e 0d 0a 3c 74 72 3e 3c 74 64 20  ></tr>..<tr><td 
39b0: 61 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e 34 3c  align="right">4<
39c0: 2f 74 64 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72  /td><td align="r
39d0: 69 67 68 74 22 3e 33 33 36 32 33 35 35 3c 2f 74  ight">3362355</t
39e0: 64 3e 3c 74 64 3e 52 6f 62 62 65 6e 20 49 73 6c  d><td>Robben Isl
39f0: 61 6e 64 3c 2f 74 64 3e 3c 74 64 3e 5a 41 3c 2f  and</td><td>ZA</
3a00: 74 64 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69  td><td align="ri
3a10: 67 68 74 22 3e 33 32 36 35 2e 33 30 32 35 36 38  ght">3265.302568
3a20: 3c 2f 74 64 3e 3c 2f 74 72 3e 0d 0a 3c 74 72 3e  </td></tr>..<tr>
3a30: 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69 67 68 74  <td align="right
3a40: 22 3e 35 3c 2f 74 64 3e 3c 74 64 20 61 6c 69 67  ">5</td><td alig
3a50: 6e 3d 22 72 69 67 68 74 22 3e 36 33 30 30 36 32  n="right">630062
3a60: 32 3c 2f 74 64 3e 3c 74 64 3e 53 2e 20 50 2e 20  2</td><td>S. P. 
3a70: 41 6c 64 65 69 61 20 41 65 72 6f 64 72 6f 6d 65  Aldeia Aerodrome
3a80: 3c 2f 74 64 3e 3c 74 64 3e 42 52 3c 2f 74 64 3e  </td><td>BR</td>
3a90: 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69 67 68 74  <td align="right
3aa0: 22 3e 33 32 36 37 2e 32 33 34 31 34 32 3c 2f 74  ">3267.234142</t
3ab0: 64 3e 3c 2f 74 72 3e 0d 0a 3c 2f 74 61 62 6c 65  d></tr>..</table
3ac0: 3e 3c 62 72 3e 0d 0a 54 68 69 73 20 6c 61 73 74  ><br>..This last
3ad0: 20 4b 4e 4e 20 71 75 65 72 79 20 69 73 20 76 65   KNN query is ve
3ae0: 72 79 20 73 69 6d 69 6c 61 72 20 74 6f 20 74 68  ry similar to th
3af0: 65 20 70 72 65 76 69 6f 75 73 20 6f 6e 65 2e 3c  e previous one.<
3b00: 62 72 3e 0d 0a 54 68 69 73 20 74 69 6d 65 20 77  br>..This time w
3b10: 65 27 76 65 20 70 6c 61 63 65 64 20 74 68 65 20  e've placed the 
3b20: 6f 72 69 67 69 6e 20 6c 6f 63 61 74 69 6f 6e 20  origin location 
3b30: 73 6f 6d 65 77 68 65 72 65 20 69 6e 20 74 68 65  somewhere in the
3b40: 20 62 6c 75 65 20 64 65 65 70 20 77 61 74 65 72   blue deep water
3b50: 73 20 6f 66 20 74 68 65 20 53 6f 75 74 68 20 41  s of the South A
3b60: 74 6c 61 6e 74 69 63 20 28 6d 6f 72 65 20 6f 72  tlantic (more or
3b70: 20 6c 65 73 73 20 6d 69 64 77 61 79 20 62 65 74   less midway bet
3b80: 77 65 65 6e 20 42 72 61 7a 69 6c 20 61 6e 64 20  ween Brazil and 
3b90: 53 6f 75 74 68 20 41 66 72 69 63 61 29 20 61 6e  South Africa) an
3ba0: 64 20 63 6f 6e 73 65 71 75 65 6e 74 6c 79 20 74  d consequently t
3bb0: 68 65 20 6e 65 61 72 65 73 74 20 61 69 72 70 6f  he nearest airpo
3bc0: 72 74 73 20 61 72 65 20 6e 6f 74 20 72 65 61 6c  rts are not real
3bd0: 6c 79 20 73 6f 20 6e 65 61 72 2c 20 62 65 63 61  ly so near, beca
3be0: 75 73 65 20 74 68 65 79 20 61 72 65 20 6c 6f 63  use they are loc
3bf0: 61 74 65 64 20 6d 61 6e 79 20 74 68 6f 75 73 61  ated many thousa
3c00: 6e 64 73 20 6f 66 20 4b 69 6c 6f 6d 65 74 65 72  nds of Kilometer
3c10: 73 20 61 77 61 79 2e 0d 0a 41 73 20 79 6f 75 20  s away...As you 
3c20: 63 61 6e 20 65 61 73 69 6c 79 20 63 68 65 63 6b  can easily check
3c30: 20 62 79 20 79 6f 75 72 73 65 6c 66 20 61 20 4b   by yourself a K
3c40: 4e 4e 20 71 75 65 72 79 20 77 69 6c 6c 20 65 66  NN query will ef
3c50: 66 69 63 69 65 6e 74 6c 79 20 70 65 72 66 6f 72  ficiently perfor
3c60: 6d 20 65 76 65 6e 20 75 6e 64 65 72 20 73 75 63  m even under suc
3c70: 68 20 75 6e 75 73 75 61 6c 20 63 6f 6e 64 69 74  h unusual condit
3c80: 69 6f 6e 73 2e 3c 62 72 3e 3c 62 72 3e 0d 0a 3c  ions.<br><br>..<
3c90: 69 6d 67 20 73 72 63 3d 22 68 74 74 70 73 3a 2f  img src="https:/
3ca0: 2f 77 77 77 2e 67 61 69 61 2d 67 69 73 2e 69 74  /www.gaia-gis.it
3cb0: 2f 67 61 69 61 2d 73 69 6e 73 2f 6b 6e 6e 2f 73  /gaia-sins/knn/s
3cc0: 6f 75 74 68 2d 61 74 6c 61 6e 74 69 63 2e 70 6e  outh-atlantic.pn
3cd0: 67 22 20 61 6c 74 3d 22 53 6f 75 74 68 20 41 74  g" alt="South At
3ce0: 6c 61 6e 74 69 63 22 3e 3c 62 72 3e 3c 62 72 3e  lantic"><br><br>
3cf0: 0d 0a 3c 62 3e 53 75 6d 6d 61 72 79 3c 2f 62 3e  ..<b>Summary</b>
3d00: 3a 20 4b 4e 4e 20 71 75 65 72 69 65 73 20 6e 65  : KNN queries ne
3d10: 76 65 72 20 61 73 73 75 6d 65 20 61 6e 79 20 70  ver assume any p
3d20: 72 65 64 65 66 69 6e 65 64 20 64 69 73 74 72 69  redefined distri
3d30: 62 75 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 65  bution of the se
3d40: 61 72 63 68 65 64 20 69 74 65 6d 73 2c 20 61 6e  arched items, an
3d50: 64 20 77 69 6c 6c 20 65 78 74 72 61 63 74 20 74  d will extract t
3d60: 68 65 20 63 6f 72 72 65 63 74 20 72 65 73 75 6c  he correct resul
3d70: 74 73 20 28 73 6f 72 74 65 64 20 62 79 20 64 69  ts (sorted by di
3d80: 73 74 61 6e 63 65 29 2c 20 64 65 73 70 69 74 65  stance), despite
3d90: 20 74 68 65 20 76 65 72 79 20 69 72 72 65 67 75   the very irregu
3da0: 6c 61 72 20 73 61 6d 70 6c 65 20 64 69 73 74 72  lar sample distr
3db0: 69 62 75 74 69 6f 6e 2e 3c 62 72 3e 3c 62 72 3e  ibution.<br><br>
3dc0: 0d 0a 3c 68 72 3e 0d 0a 3c 68 32 3e 41 64 76 61  ..<hr>..<h2>Adva
3dd0: 6e 63 65 64 20 74 75 74 6f 72 69 61 6c 3c 2f 68  nced tutorial</h
3de0: 32 3e 0d 0a 59 6f 75 20 63 61 6e 20 3c 61 20 68  2>..You can <a h
3df0: 72 65 66 3d 22 68 74 74 70 73 3a 2f 2f 77 77 77  ref="https://www
3e00: 2e 67 61 69 61 2d 67 69 73 2e 69 74 2f 67 61 69  .gaia-gis.it/gai
3e10: 61 2d 73 69 6e 73 2f 6b 6e 6e 2f 74 75 73 63 61  a-sins/knn/tusca
3e20: 6e 79 5f 68 6f 75 73 65 6e 75 6d 62 65 72 73 2e  ny_housenumbers.
3e30: 37 7a 22 3e 64 6f 77 6e 6c 6f 61 64 3c 2f 61 3e  7z">download</a>
3e40: 20 74 68 65 20 73 61 6d 70 6c 65 20 64 62 2d 66   the sample db-f
3e50: 69 6c 65 20 75 73 65 64 20 69 6e 20 74 68 65 20  ile used in the 
3e60: 73 61 6d 70 6c 65 73 20 6f 66 20 74 68 69 73 20  samples of this 
3e70: 61 64 76 61 6e 63 65 64 20 74 75 74 6f 72 69 61  advanced tutoria
3e80: 6c 2e 3c 62 72 3e 3c 62 72 3e 0d 0a 54 68 69 73  l.<br><br>..This
3e90: 20 69 73 20 61 20 6d 75 63 68 20 6d 6f 72 65 20   is a much more 
3ea0: 64 65 6d 61 6e 64 69 6e 67 20 64 61 74 61 73 65  demanding datase
3eb0: 74 20 77 69 74 68 20 61 20 63 6f 6c 6c 65 63 74  t with a collect
3ec0: 69 6f 6e 20 6f 66 20 61 62 6f 75 74 20 3c 62 3e  ion of about <b>
3ed0: 32 2e 34 20 6d 69 6c 6c 69 6f 6e 20 68 6f 75 73  2.4 million hous
3ee0: 65 20 6e 75 6d 62 65 72 73 3c 2f 62 3e 2e 3c 62  e numbers</b>.<b
3ef0: 72 3e 0d 0a 54 68 65 20 6f 72 69 67 69 6e 61 6c  r>..The original
3f00: 20 69 6e 70 75 74 20 73 68 61 70 65 66 69 6c 65   input shapefile
3f10: 20 77 61 73 20 64 6f 77 6e 6c 6f 61 64 65 64 20   was downloaded 
3f20: 66 72 6f 6d 20 3c 61 20 68 72 65 66 3d 22 68 74  from <a href="ht
3f30: 74 70 3a 2f 2f 77 77 77 35 30 32 2e 72 65 67 69  tp://www502.regi
3f40: 6f 6e 65 2e 74 6f 73 63 61 6e 61 2e 69 74 2f 67  one.toscana.it/g
3f50: 65 6f 73 63 6f 70 69 6f 2f 63 61 72 74 6f 74 65  eoscopio/cartote
3f60: 63 61 2e 68 74 6d 6c 22 3e 54 75 73 63 61 6e 79  ca.html">Tuscany
3f70: 20 52 65 67 69 6f 6e 3c 2f 61 3e 20 28 3c 62 3e   Region</a> (<b>
3f80: 3c 69 3e 47 72 61 66 6f 20 53 74 72 61 64 61 6c  <i>Grafo Stradal
3f90: 65 20 63 69 76 69 63 69 2e 73 68 70 3c 2f 69 3e  e civici.shp</i>
3fa0: 3c 2f 62 3e 29 20 61 6e 64 20 77 61 73 20 73 75  </b>) and was su
3fb0: 62 73 65 71 75 65 6e 74 6c 79 20 72 65 61 72 72  bsequently rearr
3fc0: 61 6e 67 65 64 20 69 6e 74 6f 20 61 20 6d 6f 72  anged into a mor
3fd0: 65 20 63 6f 6e 76 65 6e 69 65 6e 74 20 66 6f 72  e convenient for
3fe0: 6d 2e 3c 62 72 3e 0d 0a 54 68 65 20 64 61 74 61  m.<br>..The data
3ff0: 73 65 74 20 69 73 20 72 65 6c 65 61 73 65 64 20  set is released 
4000: 75 6e 64 65 72 20 74 68 65 20 3c 62 3e 43 43 2d  under the <b>CC-
4010: 42 59 2d 53 41 20 34 2e 30 3c 2f 62 3e 20 6c 69  BY-SA 4.0</b> li
4020: 63 65 6e 73 65 20 74 65 72 6d 73 2e 3c 62 72 3e  cense terms.<br>
4030: 0d 0a 54 68 65 20 75 73 65 64 20 72 65 66 65 72  ..The used refer
4040: 65 6e 63 65 20 73 79 73 74 65 6d 20 69 73 20 3c  ence system is <
4050: 62 3e 33 30 30 33 20 4d 6f 6e 74 65 20 4d 61 72  b>3003 Monte Mar
4060: 69 6f 20 2f 20 49 74 61 6c 79 20 7a 6f 6e 65 20  io / Italy zone 
4070: 31 3c 2f 62 3e 3b 20 74 68 69 73 20 69 73 20 61  1</b>; this is a
4080: 20 3c 62 3e 70 6c 61 6e 61 72 3c 2f 62 3e 20 28   <b>planar</b> (
4090: 3c 69 3e 70 72 6f 6a 65 63 74 65 64 3c 2f 69 3e  <i>projected</i>
40a0: 29 20 72 65 66 65 72 65 6e 63 65 20 73 79 73 74  ) reference syst
40b0: 65 6d 20 6d 65 61 73 75 72 65 64 20 69 6e 20 3c  em measured in <
40c0: 62 3e 6d 65 74 65 72 73 3c 2f 62 3e 2e 0d 0a 3c  b>meters</b>...<
40d0: 62 72 3e 0d 0a 3c 76 65 72 62 61 74 69 6d 3e 0d  br>..<verbatim>.
40e0: 0a 53 45 4c 45 43 54 20 61 2e 70 6f 73 20 41 53  .SELECT a.pos AS
40f0: 20 72 61 6e 6b 2c 20 61 2e 66 69 64 20 41 53 20   rank, a.fid AS 
4100: 66 69 64 2c 20 61 2e 64 69 73 74 61 6e 63 65 20  fid, a.distance 
4110: 41 53 20 64 69 73 74 5f 6d 2c 20 64 2e 70 72 6f  AS dist_m, d.pro
4120: 76 20 41 53 20 70 72 6f 76 69 6e 63 65 2c 0d 0a  v AS province,..
4130: 20 20 20 20 20 20 20 64 2e 6e 6f 6d 65 20 41 53         d.nome AS
4140: 20 6d 75 6e 69 63 69 70 61 6c 69 74 79 2c 20 63   municipality, c
4150: 2e 74 6f 70 6f 6e 69 6d 6f 20 41 53 20 73 74 72  .toponimo AS str
4160: 65 65 74 5f 6e 61 6d 65 2c 20 62 2e 63 69 76 69  eet_name, b.civi
4170: 63 6f 20 41 53 20 68 6f 75 73 65 5f 6e 75 6d 2c  co AS house_num,
4180: 0d 0a 20 20 20 20 20 20 20 62 2e 67 65 6f 6d 20  ..       b.geom 
4190: 41 53 20 67 65 6f 6d 0d 0a 46 52 4f 4d 20 6b 6e  AS geom..FROM kn
41a0: 6e 20 41 53 20 61 0d 0a 4a 4f 49 4e 20 63 69 76  n AS a..JOIN civ
41b0: 69 63 69 20 41 53 20 62 20 4f 4e 20 28 62 2e 72  ici AS b ON (b.r
41c0: 6f 77 69 64 20 3d 20 61 2e 66 69 64 29 0d 0a 4a  owid = a.fid)..J
41d0: 4f 49 4e 20 74 6f 70 6f 6e 69 6d 69 20 41 53 20  OIN toponimi AS 
41e0: 63 20 4f 4e 20 28 63 2e 66 69 64 20 3d 20 62 2e  c ON (c.fid = b.
41f0: 69 64 5f 74 6f 70 6f 6e 69 6d 6f 29 0d 0a 4a 4f  id_toponimo)..JO
4200: 49 4e 20 63 6f 6d 75 6e 69 20 41 53 20 64 20 4f  IN comuni AS d O
4210: 4e 20 28 64 2e 66 69 64 20 3d 20 63 2e 69 64 5f  N (d.fid = c.id_
4220: 63 6f 6d 75 6e 65 29 0d 0a 57 48 45 52 45 20 61  comune)..WHERE a
4230: 2e 66 5f 74 61 62 6c 65 5f 6e 61 6d 65 20 3d 20  .f_table_name = 
4240: 27 63 69 76 69 63 69 27 20 41 4e 44 20 61 2e 72  'civici' AND a.r
4250: 65 66 5f 67 65 6f 6d 65 74 72 79 20 3d 20 4d 61  ef_geometry = Ma
4260: 6b 65 50 6f 69 6e 74 28 31 37 33 33 30 30 33 2c  kePoint(1733003,
4270: 20 34 38 31 36 33 33 32 2c 20 33 30 30 33 29 20   4816332, 3003) 
4280: 41 4e 44 20 61 2e 6d 61 78 5f 69 74 65 6d 73 20  AND a.max_items 
4290: 3d 20 31 30 3b 0d 0a 0d 0a 53 45 4c 45 43 54 20  = 10;....SELECT 
42a0: 61 2e 70 6f 73 20 41 53 20 72 61 6e 6b 2c 20 61  a.pos AS rank, a
42b0: 2e 66 69 64 20 41 53 20 66 69 64 2c 20 61 2e 64  .fid AS fid, a.d
42c0: 69 73 74 61 6e 63 65 20 41 53 20 64 69 73 74 5f  istance AS dist_
42d0: 6d 2c 20 62 2e 70 72 6f 76 20 41 53 20 70 72 6f  m, b.prov AS pro
42e0: 76 69 6e 63 65 2c 0d 0a 20 20 20 20 20 20 20 62  vince,..       b
42f0: 2e 63 6f 6d 75 6e 65 20 41 53 20 6d 75 6e 69 63  .comune AS munic
4300: 69 70 61 6c 69 74 79 2c 20 62 2e 74 6f 70 6f 6e  ipality, b.topon
4310: 69 6d 6f 20 41 53 20 73 74 72 65 65 74 5f 6e 61  imo AS street_na
4320: 6d 65 2c 20 62 2e 63 69 76 69 63 6f 20 41 53 20  me, b.civico AS 
4330: 68 6f 75 73 65 5f 6e 75 6d 2c 20 0d 0a 20 20 20  house_num, ..   
4340: 20 20 20 20 62 2e 67 65 6f 6d 20 41 53 20 67 65      b.geom AS ge
4350: 6f 6d 0d 0a 46 52 4f 4d 20 6b 6e 6e 20 41 53 20  om..FROM knn AS 
4360: 61 0d 0a 4a 4f 49 4e 20 76 77 5f 63 69 76 69 63  a..JOIN vw_civic
4370: 69 20 41 53 20 62 20 4f 4e 20 28 62 2e 72 6f 77  i AS b ON (b.row
4380: 69 64 20 3d 20 61 2e 66 69 64 29 0d 0a 57 48 45  id = a.fid)..WHE
4390: 52 45 20 61 2e 66 5f 74 61 62 6c 65 5f 6e 61 6d  RE a.f_table_nam
43a0: 65 20 3d 20 27 76 77 5f 63 69 76 69 63 69 27 20  e = 'vw_civici' 
43b0: 41 4e 44 20 61 2e 72 65 66 5f 67 65 6f 6d 65 74  AND a.ref_geomet
43c0: 72 79 20 3d 20 4d 61 6b 65 50 6f 69 6e 74 28 31  ry = MakePoint(1
43d0: 37 33 33 30 30 33 2c 20 34 38 31 36 33 33 32 2c  733003, 4816332,
43e0: 20 33 30 30 33 29 20 41 4e 44 20 61 2e 6d 61 78   3003) AND a.max
43f0: 5f 69 74 65 6d 73 20 3d 20 31 30 3b 0d 0a 3c 2f  _items = 10;..</
4400: 76 65 72 62 61 74 69 6d 3e 0d 0a 54 68 65 20 66  verbatim>..The f
4410: 6f 6c 6c 6f 77 69 6e 67 20 6d 61 70 20 63 6f 72  ollowing map cor
4420: 72 65 73 70 6f 6e 64 73 20 74 6f 20 74 68 65 20  responds to the 
4430: 61 62 6f 76 65 20 71 75 65 72 69 65 73 3a 20 74  above queries: t
4440: 68 65 20 6f 72 69 67 69 6e 20 70 6f 69 6e 74 20  he origin point 
4450: 69 73 20 70 6f 73 69 74 69 6f 6e 65 64 20 6f 6e  is positioned on
4460: 20 61 20 72 6f 61 64 20 6a 75 6e 63 74 69 6f 6e   a road junction
4470: 20 69 6e 20 74 68 65 20 63 65 6e 74 72 61 6c 20   in the central 
4480: 61 72 65 61 20 6f 66 20 61 20 64 65 6e 73 65 6c  area of a densel
4490: 79 20 70 6f 70 75 6c 61 74 65 64 20 74 6f 77 6e  y populated town
44a0: 20 28 41 72 65 7a 7a 6f 29 2e 3c 62 72 3e 0d 0a   (Arezzo).<br>..
44b0: 4e 6f 74 20 73 75 72 70 72 69 73 69 6e 67 6c 79  Not surprisingly
44c0: 20 74 68 65 20 74 65 6e 20 6e 65 61 72 65 73 74   the ten nearest
44d0: 20 68 6f 75 73 65 20 6e 75 6d 62 65 72 73 20 68   house numbers h
44e0: 61 76 65 20 62 65 65 6e 20 6c 6f 63 61 74 65 64  ave been located
44f0: 20 77 69 74 68 69 6e 20 61 20 72 61 64 69 75 73   within a radius
4500: 20 6f 66 20 61 62 6f 75 74 20 31 30 6d 2e 3c 62   of about 10m.<b
4510: 72 3e 3c 62 72 3e 0d 0a 3c 69 6d 67 20 73 72 63  r><br>..<img src
4520: 3d 22 68 74 74 70 73 3a 2f 2f 77 77 77 2e 67 61  ="https://www.ga
4530: 69 61 2d 67 69 73 2e 69 74 2f 67 61 69 61 2d 73  ia-gis.it/gaia-s
4540: 69 6e 73 2f 6b 6e 6e 2f 68 6f 75 73 65 6e 75 6d  ins/knn/housenum
4550: 31 2e 6a 70 67 22 20 61 6c 74 3d 22 48 6f 75 73  1.jpg" alt="Hous
4560: 65 20 4e 75 6d 62 65 72 73 20 23 31 22 3e 0d 0a  e Numbers #1">..
4570: 3c 62 72 3e 3c 62 72 3e 0d 0a 42 6f 74 68 20 71  <br><br>..Both q
4580: 75 65 72 69 65 73 20 77 69 6c 6c 20 72 65 74 75  ueries will retu
4590: 72 6e 20 69 64 65 6e 74 69 63 61 6c 20 72 65 73  rn identical res
45a0: 75 6c 74 73 2e 3c 62 72 3e 0d 0a 41 20 63 6c 6f  ults.<br>..A clo
45b0: 73 65 72 20 6c 6f 6f 6b 20 77 69 6c 6c 20 73 68  ser look will sh
45c0: 6f 77 20 79 6f 75 20 74 68 61 74 20 74 68 65 20  ow you that the 
45d0: 73 65 63 6f 6e 64 20 71 75 65 72 79 20 69 73 20  second query is 
45e0: 62 61 73 65 64 20 6f 6e 20 61 20 3c 62 3e 53 70  based on a <b>Sp
45f0: 61 74 69 61 6c 20 56 69 65 77 3c 2f 62 3e 3c 62  atial View</b><b
4600: 72 3e 3c 62 72 3e 0d 0a 3c 62 3e 53 75 6d 6d 61  r><br>..<b>Summa
4610: 72 79 3c 2f 62 3e 3a 20 61 20 56 69 72 74 75 61  ry</b>: a Virtua
4620: 6c 4b 4e 4e 20 71 75 65 72 79 20 63 61 6e 20 69  lKNN query can i
4630: 6e 64 69 66 66 65 72 65 6e 74 6c 79 20 74 61 72  ndifferently tar
4640: 67 65 74 20 65 69 74 68 65 72 20 61 20 47 65 6f  get either a Geo
4650: 54 61 62 6c 65 20 6f 72 20 61 20 70 72 6f 70 65  Table or a prope
4660: 72 6c 79 20 72 65 67 69 73 74 65 72 65 64 20 53  rly registered S
4670: 70 61 74 69 61 6c 20 56 69 65 77 2e 3c 62 72 3e  patial View.<br>
4680: 3c 62 72 3e 0d 0a 3c 76 65 72 62 61 74 69 6d 3e  <br>..<verbatim>
4690: 0d 0a 53 45 4c 45 43 54 20 61 2e 70 6f 73 20 41  ..SELECT a.pos A
46a0: 53 20 72 61 6e 6b 2c 20 61 2e 66 69 64 20 41 53  S rank, a.fid AS
46b0: 20 66 69 64 2c 20 61 2e 64 69 73 74 61 6e 63 65   fid, a.distance
46c0: 20 41 53 20 64 69 73 74 5f 6d 2c 20 62 2e 70 72   AS dist_m, b.pr
46d0: 6f 76 20 41 53 20 70 72 6f 76 69 6e 63 65 2c 0d  ov AS province,.
46e0: 0a 20 20 20 20 20 20 20 62 2e 63 6f 6d 75 6e 65  .       b.comune
46f0: 20 41 53 20 6d 75 6e 69 63 69 70 61 6c 69 74 79   AS municipality
4700: 2c 20 62 2e 74 6f 70 6f 6e 69 6d 6f 20 41 53 20  , b.toponimo AS 
4710: 73 74 72 65 65 74 5f 6e 61 6d 65 2c 20 62 2e 63  street_name, b.c
4720: 69 76 69 63 6f 20 41 53 20 68 6f 75 73 65 5f 6e  ivico AS house_n
4730: 75 6d 2c 20 0d 0a 20 20 20 20 20 20 20 62 2e 67  um, ..       b.g
4740: 65 6f 6d 20 41 53 20 67 65 6f 6d 0d 0a 46 52 4f  eom AS geom..FRO
4750: 4d 20 6b 6e 6e 20 41 53 20 61 0d 0a 4a 4f 49 4e  M knn AS a..JOIN
4760: 20 76 77 5f 63 69 76 69 63 69 20 41 53 20 62 20   vw_civici AS b 
4770: 4f 4e 20 28 62 2e 72 6f 77 69 64 20 3d 20 61 2e  ON (b.rowid = a.
4780: 66 69 64 29 20 0d 0a 57 48 45 52 45 20 61 2e 66  fid) ..WHERE a.f
4790: 5f 74 61 62 6c 65 5f 6e 61 6d 65 20 3d 20 27 63  _table_name = 'c
47a0: 69 76 69 63 69 27 20 41 4e 44 20 61 2e 72 65 66  ivici' AND a.ref
47b0: 5f 67 65 6f 6d 65 74 72 79 20 3d 20 4d 61 6b 65  _geometry = Make
47c0: 50 6f 69 6e 74 28 31 35 39 35 36 32 35 2c 20 34  Point(1595625, 4
47d0: 37 36 37 34 32 30 2c 20 33 30 30 33 29 20 41 4e  767420, 3003) AN
47e0: 44 20 6d 61 78 5f 69 74 65 6d 73 20 3d 20 32 30  D max_items = 20
47f0: 3b 0d 0a 3c 2f 76 65 72 62 61 74 69 6d 3e 0d 0a  ;..</verbatim>..
4800: 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61  The following ma
4810: 70 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f  p corresponds to
4820: 20 74 68 65 20 61 62 6f 76 65 20 71 75 65 72 79   the above query
4830: 2e 20 4e 6f 77 20 74 68 65 20 6f 72 69 67 69 6e  . Now the origin
4840: 20 70 6f 69 6e 74 20 69 73 20 70 6f 73 69 74 69   point is positi
4850: 6f 6e 65 64 20 69 6e 20 74 68 65 20 54 79 72 72  oned in the Tyrr
4860: 68 65 6e 69 61 6e 20 53 65 61 2c 20 61 62 6f 75  henian Sea, abou
4870: 74 20 3c 62 3e 32 36 20 6b 6d 3c 2f 62 3e 20 66  t <b>26 km</b> f
4880: 72 6f 6d 20 74 68 65 20 54 75 73 63 61 6e 79 20  rom the Tuscany 
4890: 6d 61 69 6e 6c 61 6e 64 20 61 6e 64 20 74 68 65  mainland and the
48a0: 20 49 73 6c 61 6e 64 73 20 6f 66 20 45 6c 62 61   Islands of Elba
48b0: 20 61 6e 64 20 43 61 70 72 61 69 61 2e 3c 62 72   and Capraia.<br
48c0: 3e 0d 0a 48 65 72 65 20 74 6f 6f 20 56 69 72 74  >..Here too Virt
48d0: 75 61 6c 4b 4e 4e 20 69 73 20 63 61 70 61 62 6c  ualKNN is capabl
48e0: 65 20 6f 66 20 69 64 65 6e 74 69 66 79 69 6e 67  e of identifying
48f0: 20 74 68 65 20 6e 65 61 72 65 73 74 20 68 6f 75   the nearest hou
4900: 73 65 20 6e 75 6d 62 65 72 73 20 69 6e 20 6a 75  se numbers in ju
4910: 73 74 20 61 20 66 72 61 63 74 69 6f 6e 20 6f 66  st a fraction of
4920: 20 61 20 73 65 63 6f 6e 64 2e 3c 62 72 3e 3c 62   a second.<br><b
4930: 72 3e 0d 0a 3c 69 6d 67 20 73 72 63 3d 22 68 74  r>..<img src="ht
4940: 74 70 73 3a 2f 2f 77 77 77 2e 67 61 69 61 2d 67  tps://www.gaia-g
4950: 69 73 2e 69 74 2f 67 61 69 61 2d 73 69 6e 73 2f  is.it/gaia-sins/
4960: 6b 6e 6e 2f 68 6f 75 73 65 6e 75 6d 32 2e 70 6e  knn/housenum2.pn
4970: 67 22 20 61 6c 74 3d 22 48 6f 75 73 65 20 4e 75  g" alt="House Nu
4980: 6d 62 65 72 73 20 23 32 22 3e 3c 62 72 3e 3c 62  mbers #2"><br><b
4990: 72 3e 0d 0a 3c 62 3e 53 75 6d 6d 61 72 79 3c 2f  r>..<b>Summary</
49a0: 62 3e 3a 20 20 56 69 72 74 75 61 6c 4b 4e 4e 20  b>:  VirtualKNN 
49b0: 69 73 20 68 69 67 68 6c 79 20 65 66 66 69 63 69  is highly effici
49c0: 65 6e 74 20 61 6e 64 20 76 65 72 79 20 66 61 73  ent and very fas
49d0: 74 2e 20 45 76 65 6e 20 77 68 65 6e 20 71 75 65  t. Even when que
49e0: 72 79 69 6e 67 20 73 75 63 68 20 61 20 68 75 67  rying such a hug
49f0: 65 20 64 61 74 61 73 65 74 2c 20 77 68 69 6c 65  e dataset, while
4a00: 20 65 78 70 6c 6f 72 69 6e 67 20 74 68 65 20 6d   exploring the m
4a10: 6f 73 74 20 63 6f 6e 74 72 61 73 74 69 6e 67 20  ost contrasting 
4a20: 28 76 65 72 79 20 68 69 67 68 20 2f 20 6c 6f 77  (very high / low
4a30: 20 64 65 6e 73 69 74 79 29 20 64 61 74 61 20 64   density) data d
4a40: 69 73 74 72 69 62 75 74 69 6f 6e 73 20 2e 3c 62  istributions .<b
4a50: 72 3e 3c 62 72 3e 0d 0a 3c 74 61 62 6c 65 20 77  r><br>..<table w
4a60: 69 64 74 68 3d 22 31 30 30 25 22 3e 3c 74 72 3e  idth="100%"><tr>
4a70: 0d 0a 3c 74 64 20 77 69 64 74 68 3d 22 33 33 25  ..<td width="33%
4a80: 22 20 61 6c 69 67 6e 3d 22 6c 65 66 74 22 3e 3c  " align="left"><
4a90: 2f 74 64 3e 0d 0a 3c 74 64 20 61 6c 69 67 6e 3d  /td>..<td align=
4aa0: 22 63 65 6e 74 65 72 22 3e 3c 61 20 68 72 65 66  "center"><a href
4ab0: 3d 22 68 74 74 70 73 3a 2f 2f 77 77 77 2e 67 61  ="https://www.ga
4ac0: 69 61 2d 67 69 73 2e 69 74 2f 66 6f 73 73 69 6c  ia-gis.it/fossil
4ad0: 2f 6c 69 62 73 70 61 74 69 61 6c 69 74 65 2f 77  /libspatialite/w
4ae0: 69 6b 69 3f 6e 61 6d 65 3d 6d 69 73 63 2d 64 6f  iki?name=misc-do
4af0: 63 73 22 3e 62 61 63 6b 20 74 6f 20 69 6e 64 65  cs">back to inde
4b00: 78 3c 2f 61 3e 3c 2f 74 64 3e 0d 0a 3c 74 64 20  x</a></td>..<td 
4b10: 77 69 64 74 68 3d 22 33 33 25 22 20 61 6c 69 67  width="33%" alig
4b20: 6e 3d 22 72 69 67 68 74 22 3e 3c 2f 74 64 3e 0d  n="right"></td>.
4b30: 0a 3c 2f 74 72 3e 3c 2f 74 61 62 6c 65 3e 0a 5a  .</tr></table>.Z
4b40: 20 30 34 61 33 30 64 36 34 32 65 31 31 65 31 63   04a30d642e11e1c
4b50: 36 32 37 31 61 66 38 64 33 37 61 38 66 66 64 35  6271af8d37a8ffd5
4b60: 38 0a                                            8.