Hex Artifact Content
Not logged in

Artifact d88318ace354d8e63308a9a21de92adbbe0006a0:

Wiki page [KNN] by sandro 2015-12-08 15:45:01.
0000: 44 20 32 30 31 35 2d 31 32 2d 30 38 54 31 35 3a  D 2015-12-08T15:
0010: 34 35 3a 30 31 2e 38 33 39 0a 4c 20 4b 4e 4e 0a  45:01.839.L KNN.
0020: 50 20 31 61 64 32 63 65 64 62 34 62 66 37 65 36  P 1ad2cedb4bf7e6
0030: 37 31 32 32 31 31 66 61 30 30 36 35 33 32 63 39  712211fa006532c9
0040: 30 38 33 39 61 32 30 38 34 31 0a 55 20 73 61 6e  0839a20841.U san
0050: 64 72 6f 0a 57 20 31 35 33 32 36 0a 3c 74 61 62  dro.W 15326.<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 65 72 65 6c 79 20 66 75  een entierely fu
0290: 6e 64 65 64 20 62 79 0d 0a 3c 61 20 68 72 65 66  nded by..<a href
02a0: 3d 22 68 74 74 70 3a 2f 2f 65 6e 2e 77 69 6b 69  ="http://en.wiki
02b0: 70 65 64 69 61 2e 6f 72 67 2f 77 69 6b 69 2f 54  pedia.org/wiki/T
02c0: 75 73 63 61 6e 79 22 3e 54 75 73 63 61 6e 79 20  uscany">Tuscany 
02d0: 52 65 67 69 6f 6e 3c 2f 61 3e 20 2d 20 54 65 72  Region</a> - Ter
02e0: 72 69 74 6f 72 69 61 6c 20 61 6e 64 20 45 6e 76  ritorial and Env
02f0: 69 72 6f 6e 6d 65 6e 74 61 6c 20 49 6e 66 6f 72  ironmental Infor
0300: 6d 61 74 69 6f 6e 20 53 79 73 74 65 6d 3c 62 72  mation System<br
0310: 3e 0d 0a 3c 61 20 68 72 65 66 3d 22 68 74 74 70  >..<a href="http
0320: 3a 2f 2f 77 77 77 2e 72 65 67 69 6f 6e 65 2e 74  ://www.regione.t
0330: 6f 73 63 61 6e 61 2e 69 74 2f 61 6d 62 69 65 6e  oscana.it/ambien
0340: 74 65 65 74 65 72 72 69 74 6f 72 69 6f 2f 67 65  teeterritorio/ge
0350: 6f 67 72 61 66 69 61 67 65 6f 6c 6f 67 69 61 2f  ografiageologia/
0360: 69 6e 64 65 78 2e 68 74 6d 6c 22 3e 52 65 67 69  index.html">Regi
0370: 6f 6e 65 20 54 6f 73 63 61 6e 61 3c 2f 61 3e 20  one Toscana</a> 
0380: 2d 20 53 65 74 74 6f 72 65 20 53 69 73 74 65 6d  - Settore Sistem
0390: 61 20 49 6e 66 6f 72 6d 61 74 69 76 6f 20 54 65  a Informativo Te
03a0: 72 72 69 74 6f 72 69 61 6c 65 20 65 64 20 41 6d  rritoriale ed Am
03b0: 62 69 65 6e 74 61 6c 65 2e 0d 0a 3c 2f 74 64 3e  bientale...</td>
03c0: 3c 2f 74 72 3e 3c 2f 74 61 62 6c 65 3e 0d 0a 3c  </tr></table>..<
03d0: 68 72 3e 3c 62 72 3e 0d 0a 3c 74 61 62 6c 65 20  hr><br>..<table 
03e0: 77 69 64 74 68 3d 22 31 30 30 25 22 20 62 67 63  width="100%" bgc
03f0: 6f 6c 6f 72 3d 22 23 66 66 66 66 64 66 22 20 63  olor="#ffffdf" c
0400: 65 6c 6c 73 70 61 63 69 6e 67 3d 22 34 22 20 63  ellspacing="4" c
0410: 65 6c 6c 70 61 64 64 69 6e 67 3d 22 38 22 3e 0d  ellpadding="8">.
0420: 0a 3c 74 72 3e 3c 74 64 3e 3c 62 3e 57 68 61 74  .<tr><td><b>What
0430: 20 69 73 20 74 68 65 20 4b 4e 4e 20 28 3c 69 3e   is the KNN (<i>
0440: 4b 2d 4e 65 61 72 65 73 74 20 4e 65 69 67 68 62  K-Nearest Neighb
0450: 6f 72 73 3c 2f 69 3e 29 20 70 72 6f 62 6c 65 6d  ors</i>) problem
0460: 20 3f 3c 2f 62 3e 3c 2f 74 64 3e 3c 2f 74 72 3e   ?</b></td></tr>
0470: 0d 0a 3c 74 72 3e 3c 74 64 3e 49 6d 61 67 69 6e  ..<tr><td>Imagin
0480: 65 20 61 20 73 65 74 20 6f 66 20 61 72 62 69 74  e a set of arbit
0490: 72 61 72 79 20 67 65 6f 6d 65 74 72 69 65 73 3b  rary geometries;
04a0: 20 6d 61 79 20 77 65 6c 6c 20 62 65 2c 20 61 20   may well be, a 
04b0: 72 65 61 6c 6c 79 20 68 75 67 65 20 64 61 74 61  really huge data
04c0: 73 65 74 20 63 6f 6e 74 61 69 6e 69 6e 67 20 73  set containing s
04d0: 6f 6d 65 20 6d 69 6c 6c 69 6f 6e 20 66 65 61 74  ome million feat
04e0: 75 72 65 73 2e 3c 62 72 3e 0d 0a 4e 6f 77 20 69  ures.<br>..Now i
04f0: 6d 61 67 69 6e 65 20 74 68 61 74 20 66 6f 72 20  magine that for 
0500: 61 6e 79 20 67 6f 6f 64 20 72 65 61 73 6f 6e 20  any good reason 
0510: 79 6f 75 20 61 72 65 20 69 6e 74 65 72 65 73 74  you are interest
0520: 65 64 20 69 6e 20 71 75 69 63 6b 6c 79 20 69 64  ed in quickly id
0530: 65 6e 74 69 66 79 69 6e 67 20 61 6c 6c 20 66 65  entifying all fe
0540: 61 74 75 72 65 73 20 6c 61 79 69 6e 67 20 69 6e  atures laying in
0550: 20 63 6c 6f 73 65 20 73 70 61 74 69 61 6c 20 70   close spatial p
0560: 72 6f 78 69 6d 69 74 79 20 74 6f 20 61 6e 20 61  roximity to an a
0570: 72 62 69 74 72 61 72 79 20 6c 6f 63 61 74 69 6f  rbitrary locatio
0580: 6e 2e 3c 62 72 3e 0d 0a 54 68 69 73 20 69 73 20  n.<br>..This is 
0590: 74 68 65 20 74 79 70 69 63 61 6c 20 63 61 73 65  the typical case
05a0: 20 6f 66 20 61 20 3c 62 3e 4b 4e 4e 3c 2f 62 3e   of a <b>KNN</b>
05b0: 20 70 72 6f 62 6c 65 6d 2e 3c 2f 74 64 3e 3c 2f   problem.</td></
05c0: 74 72 3e 0d 0a 3c 74 72 3e 3c 74 64 3e 4a 75 73  tr>..<tr><td>Jus
05d0: 74 20 66 65 77 20 70 72 61 63 74 69 63 61 6c 20  t few practical 
05e0: 72 65 61 6c 20 77 6f 72 6c 64 20 65 78 61 6d 70  real world examp
05f0: 6c 65 73 3a 0d 0a 3c 75 6c 3e 0d 0a 3c 6c 69 3e  les:..<ul>..<li>
0600: 53 75 70 70 6f 73 65 20 61 20 73 6d 61 72 74 70  Suppose a smartp
0610: 68 6f 6e 65 20 61 70 70 3a 20 79 6f 75 72 20 63  hone app: your c
0620: 75 72 72 65 6e 74 20 67 65 6f 67 72 61 70 68 69  urrent geographi
0630: 63 73 20 6c 6f 63 61 74 69 6f 6e 20 69 73 20 77  cs location is w
0640: 65 6c 6c 20 6b 6e 6f 77 6e 20 62 79 20 74 68 65  ell known by the
0650: 20 69 6e 74 65 67 72 61 74 65 64 20 47 50 53 20   integrated GPS 
0660: 73 65 6e 73 6f 72 2c 20 73 6f 20 79 6f 75 20 61  sensor, so you a
0670: 72 65 20 73 69 6d 70 6c 79 20 65 78 70 65 63 74  re simply expect
0680: 69 6e 67 20 74 68 61 74 20 74 68 65 20 61 70 70  ing that the app
0690: 20 77 69 6c 6c 20 69 6d 6d 65 64 69 61 74 65 6c   will immediatel
06a0: 79 20 74 65 6c 6c 20 79 6f 75 20 77 68 69 63 68  y tell you which
06b0: 20 61 72 65 20 28 61 6e 64 20 77 68 65 72 65 20   are (and where 
06c0: 74 68 65 79 20 61 72 65 20 65 78 61 63 74 65 6c  they are exactel
06d0: 79 20 6c 6f 63 61 74 65 64 29 20 74 68 65 20 6e  y located) the n
06e0: 65 61 72 65 73 74 20 66 75 65 6c 20 73 74 61 74  earest fuel stat
06f0: 69 6f 6e 73 20 28 6f 72 20 6d 61 79 62 65 20 68  ions (or maybe h
0700: 6f 74 65 6c 73 2c 20 72 65 73 74 61 75 72 61 6e  otels, restauran
0710: 74 73 20 6f 72 20 77 68 61 74 65 76 65 72 20 65  ts or whatever e
0720: 6c 73 65 29 2e 3c 2f 6c 69 3e 0d 0a 3c 6c 69 3e  lse).</li>..<li>
0730: 53 75 70 70 6f 73 65 20 61 20 7a 6f 6f 6c 6f 67  Suppose a zoolog
0740: 69 63 61 6c 2f 65 63 68 6f 6c 6f 67 69 63 61 6c  ical/echological
0750: 20 73 74 75 64 79 3a 20 79 6f 75 20 6b 6e 6f 77   study: you know
0760: 20 77 68 65 72 65 20 61 72 65 20 6c 6f 63 61 74   where are locat
0770: 65 64 20 74 68 65 20 6e 65 73 74 69 6e 67 20 61  ed the nesting a
0780: 72 65 61 73 20 6f 66 20 73 6f 6d 65 20 72 61 72  reas of some rar
0790: 65 20 77 61 74 65 72 20 62 69 72 64 20 73 70 65  e water bird spe
07a0: 63 69 65 73 2c 20 61 6e 64 20 79 6f 75 20 6b 6e  cies, and you kn
07b0: 6f 77 20 77 68 65 72 65 20 61 72 65 20 6c 6f 63  ow where are loc
07c0: 61 74 65 64 20 61 6c 6c 20 72 69 76 65 72 73 2c  ated all rivers,
07d0: 20 6c 61 6b 65 73 2c 20 70 6f 6e 64 73 20 61 6e   lakes, ponds an
07e0: 64 20 6d 61 72 73 68 6c 61 6e 64 73 20 69 6e 20  d marshlands in 
07f0: 74 68 65 20 72 65 67 69 6f 6e 20 6f 66 20 79 6f  the region of yo
0800: 75 72 20 69 6e 74 65 72 65 73 65 74 2e 3c 62 72  ur intereset.<br
0810: 3e 0d 0a 4e 6f 77 20 79 6f 75 20 61 72 65 20 61  >..Now you are a
0820: 74 74 65 6d 70 74 69 6e 67 20 74 6f 20 67 65 74  ttempting to get
0830: 20 61 20 72 65 61 73 6f 6e 61 62 6c 65 20 61 73   a reasonable as
0840: 73 6f 63 69 61 74 69 6f 6e 20 62 65 74 77 65 65  sociation betwee
0850: 6e 20 6e 65 73 74 69 6e 67 20 61 72 65 61 73 20  n nesting areas 
0860: 61 6e 64 20 66 65 65 64 69 6e 67 20 7a 6f 6e 65  and feeding zone
0870: 73 20 62 61 73 65 64 20 6f 6e 20 6d 69 6e 69 6d  s based on minim
0880: 61 6c 20 64 69 73 74 61 6e 63 65 20 63 72 69 74  al distance crit
0890: 65 72 69 61 2e 3c 2f 6c 69 3e 0d 0a 3c 2f 75 6c  eria.</li>..</ul
08a0: 3e 0d 0a 3c 2f 74 64 3e 3c 2f 74 72 3e 0d 0a 3c  >..</td></tr>..<
08b0: 74 72 3e 3c 74 64 3e 3c 62 3e 4e 6f 74 65 3c 2f  tr><td><b>Note</
08c0: 62 3e 3a 20 69 6e 20 62 6f 74 68 20 63 61 73 65  b>: in both case
08d0: 73 20 79 6f 75 20 63 61 6e 27 74 20 73 61 66 65  s you can't safe
08e0: 6c 79 20 69 64 65 6e 74 69 66 79 20 61 20 72 65  ly identify a re
08f0: 61 73 6f 6e 61 62 6c 65 20 3c 69 3e 3c 75 3e 6d  asonable <i><u>m
0900: 69 6e 69 6d 61 6c 20 72 61 64 69 75 73 3c 2f 75  inimal radius</u
0910: 3e 3c 2f 69 3e 20 73 6f 20 74 6f 20 75 73 65 66  ></i> so to usef
0920: 75 6c 6c 79 20 72 65 73 74 72 69 63 74 20 79 6f  ully restrict yo
0930: 75 72 20 73 65 61 72 63 68 2c 20 73 69 6d 70 6c  ur search, simpl
0940: 79 20 62 65 63 61 75 73 65 20 79 6f 75 20 63 61  y because you ca
0950: 6e 27 74 20 6d 61 6b 65 20 61 6e 79 20 61 73 73  n't make any ass
0960: 75 6d 70 74 69 6f 6e 20 61 62 6f 75 74 20 74 68  umption about th
0970: 65 20 67 65 6f 67 72 61 70 68 69 63 20 64 69 73  e geographic dis
0980: 70 65 6e 73 69 6f 6e 20 6f 66 20 79 6f 75 72 20  pension of your 
0990: 74 61 72 67 65 74 73 2e 3c 62 72 3e 0d 0a 54 68  targets.<br>..Th
09a0: 65 20 64 69 73 74 61 6e 63 65 73 20 6d 61 79 20  e distances may 
09b0: 6e 6f 74 69 63 65 61 62 6c 79 20 76 61 72 79 20  noticeably vary 
09c0: 66 72 6f 6d 20 63 61 73 65 20 74 6f 20 63 61 73  from case to cas
09d0: 65 2c 20 61 6e 64 20 79 6f 75 20 68 61 76 65 6e  e, and you haven
09e0: 27 74 20 61 6e 79 20 69 64 65 61 20 61 62 6f 75  't any idea abou
09f0: 74 20 74 68 69 73 20 62 65 63 61 75 73 65 20 69  t this because i
0a00: 74 20 73 74 72 69 63 74 6c 79 20 64 65 70 65 6e  t strictly depen
0a10: 64 73 20 6f 6e 20 74 68 65 20 61 63 74 75 61 6c  ds on the actual
0a20: 20 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 74 68 65   location of the
0a30: 20 73 65 61 72 63 68 20 6f 72 69 67 69 6e 20 61   search origin a
0a40: 6e 64 20 6f 6e 20 74 68 65 20 73 74 61 74 69 73  nd on the statis
0a50: 74 69 63 61 6c 20 64 69 73 70 65 72 73 69 6f 6e  tical dispersion
0a60: 20 6f 66 20 74 68 65 20 74 61 72 67 65 74 73 20   of the targets 
0a70: 74 6f 20 62 65 20 73 65 61 72 63 68 65 64 2e 3c  to be searched.<
0a80: 2f 74 64 3e 3c 2f 74 72 3e 0d 0a 3c 74 72 3e 3c  /td></tr>..<tr><
0a90: 74 64 3e 3c 68 72 3e 3c 2f 74 64 3e 3c 2f 74 72  td><hr></td></tr
0aa0: 3e 0d 0a 3c 74 72 3e 3c 74 64 3e 0d 0a 3c 62 3e  >..<tr><td>..<b>
0ab0: 41 20 66 69 72 73 74 20 6e 61 69 76 65 20 28 61  A first naive (a
0ac0: 6e 64 20 76 65 72 79 20 69 6e 65 66 66 69 63 69  nd very ineffici
0ad0: 65 6e 74 29 20 61 70 70 72 6f 61 63 68 3c 2f 62  ent) approach</b
0ae0: 3e 3c 62 72 3e 0d 0a 3c 76 65 72 62 61 74 69 6d  ><br>..<verbatim
0af0: 3e 0d 0a 53 45 4c 45 43 54 20 67 65 6f 6e 61 6d  >..SELECT geonam
0b00: 65 69 64 2c 20 6e 61 6d 65 2c 20 63 6f 75 6e 74  eid, name, count
0b10: 72 79 2c 20 0d 0a 20 20 20 20 4d 69 6e 28 53 54  ry, ..    Min(ST
0b20: 5f 44 69 73 74 61 6e 63 65 28 4d 61 6b 65 50 6f  _Distance(MakePo
0b30: 69 6e 74 28 31 30 2c 20 34 33 29 2c 20 67 65 6f  int(10, 43), geo
0b40: 6d 2c 20 31 29 29 20 2f 20 31 30 30 30 2e 30 20  m, 1)) / 1000.0 
0b50: 41 53 20 64 69 73 74 5f 6b 6d 0d 0a 46 52 4f 4d  AS dist_km..FROM
0b60: 20 61 69 72 70 6f 72 74 73 3b 0d 0a 2d 2d 2d 2d   airports;..----
0b70: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0b80: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0d 0a 36 32  ------------..62
0b90: 39 39 36 32 33 09 7c 20 4d 61 72 69 6e 61 20 44  99623.| Marina D
0ba0: 69 20 43 61 6d 70 6f 20 7c 20 49 54 20 7c 20 33  i Campo | IT | 3
0bb0: 33 2e 30 34 33 33 32 30 0d 0a 0d 0a 53 45 4c 45  3.043320....SELE
0bc0: 43 54 20 67 65 6f 6e 61 6d 65 69 64 2c 20 6e 61  CT geonameid, na
0bd0: 6d 65 2c 20 63 6f 75 6e 74 72 79 2c 20 0d 0a 20  me, country, .. 
0be0: 20 20 20 4d 69 6e 28 53 54 5f 44 69 73 74 61 6e     Min(ST_Distan
0bf0: 63 65 28 4d 61 6b 65 50 6f 69 6e 74 28 31 30 2c  ce(MakePoint(10,
0c00: 20 34 33 29 2c 20 67 65 6f 6d 2c 20 31 29 29 20   43), geom, 1)) 
0c10: 2f 20 31 30 30 30 2e 30 20 41 53 20 64 69 73 74  / 1000.0 AS dist
0c20: 5f 6b 6d 0d 0a 46 52 4f 4d 20 61 69 72 70 6f 72  _km..FROM airpor
0c30: 74 73 0d 0a 57 48 45 52 45 20 67 65 6f 6e 61 6d  ts..WHERE geonam
0c40: 65 69 64 20 4e 4f 54 20 49 4e 20 28 36 32 39 39  eid NOT IN (6299
0c50: 36 32 33 29 3b 0d 0a 2d 2d 2d 2d 2d 2d 2d 2d 2d  623);..---------
0c60: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0c70: 2d 2d 2d 2d 2d 2d 2d 2d 0d 0a 36 32 39 39 33 39  --------..629939
0c80: 32 20 7c 20 42 61 73 74 69 61 2d 50 6f 72 65 74  2 | Bastia-Poret
0c90: 74 61 20 7c 20 46 52 20 7c 20 36 35 2e 32 32 36  ta | FR | 65.226
0ca0: 35 37 33 0d 0a 0d 0a 53 45 4c 45 43 54 20 67 65  573....SELECT ge
0cb0: 6f 6e 61 6d 65 69 64 2c 20 6e 61 6d 65 2c 20 63  onameid, name, c
0cc0: 6f 75 6e 74 72 79 2c 0d 0a 20 20 20 20 4d 69 6e  ountry,..    Min
0cd0: 28 53 54 5f 44 69 73 74 61 6e 63 65 28 4d 61 6b  (ST_Distance(Mak
0ce0: 65 50 6f 69 6e 74 28 31 30 2c 20 34 33 29 2c 20  ePoint(10, 43), 
0cf0: 67 65 6f 6d 2c 20 31 29 29 20 2f 20 31 30 30 30  geom, 1)) / 1000
0d00: 2e 30 20 41 53 20 64 69 73 74 5f 6b 6d 0d 0a 46  .0 AS dist_km..F
0d10: 52 4f 4d 20 61 69 72 70 6f 72 74 73 0d 0a 57 48  ROM airports..WH
0d20: 45 52 45 20 67 65 6f 6e 61 6d 65 69 64 20 4e 4f  ERE geonameid NO
0d30: 54 20 49 4e 20 28 36 32 39 39 36 32 33 2c 20 36  T IN (6299623, 6
0d40: 32 39 39 33 39 32 29 3b 0d 0a 2d 2d 2d 2d 2d 2d  299392);..------
0d50: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0d60: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0d 0a 36 32 39  -----------..629
0d70: 39 36 32 38 20 7c 20 50 69 73 61 20 2f 20 53 2e  9628 | Pisa / S.
0d80: 20 47 69 75 73 74 6f 20 7c 20 49 54 09 7c 20 38   Giusto | IT.| 8
0d90: 32 2e 33 38 37 30 31 34 0d 0a 0d 0a 53 45 4c 45  2.387014....SELE
0da0: 43 54 20 67 65 6f 6e 61 6d 65 69 64 2c 20 6e 61  CT geonameid, na
0db0: 6d 65 2c 20 63 6f 75 6e 74 72 79 2c 20 0d 0a 20  me, country, .. 
0dc0: 20 20 20 4d 69 6e 28 53 54 5f 44 69 73 74 61 6e     Min(ST_Distan
0dd0: 63 65 28 4d 61 6b 65 50 6f 69 6e 74 28 31 30 2c  ce(MakePoint(10,
0de0: 20 34 33 29 2c 20 67 65 6f 6d 2c 20 31 29 29 20   43), geom, 1)) 
0df0: 2f 20 31 30 30 30 2e 30 20 41 53 20 64 69 73 74  / 1000.0 AS dist
0e00: 5f 6b 6d 0d 0a 46 52 4f 4d 20 61 69 72 70 6f 72  _km..FROM airpor
0e10: 74 73 0d 0a 57 48 45 52 45 20 67 65 6f 6e 61 6d  ts..WHERE geonam
0e20: 65 69 64 20 4e 4f 54 20 49 4e 20 28 36 32 39 39  eid NOT IN (6299
0e30: 36 32 33 2c 20 36 32 39 39 33 39 32 2c 20 36 32  623, 6299392, 62
0e40: 39 39 36 32 38 29 3b 0d 0a 2d 2d 2d 2d 2d 2d 2d  99628);..-------
0e50: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0e60: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0d 0a 36 32 39 39  ----------..6299
0e70: 36 33 30 20 7c 20 47 72 6f 73 73 65 74 6f 20 41  630 | Grosseto A
0e80: 69 72 70 6f 72 74 20 7c 20 49 54 20 7c 20 39 31  irport | IT | 91
0e90: 2e 35 34 39 37 37 33 0d 0a 0d 0a 53 45 4c 45 43  .549773....SELEC
0ea0: 54 20 67 65 6f 6e 61 6d 65 69 64 2c 20 6e 61 6d  T geonameid, nam
0eb0: 65 2c 20 63 6f 75 6e 74 72 79 2c 20 0d 0a 20 20  e, country, ..  
0ec0: 20 20 4d 69 6e 28 53 54 5f 44 69 73 74 61 6e 63    Min(ST_Distanc
0ed0: 65 28 4d 61 6b 65 50 6f 69 6e 74 28 31 30 2c 20  e(MakePoint(10, 
0ee0: 34 33 29 2c 20 67 65 6f 6d 2c 20 31 29 29 20 2f  43), geom, 1)) /
0ef0: 20 31 30 30 30 2e 30 20 41 53 20 64 69 73 74 5f   1000.0 AS dist_
0f00: 6b 6d 0d 0a 46 52 4f 4d 20 61 69 72 70 6f 72 74  km..FROM airport
0f10: 73 0d 0a 57 48 45 52 45 20 67 65 6f 6e 61 6d 65  s..WHERE geoname
0f20: 69 64 20 4e 4f 54 20 49 4e 20 28 36 32 39 39 36  id NOT IN (62996
0f30: 32 33 2c 20 36 32 39 39 33 39 32 2c 20 36 32 39  23, 6299392, 629
0f40: 39 36 32 38 2c 20 36 32 39 39 36 33 30 29 3b 0d  9628, 6299630);.
0f50: 0a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  .---------------
0f60: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0f70: 2d 2d 0d 0a 36 36 39 34 34 39 35 20 7c 20 43 6f  --..6694495 | Co
0f80: 72 74 65 20 7c 20 46 52 20 7c 20 31 30 32 2e 38  rte | FR | 102.8
0f90: 31 39 37 37 38 0d 0a 3c 2f 76 65 72 62 61 74 69  19778..</verbati
0fa0: 6d 3e 0d 0a 57 65 20 63 6f 75 6c 64 20 65 66 66  m>..We could eff
0fb0: 65 63 74 69 76 65 6c 79 20 65 78 65 63 75 74 65  ectively execute
0fc0: 20 61 20 66 69 72 73 74 20 53 51 4c 20 71 75 65   a first SQL que
0fd0: 72 79 20 75 73 69 6e 67 20 74 68 65 20 3c 62 3e  ry using the <b>
0fe0: 4d 69 6e 28 29 3c 2f 62 3e 20 61 67 67 72 65 67  Min()</b> aggreg
0ff0: 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 69 6e 20  ate function in 
1000: 6f 72 64 65 72 20 74 6f 20 69 64 65 6e 74 69 66  order to identif
1010: 79 20 74 68 65 20 63 6c 6f 73 65 73 74 20 61 69  y the closest ai
1020: 72 70 6f 72 74 20 74 6f 20 61 6e 20 61 72 62 69  rport to an arbi
1030: 74 72 61 72 79 20 70 6f 73 69 74 69 6f 6e 20 28  trary position (
1040: 3c 69 3e 6c 61 74 69 74 75 64 65 3c 2f 69 3e 3d  <i>latitude</i>=
1050: 3c 62 3e 34 33 2e 30 3c 2f 62 3e 2c 20 3c 69 3e  <b>43.0</b>, <i>
1060: 6c 6f 6e 67 69 74 75 64 65 3c 2f 69 3e 3d 3c 62  longitude</i>=<b
1070: 3e 31 30 2e 30 3c 2f 62 3e 29 20 6c 6f 63 61 74  >10.0</b>) locat
1080: 65 64 20 69 6e 74 6f 20 74 68 65 20 6d 69 64 64  ed into the midd
1090: 6c 65 20 6f 66 20 74 68 65 20 54 79 72 72 68 65  le of the Tyrrhe
10a0: 6e 69 61 6e 20 53 65 61 2e 3c 62 72 3e 0d 0a 54  nian Sea.<br>..T
10b0: 68 65 6e 20 77 65 20 63 6f 75 6c 64 20 65 76 65  hen we could eve
10c0: 6e 74 75 61 6c 6c 79 20 72 65 70 65 61 74 20 6d  ntually repeat m
10d0: 6f 72 65 20 74 69 6d 65 73 20 74 68 65 20 73 61  ore times the sa
10e0: 6d 65 20 69 64 65 6e 74 69 63 61 6c 20 71 75 65  me identical que
10f0: 72 79 2c 20 65 61 63 68 20 74 69 6d 65 20 65 78  ry, each time ex
1100: 63 6c 75 64 69 6e 67 20 61 6c 6c 20 61 69 72 70  cluding all airp
1110: 6f 72 74 73 20 77 65 27 76 65 20 61 6c 72 65 61  orts we've alrea
1120: 64 79 20 69 64 65 6e 74 69 66 69 65 64 20 69 6e  dy identified in
1130: 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 70 72   the previous pr
1140: 6f 63 65 73 73 69 6e 67 20 73 74 65 70 73 2e 3c  ocessing steps.<
1150: 62 72 3e 0d 0a 54 68 65 20 66 69 6e 61 6c 20 72  br>..The final r
1160: 65 73 75 6c 74 20 77 69 6c 6c 20 62 65 20 74 68  esult will be th
1170: 65 20 66 75 6c 6c 20 6c 69 73 74 20 6f 66 20 74  e full list of t
1180: 68 65 20 66 69 72 73 74 20 66 69 76 65 20 61 69  he first five ai
1190: 72 70 6f 72 74 73 20 63 6c 6f 73 65 73 74 20 74  rports closest t
11a0: 6f 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20  o the reference 
11b0: 6c 6f 63 61 74 69 6f 6e 20 6f 72 64 65 72 65 64  location ordered
11c0: 20 62 79 20 69 6e 63 72 65 61 73 69 6e 67 20 64   by increasing d
11d0: 69 73 74 61 6e 63 65 2e 0d 0a 3c 62 72 3e 3c 62  istance...<br><b
11e0: 72 3e 0d 0a 3c 74 61 62 6c 65 20 62 67 63 6f 6c  r>..<table bgcol
11f0: 6f 72 3d 22 23 64 30 66 66 62 30 22 20 63 65 6c  or="#d0ffb0" cel
1200: 6c 73 70 61 63 69 6e 67 3d 22 34 22 20 63 65 6c  lspacing="4" cel
1210: 6c 70 61 64 64 69 6e 67 3d 22 34 22 20 62 6f 72  lpadding="4" bor
1220: 64 65 72 3d 22 31 22 3e 0d 0a 3c 74 72 3e 3c 74  der="1">..<tr><t
1230: 64 20 62 67 63 6f 6c 6f 72 3d 22 23 63 30 64 64  d bgcolor="#c0dd
1240: 61 6f 22 3e 3c 62 3e 72 61 6e 6b 3c 2f 62 3e 3c  ao"><b>rank</b><
1250: 2f 74 64 3e 3c 74 64 20 62 67 63 6f 6c 6f 72 3d  /td><td bgcolor=
1260: 22 23 63 30 64 64 61 6f 22 3e 3c 62 3e 67 65 6f  "#c0ddao"><b>geo
1270: 6e 61 6d 65 69 64 3c 2f 62 3e 3c 2f 74 64 3e 3c  nameid</b></td><
1280: 74 64 20 62 67 63 6f 6c 6f 72 3d 22 23 63 30 64  td bgcolor="#c0d
1290: 64 61 6f 22 3e 3c 62 3e 6e 61 6d 65 3c 2f 62 3e  dao"><b>name</b>
12a0: 3c 2f 74 64 3e 3c 74 64 20 62 67 63 6f 6c 6f 72  </td><td bgcolor
12b0: 3d 22 23 63 30 64 64 61 6f 22 3e 3c 62 3e 63 6f  ="#c0ddao"><b>co
12c0: 75 6e 74 72 79 3c 2f 62 3e 3c 2f 74 64 3e 3c 74  untry</b></td><t
12d0: 64 20 62 67 63 6f 6c 6f 72 3d 22 23 63 30 64 64  d bgcolor="#c0dd
12e0: 61 6f 22 3e 3c 62 3e 64 69 73 74 5f 6b 6d 3c 2f  ao"><b>dist_km</
12f0: 62 3e 3c 2f 74 64 3e 3c 2f 74 72 3e 0d 0a 3c 74  b></td></tr>..<t
1300: 72 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69 67  r><td align="rig
1310: 68 74 22 3e 31 3c 2f 74 64 3e 3c 74 64 20 61 6c  ht">1</td><td al
1320: 69 67 6e 3d 22 72 69 67 68 74 22 3e 36 32 39 39  ign="right">6299
1330: 36 32 33 3c 2f 74 64 3e 3c 74 64 3e 4d 61 72 69  623</td><td>Mari
1340: 6e 61 20 44 69 20 43 61 6d 70 6f 3c 2f 74 64 3e  na Di Campo</td>
1350: 3c 74 64 3e 49 54 3c 2f 74 64 3e 3c 74 64 20 61  <td>IT</td><td a
1360: 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e 33 33 2e  lign="right">33.
1370: 30 34 33 33 32 30 3c 2f 74 64 3e 3c 2f 74 72 3e  043320</td></tr>
1380: 0d 0a 3c 74 72 3e 3c 74 64 20 61 6c 69 67 6e 3d  ..<tr><td align=
1390: 22 72 69 67 68 74 22 3e 32 3c 2f 74 64 3e 3c 74  "right">2</td><t
13a0: 64 20 61 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e  d align="right">
13b0: 36 32 39 39 33 39 32 3c 2f 74 64 3e 3c 74 64 3e  6299392</td><td>
13c0: 42 61 73 74 69 61 2d 50 6f 72 65 74 74 61 3c 2f  Bastia-Poretta</
13d0: 74 64 3e 3c 74 64 3e 46 52 3c 2f 74 64 3e 3c 74  td><td>FR</td><t
13e0: 64 20 61 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e  d align="right">
13f0: 36 35 2e 32 32 36 35 37 33 3c 2f 74 64 3e 3c 2f  65.226573</td></
1400: 74 72 3e 0d 0a 3c 74 72 3e 3c 74 64 20 61 6c 69  tr>..<tr><td ali
1410: 67 6e 3d 22 72 69 67 68 74 22 3e 33 3c 2f 74 64  gn="right">3</td
1420: 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69 67 68  ><td align="righ
1430: 74 22 3e 36 32 39 39 36 32 38 3c 2f 74 64 3e 3c  t">6299628</td><
1440: 74 64 3e 50 69 73 61 20 2f 20 53 2e 20 47 69 75  td>Pisa / S. Giu
1450: 73 74 6f 3c 2f 74 64 3e 3c 74 64 3e 49 54 3c 2f  sto</td><td>IT</
1460: 74 64 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69  td><td align="ri
1470: 67 68 74 22 3e 38 32 2e 33 38 37 30 31 34 3c 2f  ght">82.387014</
1480: 74 64 3e 3c 2f 74 72 3e 0d 0a 3c 74 72 3e 3c 74  td></tr>..<tr><t
1490: 64 20 61 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e  d align="right">
14a0: 34 3c 2f 74 64 3e 3c 74 64 20 61 6c 69 67 6e 3d  4</td><td align=
14b0: 22 72 69 67 68 74 22 3e 36 32 39 39 36 33 30 3c  "right">6299630<
14c0: 2f 74 64 3e 3c 74 64 3e 47 72 6f 73 73 65 74 6f  /td><td>Grosseto
14d0: 20 41 69 72 70 6f 72 74 3c 2f 74 64 3e 3c 74 64   Airport</td><td
14e0: 3e 49 54 3c 2f 74 64 3e 3c 74 64 20 61 6c 69 67  >IT</td><td alig
14f0: 6e 3d 22 72 69 67 68 74 22 3e 39 31 2e 35 34 39  n="right">91.549
1500: 37 37 33 3c 2f 74 64 3e 3c 2f 74 72 3e 0d 0a 3c  773</td></tr>..<
1510: 74 72 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69  tr><td align="ri
1520: 67 68 74 22 3e 35 3c 2f 74 64 3e 3c 74 64 20 61  ght">5</td><td a
1530: 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e 36 36 39  lign="right">669
1540: 34 34 39 35 3c 2f 74 64 3e 3c 74 64 3e 43 6f 72  4495</td><td>Cor
1550: 74 65 3c 2f 74 64 3e 3c 74 64 3e 46 52 3c 2f 74  te</td><td>FR</t
1560: 64 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69 67  d><td align="rig
1570: 68 74 22 3e 31 30 32 2e 38 31 39 37 37 38 3c 2f  ht">102.819778</
1580: 74 64 3e 3c 2f 74 72 3e 0d 0a 3c 2f 74 61 62 6c  td></tr>..</tabl
1590: 65 3e 3c 62 72 3e 0d 0a 53 75 63 68 20 61 20 6e  e><br>..Such a n
15a0: 61 69 76 65 20 61 70 70 72 6f 61 63 68 20 77 69  aive approach wi
15b0: 6c 6c 20 73 75 72 65 6c 79 20 62 65 20 68 69 67  ll surely be hig
15c0: 68 6c 79 20 69 6d 70 72 61 63 74 69 63 61 6c 2c  hly impractical,
15d0: 20 62 65 63 61 75 73 65 20 69 74 20 73 74 72 69   because it stri
15e0: 63 74 6c 79 20 72 65 71 75 69 72 65 73 20 74 6f  ctly requires to
15f0: 20 68 61 6e 64 2d 77 72 69 74 65 20 73 65 76 65   hand-write seve
1600: 72 61 6c 20 3c 69 3e 61 64 20 68 6f 63 3c 2f 69  ral <i>ad hoc</i
1610: 3e 20 53 51 4c 20 71 75 65 72 69 65 73 20 6f 6e  > SQL queries on
1620: 65 20 62 79 20 6f 6e 65 2e 3c 62 72 3e 0d 0a 57  e by one.<br>..W
1630: 72 69 74 69 6e 67 20 61 20 66 75 6c 6c 79 20 61  riting a fully a
1640: 75 74 6f 6d 61 74 65 64 20 53 51 4c 20 73 63 72  utomated SQL scr
1650: 69 70 74 20 69 73 6e 27 74 20 61 74 20 61 6c 6c  ipt isn't at all
1660: 20 61 20 73 69 6d 70 6c 65 20 74 61 73 6b 2c 20   a simple task, 
1670: 61 6e 64 20 73 6f 6d 65 20 6b 69 6e 64 20 6f 66  and some kind of
1680: 20 68 69 67 68 65 72 20 6c 65 76 65 6c 20 73 63   higher level sc
1690: 72 69 70 74 69 6e 67 20 28 65 2e 67 2e 20 50 79  ripting (e.g. Py
16a0: 74 68 6f 6e 29 20 77 6f 75 6c 64 20 62 65 20 65  thon) would be e
16b0: 61 73 69 6c 79 20 72 65 71 75 69 72 65 64 20 69  asily required i
16c0: 6e 20 61 6e 79 20 72 65 61 6c 69 73 74 69 63 20  n any realistic 
16d0: 73 63 65 6e 61 72 69 6f 2e 3c 62 72 3e 0d 0a 41  scenario.<br>..A
16e0: 6e 79 77 61 79 20 61 75 74 6f 6d 61 74 69 6e 67  nyway automating
16f0: 20 73 6f 6d 65 77 61 79 20 61 6c 6c 20 72 65 71   someway all req
1700: 75 69 72 65 64 20 53 51 4c 20 71 75 65 72 69 65  uired SQL querie
1710: 73 20 69 73 6e 27 74 20 74 68 65 20 6d 6f 73 74  s isn't the most
1720: 20 63 72 69 74 69 63 61 6c 20 69 73 73 75 65 20   critical issue 
1730: 77 65 20 68 61 76 65 20 74 6f 20 66 61 63 65 3b  we have to face;
1740: 20 74 68 65 20 72 65 61 6c 20 63 72 69 74 69 63   the real critic
1750: 61 6c 20 62 6f 74 74 6c 65 20 6e 65 63 6b 20 69  al bottle neck i
1760: 6e 20 74 68 69 73 20 74 6f 6f 20 6d 75 63 68 20  n this too much 
1770: 6e 61 69 76 65 20 61 70 70 72 6f 61 63 68 20 69  naive approach i
1780: 73 20 74 68 61 74 20 65 61 63 68 20 73 69 6e 67  s that each sing
1790: 6c 65 20 71 75 65 72 79 20 77 69 6c 6c 20 6e 65  le query will ne
17a0: 63 65 73 73 61 72 69 6c 79 20 70 65 72 66 6f 72  cessarily perfor
17b0: 6d 20 61 6e 20 61 77 66 75 6c 6c 20 3c 61 20 68  m an awfull <a h
17c0: 72 65 66 3d 22 68 74 74 70 73 3a 2f 2f 65 6e 2e  ref="https://en.
17d0: 77 69 6b 69 70 65 64 69 61 2e 6f 72 67 2f 77 69  wikipedia.org/wi
17e0: 6b 69 2f 46 75 6c 6c 5f 74 61 62 6c 65 5f 73 63  ki/Full_table_sc
17f0: 61 6e 22 3e 66 75 6c 6c 20 74 61 62 6c 65 20 73  an">full table s
1800: 63 61 6e 3c 2f 61 3e 3c 62 72 3e 0d 0a 54 68 69  can</a><br>..Thi
1810: 73 20 77 69 6c 6c 20 64 69 72 65 63 74 6c 79 20  s will directly 
1820: 63 61 75 73 65 20 62 61 72 65 6c 79 20 74 6f 6c  cause barely tol
1830: 65 72 61 62 6c 65 20 73 6c 75 67 67 69 73 68 20  erable sluggish 
1840: 70 65 72 66 6f 72 6d 61 6e 63 65 73 2c 20 61 6e  performances, an
1850: 64 20 74 68 65 20 6f 76 65 72 61 6c 6c 20 70 65  d the overall pe
1860: 72 66 6f 72 6d 61 6e 63 65 20 77 69 6c 6c 20 71  rformance will q
1870: 75 69 63 6b 6c 79 20 64 65 67 72 61 64 61 74 65  uickly degradate
1880: 20 61 73 20 74 68 65 20 64 61 74 61 73 65 74 20   as the dataset 
1890: 73 69 7a 65 20 77 69 6c 6c 20 70 72 6f 67 72 65  size will progre
18a0: 73 73 69 76 65 6c 79 20 69 6e 63 72 65 61 73 65  ssively increase
18b0: 2e 3c 62 72 3e 3c 62 72 3e 0d 0a 3c 62 3e 43 6f  .<br><br>..<b>Co
18c0: 6e 63 6c 75 73 69 6f 6e 3c 2f 62 3e 3a 3c 62 72  nclusion</b>:<br
18d0: 3e 0d 0a 53 6f 6d 65 20 73 69 6d 70 6c 65 72 20  >..Some simpler 
18e0: 61 6e 64 20 73 6d 61 72 74 65 72 20 61 70 70 72  and smarter appr
18f0: 6f 61 63 68 20 69 73 20 73 75 72 65 6c 79 20 72  oach is surely r
1900: 65 71 75 69 72 65 64 3a 20 70 6f 73 73 69 62 6c  equired: possibl
1910: 79 20 6f 6e 65 20 74 61 6b 69 6e 67 20 66 75 6c  y one taking ful
1920: 6c 20 70 72 6f 66 69 74 20 66 72 6f 6d 20 61 6e  l profit from an
1930: 20 3c 62 3e 52 2a 54 52 65 65 20 53 70 61 74 69   <b>R*TRee Spati
1940: 61 6c 20 49 6e 64 65 78 3c 2f 62 3e 20 73 75 70  al Index</b> sup
1950: 70 6f 72 74 69 6e 67 20 74 68 65 20 47 65 6f 6d  porting the Geom
1960: 65 74 72 69 65 73 20 74 6f 20 62 65 20 73 65 61  etries to be sea
1970: 72 63 68 65 64 2e 0d 0a 3c 2f 74 64 3e 3c 2f 74  rched...</td></t
1980: 72 3e 0d 0a 3c 2f 74 61 62 6c 65 3e 0d 0a 3c 68  r>..</table>..<h
1990: 32 3e 56 69 72 74 75 61 6c 4b 4e 4e 3c 2f 68 32  2>VirtualKNN</h2
19a0: 3e 0d 0a 53 74 61 72 74 69 6e 67 20 73 69 6e 63  >..Starting sinc
19b0: 65 20 76 65 72 73 69 6f 6e 20 3c 62 3e 34 2e 34  e version <b>4.4
19c0: 2e 30 3c 2f 62 3e 20 53 70 61 74 69 61 4c 69 74  .0</b> SpatiaLit
19d0: 65 20 73 75 70 70 6f 72 74 73 20 61 20 3c 62 3e  e supports a <b>
19e0: 56 69 72 74 75 61 6c 4b 4e 4e 3c 2f 62 3e 20 3c  VirtualKNN</b> <
19f0: 61 20 68 72 65 66 3d 22 68 74 74 70 73 3a 2f 2f  a href="https://
1a00: 77 77 77 2e 73 71 6c 69 74 65 2e 6f 72 67 2f 76  www.sqlite.org/v
1a10: 74 61 62 2e 68 74 6d 6c 22 3e 56 69 72 74 75 61  tab.html">Virtua
1a20: 6c 20 54 61 62 6c 65 3c 2f 61 3e 20 73 70 65 63  l Table</a> spec
1a30: 69 66 69 63 61 6c 6c 79 20 69 6e 74 65 6e 64 65  ifically intende
1a40: 64 20 61 73 20 61 20 63 6f 6d 70 6c 65 74 65 20  d as a complete 
1a50: 61 6e 64 20 68 69 67 68 6c 79 20 65 66 66 69 63  and highly effic
1a60: 69 65 6e 74 20 73 6f 6c 75 74 69 6f 6e 20 74 6f  ient solution to
1a70: 20 74 68 65 20 3c 62 3e 4b 4e 4e 3c 2f 62 3e 20   the <b>KNN</b> 
1a80: 70 72 6f 62 6c 65 6d 2e 0d 0a 3c 76 65 72 62 61  problem...<verba
1a90: 74 69 6d 3e 0d 0a 43 52 45 41 54 45 20 56 49 52  tim>..CREATE VIR
1aa0: 54 55 41 4c 20 54 41 42 4c 45 20 6b 6e 6e 20 55  TUAL TABLE knn U
1ab0: 53 49 4e 47 20 56 69 72 74 75 61 6c 4b 4e 4e 28  SING VirtualKNN(
1ac0: 29 3b 0d 0a 3c 2f 76 65 72 62 61 74 69 6d 3e 0d  );..</verbatim>.
1ad0: 0a 45 76 65 72 79 20 6e 65 77 20 64 62 2d 66 69  .Every new db-fi
1ae0: 6c 65 20 62 65 69 6e 67 20 63 72 65 61 74 65 64  le being created
1af0: 20 62 79 20 3c 62 3e 34 2e 34 2e 30 3c 2f 62 3e   by <b>4.4.0</b>
1b00: 20 69 74 73 65 6c 66 20 77 69 6c 6c 20 61 6c 77   itself will alw
1b10: 61 79 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c  ays automaticall
1b20: 79 20 64 65 66 69 6e 65 20 61 20 3c 62 3e 4b 4e  y define a <b>KN
1b30: 4e 3c 2f 62 3e 20 76 69 72 74 75 61 6c 20 74 61  N</b> virtual ta
1b40: 62 6c 65 2e 3c 62 72 3e 0d 0a 59 6f 75 20 63 6f  ble.<br>..You co
1b50: 75 6c 64 20 65 76 65 6e 74 75 61 6c 6c 79 20 61  uld eventually a
1b60: 64 64 20 66 75 6c 6c 20 4b 4e 4e 20 73 75 70 70  dd full KNN supp
1b70: 6f 72 74 20 74 6f 20 61 6e 79 20 64 62 2d 66 69  ort to any db-fi
1b80: 6c 65 20 63 72 65 61 74 65 64 20 62 79 20 61 6e  le created by an
1b90: 79 20 65 61 72 6c 69 65 72 20 76 65 72 73 69 6f  y earlier versio
1ba0: 6e 20 6f 66 20 53 70 61 74 69 61 4c 69 74 65 20  n of SpatiaLite 
1bb0: 62 79 20 6d 61 6e 75 61 6c 6c 79 20 65 78 65 63  by manually exec
1bc0: 75 74 69 6e 67 20 74 68 65 20 61 62 6f 76 65 20  uting the above 
1bd0: 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 3c 62  SQL statement.<b
1be0: 72 3e 0d 0a 3c 62 3e 4e 6f 74 65 3c 2f 62 3e 3a  r>..<b>Note</b>:
1bf0: 20 56 69 72 74 75 61 6c 4b 4e 4e 20 6e 65 63 65   VirtualKNN nece
1c00: 73 73 61 72 69 6c 79 20 72 65 71 75 69 72 65 73  ssarily requires
1c10: 20 34 2e 34 2e 30 20 62 69 6e 61 72 79 20 73 75   4.4.0 binary su
1c20: 70 70 6f 72 74 2c 20 73 6f 20 61 6e 79 20 61 74  pport, so any at
1c30: 74 65 6d 70 74 20 74 6f 20 6f 70 65 6e 20 61 20  tempt to open a 
1c40: 64 62 2d 66 69 6c 65 20 69 6e 63 6c 75 64 69 6e  db-file includin
1c50: 67 20 61 20 56 69 72 74 75 61 6c 4b 4e 4e 20 74  g a VirtualKNN t
1c60: 61 62 6c 65 20 62 79 20 75 73 69 6e 67 20 73 6f  able by using so
1c70: 6d 65 20 70 72 65 76 69 6f 79 73 20 76 65 72 73  me previoys vers
1c80: 69 6f 6e 20 28 3c 3d 20 3c 62 3e 34 2e 33 2e 30  ion (<= <b>4.3.0
1c90: 61 3c 2f 62 3e 29 20 77 69 6c 6c 20 73 75 72 65  a</b>) will sure
1ca0: 6c 79 20 72 61 69 73 65 20 61 6e 20 65 72 72 6f  ly raise an erro
1cb0: 72 20 63 6f 6e 64 69 74 69 6f 6e 2e 3c 62 72 3e  r condition.<br>
1cc0: 3c 62 72 3e 0d 0a 3c 68 72 3e 0d 0a 3c 68 33 3e  <br>..<hr>..<h3>
1cd0: 41 20 66 69 72 73 74 20 62 61 73 69 63 61 6c 6c  A first basicall
1ce0: 79 20 73 69 6d 70 6c 65 20 4b 4e 4e 20 71 75 65  y simple KNN que
1cf0: 72 79 3c 2f 68 33 3e 0d 0a 3c 76 65 72 62 61 74  ry</h3>..<verbat
1d00: 69 6d 3e 0d 0a 53 45 4c 45 43 54 20 2a 20 46 52  im>..SELECT * FR
1d10: 4f 4d 20 6b 6e 6e 0d 0a 57 48 45 52 45 20 66 5f  OM knn..WHERE f_
1d20: 74 61 62 6c 65 5f 6e 61 6d 65 20 3d 20 27 61 69  table_name = 'ai
1d30: 72 70 6f 72 74 73 27 20 41 4e 44 20 72 65 66 5f  rports' AND ref_
1d40: 67 65 6f 6d 65 74 72 79 20 3d 20 4d 61 6b 65 50  geometry = MakeP
1d50: 6f 69 6e 74 28 31 30 2c 20 34 33 29 3b 0d 0a 3c  oint(10, 43);..<
1d60: 2f 76 65 72 62 61 74 69 6d 3e 0d 0a 3c 74 61 62  /verbatim>..<tab
1d70: 6c 65 20 62 67 63 6f 6c 6f 72 3d 22 23 64 30 66  le bgcolor="#d0f
1d80: 66 62 30 22 20 63 65 6c 6c 73 70 61 63 69 6e 67  fb0" cellspacing
1d90: 3d 22 34 22 20 63 65 6c 6c 70 61 64 64 69 6e 67  ="4" cellpadding
1da0: 3d 22 34 22 20 62 6f 72 64 65 72 3d 22 31 22 3e  ="4" border="1">
1db0: 0d 0a 3c 74 72 3e 3c 74 64 20 62 67 63 6f 6c 6f  ..<tr><td bgcolo
1dc0: 72 3d 22 23 63 30 64 64 61 6f 22 3e 3c 62 3e 66  r="#c0ddao"><b>f
1dd0: 5f 74 61 62 6c 65 5f 6e 61 6d 65 3c 2f 62 3e 3c  _table_name</b><
1de0: 2f 74 64 3e 3c 74 64 20 62 67 63 6f 6c 6f 72 3d  /td><td bgcolor=
1df0: 22 23 63 30 64 64 61 6f 22 3e 3c 62 3e 66 5f 67  "#c0ddao"><b>f_g
1e00: 65 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e 3c 2f  eometry_column</
1e10: 62 3e 3c 2f 74 64 3e 3c 74 64 20 62 67 63 6f 6c  b></td><td bgcol
1e20: 6f 72 3d 22 23 63 30 64 64 61 6f 22 3e 3c 62 3e  or="#c0ddao"><b>
1e30: 72 65 66 5f 67 65 6f 6d 65 74 72 79 3c 2f 62 3e  ref_geometry</b>
1e40: 3c 2f 74 64 3e 3c 74 64 20 62 67 63 6f 6c 6f 72  </td><td bgcolor
1e50: 3d 22 23 63 30 64 64 61 6f 22 3e 3c 62 3e 6d 61  ="#c0ddao"><b>ma
1e60: 78 5f 69 74 65 6d 73 3c 2f 62 3e 3c 2f 74 64 3e  x_items</b></td>
1e70: 3c 74 64 20 62 67 63 6f 6c 6f 72 3d 22 23 63 30  <td bgcolor="#c0
1e80: 64 64 61 6f 22 3e 3c 62 3e 70 6f 73 3c 2f 62 3e  ddao"><b>pos</b>
1e90: 3c 2f 74 64 3e 3c 74 64 20 62 67 63 6f 6c 6f 72  </td><td bgcolor
1ea0: 3d 22 23 63 30 64 64 61 6f 22 3e 3c 62 3e 66 69  ="#c0ddao"><b>fi
1eb0: 64 3c 2f 62 3e 3c 2f 74 64 3e 3c 74 64 20 62 67  d</b></td><td bg
1ec0: 63 6f 6c 6f 72 3d 22 23 63 30 64 64 61 6f 22 3e  color="#c0ddao">
1ed0: 3c 62 3e 64 69 73 74 61 6e 63 65 3c 2f 62 3e 3c  <b>distance</b><
1ee0: 2f 74 64 3e 3c 2f 74 72 3e 0d 0a 3c 74 72 3e 3c  /td></tr>..<tr><
1ef0: 74 64 3e 61 69 72 70 6f 72 74 73 3c 2f 74 64 3e  td>airports</td>
1f00: 3c 74 64 3e 67 65 6f 6d 3c 2f 74 64 3e 3c 74 64  <td>geom</td><td
1f10: 3e 42 4c 4f 42 20 73 7a 3d 36 30 20 47 45 4f 4d  >BLOB sz=60 GEOM
1f20: 45 54 52 59 3c 2f 74 64 3e 3c 74 64 20 61 6c 69  ETRY</td><td ali
1f30: 67 6e 3d 22 72 69 67 68 74 22 3e 33 3c 2f 74 64  gn="right">3</td
1f40: 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69 67 68  ><td align="righ
1f50: 74 22 3e 31 3c 2f 74 64 3e 3c 74 64 20 61 6c 69  t">1</td><td ali
1f60: 67 6e 3d 22 72 69 67 68 74 22 3e 36 32 39 39 36  gn="right">62996
1f70: 32 33 3c 2f 74 64 3e 3c 74 64 20 61 6c 69 67 6e  23</td><td align
1f80: 3d 22 72 69 67 68 74 22 3e 33 33 30 34 33 2e 33  ="right">33043.3
1f90: 31 39 35 32 30 3c 2f 74 64 3e 3c 2f 74 72 3e 0d  19520</td></tr>.
1fa0: 0a 3c 74 72 3e 3c 74 64 3e 61 69 72 70 6f 72 74  .<tr><td>airport
1fb0: 73 3c 2f 74 64 3e 3c 74 64 3e 67 65 6f 6d 3c 2f  s</td><td>geom</
1fc0: 74 64 3e 3c 74 64 3e 42 4c 4f 42 20 73 7a 3d 36  td><td>BLOB sz=6
1fd0: 30 20 47 45 4f 4d 45 54 52 59 3c 2f 74 64 3e 3c  0 GEOMETRY</td><
1fe0: 74 64 20 61 6c 69 67 6e 3d 22 72 69 67 68 74 22  td align="right"
1ff0: 3e 33 3c 2f 74 64 3e 3c 74 64 20 61 6c 69 67 6e  >3</td><td align
2000: 3d 22 72 69 67 68 74 22 3e 32 3c 2f 74 64 3e 3c  ="right">2</td><
2010: 74 64 20 61 6c 69 67 6e 3d 22 72 69 67 68 74 22  td align="right"
2020: 3e 36 32 39 39 33 39 32 3c 2f 74 64 3e 3c 74 64  >6299392</td><td
2030: 20 61 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e 36   align="right">6
2040: 35 32 32 36 2e 35 37 33 31 34 39 3c 2f 74 64 3e  5226.573149</td>
2050: 3c 2f 74 72 3e 0d 0a 3c 74 72 3e 3c 74 64 3e 61  </tr>..<tr><td>a
2060: 69 72 70 6f 72 74 73 3c 2f 74 64 3e 3c 74 64 3e  irports</td><td>
2070: 67 65 6f 6d 3c 2f 74 64 3e 3c 74 64 3e 42 4c 4f  geom</td><td>BLO
2080: 42 20 73 7a 3d 36 30 20 47 45 4f 4d 45 54 52 59  B sz=60 GEOMETRY
2090: 3c 2f 74 64 3e 3c 74 64 20 61 6c 69 67 6e 3d 22  </td><td align="
20a0: 72 69 67 68 74 22 3e 33 3c 2f 74 64 3e 3c 74 64  right">3</td><td
20b0: 20 61 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e 33   align="right">3
20c0: 3c 2f 74 64 3e 3c 74 64 20 61 6c 69 67 6e 3d 22  </td><td align="
20d0: 72 69 67 68 74 22 3e 36 32 39 39 36 32 38 3c 2f  right">6299628</
20e0: 74 64 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69  td><td align="ri
20f0: 67 68 74 22 3e 38 32 33 38 37 2e 30 31 34 30 32  ght">82387.01402
2100: 38 3c 2f 74 64 3e 3c 2f 74 72 3e 0d 0a 3c 2f 74  8</td></tr>..</t
2110: 61 62 6c 65 3e 3c 62 72 3e 0d 0a 3c 75 6c 3e 0d  able><br>..<ul>.
2120: 0a 3c 6c 69 3e 61 20 56 69 72 74 75 61 6c 4b 4e  .<li>a VirtualKN
2130: 4e 20 71 75 65 72 79 20 63 6c 6f 73 65 6c 79 20  N query closely 
2140: 72 65 73 65 6d 62 6c 65 73 20 61 20 56 69 72 74  resembles a Virt
2150: 75 61 6c 53 70 61 74 69 61 6c 49 6e 64 65 78 20  ualSpatialIndex 
2160: 71 75 65 72 79 2e 0d 0a 54 68 69 73 20 73 68 6f  query...This sho
2170: 75 6c 64 20 6e 6f 74 20 73 75 72 70 72 69 73 65  uld not surprise
2180: 20 74 6f 6f 20 6d 75 63 68 2c 20 62 65 63 61 75   too much, becau
2190: 73 65 20 62 6f 74 68 20 74 68 65 6d 20 61 72 65  se both them are
21a0: 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 62 65 20   intended to be 
21b0: 77 72 61 70 70 65 64 20 61 72 6f 75 6e 64 20 61  wrapped around a
21c0: 6e 20 75 6e 64 65 72 6c 79 69 6e 67 20 3c 62 3e  n underlying <b>
21d0: 52 2a 54 72 65 65 20 53 70 61 74 69 61 6c 20 49  R*Tree Spatial I
21e0: 6e 64 65 78 3c 2f 62 3e 2e 3c 2f 6c 69 3e 20 0d  ndex</b>.</li> .
21f0: 0a 3c 6c 69 3e 41 6e 79 20 76 61 6c 69 64 20 56  .<li>Any valid V
2200: 69 72 74 75 61 6c 4b 4e 4e 20 71 75 65 72 79 20  irtualKNN query 
2210: 73 68 6f 75 6c 64 20 6e 65 63 65 73 73 61 72 69  should necessari
2220: 6c 79 20 68 61 76 65 20 61 20 66 6f 72 6d 20 6c  ly have a form l
2230: 69 6b 65 3a 0d 0a 3c 62 3e 57 48 45 52 45 20 3c  ike:..<b>WHERE <
2240: 69 3e 6b 6e 6e 2d 63 6f 6c 75 6d 6e 3c 2f 69 3e  i>knn-column</i>
2250: 20 3d 20 3c 69 3e 76 61 6c 75 65 3c 2f 69 3e 20   = <i>value</i> 
2260: 41 4e 44 20 3c 69 3e 6b 6e 6e 2d 63 6f 6c 75 6d  AND <i>knn-colum
2270: 6e 3c 2f 69 3e 20 3d 20 3c 69 3e 76 61 6c 75 65  n</i> = <i>value
2280: 3c 2f 69 3e 20 2e 2e 2e 3c 2f 62 3e 3c 2f 6c 69  </i> ...</b></li
2290: 3e 0d 0a 3c 6c 69 3e 74 68 65 20 69 6e 70 75 74  >..<li>the input
22a0: 20 63 6f 6c 75 6d 6e 73 20 28 74 68 65 20 6f 6e   columns (the on
22b0: 65 73 20 79 6f 75 20 63 61 6e 20 72 65 66 65 72  es you can refer
22c0: 65 6e 63 65 20 69 6e 74 6f 20 61 20 3c 62 3e 57  ence into a <b>W
22d0: 48 45 52 45 3c 2f 62 3e 20 63 6c 61 75 73 65 29  HERE</b> clause)
22e0: 20 61 72 65 3a 0d 0a 3c 75 6c 3e 0d 0a 3c 6c 69   are:..<ul>..<li
22f0: 3e 3c 62 3e 66 5f 74 61 62 6c 65 5f 6e 61 6d 65  ><b>f_table_name
2300: 3c 2f 62 3e 20 28 3c 69 3e 6d 61 6e 64 61 74 6f  </b> (<i>mandato
2310: 72 79 3c 2f 69 3e 29 3c 62 72 3e 0d 0a 6e 61 6d  ry</i>)<br>..nam
2320: 65 20 6f 66 20 74 68 65 20 47 65 6f 54 61 62 6c  e of the GeoTabl
2330: 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65  e containing the
2340: 20 47 65 6f 6d 65 74 72 69 65 73 20 74 6f 20 62   Geometries to b
2350: 65 20 73 65 61 72 63 68 65 64 2e 3c 2f 6c 69 3e  e searched.</li>
2360: 0d 0a 3c 6c 69 3e 3c 62 3e 66 5f 67 65 6f 6d 65  ..<li><b>f_geome
2370: 74 72 79 5f 63 6f 6c 75 6d 6e 3c 2f 62 3e 20 28  try_column</b> (
2380: 3c 69 3e 6f 70 74 69 6f 6e 61 6c 3c 2f 69 3e 29  <i>optional</i>)
2390: 3c 62 72 3e 0d 0a 6e 61 6d 65 20 6f 66 20 74 68  <br>..name of th
23a0: 65 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20  e column of the 
23b0: 61 62 6f 76 65 20 74 61 62 6c 65 20 63 6f 6e 74  above table cont
23c0: 61 69 6e 69 6e 67 20 74 68 65 20 47 65 6f 6d 65  aining the Geome
23d0: 74 72 69 65 73 20 74 6f 20 62 65 20 73 65 61 72  tries to be sear
23e0: 63 68 65 64 2e 0d 0a 3c 6f 6c 3e 0d 0a 3c 6c 69  ched...<ol>..<li
23f0: 3e 49 66 20 74 68 65 20 74 61 62 6c 65 20 69 64  >If the table id
2400: 65 6e 74 69 66 69 65 64 20 62 79 20 3c 62 3e 66  entified by <b>f
2410: 5f 74 61 62 6c 65 5f 6e 61 6d 65 3c 2f 62 3e 20  _table_name</b> 
2420: 6a 75 73 74 20 63 6f 6e 74 61 69 6e 73 20 61 20  just contains a 
2430: 73 69 6e 67 6c 65 20 47 65 6f 6d 65 74 72 79 20  single Geometry 
2440: 63 6f 6c 75 6d 6e 20 79 6f 75 20 63 61 6e 20 73  column you can s
2450: 61 66 65 6c 79 20 6f 6d 69 74 20 74 6f 20 73 70  afely omit to sp
2460: 65 63 69 66 79 20 74 68 65 20 3c 62 3e 66 5f 67  ecify the <b>f_g
2470: 65 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e 3c 2f  eometry_column</
2480: 62 3e 20 61 72 67 75 6d 65 6e 74 20 28 69 74 20  b> argument (it 
2490: 77 69 6c 6c 20 62 65 20 61 75 74 6f 6d 61 74 69  will be automati
24a0: 63 61 6c 6c 79 20 73 65 74 29 2e 3c 2f 6c 69 3e  cally set).</li>
24b0: 0d 0a 3c 6c 69 3e 49 66 20 69 6e 73 74 65 61 64  ..<li>If instead
24c0: 20 74 68 65 20 74 61 62 6c 65 20 69 64 65 6e 74   the table ident
24d0: 69 66 69 65 64 20 62 79 20 3c 62 3e 66 5f 74 61  ified by <b>f_ta
24e0: 62 6c 65 5f 6e 61 6d 65 3c 2f 62 3e 20 63 6f 6e  ble_name</b> con
24f0: 74 61 69 6e 73 20 74 77 6f 20 28 6f 72 20 6d 6f  tains two (or mo
2500: 72 65 29 20 47 65 6f 6d 65 74 72 79 20 63 6f 6c  re) Geometry col
2510: 75 6d 6e 73 20 65 78 70 6c 69 63 69 74 6c 79 20  umns explicitly 
2520: 73 70 65 63 69 66 79 69 6e 67 20 74 68 65 20 3c  specifying the <
2530: 62 3e 66 5f 67 65 6f 6d 65 74 72 79 5f 63 6f 6c  b>f_geometry_col
2540: 75 6d 6e 3c 2f 62 3e 20 61 72 67 75 6d 65 6e 74  umn</b> argument
2550: 20 69 73 20 73 74 72 69 63 74 6c 79 20 72 65 71   is strictly req
2560: 75 69 72 65 64 2c 20 73 6f 20 74 6f 20 67 65 74  uired, so to get
2570: 20 61 6e 20 75 6e 61 6d 62 69 67 75 6f 73 20 64   an unambiguos d
2580: 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20 74 68 65  efinition of the
2590: 20 69 6e 74 65 6e 64 65 64 20 73 65 61 72 63 68   intended search
25a0: 20 63 6f 6e 74 65 78 74 2e 3c 2f 6c 69 3e 0d 0a   context.</li>..
25b0: 3c 6c 69 3e 49 6e 20 61 6e 79 20 63 61 73 65 20  <li>In any case 
25c0: 3c 62 3e 66 5f 74 61 62 6c 65 5f 6e 61 6d 65 3c  <b>f_table_name<
25d0: 2f 62 3e 20 61 6e 64 20 3c 62 3e 66 5f 67 65 6f  /b> and <b>f_geo
25e0: 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e 3c 2f 62 3e  metry_column</b>
25f0: 20 6d 75 73 74 20 65 78 65 63 74 6c 79 20 6d 61   must exectly ma
2600: 74 63 68 20 61 20 70 72 6f 70 65 72 6c 79 20 64  tch a properly d
2610: 65 66 69 6e 65 64 20 47 65 6f 6d 65 74 72 79 20  efined Geometry 
2620: 63 6f 6c 75 6d 6e 20 73 75 70 70 6f 72 74 65 64  column supported
2630: 20 62 79 20 61 20 63 6f 72 72 65 73 70 6f 6e 64   by a correspond
2640: 69 6e 67 20 53 70 61 74 69 61 6c 20 49 6e 64 65  ing Spatial Inde
2650: 78 2e 3c 2f 6c 69 3e 0d 0a 3c 2f 6f 6c 3e 3c 2f  x.</li>..</ol></
2660: 6c 69 3e 0d 0a 3c 6c 69 3e 3c 62 3e 72 65 66 5f  li>..<li><b>ref_
2670: 67 65 6f 6d 65 74 72 79 3c 2f 62 3e 20 28 3c 69  geometry</b> (<i
2680: 3e 6d 61 6e 64 61 74 6f 72 79 3c 2f 69 3e 29 3c  >mandatory</i>)<
2690: 62 72 3e 0d 0a 61 6e 79 20 61 72 62 69 74 72 61  br>..any arbitra
26a0: 72 79 20 47 65 6f 6d 65 74 72 79 20 28 3c 62 3e  ry Geometry (<b>
26b0: 50 4f 49 4e 54 3c 2f 62 3e 2c 20 3c 62 3e 4c 49  POINT</b>, <b>LI
26c0: 4e 45 53 54 52 49 4e 47 3c 2f 62 3e 2c 20 3c 62  NESTRING</b>, <b
26d0: 3e 50 4f 4c 59 47 4f 4e 3c 2f 62 3e 20 6f 72 20  >POLYGON</b> or 
26e0: 77 68 61 74 65 76 65 72 20 65 6c 73 65 29 20 69  whatever else) i
26f0: 6e 74 65 6e 64 65 64 20 74 6f 20 72 65 70 72 65  ntended to repre
2700: 73 65 6e 74 20 74 68 65 20 6f 72 69 67 69 6e 20  sent the origin 
2710: 6f 66 20 74 68 65 20 4b 4e 4e 20 73 65 61 72 63  of the KNN searc
2720: 68 2e 3c 62 72 3e 0d 0a 53 68 6f 75 6c 64 20 6e  h.<br>..Should n
2730: 65 63 65 73 73 61 72 69 6c 79 20 62 65 20 69 6e  ecessarily be in
2740: 20 74 68 65 20 73 61 6d 65 20 53 52 49 44 20 6f   the same SRID o
2750: 66 20 74 68 65 20 74 61 72 67 65 74 20 47 65 6f  f the target Geo
2760: 6d 65 74 72 69 65 73 20 74 6f 20 62 65 20 73 65  metries to be se
2770: 61 72 63 68 65 64 2e 3c 2f 6c 69 3e 0d 0a 3c 6c  arched.</li>..<l
2780: 69 3e 3c 62 3e 6d 61 78 5f 69 74 65 6d 73 3c 2f  i><b>max_items</
2790: 62 3e 20 28 3c 69 3e 6f 70 74 69 6f 6e 61 6c 3c  b> (<i>optional<
27a0: 2f 69 3e 29 3c 62 72 3e 0d 0a 6d 61 78 69 6d 75  /i>)<br>..maximu
27b0: 6d 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73  m number of rows
27c0: 20 74 6f 20 62 65 20 72 65 74 75 72 6e 65 64 20   to be returned 
27d0: 69 6e 74 6f 20 74 68 65 20 72 65 73 75 6c 74 73  into the results
27e0: 65 74 2e 3c 62 72 3e 0d 0a 54 68 65 20 76 61 6c  et.<br>..The val
27f0: 69 64 20 72 61 6e 67 65 20 69 73 20 66 72 6f 6d  id range is from
2800: 20 3c 62 3e 31 3c 2f 62 3e 20 74 6f 20 3c 62 3e   <b>1</b> to <b>
2810: 31 30 32 34 3c 2f 62 3e 20 28 68 69 67 68 65 72  1024</b> (higher
2820: 20 76 61 6c 75 65 73 20 77 69 6c 6c 20 72 65 71   values will req
2830: 75 69 72 65 20 61 20 6c 6f 6e 67 65 72 20 74 69  uire a longer ti
2840: 6d 65 20 74 6f 20 62 65 20 70 72 6f 63 65 73 73  me to be process
2850: 65 64 29 2e 3c 62 72 3e 0d 0a 42 79 20 64 65 66  ed).<br>..By def
2860: 61 75 6c 74 20 6f 6e 6c 79 20 74 68 65 20 66 69  ault only the fi
2870: 72 73 74 20 3c 62 3e 33 3c 2f 62 3e 20 6e 65 61  rst <b>3</b> nea
2880: 72 65 73 74 20 47 65 6f 6d 65 74 72 69 65 73 20  rest Geometries 
2890: 77 69 6c 6c 20 62 65 20 69 64 65 6e 74 69 66 69  will be identifi
28a0: 65 64 2e 3c 2f 6c 69 3e 0d 0a 3c 2f 75 6c 3e 3c  ed.</li>..</ul><
28b0: 2f 6c 69 3e 0d 0a 3c 6c 69 3e 74 68 65 20 6f 75  /li>..<li>the ou
28c0: 74 70 75 74 20 63 6f 6c 75 6d 6e 73 20 28 74 68  tput columns (th
28d0: 65 20 6f 6e 65 73 20 63 6f 6e 74 61 69 6e 69 6e  e ones containin
28e0: 67 20 76 61 6c 75 65 73 20 72 65 74 72 69 65 76  g values retriev
28f0: 65 64 20 62 79 20 74 68 65 20 4b 4e 4e 20 73 65  ed by the KNN se
2900: 61 72 63 68 29 20 61 72 65 3a 0d 0a 3c 75 6c 3e  arch) are:..<ul>
2910: 0d 0a 3c 6c 69 3e 3c 62 3e 70 6f 73 3c 2f 62 3e  ..<li><b>pos</b>
2920: 20 28 3c 69 3e 49 4e 54 45 47 45 52 3c 2f 69 3e   (<i>INTEGER</i>
2930: 29 3c 62 72 3e 0d 0a 72 65 6c 61 74 69 76 65 20  )<br>..relative 
2940: 72 61 6e 6b 3a 20 74 68 65 20 63 6c 6f 73 65 73  rank: the closes
2950: 74 20 69 74 65 6d 20 77 69 6c 6c 20 62 65 20 3c  t item will be <
2960: 62 3e 23 31 3c 2f 62 3e 2c 20 74 68 65 20 73 65  b>#1</b>, the se
2970: 63 6f 6e 64 20 63 6c 6f 73 65 73 74 20 69 74 65  cond closest ite
2980: 6d 20 77 69 6c 6c 20 62 65 20 3c 62 3e 23 32 3c  m will be <b>#2<
2990: 2f 62 3e 20 61 6e 64 20 73 6f 20 6f 6e 2e 3c 2f  /b> and so on.</
29a0: 6c 69 3e 0d 0a 3c 6c 69 3e 3c 62 3e 66 69 64 3c  li>..<li><b>fid<
29b0: 2f 62 3e 20 28 3c 69 3e 49 4e 54 45 47 45 52 3c  /b> (<i>INTEGER<
29c0: 2f 69 3e 29 3c 62 72 3e 0d 0a 74 68 65 20 75 6e  /i>)<br>..the un
29d0: 69 71 75 65 20 3c 62 3e 52 4f 57 49 44 3c 2f 62  ique <b>ROWID</b
29e0: 3e 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 63  > value of the c
29f0: 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 66 65 61  orresponding fea
2a00: 74 75 72 65 20 69 6e 74 6f 20 74 68 65 20 73 65  ture into the se
2a10: 61 72 63 68 65 64 20 47 65 6f 54 61 62 6c 65 2e  arched GeoTable.
2a20: 3c 2f 6c 69 3e 0d 0a 3c 6c 69 3e 3c 62 3e 64 69  </li>..<li><b>di
2a30: 73 74 61 6e 63 65 3c 2f 62 3e 20 28 3c 69 3e 44  stance</b> (<i>D
2a40: 4f 55 42 4c 45 3c 2f 69 3e 29 3c 62 72 3e 0d 0a  OUBLE</i>)<br>..
2a50: 74 68 65 20 6d 69 6e 69 6d 75 6d 20 64 69 73 74  the minimum dist
2a60: 61 6e 63 65 20 69 6e 74 65 72 63 75 72 72 69 6e  ance intercurrin
2a70: 67 20 62 65 74 77 65 65 6e 20 74 68 65 20 63 6f  g between the co
2a80: 72 72 65 73 70 6f 6e 64 69 6e 67 20 66 65 61 74  rresponding feat
2a90: 75 72 65 20 69 6e 74 6f 20 74 68 65 20 73 65 61  ure into the sea
2aa0: 72 63 68 65 64 20 47 65 6f 54 61 62 6c 65 20 61  rched GeoTable a
2ab0: 6e 64 20 74 68 65 20 6f 72 69 67 69 6e 20 64 65  nd the origin de
2ac0: 66 69 6e 65 64 20 62 79 20 3c 62 3e 72 65 66 5f  fined by <b>ref_
2ad0: 67 65 6f 6d 65 74 72 79 3c 2f 62 3e 2e 0d 0a 3c  geometry</b>...<
2ae0: 6f 6c 3e 0d 0a 3c 6c 69 3e 54 68 65 20 75 6e 69  ol>..<li>The uni
2af0: 74 20 6d 65 61 73 75 72 65 20 66 6f 72 20 61 6c  t measure for al
2b00: 6c 20 64 69 73 74 61 6e 63 65 73 20 77 69 6c 6c  l distances will
2b10: 20 61 6c 77 61 79 73 20 62 65 20 74 68 65 20 6f   always be the o
2b20: 6e 65 20 64 65 63 6c 61 72 65 64 20 62 79 20 74  ne declared by t
2b30: 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  he corresponding
2b40: 20 53 52 49 44 20 64 65 66 69 6e 69 74 69 6f 6e   SRID definition
2b50: 2c 20 69 66 20 74 68 69 73 20 6f 6e 65 20 63 6f  , if this one co
2b60: 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 73 6f 6d  rresponds to som
2b70: 65 20 3c 62 3e 70 6c 61 6e 61 72 3c 2f 62 3e 20  e <b>planar</b> 
2b80: 28 61 6b 61 20 3c 62 3e 70 72 6f 6a 65 63 74 65  (aka <b>projecte
2b90: 64 3c 2f 62 3e 29 20 72 65 66 65 72 65 6e 63 65  d</b>) reference
2ba0: 20 73 79 73 74 65 6d 2e 3c 2f 6c 69 3e 0d 0a 3c   system.</li>..<
2bb0: 6c 69 3e 49 66 20 69 6e 73 74 65 61 64 20 74 68  li>If instead th
2bc0: 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  e corresponding 
2bd0: 53 52 49 44 20 64 65 66 69 6e 69 74 69 6f 6e 20  SRID definition 
2be0: 69 73 20 6f 66 20 74 68 65 20 3c 62 3e 67 65 6f  is of the <b>geo
2bf0: 67 72 61 70 68 69 63 3c 2f 62 3e 20 74 79 70 65  graphic</b> type
2c00: 20 28 3c 69 3e 6c 6f 6e 67 69 74 75 64 65 3c 2f   (<i>longitude</
2c10: 69 3e 20 61 6e 64 20 3c 69 3e 6c 61 74 69 74 75  i> and <i>latitu
2c20: 64 65 3c 2f 69 3e 20 61 6e 67 6c 65 73 29 20 61  de</i> angles) a
2c30: 6c 6c 20 64 69 73 74 61 6e 63 65 73 20 77 69 6c  ll distances wil
2c40: 6c 20 61 6c 77 61 79 73 20 62 65 20 6d 65 61 73  l always be meas
2c50: 75 72 65 64 20 69 6e 20 3c 62 3e 6d 65 74 72 65  ured in <b>metre
2c60: 73 3c 2f 62 3e 2c 20 61 6e 64 20 74 68 65 20 6d  s</b>, and the m
2c70: 6f 73 74 20 70 72 65 63 69 73 65 20 67 65 6f 64  ost precise geod
2c80: 65 74 69 63 20 66 6f 72 6d 75 6c 61 73 20 77 69  etic formulas wi
2c90: 6c 6c 20 62 65 20 61 75 74 6f 6d 61 74 69 63 61  ll be automatica
2ca0: 6c 6c 79 20 61 70 70 6c 69 65 64 2e 3c 2f 6c 69  lly applied.</li
2cb0: 3e 0d 0a 3c 2f 6f 6c 3e 3c 2f 6c 69 3e 0d 0a 3c  >..</ol></li>..<
2cc0: 2f 75 6c 3e 3c 2f 6c 69 3e 0d 0a 3c 2f 75 6c 3e  /ul></li>..</ul>
2cd0: 0d 0a 3c 68 72 3e 0d 0a 3c 68 33 3e 41 20 73 65  ..<hr>..<h3>A se
2ce0: 63 6f 6e 64 20 65 78 61 6d 70 6c 65 20 6f 66 20  cond example of 
2cf0: 61 20 6d 6f 72 65 20 73 6f 70 68 69 73 74 69 63  a more sophistic
2d00: 61 72 64 20 4b 4e 4e 20 71 75 65 72 79 3c 2f 68  ard KNN query</h
2d10: 33 3e 0d 0a 3c 76 65 72 62 61 74 69 6d 3e 0d 0a  3>..<verbatim>..
2d20: 53 45 4c 45 43 54 20 61 2e 70 6f 73 2c 20 62 2e  SELECT a.pos, b.
2d30: 6e 61 6d 65 2c 20 62 2e 63 6f 75 6e 74 72 79 2c  name, b.country,
2d40: 20 61 2e 64 69 73 74 61 6e 63 65 20 2f 20 31 30   a.distance / 10
2d50: 30 30 2e 30 20 41 53 20 64 69 73 74 5f 6b 6d 0d  00.0 AS dist_km.
2d60: 0a 46 52 4f 4d 20 6b 6e 6e 20 41 53 20 61 0d 0a  .FROM knn AS a..
2d70: 4a 4f 49 4e 20 61 69 72 70 6f 72 74 73 20 41 53  JOIN airports AS
2d80: 20 62 20 4f 4e 20 28 62 2e 67 65 6f 6e 61 6d 65   b ON (b.geoname
2d90: 69 64 20 3d 20 61 2e 66 69 64 29 0d 0a 57 48 45  id = a.fid)..WHE
2da0: 52 45 20 66 5f 74 61 62 6c 65 5f 6e 61 6d 65 20  RE f_table_name 
2db0: 3d 20 27 61 69 72 70 6f 72 74 73 27 20 41 4e 44  = 'airports' AND
2dc0: 20 72 65 66 5f 67 65 6f 6d 65 74 72 79 20 3d 20   ref_geometry = 
2dd0: 4d 61 6b 65 50 6f 69 6e 74 28 31 30 2c 20 34 33  MakePoint(10, 43
2de0: 29 20 41 4e 44 20 6d 61 78 5f 69 74 65 6d 73 20  ) AND max_items 
2df0: 3d 20 35 3b 0d 0a 3c 2f 76 65 72 62 61 74 69 6d  = 5;..</verbatim
2e00: 3e 0d 0a 3c 74 61 62 6c 65 20 62 67 63 6f 6c 6f  >..<table bgcolo
2e10: 72 3d 22 23 64 30 66 66 62 30 22 20 63 65 6c 6c  r="#d0ffb0" cell
2e20: 73 70 61 63 69 6e 67 3d 22 34 22 20 63 65 6c 6c  spacing="4" cell
2e30: 70 61 64 64 69 6e 67 3d 22 34 22 20 62 6f 72 64  padding="4" bord
2e40: 65 72 3d 22 31 22 3e 0d 0a 3c 74 72 3e 3c 74 64  er="1">..<tr><td
2e50: 20 62 67 63 6f 6c 6f 72 3d 22 23 63 30 64 64 61   bgcolor="#c0dda
2e60: 6f 22 3e 3c 62 3e 72 61 6e 6b 3c 2f 62 3e 3c 2f  o"><b>rank</b></
2e70: 74 64 3e 3c 74 64 20 62 67 63 6f 6c 6f 72 3d 22  td><td bgcolor="
2e80: 23 63 30 64 64 61 6f 22 3e 3c 62 3e 67 65 6f 6e  #c0ddao"><b>geon
2e90: 61 6d 65 69 64 3c 2f 62 3e 3c 2f 74 64 3e 3c 74  ameid</b></td><t
2ea0: 64 20 62 67 63 6f 6c 6f 72 3d 22 23 63 30 64 64  d bgcolor="#c0dd
2eb0: 61 6f 22 3e 3c 62 3e 6e 61 6d 65 3c 2f 62 3e 3c  ao"><b>name</b><
2ec0: 2f 74 64 3e 3c 74 64 20 62 67 63 6f 6c 6f 72 3d  /td><td bgcolor=
2ed0: 22 23 63 30 64 64 61 6f 22 3e 3c 62 3e 63 6f 75  "#c0ddao"><b>cou
2ee0: 6e 74 72 79 3c 2f 62 3e 3c 2f 74 64 3e 3c 74 64  ntry</b></td><td
2ef0: 20 62 67 63 6f 6c 6f 72 3d 22 23 63 30 64 64 61   bgcolor="#c0dda
2f00: 6f 22 3e 3c 62 3e 64 69 73 74 5f 6b 6d 3c 2f 62  o"><b>dist_km</b
2f10: 3e 3c 2f 74 64 3e 3c 2f 74 72 3e 0d 0a 3c 74 72  ></td></tr>..<tr
2f20: 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69 67 68  ><td align="righ
2f30: 74 22 3e 31 3c 2f 74 64 3e 3c 74 64 20 61 6c 69  t">1</td><td ali
2f40: 67 6e 3d 22 72 69 67 68 74 22 3e 36 32 39 39 36  gn="right">62996
2f50: 32 33 3c 2f 74 64 3e 3c 74 64 3e 4d 61 72 69 6e  23</td><td>Marin
2f60: 61 20 44 69 20 43 61 6d 70 6f 3c 2f 74 64 3e 3c  a Di Campo</td><
2f70: 74 64 3e 49 54 3c 2f 74 64 3e 3c 74 64 20 61 6c  td>IT</td><td al
2f80: 69 67 6e 3d 22 72 69 67 68 74 22 3e 33 33 2e 30  ign="right">33.0
2f90: 34 33 33 32 30 3c 2f 74 64 3e 3c 2f 74 72 3e 0d  43320</td></tr>.
2fa0: 0a 3c 74 72 3e 3c 74 64 20 61 6c 69 67 6e 3d 22  .<tr><td align="
2fb0: 72 69 67 68 74 22 3e 32 3c 2f 74 64 3e 3c 74 64  right">2</td><td
2fc0: 20 61 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e 36   align="right">6
2fd0: 32 39 39 33 39 32 3c 2f 74 64 3e 3c 74 64 3e 42  299392</td><td>B
2fe0: 61 73 74 69 61 2d 50 6f 72 65 74 74 61 3c 2f 74  astia-Poretta</t
2ff0: 64 3e 3c 74 64 3e 46 52 3c 2f 74 64 3e 3c 74 64  d><td>FR</td><td
3000: 20 61 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e 36   align="right">6
3010: 35 2e 32 32 36 35 37 33 3c 2f 74 64 3e 3c 2f 74  5.226573</td></t
3020: 72 3e 0d 0a 3c 74 72 3e 3c 74 64 20 61 6c 69 67  r>..<tr><td alig
3030: 6e 3d 22 72 69 67 68 74 22 3e 33 3c 2f 74 64 3e  n="right">3</td>
3040: 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69 67 68 74  <td align="right
3050: 22 3e 36 32 39 39 36 32 38 3c 2f 74 64 3e 3c 74  ">6299628</td><t
3060: 64 3e 50 69 73 61 20 2f 20 53 2e 20 47 69 75 73  d>Pisa / S. Gius
3070: 74 6f 3c 2f 74 64 3e 3c 74 64 3e 49 54 3c 2f 74  to</td><td>IT</t
3080: 64 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69 67  d><td align="rig
3090: 68 74 22 3e 38 32 2e 33 38 37 30 31 34 3c 2f 74  ht">82.387014</t
30a0: 64 3e 3c 2f 74 72 3e 0d 0a 3c 74 72 3e 3c 74 64  d></tr>..<tr><td
30b0: 20 61 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e 34   align="right">4
30c0: 3c 2f 74 64 3e 3c 74 64 20 61 6c 69 67 6e 3d 22  </td><td align="
30d0: 72 69 67 68 74 22 3e 36 32 39 39 36 33 30 3c 2f  right">6299630</
30e0: 74 64 3e 3c 74 64 3e 47 72 6f 73 73 65 74 6f 20  td><td>Grosseto 
30f0: 41 69 72 70 6f 72 74 3c 2f 74 64 3e 3c 74 64 3e  Airport</td><td>
3100: 49 54 3c 2f 74 64 3e 3c 74 64 20 61 6c 69 67 6e  IT</td><td align
3110: 3d 22 72 69 67 68 74 22 3e 39 31 2e 35 34 39 37  ="right">91.5497
3120: 37 33 3c 2f 74 64 3e 3c 2f 74 72 3e 0d 0a 3c 74  73</td></tr>..<t
3130: 72 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69 67  r><td align="rig
3140: 68 74 22 3e 35 3c 2f 74 64 3e 3c 74 64 20 61 6c  ht">5</td><td al
3150: 69 67 6e 3d 22 72 69 67 68 74 22 3e 36 36 39 34  ign="right">6694
3160: 34 39 35 3c 2f 74 64 3e 3c 74 64 3e 43 6f 72 74  495</td><td>Cort
3170: 65 3c 2f 74 64 3e 3c 74 64 3e 46 52 3c 2f 74 64  e</td><td>FR</td
3180: 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69 67 68  ><td align="righ
3190: 74 22 3e 31 30 32 2e 38 31 39 37 37 38 3c 2f 74  t">102.819778</t
31a0: 64 3e 3c 2f 74 72 3e 0d 0a 3c 2f 74 61 62 6c 65  d></tr>..</table
31b0: 3e 3c 62 72 3e 0d 0a 54 68 69 73 20 73 65 63 6f  ><br>..This seco
31c0: 6e 64 20 4b 4e 4e 20 71 75 65 72 79 20 77 69 6c  nd KNN query wil
31d0: 6c 20 72 65 74 75 72 6e 20 74 68 65 20 73 61 6d  l return the sam
31e0: 65 20 69 64 65 6e 74 69 63 61 6c 20 72 65 73 75  e identical resu
31f0: 6c 74 73 20 77 65 20 61 6c 72 65 61 64 79 20 67  lts we already g
3200: 6f 74 20 62 65 66 6f 72 65 20 62 79 20 75 73 69  ot before by usi
3210: 6e 67 20 74 68 65 20 73 69 6c 6c 79 20 6e 61 69  ng the silly nai
3220: 76 65 20 61 70 70 72 6f 61 63 68 2e 3c 62 72 3e  ve approach.<br>
3230: 0d 0a 54 68 65 20 73 74 72 69 6b 69 6e 67 20 64  ..The striking d
3240: 69 66 66 65 72 65 6e 63 65 20 69 73 20 74 68 61  ifference is tha
3250: 74 20 74 68 69 73 20 4b 4e 4e 20 71 75 65 72 79  t this KNN query
3260: 20 73 75 63 63 65 73 66 75 6c 6c 79 20 63 6f 6d   succesfully com
3270: 70 6c 65 74 65 64 20 69 6e 20 6a 75 73 74 20 61  pleted in just a
3280: 20 66 72 61 63 74 69 6f 6e 20 6f 66 20 61 20 73   fraction of a s
3290: 65 63 6f 6e 64 2c 20 77 69 68 6c 73 74 20 65 78  econd, wihlst ex
32a0: 65 63 79 74 69 6e 67 20 74 68 65 20 6e 61 69 76  ecyting the naiv
32b0: 65 20 71 75 65 72 69 65 73 20 63 6f 6d 70 6c 65  e queries comple
32c0: 78 69 76 65 6c 79 20 72 65 71 75 69 72 65 64 20  xively required 
32d0: 6d 61 6e 79 20 73 65 63 6f 6e 64 73 20 28 61 6e  many seconds (an
32e0: 64 20 70 6c 65 61 73 65 20 63 6f 6e 73 69 64 65  d please conside
32f0: 72 20 74 68 61 74 20 74 68 69 73 20 6f 6e 65 20  r that this one 
3300: 69 73 20 61 20 72 65 6c 61 74 69 76 65 6c 79 20  is a relatively 
3310: 73 6d 61 6c 6c 20 64 61 74 61 73 65 74 29 2e 3c  small dataset).<
3320: 62 72 3e 3c 62 72 3e 0d 0a 3c 62 3e 4c 65 73 73  br><br>..<b>Less
3330: 6f 6e 20 74 6f 20 6c 65 61 72 6e 3c 2f 62 3e 3a  on to learn</b>:
3340: 20 4b 4e 4e 20 71 75 65 72 69 65 73 20 61 72 65   KNN queries are
3350: 20 72 65 61 6c 6c 79 20 65 66 66 69 63 69 65 6e   really efficien
3360: 74 20 61 6e 64 20 66 61 73 74 2c 20 62 65 63 61  t and fast, beca
3370: 75 73 65 20 74 68 65 79 20 64 69 72 65 63 74 6c  use they directl
3380: 79 20 69 6e 74 65 72 61 63 74 20 77 69 74 68 20  y interact with 
3390: 74 68 65 20 6c 6f 77 65 72 6d 6f 73 74 20 6c 65  the lowermost le
33a0: 76 65 6c 73 20 6f 66 20 74 68 65 20 52 2a 54 72  vels of the R*Tr
33b0: 65 65 20 53 70 61 74 69 61 6c 20 49 6e 64 65 78  ee Spatial Index
33c0: 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e   implementation.
33d0: 3c 62 72 3e 3c 62 72 3e 0d 0a 3c 68 72 3e 0d 0a  <br><br>..<hr>..
33e0: 3c 68 33 3e 41 20 74 68 69 72 64 20 65 78 61 6d  <h3>A third exam
33f0: 70 6c 65 20 6f 66 20 4b 4e 4e 20 71 75 65 72 79  ple of KNN query
3400: 3c 2f 68 33 3e 0d 0a 3c 76 65 72 62 61 74 69 6d  </h3>..<verbatim
3410: 3e 0d 0a 53 45 4c 45 43 54 20 61 2e 70 6f 73 20  >..SELECT a.pos 
3420: 41 53 20 72 61 6e 6b 2c 20 62 2e 67 65 6f 6e 61  AS rank, b.geona
3430: 6d 65 69 64 2c 20 62 2e 6e 61 6d 65 2c 20 62 2e  meid, b.name, b.
3440: 63 6f 75 6e 74 72 79 2c 20 61 2e 64 69 73 74 61  country, a.dista
3450: 6e 63 65 20 2f 20 31 30 30 30 2e 30 20 41 53 20  nce / 1000.0 AS 
3460: 64 69 73 74 5f 6b 6d 0d 0a 46 52 4f 4d 20 6b 6e  dist_km..FROM kn
3470: 6e 20 41 53 20 61 0d 0a 4a 4f 49 4e 20 61 69 72  n AS a..JOIN air
3480: 70 6f 72 74 73 20 41 53 20 62 20 4f 4e 20 28 62  ports AS b ON (b
3490: 2e 67 65 6f 6e 61 6d 65 69 64 20 3d 20 61 2e 66  .geonameid = a.f
34a0: 69 64 29 0d 0a 57 48 45 52 45 20 66 5f 74 61 62  id)..WHERE f_tab
34b0: 6c 65 5f 6e 61 6d 65 20 3d 20 27 61 69 72 70 6f  le_name = 'airpo
34c0: 72 74 73 27 20 41 4e 44 20 72 65 66 5f 67 65 6f  rts' AND ref_geo
34d0: 6d 65 74 72 79 20 3d 20 4d 61 6b 65 50 6f 69 6e  metry = MakePoin
34e0: 74 28 2d 31 37 2e 33 2c 20 2d 34 34 29 20 41 4e  t(-17.3, -44) AN
34f0: 44 20 6d 61 78 5f 69 74 65 6d 73 20 3d 20 35 3b  D max_items = 5;
3500: 0d 0a 3c 2f 76 65 72 62 61 74 69 6d 3e 0d 0a 3c  ..</verbatim>..<
3510: 74 61 62 6c 65 20 62 67 63 6f 6c 6f 72 3d 22 23  table bgcolor="#
3520: 64 30 66 66 62 30 22 20 63 65 6c 6c 73 70 61 63  d0ffb0" cellspac
3530: 69 6e 67 3d 22 34 22 20 63 65 6c 6c 70 61 64 64  ing="4" cellpadd
3540: 69 6e 67 3d 22 34 22 20 62 6f 72 64 65 72 3d 22  ing="4" border="
3550: 31 22 3e 0d 0a 3c 74 72 3e 3c 74 64 20 62 67 63  1">..<tr><td bgc
3560: 6f 6c 6f 72 3d 22 23 63 30 64 64 61 6f 22 3e 3c  olor="#c0ddao"><
3570: 62 3e 72 61 6e 6b 3c 2f 62 3e 3c 2f 74 64 3e 3c  b>rank</b></td><
3580: 74 64 20 62 67 63 6f 6c 6f 72 3d 22 23 63 30 64  td bgcolor="#c0d
3590: 64 61 6f 22 3e 3c 62 3e 67 65 6f 6e 61 6d 65 69  dao"><b>geonamei
35a0: 64 3c 2f 62 3e 3c 2f 74 64 3e 3c 74 64 20 62 67  d</b></td><td bg
35b0: 63 6f 6c 6f 72 3d 22 23 63 30 64 64 61 6f 22 3e  color="#c0ddao">
35c0: 3c 62 3e 6e 61 6d 65 3c 2f 62 3e 3c 2f 74 64 3e  <b>name</b></td>
35d0: 3c 74 64 20 62 67 63 6f 6c 6f 72 3d 22 23 63 30  <td bgcolor="#c0
35e0: 64 64 61 6f 22 3e 3c 62 3e 63 6f 75 6e 74 72 79  ddao"><b>country
35f0: 3c 2f 62 3e 3c 2f 74 64 3e 3c 74 64 20 62 67 63  </b></td><td bgc
3600: 6f 6c 6f 72 3d 22 23 63 30 64 64 61 6f 22 3e 3c  olor="#c0ddao"><
3610: 62 3e 64 69 73 74 5f 6b 6d 3c 2f 62 3e 3c 2f 74  b>dist_km</b></t
3620: 64 3e 3c 2f 74 72 3e 0d 0a 3c 74 72 3e 3c 74 64  d></tr>..<tr><td
3630: 20 61 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e 31   align="right">1
3640: 3c 2f 74 64 3e 3c 74 64 20 61 6c 69 67 6e 3d 22  </td><td align="
3650: 72 69 67 68 74 22 3e 37 36 36 38 34 33 33 3c 2f  right">7668433</
3660: 74 64 3e 3c 74 64 3e 43 61 62 6f 20 46 72 69 6f  td><td>Cabo Frio
3670: 20 41 69 72 70 6f 72 74 3c 2f 74 64 3e 3c 74 64   Airport</td><td
3680: 3e 42 52 3c 2f 74 64 3e 3c 74 64 20 61 6c 69 67  >BR</td><td alig
3690: 6e 3d 22 72 69 67 68 74 22 3e 33 32 35 36 2e 35  n="right">3256.5
36a0: 38 39 39 36 34 3c 2f 74 64 3e 3c 2f 74 72 3e 0d  89964</td></tr>.
36b0: 0a 3c 74 72 3e 3c 74 64 20 61 6c 69 67 6e 3d 22  .<tr><td align="
36c0: 72 69 67 68 74 22 3e 32 3c 2f 74 64 3e 3c 74 64  right">2</td><td
36d0: 20 61 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e 33   align="right">3
36e0: 33 35 39 30 30 31 3c 2f 74 64 3e 3c 74 64 3e 59  359001</td><td>Y
36f0: 6f 75 6e 67 73 66 69 65 6c 64 3c 2f 74 64 3e 3c  oungsfield</td><
3700: 74 64 3e 5a 41 3c 2f 74 64 3e 3c 74 64 20 61 6c  td>ZA</td><td al
3710: 69 67 6e 3d 22 72 69 67 68 74 22 3e 33 32 36 32  ign="right">3262
3720: 2e 32 37 31 30 32 37 3c 2f 74 64 3e 3c 2f 74 72  .271027</td></tr
3730: 3e 0d 0a 3c 74 72 3e 3c 74 64 20 61 6c 69 67 6e  >..<tr><td align
3740: 3d 22 72 69 67 68 74 22 3e 33 3c 2f 74 64 3e 3c  ="right">3</td><
3750: 74 64 20 61 6c 69 67 6e 3d 22 72 69 67 68 74 22  td align="right"
3760: 3e 37 37 33 30 31 35 33 3c 2f 74 64 3e 3c 74 64  >7730153</td><td
3770: 3e 55 6d 62 65 72 74 6f 20 4d 6f 64 69 61 6e 6f  >Umberto Modiano
3780: 20 41 69 72 70 6f 72 74 3c 2f 74 64 3e 3c 74 64   Airport</td><td
3790: 3e 42 52 3c 2f 74 64 3e 3c 74 64 20 61 6c 69 67  >BR</td><td alig
37a0: 6e 3d 22 72 69 67 68 74 22 3e 33 32 36 32 2e 36  n="right">3262.6
37b0: 39 33 30 33 32 3c 2f 74 64 3e 3c 2f 74 72 3e 0d  93032</td></tr>.
37c0: 0a 3c 74 72 3e 3c 74 64 20 61 6c 69 67 6e 3d 22  .<tr><td align="
37d0: 72 69 67 68 74 22 3e 34 3c 2f 74 64 3e 3c 74 64  right">4</td><td
37e0: 20 61 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e 33   align="right">3
37f0: 33 36 32 33 35 35 3c 2f 74 64 3e 3c 74 64 3e 52  362355</td><td>R
3800: 6f 62 62 65 6e 20 49 73 6c 61 6e 64 3c 2f 74 64  obben Island</td
3810: 3e 3c 74 64 3e 5a 41 3c 2f 74 64 3e 3c 74 64 20  ><td>ZA</td><td 
3820: 61 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e 33 32  align="right">32
3830: 36 35 2e 33 30 32 35 36 38 3c 2f 74 64 3e 3c 2f  65.302568</td></
3840: 74 72 3e 0d 0a 3c 74 72 3e 3c 74 64 20 61 6c 69  tr>..<tr><td ali
3850: 67 6e 3d 22 72 69 67 68 74 22 3e 35 3c 2f 74 64  gn="right">5</td
3860: 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69 67 68  ><td align="righ
3870: 74 22 3e 36 33 30 30 36 32 32 3c 2f 74 64 3e 3c  t">6300622</td><
3880: 74 64 3e 53 2e 20 50 2e 20 41 6c 64 65 69 61 20  td>S. P. Aldeia 
3890: 41 65 72 6f 64 72 6f 6d 65 3c 2f 74 64 3e 3c 74  Aerodrome</td><t
38a0: 64 3e 5a 41 3c 2f 74 64 3e 3c 74 64 20 61 6c 69  d>ZA</td><td ali
38b0: 67 6e 3d 22 72 69 67 68 74 22 3e 33 32 36 37 2e  gn="right">3267.
38c0: 32 33 34 31 34 32 3c 2f 74 64 3e 3c 2f 74 72 3e  234142</td></tr>
38d0: 0d 0a 3c 2f 74 61 62 6c 65 3e 3c 62 72 3e 0d 0a  ..</table><br>..
38e0: 54 68 69 73 20 6c 61 73 74 20 4b 4e 4e 20 71 75  This last KNN qu
38f0: 65 72 79 20 69 73 20 76 65 72 79 20 73 69 6d 69  ery is very simi
3900: 6c 61 72 20 74 6f 20 74 68 65 20 70 72 65 76 69  lar to the previ
3910: 6f 75 73 20 6f 6e 65 2e 3c 62 72 3e 0d 0a 54 68  ous one.<br>..Th
3920: 69 73 20 74 69 6d 65 20 77 65 27 76 65 20 70 6c  is time we've pl
3930: 61 63 65 64 20 74 68 65 20 6f 72 69 67 69 6e 20  aced the origin 
3940: 6c 6f 63 61 74 69 6f 6e 20 73 6f 6d 65 77 68 65  location somewhe
3950: 72 65 20 69 6e 74 6f 20 74 68 65 20 53 6f 75 74  re into the Sout
3960: 68 20 41 74 6c 61 6e 74 69 63 20 62 6c 75 65 20  h Atlantic blue 
3970: 64 65 65 70 20 77 61 74 65 72 73 20 28 6d 6f 72  deep waters (mor
3980: 65 20 6f 72 20 6c 65 73 73 20 6d 69 64 77 61 79  e or less midway
3990: 20 66 72 6f 6d 20 62 6f 74 68 20 42 72 61 7a 69   from both Brazi
39a0: 6c 20 61 6e 64 20 53 6f 75 74 68 20 41 66 72 69  l and South Afri
39b0: 63 61 29 20 61 6e 64 20 63 6f 6e 73 65 71 75 6e  ca) and consequn
39c0: 74 6c 79 20 74 68 65 20 6e 65 61 72 65 73 74 20  tly the nearest 
39d0: 61 69 72 70 6f 72 74 73 20 61 72 65 20 6e 6f 74  airports are not
39e0: 20 72 65 61 6c 6c 79 20 73 6f 20 6e 65 61 72 2c   really so near,
39f0: 20 62 65 63 61 75 73 65 20 74 68 65 79 20 61 72   because they ar
3a00: 65 20 6c 6f 63 61 74 65 64 20 6d 61 6e 79 20 74  e located many t
3a10: 68 6f 75 73 61 6e 64 73 20 4b 6d 20 61 77 61 79  housands Km away
3a20: 2e 0d 0a 41 73 20 79 6f 75 20 63 61 6e 20 65 61  ...As you can ea
3a30: 73 69 6c 79 20 63 68 65 63 6b 20 61 20 4b 4e 4e  sily check a KNN
3a40: 20 71 75 65 72 79 20 77 69 6c 6c 20 62 72 69 6c   query will bril
3a50: 6c 69 61 6e 74 6c 79 20 70 65 72 66 6f 72 6d 20  liantly perform 
3a60: 65 76 65 6e 20 75 6e 64 65 72 20 73 75 63 68 20  even under such 
3a70: 75 6e 75 73 75 61 6c 20 63 6f 6e 64 69 74 69 6f  unusual conditio
3a80: 6e 73 2e 3c 62 72 3e 3c 62 72 3e 0d 0a 3c 62 3e  ns.<br><br>..<b>
3a90: 4c 65 73 73 6f 6e 20 74 6f 20 6c 65 61 72 6e 3c  Lesson to learn<
3aa0: 2f 62 3e 3a 20 4b 4e 4e 20 71 75 65 72 69 65 73  /b>: KNN queries
3ab0: 20 6e 65 76 65 72 20 61 73 73 75 6d 65 20 61 6e   never assume an
3ac0: 79 20 70 72 65 64 65 66 69 6e 65 64 20 64 69 73  y predefined dis
3ad0: 74 72 69 62 75 74 69 6f 6e 20 6f 66 20 74 68 65  tribution of the
3ae0: 20 73 65 61 72 63 68 65 64 20 69 74 65 6d 73 2c   searched items,
3af0: 20 61 6e 64 20 63 61 6e 20 61 75 74 6f 6d 61 74   and can automat
3b00: 69 63 61 6c 6c 79 20 61 64 61 70 74 20 69 6e 20  ically adapt in 
3b10: 61 20 76 65 72 79 20 65 66 66 69 63 69 65 6e 74  a very efficient
3b20: 20 77 61 79 20 74 6f 20 74 68 65 20 6d 6f 73 74   way to the most
3b30: 20 69 72 72 65 67 75 6c 61 72 20 73 61 6d 70 6c   irregular sampl
3b40: 65 20 64 69 73 74 72 69 62 75 74 69 6f 6e 73 2e  e distributions.
3b50: 0d 0a 0d 0a 3c 74 61 62 6c 65 20 77 69 64 74 68  ....<table width
3b60: 3d 22 31 30 30 25 22 3e 3c 74 72 3e 0d 0a 3c 74  ="100%"><tr>..<t
3b70: 64 20 77 69 64 74 68 3d 22 33 33 25 22 20 61 6c  d width="33%" al
3b80: 69 67 6e 3d 22 6c 65 66 74 22 3e 3c 2f 74 64 3e  ign="left"></td>
3b90: 0d 0a 3c 74 64 20 61 6c 69 67 6e 3d 22 63 65 6e  ..<td align="cen
3ba0: 74 65 72 22 3e 3c 61 20 68 72 65 66 3d 22 68 74  ter"><a href="ht
3bb0: 74 70 73 3a 2f 2f 77 77 77 2e 67 61 69 61 2d 67  tps://www.gaia-g
3bc0: 69 73 2e 69 74 2f 66 6f 73 73 69 6c 2f 6c 69 62  is.it/fossil/lib
3bd0: 73 70 61 74 69 61 6c 69 74 65 2f 77 69 6b 69 3f  spatialite/wiki?
3be0: 6e 61 6d 65 3d 6d 69 73 63 2d 64 6f 63 73 22 3e  name=misc-docs">
3bf0: 62 61 63 6b 20 74 6f 20 69 6e 64 65 78 3c 2f 61  back to index</a
3c00: 3e 3c 2f 74 64 3e 0d 0a 3c 74 64 20 77 69 64 74  ></td>..<td widt
3c10: 68 3d 22 33 33 25 22 20 61 6c 69 67 6e 3d 22 72  h="33%" align="r
3c20: 69 67 68 74 22 3e 3c 2f 74 64 3e 0d 0a 3c 2f 74  ight"></td>..</t
3c30: 72 3e 3c 2f 74 61 62 6c 65 3e 0a 5a 20 38 33 65  r></table>.Z 83e
3c40: 36 31 32 63 38 62 64 64 31 37 33 65 38 66 66 39  612c8bdd173e8ff9
3c50: 65 64 31 62 66 37 65 38 34 30 31 38 39 0a        ed1bf7e840189.