Hex Artifact Content
Not logged in

Artifact 4b4b72829e88e524bf75a35a8bd64e931bbb4263:

Wiki page [KNN] by sandro 2017-08-09 06:36:13.
0000: 44 20 32 30 31 37 2d 30 38 2d 30 39 54 30 36 3a  D 2017-08-09T06:
0010: 33 36 3a 31 33 2e 33 35 33 0a 4c 20 4b 4e 4e 0a  36:13.353.L KNN.
0020: 50 20 35 36 63 61 30 34 36 39 34 32 32 32 65 31  P 56ca04694222e1
0030: 62 66 65 32 62 31 37 31 37 39 34 33 34 34 39 61  bfe2b1717943449a
0040: 33 64 37 39 32 35 36 37 66 66 0a 55 20 73 61 6e  3d792567ff.U san
0050: 64 72 6f 0a 57 20 31 39 31 39 31 0a 3c 74 61 62  dro.W 19191.<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 34 2e 33 2e 30 2b 64 6f 63 22 3e 62 61  me=4.3.0+doc">ba
01b0: 63 6b 20 74 6f 20 69 6e 64 65 78 3c 2f 61 3e 3c  ck to index</a><
01c0: 2f 74 64 3e 0d 0a 3c 74 64 20 77 69 64 74 68 3d  /td>..<td width=
01d0: 22 33 33 25 22 20 61 6c 69 67 6e 3d 22 72 69 67  "33%" align="rig
01e0: 68 74 22 3e 3c 2f 74 64 3e 0d 0a 3c 2f 74 72 3e  ht"></td>..</tr>
01f0: 3c 2f 74 61 62 6c 65 3e 3c 62 72 3e 0d 0a 3c 74  </table><br>..<t
0200: 61 62 6c 65 20 62 67 63 6f 6c 6f 72 3d 22 23 64  able bgcolor="#d
0210: 30 65 30 66 66 22 20 77 69 64 74 68 3d 22 31 30  0e0ff" width="10
0220: 30 25 22 3e 0d 0a 3c 74 72 3e 3c 74 64 20 61 6c  0%">..<tr><td al
0230: 69 67 6e 3d 22 63 65 6e 74 65 72 22 3e 0d 0a 3c  ign="center">..<
0240: 62 3e 43 72 65 64 69 74 73 3c 2f 62 3e 3c 62 72  b>Credits</b><br
0250: 3e 0d 0a 44 65 76 65 6c 6f 70 6d 65 6e 74 20 6f  >..Development o
0260: 66 20 3c 62 3e 3c 69 3e 56 69 72 74 75 61 6c 4b  f <b><i>VirtualK
0270: 4e 4e 3c 2f 69 3e 3c 2f 62 3e 20 68 61 73 20 62  NN</i></b> has b
0280: 65 65 6e 20 65 6e 74 69 72 65 6c 79 20 66 75 6e  een entirely fun
0290: 64 65 64 20 62 79 0d 0a 3c 61 20 68 72 65 66 3d  ded by..<a href=
02a0: 22 68 74 74 70 3a 2f 2f 65 6e 2e 77 69 6b 69 70  "http://en.wikip
02b0: 65 64 69 61 2e 6f 72 67 2f 77 69 6b 69 2f 54 75  edia.org/wiki/Tu
02c0: 73 63 61 6e 79 22 3e 54 75 73 63 61 6e 79 20 52  scany">Tuscany R
02d0: 65 67 69 6f 6e 3c 2f 61 3e 20 2d 20 54 65 72 72  egion</a> - Terr
02e0: 69 74 6f 72 69 61 6c 20 61 6e 64 20 45 6e 76 69  itorial and Envi
02f0: 72 6f 6e 6d 65 6e 74 61 6c 20 49 6e 66 6f 72 6d  ronmental Inform
0300: 61 74 69 6f 6e 20 53 79 73 74 65 6d 3c 62 72 3e  ation System<br>
0310: 0d 0a 3c 61 20 68 72 65 66 3d 22 68 74 74 70 3a  ..<a href="http:
0320: 2f 2f 77 77 77 2e 72 65 67 69 6f 6e 65 2e 74 6f  //www.regione.to
0330: 73 63 61 6e 61 2e 69 74 2f 61 6d 62 69 65 6e 74  scana.it/ambient
0340: 65 65 74 65 72 72 69 74 6f 72 69 6f 2f 67 65 6f  eeterritorio/geo
0350: 67 72 61 66 69 61 67 65 6f 6c 6f 67 69 61 2f 69  grafiageologia/i
0360: 6e 64 65 78 2e 68 74 6d 6c 22 3e 52 65 67 69 6f  ndex.html">Regio
0370: 6e 65 20 54 6f 73 63 61 6e 61 3c 2f 61 3e 20 2d  ne Toscana</a> -
0380: 20 53 65 74 74 6f 72 65 20 53 69 73 74 65 6d 61   Settore Sistema
0390: 20 49 6e 66 6f 72 6d 61 74 69 76 6f 20 54 65 72   Informativo Ter
03a0: 72 69 74 6f 72 69 61 6c 65 20 65 64 20 41 6d 62  ritoriale ed Amb
03b0: 69 65 6e 74 61 6c 65 2e 0d 0a 3c 2f 74 64 3e 3c  ientale...</td><
03c0: 2f 74 72 3e 3c 2f 74 61 62 6c 65 3e 3c 62 72 3e  /tr></table><br>
03d0: 0d 0a 3c 68 72 3e 3c 62 72 3e 0d 0a 3c 74 61 62  ..<hr><br>..<tab
03e0: 6c 65 20 77 69 64 74 68 3d 22 31 30 30 25 22 20  le width="100%" 
03f0: 62 67 63 6f 6c 6f 72 3d 22 23 66 66 66 66 64 66  bgcolor="#ffffdf
0400: 22 20 63 65 6c 6c 73 70 61 63 69 6e 67 3d 22 34  " cellspacing="4
0410: 22 20 63 65 6c 6c 70 61 64 64 69 6e 67 3d 22 38  " cellpadding="8
0420: 22 3e 0d 0a 3c 74 72 3e 3c 74 64 3e 3c 62 3e 57  ">..<tr><td><b>W
0430: 68 61 74 20 69 73 20 74 68 65 20 4b 4e 4e 20 28  hat is the KNN (
0440: 3c 69 3e 4b 2d 4e 65 61 72 65 73 74 20 4e 65 69  <i>K-Nearest Nei
0450: 67 68 62 6f 72 73 3c 2f 69 3e 29 20 70 72 6f 62  ghbors</i>) prob
0460: 6c 65 6d 20 3f 3c 2f 62 3e 3c 2f 74 64 3e 3c 2f  lem ?</b></td></
0470: 74 72 3e 0d 0a 3c 74 72 3e 3c 74 64 3e 49 6d 61  tr>..<tr><td>Ima
0480: 67 69 6e 65 20 61 20 73 65 74 20 6f 66 20 61 72  gine a set of ar
0490: 62 69 74 72 61 72 79 20 67 65 6f 6d 65 74 72 69  bitrary geometri
04a0: 65 73 3b 20 77 68 69 63 68 20 6d 61 79 20 77 65  es; which may we
04b0: 6c 6c 20 62 65 2c 20 61 20 76 65 72 79 20 68 75  ll be, a very hu
04c0: 67 65 20 64 61 74 61 73 65 74 20 63 6f 6e 74 61  ge dataset conta
04d0: 69 6e 69 6e 67 20 73 6f 6d 65 20 6d 69 6c 6c 69  ining some milli
04e0: 6f 6e 73 20 6f 66 20 66 65 61 74 75 72 65 73 2e  ons of features.
04f0: 3c 62 72 3e 0d 0a 4e 6f 77 20 69 6d 61 67 69 6e  <br>..Now imagin
0500: 65 20 74 68 61 74 20 66 6f 72 20 77 68 61 74 65  e that for whate
0510: 76 65 72 20 72 65 61 73 6f 6e 20 79 6f 75 20 61  ver reason you a
0520: 72 65 20 69 6e 74 65 72 65 73 74 65 64 20 69 6e  re interested in
0530: 20 71 75 69 63 6b 6c 79 20 69 64 65 6e 74 69 66   quickly identif
0540: 79 69 6e 67 20 61 6c 6c 20 66 65 61 74 75 72 65  ying all feature
0550: 73 20 77 69 74 68 69 6e 20 61 20 20 63 6c 6f 73  s within a  clos
0560: 65 20 73 70 61 74 69 61 6c 20 70 72 6f 78 69 6d  e spatial proxim
0570: 69 74 79 20 74 6f 20 61 6e 20 61 72 62 69 74 72  ity to an arbitr
0580: 61 72 79 20 6c 6f 63 61 74 69 6f 6e 2e 3c 62 72  ary location.<br
0590: 3e 0d 0a 54 68 69 73 20 69 73 20 74 68 65 20 74  >..This is the t
05a0: 79 70 69 63 61 6c 20 3c 62 3e 4b 4e 4e 3c 2f 62  ypical <b>KNN</b
05b0: 3e 20 70 72 6f 62 6c 65 6d 2e 3c 2f 74 64 3e 3c  > problem.</td><
05c0: 2f 74 72 3e 0d 0a 3c 74 72 3e 3c 74 64 3e 4a 75  /tr>..<tr><td>Ju
05d0: 73 74 20 66 65 77 20 70 72 61 63 74 69 63 61 6c  st few practical
05e0: 20 72 65 61 6c 20 77 6f 72 6c 64 20 65 78 61 6d   real world exam
05f0: 70 6c 65 73 3a 0d 0a 3c 75 6c 3e 0d 0a 3c 6c 69  ples:..<ul>..<li
0600: 3e 53 75 70 70 6f 73 65 20 77 68 69 6c 65 20 75  >Suppose while u
0610: 73 69 6e 67 20 61 20 73 6d 61 72 74 70 68 6f 6e  sing a smartphon
0620: 65 20 61 70 70 3a 20 79 6f 75 72 20 63 75 72 72  e app: your curr
0630: 65 6e 74 20 67 65 6f 67 72 61 70 68 69 63 20 6c  ent geographic l
0640: 6f 63 61 74 69 6f 6e 20 69 73 20 77 65 6c 6c 20  ocation is well 
0650: 6b 6e 6f 77 6e 20 62 79 20 74 68 65 20 69 6e 74  known by the int
0660: 65 67 72 61 74 65 64 20 47 50 53 20 73 65 6e 73  egrated GPS sens
0670: 6f 72 2c 20 73 6f 20 79 6f 75 20 61 72 65 20 73  or, so you are s
0680: 69 6d 70 6c 79 20 65 78 70 65 63 74 69 6e 67 20  imply expecting 
0690: 74 68 61 74 20 74 68 65 20 61 70 70 20 77 69 6c  that the app wil
06a0: 6c 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74 65  l immediately te
06b0: 6c 6c 20 77 68 65 72 65 20 79 6f 75 20 61 72 65  ll where you are
06c0: 20 28 61 6e 64 20 74 68 65 20 65 78 61 63 74 20   (and the exact 
06d0: 6c 6f 63 61 74 69 6f 6e 20 6f 66 29 20 74 68 65  location of) the
06e0: 20 6e 65 61 72 65 73 74 20 66 75 65 6c 20 73 74   nearest fuel st
06f0: 61 74 69 6f 6e 73 20 28 68 6f 74 65 6c 73 2c 20  ations (hotels, 
0700: 72 65 73 74 61 75 72 61 6e 74 73 20 65 74 63 29  restaurants etc)
0710: 2e 3c 2f 6c 69 3e 0d 0a 3c 6c 69 3e 53 75 70 70  .</li>..<li>Supp
0720: 6f 73 65 20 61 20 7a 6f 6f 6c 6f 67 69 63 61 6c  ose a zoological
0730: 2f 65 63 6f 6c 6f 67 69 63 61 6c 20 73 74 75 64  /ecological stud
0740: 79 3a 20 79 6f 75 20 6b 6e 6f 77 20 77 68 65 72  y: you know wher
0750: 65 20 61 72 65 20 6c 6f 63 61 74 65 64 20 74 68  e are located th
0760: 65 20 6e 65 73 74 69 6e 67 20 61 72 65 61 73 20  e nesting areas 
0770: 6f 66 20 73 6f 6d 65 20 72 61 72 65 20 77 61 74  of some rare wat
0780: 65 72 20 62 69 72 64 20 73 70 65 63 69 65 73 2c  er bird species,
0790: 20 61 6c 73 6f 20 74 68 65 20 6c 6f 63 61 74 69   also the locati
07a0: 6f 6e 20 6f 66 20 61 6c 6c 20 72 69 76 65 72 73  on of all rivers
07b0: 2c 20 6c 61 6b 65 73 2c 20 70 6f 6e 64 73 20 61  , lakes, ponds a
07c0: 6e 64 20 6d 61 72 73 68 6c 61 6e 64 73 20 69 6e  nd marshlands in
07d0: 20 74 68 65 20 61 72 65 61 20 79 6f 75 20 61 72   the area you ar
07e0: 65 20 69 6e 74 65 72 65 73 74 65 64 20 69 6e 2e  e interested in.
07f0: 3c 62 72 3e 0d 0a 4e 6f 77 20 79 6f 75 20 61 72  <br>..Now you ar
0800: 65 20 61 74 74 65 6d 70 74 69 6e 67 20 74 6f 20  e attempting to 
0810: 67 65 74 20 61 20 72 65 61 73 6f 6e 61 62 6c 65  get a reasonable
0820: 20 61 73 73 6f 63 69 61 74 69 6f 6e 20 62 65 74   association bet
0830: 77 65 65 6e 20 6e 65 73 74 69 6e 67 20 61 72 65  ween nesting are
0840: 61 73 20 61 6e 64 20 66 65 65 64 69 6e 67 20 7a  as and feeding z
0850: 6f 6e 65 73 20 62 61 73 65 64 20 6f 6e 20 6d 69  ones based on mi
0860: 6e 69 6d 61 6c 20 64 69 73 74 61 6e 63 65 20 63  nimal distance c
0870: 72 69 74 65 72 69 61 2e 3c 2f 6c 69 3e 0d 0a 3c  riteria.</li>..<
0880: 2f 75 6c 3e 0d 0a 3c 2f 74 64 3e 3c 2f 74 72 3e  /ul>..</td></tr>
0890: 0d 0a 3c 74 72 3e 3c 74 64 3e 57 68 65 72 65 62  ..<tr><td>Whereb
08a0: 79 2c 20 69 6e 20 62 6f 74 68 20 63 61 73 65 73  y, in both cases
08b0: 20 79 6f 75 20 63 61 6e 27 74 20 73 61 66 65 6c   you can't safel
08c0: 79 20 69 64 65 6e 74 69 66 79 20 61 20 72 65 61  y identify a rea
08d0: 73 6f 6e 61 62 6c 65 20 3c 69 3e 3c 75 3e 6d 69  sonable <i><u>mi
08e0: 6e 69 6d 61 6c 20 72 61 64 69 75 73 3c 2f 75 3e  nimal radius</u>
08f0: 3c 2f 69 3e 20 73 6f 20 74 6f 20 75 73 65 66 75  </i> so to usefu
0900: 6c 6c 79 20 72 65 73 74 72 69 63 74 20 79 6f 75  lly restrict you
0910: 72 20 73 65 61 72 63 68 20 62 79 2c 20 73 69 6e  r search by, sin
0920: 63 65 20 79 6f 75 20 63 61 6e 27 74 20 6d 61 6b  ce you can't mak
0930: 65 20 61 6e 79 20 76 61 6c 69 64 20 61 73 73 75  e any valid assu
0940: 6d 70 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65  mption about the
0950: 20 67 65 6f 67 72 61 70 68 69 63 20 64 69 73 70   geographic disp
0960: 65 72 73 69 6f 6e 20 6f 66 20 79 6f 75 72 20 74  ersion of your t
0970: 61 72 67 65 74 73 2e 3c 62 72 3e 0d 0a 54 68 65  argets.<br>..The
0980: 20 64 69 73 74 61 6e 63 65 73 20 6d 61 79 20 6e   distances may n
0990: 6f 74 69 63 65 61 62 6c 79 20 76 61 72 79 20 66  oticeably vary f
09a0: 72 6f 6d 20 63 61 73 65 20 74 6f 20 63 61 73 65  rom case to case
09b0: 2c 20 61 6e 64 20 79 6f 75 20 68 61 76 65 6e 27  , and you haven'
09c0: 74 20 61 6e 79 20 69 64 65 61 20 61 62 6f 75 74  t any idea about
09d0: 20 74 68 69 73 2c 20 73 69 6e 63 65 20 69 74 20   this, since it 
09e0: 73 74 72 69 63 74 6c 79 20 64 65 70 65 6e 64 73  strictly depends
09f0: 20 6f 6e 20 74 68 65 20 61 63 74 75 61 6c 20 6c   on the actual l
0a00: 6f 63 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73  ocation of the s
0a10: 65 61 72 63 68 20 6f 72 69 67 69 6e 20 61 6e 64  earch origin and
0a20: 20 6f 6e 20 74 68 65 20 73 74 61 74 69 73 74 69   on the statisti
0a30: 63 61 6c 20 64 69 73 70 65 72 73 69 6f 6e 20 6f  cal dispersion o
0a40: 66 20 74 68 65 20 74 61 72 67 65 74 73 20 74 6f  f the targets to
0a50: 20 62 65 20 73 65 61 72 63 68 65 64 2e 3c 2f 74   be searched.</t
0a60: 64 3e 3c 2f 74 72 3e 0d 0a 3c 2f 74 61 62 6c 65  d></tr>..</table
0a70: 3e 3c 62 72 3e 0d 0a 3c 68 72 3e 3c 62 72 3e 0d  ><br>..<hr><br>.
0a80: 0a 3c 74 61 62 6c 65 20 77 69 64 74 68 3d 22 31  .<table width="1
0a90: 30 30 25 22 3e 0d 0a 3c 74 72 3e 3c 74 64 20 76  00%">..<tr><td v
0aa0: 61 6c 69 67 6e 3d 22 6d 69 64 64 6c 65 22 3e 0d  align="middle">.
0ab0: 0a 59 6f 75 20 63 61 6e 20 3c 61 20 68 72 65 66  .You can <a href
0ac0: 3d 22 68 74 74 70 73 3a 2f 2f 77 77 77 2e 67 61  ="https://www.ga
0ad0: 69 61 2d 67 69 73 2e 69 74 2f 67 61 69 61 2d 73  ia-gis.it/gaia-s
0ae0: 69 6e 73 2f 6b 6e 6e 2f 61 69 72 70 6f 72 74 73  ins/knn/airports
0af0: 2e 37 7a 22 3e 64 6f 77 6e 6c 6f 61 64 3c 2f 61  .7z">download</a
0b00: 3e 20 61 20 73 61 6d 70 6c 65 20 64 62 2d 66 69  > a sample db-fi
0b10: 6c 65 20 65 78 61 63 74 6c 79 20 63 6f 72 72 65  le exactly corre
0b20: 73 70 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65 20  sponding to the 
0b30: 65 78 61 6d 70 6c 65 73 20 77 65 27 6c 6c 20 75  examples we'll u
0b40: 73 65 20 69 6e 20 74 68 69 73 20 66 69 72 73 74  se in this first
0b50: 20 74 75 74 6f 72 69 61 6c 2e 3c 62 72 3e 3c 62   tutorial.<br><b
0b60: 72 3e 0d 0a 54 68 69 73 20 64 61 74 61 73 65 74  r>..This dataset
0b70: 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20   corresponds to 
0b80: 61 20 63 6f 6c 6c 65 63 74 69 6f 6e 20 6f 66 20  a collection of 
0b90: 61 62 6f 75 74 20 3c 62 3e 33 30 2e 30 30 30 20  about <b>30.000 
0ba0: 61 69 72 70 6f 72 74 73 3c 2f 62 3e 20 73 63 61  airports</b> sca
0bb0: 74 74 65 72 65 64 20 61 6c 6c 20 6f 76 65 72 20  ttered all over 
0bc0: 74 68 65 20 77 6f 72 6c 64 3b 20 74 68 65 20 6f  the world; the o
0bd0: 72 69 67 69 6e 61 6c 20 69 6e 70 75 74 20 73 68  riginal input sh
0be0: 61 70 65 66 69 6c 65 20 77 61 73 20 64 6f 77 6e  apefile was down
0bf0: 6c 6f 61 64 65 64 20 66 72 6f 6d 20 3c 61 20 68  loaded from <a h
0c00: 72 65 66 3d 22 68 74 74 70 73 3a 2f 2f 77 77 77  ref="https://www
0c10: 2e 73 68 61 72 65 67 65 6f 2e 61 63 2e 75 6b 2f  .sharegeo.ac.uk/
0c20: 68 61 6e 64 6c 65 2f 31 30 36 37 32 2f 33 30 33  handle/10672/303
0c30: 22 3e 68 65 72 65 3c 2f 61 3e 3c 62 72 3e 0d 0a  ">here</a><br>..
0c40: 54 68 65 20 72 65 66 65 72 65 6e 63 65 20 73 79  The reference sy
0c50: 73 74 65 6d 20 69 73 20 3c 62 3e 34 33 32 36 20  stem is <b>4326 
0c60: 57 47 53 38 34 3c 2f 62 3e 20 28 62 61 73 65 64  WGS84</b> (based
0c70: 20 6f 6e 20 3c 69 3e 6c 6f 6e 67 69 74 75 64 65   on <i>longitude
0c80: 3c 2f 69 3e 20 61 6e 64 20 3c 69 3e 6c 61 74 69  </i> and <i>lati
0c90: 74 75 64 65 3c 2f 69 3e 20 61 6e 67 6c 65 73 29  tude</i> angles)
0ca0: 2e 0d 0a 3c 62 72 3e 3c 62 72 3e 3c 62 72 3e 3c  ...<br><br><br><
0cb0: 62 72 3e 0d 0a 54 68 65 20 6d 61 70 20 6f 6e 20  br>..The map on 
0cc0: 74 68 65 20 72 69 67 68 74 20 73 68 6f 77 73 20  the right shows 
0cd0: 74 68 65 20 67 65 6f 67 72 61 70 68 69 63 20 63  the geographic c
0ce0: 6f 6e 74 65 78 74 20 6f 66 20 74 68 69 73 20 66  ontext of this f
0cf0: 69 72 73 74 20 4b 4e 4e 20 65 78 61 6d 70 6c 65  irst KNN example
0d00: 2e 3c 2f 74 64 3e 0d 0a 3c 74 64 3e 3c 69 6d 67  .</td>..<td><img
0d10: 20 73 72 63 3d 22 68 74 74 70 73 3a 2f 2f 77 77   src="https://ww
0d20: 77 2e 67 61 69 61 2d 67 69 73 2e 69 74 2f 67 61  w.gaia-gis.it/ga
0d30: 69 61 2d 73 69 6e 73 2f 6b 6e 6e 2f 74 79 72 72  ia-sins/knn/tyrr
0d40: 68 65 6e 69 61 6e 2d 73 65 61 2e 70 6e 67 22 20  henian-sea.png" 
0d50: 61 6c 74 3d 22 54 79 72 72 68 65 6e 69 61 6e 20  alt="Tyrrhenian 
0d60: 53 65 61 22 3e 3c 2f 74 64 3e 0d 0a 3c 2f 74 61  Sea"></td>..</ta
0d70: 62 6c 65 3e 3c 62 72 3e 0d 0a 3c 68 32 3e 41 20  ble><br>..<h2>A 
0d80: 66 69 72 73 74 2c 20 73 69 6d 70 6c 65 20 61 6e  first, simple an
0d90: 64 20 76 65 72 79 20 69 6e 65 66 66 69 63 69 65  d very inefficie
0da0: 6e 74 2c 20 61 70 70 72 6f 61 63 68 3c 2f 68 32  nt, approach</h2
0db0: 3e 0d 0a 3c 76 65 72 62 61 74 69 6d 3e 0d 0a 53  >..<verbatim>..S
0dc0: 45 4c 45 43 54 20 67 65 6f 6e 61 6d 65 69 64 2c  ELECT geonameid,
0dd0: 20 6e 61 6d 65 2c 20 63 6f 75 6e 74 72 79 2c 20   name, country, 
0de0: 0d 0a 20 20 20 20 4d 69 6e 28 53 54 5f 44 69 73  ..    Min(ST_Dis
0df0: 74 61 6e 63 65 28 4d 61 6b 65 50 6f 69 6e 74 28  tance(MakePoint(
0e00: 31 30 2c 20 34 33 29 2c 20 67 65 6f 6d 2c 20 31  10, 43), geom, 1
0e10: 29 29 20 2f 20 31 30 30 30 2e 30 20 41 53 20 64  )) / 1000.0 AS d
0e20: 69 73 74 5f 6b 6d 0d 0a 46 52 4f 4d 20 61 69 72  ist_km..FROM air
0e30: 70 6f 72 74 73 3b 0d 0a 2d 2d 2d 2d 2d 2d 2d 2d  ports;..--------
0e40: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0e50: 2d 2d 2d 2d 2d 2d 2d 2d 0d 0a 36 32 39 39 36 32  --------..629962
0e60: 33 09 7c 20 4d 61 72 69 6e 61 20 44 69 20 43 61  3.| Marina Di Ca
0e70: 6d 70 6f 20 7c 20 49 54 20 7c 20 33 33 2e 30 34  mpo | IT | 33.04
0e80: 33 33 32 30 0d 0a 0d 0a 53 45 4c 45 43 54 20 67  3320....SELECT g
0e90: 65 6f 6e 61 6d 65 69 64 2c 20 6e 61 6d 65 2c 20  eonameid, name, 
0ea0: 63 6f 75 6e 74 72 79 2c 20 0d 0a 20 20 20 20 4d  country, ..    M
0eb0: 69 6e 28 53 54 5f 44 69 73 74 61 6e 63 65 28 4d  in(ST_Distance(M
0ec0: 61 6b 65 50 6f 69 6e 74 28 31 30 2c 20 34 33 29  akePoint(10, 43)
0ed0: 2c 20 67 65 6f 6d 2c 20 31 29 29 20 2f 20 31 30  , geom, 1)) / 10
0ee0: 30 30 2e 30 20 41 53 20 64 69 73 74 5f 6b 6d 0d  00.0 AS dist_km.
0ef0: 0a 46 52 4f 4d 20 61 69 72 70 6f 72 74 73 0d 0a  .FROM airports..
0f00: 57 48 45 52 45 20 67 65 6f 6e 61 6d 65 69 64 20  WHERE geonameid 
0f10: 4e 4f 54 20 49 4e 20 28 36 32 39 39 36 32 33 29  NOT IN (6299623)
0f20: 3b 0d 0a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ;..-------------
0f30: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0f40: 2d 2d 2d 2d 0d 0a 36 32 39 39 33 39 32 20 7c 20  ----..6299392 | 
0f50: 42 61 73 74 69 61 2d 50 6f 72 65 74 74 61 20 7c  Bastia-Poretta |
0f60: 20 46 52 20 7c 20 36 35 2e 32 32 36 35 37 33 0d   FR | 65.226573.
0f70: 0a 0d 0a 53 45 4c 45 43 54 20 67 65 6f 6e 61 6d  ...SELECT geonam
0f80: 65 69 64 2c 20 6e 61 6d 65 2c 20 63 6f 75 6e 74  eid, name, count
0f90: 72 79 2c 0d 0a 20 20 20 20 4d 69 6e 28 53 54 5f  ry,..    Min(ST_
0fa0: 44 69 73 74 61 6e 63 65 28 4d 61 6b 65 50 6f 69  Distance(MakePoi
0fb0: 6e 74 28 31 30 2c 20 34 33 29 2c 20 67 65 6f 6d  nt(10, 43), geom
0fc0: 2c 20 31 29 29 20 2f 20 31 30 30 30 2e 30 20 41  , 1)) / 1000.0 A
0fd0: 53 20 64 69 73 74 5f 6b 6d 0d 0a 46 52 4f 4d 20  S dist_km..FROM 
0fe0: 61 69 72 70 6f 72 74 73 0d 0a 57 48 45 52 45 20  airports..WHERE 
0ff0: 67 65 6f 6e 61 6d 65 69 64 20 4e 4f 54 20 49 4e  geonameid NOT IN
1000: 20 28 36 32 39 39 36 32 33 2c 20 36 32 39 39 33   (6299623, 62993
1010: 39 32 29 3b 0d 0a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  92);..----------
1020: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1030: 2d 2d 2d 2d 2d 2d 2d 0d 0a 36 32 39 39 36 32 38  -------..6299628
1040: 20 7c 20 50 69 73 61 20 2f 20 53 2e 20 47 69 75   | Pisa / S. Giu
1050: 73 74 6f 20 7c 20 49 54 09 7c 20 38 32 2e 33 38  sto | IT.| 82.38
1060: 37 30 31 34 0d 0a 0d 0a 53 45 4c 45 43 54 20 67  7014....SELECT g
1070: 65 6f 6e 61 6d 65 69 64 2c 20 6e 61 6d 65 2c 20  eonameid, name, 
1080: 63 6f 75 6e 74 72 79 2c 20 0d 0a 20 20 20 20 4d  country, ..    M
1090: 69 6e 28 53 54 5f 44 69 73 74 61 6e 63 65 28 4d  in(ST_Distance(M
10a0: 61 6b 65 50 6f 69 6e 74 28 31 30 2c 20 34 33 29  akePoint(10, 43)
10b0: 2c 20 67 65 6f 6d 2c 20 31 29 29 20 2f 20 31 30  , geom, 1)) / 10
10c0: 30 30 2e 30 20 41 53 20 64 69 73 74 5f 6b 6d 0d  00.0 AS dist_km.
10d0: 0a 46 52 4f 4d 20 61 69 72 70 6f 72 74 73 0d 0a  .FROM airports..
10e0: 57 48 45 52 45 20 67 65 6f 6e 61 6d 65 69 64 20  WHERE geonameid 
10f0: 4e 4f 54 20 49 4e 20 28 36 32 39 39 36 32 33 2c  NOT IN (6299623,
1100: 20 36 32 39 39 33 39 32 2c 20 36 32 39 39 36 32   6299392, 629962
1110: 38 29 3b 0d 0a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  8);..-----------
1120: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1130: 2d 2d 2d 2d 2d 2d 0d 0a 36 32 39 39 36 33 30 20  ------..6299630 
1140: 7c 20 47 72 6f 73 73 65 74 6f 20 41 69 72 70 6f  | Grosseto Airpo
1150: 72 74 20 7c 20 49 54 20 7c 20 39 31 2e 35 34 39  rt | IT | 91.549
1160: 37 37 33 0d 0a 0d 0a 53 45 4c 45 43 54 20 67 65  773....SELECT ge
1170: 6f 6e 61 6d 65 69 64 2c 20 6e 61 6d 65 2c 20 63  onameid, name, c
1180: 6f 75 6e 74 72 79 2c 20 0d 0a 20 20 20 20 4d 69  ountry, ..    Mi
1190: 6e 28 53 54 5f 44 69 73 74 61 6e 63 65 28 4d 61  n(ST_Distance(Ma
11a0: 6b 65 50 6f 69 6e 74 28 31 30 2c 20 34 33 29 2c  kePoint(10, 43),
11b0: 20 67 65 6f 6d 2c 20 31 29 29 20 2f 20 31 30 30   geom, 1)) / 100
11c0: 30 2e 30 20 41 53 20 64 69 73 74 5f 6b 6d 0d 0a  0.0 AS dist_km..
11d0: 46 52 4f 4d 20 61 69 72 70 6f 72 74 73 0d 0a 57  FROM airports..W
11e0: 48 45 52 45 20 67 65 6f 6e 61 6d 65 69 64 20 4e  HERE geonameid N
11f0: 4f 54 20 49 4e 20 28 36 32 39 39 36 32 33 2c 20  OT IN (6299623, 
1200: 36 32 39 39 33 39 32 2c 20 36 32 39 39 36 32 38  6299392, 6299628
1210: 2c 20 36 32 39 39 36 33 30 29 3b 0d 0a 2d 2d 2d  , 6299630);..---
1220: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1230: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0d 0a  --------------..
1240: 36 36 39 34 34 39 35 20 7c 20 43 6f 72 74 65 20  6694495 | Corte 
1250: 7c 20 46 52 20 7c 20 31 30 32 2e 38 31 39 37 37  | FR | 102.81977
1260: 38 0d 0a 3c 2f 76 65 72 62 61 74 69 6d 3e 0d 0a  8..</verbatim>..
1270: 57 65 20 63 6f 75 6c 64 20 65 66 66 65 63 74 69  We could effecti
1280: 76 65 6c 79 20 65 78 65 63 75 74 65 20 61 20 66  vely execute a f
1290: 69 72 73 74 20 53 51 4c 20 71 75 65 72 79 20 75  irst SQL query u
12a0: 73 69 6e 67 20 74 68 65 20 3c 62 3e 4d 69 6e 28  sing the <b>Min(
12b0: 29 3c 2f 62 3e 20 61 67 67 72 65 67 61 74 65 20  )</b> aggregate 
12c0: 66 75 6e 63 74 69 6f 6e 20 69 6e 20 6f 72 64 65  function in orde
12d0: 72 20 74 6f 20 69 64 65 6e 74 69 66 79 20 74 68  r to identify th
12e0: 65 20 63 6c 6f 73 65 73 74 20 61 69 72 70 6f 72  e closest airpor
12f0: 74 20 74 6f 20 61 6e 20 61 72 62 69 74 72 61 72  t to an arbitrar
1300: 79 20 70 6f 73 69 74 69 6f 6e 20 28 3c 69 3e 6c  y position (<i>l
1310: 61 74 69 74 75 64 65 3c 2f 69 3e 3d 3c 62 3e 34  atitude</i>=<b>4
1320: 33 2e 30 3c 2f 62 3e 2c 20 3c 69 3e 6c 6f 6e 67  3.0</b>, <i>long
1330: 69 74 75 64 65 3c 2f 69 3e 3d 3c 62 3e 31 30 2e  itude</i>=<b>10.
1340: 30 3c 2f 62 3e 29 20 6c 6f 63 61 74 65 64 20 69  0</b>) located i
1350: 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20  n the middle of 
1360: 74 68 65 20 54 79 72 72 68 65 6e 69 61 6e 20 53  the Tyrrhenian S
1370: 65 61 2e 3c 62 72 3e 0d 0a 54 68 65 6e 20 77 65  ea.<br>..Then we
1380: 20 63 6f 75 6c 64 20 63 6f 6e 74 69 6e 75 61 6c   could continual
1390: 6c 79 20 72 65 70 65 61 74 20 74 68 65 20 69 64  ly repeat the id
13a0: 65 6e 74 69 63 61 6c 20 71 75 65 72 79 2c 20 65  entical query, e
13b0: 61 63 68 20 74 69 6d 65 20 65 78 63 6c 75 64 69  ach time excludi
13c0: 6e 67 20 61 6c 6c 20 61 69 72 70 6f 72 74 73 20  ng all airports 
13d0: 77 65 27 76 65 20 61 6c 72 65 61 64 79 20 69 64  we've already id
13e0: 65 6e 74 69 66 69 65 64 20 69 6e 20 74 68 65 20  entified in the 
13f0: 70 72 65 76 69 6f 75 73 20 70 72 6f 63 65 73 73  previous process
1400: 65 64 20 73 74 65 70 73 2e 3c 62 72 3e 0d 0a 54  ed steps.<br>..T
1410: 68 65 20 66 69 6e 61 6c 20 72 65 73 75 6c 74 20  he final result 
1420: 77 69 6c 6c 20 62 65 20 61 20 66 75 6c 6c 20 6c  will be a full l
1430: 69 73 74 20 6f 66 20 74 68 65 20 66 69 72 73 74  ist of the first
1440: 20 66 69 76 65 20 61 69 72 70 6f 72 74 73 20 63   five airports c
1450: 6c 6f 73 65 73 74 20 74 6f 20 74 68 65 20 72 65  losest to the re
1460: 66 65 72 65 6e 63 65 20 6c 6f 63 61 74 69 6f 6e  ference location
1470: 20 6f 72 64 65 72 65 64 20 62 79 20 69 6e 63 72   ordered by incr
1480: 65 61 73 69 6e 67 20 64 69 73 74 61 6e 63 65 2e  easing distance.
1490: 0d 0a 3c 62 72 3e 3c 62 72 3e 0d 0a 3c 74 61 62  ..<br><br>..<tab
14a0: 6c 65 20 62 67 63 6f 6c 6f 72 3d 22 23 64 30 66  le bgcolor="#d0f
14b0: 66 62 30 22 20 63 65 6c 6c 73 70 61 63 69 6e 67  fb0" cellspacing
14c0: 3d 22 34 22 20 63 65 6c 6c 70 61 64 64 69 6e 67  ="4" cellpadding
14d0: 3d 22 34 22 20 62 6f 72 64 65 72 3d 22 31 22 3e  ="4" border="1">
14e0: 0d 0a 3c 74 72 3e 3c 74 64 20 62 67 63 6f 6c 6f  ..<tr><td bgcolo
14f0: 72 3d 22 23 63 30 64 64 61 6f 22 3e 3c 62 3e 72  r="#c0ddao"><b>r
1500: 61 6e 6b 3c 2f 62 3e 3c 2f 74 64 3e 3c 74 64 20  ank</b></td><td 
1510: 62 67 63 6f 6c 6f 72 3d 22 23 63 30 64 64 61 6f  bgcolor="#c0ddao
1520: 22 3e 3c 62 3e 67 65 6f 6e 61 6d 65 69 64 3c 2f  "><b>geonameid</
1530: 62 3e 3c 2f 74 64 3e 3c 74 64 20 62 67 63 6f 6c  b></td><td bgcol
1540: 6f 72 3d 22 23 63 30 64 64 61 6f 22 3e 3c 62 3e  or="#c0ddao"><b>
1550: 6e 61 6d 65 3c 2f 62 3e 3c 2f 74 64 3e 3c 74 64  name</b></td><td
1560: 20 62 67 63 6f 6c 6f 72 3d 22 23 63 30 64 64 61   bgcolor="#c0dda
1570: 6f 22 3e 3c 62 3e 63 6f 75 6e 74 72 79 3c 2f 62  o"><b>country</b
1580: 3e 3c 2f 74 64 3e 3c 74 64 20 62 67 63 6f 6c 6f  ></td><td bgcolo
1590: 72 3d 22 23 63 30 64 64 61 6f 22 3e 3c 62 3e 64  r="#c0ddao"><b>d
15a0: 69 73 74 5f 6b 6d 3c 2f 62 3e 3c 2f 74 64 3e 3c  ist_km</b></td><
15b0: 2f 74 72 3e 0d 0a 3c 74 72 3e 3c 74 64 20 61 6c  /tr>..<tr><td al
15c0: 69 67 6e 3d 22 72 69 67 68 74 22 3e 31 3c 2f 74  ign="right">1</t
15d0: 64 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69 67  d><td align="rig
15e0: 68 74 22 3e 36 32 39 39 36 32 33 3c 2f 74 64 3e  ht">6299623</td>
15f0: 3c 74 64 3e 4d 61 72 69 6e 61 20 44 69 20 43 61  <td>Marina Di Ca
1600: 6d 70 6f 3c 2f 74 64 3e 3c 74 64 3e 49 54 3c 2f  mpo</td><td>IT</
1610: 74 64 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69  td><td align="ri
1620: 67 68 74 22 3e 33 33 2e 30 34 33 33 32 30 3c 2f  ght">33.043320</
1630: 74 64 3e 3c 2f 74 72 3e 0d 0a 3c 74 72 3e 3c 74  td></tr>..<tr><t
1640: 64 20 61 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e  d align="right">
1650: 32 3c 2f 74 64 3e 3c 74 64 20 61 6c 69 67 6e 3d  2</td><td align=
1660: 22 72 69 67 68 74 22 3e 36 32 39 39 33 39 32 3c  "right">6299392<
1670: 2f 74 64 3e 3c 74 64 3e 42 61 73 74 69 61 2d 50  /td><td>Bastia-P
1680: 6f 72 65 74 74 61 3c 2f 74 64 3e 3c 74 64 3e 46  oretta</td><td>F
1690: 52 3c 2f 74 64 3e 3c 74 64 20 61 6c 69 67 6e 3d  R</td><td align=
16a0: 22 72 69 67 68 74 22 3e 36 35 2e 32 32 36 35 37  "right">65.22657
16b0: 33 3c 2f 74 64 3e 3c 2f 74 72 3e 0d 0a 3c 74 72  3</td></tr>..<tr
16c0: 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69 67 68  ><td align="righ
16d0: 74 22 3e 33 3c 2f 74 64 3e 3c 74 64 20 61 6c 69  t">3</td><td ali
16e0: 67 6e 3d 22 72 69 67 68 74 22 3e 36 32 39 39 36  gn="right">62996
16f0: 32 38 3c 2f 74 64 3e 3c 74 64 3e 50 69 73 61 20  28</td><td>Pisa 
1700: 2f 20 53 2e 20 47 69 75 73 74 6f 3c 2f 74 64 3e  / S. Giusto</td>
1710: 3c 74 64 3e 49 54 3c 2f 74 64 3e 3c 74 64 20 61  <td>IT</td><td a
1720: 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e 38 32 2e  lign="right">82.
1730: 33 38 37 30 31 34 3c 2f 74 64 3e 3c 2f 74 72 3e  387014</td></tr>
1740: 0d 0a 3c 74 72 3e 3c 74 64 20 61 6c 69 67 6e 3d  ..<tr><td align=
1750: 22 72 69 67 68 74 22 3e 34 3c 2f 74 64 3e 3c 74  "right">4</td><t
1760: 64 20 61 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e  d align="right">
1770: 36 32 39 39 36 33 30 3c 2f 74 64 3e 3c 74 64 3e  6299630</td><td>
1780: 47 72 6f 73 73 65 74 6f 20 41 69 72 70 6f 72 74  Grosseto Airport
1790: 3c 2f 74 64 3e 3c 74 64 3e 49 54 3c 2f 74 64 3e  </td><td>IT</td>
17a0: 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69 67 68 74  <td align="right
17b0: 22 3e 39 31 2e 35 34 39 37 37 33 3c 2f 74 64 3e  ">91.549773</td>
17c0: 3c 2f 74 72 3e 0d 0a 3c 74 72 3e 3c 74 64 20 61  </tr>..<tr><td a
17d0: 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e 35 3c 2f  lign="right">5</
17e0: 74 64 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69  td><td align="ri
17f0: 67 68 74 22 3e 36 36 39 34 34 39 35 3c 2f 74 64  ght">6694495</td
1800: 3e 3c 74 64 3e 43 6f 72 74 65 3c 2f 74 64 3e 3c  ><td>Corte</td><
1810: 74 64 3e 46 52 3c 2f 74 64 3e 3c 74 64 20 61 6c  td>FR</td><td al
1820: 69 67 6e 3d 22 72 69 67 68 74 22 3e 31 30 32 2e  ign="right">102.
1830: 38 31 39 37 37 38 3c 2f 74 64 3e 3c 2f 74 72 3e  819778</td></tr>
1840: 0d 0a 3c 2f 74 61 62 6c 65 3e 3c 62 72 3e 0d 0a  ..</table><br>..
1850: 53 75 63 68 20 61 20 73 69 6d 70 6c 65 20 61 70  Such a simple ap
1860: 70 72 6f 61 63 68 20 69 73 20 68 69 67 68 6c 79  proach is highly
1870: 20 69 6d 70 72 61 63 74 69 63 61 6c 2c 20 62 65   impractical, be
1880: 63 61 75 73 65 20 69 74 20 73 74 72 69 63 74 6c  cause it strictl
1890: 79 20 72 65 71 75 69 72 65 73 20 74 6f 20 68 61  y requires to ha
18a0: 6e 64 2d 77 72 69 74 65 20 73 65 76 65 72 61 6c  nd-write several
18b0: 20 3c 69 3e 61 64 20 68 6f 63 3c 2f 69 3e 20 53   <i>ad hoc</i> S
18c0: 51 4c 20 71 75 65 72 69 65 73 20 6f 6e 65 20 62  QL queries one b
18d0: 79 20 6f 6e 65 2e 3c 62 72 3e 0d 0a 57 72 69 74  y one.<br>..Writ
18e0: 69 6e 67 20 61 20 66 75 6c 6c 79 20 61 75 74 6f  ing a fully auto
18f0: 6d 61 74 65 64 20 53 51 4c 20 73 63 72 69 70 74  mated SQL script
1900: 20 69 73 6e 27 74 20 61 74 20 61 6c 6c 20 61 20   isn't at all a 
1910: 73 69 6d 70 6c 65 20 74 61 73 6b 2c 20 61 6e 64  simple task, and
1920: 20 73 6f 6d 65 20 6b 69 6e 64 20 6f 66 20 68 69   some kind of hi
1930: 67 68 65 72 20 6c 65 76 65 6c 20 73 63 72 69 70  gher level scrip
1940: 74 69 6e 67 20 28 65 2e 67 2e 20 50 79 74 68 6f  ting (e.g. Pytho
1950: 6e 29 20 77 6f 75 6c 64 20 62 65 20 65 61 73 69  n) would be easi
1960: 6c 79 20 72 65 71 75 69 72 65 64 20 69 6e 20 61  ly required in a
1970: 6e 79 20 72 65 61 6c 69 73 74 69 63 20 73 63 65  ny realistic sce
1980: 6e 61 72 69 6f 2e 3c 62 72 3e 0d 0a 54 68 65 20  nario.<br>..The 
1990: 61 75 74 6f 6d 61 74 69 6f 6e 20 6f 66 20 61 6c  automation of al
19a0: 6c 20 72 65 71 75 69 72 65 64 20 53 51 4c 20 71  l required SQL q
19b0: 75 65 72 69 65 73 20 69 73 6e 27 74 20 74 68 65  ueries isn't the
19c0: 20 6d 6f 73 74 20 63 72 69 74 69 63 61 6c 20 69   most critical i
19d0: 73 73 75 65 20 77 65 20 68 61 76 65 20 74 6f 20  ssue we have to 
19e0: 66 61 63 65 3b 20 74 68 65 20 72 65 61 6c 2c 20  face; the real, 
19f0: 63 72 69 74 69 63 61 6c 2c 20 62 6f 74 74 6c 65  critical, bottle
1a00: 6e 65 63 6b 20 77 69 74 68 20 74 68 69 73 20 73  neck with this s
1a10: 69 6d 70 6c 65 20 61 70 70 72 6f 61 63 68 2c 20  imple approach, 
1a20: 69 73 20 74 68 61 74 20 65 61 63 68 20 6f 66 20  is that each of 
1a30: 74 68 65 73 65 20 71 75 65 72 69 65 73 20 77 69  these queries wi
1a40: 6c 6c 20 63 61 75 73 65 20 61 6e 20 61 77 66 75  ll cause an awfu
1a50: 6c 20 3c 61 20 68 72 65 66 3d 22 68 74 74 70 73  l <a href="https
1a60: 3a 2f 2f 65 6e 2e 77 69 6b 69 70 65 64 69 61 2e  ://en.wikipedia.
1a70: 6f 72 67 2f 77 69 6b 69 2f 46 75 6c 6c 5f 74 61  org/wiki/Full_ta
1a80: 62 6c 65 5f 73 63 61 6e 22 3e 66 75 6c 6c 20 74  ble_scan">full t
1a90: 61 62 6c 65 20 73 63 61 6e 3c 2f 61 3e 3c 62 72  able scan</a><br
1aa0: 3e 0d 0a 54 68 69 73 20 77 69 6c 6c 20 62 65 20  >..This will be 
1ab0: 74 68 65 20 63 61 75 73 65 20 6f 66 20 61 6e 20  the cause of an 
1ac0: 69 6e 74 6f 6c 65 72 61 62 6c 79 20 73 6c 75 67  intolerably slug
1ad0: 67 69 73 68 20 70 65 72 66 6f 72 6d 61 6e 63 65  gish performance
1ae0: 2c 20 61 6e 64 20 74 68 65 20 6f 76 65 72 61 6c  , and the overal
1af0: 6c 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 77 69  l performance wi
1b00: 6c 6c 20 71 75 69 63 6b 6c 79 20 62 65 63 6f 6d  ll quickly becom
1b10: 65 20 77 6f 72 73 65 20 61 73 20 74 68 65 20 64  e worse as the d
1b20: 61 74 61 73 65 74 20 70 72 6f 67 72 65 73 73 69  ataset progressi
1b30: 76 65 6c 79 20 69 6e 63 72 65 61 73 65 73 20 69  vely increases i
1b40: 6e 20 73 69 7a 65 2e 0d 0a 3c 62 72 3e 3c 62 72  n size...<br><br
1b50: 3e 0d 0a 3c 62 3e 43 6f 6e 63 6c 75 73 69 6f 6e  >..<b>Conclusion
1b60: 3c 2f 62 3e 3a 3c 62 72 3e 0d 0a 53 6f 6d 65 20  </b>:<br>..Some 
1b70: 65 61 73 69 65 72 20 61 6e 64 20 73 6d 61 72 74  easier and smart
1b80: 65 72 20 61 70 70 72 6f 61 63 68 20 69 73 20 72  er approach is r
1b90: 65 71 75 69 72 65 64 3a 20 70 6f 73 73 69 62 6c  equired: possibl
1ba0: 79 20 6f 6e 65 20 74 61 6b 69 6e 67 20 66 75 6c  y one taking ful
1bb0: 6c 20 70 72 6f 66 69 74 20 66 72 6f 6d 20 61 6e  l profit from an
1bc0: 20 3c 62 3e 52 2a 54 52 65 65 20 53 70 61 74 69   <b>R*TRee Spati
1bd0: 61 6c 20 49 6e 64 65 78 3c 2f 62 3e 20 73 75 70  al Index</b> sup
1be0: 70 6f 72 74 69 6e 67 20 74 68 65 20 47 65 6f 6d  porting the Geom
1bf0: 65 74 72 69 65 73 20 74 6f 20 62 65 20 73 65 61  etries to be sea
1c00: 72 63 68 65 64 2e 3c 62 72 3e 3c 62 72 3e 0d 0a  rched.<br><br>..
1c10: 3c 68 72 3e 0d 0a 3c 68 32 3e 56 69 72 74 75 61  <hr>..<h2>Virtua
1c20: 6c 4b 4e 4e 3c 2f 68 32 3e 0d 0a 53 74 61 72 74  lKNN</h2>..Start
1c30: 69 6e 67 20 77 69 74 68 20 76 65 72 73 69 6f 6e  ing with version
1c40: 20 3c 62 3e 34 2e 34 2e 30 3c 2f 62 3e 20 53 70   <b>4.4.0</b> Sp
1c50: 61 74 69 61 4c 69 74 65 20 73 75 70 70 6f 72 74  atiaLite support
1c60: 73 20 61 20 3c 62 3e 56 69 72 74 75 61 6c 4b 4e  s a <b>VirtualKN
1c70: 4e 3c 2f 62 3e 20 3c 61 20 68 72 65 66 3d 22 68  N</b> <a href="h
1c80: 74 74 70 73 3a 2f 2f 77 77 77 2e 73 71 6c 69 74  ttps://www.sqlit
1c90: 65 2e 6f 72 67 2f 76 74 61 62 2e 68 74 6d 6c 22  e.org/vtab.html"
1ca0: 3e 56 69 72 74 75 61 6c 20 54 61 62 6c 65 3c 2f  >Virtual Table</
1cb0: 61 3e 20 73 70 65 63 69 66 69 63 61 6c 6c 79 20  a> specifically 
1cc0: 69 6e 74 65 6e 64 65 64 20 61 73 20 61 20 63 6f  intended as a co
1cd0: 6d 70 6c 65 74 65 20 61 6e 64 20 68 69 67 68 6c  mplete and highl
1ce0: 79 20 65 66 66 69 63 69 65 6e 74 20 73 6f 6c 75  y efficient solu
1cf0: 74 69 6f 6e 20 74 6f 20 74 68 65 20 3c 62 3e 4b  tion to the <b>K
1d00: 4e 4e 3c 2f 62 3e 20 70 72 6f 62 6c 65 6d 2e 0d  NN</b> problem..
1d10: 0a 3c 76 65 72 62 61 74 69 6d 3e 0d 0a 43 52 45  .<verbatim>..CRE
1d20: 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 4c  ATE VIRTUAL TABL
1d30: 45 20 6b 6e 6e 20 55 53 49 4e 47 20 56 69 72 74  E knn USING Virt
1d40: 75 61 6c 4b 4e 4e 28 29 3b 0d 0a 3c 2f 76 65 72  ualKNN();..</ver
1d50: 62 61 74 69 6d 3e 0d 0a 45 76 65 72 79 20 6e 65  batim>..Every ne
1d60: 77 20 64 62 2d 66 69 6c 65 20 62 65 69 6e 67 20  w db-file being 
1d70: 63 72 65 61 74 65 64 20 77 69 74 68 20 3c 62 3e  created with <b>
1d80: 34 2e 34 2e 30 3c 2f 62 3e 20 77 69 6c 6c 20 61  4.4.0</b> will a
1d90: 6c 77 61 79 73 20 61 75 74 6f 6d 61 74 69 63 61  lways automatica
1da0: 6c 6c 79 20 64 65 66 69 6e 65 20 61 20 3c 62 3e  lly define a <b>
1db0: 4b 4e 4e 3c 2f 62 3e 20 76 69 72 74 75 61 6c 20  KNN</b> virtual 
1dc0: 74 61 62 6c 65 2e 3c 62 72 3e 0d 0a 4f 6e 20 61  table.<br>..On a
1dd0: 6e 79 20 65 61 72 6c 69 65 72 20 76 65 72 73 69  ny earlier versi
1de0: 6f 6e 20 6f 66 20 61 20 53 70 61 74 69 61 4c 69  on of a SpatiaLi
1df0: 74 65 20 64 62 2d 66 69 6c 65 2c 20 79 6f 75 20  te db-file, you 
1e00: 63 61 6e 20 61 64 64 20 74 68 65 20 4b 4e 4e 20  can add the KNN 
1e10: 73 75 70 70 6f 72 74 20 62 79 20 6d 61 6e 75 61  support by manua
1e20: 6c 6c 79 20 65 78 65 63 75 74 69 6e 67 20 74 68  lly executing th
1e30: 65 20 61 62 6f 76 65 20 53 51 4c 20 73 74 61 74  e above SQL stat
1e40: 65 6d 65 6e 74 2e 3c 62 72 3e 0d 0a 3c 62 3e 4e  ement.<br>..<b>N
1e50: 6f 74 65 3c 2f 62 3e 3a 20 56 69 72 74 75 61 6c  ote</b>: Virtual
1e60: 4b 4e 4e 20 6e 65 63 65 73 73 61 72 69 6c 79 20  KNN necessarily 
1e70: 72 65 71 75 69 72 65 73 20 34 2e 34 2e 30 20 62  requires 4.4.0 b
1e80: 69 6e 61 72 79 20 73 75 70 70 6f 72 74 2c 20 73  inary support, s
1e90: 6f 20 61 6e 79 20 61 74 74 65 6d 70 74 20 74 6f  o any attempt to
1ea0: 20 6f 70 65 6e 20 61 20 64 62 2d 66 69 6c 65 20   open a db-file 
1eb0: 69 6e 63 6c 75 64 69 6e 67 20 61 20 56 69 72 74  including a Virt
1ec0: 75 61 6c 4b 4e 4e 20 74 61 62 6c 65 20 62 79 20  ualKNN table by 
1ed0: 75 73 69 6e 67 20 73 6f 6d 65 20 70 72 65 76 69  using some previ
1ee0: 6f 75 73 20 76 65 72 73 69 6f 6e 20 28 3c 3d 20  ous version (<= 
1ef0: 3c 62 3e 34 2e 33 2e 30 61 3c 2f 62 3e 29 20 77  <b>4.3.0a</b>) w
1f00: 69 6c 6c 20 73 75 72 65 6c 79 20 72 61 69 73 65  ill surely raise
1f10: 20 61 6e 20 65 72 72 6f 72 20 63 6f 6e 64 69 74   an error condit
1f20: 69 6f 6e 2e 0d 0a 3c 68 33 3e 41 20 66 69 72 73  ion...<h3>A firs
1f30: 74 20 62 61 73 69 63 61 6c 6c 79 20 73 69 6d 70  t basically simp
1f40: 6c 65 20 4b 4e 4e 20 71 75 65 72 79 3c 2f 68 33  le KNN query</h3
1f50: 3e 0d 0a 3c 76 65 72 62 61 74 69 6d 3e 0d 0a 53  >..<verbatim>..S
1f60: 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 6b 6e 6e  ELECT * FROM knn
1f70: 0d 0a 57 48 45 52 45 20 66 5f 74 61 62 6c 65 5f  ..WHERE f_table_
1f80: 6e 61 6d 65 20 3d 20 27 61 69 72 70 6f 72 74 73  name = 'airports
1f90: 27 20 41 4e 44 20 72 65 66 5f 67 65 6f 6d 65 74  ' AND ref_geomet
1fa0: 72 79 20 3d 20 4d 61 6b 65 50 6f 69 6e 74 28 31  ry = MakePoint(1
1fb0: 30 2c 20 34 33 29 3b 0d 0a 3c 2f 76 65 72 62 61  0, 43);..</verba
1fc0: 74 69 6d 3e 0d 0a 3c 74 61 62 6c 65 20 62 67 63  tim>..<table bgc
1fd0: 6f 6c 6f 72 3d 22 23 64 30 66 66 62 30 22 20 63  olor="#d0ffb0" c
1fe0: 65 6c 6c 73 70 61 63 69 6e 67 3d 22 34 22 20 63  ellspacing="4" c
1ff0: 65 6c 6c 70 61 64 64 69 6e 67 3d 22 34 22 20 62  ellpadding="4" b
2000: 6f 72 64 65 72 3d 22 31 22 3e 0d 0a 3c 74 72 3e  order="1">..<tr>
2010: 3c 74 64 20 62 67 63 6f 6c 6f 72 3d 22 23 63 30  <td bgcolor="#c0
2020: 64 64 61 6f 22 3e 3c 62 3e 66 5f 74 61 62 6c 65  ddao"><b>f_table
2030: 5f 6e 61 6d 65 3c 2f 62 3e 3c 2f 74 64 3e 3c 74  _name</b></td><t
2040: 64 20 62 67 63 6f 6c 6f 72 3d 22 23 63 30 64 64  d bgcolor="#c0dd
2050: 61 6f 22 3e 3c 62 3e 66 5f 67 65 6f 6d 65 74 72  ao"><b>f_geometr
2060: 79 5f 63 6f 6c 75 6d 6e 3c 2f 62 3e 3c 2f 74 64  y_column</b></td
2070: 3e 3c 74 64 20 62 67 63 6f 6c 6f 72 3d 22 23 63  ><td bgcolor="#c
2080: 30 64 64 61 6f 22 3e 3c 62 3e 72 65 66 5f 67 65  0ddao"><b>ref_ge
2090: 6f 6d 65 74 72 79 3c 2f 62 3e 3c 2f 74 64 3e 3c  ometry</b></td><
20a0: 74 64 20 62 67 63 6f 6c 6f 72 3d 22 23 63 30 64  td bgcolor="#c0d
20b0: 64 61 6f 22 3e 3c 62 3e 6d 61 78 5f 69 74 65 6d  dao"><b>max_item
20c0: 73 3c 2f 62 3e 3c 2f 74 64 3e 3c 74 64 20 62 67  s</b></td><td bg
20d0: 63 6f 6c 6f 72 3d 22 23 63 30 64 64 61 6f 22 3e  color="#c0ddao">
20e0: 3c 62 3e 70 6f 73 3c 2f 62 3e 3c 2f 74 64 3e 3c  <b>pos</b></td><
20f0: 74 64 20 62 67 63 6f 6c 6f 72 3d 22 23 63 30 64  td bgcolor="#c0d
2100: 64 61 6f 22 3e 3c 62 3e 66 69 64 3c 2f 62 3e 3c  dao"><b>fid</b><
2110: 2f 74 64 3e 3c 74 64 20 62 67 63 6f 6c 6f 72 3d  /td><td bgcolor=
2120: 22 23 63 30 64 64 61 6f 22 3e 3c 62 3e 64 69 73  "#c0ddao"><b>dis
2130: 74 61 6e 63 65 3c 2f 62 3e 3c 2f 74 64 3e 3c 2f  tance</b></td></
2140: 74 72 3e 0d 0a 3c 74 72 3e 3c 74 64 3e 61 69 72  tr>..<tr><td>air
2150: 70 6f 72 74 73 3c 2f 74 64 3e 3c 74 64 3e 67 65  ports</td><td>ge
2160: 6f 6d 3c 2f 74 64 3e 3c 74 64 3e 42 4c 4f 42 20  om</td><td>BLOB 
2170: 73 7a 3d 36 30 20 47 45 4f 4d 45 54 52 59 3c 2f  sz=60 GEOMETRY</
2180: 74 64 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69  td><td align="ri
2190: 67 68 74 22 3e 33 3c 2f 74 64 3e 3c 74 64 20 61  ght">3</td><td a
21a0: 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e 31 3c 2f  lign="right">1</
21b0: 74 64 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69  td><td align="ri
21c0: 67 68 74 22 3e 36 32 39 39 36 32 33 3c 2f 74 64  ght">6299623</td
21d0: 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69 67 68  ><td align="righ
21e0: 74 22 3e 33 33 30 34 33 2e 33 31 39 35 32 30 3c  t">33043.319520<
21f0: 2f 74 64 3e 3c 2f 74 72 3e 0d 0a 3c 74 72 3e 3c  /td></tr>..<tr><
2200: 74 64 3e 61 69 72 70 6f 72 74 73 3c 2f 74 64 3e  td>airports</td>
2210: 3c 74 64 3e 67 65 6f 6d 3c 2f 74 64 3e 3c 74 64  <td>geom</td><td
2220: 3e 42 4c 4f 42 20 73 7a 3d 36 30 20 47 45 4f 4d  >BLOB sz=60 GEOM
2230: 45 54 52 59 3c 2f 74 64 3e 3c 74 64 20 61 6c 69  ETRY</td><td ali
2240: 67 6e 3d 22 72 69 67 68 74 22 3e 33 3c 2f 74 64  gn="right">3</td
2250: 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69 67 68  ><td align="righ
2260: 74 22 3e 32 3c 2f 74 64 3e 3c 74 64 20 61 6c 69  t">2</td><td ali
2270: 67 6e 3d 22 72 69 67 68 74 22 3e 36 32 39 39 33  gn="right">62993
2280: 39 32 3c 2f 74 64 3e 3c 74 64 20 61 6c 69 67 6e  92</td><td align
2290: 3d 22 72 69 67 68 74 22 3e 36 35 32 32 36 2e 35  ="right">65226.5
22a0: 37 33 31 34 39 3c 2f 74 64 3e 3c 2f 74 72 3e 0d  73149</td></tr>.
22b0: 0a 3c 74 72 3e 3c 74 64 3e 61 69 72 70 6f 72 74  .<tr><td>airport
22c0: 73 3c 2f 74 64 3e 3c 74 64 3e 67 65 6f 6d 3c 2f  s</td><td>geom</
22d0: 74 64 3e 3c 74 64 3e 42 4c 4f 42 20 73 7a 3d 36  td><td>BLOB sz=6
22e0: 30 20 47 45 4f 4d 45 54 52 59 3c 2f 74 64 3e 3c  0 GEOMETRY</td><
22f0: 74 64 20 61 6c 69 67 6e 3d 22 72 69 67 68 74 22  td align="right"
2300: 3e 33 3c 2f 74 64 3e 3c 74 64 20 61 6c 69 67 6e  >3</td><td align
2310: 3d 22 72 69 67 68 74 22 3e 33 3c 2f 74 64 3e 3c  ="right">3</td><
2320: 74 64 20 61 6c 69 67 6e 3d 22 72 69 67 68 74 22  td align="right"
2330: 3e 36 32 39 39 36 32 38 3c 2f 74 64 3e 3c 74 64  >6299628</td><td
2340: 20 61 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e 38   align="right">8
2350: 32 33 38 37 2e 30 31 34 30 32 38 3c 2f 74 64 3e  2387.014028</td>
2360: 3c 2f 74 72 3e 0d 0a 3c 2f 74 61 62 6c 65 3e 3c  </tr>..</table><
2370: 62 72 3e 0d 0a 3c 75 6c 3e 0d 0a 3c 6c 69 3e 61  br>..<ul>..<li>a
2380: 20 56 69 72 74 75 61 6c 4b 4e 4e 20 71 75 65 72   VirtualKNN quer
2390: 79 20 63 6c 6f 73 65 6c 79 20 72 65 73 65 6d 62  y closely resemb
23a0: 6c 65 73 20 61 20 56 69 72 74 75 61 6c 53 70 61  les a VirtualSpa
23b0: 74 69 61 6c 49 6e 64 65 78 20 71 75 65 72 79 2e  tialIndex query.
23c0: 0d 0a 54 68 69 73 20 69 73 20 6e 6f 74 20 73 75  ..This is not su
23d0: 72 70 72 69 73 69 6e 67 2c 20 73 69 6e 63 65 20  rprising, since 
23e0: 62 6f 74 68 20 6f 66 20 74 68 65 6d 20 61 72 65  both of them are
23f0: 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 62 65 20   intended to be 
2400: 77 72 61 70 70 65 64 20 61 72 6f 75 6e 64 20 61  wrapped around a
2410: 6e 20 75 6e 64 65 72 6c 79 69 6e 67 20 3c 62 3e  n underlying <b>
2420: 52 2a 54 72 65 65 20 53 70 61 74 69 61 6c 20 49  R*Tree Spatial I
2430: 6e 64 65 78 3c 2f 62 3e 2e 3c 2f 6c 69 3e 20 0d  ndex</b>.</li> .
2440: 0a 3c 6c 69 3e 41 6e 79 20 76 61 6c 69 64 20 56  .<li>Any valid V
2450: 69 72 74 75 61 6c 4b 4e 4e 20 71 75 65 72 79 20  irtualKNN query 
2460: 6d 75 73 74 20 75 73 65 20 61 20 66 6f 72 6d 20  must use a form 
2470: 73 75 63 68 20 61 73 3a 0d 0a 3c 62 3e 57 48 45  such as:..<b>WHE
2480: 52 45 20 3c 69 3e 6b 6e 6e 2d 63 6f 6c 75 6d 6e  RE <i>knn-column
2490: 3c 2f 69 3e 20 3d 20 3c 69 3e 76 61 6c 75 65 3c  </i> = <i>value<
24a0: 2f 69 3e 20 41 4e 44 20 3c 69 3e 6b 6e 6e 2d 63  /i> AND <i>knn-c
24b0: 6f 6c 75 6d 6e 3c 2f 69 3e 20 3d 20 3c 69 3e 76  olumn</i> = <i>v
24c0: 61 6c 75 65 3c 2f 69 3e 20 2e 2e 2e 3c 2f 62 3e  alue</i> ...</b>
24d0: 3c 2f 6c 69 3e 0d 0a 3c 6c 69 3e 74 68 65 20 69  </li>..<li>the i
24e0: 6e 70 75 74 20 63 6f 6c 75 6d 6e 73 20 28 74 68  nput columns (th
24f0: 65 20 6f 6e 65 73 20 79 6f 75 20 63 61 6e 20 72  e ones you can r
2500: 65 66 65 72 65 6e 63 65 20 69 6e 74 6f 20 61 20  eference into a 
2510: 3c 62 3e 57 48 45 52 45 3c 2f 62 3e 20 63 6c 61  <b>WHERE</b> cla
2520: 75 73 65 29 20 61 72 65 3a 0d 0a 3c 75 6c 3e 0d  use) are:..<ul>.
2530: 0a 3c 6c 69 3e 3c 62 3e 66 5f 74 61 62 6c 65 5f  .<li><b>f_table_
2540: 6e 61 6d 65 3c 2f 62 3e 20 28 3c 69 3e 6d 61 6e  name</b> (<i>man
2550: 64 61 74 6f 72 79 3c 2f 69 3e 29 3c 62 72 3e 0d  datory</i>)<br>.
2560: 0a 6e 61 6d 65 20 6f 66 20 74 68 65 20 47 65 6f  .name of the Geo
2570: 54 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67  Table containing
2580: 20 74 68 65 20 47 65 6f 6d 65 74 72 69 65 73 20   the Geometries 
2590: 74 6f 20 62 65 20 73 65 61 72 63 68 65 64 2e 3c  to be searched.<
25a0: 2f 6c 69 3e 0d 0a 3c 6c 69 3e 3c 62 3e 66 5f 67  /li>..<li><b>f_g
25b0: 65 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e 3c 2f  eometry_column</
25c0: 62 3e 20 28 3c 69 3e 6f 70 74 69 6f 6e 61 6c 3c  b> (<i>optional<
25d0: 2f 69 3e 29 3c 62 72 3e 0d 0a 6e 61 6d 65 20 6f  /i>)<br>..name o
25e0: 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6f 66 20  f the column of 
25f0: 74 68 65 20 61 62 6f 76 65 20 74 61 62 6c 65 20  the above table 
2600: 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 47  containing the G
2610: 65 6f 6d 65 74 72 69 65 73 20 74 6f 20 62 65 20  eometries to be 
2620: 73 65 61 72 63 68 65 64 2e 0d 0a 3c 6f 6c 3e 0d  searched...<ol>.
2630: 0a 3c 6c 69 3e 49 66 20 74 68 65 20 74 61 62 6c  .<li>If the tabl
2640: 65 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 20  e identified by 
2650: 3c 62 3e 66 5f 74 61 62 6c 65 5f 6e 61 6d 65 3c  <b>f_table_name<
2660: 2f 62 3e 20 6a 75 73 74 20 63 6f 6e 74 61 69 6e  /b> just contain
2670: 73 20 61 20 73 69 6e 67 6c 65 20 47 65 6f 6d 65  s a single Geome
2680: 74 72 79 20 63 6f 6c 75 6d 6e 20 79 6f 75 20 63  try column you c
2690: 61 6e 20 73 61 66 65 6c 79 20 6f 6d 69 74 20 74  an safely omit t
26a0: 68 65 20 3c 62 3e 66 5f 67 65 6f 6d 65 74 72 79  he <b>f_geometry
26b0: 5f 63 6f 6c 75 6d 6e 3c 2f 62 3e 20 61 72 67 75  _column</b> argu
26c0: 6d 65 6e 74 20 28 69 74 20 77 69 6c 6c 20 62 65  ment (it will be
26d0: 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 73   automatically s
26e0: 65 74 29 2e 3c 2f 6c 69 3e 0d 0a 3c 6c 69 3e 49  et).</li>..<li>I
26f0: 66 2c 20 68 6f 77 65 76 65 72 2c 20 74 68 65 20  f, however, the 
2700: 74 61 62 6c 65 20 69 64 65 6e 74 69 66 69 65 64  table identified
2710: 20 62 79 20 3c 62 3e 66 5f 74 61 62 6c 65 5f 6e   by <b>f_table_n
2720: 61 6d 65 3c 2f 62 3e 20 63 6f 6e 74 61 69 6e 73  ame</b> contains
2730: 20 74 77 6f 20 28 6f 72 20 6d 6f 72 65 29 20 47   two (or more) G
2740: 65 6f 6d 65 74 72 79 20 63 6f 6c 75 6d 6e 73 20  eometry columns 
2750: 65 78 70 6c 69 63 69 74 6c 79 20 73 70 65 63 69  explicitly speci
2760: 66 79 69 6e 67 20 74 68 65 20 3c 62 3e 66 5f 67  fying the <b>f_g
2770: 65 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e 3c 2f  eometry_column</
2780: 62 3e 20 61 72 67 75 6d 65 6e 74 20 69 73 20 73  b> argument is s
2790: 74 72 69 63 74 6c 79 20 72 65 71 75 69 72 65 64  trictly required
27a0: 2c 20 74 6f 20 61 76 6f 69 64 20 61 6e 20 61 6d  , to avoid an am
27b0: 62 69 67 75 6f 75 73 20 64 65 66 69 6e 69 74 69  biguous definiti
27c0: 6f 6e 20 6f 66 20 74 68 65 20 73 65 61 72 63 68  on of the search
27d0: 20 63 6f 6e 74 65 78 74 2e 3c 2f 6c 69 3e 0d 0a   context.</li>..
27e0: 3c 6c 69 3e 49 6e 20 62 6f 74 68 20 63 61 73 65  <li>In both case
27f0: 73 20 3c 62 3e 66 5f 74 61 62 6c 65 5f 6e 61 6d  s <b>f_table_nam
2800: 65 3c 2f 62 3e 20 61 6e 64 20 3c 62 3e 66 5f 67  e</b> and <b>f_g
2810: 65 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e 3c 2f  eometry_column</
2820: 62 3e 20 6d 75 73 74 20 65 78 61 63 74 6c 79 20  b> must exactly 
2830: 6d 61 74 63 68 20 61 20 70 72 6f 70 65 72 6c 79  match a properly
2840: 20 64 65 66 69 6e 65 64 20 47 65 6f 6d 65 74 72   defined Geometr
2850: 79 20 63 6f 6c 75 6d 6e 20 73 75 70 70 6f 72 74  y column support
2860: 65 64 20 62 79 20 61 20 63 6f 72 72 65 73 70 6f  ed by a correspo
2870: 6e 64 69 6e 67 20 53 70 61 74 69 61 6c 20 49 6e  nding Spatial In
2880: 64 65 78 2e 3c 2f 6c 69 3e 0d 0a 3c 2f 6f 6c 3e  dex.</li>..</ol>
2890: 3c 2f 6c 69 3e 0d 0a 3c 6c 69 3e 3c 62 3e 72 65  </li>..<li><b>re
28a0: 66 5f 67 65 6f 6d 65 74 72 79 3c 2f 62 3e 20 28  f_geometry</b> (
28b0: 3c 69 3e 6d 61 6e 64 61 74 6f 72 79 3c 2f 69 3e  <i>mandatory</i>
28c0: 29 3c 62 72 3e 0d 0a 61 6e 79 20 61 72 62 69 74  )<br>..any arbit
28d0: 72 61 72 79 20 47 65 6f 6d 65 74 72 79 20 28 3c  rary Geometry (<
28e0: 62 3e 50 4f 49 4e 54 3c 2f 62 3e 2c 20 3c 62 3e  b>POINT</b>, <b>
28f0: 4c 49 4e 45 53 54 52 49 4e 47 3c 2f 62 3e 2c 20  LINESTRING</b>, 
2900: 3c 62 3e 50 4f 4c 59 47 4f 4e 3c 2f 62 3e 20 6f  <b>POLYGON</b> o
2910: 72 20 77 68 61 74 65 76 65 72 20 65 6c 73 65 29  r whatever else)
2920: 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 72 65 70   intended to rep
2930: 72 65 73 65 6e 74 20 74 68 65 20 6f 72 69 67 69  resent the origi
2940: 6e 20 6f 66 20 74 68 65 20 4b 4e 4e 20 73 65 61  n of the KNN sea
2950: 72 63 68 2e 3c 62 72 3e 0d 0a 54 68 69 73 20 47  rch.<br>..This G
2960: 65 6f 6d 65 74 72 79 20 6d 75 73 74 20 63 6f 6e  eometry must con
2970: 74 61 69 6e 20 74 68 65 20 73 61 6d 65 20 53 52  tain the same SR
2980: 49 44 20 61 73 20 74 68 65 20 74 61 72 67 65 74  ID as the target
2990: 20 47 65 6f 6d 65 74 72 69 65 73 20 74 6f 20 62   Geometries to b
29a0: 65 20 73 65 61 72 63 68 65 64 2e 3c 2f 6c 69 3e  e searched.</li>
29b0: 0d 0a 3c 6c 69 3e 3c 62 3e 6d 61 78 5f 69 74 65  ..<li><b>max_ite
29c0: 6d 73 3c 2f 62 3e 20 28 3c 69 3e 6f 70 74 69 6f  ms</b> (<i>optio
29d0: 6e 61 6c 3c 2f 69 3e 29 3c 62 72 3e 0d 0a 6d 61  nal</i>)<br>..ma
29e0: 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20  ximum number of 
29f0: 72 6f 77 73 20 74 6f 20 62 65 20 72 65 74 75 72  rows to be retur
2a00: 6e 65 64 20 69 6e 74 6f 20 74 68 65 20 72 65 73  ned into the res
2a10: 75 6c 74 73 65 74 2e 3c 62 72 3e 0d 0a 54 68 65  ultset.<br>..The
2a20: 20 76 61 6c 69 64 20 72 61 6e 67 65 20 69 73 20   valid range is 
2a30: 66 72 6f 6d 20 3c 62 3e 31 3c 2f 62 3e 20 74 6f  from <b>1</b> to
2a40: 20 3c 62 3e 31 30 32 34 3c 2f 62 3e 20 28 68 69   <b>1024</b> (hi
2a50: 67 68 65 72 20 76 61 6c 75 65 73 20 77 69 6c 6c  gher values will
2a60: 20 72 65 71 75 69 72 65 20 61 20 6c 6f 6e 67 65   require a longe
2a70: 72 20 74 69 6d 65 20 74 6f 20 62 65 20 70 72 6f  r time to be pro
2a80: 63 65 73 73 65 64 29 2e 3c 62 72 3e 0d 0a 42 79  cessed).<br>..By
2a90: 20 64 65 66 61 75 6c 74 20 6f 6e 6c 79 20 74 68   default only th
2aa0: 65 20 66 69 72 73 74 20 3c 62 3e 33 3c 2f 62 3e  e first <b>3</b>
2ab0: 20 72 65 73 75 6c 74 73 20 77 69 6c 6c 20 62 65   results will be
2ac0: 20 72 65 74 75 72 6e 65 64 2e 3c 2f 6c 69 3e 0d   returned.</li>.
2ad0: 0a 3c 2f 75 6c 3e 3c 2f 6c 69 3e 0d 0a 3c 6c 69  .</ul></li>..<li
2ae0: 3e 74 68 65 20 6f 75 74 70 75 74 20 63 6f 6c 75  >the output colu
2af0: 6d 6e 73 20 28 74 68 65 20 6f 6e 65 73 20 63 6f  mns (the ones co
2b00: 6e 74 61 69 6e 69 6e 67 20 76 61 6c 75 65 73 20  ntaining values 
2b10: 72 65 74 72 69 65 76 65 64 20 62 79 20 74 68 65  retrieved by the
2b20: 20 4b 4e 4e 20 73 65 61 72 63 68 29 20 61 72 65   KNN search) are
2b30: 3a 0d 0a 3c 75 6c 3e 0d 0a 3c 6c 69 3e 3c 62 3e  :..<ul>..<li><b>
2b40: 70 6f 73 3c 2f 62 3e 20 28 3c 69 3e 49 4e 54 45  pos</b> (<i>INTE
2b50: 47 45 52 3c 2f 69 3e 29 3c 62 72 3e 0d 0a 72 65  GER</i>)<br>..re
2b60: 6c 61 74 69 76 65 20 72 61 6e 6b 20 28 73 6f 72  lative rank (sor
2b70: 74 65 64 20 62 79 20 64 69 73 74 61 6e 63 65 29  ted by distance)
2b80: 3a 20 74 68 65 20 63 6c 6f 73 65 73 74 20 69 74  : the closest it
2b90: 65 6d 20 77 69 6c 6c 20 62 65 20 3c 62 3e 23 31  em will be <b>#1
2ba0: 3c 2f 62 3e 2c 20 74 68 65 20 73 65 63 6f 6e 64  </b>, the second
2bb0: 20 63 6c 6f 73 65 73 74 20 69 74 65 6d 20 77 69   closest item wi
2bc0: 6c 6c 20 62 65 20 3c 62 3e 23 32 3c 2f 62 3e 20  ll be <b>#2</b> 
2bd0: 61 6e 64 20 73 6f 20 6f 6e 2e 3c 2f 6c 69 3e 0d  and so on.</li>.
2be0: 0a 3c 6c 69 3e 3c 62 3e 66 69 64 3c 2f 62 3e 20  .<li><b>fid</b> 
2bf0: 28 3c 69 3e 49 4e 54 45 47 45 52 3c 2f 69 3e 29  (<i>INTEGER</i>)
2c00: 3c 62 72 3e 0d 0a 74 68 65 20 75 6e 69 71 75 65  <br>..the unique
2c10: 20 3c 62 3e 52 4f 57 49 44 3c 2f 62 3e 20 76 61   <b>ROWID</b> va
2c20: 6c 75 65 20 6f 66 20 74 68 65 20 63 6f 72 72 65  lue of the corre
2c30: 73 70 6f 6e 64 69 6e 67 20 66 65 61 74 75 72 65  sponding feature
2c40: 20 69 6e 74 6f 20 74 68 65 20 73 65 61 72 63 68   into the search
2c50: 65 64 20 47 65 6f 54 61 62 6c 65 2e 3c 2f 6c 69  ed GeoTable.</li
2c60: 3e 0d 0a 3c 6c 69 3e 3c 62 3e 64 69 73 74 61 6e  >..<li><b>distan
2c70: 63 65 3c 2f 62 3e 20 28 3c 69 3e 44 4f 55 42 4c  ce</b> (<i>DOUBL
2c80: 45 3c 2f 69 3e 29 3c 62 72 3e 0d 0a 74 68 65 20  E</i>)<br>..the 
2c90: 6d 69 6e 69 6d 75 6d 20 64 69 73 74 61 6e 63 65  minimum distance
2ca0: 20 62 65 74 77 65 65 6e 20 74 68 65 20 63 6f 72   between the cor
2cb0: 72 65 73 70 6f 6e 64 69 6e 67 20 66 65 61 74 75  responding featu
2cc0: 72 65 20 69 6e 74 6f 20 74 68 65 20 73 65 61 72  re into the sear
2cd0: 63 68 65 64 20 47 65 6f 54 61 62 6c 65 20 61 6e  ched GeoTable an
2ce0: 64 20 74 68 65 20 6f 72 69 67 69 6e 20 64 65 66  d the origin def
2cf0: 69 6e 65 64 20 62 79 20 3c 62 3e 72 65 66 5f 67  ined by <b>ref_g
2d00: 65 6f 6d 65 74 72 79 3c 2f 62 3e 2e 0d 0a 3c 6f  eometry</b>...<o
2d10: 6c 3e 0d 0a 3c 6c 69 3e 57 68 65 6e 20 74 68 65  l>..<li>When the
2d20: 20 64 65 66 69 6e 65 64 20 53 52 49 44 20 75 73   defined SRID us
2d30: 65 73 20 61 20 3c 62 3e 70 6c 61 6e 61 72 3c 2f  es a <b>planar</
2d40: 62 3e 20 28 61 6b 61 20 3c 69 3e 70 72 6f 6a 65  b> (aka <i>proje
2d50: 63 74 65 64 3c 2f 69 3e 29 20 72 65 66 65 72 65  cted</i>) refere
2d60: 6e 63 65 20 73 79 73 74 65 6d 2c 20 61 6c 6c 20  nce system, all 
2d70: 64 69 73 74 61 6e 63 65 73 20 77 69 6c 6c 20 62  distances will b
2d80: 65 20 72 65 74 75 72 6e 65 64 20 69 6e 20 74 68  e returned in th
2d90: 65 20 75 6e 69 74 73 20 64 65 66 69 6e 65 64 20  e units defined 
2da0: 62 79 20 74 68 65 20 70 72 6f 6a 65 63 74 69 6f  by the projectio
2db0: 6e 20 28 6d 65 74 65 72 73 2c 20 66 65 65 74 2c  n (meters, feet,
2dc0: 20 63 68 61 69 6e 73 20 65 74 63 2e 29 2e 3c 2f   chains etc.).</
2dd0: 6c 69 3e 0d 0a 3c 6c 69 3e 57 68 65 6e 20 75 73  li>..<li>When us
2de0: 69 6e 67 20 61 20 3c 62 3e 67 65 6f 67 72 61 70  ing a <b>geograp
2df0: 68 69 63 3c 2f 62 3e 20 74 79 70 65 20 28 3c 69  hic</b> type (<i
2e00: 3e 6c 6f 6e 67 69 74 75 64 65 3c 2f 69 3e 20 61  >longitude</i> a
2e10: 6e 64 20 3c 69 3e 6c 61 74 69 74 75 64 65 3c 2f  nd <i>latitude</
2e20: 69 3e 20 3c 62 3e 64 65 67 72 65 65 73 3c 2f 62  i> <b>degrees</b
2e30: 3e 29 2c 20 61 6c 6c 20 64 69 73 74 61 6e 63 65  >), all distance
2e40: 73 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 62 65  s will always be
2e50: 20 6d 65 61 73 75 72 65 64 20 69 6e 20 3c 62 3e   measured in <b>
2e60: 6d 65 74 65 72 73 3c 2f 62 3e 2c 20 77 69 74 68  meters</b>, with
2e70: 20 74 68 65 20 6d 6f 73 74 20 70 72 65 63 69 73   the most precis
2e80: 65 20 67 65 6f 64 65 74 69 63 20 66 6f 72 6d 75  e geodetic formu
2e90: 6c 61 73 20 62 65 69 6e 67 20 61 75 74 6f 6d 61  las being automa
2ea0: 74 69 63 61 6c 6c 79 20 61 70 70 6c 69 65 64 2e  tically applied.
2eb0: 3c 2f 6c 69 3e 0d 0a 3c 2f 6f 6c 3e 3c 2f 6c 69  </li>..</ol></li
2ec0: 3e 0d 0a 3c 2f 75 6c 3e 3c 2f 6c 69 3e 0d 0a 3c  >..</ul></li>..<
2ed0: 2f 75 6c 3e 3c 62 72 3e 0d 0a 3c 68 72 3e 0d 0a  /ul><br>..<hr>..
2ee0: 3c 68 33 3e 41 20 73 65 63 6f 6e 64 20 73 61 6d  <h3>A second sam
2ef0: 70 6c 65 20 6f 66 20 61 20 6d 6f 72 65 20 73 6f  ple of a more so
2f00: 70 68 69 73 74 69 63 61 74 65 64 20 4b 4e 4e 20  phisticated KNN 
2f10: 71 75 65 72 79 3c 2f 68 33 3e 0d 0a 3c 76 65 72  query</h3>..<ver
2f20: 62 61 74 69 6d 3e 0d 0a 53 45 4c 45 43 54 20 61  batim>..SELECT a
2f30: 2e 70 6f 73 20 41 53 20 72 61 6e 6b 2c 20 62 2e  .pos AS rank, b.
2f40: 67 65 6f 6e 61 6d 65 69 64 2c 20 62 2e 6e 61 6d  geonameid, b.nam
2f50: 65 2c 20 62 2e 63 6f 75 6e 74 72 79 2c 20 61 2e  e, b.country, a.
2f60: 64 69 73 74 61 6e 63 65 20 2f 20 31 30 30 30 2e  distance / 1000.
2f70: 30 20 41 53 20 64 69 73 74 5f 6b 6d 0d 0a 46 52  0 AS dist_km..FR
2f80: 4f 4d 20 6b 6e 6e 20 41 53 20 61 0d 0a 4a 4f 49  OM knn AS a..JOI
2f90: 4e 20 61 69 72 70 6f 72 74 73 20 41 53 20 62 20  N airports AS b 
2fa0: 4f 4e 20 28 62 2e 67 65 6f 6e 61 6d 65 69 64 20  ON (b.geonameid 
2fb0: 3d 20 61 2e 66 69 64 29 0d 0a 57 48 45 52 45 20  = a.fid)..WHERE 
2fc0: 66 5f 74 61 62 6c 65 5f 6e 61 6d 65 20 3d 20 27  f_table_name = '
2fd0: 61 69 72 70 6f 72 74 73 27 20 41 4e 44 20 72 65  airports' AND re
2fe0: 66 5f 67 65 6f 6d 65 74 72 79 20 3d 20 4d 61 6b  f_geometry = Mak
2ff0: 65 50 6f 69 6e 74 28 31 30 2c 20 34 33 29 20 41  ePoint(10, 43) A
3000: 4e 44 20 6d 61 78 5f 69 74 65 6d 73 20 3d 20 35  ND max_items = 5
3010: 3b 0d 0a 3c 2f 76 65 72 62 61 74 69 6d 3e 0d 0a  ;..</verbatim>..
3020: 3c 74 61 62 6c 65 20 62 67 63 6f 6c 6f 72 3d 22  <table bgcolor="
3030: 23 64 30 66 66 62 30 22 20 63 65 6c 6c 73 70 61  #d0ffb0" cellspa
3040: 63 69 6e 67 3d 22 34 22 20 63 65 6c 6c 70 61 64  cing="4" cellpad
3050: 64 69 6e 67 3d 22 34 22 20 62 6f 72 64 65 72 3d  ding="4" border=
3060: 22 31 22 3e 0d 0a 3c 74 72 3e 3c 74 64 20 62 67  "1">..<tr><td bg
3070: 63 6f 6c 6f 72 3d 22 23 63 30 64 64 61 6f 22 3e  color="#c0ddao">
3080: 3c 62 3e 72 61 6e 6b 3c 2f 62 3e 3c 2f 74 64 3e  <b>rank</b></td>
3090: 3c 74 64 20 62 67 63 6f 6c 6f 72 3d 22 23 63 30  <td bgcolor="#c0
30a0: 64 64 61 6f 22 3e 3c 62 3e 67 65 6f 6e 61 6d 65  ddao"><b>geoname
30b0: 69 64 3c 2f 62 3e 3c 2f 74 64 3e 3c 74 64 20 62  id</b></td><td b
30c0: 67 63 6f 6c 6f 72 3d 22 23 63 30 64 64 61 6f 22  gcolor="#c0ddao"
30d0: 3e 3c 62 3e 6e 61 6d 65 3c 2f 62 3e 3c 2f 74 64  ><b>name</b></td
30e0: 3e 3c 74 64 20 62 67 63 6f 6c 6f 72 3d 22 23 63  ><td bgcolor="#c
30f0: 30 64 64 61 6f 22 3e 3c 62 3e 63 6f 75 6e 74 72  0ddao"><b>countr
3100: 79 3c 2f 62 3e 3c 2f 74 64 3e 3c 74 64 20 62 67  y</b></td><td bg
3110: 63 6f 6c 6f 72 3d 22 23 63 30 64 64 61 6f 22 3e  color="#c0ddao">
3120: 3c 62 3e 64 69 73 74 5f 6b 6d 3c 2f 62 3e 3c 2f  <b>dist_km</b></
3130: 74 64 3e 3c 2f 74 72 3e 0d 0a 3c 74 72 3e 3c 74  td></tr>..<tr><t
3140: 64 20 61 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e  d align="right">
3150: 31 3c 2f 74 64 3e 3c 74 64 20 61 6c 69 67 6e 3d  1</td><td align=
3160: 22 72 69 67 68 74 22 3e 36 32 39 39 36 32 33 3c  "right">6299623<
3170: 2f 74 64 3e 3c 74 64 3e 4d 61 72 69 6e 61 20 44  /td><td>Marina D
3180: 69 20 43 61 6d 70 6f 3c 2f 74 64 3e 3c 74 64 3e  i Campo</td><td>
3190: 49 54 3c 2f 74 64 3e 3c 74 64 20 61 6c 69 67 6e  IT</td><td align
31a0: 3d 22 72 69 67 68 74 22 3e 33 33 2e 30 34 33 33  ="right">33.0433
31b0: 32 30 3c 2f 74 64 3e 3c 2f 74 72 3e 0d 0a 3c 74  20</td></tr>..<t
31c0: 72 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69 67  r><td align="rig
31d0: 68 74 22 3e 32 3c 2f 74 64 3e 3c 74 64 20 61 6c  ht">2</td><td al
31e0: 69 67 6e 3d 22 72 69 67 68 74 22 3e 36 32 39 39  ign="right">6299
31f0: 33 39 32 3c 2f 74 64 3e 3c 74 64 3e 42 61 73 74  392</td><td>Bast
3200: 69 61 2d 50 6f 72 65 74 74 61 3c 2f 74 64 3e 3c  ia-Poretta</td><
3210: 74 64 3e 46 52 3c 2f 74 64 3e 3c 74 64 20 61 6c  td>FR</td><td al
3220: 69 67 6e 3d 22 72 69 67 68 74 22 3e 36 35 2e 32  ign="right">65.2
3230: 32 36 35 37 33 3c 2f 74 64 3e 3c 2f 74 72 3e 0d  26573</td></tr>.
3240: 0a 3c 74 72 3e 3c 74 64 20 61 6c 69 67 6e 3d 22  .<tr><td align="
3250: 72 69 67 68 74 22 3e 33 3c 2f 74 64 3e 3c 74 64  right">3</td><td
3260: 20 61 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e 36   align="right">6
3270: 32 39 39 36 32 38 3c 2f 74 64 3e 3c 74 64 3e 50  299628</td><td>P
3280: 69 73 61 20 2f 20 53 2e 20 47 69 75 73 74 6f 3c  isa / S. Giusto<
3290: 2f 74 64 3e 3c 74 64 3e 49 54 3c 2f 74 64 3e 3c  /td><td>IT</td><
32a0: 74 64 20 61 6c 69 67 6e 3d 22 72 69 67 68 74 22  td align="right"
32b0: 3e 38 32 2e 33 38 37 30 31 34 3c 2f 74 64 3e 3c  >82.387014</td><
32c0: 2f 74 72 3e 0d 0a 3c 74 72 3e 3c 74 64 20 61 6c  /tr>..<tr><td al
32d0: 69 67 6e 3d 22 72 69 67 68 74 22 3e 34 3c 2f 74  ign="right">4</t
32e0: 64 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69 67  d><td align="rig
32f0: 68 74 22 3e 36 32 39 39 36 33 30 3c 2f 74 64 3e  ht">6299630</td>
3300: 3c 74 64 3e 47 72 6f 73 73 65 74 6f 20 41 69 72  <td>Grosseto Air
3310: 70 6f 72 74 3c 2f 74 64 3e 3c 74 64 3e 49 54 3c  port</td><td>IT<
3320: 2f 74 64 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72  /td><td align="r
3330: 69 67 68 74 22 3e 39 31 2e 35 34 39 37 37 33 3c  ight">91.549773<
3340: 2f 74 64 3e 3c 2f 74 72 3e 0d 0a 3c 74 72 3e 3c  /td></tr>..<tr><
3350: 74 64 20 61 6c 69 67 6e 3d 22 72 69 67 68 74 22  td align="right"
3360: 3e 35 3c 2f 74 64 3e 3c 74 64 20 61 6c 69 67 6e  >5</td><td align
3370: 3d 22 72 69 67 68 74 22 3e 36 36 39 34 34 39 35  ="right">6694495
3380: 3c 2f 74 64 3e 3c 74 64 3e 43 6f 72 74 65 3c 2f  </td><td>Corte</
3390: 74 64 3e 3c 74 64 3e 46 52 3c 2f 74 64 3e 3c 74  td><td>FR</td><t
33a0: 64 20 61 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e  d align="right">
33b0: 31 30 32 2e 38 31 39 37 37 38 3c 2f 74 64 3e 3c  102.819778</td><
33c0: 2f 74 72 3e 0d 0a 3c 2f 74 61 62 6c 65 3e 3c 62  /tr>..</table><b
33d0: 72 3e 0d 0a 54 68 69 73 20 73 65 63 6f 6e 64 20  r>..This second 
33e0: 4b 4e 4e 20 71 75 65 72 79 20 77 69 6c 6c 20 72  KNN query will r
33f0: 65 74 75 72 6e 20 74 68 65 20 73 61 6d 65 20 69  eturn the same i
3400: 64 65 6e 74 69 63 61 6c 20 72 65 73 75 6c 74 73  dentical results
3410: 20 77 65 20 61 6c 72 65 61 64 79 20 72 65 63 65   we already rece
3420: 69 76 65 64 20 62 65 66 6f 72 65 20 62 79 20 75  ived before by u
3430: 73 69 6e 67 20 74 68 65 20 73 69 6d 70 6c 65 20  sing the simple 
3440: 61 70 70 72 6f 61 63 68 2e 3c 62 72 3e 0d 0a 54  approach.<br>..T
3450: 68 65 20 73 74 72 69 6b 69 6e 67 20 64 69 66 66  he striking diff
3460: 65 72 65 6e 63 65 20 69 73 20 74 68 61 74 20 74  erence is that t
3470: 68 69 73 20 4b 4e 4e 20 71 75 65 72 79 20 73 75  his KNN query su
3480: 63 63 65 73 73 66 75 6c 6c 79 20 63 6f 6d 70 6c  ccessfully compl
3490: 65 74 65 73 20 69 6e 20 6a 75 73 74 20 61 20 66  etes in just a f
34a0: 72 61 63 74 69 6f 6e 20 6f 66 20 61 20 73 65 63  raction of a sec
34b0: 6f 6e 64 2c 20 77 68 69 6c 65 20 65 78 65 63 75  ond, while execu
34c0: 74 69 6e 67 20 74 68 65 20 66 69 72 73 74 20 69  ting the first i
34d0: 6e 65 66 66 69 63 69 65 6e 74 20 71 75 65 72 69  nefficient queri
34e0: 65 73 20 72 65 71 75 69 72 65 64 20 6d 61 6e 79  es required many
34f0: 20 73 65 63 6f 6e 64 73 20 28 61 6e 64 20 74 68   seconds (and th
3500: 61 74 20 77 69 74 68 20 61 20 72 65 6c 61 74 69  at with a relati
3510: 76 65 6c 79 20 73 6d 61 6c 6c 20 64 61 74 61 73  vely small datas
3520: 65 74 29 2e 3c 62 72 3e 3c 62 72 3e 0d 0a 3c 62  et).<br><br>..<b
3530: 3e 53 75 6d 6d 61 72 79 3c 2f 62 3e 3a 20 4b 4e  >Summary</b>: KN
3540: 4e 20 71 75 65 72 69 65 73 20 61 72 65 20 72 65  N queries are re
3550: 61 6c 6c 79 20 65 66 66 69 63 69 65 6e 74 20 61  ally efficient a
3560: 6e 64 20 66 61 73 74 2c 20 62 65 63 61 75 73 65  nd fast, because
3570: 20 74 68 65 79 20 64 69 72 65 63 74 6c 79 20 69   they directly i
3580: 6e 74 65 72 61 63 74 20 77 69 74 68 20 74 68 65  nteract with the
3590: 20 6c 6f 77 65 72 6d 6f 73 74 20 6c 65 76 65 6c   lowermost level
35a0: 73 20 6f 66 20 74 68 65 20 52 2a 54 72 65 65 20  s of the R*Tree 
35b0: 53 70 61 74 69 61 6c 20 49 6e 64 65 78 20 69 6d  Spatial Index im
35c0: 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 3c 62 72  plementation.<br
35d0: 3e 3c 62 72 3e 0d 0a 3c 68 72 3e 0d 0a 3c 68 33  ><br>..<hr>..<h3
35e0: 3e 41 20 74 68 69 72 64 20 73 61 6d 70 6c 65 20  >A third sample 
35f0: 6f 66 20 4b 4e 4e 20 71 75 65 72 79 3c 2f 68 33  of KNN query</h3
3600: 3e 0d 0a 3c 76 65 72 62 61 74 69 6d 3e 0d 0a 53  >..<verbatim>..S
3610: 45 4c 45 43 54 20 61 2e 70 6f 73 20 41 53 20 72  ELECT a.pos AS r
3620: 61 6e 6b 2c 20 62 2e 67 65 6f 6e 61 6d 65 69 64  ank, b.geonameid
3630: 2c 20 62 2e 6e 61 6d 65 2c 20 62 2e 63 6f 75 6e  , b.name, b.coun
3640: 74 72 79 2c 20 61 2e 64 69 73 74 61 6e 63 65 20  try, a.distance 
3650: 2f 20 31 30 30 30 2e 30 20 41 53 20 64 69 73 74  / 1000.0 AS dist
3660: 5f 6b 6d 0d 0a 46 52 4f 4d 20 6b 6e 6e 20 41 53  _km..FROM knn AS
3670: 20 61 0d 0a 4a 4f 49 4e 20 61 69 72 70 6f 72 74   a..JOIN airport
3680: 73 20 41 53 20 62 20 4f 4e 20 28 62 2e 67 65 6f  s AS b ON (b.geo
3690: 6e 61 6d 65 69 64 20 3d 20 61 2e 66 69 64 29 0d  nameid = a.fid).
36a0: 0a 57 48 45 52 45 20 66 5f 74 61 62 6c 65 5f 6e  .WHERE f_table_n
36b0: 61 6d 65 20 3d 20 27 61 69 72 70 6f 72 74 73 27  ame = 'airports'
36c0: 20 41 4e 44 20 72 65 66 5f 67 65 6f 6d 65 74 72   AND ref_geometr
36d0: 79 20 3d 20 4d 61 6b 65 50 6f 69 6e 74 28 2d 31  y = MakePoint(-1
36e0: 37 2e 33 2c 20 2d 34 34 29 20 41 4e 44 20 6d 61  7.3, -44) AND ma
36f0: 78 5f 69 74 65 6d 73 20 3d 20 35 3b 0d 0a 3c 2f  x_items = 5;..</
3700: 76 65 72 62 61 74 69 6d 3e 0d 0a 3c 74 61 62 6c  verbatim>..<tabl
3710: 65 20 62 67 63 6f 6c 6f 72 3d 22 23 64 30 66 66  e bgcolor="#d0ff
3720: 62 30 22 20 63 65 6c 6c 73 70 61 63 69 6e 67 3d  b0" cellspacing=
3730: 22 34 22 20 63 65 6c 6c 70 61 64 64 69 6e 67 3d  "4" cellpadding=
3740: 22 34 22 20 62 6f 72 64 65 72 3d 22 31 22 3e 0d  "4" border="1">.
3750: 0a 3c 74 72 3e 3c 74 64 20 62 67 63 6f 6c 6f 72  .<tr><td bgcolor
3760: 3d 22 23 63 30 64 64 61 6f 22 3e 3c 62 3e 72 61  ="#c0ddao"><b>ra
3770: 6e 6b 3c 2f 62 3e 3c 2f 74 64 3e 3c 74 64 20 62  nk</b></td><td b
3780: 67 63 6f 6c 6f 72 3d 22 23 63 30 64 64 61 6f 22  gcolor="#c0ddao"
3790: 3e 3c 62 3e 67 65 6f 6e 61 6d 65 69 64 3c 2f 62  ><b>geonameid</b
37a0: 3e 3c 2f 74 64 3e 3c 74 64 20 62 67 63 6f 6c 6f  ></td><td bgcolo
37b0: 72 3d 22 23 63 30 64 64 61 6f 22 3e 3c 62 3e 6e  r="#c0ddao"><b>n
37c0: 61 6d 65 3c 2f 62 3e 3c 2f 74 64 3e 3c 74 64 20  ame</b></td><td 
37d0: 62 67 63 6f 6c 6f 72 3d 22 23 63 30 64 64 61 6f  bgcolor="#c0ddao
37e0: 22 3e 3c 62 3e 63 6f 75 6e 74 72 79 3c 2f 62 3e  "><b>country</b>
37f0: 3c 2f 74 64 3e 3c 74 64 20 62 67 63 6f 6c 6f 72  </td><td bgcolor
3800: 3d 22 23 63 30 64 64 61 6f 22 3e 3c 62 3e 64 69  ="#c0ddao"><b>di
3810: 73 74 5f 6b 6d 3c 2f 62 3e 3c 2f 74 64 3e 3c 2f  st_km</b></td></
3820: 74 72 3e 0d 0a 3c 74 72 3e 3c 74 64 20 61 6c 69  tr>..<tr><td ali
3830: 67 6e 3d 22 72 69 67 68 74 22 3e 31 3c 2f 74 64  gn="right">1</td
3840: 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69 67 68  ><td align="righ
3850: 74 22 3e 37 36 36 38 34 33 33 3c 2f 74 64 3e 3c  t">7668433</td><
3860: 74 64 3e 43 61 62 6f 20 46 72 69 6f 20 41 69 72  td>Cabo Frio Air
3870: 70 6f 72 74 3c 2f 74 64 3e 3c 74 64 3e 42 52 3c  port</td><td>BR<
3880: 2f 74 64 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72  /td><td align="r
3890: 69 67 68 74 22 3e 33 32 35 36 2e 35 38 39 39 36  ight">3256.58996
38a0: 34 3c 2f 74 64 3e 3c 2f 74 72 3e 0d 0a 3c 74 72  4</td></tr>..<tr
38b0: 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69 67 68  ><td align="righ
38c0: 74 22 3e 32 3c 2f 74 64 3e 3c 74 64 20 61 6c 69  t">2</td><td ali
38d0: 67 6e 3d 22 72 69 67 68 74 22 3e 33 33 35 39 30  gn="right">33590
38e0: 30 31 3c 2f 74 64 3e 3c 74 64 3e 59 6f 75 6e 67  01</td><td>Young
38f0: 73 66 69 65 6c 64 3c 2f 74 64 3e 3c 74 64 3e 5a  sfield</td><td>Z
3900: 41 3c 2f 74 64 3e 3c 74 64 20 61 6c 69 67 6e 3d  A</td><td align=
3910: 22 72 69 67 68 74 22 3e 33 32 36 32 2e 32 37 31  "right">3262.271
3920: 30 32 37 3c 2f 74 64 3e 3c 2f 74 72 3e 0d 0a 3c  027</td></tr>..<
3930: 74 72 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69  tr><td align="ri
3940: 67 68 74 22 3e 33 3c 2f 74 64 3e 3c 74 64 20 61  ght">3</td><td a
3950: 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e 37 37 33  lign="right">773
3960: 30 31 35 33 3c 2f 74 64 3e 3c 74 64 3e 55 6d 62  0153</td><td>Umb
3970: 65 72 74 6f 20 4d 6f 64 69 61 6e 6f 20 41 69 72  erto Modiano Air
3980: 70 6f 72 74 3c 2f 74 64 3e 3c 74 64 3e 42 52 3c  port</td><td>BR<
3990: 2f 74 64 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72  /td><td align="r
39a0: 69 67 68 74 22 3e 33 32 36 32 2e 36 39 33 30 33  ight">3262.69303
39b0: 32 3c 2f 74 64 3e 3c 2f 74 72 3e 0d 0a 3c 74 72  2</td></tr>..<tr
39c0: 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69 67 68  ><td align="righ
39d0: 74 22 3e 34 3c 2f 74 64 3e 3c 74 64 20 61 6c 69  t">4</td><td ali
39e0: 67 6e 3d 22 72 69 67 68 74 22 3e 33 33 36 32 33  gn="right">33623
39f0: 35 35 3c 2f 74 64 3e 3c 74 64 3e 52 6f 62 62 65  55</td><td>Robbe
3a00: 6e 20 49 73 6c 61 6e 64 3c 2f 74 64 3e 3c 74 64  n Island</td><td
3a10: 3e 5a 41 3c 2f 74 64 3e 3c 74 64 20 61 6c 69 67  >ZA</td><td alig
3a20: 6e 3d 22 72 69 67 68 74 22 3e 33 32 36 35 2e 33  n="right">3265.3
3a30: 30 32 35 36 38 3c 2f 74 64 3e 3c 2f 74 72 3e 0d  02568</td></tr>.
3a40: 0a 3c 74 72 3e 3c 74 64 20 61 6c 69 67 6e 3d 22  .<tr><td align="
3a50: 72 69 67 68 74 22 3e 35 3c 2f 74 64 3e 3c 74 64  right">5</td><td
3a60: 20 61 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e 36   align="right">6
3a70: 33 30 30 36 32 32 3c 2f 74 64 3e 3c 74 64 3e 53  300622</td><td>S
3a80: 2e 20 50 2e 20 41 6c 64 65 69 61 20 41 65 72 6f  . P. Aldeia Aero
3a90: 64 72 6f 6d 65 3c 2f 74 64 3e 3c 74 64 3e 42 52  drome</td><td>BR
3aa0: 3c 2f 74 64 3e 3c 74 64 20 61 6c 69 67 6e 3d 22  </td><td align="
3ab0: 72 69 67 68 74 22 3e 33 32 36 37 2e 32 33 34 31  right">3267.2341
3ac0: 34 32 3c 2f 74 64 3e 3c 2f 74 72 3e 0d 0a 3c 2f  42</td></tr>..</
3ad0: 74 61 62 6c 65 3e 3c 62 72 3e 0d 0a 54 68 69 73  table><br>..This
3ae0: 20 6c 61 73 74 20 4b 4e 4e 20 71 75 65 72 79 20   last KNN query 
3af0: 69 73 20 76 65 72 79 20 73 69 6d 69 6c 61 72 20  is very similar 
3b00: 74 6f 20 74 68 65 20 70 72 65 76 69 6f 75 73 20  to the previous 
3b10: 6f 6e 65 2e 3c 62 72 3e 0d 0a 54 68 69 73 20 74  one.<br>..This t
3b20: 69 6d 65 20 77 65 27 76 65 20 70 6c 61 63 65 64  ime we've placed
3b30: 20 74 68 65 20 6f 72 69 67 69 6e 20 6c 6f 63 61   the origin loca
3b40: 74 69 6f 6e 20 73 6f 6d 65 77 68 65 72 65 20 69  tion somewhere i
3b50: 6e 20 74 68 65 20 62 6c 75 65 20 64 65 65 70 20  n the blue deep 
3b60: 77 61 74 65 72 73 20 6f 66 20 74 68 65 20 53 6f  waters of the So
3b70: 75 74 68 20 41 74 6c 61 6e 74 69 63 20 28 6d 6f  uth Atlantic (mo
3b80: 72 65 20 6f 72 20 6c 65 73 73 20 6d 69 64 77 61  re or less midwa
3b90: 79 20 62 65 74 77 65 65 6e 20 42 72 61 7a 69 6c  y between Brazil
3ba0: 20 61 6e 64 20 53 6f 75 74 68 20 41 66 72 69 63   and South Afric
3bb0: 61 29 20 61 6e 64 20 63 6f 6e 73 65 71 75 65 6e  a) and consequen
3bc0: 74 6c 79 20 74 68 65 20 6e 65 61 72 65 73 74 20  tly the nearest 
3bd0: 61 69 72 70 6f 72 74 73 20 61 72 65 20 6e 6f 74  airports are not
3be0: 20 72 65 61 6c 6c 79 20 73 6f 20 6e 65 61 72 2c   really so near,
3bf0: 20 62 65 63 61 75 73 65 20 74 68 65 79 20 61 72   because they ar
3c00: 65 20 6c 6f 63 61 74 65 64 20 6d 61 6e 79 20 74  e located many t
3c10: 68 6f 75 73 61 6e 64 73 20 6f 66 20 4b 69 6c 6f  housands of Kilo
3c20: 6d 65 74 65 72 73 20 61 77 61 79 2e 0d 0a 41 73  meters away...As
3c30: 20 79 6f 75 20 63 61 6e 20 65 61 73 69 6c 79 20   you can easily 
3c40: 63 68 65 63 6b 20 62 79 20 79 6f 75 72 73 65 6c  check by yoursel
3c50: 66 20 61 20 4b 4e 4e 20 71 75 65 72 79 20 77 69  f a KNN query wi
3c60: 6c 6c 20 65 66 66 69 63 69 65 6e 74 6c 79 20 70  ll efficiently p
3c70: 65 72 66 6f 72 6d 20 65 76 65 6e 20 75 6e 64 65  erform even unde
3c80: 72 20 73 75 63 68 20 75 6e 75 73 75 61 6c 20 63  r such unusual c
3c90: 6f 6e 64 69 74 69 6f 6e 73 2e 3c 62 72 3e 3c 62  onditions.<br><b
3ca0: 72 3e 0d 0a 3c 69 6d 67 20 73 72 63 3d 22 68 74  r>..<img src="ht
3cb0: 74 70 73 3a 2f 2f 77 77 77 2e 67 61 69 61 2d 67  tps://www.gaia-g
3cc0: 69 73 2e 69 74 2f 67 61 69 61 2d 73 69 6e 73 2f  is.it/gaia-sins/
3cd0: 6b 6e 6e 2f 73 6f 75 74 68 2d 61 74 6c 61 6e 74  knn/south-atlant
3ce0: 69 63 2e 70 6e 67 22 20 61 6c 74 3d 22 53 6f 75  ic.png" alt="Sou
3cf0: 74 68 20 41 74 6c 61 6e 74 69 63 22 3e 3c 62 72  th Atlantic"><br
3d00: 3e 3c 62 72 3e 0d 0a 3c 62 3e 53 75 6d 6d 61 72  ><br>..<b>Summar
3d10: 79 3c 2f 62 3e 3a 20 4b 4e 4e 20 71 75 65 72 69  y</b>: KNN queri
3d20: 65 73 20 6e 65 76 65 72 20 61 73 73 75 6d 65 20  es never assume 
3d30: 61 6e 79 20 70 72 65 64 65 66 69 6e 65 64 20 64  any predefined d
3d40: 69 73 74 72 69 62 75 74 69 6f 6e 20 6f 66 20 74  istribution of t
3d50: 68 65 20 73 65 61 72 63 68 65 64 20 69 74 65 6d  he searched item
3d60: 73 2c 20 61 6e 64 20 77 69 6c 6c 20 65 78 74 72  s, and will extr
3d70: 61 63 74 20 74 68 65 20 63 6f 72 72 65 63 74 20  act the correct 
3d80: 72 65 73 75 6c 74 73 20 28 73 6f 72 74 65 64 20  results (sorted 
3d90: 62 79 20 64 69 73 74 61 6e 63 65 29 2c 20 64 65  by distance), de
3da0: 73 70 69 74 65 20 74 68 65 20 76 65 72 79 20 69  spite the very i
3db0: 72 72 65 67 75 6c 61 72 20 73 61 6d 70 6c 65 20  rregular sample 
3dc0: 64 69 73 74 72 69 62 75 74 69 6f 6e 2e 3c 62 72  distribution.<br
3dd0: 3e 3c 62 72 3e 0d 0a 3c 68 72 3e 0d 0a 3c 68 32  ><br>..<hr>..<h2
3de0: 3e 41 64 76 61 6e 63 65 64 20 74 75 74 6f 72 69  >Advanced tutori
3df0: 61 6c 3c 2f 68 32 3e 0d 0a 59 6f 75 20 63 61 6e  al</h2>..You can
3e00: 20 3c 61 20 68 72 65 66 3d 22 68 74 74 70 73 3a   <a href="https:
3e10: 2f 2f 77 77 77 2e 67 61 69 61 2d 67 69 73 2e 69  //www.gaia-gis.i
3e20: 74 2f 67 61 69 61 2d 73 69 6e 73 2f 6b 6e 6e 2f  t/gaia-sins/knn/
3e30: 74 75 73 63 61 6e 79 5f 68 6f 75 73 65 6e 75 6d  tuscany_housenum
3e40: 62 65 72 73 2e 37 7a 22 3e 64 6f 77 6e 6c 6f 61  bers.7z">downloa
3e50: 64 3c 2f 61 3e 20 74 68 65 20 73 61 6d 70 6c 65  d</a> the sample
3e60: 20 64 62 2d 66 69 6c 65 20 75 73 65 64 20 69 6e   db-file used in
3e70: 20 74 68 65 20 73 61 6d 70 6c 65 73 20 6f 66 20   the samples of 
3e80: 74 68 69 73 20 61 64 76 61 6e 63 65 64 20 74 75  this advanced tu
3e90: 74 6f 72 69 61 6c 2e 3c 62 72 3e 3c 62 72 3e 0d  torial.<br><br>.
3ea0: 0a 54 68 69 73 20 69 73 20 61 20 6d 75 63 68 20  .This is a much 
3eb0: 6d 6f 72 65 20 64 65 6d 61 6e 64 69 6e 67 20 64  more demanding d
3ec0: 61 74 61 73 65 74 20 77 69 74 68 20 61 20 63 6f  ataset with a co
3ed0: 6c 6c 65 63 74 69 6f 6e 20 6f 66 20 61 62 6f 75  llection of abou
3ee0: 74 20 3c 62 3e 32 2e 34 20 6d 69 6c 6c 69 6f 6e  t <b>2.4 million
3ef0: 20 68 6f 75 73 65 20 6e 75 6d 62 65 72 73 3c 2f   house numbers</
3f00: 62 3e 2e 3c 62 72 3e 0d 0a 54 68 65 20 6f 72 69  b>.<br>..The ori
3f10: 67 69 6e 61 6c 20 69 6e 70 75 74 20 73 68 61 70  ginal input shap
3f20: 65 66 69 6c 65 20 77 61 73 20 64 6f 77 6e 6c 6f  efile was downlo
3f30: 61 64 65 64 20 66 72 6f 6d 20 3c 61 20 68 72 65  aded from <a hre
3f40: 66 3d 22 68 74 74 70 3a 2f 2f 77 77 77 35 30 32  f="http://www502
3f50: 2e 72 65 67 69 6f 6e 65 2e 74 6f 73 63 61 6e 61  .regione.toscana
3f60: 2e 69 74 2f 67 65 6f 73 63 6f 70 69 6f 2f 63 61  .it/geoscopio/ca
3f70: 72 74 6f 74 65 63 61 2e 68 74 6d 6c 22 3e 54 75  rtoteca.html">Tu
3f80: 73 63 61 6e 79 20 52 65 67 69 6f 6e 3c 2f 61 3e  scany Region</a>
3f90: 20 28 3c 62 3e 3c 69 3e 47 72 61 66 6f 20 53 74   (<b><i>Grafo St
3fa0: 72 61 64 61 6c 65 20 63 69 76 69 63 69 2e 73 68  radale civici.sh
3fb0: 70 3c 2f 69 3e 3c 2f 62 3e 29 20 61 6e 64 20 77  p</i></b>) and w
3fc0: 61 73 20 73 75 62 73 65 71 75 65 6e 74 6c 79 20  as subsequently 
3fd0: 72 65 61 72 72 61 6e 67 65 64 20 69 6e 74 6f 20  rearranged into 
3fe0: 61 20 6d 6f 72 65 20 63 6f 6e 76 65 6e 69 65 6e  a more convenien
3ff0: 74 20 66 6f 72 6d 2e 3c 62 72 3e 0d 0a 54 68 65  t form.<br>..The
4000: 20 64 61 74 61 73 65 74 20 69 73 20 72 65 6c 65   dataset is rele
4010: 61 73 65 64 20 75 6e 64 65 72 20 74 68 65 20 3c  ased under the <
4020: 62 3e 43 43 2d 42 59 2d 53 41 20 34 2e 30 3c 2f  b>CC-BY-SA 4.0</
4030: 62 3e 20 6c 69 63 65 6e 73 65 20 74 65 72 6d 73  b> license terms
4040: 2e 3c 62 72 3e 0d 0a 54 68 65 20 75 73 65 64 20  .<br>..The used 
4050: 72 65 66 65 72 65 6e 63 65 20 73 79 73 74 65 6d  reference system
4060: 20 69 73 20 3c 62 3e 33 30 30 33 20 4d 6f 6e 74   is <b>3003 Mont
4070: 65 20 4d 61 72 69 6f 20 2f 20 49 74 61 6c 79 20  e Mario / Italy 
4080: 7a 6f 6e 65 20 31 3c 2f 62 3e 3b 20 74 68 69 73  zone 1</b>; this
4090: 20 69 73 20 61 20 3c 62 3e 70 6c 61 6e 61 72 3c   is a <b>planar<
40a0: 2f 62 3e 20 28 3c 69 3e 70 72 6f 6a 65 63 74 65  /b> (<i>projecte
40b0: 64 3c 2f 69 3e 29 20 72 65 66 65 72 65 6e 63 65  d</i>) reference
40c0: 20 73 79 73 74 65 6d 20 6d 65 61 73 75 72 65 64   system measured
40d0: 20 69 6e 20 3c 62 3e 6d 65 74 65 72 73 3c 2f 62   in <b>meters</b
40e0: 3e 2e 0d 0a 3c 62 72 3e 0d 0a 3c 76 65 72 62 61  >...<br>..<verba
40f0: 74 69 6d 3e 0d 0a 53 45 4c 45 43 54 20 61 2e 70  tim>..SELECT a.p
4100: 6f 73 20 41 53 20 72 61 6e 6b 2c 20 61 2e 66 69  os AS rank, a.fi
4110: 64 20 41 53 20 66 69 64 2c 20 61 2e 64 69 73 74  d AS fid, a.dist
4120: 61 6e 63 65 20 41 53 20 64 69 73 74 5f 6d 2c 20  ance AS dist_m, 
4130: 64 2e 70 72 6f 76 20 41 53 20 70 72 6f 76 69 6e  d.prov AS provin
4140: 63 65 2c 0d 0a 20 20 20 20 20 20 20 64 2e 6e 6f  ce,..       d.no
4150: 6d 65 20 41 53 20 6d 75 6e 69 63 69 70 61 6c 69  me AS municipali
4160: 74 79 2c 20 63 2e 74 6f 70 6f 6e 69 6d 6f 20 41  ty, c.toponimo A
4170: 53 20 73 74 72 65 65 74 5f 6e 61 6d 65 2c 20 62  S street_name, b
4180: 2e 63 69 76 69 63 6f 20 41 53 20 68 6f 75 73 65  .civico AS house
4190: 5f 6e 75 6d 2c 0d 0a 20 20 20 20 20 20 20 62 2e  _num,..       b.
41a0: 67 65 6f 6d 20 41 53 20 67 65 6f 6d 0d 0a 46 52  geom AS geom..FR
41b0: 4f 4d 20 6b 6e 6e 20 41 53 20 61 0d 0a 4a 4f 49  OM knn AS a..JOI
41c0: 4e 20 63 69 76 69 63 69 20 41 53 20 62 20 4f 4e  N civici AS b ON
41d0: 20 28 62 2e 72 6f 77 69 64 20 3d 20 61 2e 66 69   (b.rowid = a.fi
41e0: 64 29 0d 0a 4a 4f 49 4e 20 74 6f 70 6f 6e 69 6d  d)..JOIN toponim
41f0: 69 20 41 53 20 63 20 4f 4e 20 28 63 2e 66 69 64  i AS c ON (c.fid
4200: 20 3d 20 62 2e 69 64 5f 74 6f 70 6f 6e 69 6d 6f   = b.id_toponimo
4210: 29 0d 0a 4a 4f 49 4e 20 63 6f 6d 75 6e 69 20 41  )..JOIN comuni A
4220: 53 20 64 20 4f 4e 20 28 64 2e 66 69 64 20 3d 20  S d ON (d.fid = 
4230: 63 2e 69 64 5f 63 6f 6d 75 6e 65 29 0d 0a 57 48  c.id_comune)..WH
4240: 45 52 45 20 61 2e 66 5f 74 61 62 6c 65 5f 6e 61  ERE a.f_table_na
4250: 6d 65 20 3d 20 27 63 69 76 69 63 69 27 20 41 4e  me = 'civici' AN
4260: 44 20 61 2e 72 65 66 5f 67 65 6f 6d 65 74 72 79  D a.ref_geometry
4270: 20 3d 20 4d 61 6b 65 50 6f 69 6e 74 28 31 37 33   = MakePoint(173
4280: 33 30 30 33 2c 20 34 38 31 36 33 33 32 2c 20 33  3003, 4816332, 3
4290: 30 30 33 29 20 41 4e 44 20 61 2e 6d 61 78 5f 69  003) AND a.max_i
42a0: 74 65 6d 73 20 3d 20 31 30 3b 0d 0a 0d 0a 53 45  tems = 10;....SE
42b0: 4c 45 43 54 20 61 2e 70 6f 73 20 41 53 20 72 61  LECT a.pos AS ra
42c0: 6e 6b 2c 20 61 2e 66 69 64 20 41 53 20 66 69 64  nk, a.fid AS fid
42d0: 2c 20 61 2e 64 69 73 74 61 6e 63 65 20 41 53 20  , a.distance AS 
42e0: 64 69 73 74 5f 6d 2c 20 62 2e 70 72 6f 76 20 41  dist_m, b.prov A
42f0: 53 20 70 72 6f 76 69 6e 63 65 2c 0d 0a 20 20 20  S province,..   
4300: 20 20 20 20 62 2e 63 6f 6d 75 6e 65 20 41 53 20      b.comune AS 
4310: 6d 75 6e 69 63 69 70 61 6c 69 74 79 2c 20 62 2e  municipality, b.
4320: 74 6f 70 6f 6e 69 6d 6f 20 41 53 20 73 74 72 65  toponimo AS stre
4330: 65 74 5f 6e 61 6d 65 2c 20 62 2e 63 69 76 69 63  et_name, b.civic
4340: 6f 20 41 53 20 68 6f 75 73 65 5f 6e 75 6d 2c 20  o AS house_num, 
4350: 0d 0a 20 20 20 20 20 20 20 62 2e 67 65 6f 6d 20  ..       b.geom 
4360: 41 53 20 67 65 6f 6d 0d 0a 46 52 4f 4d 20 6b 6e  AS geom..FROM kn
4370: 6e 20 41 53 20 61 0d 0a 4a 4f 49 4e 20 76 77 5f  n AS a..JOIN vw_
4380: 63 69 76 69 63 69 20 41 53 20 62 20 4f 4e 20 28  civici AS b ON (
4390: 62 2e 72 6f 77 69 64 20 3d 20 61 2e 66 69 64 29  b.rowid = a.fid)
43a0: 0d 0a 57 48 45 52 45 20 61 2e 66 5f 74 61 62 6c  ..WHERE a.f_tabl
43b0: 65 5f 6e 61 6d 65 20 3d 20 27 76 77 5f 63 69 76  e_name = 'vw_civ
43c0: 69 63 69 27 20 41 4e 44 20 61 2e 72 65 66 5f 67  ici' AND a.ref_g
43d0: 65 6f 6d 65 74 72 79 20 3d 20 4d 61 6b 65 50 6f  eometry = MakePo
43e0: 69 6e 74 28 31 37 33 33 30 30 33 2c 20 34 38 31  int(1733003, 481
43f0: 36 33 33 32 2c 20 33 30 30 33 29 20 41 4e 44 20  6332, 3003) AND 
4400: 61 2e 6d 61 78 5f 69 74 65 6d 73 20 3d 20 31 30  a.max_items = 10
4410: 3b 0d 0a 3c 2f 76 65 72 62 61 74 69 6d 3e 0d 0a  ;..</verbatim>..
4420: 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61  The following ma
4430: 70 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f  p corresponds to
4440: 20 74 68 65 20 61 62 6f 76 65 20 71 75 65 72 69   the above queri
4450: 65 73 3a 20 74 68 65 20 6f 72 69 67 69 6e 20 70  es: the origin p
4460: 6f 69 6e 74 20 69 73 20 70 6f 73 69 74 69 6f 6e  oint is position
4470: 65 64 20 6f 6e 20 61 20 72 6f 61 64 20 6a 75 6e  ed on a road jun
4480: 63 74 69 6f 6e 20 69 6e 20 74 68 65 20 63 65 6e  ction in the cen
4490: 74 72 61 6c 20 61 72 65 61 20 6f 66 20 61 20 64  tral area of a d
44a0: 65 6e 73 65 6c 79 20 70 6f 70 75 6c 61 74 65 64  ensely populated
44b0: 20 74 6f 77 6e 20 28 41 72 65 7a 7a 6f 29 2e 3c   town (Arezzo).<
44c0: 62 72 3e 0d 0a 4e 6f 74 20 73 75 72 70 72 69 73  br>..Not surpris
44d0: 69 6e 67 6c 79 20 74 68 65 20 74 65 6e 20 6e 65  ingly the ten ne
44e0: 61 72 65 73 74 20 68 6f 75 73 65 20 6e 75 6d 62  arest house numb
44f0: 65 72 73 20 68 61 76 65 20 62 65 65 6e 20 6c 6f  ers have been lo
4500: 63 61 74 65 64 20 77 69 74 68 69 6e 20 61 20 72  cated within a r
4510: 61 64 69 75 73 20 6f 66 20 61 62 6f 75 74 20 31  adius of about 1
4520: 30 6d 2e 3c 62 72 3e 3c 62 72 3e 0d 0a 3c 69 6d  0m.<br><br>..<im
4530: 67 20 73 72 63 3d 22 68 74 74 70 73 3a 2f 2f 77  g src="https://w
4540: 77 77 2e 67 61 69 61 2d 67 69 73 2e 69 74 2f 67  ww.gaia-gis.it/g
4550: 61 69 61 2d 73 69 6e 73 2f 6b 6e 6e 2f 68 6f 75  aia-sins/knn/hou
4560: 73 65 6e 75 6d 31 2e 6a 70 67 22 20 61 6c 74 3d  senum1.jpg" alt=
4570: 22 48 6f 75 73 65 20 4e 75 6d 62 65 72 73 20 23  "House Numbers #
4580: 31 22 3e 0d 0a 3c 62 72 3e 3c 62 72 3e 0d 0a 42  1">..<br><br>..B
4590: 6f 74 68 20 71 75 65 72 69 65 73 20 77 69 6c 6c  oth queries will
45a0: 20 72 65 74 75 72 6e 20 69 64 65 6e 74 69 63 61   return identica
45b0: 6c 20 72 65 73 75 6c 74 73 2e 3c 62 72 3e 0d 0a  l results.<br>..
45c0: 41 20 63 6c 6f 73 65 72 20 6c 6f 6f 6b 20 77 69  A closer look wi
45d0: 6c 6c 20 73 68 6f 77 20 79 6f 75 20 74 68 61 74  ll show you that
45e0: 20 74 68 65 20 73 65 63 6f 6e 64 20 71 75 65 72   the second quer
45f0: 79 20 69 73 20 62 61 73 65 64 20 6f 6e 20 61 20  y is based on a 
4600: 3c 62 3e 53 70 61 74 69 61 6c 20 56 69 65 77 3c  <b>Spatial View<
4610: 2f 62 3e 3c 62 72 3e 3c 62 72 3e 0d 0a 3c 62 3e  /b><br><br>..<b>
4620: 53 75 6d 6d 61 72 79 3c 2f 62 3e 3a 20 61 20 56  Summary</b>: a V
4630: 69 72 74 75 61 6c 4b 4e 4e 20 71 75 65 72 79 20  irtualKNN query 
4640: 63 61 6e 20 69 6e 64 69 66 66 65 72 65 6e 74 6c  can indifferentl
4650: 79 20 74 61 72 67 65 74 20 65 69 74 68 65 72 20  y target either 
4660: 61 20 47 65 6f 54 61 62 6c 65 20 6f 72 20 61 20  a GeoTable or a 
4670: 70 72 6f 70 65 72 6c 79 20 72 65 67 69 73 74 65  properly registe
4680: 72 65 64 20 53 70 61 74 69 61 6c 20 56 69 65 77  red Spatial View
4690: 2e 3c 62 72 3e 3c 62 72 3e 0d 0a 3c 76 65 72 62  .<br><br>..<verb
46a0: 61 74 69 6d 3e 0d 0a 53 45 4c 45 43 54 20 61 2e  atim>..SELECT a.
46b0: 70 6f 73 20 41 53 20 72 61 6e 6b 2c 20 61 2e 66  pos AS rank, a.f
46c0: 69 64 20 41 53 20 66 69 64 2c 20 61 2e 64 69 73  id AS fid, a.dis
46d0: 74 61 6e 63 65 20 41 53 20 64 69 73 74 5f 6d 2c  tance AS dist_m,
46e0: 20 62 2e 70 72 6f 76 20 41 53 20 70 72 6f 76 69   b.prov AS provi
46f0: 6e 63 65 2c 0d 0a 20 20 20 20 20 20 20 62 2e 63  nce,..       b.c
4700: 6f 6d 75 6e 65 20 41 53 20 6d 75 6e 69 63 69 70  omune AS municip
4710: 61 6c 69 74 79 2c 20 62 2e 74 6f 70 6f 6e 69 6d  ality, b.toponim
4720: 6f 20 41 53 20 73 74 72 65 65 74 5f 6e 61 6d 65  o AS street_name
4730: 2c 20 62 2e 63 69 76 69 63 6f 20 41 53 20 68 6f  , b.civico AS ho
4740: 75 73 65 5f 6e 75 6d 2c 20 0d 0a 20 20 20 20 20  use_num, ..     
4750: 20 20 62 2e 67 65 6f 6d 20 41 53 20 67 65 6f 6d    b.geom AS geom
4760: 0d 0a 46 52 4f 4d 20 6b 6e 6e 20 41 53 20 61 0d  ..FROM knn AS a.
4770: 0a 4a 4f 49 4e 20 76 77 5f 63 69 76 69 63 69 20  .JOIN vw_civici 
4780: 41 53 20 62 20 4f 4e 20 28 62 2e 72 6f 77 69 64  AS b ON (b.rowid
4790: 20 3d 20 61 2e 66 69 64 29 20 0d 0a 57 48 45 52   = a.fid) ..WHER
47a0: 45 20 61 2e 66 5f 74 61 62 6c 65 5f 6e 61 6d 65  E a.f_table_name
47b0: 20 3d 20 27 63 69 76 69 63 69 27 20 41 4e 44 20   = 'civici' AND 
47c0: 61 2e 72 65 66 5f 67 65 6f 6d 65 74 72 79 20 3d  a.ref_geometry =
47d0: 20 4d 61 6b 65 50 6f 69 6e 74 28 31 35 39 35 36   MakePoint(15956
47e0: 32 35 2c 20 34 37 36 37 34 32 30 2c 20 33 30 30  25, 4767420, 300
47f0: 33 29 20 41 4e 44 20 6d 61 78 5f 69 74 65 6d 73  3) AND max_items
4800: 20 3d 20 32 30 3b 0d 0a 3c 2f 76 65 72 62 61 74   = 20;..</verbat
4810: 69 6d 3e 0d 0a 54 68 65 20 66 6f 6c 6c 6f 77 69  im>..The followi
4820: 6e 67 20 6d 61 70 20 63 6f 72 72 65 73 70 6f 6e  ng map correspon
4830: 64 73 20 74 6f 20 74 68 65 20 61 62 6f 76 65 20  ds to the above 
4840: 71 75 65 72 79 2e 20 4e 6f 77 20 74 68 65 20 6f  query. Now the o
4850: 72 69 67 69 6e 20 70 6f 69 6e 74 20 69 73 20 70  rigin point is p
4860: 6f 73 69 74 69 6f 6e 65 64 20 69 6e 20 74 68 65  ositioned in the
4870: 20 54 79 72 72 68 65 6e 69 61 6e 20 53 65 61 2c   Tyrrhenian Sea,
4880: 20 61 62 6f 75 74 20 3c 62 3e 32 36 20 6b 6d 3c   about <b>26 km<
4890: 2f 62 3e 20 66 72 6f 6d 20 74 68 65 20 54 75 73  /b> from the Tus
48a0: 63 61 6e 79 20 6d 61 69 6e 6c 61 6e 64 20 61 6e  cany mainland an
48b0: 64 20 74 68 65 20 49 73 6c 61 6e 64 73 20 6f 66  d the Islands of
48c0: 20 45 6c 62 61 20 61 6e 64 20 43 61 70 72 61 69   Elba and Caprai
48d0: 61 2e 3c 62 72 3e 0d 0a 48 65 72 65 20 74 6f 6f  a.<br>..Here too
48e0: 20 56 69 72 74 75 61 6c 4b 4e 4e 20 69 73 20 63   VirtualKNN is c
48f0: 61 70 61 62 6c 65 20 6f 66 20 69 64 65 6e 74 69  apable of identi
4900: 66 79 69 6e 67 20 74 68 65 20 6e 65 61 72 65 73  fying the neares
4910: 74 20 68 6f 75 73 65 20 6e 75 6d 62 65 72 73 20  t house numbers 
4920: 69 6e 20 6a 75 73 74 20 61 20 66 72 61 63 74 69  in just a fracti
4930: 6f 6e 20 6f 66 20 61 20 73 65 63 6f 6e 64 2e 3c  on of a second.<
4940: 62 72 3e 3c 62 72 3e 0d 0a 3c 69 6d 67 20 73 72  br><br>..<img sr
4950: 63 3d 22 68 74 74 70 73 3a 2f 2f 77 77 77 2e 67  c="https://www.g
4960: 61 69 61 2d 67 69 73 2e 69 74 2f 67 61 69 61 2d  aia-gis.it/gaia-
4970: 73 69 6e 73 2f 6b 6e 6e 2f 68 6f 75 73 65 6e 75  sins/knn/housenu
4980: 6d 32 2e 70 6e 67 22 20 61 6c 74 3d 22 48 6f 75  m2.png" alt="Hou
4990: 73 65 20 4e 75 6d 62 65 72 73 20 23 32 22 3e 3c  se Numbers #2"><
49a0: 62 72 3e 3c 62 72 3e 0d 0a 3c 62 3e 53 75 6d 6d  br><br>..<b>Summ
49b0: 61 72 79 3c 2f 62 3e 3a 20 20 56 69 72 74 75 61  ary</b>:  Virtua
49c0: 6c 4b 4e 4e 20 69 73 20 68 69 67 68 6c 79 20 65  lKNN is highly e
49d0: 66 66 69 63 69 65 6e 74 20 61 6e 64 20 76 65 72  fficient and ver
49e0: 79 20 66 61 73 74 2e 20 45 76 65 6e 20 77 68 65  y fast. Even whe
49f0: 6e 20 71 75 65 72 79 69 6e 67 20 73 75 63 68 20  n querying such 
4a00: 61 20 68 75 67 65 20 64 61 74 61 73 65 74 2c 20  a huge dataset, 
4a10: 77 68 69 6c 65 20 65 78 70 6c 6f 72 69 6e 67 20  while exploring 
4a20: 74 68 65 20 6d 6f 73 74 20 63 6f 6e 74 72 61 73  the most contras
4a30: 74 69 6e 67 20 28 76 65 72 79 20 68 69 67 68 20  ting (very high 
4a40: 2f 20 6c 6f 77 20 64 65 6e 73 69 74 79 29 20 64  / low density) d
4a50: 61 74 61 20 64 69 73 74 72 69 62 75 74 69 6f 6e  ata distribution
4a60: 73 20 2e 3c 62 72 3e 3c 62 72 3e 0d 0a 3c 74 61  s .<br><br>..<ta
4a70: 62 6c 65 20 77 69 64 74 68 3d 22 31 30 30 25 22  ble width="100%"
4a80: 3e 3c 74 72 3e 0d 0a 3c 74 64 20 77 69 64 74 68  ><tr>..<td width
4a90: 3d 22 33 33 25 22 20 61 6c 69 67 6e 3d 22 6c 65  ="33%" align="le
4aa0: 66 74 22 3e 3c 2f 74 64 3e 0d 0a 3c 74 64 20 61  ft"></td>..<td a
4ab0: 6c 69 67 6e 3d 22 63 65 6e 74 65 72 22 3e 3c 61  lign="center"><a
4ac0: 20 68 72 65 66 3d 22 68 74 74 70 73 3a 2f 2f 77   href="https://w
4ad0: 77 77 2e 67 61 69 61 2d 67 69 73 2e 69 74 2f 66  ww.gaia-gis.it/f
4ae0: 6f 73 73 69 6c 2f 6c 69 62 73 70 61 74 69 61 6c  ossil/libspatial
4af0: 69 74 65 2f 77 69 6b 69 3f 6e 61 6d 65 3d 34 2e  ite/wiki?name=4.
4b00: 33 2e 30 2b 64 6f 63 22 3e 62 61 63 6b 20 74 6f  3.0+doc">back to
4b10: 20 69 6e 64 65 78 3c 2f 61 3e 3c 2f 74 64 3e 0d   index</a></td>.
4b20: 0a 3c 74 64 20 77 69 64 74 68 3d 22 33 33 25 22  .<td width="33%"
4b30: 20 61 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e 3c   align="right"><
4b40: 2f 74 64 3e 0d 0a 3c 2f 74 72 3e 3c 2f 74 61 62  /td>..</tr></tab
4b50: 6c 65 3e 0a 5a 20 34 32 61 62 61 35 33 30 63 37  le>.Z 42aba530c7
4b60: 64 36 36 36 33 30 35 30 64 33 63 66 65 65 63 65  d6663050d3cfeece
4b70: 37 38 62 38 30 36 0a                             78b806.