Hex Artifact Content
Not logged in

Artifact 806fd4da03659c33c3e154fa0d17cf6cf6a9056f:

Wiki page [KNN] by sandro 2015-12-11 12:38:29.
0000: 44 20 32 30 31 35 2d 31 32 2d 31 31 54 31 32 3a  D 2015-12-11T12:
0010: 33 38 3a 32 39 2e 35 38 32 0a 4c 20 4b 4e 4e 0a  38:29.582.L KNN.
0020: 50 20 62 63 65 31 66 37 65 31 64 37 38 66 36 30  P bce1f7e1d78f60
0030: 63 37 64 62 62 64 39 30 62 66 31 32 31 36 32 61  c7dbbd90bf12162a
0040: 66 37 30 30 39 64 33 61 62 36 0a 55 20 73 61 6e  f7009d3ab6.U san
0050: 64 72 6f 0a 57 20 31 39 33 31 38 0a 3c 74 61 62  dro.W 19318.<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 66 6f 72 20 77  e that for for w
0510: 68 61 74 65 76 65 72 20 72 65 61 73 6f 6e 20 79  hatever reason y
0520: 6f 75 20 61 72 65 20 69 6e 74 65 72 65 73 74 65  ou are intereste
0530: 64 20 69 6e 20 71 75 69 63 6b 6c 79 20 69 64 65  d in quickly ide
0540: 6e 74 69 66 79 69 6e 67 20 61 6c 6c 20 66 65 61  ntifying all fea
0550: 74 75 72 65 73 20 77 69 74 68 69 6e 20 61 20 20  tures within a  
0560: 63 6c 6f 73 65 20 73 70 61 74 69 61 6c 20 70 72  close spatial pr
0570: 6f 78 69 6d 69 74 79 20 74 6f 20 61 6e 20 61 72  oximity to an ar
0580: 62 69 74 72 61 72 79 20 6c 6f 63 61 74 69 6f 6e  bitrary location
0590: 2e 3c 62 72 3e 0d 0a 54 68 69 73 20 69 73 20 74  .<br>..This is t
05a0: 68 65 20 74 79 70 69 63 61 6c 20 3c 62 3e 4b 4e  he typical <b>KN
05b0: 4e 3c 2f 62 3e 20 70 72 6f 62 6c 65 6d 2e 3c 2f  N</b> problem.</
05c0: 74 64 3e 3c 2f 74 72 3e 0d 0a 3c 74 72 3e 3c 74  td></tr>..<tr><t
05d0: 64 3e 4a 75 73 74 20 66 65 77 20 70 72 61 63 74  d>Just few pract
05e0: 69 63 61 6c 20 72 65 61 6c 20 77 6f 72 6c 64 20  ical real world 
05f0: 65 78 61 6d 70 6c 65 73 3a 0d 0a 3c 75 6c 3e 0d  examples:..<ul>.
0600: 0a 3c 6c 69 3e 53 75 70 70 6f 73 65 20 77 68 69  .<li>Suppose whi
0610: 6c 65 20 75 73 69 6e 67 20 61 20 73 6d 61 72 74  le using a smart
0620: 70 68 6f 6e 65 20 61 70 70 3a 20 79 6f 75 72 20  phone app: your 
0630: 63 75 72 72 65 6e 74 20 67 65 6f 67 72 61 70 68  current geograph
0640: 69 63 20 6c 6f 63 61 74 69 6f 6e 20 69 73 20 77  ic location is w
0650: 65 6c 6c 20 6b 6e 6f 77 6e 20 62 79 20 74 68 65  ell known by the
0660: 20 69 6e 74 65 67 72 61 74 65 64 20 47 50 53 20   integrated GPS 
0670: 73 65 6e 73 6f 72 2c 20 73 6f 20 79 6f 75 20 61  sensor, so you a
0680: 72 65 20 73 69 6d 70 6c 79 20 65 78 70 65 63 74  re simply expect
0690: 69 6e 67 20 74 68 61 74 20 74 68 65 20 61 70 70  ing that the app
06a0: 20 77 69 6c 6c 20 69 6d 6d 65 64 69 61 74 65 6c   will immediatel
06b0: 79 20 74 65 6c 6c 20 77 68 65 72 65 20 79 6f 75  y tell where you
06c0: 20 61 72 65 20 28 61 6e 64 20 74 68 65 20 65 78   are (and the ex
06d0: 61 63 74 20 6c 6f 63 61 74 69 6f 6e 20 6f 66 29  act location of)
06e0: 20 74 68 65 20 6e 65 61 72 65 73 74 20 66 75 65   the nearest fue
06f0: 6c 20 73 74 61 74 69 6f 6e 73 20 28 68 6f 74 65  l stations (hote
0700: 6c 73 2c 20 72 65 73 74 61 75 72 61 6e 74 73 20  ls, restaurants 
0710: 65 74 63 29 2e 3c 2f 6c 69 3e 0d 0a 3c 6c 69 3e  etc).</li>..<li>
0720: 53 75 70 70 6f 73 65 20 61 20 7a 6f 6f 6c 6f 67  Suppose a zoolog
0730: 69 63 61 6c 2f 65 63 6f 6c 6f 67 69 63 61 6c 20  ical/ecological 
0740: 73 74 75 64 79 3a 20 79 6f 75 20 6b 6e 6f 77 20  study: you know 
0750: 77 68 65 72 65 20 61 72 65 20 6c 6f 63 61 74 65  where are locate
0760: 64 20 74 68 65 20 6e 65 73 74 69 6e 67 20 61 72  d the nesting ar
0770: 65 61 73 20 6f 66 20 73 6f 6d 65 20 72 61 72 65  eas of some rare
0780: 20 77 61 74 65 72 20 62 69 72 64 20 73 70 65 63   water bird spec
0790: 69 65 73 2c 20 61 6c 73 6f 20 74 68 65 20 6c 6f  ies, also the lo
07a0: 63 61 74 69 6f 6e 20 6f 66 20 61 6c 6c 20 72 69  cation of all ri
07b0: 76 65 72 73 2c 20 6c 61 6b 65 73 2c 20 70 6f 6e  vers, lakes, pon
07c0: 64 73 20 61 6e 64 20 6d 61 72 73 68 6c 61 6e 64  ds and marshland
07d0: 73 20 69 6e 20 74 68 65 20 61 72 65 61 20 79 6f  s in the area yo
07e0: 75 20 61 72 65 20 69 6e 74 65 72 65 73 74 65 64  u are interested
07f0: 20 69 6e 2e 3c 62 72 3e 0d 0a 4e 6f 77 20 79 6f   in.<br>..Now yo
0800: 75 20 61 72 65 20 61 74 74 65 6d 70 74 69 6e 67  u are attempting
0810: 20 74 6f 20 67 65 74 20 61 20 72 65 61 73 6f 6e   to get a reason
0820: 61 62 6c 65 20 61 73 73 6f 63 69 61 74 69 6f 6e  able association
0830: 20 62 65 74 77 65 65 6e 20 6e 65 73 74 69 6e 67   between nesting
0840: 20 61 72 65 61 73 20 61 6e 64 20 66 65 65 64 69   areas and feedi
0850: 6e 67 20 7a 6f 6e 65 73 20 62 61 73 65 64 20 6f  ng zones based o
0860: 6e 20 6d 69 6e 69 6d 61 6c 20 64 69 73 74 61 6e  n minimal distan
0870: 63 65 20 63 72 69 74 65 72 69 61 2e 3c 2f 6c 69  ce criteria.</li
0880: 3e 0d 0a 3c 2f 75 6c 3e 0d 0a 3c 2f 74 64 3e 3c  >..</ul>..</td><
0890: 2f 74 72 3e 0d 0a 3c 74 72 3e 3c 74 64 3e 57 68  /tr>..<tr><td>Wh
08a0: 65 72 65 62 79 2c 20 69 6e 20 62 6f 74 68 20 63  ereby, in both c
08b0: 61 73 65 73 20 79 6f 75 20 63 61 6e 27 74 20 73  ases you can't s
08c0: 61 66 65 6c 79 20 69 64 65 6e 74 69 66 79 20 61  afely identify a
08d0: 20 72 65 61 73 6f 6e 61 62 6c 65 20 3c 69 3e 3c   reasonable <i><
08e0: 75 3e 6d 69 6e 69 6d 61 6c 20 72 61 64 69 75 73  u>minimal radius
08f0: 3c 2f 75 3e 3c 2f 69 3e 20 73 6f 20 74 6f 20 75  </u></i> so to u
0900: 73 65 66 75 6c 6c 79 20 72 65 73 74 72 69 63 74  sefully restrict
0910: 20 79 6f 75 72 20 73 65 61 72 63 68 20 62 79 2c   your search by,
0920: 20 73 69 6e 63 65 20 79 6f 75 20 63 61 6e 27 74   since you can't
0930: 20 6d 61 6b 65 20 61 6e 79 20 76 61 6c 69 64 20   make any valid 
0940: 61 73 73 75 6d 70 74 69 6f 6e 20 61 62 6f 75 74  assumption about
0950: 20 74 68 65 20 67 65 6f 67 72 61 70 68 69 63 20   the geographic 
0960: 64 69 73 70 65 72 73 69 6f 6e 20 6f 66 20 79 6f  dispersion of yo
0970: 75 72 20 74 61 72 67 65 74 73 2e 3c 62 72 3e 0d  ur targets.<br>.
0980: 0a 54 68 65 20 64 69 73 74 61 6e 63 65 73 20 6d  .The distances m
0990: 61 79 20 6e 6f 74 69 63 65 61 62 6c 79 20 76 61  ay noticeably va
09a0: 72 79 20 66 72 6f 6d 20 63 61 73 65 20 74 6f 20  ry from case to 
09b0: 63 61 73 65 2c 20 61 6e 64 20 79 6f 75 20 68 61  case, and you ha
09c0: 76 65 6e 27 74 20 61 6e 79 20 69 64 65 61 20 61  ven't any idea a
09d0: 62 6f 75 74 20 74 68 69 73 2c 20 73 69 6e 63 65  bout this, since
09e0: 20 69 74 20 73 74 72 69 63 74 6c 79 20 64 65 70   it strictly dep
09f0: 65 6e 64 73 20 6f 6e 20 74 68 65 20 61 63 74 75  ends on the actu
0a00: 61 6c 20 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 74  al location of t
0a10: 68 65 20 73 65 61 72 63 68 20 6f 72 69 67 69 6e  he search origin
0a20: 20 61 6e 64 20 6f 6e 20 74 68 65 20 73 74 61 74   and on the stat
0a30: 69 73 74 69 63 61 6c 20 64 69 73 70 65 72 73 69  istical dispersi
0a40: 6f 6e 20 6f 66 20 74 68 65 20 74 61 72 67 65 74  on of the target
0a50: 73 20 74 6f 20 62 65 20 73 65 61 72 63 68 65 64  s to be searched
0a60: 2e 3c 2f 74 64 3e 3c 2f 74 72 3e 0d 0a 3c 2f 74  .</td></tr>..</t
0a70: 61 62 6c 65 3e 3c 62 72 3e 0d 0a 3c 68 72 3e 3c  able><br>..<hr><
0a80: 62 72 3e 0d 0a 3c 74 61 62 6c 65 20 77 69 64 74  br>..<table widt
0a90: 68 3d 22 31 30 30 25 22 3e 0d 0a 3c 74 72 3e 3c  h="100%">..<tr><
0aa0: 74 64 20 76 61 6c 69 67 6e 3d 22 6d 69 64 64 6c  td valign="middl
0ab0: 65 22 3e 0d 0a 59 6f 75 20 63 61 6e 20 3c 61 20  e">..You can <a 
0ac0: 68 72 65 66 3d 22 68 74 74 70 73 3a 2f 2f 77 77  href="https://ww
0ad0: 77 2e 67 61 69 61 2d 67 69 73 2e 69 74 2f 67 61  w.gaia-gis.it/ga
0ae0: 69 61 2d 73 69 6e 73 2f 6b 6e 6e 2f 61 69 72 70  ia-sins/knn/airp
0af0: 6f 72 74 73 2e 37 7a 22 3e 64 6f 77 6e 6c 6f 61  orts.7z">downloa
0b00: 64 3c 2f 61 3e 20 61 20 73 61 6d 70 6c 65 20 64  d</a> a sample d
0b10: 62 2d 66 69 6c 65 20 65 78 61 63 74 6c 79 20 63  b-file exactly c
0b20: 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20  orresponding to 
0b30: 74 68 65 20 70 72 61 63 74 69 63 61 6c 20 65 78  the practical ex
0b40: 61 6d 70 6c 65 73 20 77 65 27 6c 6c 20 75 73 65  amples we'll use
0b50: 20 69 6e 20 74 68 69 73 20 66 69 72 73 74 20 74   in this first t
0b60: 75 74 6f 72 69 61 6c 2e 3c 62 72 3e 3c 62 72 3e  utorial.<br><br>
0b70: 0d 0a 54 68 69 73 20 64 61 74 61 73 65 74 20 63  ..This dataset c
0b80: 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 61 20  orresponds to a 
0b90: 63 6f 6c 6c 65 63 74 69 6f 6e 20 6f 66 20 61 62  collection of ab
0ba0: 6f 75 74 20 3c 62 3e 33 30 2e 30 30 30 20 61 69  out <b>30.000 ai
0bb0: 72 70 6f 72 74 73 3c 2f 62 3e 20 73 63 61 74 74  rports</b> scatt
0bc0: 65 72 65 64 20 61 6c 6c 20 6f 76 65 72 20 74 68  ered all over th
0bd0: 65 20 77 6f 72 6c 64 3b 20 74 68 65 20 6f 72 69  e world; the ori
0be0: 67 69 6e 61 6c 20 69 6e 70 75 74 20 73 68 61 70  ginal input shap
0bf0: 65 66 69 6c 65 20 77 61 73 20 64 6f 77 6e 6c 6f  efile was downlo
0c00: 61 64 65 64 20 66 72 6f 6d 20 3c 61 20 68 72 65  aded from <a hre
0c10: 66 3d 22 68 74 74 70 73 3a 2f 2f 77 77 77 2e 73  f="https://www.s
0c20: 68 61 72 65 67 65 6f 2e 61 63 2e 75 6b 2f 68 61  haregeo.ac.uk/ha
0c30: 6e 64 6c 65 2f 31 30 36 37 32 2f 33 30 33 22 3e  ndle/10672/303">
0c40: 68 65 72 65 3c 2f 61 3e 3c 62 72 3e 0d 0a 54 68  here</a><br>..Th
0c50: 65 20 72 65 66 65 72 65 6e 63 65 20 73 79 73 74  e reference syst
0c60: 65 6d 20 69 73 20 3c 62 3e 34 33 32 36 20 57 47  em is <b>4326 WG
0c70: 53 38 34 3c 2f 62 3e 20 28 62 61 73 65 64 20 6f  S84</b> (based o
0c80: 6e 20 3c 69 3e 6c 6f 6e 67 69 74 75 64 65 3c 2f  n <i>longitude</
0c90: 69 3e 20 61 6e 64 20 3c 69 3e 6c 61 74 69 74 75  i> and <i>latitu
0ca0: 64 65 3c 2f 69 3e 20 61 6e 67 6c 65 73 29 2e 0d  de</i> angles)..
0cb0: 0a 3c 62 72 3e 3c 62 72 3e 3c 62 72 3e 3c 62 72  .<br><br><br><br
0cc0: 3e 0d 0a 54 68 65 20 6d 61 70 20 6f 6e 20 74 68  >..The map on th
0cd0: 65 20 72 69 67 68 74 20 73 68 6f 77 73 20 74 68  e right shows th
0ce0: 65 20 67 65 6f 67 72 61 70 68 69 63 20 63 6f 6e  e geographic con
0cf0: 74 65 78 74 20 6f 66 20 74 68 69 73 20 66 69 72  text of this fir
0d00: 73 74 20 4b 4e 4e 20 65 78 61 6d 70 6c 65 2e 3c  st KNN example.<
0d10: 2f 74 64 3e 0d 0a 3c 74 64 3e 3c 69 6d 67 20 73  /td>..<td><img s
0d20: 72 63 3d 22 68 74 74 70 73 3a 2f 2f 77 77 77 2e  rc="https://www.
0d30: 67 61 69 61 2d 67 69 73 2e 69 74 2f 67 61 69 61  gaia-gis.it/gaia
0d40: 2d 73 69 6e 73 2f 6b 6e 6e 2f 74 79 72 72 68 65  -sins/knn/tyrrhe
0d50: 6e 69 61 6e 2d 73 65 61 2e 70 6e 67 22 20 61 6c  nian-sea.png" al
0d60: 74 3d 22 54 79 72 72 68 65 6e 69 61 6e 20 53 65  t="Tyrrhenian Se
0d70: 61 22 3e 3c 2f 74 64 3e 0d 0a 3c 2f 74 61 62 6c  a"></td>..</tabl
0d80: 65 3e 3c 62 72 3e 0d 0a 3c 68 32 3e 41 20 66 69  e><br>..<h2>A fi
0d90: 72 73 74 2c 20 73 69 6d 70 6c 65 20 61 6e 64 20  rst, simple and 
0da0: 76 65 72 79 20 69 6e 65 66 66 69 63 69 65 6e 74  very inefficient
0db0: 2c 20 61 70 70 72 6f 61 63 68 3c 2f 68 32 3e 0d  , approach</h2>.
0dc0: 0a 3c 76 65 72 62 61 74 69 6d 3e 0d 0a 53 45 4c  .<verbatim>..SEL
0dd0: 45 43 54 20 67 65 6f 6e 61 6d 65 69 64 2c 20 6e  ECT geonameid, n
0de0: 61 6d 65 2c 20 63 6f 75 6e 74 72 79 2c 20 0d 0a  ame, country, ..
0df0: 20 20 20 20 4d 69 6e 28 53 54 5f 44 69 73 74 61      Min(ST_Dista
0e00: 6e 63 65 28 4d 61 6b 65 50 6f 69 6e 74 28 31 30  nce(MakePoint(10
0e10: 2c 20 34 33 29 2c 20 67 65 6f 6d 2c 20 31 29 29  , 43), geom, 1))
0e20: 20 2f 20 31 30 30 30 2e 30 20 41 53 20 64 69 73   / 1000.0 AS dis
0e30: 74 5f 6b 6d 0d 0a 46 52 4f 4d 20 61 69 72 70 6f  t_km..FROM airpo
0e40: 72 74 73 3b 0d 0a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  rts;..----------
0e50: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0e60: 2d 2d 2d 2d 2d 2d 0d 0a 36 32 39 39 36 32 33 09  ------..6299623.
0e70: 7c 20 4d 61 72 69 6e 61 20 44 69 20 43 61 6d 70  | Marina Di Camp
0e80: 6f 20 7c 20 49 54 20 7c 20 33 33 2e 30 34 33 33  o | IT | 33.0433
0e90: 32 30 0d 0a 0d 0a 53 45 4c 45 43 54 20 67 65 6f  20....SELECT geo
0ea0: 6e 61 6d 65 69 64 2c 20 6e 61 6d 65 2c 20 63 6f  nameid, name, co
0eb0: 75 6e 74 72 79 2c 20 0d 0a 20 20 20 20 4d 69 6e  untry, ..    Min
0ec0: 28 53 54 5f 44 69 73 74 61 6e 63 65 28 4d 61 6b  (ST_Distance(Mak
0ed0: 65 50 6f 69 6e 74 28 31 30 2c 20 34 33 29 2c 20  ePoint(10, 43), 
0ee0: 67 65 6f 6d 2c 20 31 29 29 20 2f 20 31 30 30 30  geom, 1)) / 1000
0ef0: 2e 30 20 41 53 20 64 69 73 74 5f 6b 6d 0d 0a 46  .0 AS dist_km..F
0f00: 52 4f 4d 20 61 69 72 70 6f 72 74 73 0d 0a 57 48  ROM airports..WH
0f10: 45 52 45 20 67 65 6f 6e 61 6d 65 69 64 20 4e 4f  ERE geonameid NO
0f20: 54 20 49 4e 20 28 36 32 39 39 36 32 33 29 3b 0d  T IN (6299623);.
0f30: 0a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  .---------------
0f40: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0f50: 2d 2d 0d 0a 36 32 39 39 33 39 32 20 7c 20 42 61  --..6299392 | Ba
0f60: 73 74 69 61 2d 50 6f 72 65 74 74 61 20 7c 20 46  stia-Poretta | F
0f70: 52 20 7c 20 36 35 2e 32 32 36 35 37 33 0d 0a 0d  R | 65.226573...
0f80: 0a 53 45 4c 45 43 54 20 67 65 6f 6e 61 6d 65 69  .SELECT geonamei
0f90: 64 2c 20 6e 61 6d 65 2c 20 63 6f 75 6e 74 72 79  d, name, country
0fa0: 2c 0d 0a 20 20 20 20 4d 69 6e 28 53 54 5f 44 69  ,..    Min(ST_Di
0fb0: 73 74 61 6e 63 65 28 4d 61 6b 65 50 6f 69 6e 74  stance(MakePoint
0fc0: 28 31 30 2c 20 34 33 29 2c 20 67 65 6f 6d 2c 20  (10, 43), geom, 
0fd0: 31 29 29 20 2f 20 31 30 30 30 2e 30 20 41 53 20  1)) / 1000.0 AS 
0fe0: 64 69 73 74 5f 6b 6d 0d 0a 46 52 4f 4d 20 61 69  dist_km..FROM ai
0ff0: 72 70 6f 72 74 73 0d 0a 57 48 45 52 45 20 67 65  rports..WHERE ge
1000: 6f 6e 61 6d 65 69 64 20 4e 4f 54 20 49 4e 20 28  onameid NOT IN (
1010: 36 32 39 39 36 32 33 2c 20 36 32 39 39 33 39 32  6299623, 6299392
1020: 29 3b 0d 0a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  );..------------
1030: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1040: 2d 2d 2d 2d 2d 0d 0a 36 32 39 39 36 32 38 20 7c  -----..6299628 |
1050: 20 50 69 73 61 20 2f 20 53 2e 20 47 69 75 73 74   Pisa / S. Giust
1060: 6f 20 7c 20 49 54 09 7c 20 38 32 2e 33 38 37 30  o | IT.| 82.3870
1070: 31 34 0d 0a 0d 0a 53 45 4c 45 43 54 20 67 65 6f  14....SELECT geo
1080: 6e 61 6d 65 69 64 2c 20 6e 61 6d 65 2c 20 63 6f  nameid, name, co
1090: 75 6e 74 72 79 2c 20 0d 0a 20 20 20 20 4d 69 6e  untry, ..    Min
10a0: 28 53 54 5f 44 69 73 74 61 6e 63 65 28 4d 61 6b  (ST_Distance(Mak
10b0: 65 50 6f 69 6e 74 28 31 30 2c 20 34 33 29 2c 20  ePoint(10, 43), 
10c0: 67 65 6f 6d 2c 20 31 29 29 20 2f 20 31 30 30 30  geom, 1)) / 1000
10d0: 2e 30 20 41 53 20 64 69 73 74 5f 6b 6d 0d 0a 46  .0 AS dist_km..F
10e0: 52 4f 4d 20 61 69 72 70 6f 72 74 73 0d 0a 57 48  ROM airports..WH
10f0: 45 52 45 20 67 65 6f 6e 61 6d 65 69 64 20 4e 4f  ERE geonameid NO
1100: 54 20 49 4e 20 28 36 32 39 39 36 32 33 2c 20 36  T IN (6299623, 6
1110: 32 39 39 33 39 32 2c 20 36 32 39 39 36 32 38 29  299392, 6299628)
1120: 3b 0d 0a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ;..-------------
1130: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1140: 2d 2d 2d 2d 0d 0a 36 32 39 39 36 33 30 20 7c 20  ----..6299630 | 
1150: 47 72 6f 73 73 65 74 6f 20 41 69 72 70 6f 72 74  Grosseto Airport
1160: 20 7c 20 49 54 20 7c 20 39 31 2e 35 34 39 37 37   | IT | 91.54977
1170: 33 0d 0a 0d 0a 53 45 4c 45 43 54 20 67 65 6f 6e  3....SELECT geon
1180: 61 6d 65 69 64 2c 20 6e 61 6d 65 2c 20 63 6f 75  ameid, name, cou
1190: 6e 74 72 79 2c 20 0d 0a 20 20 20 20 4d 69 6e 28  ntry, ..    Min(
11a0: 53 54 5f 44 69 73 74 61 6e 63 65 28 4d 61 6b 65  ST_Distance(Make
11b0: 50 6f 69 6e 74 28 31 30 2c 20 34 33 29 2c 20 67  Point(10, 43), g
11c0: 65 6f 6d 2c 20 31 29 29 20 2f 20 31 30 30 30 2e  eom, 1)) / 1000.
11d0: 30 20 41 53 20 64 69 73 74 5f 6b 6d 0d 0a 46 52  0 AS dist_km..FR
11e0: 4f 4d 20 61 69 72 70 6f 72 74 73 0d 0a 57 48 45  OM airports..WHE
11f0: 52 45 20 67 65 6f 6e 61 6d 65 69 64 20 4e 4f 54  RE geonameid NOT
1200: 20 49 4e 20 28 36 32 39 39 36 32 33 2c 20 36 32   IN (6299623, 62
1210: 39 39 33 39 32 2c 20 36 32 39 39 36 32 38 2c 20  99392, 6299628, 
1220: 36 32 39 39 36 33 30 29 3b 0d 0a 2d 2d 2d 2d 2d  6299630);..-----
1230: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1240: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0d 0a 36 36  ------------..66
1250: 39 34 34 39 35 20 7c 20 43 6f 72 74 65 20 7c 20  94495 | Corte | 
1260: 46 52 20 7c 20 31 30 32 2e 38 31 39 37 37 38 0d  FR | 102.819778.
1270: 0a 3c 2f 76 65 72 62 61 74 69 6d 3e 0d 0a 57 65  .</verbatim>..We
1280: 20 63 6f 75 6c 64 20 65 66 66 65 63 74 69 76 65   could effective
1290: 6c 79 20 65 78 65 63 75 74 65 20 61 20 66 69 72  ly execute a fir
12a0: 73 74 20 53 51 4c 20 71 75 65 72 79 20 75 73 69  st SQL query usi
12b0: 6e 67 20 74 68 65 20 3c 62 3e 4d 69 6e 28 29 3c  ng the <b>Min()<
12c0: 2f 62 3e 20 61 67 67 72 65 67 61 74 65 20 66 75  /b> aggregate fu
12d0: 6e 63 74 69 6f 6e 20 69 6e 20 6f 72 64 65 72 20  nction in order 
12e0: 74 6f 20 69 64 65 6e 74 69 66 79 20 74 68 65 20  to identify the 
12f0: 63 6c 6f 73 65 73 74 20 61 69 72 70 6f 72 74 20  closest airport 
1300: 74 6f 20 61 6e 20 61 72 62 69 74 72 61 72 79 20  to an arbitrary 
1310: 70 6f 73 69 74 69 6f 6e 20 28 3c 69 3e 6c 61 74  position (<i>lat
1320: 69 74 75 64 65 3c 2f 69 3e 3d 3c 62 3e 34 33 2e  itude</i>=<b>43.
1330: 30 3c 2f 62 3e 2c 20 3c 69 3e 6c 6f 6e 67 69 74  0</b>, <i>longit
1340: 75 64 65 3c 2f 69 3e 3d 3c 62 3e 31 30 2e 30 3c  ude</i>=<b>10.0<
1350: 2f 62 3e 29 20 6c 6f 63 61 74 65 64 20 69 6e 20  /b>) located in 
1360: 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 74 68  the middle of th
1370: 65 20 54 79 72 72 68 65 6e 69 61 6e 20 53 65 61  e Tyrrhenian Sea
1380: 2e 3c 62 72 3e 0d 0a 54 68 65 6e 20 77 65 20 63  .<br>..Then we c
1390: 6f 75 6c 64 20 65 76 65 6e 74 75 61 6c 6c 79 20  ould eventually 
13a0: 72 65 70 65 61 74 61 62 6c 79 20 65 78 65 63 75  repeatably execu
13b0: 74 65 20 74 68 65 20 69 64 65 6e 74 69 63 61 6c  te the identical
13c0: 20 71 75 65 72 79 2c 20 65 61 63 68 20 74 69 6d   query, each tim
13d0: 65 20 65 78 63 6c 75 64 69 6e 67 20 61 6c 6c 20  e excluding all 
13e0: 61 69 72 70 6f 72 74 73 20 77 65 27 76 65 20 61  airports we've a
13f0: 6c 72 65 61 64 79 20 69 64 65 6e 74 69 66 69 65  lready identifie
1400: 64 20 69 6e 20 74 68 65 20 70 72 65 76 69 6f 75  d in the previou
1410: 73 20 70 72 6f 63 65 73 73 65 64 20 73 74 65 70  s processed step
1420: 73 2e 3c 62 72 3e 0d 0a 54 68 65 20 66 69 6e 61  s.<br>..The fina
1430: 6c 20 72 65 73 75 6c 74 20 77 69 6c 6c 20 62 65  l result will be
1440: 20 74 68 65 20 66 75 6c 6c 20 6c 69 73 74 20 6f   the full list o
1450: 66 20 74 68 65 20 66 69 72 73 74 20 66 69 76 65  f the first five
1460: 20 61 69 72 70 6f 72 74 73 20 63 6c 6f 73 65 73   airports closes
1470: 74 20 74 6f 20 74 68 65 20 72 65 66 65 72 65 6e  t to the referen
1480: 63 65 20 6c 6f 63 61 74 69 6f 6e 20 6f 72 64 65  ce location orde
1490: 72 65 64 20 62 79 20 69 6e 63 72 65 61 73 69 6e  red by increasin
14a0: 67 20 64 69 73 74 61 6e 63 65 2e 0d 0a 3c 62 72  g distance...<br
14b0: 3e 3c 62 72 3e 0d 0a 3c 74 61 62 6c 65 20 62 67  ><br>..<table bg
14c0: 63 6f 6c 6f 72 3d 22 23 64 30 66 66 62 30 22 20  color="#d0ffb0" 
14d0: 63 65 6c 6c 73 70 61 63 69 6e 67 3d 22 34 22 20  cellspacing="4" 
14e0: 63 65 6c 6c 70 61 64 64 69 6e 67 3d 22 34 22 20  cellpadding="4" 
14f0: 62 6f 72 64 65 72 3d 22 31 22 3e 0d 0a 3c 74 72  border="1">..<tr
1500: 3e 3c 74 64 20 62 67 63 6f 6c 6f 72 3d 22 23 63  ><td bgcolor="#c
1510: 30 64 64 61 6f 22 3e 3c 62 3e 72 61 6e 6b 3c 2f  0ddao"><b>rank</
1520: 62 3e 3c 2f 74 64 3e 3c 74 64 20 62 67 63 6f 6c  b></td><td bgcol
1530: 6f 72 3d 22 23 63 30 64 64 61 6f 22 3e 3c 62 3e  or="#c0ddao"><b>
1540: 67 65 6f 6e 61 6d 65 69 64 3c 2f 62 3e 3c 2f 74  geonameid</b></t
1550: 64 3e 3c 74 64 20 62 67 63 6f 6c 6f 72 3d 22 23  d><td bgcolor="#
1560: 63 30 64 64 61 6f 22 3e 3c 62 3e 6e 61 6d 65 3c  c0ddao"><b>name<
1570: 2f 62 3e 3c 2f 74 64 3e 3c 74 64 20 62 67 63 6f  /b></td><td bgco
1580: 6c 6f 72 3d 22 23 63 30 64 64 61 6f 22 3e 3c 62  lor="#c0ddao"><b
1590: 3e 63 6f 75 6e 74 72 79 3c 2f 62 3e 3c 2f 74 64  >country</b></td
15a0: 3e 3c 74 64 20 62 67 63 6f 6c 6f 72 3d 22 23 63  ><td bgcolor="#c
15b0: 30 64 64 61 6f 22 3e 3c 62 3e 64 69 73 74 5f 6b  0ddao"><b>dist_k
15c0: 6d 3c 2f 62 3e 3c 2f 74 64 3e 3c 2f 74 72 3e 0d  m</b></td></tr>.
15d0: 0a 3c 74 72 3e 3c 74 64 20 61 6c 69 67 6e 3d 22  .<tr><td align="
15e0: 72 69 67 68 74 22 3e 31 3c 2f 74 64 3e 3c 74 64  right">1</td><td
15f0: 20 61 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e 36   align="right">6
1600: 32 39 39 36 32 33 3c 2f 74 64 3e 3c 74 64 3e 4d  299623</td><td>M
1610: 61 72 69 6e 61 20 44 69 20 43 61 6d 70 6f 3c 2f  arina Di Campo</
1620: 74 64 3e 3c 74 64 3e 49 54 3c 2f 74 64 3e 3c 74  td><td>IT</td><t
1630: 64 20 61 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e  d align="right">
1640: 33 33 2e 30 34 33 33 32 30 3c 2f 74 64 3e 3c 2f  33.043320</td></
1650: 74 72 3e 0d 0a 3c 74 72 3e 3c 74 64 20 61 6c 69  tr>..<tr><td ali
1660: 67 6e 3d 22 72 69 67 68 74 22 3e 32 3c 2f 74 64  gn="right">2</td
1670: 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69 67 68  ><td align="righ
1680: 74 22 3e 36 32 39 39 33 39 32 3c 2f 74 64 3e 3c  t">6299392</td><
1690: 74 64 3e 42 61 73 74 69 61 2d 50 6f 72 65 74 74  td>Bastia-Porett
16a0: 61 3c 2f 74 64 3e 3c 74 64 3e 46 52 3c 2f 74 64  a</td><td>FR</td
16b0: 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69 67 68  ><td align="righ
16c0: 74 22 3e 36 35 2e 32 32 36 35 37 33 3c 2f 74 64  t">65.226573</td
16d0: 3e 3c 2f 74 72 3e 0d 0a 3c 74 72 3e 3c 74 64 20  ></tr>..<tr><td 
16e0: 61 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e 33 3c  align="right">3<
16f0: 2f 74 64 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72  /td><td align="r
1700: 69 67 68 74 22 3e 36 32 39 39 36 32 38 3c 2f 74  ight">6299628</t
1710: 64 3e 3c 74 64 3e 50 69 73 61 20 2f 20 53 2e 20  d><td>Pisa / S. 
1720: 47 69 75 73 74 6f 3c 2f 74 64 3e 3c 74 64 3e 49  Giusto</td><td>I
1730: 54 3c 2f 74 64 3e 3c 74 64 20 61 6c 69 67 6e 3d  T</td><td align=
1740: 22 72 69 67 68 74 22 3e 38 32 2e 33 38 37 30 31  "right">82.38701
1750: 34 3c 2f 74 64 3e 3c 2f 74 72 3e 0d 0a 3c 74 72  4</td></tr>..<tr
1760: 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69 67 68  ><td align="righ
1770: 74 22 3e 34 3c 2f 74 64 3e 3c 74 64 20 61 6c 69  t">4</td><td ali
1780: 67 6e 3d 22 72 69 67 68 74 22 3e 36 32 39 39 36  gn="right">62996
1790: 33 30 3c 2f 74 64 3e 3c 74 64 3e 47 72 6f 73 73  30</td><td>Gross
17a0: 65 74 6f 20 41 69 72 70 6f 72 74 3c 2f 74 64 3e  eto Airport</td>
17b0: 3c 74 64 3e 49 54 3c 2f 74 64 3e 3c 74 64 20 61  <td>IT</td><td a
17c0: 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e 39 31 2e  lign="right">91.
17d0: 35 34 39 37 37 33 3c 2f 74 64 3e 3c 2f 74 72 3e  549773</td></tr>
17e0: 0d 0a 3c 74 72 3e 3c 74 64 20 61 6c 69 67 6e 3d  ..<tr><td align=
17f0: 22 72 69 67 68 74 22 3e 35 3c 2f 74 64 3e 3c 74  "right">5</td><t
1800: 64 20 61 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e  d align="right">
1810: 36 36 39 34 34 39 35 3c 2f 74 64 3e 3c 74 64 3e  6694495</td><td>
1820: 43 6f 72 74 65 3c 2f 74 64 3e 3c 74 64 3e 46 52  Corte</td><td>FR
1830: 3c 2f 74 64 3e 3c 74 64 20 61 6c 69 67 6e 3d 22  </td><td align="
1840: 72 69 67 68 74 22 3e 31 30 32 2e 38 31 39 37 37  right">102.81977
1850: 38 3c 2f 74 64 3e 3c 2f 74 72 3e 0d 0a 3c 2f 74  8</td></tr>..</t
1860: 61 62 6c 65 3e 3c 62 72 3e 0d 0a 53 75 63 68 20  able><br>..Such 
1870: 61 20 73 69 6d 70 6c 65 20 61 70 70 72 6f 61 63  a simple approac
1880: 68 20 77 6f 75 6c 64 20 62 65 20 68 69 67 68 6c  h would be highl
1890: 79 20 69 6d 70 72 61 63 74 69 63 61 6c 2c 20 62  y impractical, b
18a0: 65 63 61 75 73 65 20 69 74 20 73 74 72 69 63 74  ecause it strict
18b0: 6c 79 20 72 65 71 75 69 72 65 73 20 74 6f 20 68  ly requires to h
18c0: 61 6e 64 2d 77 72 69 74 65 20 73 65 76 65 72 61  and-write severa
18d0: 6c 20 3c 69 3e 61 64 20 68 6f 63 3c 2f 69 3e 20  l <i>ad hoc</i> 
18e0: 53 51 4c 20 71 75 65 72 69 65 73 20 6f 6e 65 20  SQL queries one 
18f0: 62 79 20 6f 6e 65 2e 3c 62 72 3e 0d 0a 57 72 69  by one.<br>..Wri
1900: 74 69 6e 67 20 61 20 66 75 6c 6c 79 20 61 75 74  ting a fully aut
1910: 6f 6d 61 74 65 64 20 53 51 4c 20 73 63 72 69 70  omated SQL scrip
1920: 74 20 69 73 6e 27 74 20 61 74 20 61 6c 6c 20 61  t isn't at all a
1930: 20 73 69 6d 70 6c 65 20 74 61 73 6b 2c 20 61 6e   simple task, an
1940: 64 20 73 6f 6d 65 20 6b 69 6e 64 20 6f 66 20 68  d some kind of h
1950: 69 67 68 65 72 20 6c 65 76 65 6c 20 73 63 72 69  igher level scri
1960: 70 74 69 6e 67 20 28 65 2e 67 2e 20 50 79 74 68  pting (e.g. Pyth
1970: 6f 6e 29 20 77 6f 75 6c 64 20 62 65 20 65 61 73  on) would be eas
1980: 69 6c 79 20 72 65 71 75 69 72 65 64 20 69 6e 20  ily required in 
1990: 61 6e 79 20 72 65 61 6c 69 73 74 69 63 20 73 63  any realistic sc
19a0: 65 6e 61 72 69 6f 2e 3c 62 72 3e 0d 0a 54 68 65  enario.<br>..The
19b0: 20 61 75 74 6f 6d 61 74 69 6f 6e 20 6f 66 20 61   automation of a
19c0: 6c 6c 20 72 65 71 75 69 72 65 64 20 53 51 4c 20  ll required SQL 
19d0: 71 75 65 72 69 65 73 20 69 73 6e 27 74 20 74 68  queries isn't th
19e0: 65 20 6d 6f 73 74 20 63 72 69 74 69 63 61 6c 20  e most critical 
19f0: 69 73 73 75 65 20 77 65 20 68 61 76 65 20 74 6f  issue we have to
1a00: 20 66 61 63 65 3b 20 74 68 65 20 72 65 61 6c 2c   face; the real,
1a10: 20 63 72 69 74 69 63 61 6c 2c 20 62 6f 74 74 6c   critical, bottl
1a20: 65 20 6e 65 63 6b 20 77 69 74 68 20 74 68 69 73  e neck with this
1a30: 20 73 69 6d 70 6c 65 20 61 70 70 72 6f 61 63 68   simple approach
1a40: 2c 20 69 73 20 74 68 61 74 20 65 61 63 68 20 6f  , is that each o
1a50: 66 20 74 68 65 73 65 20 71 75 65 72 69 65 73 20  f these queries 
1a60: 77 69 6c 6c 20 63 61 75 73 65 20 61 6e 20 61 77  will cause an aw
1a70: 66 75 6c 20 3c 61 20 68 72 65 66 3d 22 68 74 74  ful <a href="htt
1a80: 70 73 3a 2f 2f 65 6e 2e 77 69 6b 69 70 65 64 69  ps://en.wikipedi
1a90: 61 2e 6f 72 67 2f 77 69 6b 69 2f 46 75 6c 6c 5f  a.org/wiki/Full_
1aa0: 74 61 62 6c 65 5f 73 63 61 6e 22 3e 66 75 6c 6c  table_scan">full
1ab0: 20 74 61 62 6c 65 20 73 63 61 6e 3c 2f 61 3e 3c   table scan</a><
1ac0: 62 72 3e 0d 0a 54 68 69 73 20 77 69 6c 6c 20 62  br>..This will b
1ad0: 65 20 74 68 65 20 63 61 75 73 65 20 6f 66 20 61  e the cause of a
1ae0: 6e 20 69 6e 74 6f 6c 65 72 61 62 6c 79 20 73 6c  n intolerably sl
1af0: 75 67 67 69 73 68 20 70 65 72 66 6f 72 6d 61 6e  uggish performan
1b00: 63 65 2c 20 61 6e 64 20 74 68 65 20 6f 76 65 72  ce, and the over
1b10: 61 6c 6c 20 70 65 72 66 6f 72 6d 61 6e 63 65 20  all performance 
1b20: 77 69 6c 6c 20 71 75 69 63 6b 6c 79 20 62 65 63  will quickly bec
1b30: 6f 6d 65 20 77 6f 72 73 65 20 61 73 20 74 68 65  ome worse as the
1b40: 20 64 61 74 61 73 65 74 20 70 72 6f 67 72 65 73   dataset progres
1b50: 73 69 76 65 6c 79 20 69 6e 63 72 65 61 73 65 73  sively increases
1b60: 20 69 6e 20 73 69 7a 65 2e 0d 0a 3c 62 72 3e 3c   in size...<br><
1b70: 62 72 3e 0d 0a 3c 62 3e 43 6f 6e 63 6c 75 73 69  br>..<b>Conclusi
1b80: 6f 6e 3c 2f 62 3e 3a 3c 62 72 3e 0d 0a 53 6f 6d  on</b>:<br>..Som
1b90: 65 20 65 61 73 69 65 72 20 61 6e 64 20 73 6d 61  e easier and sma
1ba0: 72 74 65 72 20 61 70 70 72 6f 61 63 68 20 69 73  rter approach is
1bb0: 20 73 75 72 65 6c 79 20 72 65 71 75 69 72 65 64   surely required
1bc0: 3a 20 70 6f 73 73 69 62 6c 79 20 6f 6e 65 20 74  : possibly one t
1bd0: 61 6b 69 6e 67 20 66 75 6c 6c 20 70 72 6f 66 69  aking full profi
1be0: 74 20 66 72 6f 6d 20 61 6e 20 3c 62 3e 52 2a 54  t from an <b>R*T
1bf0: 52 65 65 20 53 70 61 74 69 61 6c 20 49 6e 64 65  Ree Spatial Inde
1c00: 78 3c 2f 62 3e 20 73 75 70 70 6f 72 74 69 6e 67  x</b> supporting
1c10: 20 74 68 65 20 47 65 6f 6d 65 74 72 69 65 73 20   the Geometries 
1c20: 74 6f 20 62 65 20 73 65 61 72 63 68 65 64 2e 3c  to be searched.<
1c30: 62 72 3e 3c 62 72 3e 0d 0a 3c 68 72 3e 0d 0a 3c  br><br>..<hr>..<
1c40: 68 32 3e 56 69 72 74 75 61 6c 4b 4e 4e 3c 2f 68  h2>VirtualKNN</h
1c50: 32 3e 0d 0a 53 74 61 72 74 69 6e 67 20 73 69 6e  2>..Starting sin
1c60: 63 65 20 76 65 72 73 69 6f 6e 20 3c 62 3e 34 2e  ce version <b>4.
1c70: 34 2e 30 3c 2f 62 3e 20 53 70 61 74 69 61 4c 69  4.0</b> SpatiaLi
1c80: 74 65 20 73 75 70 70 6f 72 74 73 20 61 20 3c 62  te supports a <b
1c90: 3e 56 69 72 74 75 61 6c 4b 4e 4e 3c 2f 62 3e 20  >VirtualKNN</b> 
1ca0: 3c 61 20 68 72 65 66 3d 22 68 74 74 70 73 3a 2f  <a href="https:/
1cb0: 2f 77 77 77 2e 73 71 6c 69 74 65 2e 6f 72 67 2f  /www.sqlite.org/
1cc0: 76 74 61 62 2e 68 74 6d 6c 22 3e 56 69 72 74 75  vtab.html">Virtu
1cd0: 61 6c 20 54 61 62 6c 65 3c 2f 61 3e 20 73 70 65  al Table</a> spe
1ce0: 63 69 66 69 63 61 6c 6c 79 20 69 6e 74 65 6e 64  cifically intend
1cf0: 65 64 20 61 73 20 61 20 63 6f 6d 70 6c 65 74 65  ed as a complete
1d00: 20 61 6e 64 20 68 69 67 68 6c 79 20 65 66 66 69   and highly effi
1d10: 63 69 65 6e 74 20 73 6f 6c 75 74 69 6f 6e 20 74  cient solution t
1d20: 6f 20 74 68 65 20 3c 62 3e 4b 4e 4e 3c 2f 62 3e  o the <b>KNN</b>
1d30: 20 70 72 6f 62 6c 65 6d 2e 0d 0a 3c 76 65 72 62   problem...<verb
1d40: 61 74 69 6d 3e 0d 0a 43 52 45 41 54 45 20 56 49  atim>..CREATE VI
1d50: 52 54 55 41 4c 20 54 41 42 4c 45 20 6b 6e 6e 20  RTUAL TABLE knn 
1d60: 55 53 49 4e 47 20 56 69 72 74 75 61 6c 4b 4e 4e  USING VirtualKNN
1d70: 28 29 3b 0d 0a 3c 2f 76 65 72 62 61 74 69 6d 3e  ();..</verbatim>
1d80: 0d 0a 45 76 65 72 79 20 6e 65 77 20 64 62 2d 66  ..Every new db-f
1d90: 69 6c 65 20 62 65 69 6e 67 20 63 72 65 61 74 65  ile being create
1da0: 64 20 62 79 20 3c 62 3e 34 2e 34 2e 30 3c 2f 62  d by <b>4.4.0</b
1db0: 3e 20 69 74 73 65 6c 66 20 77 69 6c 6c 20 61 6c  > itself will al
1dc0: 77 61 79 73 20 61 75 74 6f 6d 61 74 69 63 61 6c  ways automatical
1dd0: 6c 79 20 64 65 66 69 6e 65 20 61 20 3c 62 3e 4b  ly define a <b>K
1de0: 4e 4e 3c 2f 62 3e 20 76 69 72 74 75 61 6c 20 74  NN</b> virtual t
1df0: 61 62 6c 65 2e 3c 62 72 3e 0d 0a 59 6f 75 20 63  able.<br>..You c
1e00: 6f 75 6c 64 20 65 76 65 6e 74 75 61 6c 6c 79 20  ould eventually 
1e10: 61 64 64 20 66 75 6c 6c 20 4b 4e 4e 20 73 75 70  add full KNN sup
1e20: 70 6f 72 74 20 74 6f 20 61 6e 79 20 64 62 2d 66  port to any db-f
1e30: 69 6c 65 20 63 72 65 61 74 65 64 20 62 79 20 61  ile created by a
1e40: 6e 79 20 65 61 72 6c 69 65 72 20 76 65 72 73 69  ny earlier versi
1e50: 6f 6e 20 6f 66 20 53 70 61 74 69 61 4c 69 74 65  on of SpatiaLite
1e60: 20 62 79 20 6d 61 6e 75 61 6c 6c 79 20 65 78 65   by manually exe
1e70: 63 75 74 69 6e 67 20 74 68 65 20 61 62 6f 76 65  cuting the above
1e80: 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 3c   SQL statement.<
1e90: 62 72 3e 0d 0a 3c 62 3e 4e 6f 74 65 3c 2f 62 3e  br>..<b>Note</b>
1ea0: 3a 20 56 69 72 74 75 61 6c 4b 4e 4e 20 6e 65 63  : VirtualKNN nec
1eb0: 65 73 73 61 72 69 6c 79 20 72 65 71 75 69 72 65  essarily require
1ec0: 73 20 34 2e 34 2e 30 20 62 69 6e 61 72 79 20 73  s 4.4.0 binary s
1ed0: 75 70 70 6f 72 74 2c 20 73 6f 20 61 6e 79 20 61  upport, so any a
1ee0: 74 74 65 6d 70 74 20 74 6f 20 6f 70 65 6e 20 61  ttempt to open a
1ef0: 20 64 62 2d 66 69 6c 65 20 69 6e 63 6c 75 64 69   db-file includi
1f00: 6e 67 20 61 20 56 69 72 74 75 61 6c 4b 4e 4e 20  ng a VirtualKNN 
1f10: 74 61 62 6c 65 20 62 79 20 75 73 69 6e 67 20 73  table by using s
1f20: 6f 6d 65 20 70 72 65 76 69 6f 75 73 20 76 65 72  ome previous ver
1f30: 73 69 6f 6e 20 28 3c 3d 20 3c 62 3e 34 2e 33 2e  sion (<= <b>4.3.
1f40: 30 61 3c 2f 62 3e 29 20 77 69 6c 6c 20 73 75 72  0a</b>) will sur
1f50: 65 6c 79 20 72 61 69 73 65 20 61 6e 20 65 72 72  ely raise an err
1f60: 6f 72 20 63 6f 6e 64 69 74 69 6f 6e 2e 0d 0a 3c  or condition...<
1f70: 68 33 3e 41 20 66 69 72 73 74 20 62 61 73 69 63  h3>A first basic
1f80: 61 6c 6c 79 20 73 69 6d 70 6c 65 20 4b 4e 4e 20  ally simple KNN 
1f90: 71 75 65 72 79 3c 2f 68 33 3e 0d 0a 3c 76 65 72  query</h3>..<ver
1fa0: 62 61 74 69 6d 3e 0d 0a 53 45 4c 45 43 54 20 2a  batim>..SELECT *
1fb0: 20 46 52 4f 4d 20 6b 6e 6e 0d 0a 57 48 45 52 45   FROM knn..WHERE
1fc0: 20 66 5f 74 61 62 6c 65 5f 6e 61 6d 65 20 3d 20   f_table_name = 
1fd0: 27 61 69 72 70 6f 72 74 73 27 20 41 4e 44 20 72  'airports' AND r
1fe0: 65 66 5f 67 65 6f 6d 65 74 72 79 20 3d 20 4d 61  ef_geometry = Ma
1ff0: 6b 65 50 6f 69 6e 74 28 31 30 2c 20 34 33 29 3b  kePoint(10, 43);
2000: 0d 0a 3c 2f 76 65 72 62 61 74 69 6d 3e 0d 0a 3c  ..</verbatim>..<
2010: 74 61 62 6c 65 20 62 67 63 6f 6c 6f 72 3d 22 23  table bgcolor="#
2020: 64 30 66 66 62 30 22 20 63 65 6c 6c 73 70 61 63  d0ffb0" cellspac
2030: 69 6e 67 3d 22 34 22 20 63 65 6c 6c 70 61 64 64  ing="4" cellpadd
2040: 69 6e 67 3d 22 34 22 20 62 6f 72 64 65 72 3d 22  ing="4" border="
2050: 31 22 3e 0d 0a 3c 74 72 3e 3c 74 64 20 62 67 63  1">..<tr><td bgc
2060: 6f 6c 6f 72 3d 22 23 63 30 64 64 61 6f 22 3e 3c  olor="#c0ddao"><
2070: 62 3e 66 5f 74 61 62 6c 65 5f 6e 61 6d 65 3c 2f  b>f_table_name</
2080: 62 3e 3c 2f 74 64 3e 3c 74 64 20 62 67 63 6f 6c  b></td><td bgcol
2090: 6f 72 3d 22 23 63 30 64 64 61 6f 22 3e 3c 62 3e  or="#c0ddao"><b>
20a0: 66 5f 67 65 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d  f_geometry_colum
20b0: 6e 3c 2f 62 3e 3c 2f 74 64 3e 3c 74 64 20 62 67  n</b></td><td bg
20c0: 63 6f 6c 6f 72 3d 22 23 63 30 64 64 61 6f 22 3e  color="#c0ddao">
20d0: 3c 62 3e 72 65 66 5f 67 65 6f 6d 65 74 72 79 3c  <b>ref_geometry<
20e0: 2f 62 3e 3c 2f 74 64 3e 3c 74 64 20 62 67 63 6f  /b></td><td bgco
20f0: 6c 6f 72 3d 22 23 63 30 64 64 61 6f 22 3e 3c 62  lor="#c0ddao"><b
2100: 3e 6d 61 78 5f 69 74 65 6d 73 3c 2f 62 3e 3c 2f  >max_items</b></
2110: 74 64 3e 3c 74 64 20 62 67 63 6f 6c 6f 72 3d 22  td><td bgcolor="
2120: 23 63 30 64 64 61 6f 22 3e 3c 62 3e 70 6f 73 3c  #c0ddao"><b>pos<
2130: 2f 62 3e 3c 2f 74 64 3e 3c 74 64 20 62 67 63 6f  /b></td><td bgco
2140: 6c 6f 72 3d 22 23 63 30 64 64 61 6f 22 3e 3c 62  lor="#c0ddao"><b
2150: 3e 66 69 64 3c 2f 62 3e 3c 2f 74 64 3e 3c 74 64  >fid</b></td><td
2160: 20 62 67 63 6f 6c 6f 72 3d 22 23 63 30 64 64 61   bgcolor="#c0dda
2170: 6f 22 3e 3c 62 3e 64 69 73 74 61 6e 63 65 3c 2f  o"><b>distance</
2180: 62 3e 3c 2f 74 64 3e 3c 2f 74 72 3e 0d 0a 3c 74  b></td></tr>..<t
2190: 72 3e 3c 74 64 3e 61 69 72 70 6f 72 74 73 3c 2f  r><td>airports</
21a0: 74 64 3e 3c 74 64 3e 67 65 6f 6d 3c 2f 74 64 3e  td><td>geom</td>
21b0: 3c 74 64 3e 42 4c 4f 42 20 73 7a 3d 36 30 20 47  <td>BLOB sz=60 G
21c0: 45 4f 4d 45 54 52 59 3c 2f 74 64 3e 3c 74 64 20  EOMETRY</td><td 
21d0: 61 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e 33 3c  align="right">3<
21e0: 2f 74 64 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72  /td><td align="r
21f0: 69 67 68 74 22 3e 31 3c 2f 74 64 3e 3c 74 64 20  ight">1</td><td 
2200: 61 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e 36 32  align="right">62
2210: 39 39 36 32 33 3c 2f 74 64 3e 3c 74 64 20 61 6c  99623</td><td al
2220: 69 67 6e 3d 22 72 69 67 68 74 22 3e 33 33 30 34  ign="right">3304
2230: 33 2e 33 31 39 35 32 30 3c 2f 74 64 3e 3c 2f 74  3.319520</td></t
2240: 72 3e 0d 0a 3c 74 72 3e 3c 74 64 3e 61 69 72 70  r>..<tr><td>airp
2250: 6f 72 74 73 3c 2f 74 64 3e 3c 74 64 3e 67 65 6f  orts</td><td>geo
2260: 6d 3c 2f 74 64 3e 3c 74 64 3e 42 4c 4f 42 20 73  m</td><td>BLOB s
2270: 7a 3d 36 30 20 47 45 4f 4d 45 54 52 59 3c 2f 74  z=60 GEOMETRY</t
2280: 64 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69 67  d><td align="rig
2290: 68 74 22 3e 33 3c 2f 74 64 3e 3c 74 64 20 61 6c  ht">3</td><td al
22a0: 69 67 6e 3d 22 72 69 67 68 74 22 3e 32 3c 2f 74  ign="right">2</t
22b0: 64 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69 67  d><td align="rig
22c0: 68 74 22 3e 36 32 39 39 33 39 32 3c 2f 74 64 3e  ht">6299392</td>
22d0: 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69 67 68 74  <td align="right
22e0: 22 3e 36 35 32 32 36 2e 35 37 33 31 34 39 3c 2f  ">65226.573149</
22f0: 74 64 3e 3c 2f 74 72 3e 0d 0a 3c 74 72 3e 3c 74  td></tr>..<tr><t
2300: 64 3e 61 69 72 70 6f 72 74 73 3c 2f 74 64 3e 3c  d>airports</td><
2310: 74 64 3e 67 65 6f 6d 3c 2f 74 64 3e 3c 74 64 3e  td>geom</td><td>
2320: 42 4c 4f 42 20 73 7a 3d 36 30 20 47 45 4f 4d 45  BLOB sz=60 GEOME
2330: 54 52 59 3c 2f 74 64 3e 3c 74 64 20 61 6c 69 67  TRY</td><td alig
2340: 6e 3d 22 72 69 67 68 74 22 3e 33 3c 2f 74 64 3e  n="right">3</td>
2350: 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69 67 68 74  <td align="right
2360: 22 3e 33 3c 2f 74 64 3e 3c 74 64 20 61 6c 69 67  ">3</td><td alig
2370: 6e 3d 22 72 69 67 68 74 22 3e 36 32 39 39 36 32  n="right">629962
2380: 38 3c 2f 74 64 3e 3c 74 64 20 61 6c 69 67 6e 3d  8</td><td align=
2390: 22 72 69 67 68 74 22 3e 38 32 33 38 37 2e 30 31  "right">82387.01
23a0: 34 30 32 38 3c 2f 74 64 3e 3c 2f 74 72 3e 0d 0a  4028</td></tr>..
23b0: 3c 2f 74 61 62 6c 65 3e 3c 62 72 3e 0d 0a 3c 75  </table><br>..<u
23c0: 6c 3e 0d 0a 3c 6c 69 3e 61 20 56 69 72 74 75 61  l>..<li>a Virtua
23d0: 6c 4b 4e 4e 20 71 75 65 72 79 20 63 6c 6f 73 65  lKNN query close
23e0: 6c 79 20 72 65 73 65 6d 62 6c 65 73 20 61 20 56  ly resembles a V
23f0: 69 72 74 75 61 6c 53 70 61 74 69 61 6c 49 6e 64  irtualSpatialInd
2400: 65 78 20 71 75 65 72 79 2e 0d 0a 54 68 69 73 20  ex query...This 
2410: 69 73 20 6e 6f 74 20 73 75 72 70 72 69 73 69 6e  is not surprisin
2420: 67 2c 20 73 69 6e 63 65 20 62 6f 74 68 20 6f 66  g, since both of
2430: 20 74 68 65 6d 20 61 72 65 20 69 6e 74 65 6e 64   them are intend
2440: 65 64 20 74 6f 20 62 65 20 77 72 61 70 70 65 64  ed to be wrapped
2450: 20 61 72 6f 75 6e 64 20 61 6e 20 75 6e 64 65 72   around an under
2460: 6c 79 69 6e 67 20 3c 62 3e 52 2a 54 72 65 65 20  lying <b>R*Tree 
2470: 53 70 61 74 69 61 6c 20 49 6e 64 65 78 3c 2f 62  Spatial Index</b
2480: 3e 2e 3c 2f 6c 69 3e 20 0d 0a 3c 6c 69 3e 41 6e  >.</li> ..<li>An
2490: 79 20 76 61 6c 69 64 20 56 69 72 74 75 61 6c 4b  y valid VirtualK
24a0: 4e 4e 20 71 75 65 72 79 20 6d 75 73 74 20 75 73  NN query must us
24b0: 65 20 61 20 66 6f 72 6d 20 73 75 63 68 20 61 73  e a form such as
24c0: 3a 0d 0a 3c 62 3e 57 48 45 52 45 20 3c 69 3e 6b  :..<b>WHERE <i>k
24d0: 6e 6e 2d 63 6f 6c 75 6d 6e 3c 2f 69 3e 20 3d 20  nn-column</i> = 
24e0: 3c 69 3e 76 61 6c 75 65 3c 2f 69 3e 20 41 4e 44  <i>value</i> AND
24f0: 20 3c 69 3e 6b 6e 6e 2d 63 6f 6c 75 6d 6e 3c 2f   <i>knn-column</
2500: 69 3e 20 3d 20 3c 69 3e 76 61 6c 75 65 3c 2f 69  i> = <i>value</i
2510: 3e 20 2e 2e 2e 3c 2f 62 3e 3c 2f 6c 69 3e 0d 0a  > ...</b></li>..
2520: 3c 6c 69 3e 74 68 65 20 69 6e 70 75 74 20 63 6f  <li>the input co
2530: 6c 75 6d 6e 73 20 28 74 68 65 20 6f 6e 65 73 20  lumns (the ones 
2540: 79 6f 75 20 63 61 6e 20 72 65 66 65 72 65 6e 63  you can referenc
2550: 65 20 69 6e 74 6f 20 61 20 3c 62 3e 57 48 45 52  e into a <b>WHER
2560: 45 3c 2f 62 3e 20 63 6c 61 75 73 65 29 20 61 72  E</b> clause) ar
2570: 65 3a 0d 0a 3c 75 6c 3e 0d 0a 3c 6c 69 3e 3c 62  e:..<ul>..<li><b
2580: 3e 66 5f 74 61 62 6c 65 5f 6e 61 6d 65 3c 2f 62  >f_table_name</b
2590: 3e 20 28 3c 69 3e 6d 61 6e 64 61 74 6f 72 79 3c  > (<i>mandatory<
25a0: 2f 69 3e 29 3c 62 72 3e 0d 0a 6e 61 6d 65 20 6f  /i>)<br>..name o
25b0: 66 20 74 68 65 20 47 65 6f 54 61 62 6c 65 20 63  f the GeoTable c
25c0: 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 47 65  ontaining the Ge
25d0: 6f 6d 65 74 72 69 65 73 20 74 6f 20 62 65 20 73  ometries to be s
25e0: 65 61 72 63 68 65 64 2e 3c 2f 6c 69 3e 0d 0a 3c  earched.</li>..<
25f0: 6c 69 3e 3c 62 3e 66 5f 67 65 6f 6d 65 74 72 79  li><b>f_geometry
2600: 5f 63 6f 6c 75 6d 6e 3c 2f 62 3e 20 28 3c 69 3e  _column</b> (<i>
2610: 6f 70 74 69 6f 6e 61 6c 3c 2f 69 3e 29 3c 62 72  optional</i>)<br
2620: 3e 0d 0a 6e 61 6d 65 20 6f 66 20 74 68 65 20 63  >..name of the c
2630: 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 61 62 6f  olumn of the abo
2640: 76 65 20 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e  ve table contain
2650: 69 6e 67 20 74 68 65 20 47 65 6f 6d 65 74 72 69  ing the Geometri
2660: 65 73 20 74 6f 20 62 65 20 73 65 61 72 63 68 65  es to be searche
2670: 64 2e 0d 0a 3c 6f 6c 3e 0d 0a 3c 6c 69 3e 49 66  d...<ol>..<li>If
2680: 20 74 68 65 20 74 61 62 6c 65 20 69 64 65 6e 74   the table ident
2690: 69 66 69 65 64 20 62 79 20 3c 62 3e 66 5f 74 61  ified by <b>f_ta
26a0: 62 6c 65 5f 6e 61 6d 65 3c 2f 62 3e 20 6a 75 73  ble_name</b> jus
26b0: 74 20 63 6f 6e 74 61 69 6e 73 20 61 20 73 69 6e  t contains a sin
26c0: 67 6c 65 20 47 65 6f 6d 65 74 72 79 20 63 6f 6c  gle Geometry col
26d0: 75 6d 6e 20 79 6f 75 20 63 61 6e 20 73 61 66 65  umn you can safe
26e0: 6c 79 20 6f 6d 69 74 20 74 68 65 20 3c 62 3e 66  ly omit the <b>f
26f0: 5f 67 65 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e  _geometry_column
2700: 3c 2f 62 3e 20 61 72 67 75 6d 65 6e 74 20 28 69  </b> argument (i
2710: 74 20 77 69 6c 6c 20 62 65 20 61 75 74 6f 6d 61  t will be automa
2720: 74 69 63 61 6c 6c 79 20 73 65 74 29 2e 3c 2f 6c  tically set).</l
2730: 69 3e 0d 0a 3c 6c 69 3e 49 66 2c 20 68 6f 77 65  i>..<li>If, howe
2740: 76 65 72 2c 20 74 68 65 20 74 61 62 6c 65 20 69  ver, the table i
2750: 64 65 6e 74 69 66 69 65 64 20 62 79 20 3c 62 3e  dentified by <b>
2760: 66 5f 74 61 62 6c 65 5f 6e 61 6d 65 3c 2f 62 3e  f_table_name</b>
2770: 20 63 6f 6e 74 61 69 6e 73 20 74 77 6f 20 28 6f   contains two (o
2780: 72 20 6d 6f 72 65 29 20 47 65 6f 6d 65 74 72 79  r more) Geometry
2790: 20 63 6f 6c 75 6d 6e 73 20 65 78 70 6c 69 63 69   columns explici
27a0: 74 6c 79 20 73 70 65 63 69 66 79 69 6e 67 20 74  tly specifying t
27b0: 68 65 20 3c 62 3e 66 5f 67 65 6f 6d 65 74 72 79  he <b>f_geometry
27c0: 5f 63 6f 6c 75 6d 6e 3c 2f 62 3e 20 61 72 67 75  _column</b> argu
27d0: 6d 65 6e 74 20 69 73 20 73 74 72 69 63 74 6c 79  ment is strictly
27e0: 20 72 65 71 75 69 72 65 64 2c 20 73 6f 20 74 6f   required, so to
27f0: 20 67 65 74 20 61 6e 20 75 6e 61 6d 62 69 67 75   get an unambigu
2800: 6f 75 73 20 64 65 66 69 6e 69 74 69 6f 6e 20 6f  ous definition o
2810: 66 20 74 68 65 20 69 6e 74 65 6e 64 65 64 20 73  f the intended s
2820: 65 61 72 63 68 20 63 6f 6e 74 65 78 74 2e 3c 2f  earch context.</
2830: 6c 69 3e 0d 0a 3c 6c 69 3e 49 6e 20 62 6f 74 68  li>..<li>In both
2840: 20 63 61 73 65 73 20 3c 62 3e 66 5f 74 61 62 6c   cases <b>f_tabl
2850: 65 5f 6e 61 6d 65 3c 2f 62 3e 20 61 6e 64 20 3c  e_name</b> and <
2860: 62 3e 66 5f 67 65 6f 6d 65 74 72 79 5f 63 6f 6c  b>f_geometry_col
2870: 75 6d 6e 3c 2f 62 3e 20 6d 75 73 74 20 65 78 61  umn</b> must exa
2880: 63 74 6c 79 20 6d 61 74 63 68 20 61 20 70 72 6f  ctly match a pro
2890: 70 65 72 6c 79 20 64 65 66 69 6e 65 64 20 47 65  perly defined Ge
28a0: 6f 6d 65 74 72 79 20 63 6f 6c 75 6d 6e 20 73 75  ometry column su
28b0: 70 70 6f 72 74 65 64 20 62 79 20 61 20 63 6f 72  pported by a cor
28c0: 72 65 73 70 6f 6e 64 69 6e 67 20 53 70 61 74 69  responding Spati
28d0: 61 6c 20 49 6e 64 65 78 2e 3c 2f 6c 69 3e 0d 0a  al Index.</li>..
28e0: 3c 2f 6f 6c 3e 3c 2f 6c 69 3e 0d 0a 3c 6c 69 3e  </ol></li>..<li>
28f0: 3c 62 3e 72 65 66 5f 67 65 6f 6d 65 74 72 79 3c  <b>ref_geometry<
2900: 2f 62 3e 20 28 3c 69 3e 6d 61 6e 64 61 74 6f 72  /b> (<i>mandator
2910: 79 3c 2f 69 3e 29 3c 62 72 3e 0d 0a 61 6e 79 20  y</i>)<br>..any 
2920: 61 72 62 69 74 72 61 72 79 20 47 65 6f 6d 65 74  arbitrary Geomet
2930: 72 79 20 28 3c 62 3e 50 4f 49 4e 54 3c 2f 62 3e  ry (<b>POINT</b>
2940: 2c 20 3c 62 3e 4c 49 4e 45 53 54 52 49 4e 47 3c  , <b>LINESTRING<
2950: 2f 62 3e 2c 20 3c 62 3e 50 4f 4c 59 47 4f 4e 3c  /b>, <b>POLYGON<
2960: 2f 62 3e 20 6f 72 20 77 68 61 74 65 76 65 72 20  /b> or whatever 
2970: 65 6c 73 65 29 20 69 6e 74 65 6e 64 65 64 20 74  else) intended t
2980: 6f 20 72 65 70 72 65 73 65 6e 74 20 74 68 65 20  o represent the 
2990: 6f 72 69 67 69 6e 20 6f 66 20 74 68 65 20 4b 4e  origin of the KN
29a0: 4e 20 73 65 61 72 63 68 2e 3c 62 72 3e 0d 0a 53  N search.<br>..S
29b0: 68 6f 75 6c 64 20 6e 65 63 65 73 73 61 72 69 6c  hould necessaril
29c0: 79 20 62 65 20 69 6e 20 74 68 65 20 73 61 6d 65  y be in the same
29d0: 20 53 52 49 44 20 6f 66 20 74 68 65 20 74 61 72   SRID of the tar
29e0: 67 65 74 20 47 65 6f 6d 65 74 72 69 65 73 20 74  get Geometries t
29f0: 6f 20 62 65 20 73 65 61 72 63 68 65 64 2e 3c 2f  o be searched.</
2a00: 6c 69 3e 0d 0a 3c 6c 69 3e 3c 62 3e 6d 61 78 5f  li>..<li><b>max_
2a10: 69 74 65 6d 73 3c 2f 62 3e 20 28 3c 69 3e 6f 70  items</b> (<i>op
2a20: 74 69 6f 6e 61 6c 3c 2f 69 3e 29 3c 62 72 3e 0d  tional</i>)<br>.
2a30: 0a 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20  .maximum number 
2a40: 6f 66 20 72 6f 77 73 20 74 6f 20 62 65 20 72 65  of rows to be re
2a50: 74 75 72 6e 65 64 20 69 6e 74 6f 20 74 68 65 20  turned into the 
2a60: 72 65 73 75 6c 74 73 65 74 2e 3c 62 72 3e 0d 0a  resultset.<br>..
2a70: 54 68 65 20 76 61 6c 69 64 20 72 61 6e 67 65 20  The valid range 
2a80: 69 73 20 66 72 6f 6d 20 3c 62 3e 31 3c 2f 62 3e  is from <b>1</b>
2a90: 20 74 6f 20 3c 62 3e 31 30 32 34 3c 2f 62 3e 20   to <b>1024</b> 
2aa0: 28 68 69 67 68 65 72 20 76 61 6c 75 65 73 20 77  (higher values w
2ab0: 69 6c 6c 20 72 65 71 75 69 72 65 20 61 20 6c 6f  ill require a lo
2ac0: 6e 67 65 72 20 74 69 6d 65 20 74 6f 20 62 65 20  nger time to be 
2ad0: 70 72 6f 63 65 73 73 65 64 29 2e 3c 62 72 3e 0d  processed).<br>.
2ae0: 0a 42 79 20 64 65 66 61 75 6c 74 20 6f 6e 6c 79  .By default only
2af0: 20 74 68 65 20 66 69 72 73 74 20 3c 62 3e 33 3c   the first <b>3<
2b00: 2f 62 3e 20 6e 65 61 72 65 73 74 20 47 65 6f 6d  /b> nearest Geom
2b10: 65 74 72 69 65 73 20 77 69 6c 6c 20 62 65 20 69  etries will be i
2b20: 64 65 6e 74 69 66 69 65 64 2e 3c 2f 6c 69 3e 0d  dentified.</li>.
2b30: 0a 3c 2f 75 6c 3e 3c 2f 6c 69 3e 0d 0a 3c 6c 69  .</ul></li>..<li
2b40: 3e 74 68 65 20 6f 75 74 70 75 74 20 63 6f 6c 75  >the output colu
2b50: 6d 6e 73 20 28 74 68 65 20 6f 6e 65 73 20 63 6f  mns (the ones co
2b60: 6e 74 61 69 6e 69 6e 67 20 76 61 6c 75 65 73 20  ntaining values 
2b70: 72 65 74 72 69 65 76 65 64 20 62 79 20 74 68 65  retrieved by the
2b80: 20 4b 4e 4e 20 73 65 61 72 63 68 29 20 61 72 65   KNN search) are
2b90: 3a 0d 0a 3c 75 6c 3e 0d 0a 3c 6c 69 3e 3c 62 3e  :..<ul>..<li><b>
2ba0: 70 6f 73 3c 2f 62 3e 20 28 3c 69 3e 49 4e 54 45  pos</b> (<i>INTE
2bb0: 47 45 52 3c 2f 69 3e 29 3c 62 72 3e 0d 0a 72 65  GER</i>)<br>..re
2bc0: 6c 61 74 69 76 65 20 72 61 6e 6b 20 28 73 6f 72  lative rank (sor
2bd0: 74 65 64 20 62 79 20 64 69 73 74 61 6e 63 65 29  ted by distance)
2be0: 3a 20 74 68 65 20 63 6c 6f 73 65 73 74 20 69 74  : the closest it
2bf0: 65 6d 20 77 69 6c 6c 20 62 65 20 3c 62 3e 23 31  em will be <b>#1
2c00: 3c 2f 62 3e 2c 20 74 68 65 20 73 65 63 6f 6e 64  </b>, the second
2c10: 20 63 6c 6f 73 65 73 74 20 69 74 65 6d 20 77 69   closest item wi
2c20: 6c 6c 20 62 65 20 3c 62 3e 23 32 3c 2f 62 3e 20  ll be <b>#2</b> 
2c30: 61 6e 64 20 73 6f 20 6f 6e 2e 3c 2f 6c 69 3e 0d  and so on.</li>.
2c40: 0a 3c 6c 69 3e 3c 62 3e 66 69 64 3c 2f 62 3e 20  .<li><b>fid</b> 
2c50: 28 3c 69 3e 49 4e 54 45 47 45 52 3c 2f 69 3e 29  (<i>INTEGER</i>)
2c60: 3c 62 72 3e 0d 0a 74 68 65 20 75 6e 69 71 75 65  <br>..the unique
2c70: 20 3c 62 3e 52 4f 57 49 44 3c 2f 62 3e 20 76 61   <b>ROWID</b> va
2c80: 6c 75 65 20 6f 66 20 74 68 65 20 63 6f 72 72 65  lue of the corre
2c90: 73 70 6f 6e 64 69 6e 67 20 66 65 61 74 75 72 65  sponding feature
2ca0: 20 69 6e 74 6f 20 74 68 65 20 73 65 61 72 63 68   into the search
2cb0: 65 64 20 47 65 6f 54 61 62 6c 65 2e 3c 2f 6c 69  ed GeoTable.</li
2cc0: 3e 0d 0a 3c 6c 69 3e 3c 62 3e 64 69 73 74 61 6e  >..<li><b>distan
2cd0: 63 65 3c 2f 62 3e 20 28 3c 69 3e 44 4f 55 42 4c  ce</b> (<i>DOUBL
2ce0: 45 3c 2f 69 3e 29 3c 62 72 3e 0d 0a 74 68 65 20  E</i>)<br>..the 
2cf0: 6d 69 6e 69 6d 75 6d 20 64 69 73 74 61 6e 63 65  minimum distance
2d00: 20 62 65 74 77 65 65 6e 20 74 68 65 20 63 6f 72   between the cor
2d10: 72 65 73 70 6f 6e 64 69 6e 67 20 66 65 61 74 75  responding featu
2d20: 72 65 20 69 6e 74 6f 20 74 68 65 20 73 65 61 72  re into the sear
2d30: 63 68 65 64 20 47 65 6f 54 61 62 6c 65 20 61 6e  ched GeoTable an
2d40: 64 20 74 68 65 20 6f 72 69 67 69 6e 20 64 65 66  d the origin def
2d50: 69 6e 65 64 20 62 79 20 3c 62 3e 72 65 66 5f 67  ined by <b>ref_g
2d60: 65 6f 6d 65 74 72 79 3c 2f 62 3e 2e 0d 0a 3c 6f  eometry</b>...<o
2d70: 6c 3e 0d 0a 3c 6c 69 3e 57 68 65 6e 20 74 68 65  l>..<li>When the
2d80: 20 64 65 66 69 6e 65 64 20 53 52 49 44 20 75 73   defined SRID us
2d90: 65 73 20 61 20 3c 62 3e 70 6c 61 6e 61 72 3c 62  es a <b>planar<b
2da0: 3e 20 28 61 6b 61 20 3c 69 3e 70 72 6f 6a 65 63  > (aka <i>projec
2db0: 74 65 64 3c 69 3e 29 20 72 65 66 65 72 65 6e 63  ted<i>) referenc
2dc0: 65 20 73 79 73 74 65 6d 2c 20 61 6c 6c 20 64 69  e system, all di
2dd0: 73 74 61 6e 63 65 73 20 77 69 6c 6c 20 62 65 20  stances will be 
2de0: 72 65 74 75 72 6e 65 64 20 69 6e 20 74 68 65 20  returned in the 
2df0: 75 6e 69 74 73 20 64 65 66 69 6e 65 64 20 62 79  units defined by
2e00: 20 74 68 65 20 70 72 6f 6a 65 63 74 69 6f 6e 20   the projection 
2e10: 28 6d 65 74 65 72 73 2c 20 66 65 65 74 2c 20 63  (meters, feet, c
2e20: 68 61 69 6e 73 20 65 74 63 2e 29 2e 3c 2f 6c 69  hains etc.).</li
2e30: 3e 0d 0a 3c 6c 69 3e 57 68 65 6e 20 75 73 69 6e  >..<li>When usin
2e40: 67 20 61 20 3c 62 3e 67 65 6f 67 72 61 70 68 69  g a <b>geographi
2e50: 63 3c 2f 62 3e 20 74 79 70 65 20 28 3c 69 3e 6c  c</b> type (<i>l
2e60: 6f 6e 67 69 74 75 64 65 3c 2f 69 3e 20 61 6e 64  ongitude</i> and
2e70: 20 3c 69 3e 6c 61 74 69 74 75 64 65 3c 2f 69 3e   <i>latitude</i>
2e80: 20 3c 62 3e 64 65 67 72 65 65 73 3c 2f 62 3e 29   <b>degrees</b>)
2e90: 2c 20 61 6c 6c 20 64 69 73 74 61 6e 63 65 73 20  , all distances 
2ea0: 77 69 6c 6c 20 61 6c 77 61 79 73 20 62 65 20 6d  will always be m
2eb0: 65 61 73 75 72 65 64 20 69 6e 20 3c 62 3e 6d 65  easured in <b>me
2ec0: 74 65 72 73 3c 2f 62 3e 2c 20 61 6e 64 20 74 68  ters</b>, and th
2ed0: 65 20 6d 6f 73 74 20 70 72 65 63 69 73 65 20 67  e most precise g
2ee0: 65 6f 64 65 74 69 63 20 66 6f 72 6d 75 6c 61 73  eodetic formulas
2ef0: 20 77 69 6c 6c 20 62 65 20 61 75 74 6f 6d 61 74   will be automat
2f00: 69 63 61 6c 6c 79 20 61 70 70 6c 69 65 64 2e 3c  ically applied.<
2f10: 2f 6c 69 3e 0d 0a 3c 2f 6f 6c 3e 3c 2f 6c 69 3e  /li>..</ol></li>
2f20: 0d 0a 3c 2f 75 6c 3e 3c 2f 6c 69 3e 0d 0a 3c 2f  ..</ul></li>..</
2f30: 75 6c 3e 3c 62 72 3e 0d 0a 3c 68 72 3e 0d 0a 3c  ul><br>..<hr>..<
2f40: 68 33 3e 41 20 73 65 63 6f 6e 64 20 65 78 61 6d  h3>A second exam
2f50: 70 6c 65 20 6f 66 20 61 20 6d 6f 72 65 20 73 6f  ple of a more so
2f60: 70 68 69 73 74 69 63 61 74 65 64 20 4b 4e 4e 20  phisticated KNN 
2f70: 71 75 65 72 79 3c 2f 68 33 3e 0d 0a 3c 76 65 72  query</h3>..<ver
2f80: 62 61 74 69 6d 3e 0d 0a 53 45 4c 45 43 54 20 61  batim>..SELECT a
2f90: 2e 70 6f 73 2c 20 62 2e 6e 61 6d 65 2c 20 62 2e  .pos, b.name, b.
2fa0: 63 6f 75 6e 74 72 79 2c 20 61 2e 64 69 73 74 61  country, a.dista
2fb0: 6e 63 65 20 2f 20 31 30 30 30 2e 30 20 41 53 20  nce / 1000.0 AS 
2fc0: 64 69 73 74 5f 6b 6d 0d 0a 46 52 4f 4d 20 6b 6e  dist_km..FROM kn
2fd0: 6e 20 41 53 20 61 0d 0a 4a 4f 49 4e 20 61 69 72  n AS a..JOIN air
2fe0: 70 6f 72 74 73 20 41 53 20 62 20 4f 4e 20 28 62  ports AS b ON (b
2ff0: 2e 67 65 6f 6e 61 6d 65 69 64 20 3d 20 61 2e 66  .geonameid = a.f
3000: 69 64 29 0d 0a 57 48 45 52 45 20 66 5f 74 61 62  id)..WHERE f_tab
3010: 6c 65 5f 6e 61 6d 65 20 3d 20 27 61 69 72 70 6f  le_name = 'airpo
3020: 72 74 73 27 20 41 4e 44 20 72 65 66 5f 67 65 6f  rts' AND ref_geo
3030: 6d 65 74 72 79 20 3d 20 4d 61 6b 65 50 6f 69 6e  metry = MakePoin
3040: 74 28 31 30 2c 20 34 33 29 20 41 4e 44 20 6d 61  t(10, 43) AND ma
3050: 78 5f 69 74 65 6d 73 20 3d 20 35 3b 0d 0a 3c 2f  x_items = 5;..</
3060: 76 65 72 62 61 74 69 6d 3e 0d 0a 3c 74 61 62 6c  verbatim>..<tabl
3070: 65 20 62 67 63 6f 6c 6f 72 3d 22 23 64 30 66 66  e bgcolor="#d0ff
3080: 62 30 22 20 63 65 6c 6c 73 70 61 63 69 6e 67 3d  b0" cellspacing=
3090: 22 34 22 20 63 65 6c 6c 70 61 64 64 69 6e 67 3d  "4" cellpadding=
30a0: 22 34 22 20 62 6f 72 64 65 72 3d 22 31 22 3e 0d  "4" border="1">.
30b0: 0a 3c 74 72 3e 3c 74 64 20 62 67 63 6f 6c 6f 72  .<tr><td bgcolor
30c0: 3d 22 23 63 30 64 64 61 6f 22 3e 3c 62 3e 72 61  ="#c0ddao"><b>ra
30d0: 6e 6b 3c 2f 62 3e 3c 2f 74 64 3e 3c 74 64 20 62  nk</b></td><td b
30e0: 67 63 6f 6c 6f 72 3d 22 23 63 30 64 64 61 6f 22  gcolor="#c0ddao"
30f0: 3e 3c 62 3e 67 65 6f 6e 61 6d 65 69 64 3c 2f 62  ><b>geonameid</b
3100: 3e 3c 2f 74 64 3e 3c 74 64 20 62 67 63 6f 6c 6f  ></td><td bgcolo
3110: 72 3d 22 23 63 30 64 64 61 6f 22 3e 3c 62 3e 6e  r="#c0ddao"><b>n
3120: 61 6d 65 3c 2f 62 3e 3c 2f 74 64 3e 3c 74 64 20  ame</b></td><td 
3130: 62 67 63 6f 6c 6f 72 3d 22 23 63 30 64 64 61 6f  bgcolor="#c0ddao
3140: 22 3e 3c 62 3e 63 6f 75 6e 74 72 79 3c 2f 62 3e  "><b>country</b>
3150: 3c 2f 74 64 3e 3c 74 64 20 62 67 63 6f 6c 6f 72  </td><td bgcolor
3160: 3d 22 23 63 30 64 64 61 6f 22 3e 3c 62 3e 64 69  ="#c0ddao"><b>di
3170: 73 74 5f 6b 6d 3c 2f 62 3e 3c 2f 74 64 3e 3c 2f  st_km</b></td></
3180: 74 72 3e 0d 0a 3c 74 72 3e 3c 74 64 20 61 6c 69  tr>..<tr><td ali
3190: 67 6e 3d 22 72 69 67 68 74 22 3e 31 3c 2f 74 64  gn="right">1</td
31a0: 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69 67 68  ><td align="righ
31b0: 74 22 3e 36 32 39 39 36 32 33 3c 2f 74 64 3e 3c  t">6299623</td><
31c0: 74 64 3e 4d 61 72 69 6e 61 20 44 69 20 43 61 6d  td>Marina Di Cam
31d0: 70 6f 3c 2f 74 64 3e 3c 74 64 3e 49 54 3c 2f 74  po</td><td>IT</t
31e0: 64 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69 67  d><td align="rig
31f0: 68 74 22 3e 33 33 2e 30 34 33 33 32 30 3c 2f 74  ht">33.043320</t
3200: 64 3e 3c 2f 74 72 3e 0d 0a 3c 74 72 3e 3c 74 64  d></tr>..<tr><td
3210: 20 61 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e 32   align="right">2
3220: 3c 2f 74 64 3e 3c 74 64 20 61 6c 69 67 6e 3d 22  </td><td align="
3230: 72 69 67 68 74 22 3e 36 32 39 39 33 39 32 3c 2f  right">6299392</
3240: 74 64 3e 3c 74 64 3e 42 61 73 74 69 61 2d 50 6f  td><td>Bastia-Po
3250: 72 65 74 74 61 3c 2f 74 64 3e 3c 74 64 3e 46 52  retta</td><td>FR
3260: 3c 2f 74 64 3e 3c 74 64 20 61 6c 69 67 6e 3d 22  </td><td align="
3270: 72 69 67 68 74 22 3e 36 35 2e 32 32 36 35 37 33  right">65.226573
3280: 3c 2f 74 64 3e 3c 2f 74 72 3e 0d 0a 3c 74 72 3e  </td></tr>..<tr>
3290: 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69 67 68 74  <td align="right
32a0: 22 3e 33 3c 2f 74 64 3e 3c 74 64 20 61 6c 69 67  ">3</td><td alig
32b0: 6e 3d 22 72 69 67 68 74 22 3e 36 32 39 39 36 32  n="right">629962
32c0: 38 3c 2f 74 64 3e 3c 74 64 3e 50 69 73 61 20 2f  8</td><td>Pisa /
32d0: 20 53 2e 20 47 69 75 73 74 6f 3c 2f 74 64 3e 3c   S. Giusto</td><
32e0: 74 64 3e 49 54 3c 2f 74 64 3e 3c 74 64 20 61 6c  td>IT</td><td al
32f0: 69 67 6e 3d 22 72 69 67 68 74 22 3e 38 32 2e 33  ign="right">82.3
3300: 38 37 30 31 34 3c 2f 74 64 3e 3c 2f 74 72 3e 0d  87014</td></tr>.
3310: 0a 3c 74 72 3e 3c 74 64 20 61 6c 69 67 6e 3d 22  .<tr><td align="
3320: 72 69 67 68 74 22 3e 34 3c 2f 74 64 3e 3c 74 64  right">4</td><td
3330: 20 61 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e 36   align="right">6
3340: 32 39 39 36 33 30 3c 2f 74 64 3e 3c 74 64 3e 47  299630</td><td>G
3350: 72 6f 73 73 65 74 6f 20 41 69 72 70 6f 72 74 3c  rosseto Airport<
3360: 2f 74 64 3e 3c 74 64 3e 49 54 3c 2f 74 64 3e 3c  /td><td>IT</td><
3370: 74 64 20 61 6c 69 67 6e 3d 22 72 69 67 68 74 22  td align="right"
3380: 3e 39 31 2e 35 34 39 37 37 33 3c 2f 74 64 3e 3c  >91.549773</td><
3390: 2f 74 72 3e 0d 0a 3c 74 72 3e 3c 74 64 20 61 6c  /tr>..<tr><td al
33a0: 69 67 6e 3d 22 72 69 67 68 74 22 3e 35 3c 2f 74  ign="right">5</t
33b0: 64 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69 67  d><td align="rig
33c0: 68 74 22 3e 36 36 39 34 34 39 35 3c 2f 74 64 3e  ht">6694495</td>
33d0: 3c 74 64 3e 43 6f 72 74 65 3c 2f 74 64 3e 3c 74  <td>Corte</td><t
33e0: 64 3e 46 52 3c 2f 74 64 3e 3c 74 64 20 61 6c 69  d>FR</td><td ali
33f0: 67 6e 3d 22 72 69 67 68 74 22 3e 31 30 32 2e 38  gn="right">102.8
3400: 31 39 37 37 38 3c 2f 74 64 3e 3c 2f 74 72 3e 0d  19778</td></tr>.
3410: 0a 3c 2f 74 61 62 6c 65 3e 3c 62 72 3e 0d 0a 54  .</table><br>..T
3420: 68 69 73 20 73 65 63 6f 6e 64 20 4b 4e 4e 20 71  his second KNN q
3430: 75 65 72 79 20 77 69 6c 6c 20 72 65 74 75 72 6e  uery will return
3440: 20 74 68 65 20 73 61 6d 65 20 69 64 65 6e 74 69   the same identi
3450: 63 61 6c 20 72 65 73 75 6c 74 73 20 77 65 20 61  cal results we a
3460: 6c 72 65 61 64 79 20 67 6f 74 20 62 65 66 6f 72  lready got befor
3470: 65 20 62 79 20 75 73 69 6e 67 20 74 68 65 20 73  e by using the s
3480: 69 6c 6c 79 20 6e 61 69 76 65 20 61 70 70 72 6f  illy naive appro
3490: 61 63 68 2e 3c 62 72 3e 0d 0a 54 68 65 20 73 74  ach.<br>..The st
34a0: 72 69 6b 69 6e 67 20 64 69 66 66 65 72 65 6e 63  riking differenc
34b0: 65 20 69 73 20 74 68 61 74 20 74 68 69 73 20 4b  e is that this K
34c0: 4e 4e 20 71 75 65 72 79 20 73 75 63 63 65 73 73  NN query success
34d0: 66 75 6c 6c 79 20 63 6f 6d 70 6c 65 74 65 64 20  fully completed 
34e0: 69 6e 20 6a 75 73 74 20 61 20 66 72 61 63 74 69  in just a fracti
34f0: 6f 6e 20 6f 66 20 61 20 73 65 63 6f 6e 64 2c 20  on of a second, 
3500: 77 68 69 6c 73 74 20 65 78 65 63 75 74 69 6e 67  whilst executing
3510: 20 74 68 65 20 66 69 72 73 74 20 69 6e 65 66 66   the first ineff
3520: 69 63 69 65 6e 74 20 71 75 65 72 69 65 73 20 72  icient queries r
3530: 65 71 75 69 72 65 64 20 6d 61 6e 79 20 73 65 63  equired many sec
3540: 6f 6e 64 73 20 28 61 6e 64 20 70 6c 65 61 73 65  onds (and please
3550: 20 63 6f 6e 73 69 64 65 72 20 74 68 61 74 20 74   consider that t
3560: 68 69 73 20 6f 6e 65 20 69 73 20 61 20 72 65 6c  his one is a rel
3570: 61 74 69 76 65 6c 79 20 73 6d 61 6c 6c 20 64 61  atively small da
3580: 74 61 73 65 74 29 2e 3c 62 72 3e 3c 62 72 3e 0d  taset).<br><br>.
3590: 0a 3c 62 3e 53 75 6d 6d 61 72 79 3c 2f 62 3e 3a  .<b>Summary</b>:
35a0: 20 4b 4e 4e 20 71 75 65 72 69 65 73 20 61 72 65   KNN queries are
35b0: 20 72 65 61 6c 6c 79 20 65 66 66 69 63 69 65 6e   really efficien
35c0: 74 20 61 6e 64 20 66 61 73 74 2c 20 62 65 63 61  t and fast, beca
35d0: 75 73 65 20 74 68 65 79 20 64 69 72 65 63 74 6c  use they directl
35e0: 79 20 69 6e 74 65 72 61 63 74 20 77 69 74 68 20  y interact with 
35f0: 74 68 65 20 6c 6f 77 65 72 6d 6f 73 74 20 6c 65  the lowermost le
3600: 76 65 6c 73 20 6f 66 20 74 68 65 20 52 2a 54 72  vels of the R*Tr
3610: 65 65 20 53 70 61 74 69 61 6c 20 49 6e 64 65 78  ee Spatial Index
3620: 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e   implementation.
3630: 3c 62 72 3e 3c 62 72 3e 0d 0a 3c 68 72 3e 0d 0a  <br><br>..<hr>..
3640: 3c 68 33 3e 41 20 74 68 69 72 64 20 73 61 6d 70  <h3>A third samp
3650: 6c 65 20 6f 66 20 4b 4e 4e 20 71 75 65 72 79 3c  le of KNN query<
3660: 2f 68 33 3e 0d 0a 3c 76 65 72 62 61 74 69 6d 3e  /h3>..<verbatim>
3670: 0d 0a 53 45 4c 45 43 54 20 61 2e 70 6f 73 20 41  ..SELECT a.pos A
3680: 53 20 72 61 6e 6b 2c 20 62 2e 67 65 6f 6e 61 6d  S rank, b.geonam
3690: 65 69 64 2c 20 62 2e 6e 61 6d 65 2c 20 62 2e 63  eid, b.name, b.c
36a0: 6f 75 6e 74 72 79 2c 20 61 2e 64 69 73 74 61 6e  ountry, a.distan
36b0: 63 65 20 2f 20 31 30 30 30 2e 30 20 41 53 20 64  ce / 1000.0 AS d
36c0: 69 73 74 5f 6b 6d 0d 0a 46 52 4f 4d 20 6b 6e 6e  ist_km..FROM knn
36d0: 20 41 53 20 61 0d 0a 4a 4f 49 4e 20 61 69 72 70   AS a..JOIN airp
36e0: 6f 72 74 73 20 41 53 20 62 20 4f 4e 20 28 62 2e  orts AS b ON (b.
36f0: 67 65 6f 6e 61 6d 65 69 64 20 3d 20 61 2e 66 69  geonameid = a.fi
3700: 64 29 0d 0a 57 48 45 52 45 20 66 5f 74 61 62 6c  d)..WHERE f_tabl
3710: 65 5f 6e 61 6d 65 20 3d 20 27 61 69 72 70 6f 72  e_name = 'airpor
3720: 74 73 27 20 41 4e 44 20 72 65 66 5f 67 65 6f 6d  ts' AND ref_geom
3730: 65 74 72 79 20 3d 20 4d 61 6b 65 50 6f 69 6e 74  etry = MakePoint
3740: 28 2d 31 37 2e 33 2c 20 2d 34 34 29 20 41 4e 44  (-17.3, -44) AND
3750: 20 6d 61 78 5f 69 74 65 6d 73 20 3d 20 35 3b 0d   max_items = 5;.
3760: 0a 3c 2f 76 65 72 62 61 74 69 6d 3e 0d 0a 3c 74  .</verbatim>..<t
3770: 61 62 6c 65 20 62 67 63 6f 6c 6f 72 3d 22 23 64  able bgcolor="#d
3780: 30 66 66 62 30 22 20 63 65 6c 6c 73 70 61 63 69  0ffb0" cellspaci
3790: 6e 67 3d 22 34 22 20 63 65 6c 6c 70 61 64 64 69  ng="4" cellpaddi
37a0: 6e 67 3d 22 34 22 20 62 6f 72 64 65 72 3d 22 31  ng="4" border="1
37b0: 22 3e 0d 0a 3c 74 72 3e 3c 74 64 20 62 67 63 6f  ">..<tr><td bgco
37c0: 6c 6f 72 3d 22 23 63 30 64 64 61 6f 22 3e 3c 62  lor="#c0ddao"><b
37d0: 3e 72 61 6e 6b 3c 2f 62 3e 3c 2f 74 64 3e 3c 74  >rank</b></td><t
37e0: 64 20 62 67 63 6f 6c 6f 72 3d 22 23 63 30 64 64  d bgcolor="#c0dd
37f0: 61 6f 22 3e 3c 62 3e 67 65 6f 6e 61 6d 65 69 64  ao"><b>geonameid
3800: 3c 2f 62 3e 3c 2f 74 64 3e 3c 74 64 20 62 67 63  </b></td><td bgc
3810: 6f 6c 6f 72 3d 22 23 63 30 64 64 61 6f 22 3e 3c  olor="#c0ddao"><
3820: 62 3e 6e 61 6d 65 3c 2f 62 3e 3c 2f 74 64 3e 3c  b>name</b></td><
3830: 74 64 20 62 67 63 6f 6c 6f 72 3d 22 23 63 30 64  td bgcolor="#c0d
3840: 64 61 6f 22 3e 3c 62 3e 63 6f 75 6e 74 72 79 3c  dao"><b>country<
3850: 2f 62 3e 3c 2f 74 64 3e 3c 74 64 20 62 67 63 6f  /b></td><td bgco
3860: 6c 6f 72 3d 22 23 63 30 64 64 61 6f 22 3e 3c 62  lor="#c0ddao"><b
3870: 3e 64 69 73 74 5f 6b 6d 3c 2f 62 3e 3c 2f 74 64  >dist_km</b></td
3880: 3e 3c 2f 74 72 3e 0d 0a 3c 74 72 3e 3c 74 64 20  ></tr>..<tr><td 
3890: 61 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e 31 3c  align="right">1<
38a0: 2f 74 64 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72  /td><td align="r
38b0: 69 67 68 74 22 3e 37 36 36 38 34 33 33 3c 2f 74  ight">7668433</t
38c0: 64 3e 3c 74 64 3e 43 61 62 6f 20 46 72 69 6f 20  d><td>Cabo Frio 
38d0: 41 69 72 70 6f 72 74 3c 2f 74 64 3e 3c 74 64 3e  Airport</td><td>
38e0: 42 52 3c 2f 74 64 3e 3c 74 64 20 61 6c 69 67 6e  BR</td><td align
38f0: 3d 22 72 69 67 68 74 22 3e 33 32 35 36 2e 35 38  ="right">3256.58
3900: 39 39 36 34 3c 2f 74 64 3e 3c 2f 74 72 3e 0d 0a  9964</td></tr>..
3910: 3c 74 72 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72  <tr><td align="r
3920: 69 67 68 74 22 3e 32 3c 2f 74 64 3e 3c 74 64 20  ight">2</td><td 
3930: 61 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e 33 33  align="right">33
3940: 35 39 30 30 31 3c 2f 74 64 3e 3c 74 64 3e 59 6f  59001</td><td>Yo
3950: 75 6e 67 73 66 69 65 6c 64 3c 2f 74 64 3e 3c 74  ungsfield</td><t
3960: 64 3e 5a 41 3c 2f 74 64 3e 3c 74 64 20 61 6c 69  d>ZA</td><td ali
3970: 67 6e 3d 22 72 69 67 68 74 22 3e 33 32 36 32 2e  gn="right">3262.
3980: 32 37 31 30 32 37 3c 2f 74 64 3e 3c 2f 74 72 3e  271027</td></tr>
3990: 0d 0a 3c 74 72 3e 3c 74 64 20 61 6c 69 67 6e 3d  ..<tr><td align=
39a0: 22 72 69 67 68 74 22 3e 33 3c 2f 74 64 3e 3c 74  "right">3</td><t
39b0: 64 20 61 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e  d align="right">
39c0: 37 37 33 30 31 35 33 3c 2f 74 64 3e 3c 74 64 3e  7730153</td><td>
39d0: 55 6d 62 65 72 74 6f 20 4d 6f 64 69 61 6e 6f 20  Umberto Modiano 
39e0: 41 69 72 70 6f 72 74 3c 2f 74 64 3e 3c 74 64 3e  Airport</td><td>
39f0: 42 52 3c 2f 74 64 3e 3c 74 64 20 61 6c 69 67 6e  BR</td><td align
3a00: 3d 22 72 69 67 68 74 22 3e 33 32 36 32 2e 36 39  ="right">3262.69
3a10: 33 30 33 32 3c 2f 74 64 3e 3c 2f 74 72 3e 0d 0a  3032</td></tr>..
3a20: 3c 74 72 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72  <tr><td align="r
3a30: 69 67 68 74 22 3e 34 3c 2f 74 64 3e 3c 74 64 20  ight">4</td><td 
3a40: 61 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e 33 33  align="right">33
3a50: 36 32 33 35 35 3c 2f 74 64 3e 3c 74 64 3e 52 6f  62355</td><td>Ro
3a60: 62 62 65 6e 20 49 73 6c 61 6e 64 3c 2f 74 64 3e  bben Island</td>
3a70: 3c 74 64 3e 5a 41 3c 2f 74 64 3e 3c 74 64 20 61  <td>ZA</td><td a
3a80: 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e 33 32 36  lign="right">326
3a90: 35 2e 33 30 32 35 36 38 3c 2f 74 64 3e 3c 2f 74  5.302568</td></t
3aa0: 72 3e 0d 0a 3c 74 72 3e 3c 74 64 20 61 6c 69 67  r>..<tr><td alig
3ab0: 6e 3d 22 72 69 67 68 74 22 3e 35 3c 2f 74 64 3e  n="right">5</td>
3ac0: 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69 67 68 74  <td align="right
3ad0: 22 3e 36 33 30 30 36 32 32 3c 2f 74 64 3e 3c 74  ">6300622</td><t
3ae0: 64 3e 53 2e 20 50 2e 20 41 6c 64 65 69 61 20 41  d>S. P. Aldeia A
3af0: 65 72 6f 64 72 6f 6d 65 3c 2f 74 64 3e 3c 74 64  erodrome</td><td
3b00: 3e 42 52 3c 2f 74 64 3e 3c 74 64 20 61 6c 69 67  >BR</td><td alig
3b10: 6e 3d 22 72 69 67 68 74 22 3e 33 32 36 37 2e 32  n="right">3267.2
3b20: 33 34 31 34 32 3c 2f 74 64 3e 3c 2f 74 72 3e 0d  34142</td></tr>.
3b30: 0a 3c 2f 74 61 62 6c 65 3e 3c 62 72 3e 0d 0a 54  .</table><br>..T
3b40: 68 69 73 20 6c 61 73 74 20 4b 4e 4e 20 71 75 65  his last KNN que
3b50: 72 79 20 69 73 20 76 65 72 79 20 73 69 6d 69 6c  ry is very simil
3b60: 61 72 20 74 6f 20 74 68 65 20 70 72 65 76 69 6f  ar to the previo
3b70: 75 73 20 6f 6e 65 2e 3c 62 72 3e 0d 0a 54 68 69  us one.<br>..Thi
3b80: 73 20 74 69 6d 65 20 77 65 27 76 65 20 70 6c 61  s time we've pla
3b90: 63 65 64 20 74 68 65 20 6f 72 69 67 69 6e 20 6c  ced the origin l
3ba0: 6f 63 61 74 69 6f 6e 20 73 6f 6d 65 77 68 65 72  ocation somewher
3bb0: 65 20 69 6e 20 74 68 65 20 53 6f 75 74 68 20 41  e in the South A
3bc0: 74 6c 61 6e 74 69 63 20 62 6c 75 65 20 64 65 65  tlantic blue dee
3bd0: 70 20 77 61 74 65 72 73 20 28 6d 6f 72 65 20 6f  p waters (more o
3be0: 72 20 6c 65 73 73 20 6d 69 64 77 61 79 20 66 72  r less midway fr
3bf0: 6f 6d 20 62 6f 74 68 20 42 72 61 7a 69 6c 20 61  om both Brazil a
3c00: 6e 64 20 53 6f 75 74 68 20 41 66 72 69 63 61 29  nd South Africa)
3c10: 20 61 6e 64 20 63 6f 6e 73 65 71 75 65 6e 74 6c   and consequentl
3c20: 79 20 74 68 65 20 6e 65 61 72 65 73 74 20 61 69  y the nearest ai
3c30: 72 70 6f 72 74 73 20 61 72 65 20 6e 6f 74 20 72  rports are not r
3c40: 65 61 6c 6c 79 20 73 6f 20 6e 65 61 72 2c 20 62  eally so near, b
3c50: 65 63 61 75 73 65 20 74 68 65 79 20 61 72 65 20  ecause they are 
3c60: 6c 6f 63 61 74 65 64 20 6d 61 6e 79 20 74 68 6f  located many tho
3c70: 75 73 61 6e 64 73 20 4b 6d 20 61 77 61 79 2e 0d  usands Km away..
3c80: 0a 41 73 20 79 6f 75 20 63 61 6e 20 65 61 73 69  .As you can easi
3c90: 6c 79 20 63 68 65 63 6b 20 62 79 20 79 6f 75 72  ly check by your
3ca0: 73 65 6c 66 20 61 20 4b 4e 4e 20 71 75 65 72 79  self a KNN query
3cb0: 20 77 69 6c 6c 20 62 72 69 6c 6c 69 61 6e 74 6c   will brilliantl
3cc0: 79 20 70 65 72 66 6f 72 6d 20 65 76 65 6e 20 75  y perform even u
3cd0: 6e 64 65 72 20 73 75 63 68 20 75 6e 75 73 75 61  nder such unusua
3ce0: 6c 20 63 6f 6e 64 69 74 69 6f 6e 73 2e 3c 62 72  l conditions.<br
3cf0: 3e 3c 62 72 3e 0d 0a 3c 69 6d 67 20 73 72 63 3d  ><br>..<img src=
3d00: 22 68 74 74 70 73 3a 2f 2f 77 77 77 2e 67 61 69  "https://www.gai
3d10: 61 2d 67 69 73 2e 69 74 2f 67 61 69 61 2d 73 69  a-gis.it/gaia-si
3d20: 6e 73 2f 6b 6e 6e 2f 73 6f 75 74 68 2d 61 74 6c  ns/knn/south-atl
3d30: 61 6e 74 69 63 2e 70 6e 67 22 20 61 6c 74 3d 22  antic.png" alt="
3d40: 53 6f 75 74 68 20 41 74 6c 61 6e 74 69 63 22 3e  South Atlantic">
3d50: 3c 62 72 3e 3c 62 72 3e 0d 0a 3c 62 3e 53 75 6d  <br><br>..<b>Sum
3d60: 6d 61 72 79 3c 2f 62 3e 3a 20 4b 4e 4e 20 71 75  mary</b>: KNN qu
3d70: 65 72 69 65 73 20 6e 65 76 65 72 20 61 73 73 75  eries never assu
3d80: 6d 65 20 61 6e 79 20 70 72 65 64 65 66 69 6e 65  me any predefine
3d90: 64 20 64 69 73 74 72 69 62 75 74 69 6f 6e 20 6f  d distribution o
3da0: 66 20 74 68 65 20 73 65 61 72 63 68 65 64 20 69  f the searched i
3db0: 74 65 6d 73 2c 20 61 6e 64 20 77 69 6c 6c 20 65  tems, and will e
3dc0: 78 74 72 61 63 74 20 74 68 65 20 63 6f 72 72 65  xtract the corre
3dd0: 63 74 20 72 65 73 75 6c 74 73 20 28 73 6f 72 74  ct results (sort
3de0: 65 64 20 62 79 20 64 69 73 74 61 6e 63 65 29 2c  ed by distance),
3df0: 20 64 65 73 70 69 74 65 20 61 20 76 65 72 79 20   despite a very 
3e00: 69 72 72 65 67 75 6c 61 72 20 73 61 6d 70 6c 65  irregular sample
3e10: 20 64 69 73 74 72 69 62 75 74 69 6f 6e 2e 3c 62   distribution.<b
3e20: 72 3e 3c 62 72 3e 0d 0a 3c 68 72 3e 0d 0a 3c 68  r><br>..<hr>..<h
3e30: 32 3e 41 64 76 61 6e 63 65 64 20 74 75 74 6f 72  2>Advanced tutor
3e40: 69 61 6c 3c 2f 68 32 3e 0d 0a 59 6f 75 20 63 61  ial</h2>..You ca
3e50: 6e 20 3c 61 20 68 72 65 66 3d 22 68 74 74 70 73  n <a href="https
3e60: 3a 2f 2f 77 77 77 2e 67 61 69 61 2d 67 69 73 2e  ://www.gaia-gis.
3e70: 69 74 2f 67 61 69 61 2d 73 69 6e 73 2f 6b 6e 6e  it/gaia-sins/knn
3e80: 2f 74 75 73 63 61 6e 79 5f 68 6f 75 73 65 6e 75  /tuscany_housenu
3e90: 6d 62 65 72 73 2e 37 7a 22 3e 64 6f 77 6e 6c 6f  mbers.7z">downlo
3ea0: 61 64 3c 2f 61 3e 20 61 20 73 61 6d 70 6c 65 20  ad</a> a sample 
3eb0: 64 62 2d 66 69 6c 65 20 65 78 61 63 74 6c 79 20  db-file exactly 
3ec0: 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f  corresponding to
3ed0: 20 74 68 65 20 70 72 61 63 74 69 63 61 6c 20 65   the practical e
3ee0: 78 61 6d 70 6c 65 73 20 77 65 27 6c 6c 20 75 73  xamples we'll us
3ef0: 65 20 69 6e 20 74 68 69 73 20 61 64 76 61 6e 63  e in this advanc
3f00: 65 64 20 74 75 74 6f 72 69 61 6c 2e 3c 62 72 3e  ed tutorial.<br>
3f10: 3c 62 72 3e 0d 0a 54 68 69 73 20 69 73 20 61 20  <br>..This is a 
3f20: 6d 75 63 68 20 6d 6f 72 65 20 64 65 6d 61 6e 64  much more demand
3f30: 69 6e 67 20 64 61 74 61 73 65 74 20 63 6f 72 72  ing dataset corr
3f40: 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 61 20 63  esponding to a c
3f50: 6f 6c 6c 65 63 74 69 6f 6e 20 6f 66 20 61 62 6f  ollection of abo
3f60: 75 74 20 3c 62 3e 32 2e 34 20 6d 69 6c 6c 69 6f  ut <b>2.4 millio
3f70: 6e 20 68 6f 75 73 65 20 6e 75 6d 62 65 72 73 3c  n house numbers<
3f80: 2f 62 3e 2e 3c 62 72 3e 0d 0a 54 68 65 20 6f 72  /b>.<br>..The or
3f90: 69 67 69 6e 61 6c 20 69 6e 70 75 74 20 73 68 61  iginal input sha
3fa0: 70 65 66 69 6c 65 20 77 61 73 20 64 6f 77 6e 6c  pefile was downl
3fb0: 6f 61 64 65 64 20 66 72 6f 6d 20 3c 61 20 68 72  oaded from <a hr
3fc0: 65 66 3d 22 68 74 74 70 3a 2f 2f 77 77 77 35 30  ef="http://www50
3fd0: 32 2e 72 65 67 69 6f 6e 65 2e 74 6f 73 63 61 6e  2.regione.toscan
3fe0: 61 2e 69 74 2f 67 65 6f 73 63 6f 70 69 6f 2f 63  a.it/geoscopio/c
3ff0: 61 72 74 6f 74 65 63 61 2e 68 74 6d 6c 22 3e 54  artoteca.html">T
4000: 75 73 63 61 6e 79 20 52 65 67 69 6f 6e 3c 2f 61  uscany Region</a
4010: 3e 20 28 3c 62 3e 3c 69 3e 47 72 61 66 6f 20 53  > (<b><i>Grafo S
4020: 74 72 61 64 61 6c 65 20 63 69 76 69 63 69 2e 73  tradale civici.s
4030: 68 70 3c 2f 69 3e 3c 2f 62 3e 29 20 61 6e 64 20  hp</i></b>) and 
4040: 77 61 73 20 73 75 62 73 65 71 75 65 6e 74 6c 79  was subsequently
4050: 20 72 65 61 72 72 61 6e 67 65 64 20 69 6e 74 6f   rearranged into
4060: 20 61 20 6d 6f 72 65 20 63 6f 6e 76 65 6e 69 65   a more convenie
4070: 6e 74 20 66 6f 72 6d 2e 3c 62 72 3e 0d 0a 54 68  nt form.<br>..Th
4080: 65 20 64 61 74 61 73 65 74 20 69 73 20 72 65 6c  e dataset is rel
4090: 65 61 73 65 64 20 75 6e 64 65 72 20 74 68 65 20  eased under the 
40a0: 3c 62 3e 43 43 2d 42 59 2d 53 41 20 34 2e 30 3c  <b>CC-BY-SA 4.0<
40b0: 2f 62 3e 20 6c 69 63 65 6e 73 65 20 74 65 72 6d  /b> license term
40c0: 73 2e 3c 62 72 3e 0d 0a 54 68 65 20 72 65 66 65  s.<br>..The refe
40d0: 72 65 6e 63 65 20 73 79 73 74 65 6d 20 69 73 20  rence system is 
40e0: 3c 62 3e 33 30 30 33 20 4d 6f 6e 74 65 20 4d 61  <b>3003 Monte Ma
40f0: 72 69 6f 20 2f 20 49 74 61 6c 79 20 7a 6f 6e 65  rio / Italy zone
4100: 20 31 3c 2f 62 3e 3b 20 74 68 69 73 20 69 73 20   1</b>; this is 
4110: 61 20 3c 62 3e 70 6c 61 6e 61 72 3c 2f 62 3e 20  a <b>planar</b> 
4120: 28 3c 69 3e 70 72 6f 6a 65 63 74 65 64 3c 2f 69  (<i>projected</i
4130: 3e 29 20 72 65 66 65 72 65 6e 63 65 20 73 79 73  >) reference sys
4140: 74 65 6d 20 6d 65 61 73 75 72 65 64 20 69 6e 20  tem measured in 
4150: 3c 62 3e 6d 65 74 65 72 73 3c 2f 62 3e 2e 0d 0a  <b>meters</b>...
4160: 3c 62 72 3e 0d 0a 3c 76 65 72 62 61 74 69 6d 3e  <br>..<verbatim>
4170: 0d 0a 53 45 4c 45 43 54 20 61 2e 70 6f 73 20 41  ..SELECT a.pos A
4180: 53 20 72 61 6e 6b 2c 20 61 2e 66 69 64 20 41 53  S rank, a.fid AS
4190: 20 66 69 64 2c 20 61 2e 64 69 73 74 61 6e 63 65   fid, a.distance
41a0: 20 41 53 20 64 69 73 74 5f 6d 2c 20 64 2e 70 72   AS dist_m, d.pr
41b0: 6f 76 20 41 53 20 70 72 6f 76 69 6e 63 65 2c 0d  ov AS province,.
41c0: 0a 20 20 20 20 20 20 20 64 2e 6e 6f 6d 65 20 41  .       d.nome A
41d0: 53 20 6d 75 6e 69 63 69 70 61 6c 69 74 79 2c 20  S municipality, 
41e0: 63 2e 74 6f 70 6f 6e 69 6d 6f 20 41 53 20 73 74  c.toponimo AS st
41f0: 72 65 65 74 5f 6e 61 6d 65 2c 20 62 2e 63 69 76  reet_name, b.civ
4200: 69 63 6f 20 41 53 20 68 6f 75 73 65 5f 6e 75 6d  ico AS house_num
4210: 2c 0d 0a 20 20 20 20 20 20 20 62 2e 67 65 6f 6d  ,..       b.geom
4220: 20 41 53 20 67 65 6f 6d 0d 0a 46 52 4f 4d 20 6b   AS geom..FROM k
4230: 6e 6e 20 41 53 20 61 0d 0a 4a 4f 49 4e 20 63 69  nn AS a..JOIN ci
4240: 76 69 63 69 20 41 53 20 62 20 4f 4e 20 28 62 2e  vici AS b ON (b.
4250: 72 6f 77 69 64 20 3d 20 61 2e 66 69 64 29 0d 0a  rowid = a.fid)..
4260: 4a 4f 49 4e 20 74 6f 70 6f 6e 69 6d 69 20 41 53  JOIN toponimi AS
4270: 20 63 20 4f 4e 20 28 63 2e 66 69 64 20 3d 20 62   c ON (c.fid = b
4280: 2e 69 64 5f 74 6f 70 6f 6e 69 6d 6f 29 0d 0a 4a  .id_toponimo)..J
4290: 4f 49 4e 20 63 6f 6d 75 6e 69 20 41 53 20 64 20  OIN comuni AS d 
42a0: 4f 4e 20 28 64 2e 66 69 64 20 3d 20 63 2e 69 64  ON (d.fid = c.id
42b0: 5f 63 6f 6d 75 6e 65 29 0d 0a 57 48 45 52 45 20  _comune)..WHERE 
42c0: 61 2e 66 5f 74 61 62 6c 65 5f 6e 61 6d 65 20 3d  a.f_table_name =
42d0: 20 27 63 69 76 69 63 69 27 20 41 4e 44 20 61 2e   'civici' AND a.
42e0: 72 65 66 5f 67 65 6f 6d 65 74 72 79 20 3d 20 4d  ref_geometry = M
42f0: 61 6b 65 50 6f 69 6e 74 28 31 37 33 33 30 30 33  akePoint(1733003
4300: 2c 20 34 38 31 36 33 33 32 2c 20 33 30 30 33 29  , 4816332, 3003)
4310: 20 41 4e 44 20 61 2e 6d 61 78 5f 69 74 65 6d 73   AND a.max_items
4320: 20 3d 20 31 30 3b 0d 0a 0d 0a 53 45 4c 45 43 54   = 10;....SELECT
4330: 20 61 2e 70 6f 73 20 41 53 20 72 61 6e 6b 2c 20   a.pos AS rank, 
4340: 61 2e 66 69 64 20 41 53 20 66 69 64 2c 20 61 2e  a.fid AS fid, a.
4350: 64 69 73 74 61 6e 63 65 20 41 53 20 64 69 73 74  distance AS dist
4360: 5f 6d 2c 20 62 2e 70 72 6f 76 20 41 53 20 70 72  _m, b.prov AS pr
4370: 6f 76 69 6e 63 65 2c 0d 0a 20 20 20 20 20 20 20  ovince,..       
4380: 62 2e 63 6f 6d 75 6e 65 20 41 53 20 6d 75 6e 69  b.comune AS muni
4390: 63 69 70 61 6c 69 74 79 2c 20 62 2e 74 6f 70 6f  cipality, b.topo
43a0: 6e 69 6d 6f 20 41 53 20 73 74 72 65 65 74 5f 6e  nimo AS street_n
43b0: 61 6d 65 2c 20 62 2e 63 69 76 69 63 6f 20 41 53  ame, b.civico AS
43c0: 20 68 6f 75 73 65 5f 6e 75 6d 2c 20 0d 0a 20 20   house_num, ..  
43d0: 20 20 20 20 20 62 2e 67 65 6f 6d 20 41 53 20 67       b.geom AS g
43e0: 65 6f 6d 0d 0a 46 52 4f 4d 20 6b 6e 6e 20 41 53  eom..FROM knn AS
43f0: 20 61 0d 0a 4a 4f 49 4e 20 76 77 5f 63 69 76 69   a..JOIN vw_civi
4400: 63 69 20 41 53 20 62 20 4f 4e 20 28 62 2e 72 6f  ci AS b ON (b.ro
4410: 77 69 64 20 3d 20 61 2e 66 69 64 29 0d 0a 57 48  wid = a.fid)..WH
4420: 45 52 45 20 61 2e 66 5f 74 61 62 6c 65 5f 6e 61  ERE a.f_table_na
4430: 6d 65 20 3d 20 27 76 77 5f 63 69 76 69 63 69 27  me = 'vw_civici'
4440: 20 41 4e 44 20 61 2e 72 65 66 5f 67 65 6f 6d 65   AND a.ref_geome
4450: 74 72 79 20 3d 20 4d 61 6b 65 50 6f 69 6e 74 28  try = MakePoint(
4460: 31 37 33 33 30 30 33 2c 20 34 38 31 36 33 33 32  1733003, 4816332
4470: 2c 20 33 30 30 33 29 20 41 4e 44 20 61 2e 6d 61  , 3003) AND a.ma
4480: 78 5f 69 74 65 6d 73 20 3d 20 31 30 3b 0d 0a 3c  x_items = 10;..<
4490: 2f 76 65 72 62 61 74 69 6d 3e 0d 0a 54 68 65 20  /verbatim>..The 
44a0: 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 70 20 63 6f  following map co
44b0: 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 74 68 65  rresponds to the
44c0: 20 61 62 6f 76 65 20 71 75 65 72 69 65 73 3a 20   above queries: 
44d0: 74 68 65 20 6f 72 69 67 69 6e 20 70 6f 69 6e 74  the origin point
44e0: 20 69 73 20 70 6f 73 69 74 69 6f 6e 65 64 20 6f   is positioned o
44f0: 6e 20 61 20 72 6f 61 64 20 6a 75 6e 63 74 69 6f  n a road junctio
4500: 6e 20 69 6e 20 74 68 65 20 63 65 6e 74 72 61 6c  n in the central
4510: 20 61 72 65 61 20 6f 66 20 61 20 64 65 6e 73 65   area of a dense
4520: 6c 79 20 70 6f 70 75 6c 61 74 65 64 20 74 6f 77  ly populated tow
4530: 6e 20 28 41 72 65 7a 7a 6f 29 2e 3c 62 72 3e 0d  n (Arezzo).<br>.
4540: 0a 4e 6f 74 20 73 75 72 70 72 69 73 69 6e 67 6c  .Not surprisingl
4550: 79 20 74 68 65 20 74 65 6e 20 6e 65 61 72 65 73  y the ten neares
4560: 74 20 68 6f 75 73 65 20 6e 75 6d 62 65 72 73 20  t house numbers 
4570: 68 61 76 65 20 62 65 65 6e 20 6c 6f 63 61 74 65  have been locate
4580: 64 20 77 69 74 68 69 6e 20 61 20 72 61 64 69 75  d within a radiu
4590: 73 20 6f 66 20 61 62 6f 75 74 20 31 30 6d 2e 3c  s of about 10m.<
45a0: 62 72 3e 3c 62 72 3e 0d 0a 3c 69 6d 67 20 73 72  br><br>..<img sr
45b0: 63 3d 22 68 74 74 70 73 3a 2f 2f 77 77 77 2e 67  c="https://www.g
45c0: 61 69 61 2d 67 69 73 2e 69 74 2f 67 61 69 61 2d  aia-gis.it/gaia-
45d0: 73 69 6e 73 2f 6b 6e 6e 2f 68 6f 75 73 65 6e 75  sins/knn/housenu
45e0: 6d 31 2e 6a 70 67 22 20 61 6c 74 3d 22 48 6f 75  m1.jpg" alt="Hou
45f0: 73 65 20 4e 75 6d 62 65 72 73 20 23 31 22 3e 0d  se Numbers #1">.
4600: 0a 3c 62 72 3e 3c 62 72 3e 0d 0a 42 6f 74 68 20  .<br><br>..Both 
4610: 71 75 65 72 69 65 73 20 77 69 6c 6c 20 72 65 74  queries will ret
4620: 75 72 6e 20 74 68 65 20 73 61 6d 65 20 69 64 65  urn the same ide
4630: 6e 74 69 63 61 6c 20 72 65 73 75 6c 74 73 2e 3c  ntical results.<
4640: 62 72 3e 0d 0a 41 20 63 6c 6f 73 65 72 20 6c 6f  br>..A closer lo
4650: 6f 6b 20 77 69 6c 6c 20 73 68 6f 77 20 79 6f 75  ok will show you
4660: 20 74 68 61 74 20 74 68 65 20 73 65 63 6f 6e 64   that the second
4670: 20 71 75 65 72 79 20 69 73 20 62 61 73 65 64 20   query is based 
4680: 6f 6e 20 61 20 3c 62 3e 53 70 61 74 69 61 6c 20  on a <b>Spatial 
4690: 56 69 65 77 3c 2f 62 3e 3c 62 72 3e 3c 62 72 3e  View</b><br><br>
46a0: 0d 0a 3c 62 3e 53 75 6d 6d 61 72 79 3c 2f 62 3e  ..<b>Summary</b>
46b0: 3a 20 61 20 56 69 72 74 75 61 6c 4b 4e 4e 20 71  : a VirtualKNN q
46c0: 75 65 72 79 20 63 61 6e 20 69 6e 64 69 66 66 65  uery can indiffe
46d0: 72 65 6e 74 6c 79 20 74 61 72 67 65 74 20 65 69  rently target ei
46e0: 74 68 65 72 20 61 20 47 65 6f 54 61 62 6c 65 20  ther a GeoTable 
46f0: 6f 72 20 61 20 70 72 6f 70 65 72 6c 79 20 72 65  or a properly re
4700: 67 69 73 74 65 72 65 64 20 53 70 61 74 69 61 6c  gistered Spatial
4710: 20 56 69 65 77 2e 3c 62 72 3e 3c 62 72 3e 0d 0a   View.<br><br>..
4720: 3c 76 65 72 62 61 74 69 6d 3e 0d 0a 53 45 4c 45  <verbatim>..SELE
4730: 43 54 20 61 2e 70 6f 73 20 41 53 20 72 61 6e 6b  CT a.pos AS rank
4740: 2c 20 61 2e 66 69 64 20 41 53 20 66 69 64 2c 20  , a.fid AS fid, 
4750: 61 2e 64 69 73 74 61 6e 63 65 20 41 53 20 64 69  a.distance AS di
4760: 73 74 5f 6d 2c 20 62 2e 70 72 6f 76 20 41 53 20  st_m, b.prov AS 
4770: 70 72 6f 76 69 6e 63 65 2c 0d 0a 20 20 20 20 20  province,..     
4780: 20 20 62 2e 63 6f 6d 75 6e 65 20 41 53 20 6d 75    b.comune AS mu
4790: 6e 69 63 69 70 61 6c 69 74 79 2c 20 62 2e 74 6f  nicipality, b.to
47a0: 70 6f 6e 69 6d 6f 20 41 53 20 73 74 72 65 65 74  ponimo AS street
47b0: 5f 6e 61 6d 65 2c 20 62 2e 63 69 76 69 63 6f 20  _name, b.civico 
47c0: 41 53 20 68 6f 75 73 65 5f 6e 75 6d 2c 20 0d 0a  AS house_num, ..
47d0: 20 20 20 20 20 20 20 62 2e 67 65 6f 6d 20 41 53         b.geom AS
47e0: 20 67 65 6f 6d 0d 0a 46 52 4f 4d 20 6b 6e 6e 20   geom..FROM knn 
47f0: 41 53 20 61 0d 0a 4a 4f 49 4e 20 76 77 5f 63 69  AS a..JOIN vw_ci
4800: 76 69 63 69 20 41 53 20 62 20 4f 4e 20 28 62 2e  vici AS b ON (b.
4810: 72 6f 77 69 64 20 3d 20 61 2e 66 69 64 29 20 0d  rowid = a.fid) .
4820: 0a 57 48 45 52 45 20 61 2e 66 5f 74 61 62 6c 65  .WHERE a.f_table
4830: 5f 6e 61 6d 65 20 3d 20 27 63 69 76 69 63 69 27  _name = 'civici'
4840: 20 41 4e 44 20 61 2e 72 65 66 5f 67 65 6f 6d 65   AND a.ref_geome
4850: 74 72 79 20 3d 20 4d 61 6b 65 50 6f 69 6e 74 28  try = MakePoint(
4860: 31 35 39 35 36 32 35 2c 20 34 37 36 37 34 32 30  1595625, 4767420
4870: 2c 20 33 30 30 33 29 20 41 4e 44 20 6d 61 78 5f  , 3003) AND max_
4880: 69 74 65 6d 73 20 3d 20 32 30 3b 0d 0a 3c 2f 76  items = 20;..</v
4890: 65 72 62 61 74 69 6d 3e 0d 0a 54 68 65 20 66 6f  erbatim>..The fo
48a0: 6c 6c 6f 77 69 6e 67 20 6d 61 70 20 63 6f 72 72  llowing map corr
48b0: 65 73 70 6f 6e 64 73 20 74 6f 20 74 68 65 20 61  esponds to the a
48c0: 62 6f 76 65 20 71 75 65 72 79 2e 20 4e 6f 77 20  bove query. Now 
48d0: 74 68 65 20 6f 72 69 67 69 6e 20 70 6f 69 6e 74  the origin point
48e0: 20 69 73 20 70 6f 73 69 74 69 6f 6e 65 64 20 69   is positioned i
48f0: 6e 20 74 68 65 20 54 79 72 72 68 65 6e 69 61 6e  n the Tyrrhenian
4900: 20 53 65 61 2c 20 61 62 6f 75 74 20 3c 62 3e 32   Sea, about <b>2
4910: 36 20 6b 6d 3c 2f 62 3e 20 66 72 6f 6d 20 74 68  6 km</b> from th
4920: 65 20 54 75 73 63 61 6e 79 20 6d 61 69 6e 6c 61  e Tuscany mainla
4930: 6e 64 2c 20 74 68 65 20 49 73 6c 61 6e 64 73 20  nd, the Islands 
4940: 6f 66 20 45 6c 62 61 20 61 6e 64 20 43 61 70 72  of Elba and Capr
4950: 61 69 61 2e 3c 62 72 3e 0d 0a 48 65 72 65 20 74  aia.<br>..Here t
4960: 6f 6f 20 56 69 72 74 75 61 6c 4b 4e 4e 20 69 73  oo VirtualKNN is
4970: 20 63 61 70 61 62 6c 65 20 74 6f 20 69 64 65 6e   capable to iden
4980: 74 69 66 79 20 74 68 65 20 6e 65 61 72 65 73 74  tify the nearest
4990: 20 68 6f 75 73 65 20 6e 75 6d 62 65 72 73 20 69   house numbers i
49a0: 6e 20 6a 75 73 74 20 61 20 66 72 61 63 74 69 6f  n just a fractio
49b0: 6e 20 6f 66 20 61 20 73 65 63 6f 6e 64 2e 3c 62  n of a second.<b
49c0: 72 3e 3c 62 72 3e 0d 0a 3c 69 6d 67 20 73 72 63  r><br>..<img src
49d0: 3d 22 68 74 74 70 73 3a 2f 2f 77 77 77 2e 67 61  ="https://www.ga
49e0: 69 61 2d 67 69 73 2e 69 74 2f 67 61 69 61 2d 73  ia-gis.it/gaia-s
49f0: 69 6e 73 2f 6b 6e 6e 2f 68 6f 75 73 65 6e 75 6d  ins/knn/housenum
4a00: 32 2e 70 6e 67 22 20 61 6c 74 3d 22 48 6f 75 73  2.png" alt="Hous
4a10: 65 20 4e 75 6d 62 65 72 73 20 23 32 22 3e 3c 62  e Numbers #2"><b
4a20: 72 3e 3c 62 72 3e 0d 0a 3c 62 3e 53 75 6d 6d 61  r><br>..<b>Summa
4a30: 72 79 3c 2f 62 3e 3a 20 20 56 69 72 74 75 61 6c  ry</b>:  Virtual
4a40: 4b 4e 4e 20 69 73 20 68 69 67 68 6c 79 20 65 66  KNN is highly ef
4a50: 66 69 63 69 65 6e 74 20 61 6e 64 20 76 65 72 79  ficient and very
4a60: 20 66 61 73 74 2e 20 45 76 65 6e 20 77 68 65 6e   fast. Even when
4a70: 20 71 75 65 72 79 69 6e 67 20 73 75 63 68 20 61   querying such a
4a80: 20 68 75 67 65 20 64 61 74 61 73 65 74 2c 20 77   huge dataset, w
4a90: 68 69 6c 65 20 65 78 70 6c 6f 72 69 6e 67 20 74  hile exploring t
4aa0: 68 65 20 6d 6f 73 74 20 63 6f 6e 74 72 61 73 74  he most contrast
4ab0: 69 6e 67 20 28 76 65 72 79 20 68 69 67 68 20 2f  ing (very high /
4ac0: 20 6c 6f 77 20 64 65 6e 73 69 74 79 29 20 64 61   low density) da
4ad0: 74 61 20 64 69 73 74 72 69 62 75 74 69 6f 6e 73  ta distributions
4ae0: 20 2e 3c 62 72 3e 3c 62 72 3e 0d 0a 3c 74 61 62   .<br><br>..<tab
4af0: 6c 65 20 77 69 64 74 68 3d 22 31 30 30 25 22 3e  le width="100%">
4b00: 3c 74 72 3e 0d 0a 3c 74 64 20 77 69 64 74 68 3d  <tr>..<td width=
4b10: 22 33 33 25 22 20 61 6c 69 67 6e 3d 22 6c 65 66  "33%" align="lef
4b20: 74 22 3e 3c 2f 74 64 3e 0d 0a 3c 74 64 20 61 6c  t"></td>..<td al
4b30: 69 67 6e 3d 22 63 65 6e 74 65 72 22 3e 3c 61 20  ign="center"><a 
4b40: 68 72 65 66 3d 22 68 74 74 70 73 3a 2f 2f 77 77  href="https://ww
4b50: 77 2e 67 61 69 61 2d 67 69 73 2e 69 74 2f 66 6f  w.gaia-gis.it/fo
4b60: 73 73 69 6c 2f 6c 69 62 73 70 61 74 69 61 6c 69  ssil/libspatiali
4b70: 74 65 2f 77 69 6b 69 3f 6e 61 6d 65 3d 6d 69 73  te/wiki?name=mis
4b80: 63 2d 64 6f 63 73 22 3e 62 61 63 6b 20 74 6f 20  c-docs">back to 
4b90: 69 6e 64 65 78 3c 2f 61 3e 3c 2f 74 64 3e 0d 0a  index</a></td>..
4ba0: 3c 74 64 20 77 69 64 74 68 3d 22 33 33 25 22 20  <td width="33%" 
4bb0: 61 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e 3c 2f  align="right"></
4bc0: 74 64 3e 0d 0a 3c 2f 74 72 3e 3c 2f 74 61 62 6c  td>..</tr></tabl
4bd0: 65 3e 0a 5a 20 39 36 30 31 38 36 37 30 61 36 34  e>.Z 96018670a64
4be0: 36 66 66 38 66 32 32 30 34 39 66 33 38 65 39 39  6ff8f22049f38e99
4bf0: 61 34 64 38 34 0a                                a4d84.