Hex Artifact Content
Not logged in

Artifact 543906e4490fa67eecfe423b77ecd94a2f09cea1:

Wiki page [KNN] by sandro 2015-12-12 12:53:21.
0000: 44 20 32 30 31 35 2d 31 32 2d 31 32 54 31 32 3a  D 2015-12-12T12:
0010: 35 33 3a 32 31 2e 33 36 31 0a 4c 20 4b 4e 4e 0a  53:21.361.L KNN.
0020: 50 20 38 30 36 66 64 34 64 61 30 33 36 35 39 63  P 806fd4da03659c
0030: 33 33 63 33 65 31 35 34 66 61 30 64 31 37 63 66  33c3e154fa0d17cf
0040: 36 63 66 36 61 39 30 35 36 66 0a 55 20 73 61 6e  6cf6a9056f.U san
0050: 64 72 6f 0a 57 20 31 39 31 37 31 0a 3c 74 61 62  dro.W 19171.<tab
0060: 6c 65 20 63 65 6c 6c 73 70 61 63 69 6e 67 3d 22  le cellspacing="
0070: 31 32 22 20 77 69 64 74 68 3d 22 31 30 30 25 22  12" width="100%"
0080: 3e 0d 0a 3c 74 72 3e 3c 74 64 20 63 6f 6c 73 70  >..<tr><td colsp
0090: 61 6e 3d 22 32 22 3e 0d 0a 3c 74 61 62 6c 65 20  an="2">..<table 
00a0: 77 69 64 74 68 3d 22 31 30 30 25 22 20 62 67 63  width="100%" bgc
00b0: 6f 6c 6f 72 3d 22 23 66 30 66 30 66 38 22 3e 0d  olor="#f0f0f8">.
00c0: 0a 3c 74 72 3e 3c 74 64 20 61 6c 69 67 6e 3d 22  .<tr><td align="
00d0: 63 65 6e 74 65 72 22 3e 0d 0a 3c 68 31 3e 56 69  center">..<h1>Vi
00e0: 72 74 75 61 6c 4b 4e 4e 3a 20 61 20 71 75 69 63  rtualKNN: a quic
00f0: 6b 20 69 6e 74 72 6f 3c 2f 68 31 3e 0d 0a 3c 2f  k intro</h1>..</
0100: 74 64 3e 3c 2f 74 72 3e 3c 2f 74 61 62 6c 65 3e  td></tr></table>
0110: 0d 0a 3c 74 61 62 6c 65 20 77 69 64 74 68 3d 22  ..<table width="
0120: 31 30 30 25 22 3e 3c 74 72 3e 0d 0a 3c 74 64 20  100%"><tr>..<td 
0130: 77 69 64 74 68 3d 22 33 33 25 22 20 61 6c 69 67  width="33%" alig
0140: 6e 3d 22 6c 65 66 74 22 3e 3c 2f 74 64 3e 0d 0a  n="left"></td>..
0150: 3c 74 64 20 61 6c 69 67 6e 3d 22 63 65 6e 74 65  <td align="cente
0160: 72 22 3e 3c 61 20 68 72 65 66 3d 22 68 74 74 70  r"><a href="http
0170: 73 3a 2f 2f 77 77 77 2e 67 61 69 61 2d 67 69 73  s://www.gaia-gis
0180: 2e 69 74 2f 66 6f 73 73 69 6c 2f 6c 69 62 73 70  .it/fossil/libsp
0190: 61 74 69 61 6c 69 74 65 2f 77 69 6b 69 3f 6e 61  atialite/wiki?na
01a0: 6d 65 3d 6d 69 73 63 2d 64 6f 63 73 22 3e 62 61  me=misc-docs">ba
01b0: 63 6b 20 74 6f 20 69 6e 64 65 78 3c 2f 61 3e 3c  ck to index</a><
01c0: 2f 74 64 3e 0d 0a 3c 74 64 20 77 69 64 74 68 3d  /td>..<td width=
01d0: 22 33 33 25 22 20 61 6c 69 67 6e 3d 22 72 69 67  "33%" align="rig
01e0: 68 74 22 3e 3c 2f 74 64 3e 0d 0a 3c 2f 74 72 3e  ht"></td>..</tr>
01f0: 3c 2f 74 61 62 6c 65 3e 3c 62 72 3e 0d 0a 3c 74  </table><br>..<t
0200: 61 62 6c 65 20 62 67 63 6f 6c 6f 72 3d 22 23 64  able bgcolor="#d
0210: 30 65 30 66 66 22 20 77 69 64 74 68 3d 22 31 30  0e0ff" width="10
0220: 30 25 22 3e 0d 0a 3c 74 72 3e 3c 74 64 20 61 6c  0%">..<tr><td al
0230: 69 67 6e 3d 22 63 65 6e 74 65 72 22 3e 0d 0a 3c  ign="center">..<
0240: 62 3e 43 72 65 64 69 74 73 3c 2f 62 3e 3c 62 72  b>Credits</b><br
0250: 3e 0d 0a 44 65 76 65 6c 6f 70 6d 65 6e 74 20 6f  >..Development o
0260: 66 20 3c 62 3e 3c 69 3e 56 69 72 74 75 61 6c 4b  f <b><i>VirtualK
0270: 4e 4e 3c 2f 69 3e 3c 2f 62 3e 20 68 61 73 20 62  NN</i></b> has b
0280: 65 65 6e 20 65 6e 74 69 72 65 6c 79 20 66 75 6e  een entirely fun
0290: 64 65 64 20 62 79 0d 0a 3c 61 20 68 72 65 66 3d  ded by..<a href=
02a0: 22 68 74 74 70 3a 2f 2f 65 6e 2e 77 69 6b 69 70  "http://en.wikip
02b0: 65 64 69 61 2e 6f 72 67 2f 77 69 6b 69 2f 54 75  edia.org/wiki/Tu
02c0: 73 63 61 6e 79 22 3e 54 75 73 63 61 6e 79 20 52  scany">Tuscany R
02d0: 65 67 69 6f 6e 3c 2f 61 3e 20 2d 20 54 65 72 72  egion</a> - Terr
02e0: 69 74 6f 72 69 61 6c 20 61 6e 64 20 45 6e 76 69  itorial and Envi
02f0: 72 6f 6e 6d 65 6e 74 61 6c 20 49 6e 66 6f 72 6d  ronmental Inform
0300: 61 74 69 6f 6e 20 53 79 73 74 65 6d 3c 62 72 3e  ation System<br>
0310: 0d 0a 3c 61 20 68 72 65 66 3d 22 68 74 74 70 3a  ..<a href="http:
0320: 2f 2f 77 77 77 2e 72 65 67 69 6f 6e 65 2e 74 6f  //www.regione.to
0330: 73 63 61 6e 61 2e 69 74 2f 61 6d 62 69 65 6e 74  scana.it/ambient
0340: 65 65 74 65 72 72 69 74 6f 72 69 6f 2f 67 65 6f  eeterritorio/geo
0350: 67 72 61 66 69 61 67 65 6f 6c 6f 67 69 61 2f 69  grafiageologia/i
0360: 6e 64 65 78 2e 68 74 6d 6c 22 3e 52 65 67 69 6f  ndex.html">Regio
0370: 6e 65 20 54 6f 73 63 61 6e 61 3c 2f 61 3e 20 2d  ne Toscana</a> -
0380: 20 53 65 74 74 6f 72 65 20 53 69 73 74 65 6d 61   Settore Sistema
0390: 20 49 6e 66 6f 72 6d 61 74 69 76 6f 20 54 65 72   Informativo Ter
03a0: 72 69 74 6f 72 69 61 6c 65 20 65 64 20 41 6d 62  ritoriale ed Amb
03b0: 69 65 6e 74 61 6c 65 2e 0d 0a 3c 2f 74 64 3e 3c  ientale...</td><
03c0: 2f 74 72 3e 3c 2f 74 61 62 6c 65 3e 3c 62 72 3e  /tr></table><br>
03d0: 0d 0a 3c 68 72 3e 3c 62 72 3e 0d 0a 3c 74 61 62  ..<hr><br>..<tab
03e0: 6c 65 20 77 69 64 74 68 3d 22 31 30 30 25 22 20  le width="100%" 
03f0: 62 67 63 6f 6c 6f 72 3d 22 23 66 66 66 66 64 66  bgcolor="#ffffdf
0400: 22 20 63 65 6c 6c 73 70 61 63 69 6e 67 3d 22 34  " cellspacing="4
0410: 22 20 63 65 6c 6c 70 61 64 64 69 6e 67 3d 22 38  " cellpadding="8
0420: 22 3e 0d 0a 3c 74 72 3e 3c 74 64 3e 3c 62 3e 57  ">..<tr><td><b>W
0430: 68 61 74 20 69 73 20 74 68 65 20 4b 4e 4e 20 28  hat is the KNN (
0440: 3c 69 3e 4b 2d 4e 65 61 72 65 73 74 20 4e 65 69  <i>K-Nearest Nei
0450: 67 68 62 6f 72 73 3c 2f 69 3e 29 20 70 72 6f 62  ghbors</i>) prob
0460: 6c 65 6d 20 3f 3c 2f 62 3e 3c 2f 74 64 3e 3c 2f  lem ?</b></td></
0470: 74 72 3e 0d 0a 3c 74 72 3e 3c 74 64 3e 49 6d 61  tr>..<tr><td>Ima
0480: 67 69 6e 65 20 61 20 73 65 74 20 6f 66 20 61 72  gine a set of ar
0490: 62 69 74 72 61 72 79 20 67 65 6f 6d 65 74 72 69  bitrary geometri
04a0: 65 73 3b 20 77 68 69 63 68 20 6d 61 79 20 77 65  es; which may we
04b0: 6c 6c 20 62 65 2c 20 61 20 76 65 72 79 20 68 75  ll be, a very hu
04c0: 67 65 20 64 61 74 61 73 65 74 20 63 6f 6e 74 61  ge dataset conta
04d0: 69 6e 69 6e 67 20 73 6f 6d 65 20 6d 69 6c 6c 69  ining some milli
04e0: 6f 6e 73 20 6f 66 20 66 65 61 74 75 72 65 73 2e  ons of features.
04f0: 3c 62 72 3e 0d 0a 4e 6f 77 20 69 6d 61 67 69 6e  <br>..Now imagin
0500: 65 20 74 68 61 74 20 66 6f 72 20 77 68 61 74 65  e that for whate
0510: 76 65 72 20 72 65 61 73 6f 6e 20 79 6f 75 20 61  ver reason you a
0520: 72 65 20 69 6e 74 65 72 65 73 74 65 64 20 69 6e  re interested in
0530: 20 71 75 69 63 6b 6c 79 20 69 64 65 6e 74 69 66   quickly identif
0540: 79 69 6e 67 20 61 6c 6c 20 66 65 61 74 75 72 65  ying all feature
0550: 73 20 77 69 74 68 69 6e 20 61 20 20 63 6c 6f 73  s within a  clos
0560: 65 20 73 70 61 74 69 61 6c 20 70 72 6f 78 69 6d  e spatial proxim
0570: 69 74 79 20 74 6f 20 61 6e 20 61 72 62 69 74 72  ity to an arbitr
0580: 61 72 79 20 6c 6f 63 61 74 69 6f 6e 2e 3c 62 72  ary location.<br
0590: 3e 0d 0a 54 68 69 73 20 69 73 20 74 68 65 20 74  >..This is the t
05a0: 79 70 69 63 61 6c 20 3c 62 3e 4b 4e 4e 3c 2f 62  ypical <b>KNN</b
05b0: 3e 20 70 72 6f 62 6c 65 6d 2e 3c 2f 74 64 3e 3c  > problem.</td><
05c0: 2f 74 72 3e 0d 0a 3c 74 72 3e 3c 74 64 3e 4a 75  /tr>..<tr><td>Ju
05d0: 73 74 20 66 65 77 20 70 72 61 63 74 69 63 61 6c  st few practical
05e0: 20 72 65 61 6c 20 77 6f 72 6c 64 20 65 78 61 6d   real world exam
05f0: 70 6c 65 73 3a 0d 0a 3c 75 6c 3e 0d 0a 3c 6c 69  ples:..<ul>..<li
0600: 3e 53 75 70 70 6f 73 65 20 77 68 69 6c 65 20 75  >Suppose while u
0610: 73 69 6e 67 20 61 20 73 6d 61 72 74 70 68 6f 6e  sing a smartphon
0620: 65 20 61 70 70 3a 20 79 6f 75 72 20 63 75 72 72  e app: your curr
0630: 65 6e 74 20 67 65 6f 67 72 61 70 68 69 63 20 6c  ent geographic l
0640: 6f 63 61 74 69 6f 6e 20 69 73 20 77 65 6c 6c 20  ocation is well 
0650: 6b 6e 6f 77 6e 20 62 79 20 74 68 65 20 69 6e 74  known by the int
0660: 65 67 72 61 74 65 64 20 47 50 53 20 73 65 6e 73  egrated GPS sens
0670: 6f 72 2c 20 73 6f 20 79 6f 75 20 61 72 65 20 73  or, so you are s
0680: 69 6d 70 6c 79 20 65 78 70 65 63 74 69 6e 67 20  imply expecting 
0690: 74 68 61 74 20 74 68 65 20 61 70 70 20 77 69 6c  that the app wil
06a0: 6c 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74 65  l immediately te
06b0: 6c 6c 20 77 68 65 72 65 20 79 6f 75 20 61 72 65  ll where you are
06c0: 20 28 61 6e 64 20 74 68 65 20 65 78 61 63 74 20   (and the exact 
06d0: 6c 6f 63 61 74 69 6f 6e 20 6f 66 29 20 74 68 65  location of) the
06e0: 20 6e 65 61 72 65 73 74 20 66 75 65 6c 20 73 74   nearest fuel st
06f0: 61 74 69 6f 6e 73 20 28 68 6f 74 65 6c 73 2c 20  ations (hotels, 
0700: 72 65 73 74 61 75 72 61 6e 74 73 20 65 74 63 29  restaurants etc)
0710: 2e 3c 2f 6c 69 3e 0d 0a 3c 6c 69 3e 53 75 70 70  .</li>..<li>Supp
0720: 6f 73 65 20 61 20 7a 6f 6f 6c 6f 67 69 63 61 6c  ose a zoological
0730: 2f 65 63 6f 6c 6f 67 69 63 61 6c 20 73 74 75 64  /ecological stud
0740: 79 3a 20 79 6f 75 20 6b 6e 6f 77 20 77 68 65 72  y: you know wher
0750: 65 20 61 72 65 20 6c 6f 63 61 74 65 64 20 74 68  e are located th
0760: 65 20 6e 65 73 74 69 6e 67 20 61 72 65 61 73 20  e nesting areas 
0770: 6f 66 20 73 6f 6d 65 20 72 61 72 65 20 77 61 74  of some rare wat
0780: 65 72 20 62 69 72 64 20 73 70 65 63 69 65 73 2c  er bird species,
0790: 20 61 6c 73 6f 20 74 68 65 20 6c 6f 63 61 74 69   also the locati
07a0: 6f 6e 20 6f 66 20 61 6c 6c 20 72 69 76 65 72 73  on of all rivers
07b0: 2c 20 6c 61 6b 65 73 2c 20 70 6f 6e 64 73 20 61  , lakes, ponds a
07c0: 6e 64 20 6d 61 72 73 68 6c 61 6e 64 73 20 69 6e  nd marshlands in
07d0: 20 74 68 65 20 61 72 65 61 20 79 6f 75 20 61 72   the area you ar
07e0: 65 20 69 6e 74 65 72 65 73 74 65 64 20 69 6e 2e  e interested in.
07f0: 3c 62 72 3e 0d 0a 4e 6f 77 20 79 6f 75 20 61 72  <br>..Now you ar
0800: 65 20 61 74 74 65 6d 70 74 69 6e 67 20 74 6f 20  e attempting to 
0810: 67 65 74 20 61 20 72 65 61 73 6f 6e 61 62 6c 65  get a reasonable
0820: 20 61 73 73 6f 63 69 61 74 69 6f 6e 20 62 65 74   association bet
0830: 77 65 65 6e 20 6e 65 73 74 69 6e 67 20 61 72 65  ween nesting are
0840: 61 73 20 61 6e 64 20 66 65 65 64 69 6e 67 20 7a  as and feeding z
0850: 6f 6e 65 73 20 62 61 73 65 64 20 6f 6e 20 6d 69  ones based on mi
0860: 6e 69 6d 61 6c 20 64 69 73 74 61 6e 63 65 20 63  nimal distance c
0870: 72 69 74 65 72 69 61 2e 3c 2f 6c 69 3e 0d 0a 3c  riteria.</li>..<
0880: 2f 75 6c 3e 0d 0a 3c 2f 74 64 3e 3c 2f 74 72 3e  /ul>..</td></tr>
0890: 0d 0a 3c 74 72 3e 3c 74 64 3e 57 68 65 72 65 62  ..<tr><td>Whereb
08a0: 79 2c 20 69 6e 20 62 6f 74 68 20 63 61 73 65 73  y, in both cases
08b0: 20 79 6f 75 20 63 61 6e 27 74 20 73 61 66 65 6c   you can't safel
08c0: 79 20 69 64 65 6e 74 69 66 79 20 61 20 72 65 61  y identify a rea
08d0: 73 6f 6e 61 62 6c 65 20 3c 69 3e 3c 75 3e 6d 69  sonable <i><u>mi
08e0: 6e 69 6d 61 6c 20 72 61 64 69 75 73 3c 2f 75 3e  nimal radius</u>
08f0: 3c 2f 69 3e 20 73 6f 20 74 6f 20 75 73 65 66 75  </i> so to usefu
0900: 6c 6c 79 20 72 65 73 74 72 69 63 74 20 79 6f 75  lly restrict you
0910: 72 20 73 65 61 72 63 68 20 62 79 2c 20 73 69 6e  r search by, sin
0920: 63 65 20 79 6f 75 20 63 61 6e 27 74 20 6d 61 6b  ce you can't mak
0930: 65 20 61 6e 79 20 76 61 6c 69 64 20 61 73 73 75  e any valid assu
0940: 6d 70 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65  mption about the
0950: 20 67 65 6f 67 72 61 70 68 69 63 20 64 69 73 70   geographic disp
0960: 65 72 73 69 6f 6e 20 6f 66 20 79 6f 75 72 20 74  ersion of your t
0970: 61 72 67 65 74 73 2e 3c 62 72 3e 0d 0a 54 68 65  argets.<br>..The
0980: 20 64 69 73 74 61 6e 63 65 73 20 6d 61 79 20 6e   distances may n
0990: 6f 74 69 63 65 61 62 6c 79 20 76 61 72 79 20 66  oticeably vary f
09a0: 72 6f 6d 20 63 61 73 65 20 74 6f 20 63 61 73 65  rom case to case
09b0: 2c 20 61 6e 64 20 79 6f 75 20 68 61 76 65 6e 27  , and you haven'
09c0: 74 20 61 6e 79 20 69 64 65 61 20 61 62 6f 75 74  t any idea about
09d0: 20 74 68 69 73 2c 20 73 69 6e 63 65 20 69 74 20   this, since it 
09e0: 73 74 72 69 63 74 6c 79 20 64 65 70 65 6e 64 73  strictly depends
09f0: 20 6f 6e 20 74 68 65 20 61 63 74 75 61 6c 20 6c   on the actual l
0a00: 6f 63 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73  ocation of the s
0a10: 65 61 72 63 68 20 6f 72 69 67 69 6e 20 61 6e 64  earch origin and
0a20: 20 6f 6e 20 74 68 65 20 73 74 61 74 69 73 74 69   on the statisti
0a30: 63 61 6c 20 64 69 73 70 65 72 73 69 6f 6e 20 6f  cal dispersion o
0a40: 66 20 74 68 65 20 74 61 72 67 65 74 73 20 74 6f  f the targets to
0a50: 20 62 65 20 73 65 61 72 63 68 65 64 2e 3c 2f 74   be searched.</t
0a60: 64 3e 3c 2f 74 72 3e 0d 0a 3c 2f 74 61 62 6c 65  d></tr>..</table
0a70: 3e 3c 62 72 3e 0d 0a 3c 68 72 3e 3c 62 72 3e 0d  ><br>..<hr><br>.
0a80: 0a 3c 74 61 62 6c 65 20 77 69 64 74 68 3d 22 31  .<table width="1
0a90: 30 30 25 22 3e 0d 0a 3c 74 72 3e 3c 74 64 20 76  00%">..<tr><td v
0aa0: 61 6c 69 67 6e 3d 22 6d 69 64 64 6c 65 22 3e 0d  align="middle">.
0ab0: 0a 59 6f 75 20 63 61 6e 20 3c 61 20 68 72 65 66  .You can <a href
0ac0: 3d 22 68 74 74 70 73 3a 2f 2f 77 77 77 2e 67 61  ="https://www.ga
0ad0: 69 61 2d 67 69 73 2e 69 74 2f 67 61 69 61 2d 73  ia-gis.it/gaia-s
0ae0: 69 6e 73 2f 6b 6e 6e 2f 61 69 72 70 6f 72 74 73  ins/knn/airports
0af0: 2e 37 7a 22 3e 64 6f 77 6e 6c 6f 61 64 3c 2f 61  .7z">download</a
0b00: 3e 20 61 20 73 61 6d 70 6c 65 20 64 62 2d 66 69  > a sample db-fi
0b10: 6c 65 20 65 78 61 63 74 6c 79 20 63 6f 72 72 65  le exactly corre
0b20: 73 70 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65 20  sponding to the 
0b30: 65 78 61 6d 70 6c 65 73 20 77 65 27 6c 6c 20 75  examples we'll u
0b40: 73 65 20 69 6e 20 74 68 69 73 20 66 69 72 73 74  se in this first
0b50: 20 74 75 74 6f 72 69 61 6c 2e 3c 62 72 3e 3c 62   tutorial.<br><b
0b60: 72 3e 0d 0a 54 68 69 73 20 64 61 74 61 73 65 74  r>..This dataset
0b70: 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20   corresponds to 
0b80: 61 20 63 6f 6c 6c 65 63 74 69 6f 6e 20 6f 66 20  a collection of 
0b90: 61 62 6f 75 74 20 3c 62 3e 33 30 2e 30 30 30 20  about <b>30.000 
0ba0: 61 69 72 70 6f 72 74 73 3c 2f 62 3e 20 73 63 61  airports</b> sca
0bb0: 74 74 65 72 65 64 20 61 6c 6c 20 6f 76 65 72 20  ttered all over 
0bc0: 74 68 65 20 77 6f 72 6c 64 3b 20 74 68 65 20 6f  the world; the o
0bd0: 72 69 67 69 6e 61 6c 20 69 6e 70 75 74 20 73 68  riginal input sh
0be0: 61 70 65 66 69 6c 65 20 77 61 73 20 64 6f 77 6e  apefile was down
0bf0: 6c 6f 61 64 65 64 20 66 72 6f 6d 20 3c 61 20 68  loaded from <a h
0c00: 72 65 66 3d 22 68 74 74 70 73 3a 2f 2f 77 77 77  ref="https://www
0c10: 2e 73 68 61 72 65 67 65 6f 2e 61 63 2e 75 6b 2f  .sharegeo.ac.uk/
0c20: 68 61 6e 64 6c 65 2f 31 30 36 37 32 2f 33 30 33  handle/10672/303
0c30: 22 3e 68 65 72 65 3c 2f 61 3e 3c 62 72 3e 0d 0a  ">here</a><br>..
0c40: 54 68 65 20 72 65 66 65 72 65 6e 63 65 20 73 79  The reference sy
0c50: 73 74 65 6d 20 69 73 20 3c 62 3e 34 33 32 36 20  stem is <b>4326 
0c60: 57 47 53 38 34 3c 2f 62 3e 20 28 62 61 73 65 64  WGS84</b> (based
0c70: 20 6f 6e 20 3c 69 3e 6c 6f 6e 67 69 74 75 64 65   on <i>longitude
0c80: 3c 2f 69 3e 20 61 6e 64 20 3c 69 3e 6c 61 74 69  </i> and <i>lati
0c90: 74 75 64 65 3c 2f 69 3e 20 61 6e 67 6c 65 73 29  tude</i> angles)
0ca0: 2e 0d 0a 3c 62 72 3e 3c 62 72 3e 3c 62 72 3e 3c  ...<br><br><br><
0cb0: 62 72 3e 0d 0a 54 68 65 20 6d 61 70 20 6f 6e 20  br>..The map on 
0cc0: 74 68 65 20 72 69 67 68 74 20 73 68 6f 77 73 20  the right shows 
0cd0: 74 68 65 20 67 65 6f 67 72 61 70 68 69 63 20 63  the geographic c
0ce0: 6f 6e 74 65 78 74 20 6f 66 20 74 68 69 73 20 66  ontext of this f
0cf0: 69 72 73 74 20 4b 4e 4e 20 65 78 61 6d 70 6c 65  irst KNN example
0d00: 2e 3c 2f 74 64 3e 0d 0a 3c 74 64 3e 3c 69 6d 67  .</td>..<td><img
0d10: 20 73 72 63 3d 22 68 74 74 70 73 3a 2f 2f 77 77   src="https://ww
0d20: 77 2e 67 61 69 61 2d 67 69 73 2e 69 74 2f 67 61  w.gaia-gis.it/ga
0d30: 69 61 2d 73 69 6e 73 2f 6b 6e 6e 2f 74 79 72 72  ia-sins/knn/tyrr
0d40: 68 65 6e 69 61 6e 2d 73 65 61 2e 70 6e 67 22 20  henian-sea.png" 
0d50: 61 6c 74 3d 22 54 79 72 72 68 65 6e 69 61 6e 20  alt="Tyrrhenian 
0d60: 53 65 61 22 3e 3c 2f 74 64 3e 0d 0a 3c 2f 74 61  Sea"></td>..</ta
0d70: 62 6c 65 3e 3c 62 72 3e 0d 0a 3c 68 32 3e 41 20  ble><br>..<h2>A 
0d80: 66 69 72 73 74 2c 20 73 69 6d 70 6c 65 20 61 6e  first, simple an
0d90: 64 20 76 65 72 79 20 69 6e 65 66 66 69 63 69 65  d very inefficie
0da0: 6e 74 2c 20 61 70 70 72 6f 61 63 68 3c 2f 68 32  nt, approach</h2
0db0: 3e 0d 0a 3c 76 65 72 62 61 74 69 6d 3e 0d 0a 53  >..<verbatim>..S
0dc0: 45 4c 45 43 54 20 67 65 6f 6e 61 6d 65 69 64 2c  ELECT geonameid,
0dd0: 20 6e 61 6d 65 2c 20 63 6f 75 6e 74 72 79 2c 20   name, country, 
0de0: 0d 0a 20 20 20 20 4d 69 6e 28 53 54 5f 44 69 73  ..    Min(ST_Dis
0df0: 74 61 6e 63 65 28 4d 61 6b 65 50 6f 69 6e 74 28  tance(MakePoint(
0e00: 31 30 2c 20 34 33 29 2c 20 67 65 6f 6d 2c 20 31  10, 43), geom, 1
0e10: 29 29 20 2f 20 31 30 30 30 2e 30 20 41 53 20 64  )) / 1000.0 AS d
0e20: 69 73 74 5f 6b 6d 0d 0a 46 52 4f 4d 20 61 69 72  ist_km..FROM air
0e30: 70 6f 72 74 73 3b 0d 0a 2d 2d 2d 2d 2d 2d 2d 2d  ports;..--------
0e40: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0e50: 2d 2d 2d 2d 2d 2d 2d 2d 0d 0a 36 32 39 39 36 32  --------..629962
0e60: 33 09 7c 20 4d 61 72 69 6e 61 20 44 69 20 43 61  3.| Marina Di Ca
0e70: 6d 70 6f 20 7c 20 49 54 20 7c 20 33 33 2e 30 34  mpo | IT | 33.04
0e80: 33 33 32 30 0d 0a 0d 0a 53 45 4c 45 43 54 20 67  3320....SELECT g
0e90: 65 6f 6e 61 6d 65 69 64 2c 20 6e 61 6d 65 2c 20  eonameid, name, 
0ea0: 63 6f 75 6e 74 72 79 2c 20 0d 0a 20 20 20 20 4d  country, ..    M
0eb0: 69 6e 28 53 54 5f 44 69 73 74 61 6e 63 65 28 4d  in(ST_Distance(M
0ec0: 61 6b 65 50 6f 69 6e 74 28 31 30 2c 20 34 33 29  akePoint(10, 43)
0ed0: 2c 20 67 65 6f 6d 2c 20 31 29 29 20 2f 20 31 30  , geom, 1)) / 10
0ee0: 30 30 2e 30 20 41 53 20 64 69 73 74 5f 6b 6d 0d  00.0 AS dist_km.
0ef0: 0a 46 52 4f 4d 20 61 69 72 70 6f 72 74 73 0d 0a  .FROM airports..
0f00: 57 48 45 52 45 20 67 65 6f 6e 61 6d 65 69 64 20  WHERE geonameid 
0f10: 4e 4f 54 20 49 4e 20 28 36 32 39 39 36 32 33 29  NOT IN (6299623)
0f20: 3b 0d 0a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ;..-------------
0f30: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0f40: 2d 2d 2d 2d 0d 0a 36 32 39 39 33 39 32 20 7c 20  ----..6299392 | 
0f50: 42 61 73 74 69 61 2d 50 6f 72 65 74 74 61 20 7c  Bastia-Poretta |
0f60: 20 46 52 20 7c 20 36 35 2e 32 32 36 35 37 33 0d   FR | 65.226573.
0f70: 0a 0d 0a 53 45 4c 45 43 54 20 67 65 6f 6e 61 6d  ...SELECT geonam
0f80: 65 69 64 2c 20 6e 61 6d 65 2c 20 63 6f 75 6e 74  eid, name, count
0f90: 72 79 2c 0d 0a 20 20 20 20 4d 69 6e 28 53 54 5f  ry,..    Min(ST_
0fa0: 44 69 73 74 61 6e 63 65 28 4d 61 6b 65 50 6f 69  Distance(MakePoi
0fb0: 6e 74 28 31 30 2c 20 34 33 29 2c 20 67 65 6f 6d  nt(10, 43), geom
0fc0: 2c 20 31 29 29 20 2f 20 31 30 30 30 2e 30 20 41  , 1)) / 1000.0 A
0fd0: 53 20 64 69 73 74 5f 6b 6d 0d 0a 46 52 4f 4d 20  S dist_km..FROM 
0fe0: 61 69 72 70 6f 72 74 73 0d 0a 57 48 45 52 45 20  airports..WHERE 
0ff0: 67 65 6f 6e 61 6d 65 69 64 20 4e 4f 54 20 49 4e  geonameid NOT IN
1000: 20 28 36 32 39 39 36 32 33 2c 20 36 32 39 39 33   (6299623, 62993
1010: 39 32 29 3b 0d 0a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  92);..----------
1020: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1030: 2d 2d 2d 2d 2d 2d 2d 0d 0a 36 32 39 39 36 32 38  -------..6299628
1040: 20 7c 20 50 69 73 61 20 2f 20 53 2e 20 47 69 75   | Pisa / S. Giu
1050: 73 74 6f 20 7c 20 49 54 09 7c 20 38 32 2e 33 38  sto | IT.| 82.38
1060: 37 30 31 34 0d 0a 0d 0a 53 45 4c 45 43 54 20 67  7014....SELECT g
1070: 65 6f 6e 61 6d 65 69 64 2c 20 6e 61 6d 65 2c 20  eonameid, name, 
1080: 63 6f 75 6e 74 72 79 2c 20 0d 0a 20 20 20 20 4d  country, ..    M
1090: 69 6e 28 53 54 5f 44 69 73 74 61 6e 63 65 28 4d  in(ST_Distance(M
10a0: 61 6b 65 50 6f 69 6e 74 28 31 30 2c 20 34 33 29  akePoint(10, 43)
10b0: 2c 20 67 65 6f 6d 2c 20 31 29 29 20 2f 20 31 30  , geom, 1)) / 10
10c0: 30 30 2e 30 20 41 53 20 64 69 73 74 5f 6b 6d 0d  00.0 AS dist_km.
10d0: 0a 46 52 4f 4d 20 61 69 72 70 6f 72 74 73 0d 0a  .FROM airports..
10e0: 57 48 45 52 45 20 67 65 6f 6e 61 6d 65 69 64 20  WHERE geonameid 
10f0: 4e 4f 54 20 49 4e 20 28 36 32 39 39 36 32 33 2c  NOT IN (6299623,
1100: 20 36 32 39 39 33 39 32 2c 20 36 32 39 39 36 32   6299392, 629962
1110: 38 29 3b 0d 0a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  8);..-----------
1120: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1130: 2d 2d 2d 2d 2d 2d 0d 0a 36 32 39 39 36 33 30 20  ------..6299630 
1140: 7c 20 47 72 6f 73 73 65 74 6f 20 41 69 72 70 6f  | Grosseto Airpo
1150: 72 74 20 7c 20 49 54 20 7c 20 39 31 2e 35 34 39  rt | IT | 91.549
1160: 37 37 33 0d 0a 0d 0a 53 45 4c 45 43 54 20 67 65  773....SELECT ge
1170: 6f 6e 61 6d 65 69 64 2c 20 6e 61 6d 65 2c 20 63  onameid, name, c
1180: 6f 75 6e 74 72 79 2c 20 0d 0a 20 20 20 20 4d 69  ountry, ..    Mi
1190: 6e 28 53 54 5f 44 69 73 74 61 6e 63 65 28 4d 61  n(ST_Distance(Ma
11a0: 6b 65 50 6f 69 6e 74 28 31 30 2c 20 34 33 29 2c  kePoint(10, 43),
11b0: 20 67 65 6f 6d 2c 20 31 29 29 20 2f 20 31 30 30   geom, 1)) / 100
11c0: 30 2e 30 20 41 53 20 64 69 73 74 5f 6b 6d 0d 0a  0.0 AS dist_km..
11d0: 46 52 4f 4d 20 61 69 72 70 6f 72 74 73 0d 0a 57  FROM airports..W
11e0: 48 45 52 45 20 67 65 6f 6e 61 6d 65 69 64 20 4e  HERE geonameid N
11f0: 4f 54 20 49 4e 20 28 36 32 39 39 36 32 33 2c 20  OT IN (6299623, 
1200: 36 32 39 39 33 39 32 2c 20 36 32 39 39 36 32 38  6299392, 6299628
1210: 2c 20 36 32 39 39 36 33 30 29 3b 0d 0a 2d 2d 2d  , 6299630);..---
1220: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1230: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0d 0a  --------------..
1240: 36 36 39 34 34 39 35 20 7c 20 43 6f 72 74 65 20  6694495 | Corte 
1250: 7c 20 46 52 20 7c 20 31 30 32 2e 38 31 39 37 37  | FR | 102.81977
1260: 38 0d 0a 3c 2f 76 65 72 62 61 74 69 6d 3e 0d 0a  8..</verbatim>..
1270: 57 65 20 63 6f 75 6c 64 20 65 66 66 65 63 74 69  We could effecti
1280: 76 65 6c 79 20 65 78 65 63 75 74 65 20 61 20 66  vely execute a f
1290: 69 72 73 74 20 53 51 4c 20 71 75 65 72 79 20 75  irst SQL query u
12a0: 73 69 6e 67 20 74 68 65 20 3c 62 3e 4d 69 6e 28  sing the <b>Min(
12b0: 29 3c 2f 62 3e 20 61 67 67 72 65 67 61 74 65 20  )</b> aggregate 
12c0: 66 75 6e 63 74 69 6f 6e 20 69 6e 20 6f 72 64 65  function in orde
12d0: 72 20 74 6f 20 69 64 65 6e 74 69 66 79 20 74 68  r to identify th
12e0: 65 20 63 6c 6f 73 65 73 74 20 61 69 72 70 6f 72  e closest airpor
12f0: 74 20 74 6f 20 61 6e 20 61 72 62 69 74 72 61 72  t to an arbitrar
1300: 79 20 70 6f 73 69 74 69 6f 6e 20 28 3c 69 3e 6c  y position (<i>l
1310: 61 74 69 74 75 64 65 3c 2f 69 3e 3d 3c 62 3e 34  atitude</i>=<b>4
1320: 33 2e 30 3c 2f 62 3e 2c 20 3c 69 3e 6c 6f 6e 67  3.0</b>, <i>long
1330: 69 74 75 64 65 3c 2f 69 3e 3d 3c 62 3e 31 30 2e  itude</i>=<b>10.
1340: 30 3c 2f 62 3e 29 20 6c 6f 63 61 74 65 64 20 69  0</b>) located i
1350: 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20  n the middle of 
1360: 74 68 65 20 54 79 72 72 68 65 6e 69 61 6e 20 53  the Tyrrhenian S
1370: 65 61 2e 3c 62 72 3e 0d 0a 54 68 65 6e 20 77 65  ea.<br>..Then we
1380: 20 63 6f 75 6c 64 20 63 6f 6e 74 69 6e 75 61 6c   could continual
1390: 6c 79 20 72 65 70 65 61 74 20 74 68 65 20 69 64  ly repeat the id
13a0: 65 6e 74 69 63 61 6c 20 71 75 65 72 79 2c 20 65  entical query, e
13b0: 61 63 68 20 74 69 6d 65 20 65 78 63 6c 75 64 69  ach time excludi
13c0: 6e 67 20 61 6c 6c 20 61 69 72 70 6f 72 74 73 20  ng all airports 
13d0: 77 65 27 76 65 20 61 6c 72 65 61 64 79 20 69 64  we've already id
13e0: 65 6e 74 69 66 69 65 64 20 69 6e 20 74 68 65 20  entified in the 
13f0: 70 72 65 76 69 6f 75 73 20 70 72 6f 63 65 73 73  previous process
1400: 65 64 20 73 74 65 70 73 2e 3c 62 72 3e 0d 0a 54  ed steps.<br>..T
1410: 68 65 20 66 69 6e 61 6c 20 72 65 73 75 6c 74 20  he final result 
1420: 77 69 6c 6c 20 62 65 20 61 20 66 75 6c 6c 20 6c  will be a full l
1430: 69 73 74 20 6f 66 20 74 68 65 20 66 69 72 73 74  ist of the first
1440: 20 66 69 76 65 20 61 69 72 70 6f 72 74 73 20 63   five airports c
1450: 6c 6f 73 65 73 74 20 74 6f 20 74 68 65 20 72 65  losest to the re
1460: 66 65 72 65 6e 63 65 20 6c 6f 63 61 74 69 6f 6e  ference location
1470: 20 6f 72 64 65 72 65 64 20 62 79 20 69 6e 63 72   ordered by incr
1480: 65 61 73 69 6e 67 20 64 69 73 74 61 6e 63 65 2e  easing distance.
1490: 0d 0a 3c 62 72 3e 3c 62 72 3e 0d 0a 3c 74 61 62  ..<br><br>..<tab
14a0: 6c 65 20 62 67 63 6f 6c 6f 72 3d 22 23 64 30 66  le bgcolor="#d0f
14b0: 66 62 30 22 20 63 65 6c 6c 73 70 61 63 69 6e 67  fb0" cellspacing
14c0: 3d 22 34 22 20 63 65 6c 6c 70 61 64 64 69 6e 67  ="4" cellpadding
14d0: 3d 22 34 22 20 62 6f 72 64 65 72 3d 22 31 22 3e  ="4" border="1">
14e0: 0d 0a 3c 74 72 3e 3c 74 64 20 62 67 63 6f 6c 6f  ..<tr><td bgcolo
14f0: 72 3d 22 23 63 30 64 64 61 6f 22 3e 3c 62 3e 72  r="#c0ddao"><b>r
1500: 61 6e 6b 3c 2f 62 3e 3c 2f 74 64 3e 3c 74 64 20  ank</b></td><td 
1510: 62 67 63 6f 6c 6f 72 3d 22 23 63 30 64 64 61 6f  bgcolor="#c0ddao
1520: 22 3e 3c 62 3e 67 65 6f 6e 61 6d 65 69 64 3c 2f  "><b>geonameid</
1530: 62 3e 3c 2f 74 64 3e 3c 74 64 20 62 67 63 6f 6c  b></td><td bgcol
1540: 6f 72 3d 22 23 63 30 64 64 61 6f 22 3e 3c 62 3e  or="#c0ddao"><b>
1550: 6e 61 6d 65 3c 2f 62 3e 3c 2f 74 64 3e 3c 74 64  name</b></td><td
1560: 20 62 67 63 6f 6c 6f 72 3d 22 23 63 30 64 64 61   bgcolor="#c0dda
1570: 6f 22 3e 3c 62 3e 63 6f 75 6e 74 72 79 3c 2f 62  o"><b>country</b
1580: 3e 3c 2f 74 64 3e 3c 74 64 20 62 67 63 6f 6c 6f  ></td><td bgcolo
1590: 72 3d 22 23 63 30 64 64 61 6f 22 3e 3c 62 3e 64  r="#c0ddao"><b>d
15a0: 69 73 74 5f 6b 6d 3c 2f 62 3e 3c 2f 74 64 3e 3c  ist_km</b></td><
15b0: 2f 74 72 3e 0d 0a 3c 74 72 3e 3c 74 64 20 61 6c  /tr>..<tr><td al
15c0: 69 67 6e 3d 22 72 69 67 68 74 22 3e 31 3c 2f 74  ign="right">1</t
15d0: 64 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69 67  d><td align="rig
15e0: 68 74 22 3e 36 32 39 39 36 32 33 3c 2f 74 64 3e  ht">6299623</td>
15f0: 3c 74 64 3e 4d 61 72 69 6e 61 20 44 69 20 43 61  <td>Marina Di Ca
1600: 6d 70 6f 3c 2f 74 64 3e 3c 74 64 3e 49 54 3c 2f  mpo</td><td>IT</
1610: 74 64 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69  td><td align="ri
1620: 67 68 74 22 3e 33 33 2e 30 34 33 33 32 30 3c 2f  ght">33.043320</
1630: 74 64 3e 3c 2f 74 72 3e 0d 0a 3c 74 72 3e 3c 74  td></tr>..<tr><t
1640: 64 20 61 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e  d align="right">
1650: 32 3c 2f 74 64 3e 3c 74 64 20 61 6c 69 67 6e 3d  2</td><td align=
1660: 22 72 69 67 68 74 22 3e 36 32 39 39 33 39 32 3c  "right">6299392<
1670: 2f 74 64 3e 3c 74 64 3e 42 61 73 74 69 61 2d 50  /td><td>Bastia-P
1680: 6f 72 65 74 74 61 3c 2f 74 64 3e 3c 74 64 3e 46  oretta</td><td>F
1690: 52 3c 2f 74 64 3e 3c 74 64 20 61 6c 69 67 6e 3d  R</td><td align=
16a0: 22 72 69 67 68 74 22 3e 36 35 2e 32 32 36 35 37  "right">65.22657
16b0: 33 3c 2f 74 64 3e 3c 2f 74 72 3e 0d 0a 3c 74 72  3</td></tr>..<tr
16c0: 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69 67 68  ><td align="righ
16d0: 74 22 3e 33 3c 2f 74 64 3e 3c 74 64 20 61 6c 69  t">3</td><td ali
16e0: 67 6e 3d 22 72 69 67 68 74 22 3e 36 32 39 39 36  gn="right">62996
16f0: 32 38 3c 2f 74 64 3e 3c 74 64 3e 50 69 73 61 20  28</td><td>Pisa 
1700: 2f 20 53 2e 20 47 69 75 73 74 6f 3c 2f 74 64 3e  / S. Giusto</td>
1710: 3c 74 64 3e 49 54 3c 2f 74 64 3e 3c 74 64 20 61  <td>IT</td><td a
1720: 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e 38 32 2e  lign="right">82.
1730: 33 38 37 30 31 34 3c 2f 74 64 3e 3c 2f 74 72 3e  387014</td></tr>
1740: 0d 0a 3c 74 72 3e 3c 74 64 20 61 6c 69 67 6e 3d  ..<tr><td align=
1750: 22 72 69 67 68 74 22 3e 34 3c 2f 74 64 3e 3c 74  "right">4</td><t
1760: 64 20 61 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e  d align="right">
1770: 36 32 39 39 36 33 30 3c 2f 74 64 3e 3c 74 64 3e  6299630</td><td>
1780: 47 72 6f 73 73 65 74 6f 20 41 69 72 70 6f 72 74  Grosseto Airport
1790: 3c 2f 74 64 3e 3c 74 64 3e 49 54 3c 2f 74 64 3e  </td><td>IT</td>
17a0: 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69 67 68 74  <td align="right
17b0: 22 3e 39 31 2e 35 34 39 37 37 33 3c 2f 74 64 3e  ">91.549773</td>
17c0: 3c 2f 74 72 3e 0d 0a 3c 74 72 3e 3c 74 64 20 61  </tr>..<tr><td a
17d0: 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e 35 3c 2f  lign="right">5</
17e0: 74 64 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69  td><td align="ri
17f0: 67 68 74 22 3e 36 36 39 34 34 39 35 3c 2f 74 64  ght">6694495</td
1800: 3e 3c 74 64 3e 43 6f 72 74 65 3c 2f 74 64 3e 3c  ><td>Corte</td><
1810: 74 64 3e 46 52 3c 2f 74 64 3e 3c 74 64 20 61 6c  td>FR</td><td al
1820: 69 67 6e 3d 22 72 69 67 68 74 22 3e 31 30 32 2e  ign="right">102.
1830: 38 31 39 37 37 38 3c 2f 74 64 3e 3c 2f 74 72 3e  819778</td></tr>
1840: 0d 0a 3c 2f 74 61 62 6c 65 3e 3c 62 72 3e 0d 0a  ..</table><br>..
1850: 53 75 63 68 20 61 20 73 69 6d 70 6c 65 20 61 70  Such a simple ap
1860: 70 72 6f 61 63 68 20 69 73 20 68 69 67 68 6c 79  proach is highly
1870: 20 69 6d 70 72 61 63 74 69 63 61 6c 2c 20 62 65   impractical, be
1880: 63 61 75 73 65 20 69 74 20 73 74 72 69 63 74 6c  cause it strictl
1890: 79 20 72 65 71 75 69 72 65 73 20 74 6f 20 68 61  y requires to ha
18a0: 6e 64 2d 77 72 69 74 65 20 73 65 76 65 72 61 6c  nd-write several
18b0: 20 3c 69 3e 61 64 20 68 6f 63 3c 2f 69 3e 20 53   <i>ad hoc</i> S
18c0: 51 4c 20 71 75 65 72 69 65 73 20 6f 6e 65 20 62  QL queries one b
18d0: 79 20 6f 6e 65 2e 3c 62 72 3e 0d 0a 57 72 69 74  y one.<br>..Writ
18e0: 69 6e 67 20 61 20 66 75 6c 6c 79 20 61 75 74 6f  ing a fully auto
18f0: 6d 61 74 65 64 20 53 51 4c 20 73 63 72 69 70 74  mated SQL script
1900: 20 69 73 6e 27 74 20 61 74 20 61 6c 6c 20 61 20   isn't at all a 
1910: 73 69 6d 70 6c 65 20 74 61 73 6b 2c 20 61 6e 64  simple task, and
1920: 20 73 6f 6d 65 20 6b 69 6e 64 20 6f 66 20 68 69   some kind of hi
1930: 67 68 65 72 20 6c 65 76 65 6c 20 73 63 72 69 70  gher level scrip
1940: 74 69 6e 67 20 28 65 2e 67 2e 20 50 79 74 68 6f  ting (e.g. Pytho
1950: 6e 29 20 77 6f 75 6c 64 20 62 65 20 65 61 73 69  n) would be easi
1960: 6c 79 20 72 65 71 75 69 72 65 64 20 69 6e 20 61  ly required in a
1970: 6e 79 20 72 65 61 6c 69 73 74 69 63 20 73 63 65  ny realistic sce
1980: 6e 61 72 69 6f 2e 3c 62 72 3e 0d 0a 54 68 65 20  nario.<br>..The 
1990: 61 75 74 6f 6d 61 74 69 6f 6e 20 6f 66 20 61 6c  automation of al
19a0: 6c 20 72 65 71 75 69 72 65 64 20 53 51 4c 20 71  l required SQL q
19b0: 75 65 72 69 65 73 20 69 73 6e 27 74 20 74 68 65  ueries isn't the
19c0: 20 6d 6f 73 74 20 63 72 69 74 69 63 61 6c 20 69   most critical i
19d0: 73 73 75 65 20 77 65 20 68 61 76 65 20 74 6f 20  ssue we have to 
19e0: 66 61 63 65 3b 20 74 68 65 20 72 65 61 6c 2c 20  face; the real, 
19f0: 63 72 69 74 69 63 61 6c 2c 20 62 6f 74 74 6c 65  critical, bottle
1a00: 20 6e 65 63 6b 20 77 69 74 68 20 74 68 69 73 20   neck with this 
1a10: 73 69 6d 70 6c 65 20 61 70 70 72 6f 61 63 68 2c  simple approach,
1a20: 20 69 73 20 74 68 61 74 20 65 61 63 68 20 6f 66   is that each of
1a30: 20 74 68 65 73 65 20 71 75 65 72 69 65 73 20 77   these queries w
1a40: 69 6c 6c 20 63 61 75 73 65 20 61 6e 20 61 77 66  ill cause an awf
1a50: 75 6c 20 3c 61 20 68 72 65 66 3d 22 68 74 74 70  ul <a href="http
1a60: 73 3a 2f 2f 65 6e 2e 77 69 6b 69 70 65 64 69 61  s://en.wikipedia
1a70: 2e 6f 72 67 2f 77 69 6b 69 2f 46 75 6c 6c 5f 74  .org/wiki/Full_t
1a80: 61 62 6c 65 5f 73 63 61 6e 22 3e 66 75 6c 6c 20  able_scan">full 
1a90: 74 61 62 6c 65 20 73 63 61 6e 3c 2f 61 3e 3c 62  table scan</a><b
1aa0: 72 3e 0d 0a 54 68 69 73 20 77 69 6c 6c 20 62 65  r>..This will be
1ab0: 20 74 68 65 20 63 61 75 73 65 20 6f 66 20 61 6e   the cause of an
1ac0: 20 69 6e 74 6f 6c 65 72 61 62 6c 79 20 73 6c 75   intolerably slu
1ad0: 67 67 69 73 68 20 70 65 72 66 6f 72 6d 61 6e 63  ggish performanc
1ae0: 65 2c 20 61 6e 64 20 74 68 65 20 6f 76 65 72 61  e, and the overa
1af0: 6c 6c 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 77  ll performance w
1b00: 69 6c 6c 20 71 75 69 63 6b 6c 79 20 62 65 63 6f  ill quickly beco
1b10: 6d 65 20 77 6f 72 73 65 20 61 73 20 74 68 65 20  me worse as the 
1b20: 64 61 74 61 73 65 74 20 70 72 6f 67 72 65 73 73  dataset progress
1b30: 69 76 65 6c 79 20 69 6e 63 72 65 61 73 65 73 20  ively increases 
1b40: 69 6e 20 73 69 7a 65 2e 0d 0a 3c 62 72 3e 3c 62  in size...<br><b
1b50: 72 3e 0d 0a 3c 62 3e 43 6f 6e 63 6c 75 73 69 6f  r>..<b>Conclusio
1b60: 6e 3c 2f 62 3e 3a 3c 62 72 3e 0d 0a 53 6f 6d 65  n</b>:<br>..Some
1b70: 20 65 61 73 69 65 72 20 61 6e 64 20 73 6d 61 72   easier and smar
1b80: 74 65 72 20 61 70 70 72 6f 61 63 68 20 69 73 20  ter approach is 
1b90: 72 65 71 75 69 72 65 64 3a 20 70 6f 73 73 69 62  required: possib
1ba0: 6c 79 20 6f 6e 65 20 74 61 6b 69 6e 67 20 66 75  ly one taking fu
1bb0: 6c 6c 20 70 72 6f 66 69 74 20 66 72 6f 6d 20 61  ll profit from a
1bc0: 6e 20 3c 62 3e 52 2a 54 52 65 65 20 53 70 61 74  n <b>R*TRee Spat
1bd0: 69 61 6c 20 49 6e 64 65 78 3c 2f 62 3e 20 73 75  ial Index</b> su
1be0: 70 70 6f 72 74 69 6e 67 20 74 68 65 20 47 65 6f  pporting the Geo
1bf0: 6d 65 74 72 69 65 73 20 74 6f 20 62 65 20 73 65  metries to be se
1c00: 61 72 63 68 65 64 2e 3c 62 72 3e 3c 62 72 3e 0d  arched.<br><br>.
1c10: 0a 3c 68 72 3e 0d 0a 3c 68 32 3e 56 69 72 74 75  .<hr>..<h2>Virtu
1c20: 61 6c 4b 4e 4e 3c 2f 68 32 3e 0d 0a 53 74 61 72  alKNN</h2>..Star
1c30: 74 69 6e 67 20 77 69 74 68 20 76 65 72 73 69 6f  ting with versio
1c40: 6e 20 3c 62 3e 34 2e 34 2e 30 3c 2f 62 3e 20 53  n <b>4.4.0</b> S
1c50: 70 61 74 69 61 4c 69 74 65 20 73 75 70 70 6f 72  patiaLite suppor
1c60: 74 73 20 61 20 3c 62 3e 56 69 72 74 75 61 6c 4b  ts a <b>VirtualK
1c70: 4e 4e 3c 2f 62 3e 20 3c 61 20 68 72 65 66 3d 22  NN</b> <a href="
1c80: 68 74 74 70 73 3a 2f 2f 77 77 77 2e 73 71 6c 69  https://www.sqli
1c90: 74 65 2e 6f 72 67 2f 76 74 61 62 2e 68 74 6d 6c  te.org/vtab.html
1ca0: 22 3e 56 69 72 74 75 61 6c 20 54 61 62 6c 65 3c  ">Virtual Table<
1cb0: 2f 61 3e 20 73 70 65 63 69 66 69 63 61 6c 6c 79  /a> specifically
1cc0: 20 69 6e 74 65 6e 64 65 64 20 61 73 20 61 20 63   intended as a c
1cd0: 6f 6d 70 6c 65 74 65 20 61 6e 64 20 68 69 67 68  omplete and high
1ce0: 6c 79 20 65 66 66 69 63 69 65 6e 74 20 73 6f 6c  ly efficient sol
1cf0: 75 74 69 6f 6e 20 74 6f 20 74 68 65 20 3c 62 3e  ution to the <b>
1d00: 4b 4e 4e 3c 2f 62 3e 20 70 72 6f 62 6c 65 6d 2e  KNN</b> problem.
1d10: 0d 0a 3c 76 65 72 62 61 74 69 6d 3e 0d 0a 43 52  ..<verbatim>..CR
1d20: 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42  EATE VIRTUAL TAB
1d30: 4c 45 20 6b 6e 6e 20 55 53 49 4e 47 20 56 69 72  LE knn USING Vir
1d40: 74 75 61 6c 4b 4e 4e 28 29 3b 0d 0a 3c 2f 76 65  tualKNN();..</ve
1d50: 72 62 61 74 69 6d 3e 0d 0a 45 76 65 72 79 20 6e  rbatim>..Every n
1d60: 65 77 20 64 62 2d 66 69 6c 65 20 62 65 69 6e 67  ew db-file being
1d70: 20 63 72 65 61 74 65 64 20 77 69 74 68 20 3c 62   created with <b
1d80: 3e 34 2e 34 2e 30 3c 2f 62 3e 20 77 69 6c 6c 20  >4.4.0</b> will 
1d90: 61 6c 77 61 79 73 20 61 75 74 6f 6d 61 74 69 63  always automatic
1da0: 61 6c 6c 79 20 64 65 66 69 6e 65 20 61 20 3c 62  ally define a <b
1db0: 3e 4b 4e 4e 3c 2f 62 3e 20 76 69 72 74 75 61 6c  >KNN</b> virtual
1dc0: 20 74 61 62 6c 65 2e 3c 62 72 3e 0d 0a 4f 6e 20   table.<br>..On 
1dd0: 61 6e 79 20 65 61 72 6c 69 65 72 20 76 65 72 73  any earlier vers
1de0: 69 6f 6e 20 6f 66 20 61 20 53 70 61 74 69 61 4c  ion of a SpatiaL
1df0: 69 74 65 20 64 62 2d 66 69 6c 65 2c 20 79 6f 75  ite db-file, you
1e00: 20 63 61 6e 20 61 64 64 20 74 68 65 20 4b 4e 4e   can add the KNN
1e10: 20 73 75 70 70 6f 72 74 20 62 79 20 6d 61 6e 75   support by manu
1e20: 61 6c 6c 79 20 65 78 65 63 75 74 69 6e 67 20 74  ally executing t
1e30: 68 65 20 61 62 6f 76 65 20 53 51 4c 20 73 74 61  he above SQL sta
1e40: 74 65 6d 65 6e 74 2e 3c 62 72 3e 0d 0a 3c 62 3e  tement.<br>..<b>
1e50: 4e 6f 74 65 3c 2f 62 3e 3a 20 56 69 72 74 75 61  Note</b>: Virtua
1e60: 6c 4b 4e 4e 20 6e 65 63 65 73 73 61 72 69 6c 79  lKNN necessarily
1e70: 20 72 65 71 75 69 72 65 73 20 34 2e 34 2e 30 20   requires 4.4.0 
1e80: 62 69 6e 61 72 79 20 73 75 70 70 6f 72 74 2c 20  binary support, 
1e90: 73 6f 20 61 6e 79 20 61 74 74 65 6d 70 74 20 74  so any attempt t
1ea0: 6f 20 6f 70 65 6e 20 61 20 64 62 2d 66 69 6c 65  o open a db-file
1eb0: 20 69 6e 63 6c 75 64 69 6e 67 20 61 20 56 69 72   including a Vir
1ec0: 74 75 61 6c 4b 4e 4e 20 74 61 62 6c 65 20 62 79  tualKNN table by
1ed0: 20 75 73 69 6e 67 20 73 6f 6d 65 20 70 72 65 76   using some prev
1ee0: 69 6f 75 73 20 76 65 72 73 69 6f 6e 20 28 3c 3d  ious version (<=
1ef0: 20 3c 62 3e 34 2e 33 2e 30 61 3c 2f 62 3e 29 20   <b>4.3.0a</b>) 
1f00: 77 69 6c 6c 20 73 75 72 65 6c 79 20 72 61 69 73  will surely rais
1f10: 65 20 61 6e 20 65 72 72 6f 72 20 63 6f 6e 64 69  e an error condi
1f20: 74 69 6f 6e 2e 0d 0a 3c 68 33 3e 41 20 66 69 72  tion...<h3>A fir
1f30: 73 74 20 62 61 73 69 63 61 6c 6c 79 20 73 69 6d  st basically sim
1f40: 70 6c 65 20 4b 4e 4e 20 71 75 65 72 79 3c 2f 68  ple KNN query</h
1f50: 33 3e 0d 0a 3c 76 65 72 62 61 74 69 6d 3e 0d 0a  3>..<verbatim>..
1f60: 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 6b 6e  SELECT * FROM kn
1f70: 6e 0d 0a 57 48 45 52 45 20 66 5f 74 61 62 6c 65  n..WHERE f_table
1f80: 5f 6e 61 6d 65 20 3d 20 27 61 69 72 70 6f 72 74  _name = 'airport
1f90: 73 27 20 41 4e 44 20 72 65 66 5f 67 65 6f 6d 65  s' AND ref_geome
1fa0: 74 72 79 20 3d 20 4d 61 6b 65 50 6f 69 6e 74 28  try = MakePoint(
1fb0: 31 30 2c 20 34 33 29 3b 0d 0a 3c 2f 76 65 72 62  10, 43);..</verb
1fc0: 61 74 69 6d 3e 0d 0a 3c 74 61 62 6c 65 20 62 67  atim>..<table bg
1fd0: 63 6f 6c 6f 72 3d 22 23 64 30 66 66 62 30 22 20  color="#d0ffb0" 
1fe0: 63 65 6c 6c 73 70 61 63 69 6e 67 3d 22 34 22 20  cellspacing="4" 
1ff0: 63 65 6c 6c 70 61 64 64 69 6e 67 3d 22 34 22 20  cellpadding="4" 
2000: 62 6f 72 64 65 72 3d 22 31 22 3e 0d 0a 3c 74 72  border="1">..<tr
2010: 3e 3c 74 64 20 62 67 63 6f 6c 6f 72 3d 22 23 63  ><td bgcolor="#c
2020: 30 64 64 61 6f 22 3e 3c 62 3e 66 5f 74 61 62 6c  0ddao"><b>f_tabl
2030: 65 5f 6e 61 6d 65 3c 2f 62 3e 3c 2f 74 64 3e 3c  e_name</b></td><
2040: 74 64 20 62 67 63 6f 6c 6f 72 3d 22 23 63 30 64  td bgcolor="#c0d
2050: 64 61 6f 22 3e 3c 62 3e 66 5f 67 65 6f 6d 65 74  dao"><b>f_geomet
2060: 72 79 5f 63 6f 6c 75 6d 6e 3c 2f 62 3e 3c 2f 74  ry_column</b></t
2070: 64 3e 3c 74 64 20 62 67 63 6f 6c 6f 72 3d 22 23  d><td bgcolor="#
2080: 63 30 64 64 61 6f 22 3e 3c 62 3e 72 65 66 5f 67  c0ddao"><b>ref_g
2090: 65 6f 6d 65 74 72 79 3c 2f 62 3e 3c 2f 74 64 3e  eometry</b></td>
20a0: 3c 74 64 20 62 67 63 6f 6c 6f 72 3d 22 23 63 30  <td bgcolor="#c0
20b0: 64 64 61 6f 22 3e 3c 62 3e 6d 61 78 5f 69 74 65  ddao"><b>max_ite
20c0: 6d 73 3c 2f 62 3e 3c 2f 74 64 3e 3c 74 64 20 62  ms</b></td><td b
20d0: 67 63 6f 6c 6f 72 3d 22 23 63 30 64 64 61 6f 22  gcolor="#c0ddao"
20e0: 3e 3c 62 3e 70 6f 73 3c 2f 62 3e 3c 2f 74 64 3e  ><b>pos</b></td>
20f0: 3c 74 64 20 62 67 63 6f 6c 6f 72 3d 22 23 63 30  <td bgcolor="#c0
2100: 64 64 61 6f 22 3e 3c 62 3e 66 69 64 3c 2f 62 3e  ddao"><b>fid</b>
2110: 3c 2f 74 64 3e 3c 74 64 20 62 67 63 6f 6c 6f 72  </td><td bgcolor
2120: 3d 22 23 63 30 64 64 61 6f 22 3e 3c 62 3e 64 69  ="#c0ddao"><b>di
2130: 73 74 61 6e 63 65 3c 2f 62 3e 3c 2f 74 64 3e 3c  stance</b></td><
2140: 2f 74 72 3e 0d 0a 3c 74 72 3e 3c 74 64 3e 61 69  /tr>..<tr><td>ai
2150: 72 70 6f 72 74 73 3c 2f 74 64 3e 3c 74 64 3e 67  rports</td><td>g
2160: 65 6f 6d 3c 2f 74 64 3e 3c 74 64 3e 42 4c 4f 42  eom</td><td>BLOB
2170: 20 73 7a 3d 36 30 20 47 45 4f 4d 45 54 52 59 3c   sz=60 GEOMETRY<
2180: 2f 74 64 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72  /td><td align="r
2190: 69 67 68 74 22 3e 33 3c 2f 74 64 3e 3c 74 64 20  ight">3</td><td 
21a0: 61 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e 31 3c  align="right">1<
21b0: 2f 74 64 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72  /td><td align="r
21c0: 69 67 68 74 22 3e 36 32 39 39 36 32 33 3c 2f 74  ight">6299623</t
21d0: 64 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69 67  d><td align="rig
21e0: 68 74 22 3e 33 33 30 34 33 2e 33 31 39 35 32 30  ht">33043.319520
21f0: 3c 2f 74 64 3e 3c 2f 74 72 3e 0d 0a 3c 74 72 3e  </td></tr>..<tr>
2200: 3c 74 64 3e 61 69 72 70 6f 72 74 73 3c 2f 74 64  <td>airports</td
2210: 3e 3c 74 64 3e 67 65 6f 6d 3c 2f 74 64 3e 3c 74  ><td>geom</td><t
2220: 64 3e 42 4c 4f 42 20 73 7a 3d 36 30 20 47 45 4f  d>BLOB sz=60 GEO
2230: 4d 45 54 52 59 3c 2f 74 64 3e 3c 74 64 20 61 6c  METRY</td><td al
2240: 69 67 6e 3d 22 72 69 67 68 74 22 3e 33 3c 2f 74  ign="right">3</t
2250: 64 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69 67  d><td align="rig
2260: 68 74 22 3e 32 3c 2f 74 64 3e 3c 74 64 20 61 6c  ht">2</td><td al
2270: 69 67 6e 3d 22 72 69 67 68 74 22 3e 36 32 39 39  ign="right">6299
2280: 33 39 32 3c 2f 74 64 3e 3c 74 64 20 61 6c 69 67  392</td><td alig
2290: 6e 3d 22 72 69 67 68 74 22 3e 36 35 32 32 36 2e  n="right">65226.
22a0: 35 37 33 31 34 39 3c 2f 74 64 3e 3c 2f 74 72 3e  573149</td></tr>
22b0: 0d 0a 3c 74 72 3e 3c 74 64 3e 61 69 72 70 6f 72  ..<tr><td>airpor
22c0: 74 73 3c 2f 74 64 3e 3c 74 64 3e 67 65 6f 6d 3c  ts</td><td>geom<
22d0: 2f 74 64 3e 3c 74 64 3e 42 4c 4f 42 20 73 7a 3d  /td><td>BLOB sz=
22e0: 36 30 20 47 45 4f 4d 45 54 52 59 3c 2f 74 64 3e  60 GEOMETRY</td>
22f0: 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69 67 68 74  <td align="right
2300: 22 3e 33 3c 2f 74 64 3e 3c 74 64 20 61 6c 69 67  ">3</td><td alig
2310: 6e 3d 22 72 69 67 68 74 22 3e 33 3c 2f 74 64 3e  n="right">3</td>
2320: 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69 67 68 74  <td align="right
2330: 22 3e 36 32 39 39 36 32 38 3c 2f 74 64 3e 3c 74  ">6299628</td><t
2340: 64 20 61 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e  d align="right">
2350: 38 32 33 38 37 2e 30 31 34 30 32 38 3c 2f 74 64  82387.014028</td
2360: 3e 3c 2f 74 72 3e 0d 0a 3c 2f 74 61 62 6c 65 3e  ></tr>..</table>
2370: 3c 62 72 3e 0d 0a 3c 75 6c 3e 0d 0a 3c 6c 69 3e  <br>..<ul>..<li>
2380: 61 20 56 69 72 74 75 61 6c 4b 4e 4e 20 71 75 65  a VirtualKNN que
2390: 72 79 20 63 6c 6f 73 65 6c 79 20 72 65 73 65 6d  ry closely resem
23a0: 62 6c 65 73 20 61 20 56 69 72 74 75 61 6c 53 70  bles a VirtualSp
23b0: 61 74 69 61 6c 49 6e 64 65 78 20 71 75 65 72 79  atialIndex query
23c0: 2e 0d 0a 54 68 69 73 20 69 73 20 6e 6f 74 20 73  ...This is not s
23d0: 75 72 70 72 69 73 69 6e 67 2c 20 73 69 6e 63 65  urprising, since
23e0: 20 62 6f 74 68 20 6f 66 20 74 68 65 6d 20 61 72   both of them ar
23f0: 65 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 62 65  e intended to be
2400: 20 77 72 61 70 70 65 64 20 61 72 6f 75 6e 64 20   wrapped around 
2410: 61 6e 20 75 6e 64 65 72 6c 79 69 6e 67 20 3c 62  an underlying <b
2420: 3e 52 2a 54 72 65 65 20 53 70 61 74 69 61 6c 20  >R*Tree Spatial 
2430: 49 6e 64 65 78 3c 2f 62 3e 2e 3c 2f 6c 69 3e 20  Index</b>.</li> 
2440: 0d 0a 3c 6c 69 3e 41 6e 79 20 76 61 6c 69 64 20  ..<li>Any valid 
2450: 56 69 72 74 75 61 6c 4b 4e 4e 20 71 75 65 72 79  VirtualKNN query
2460: 20 6d 75 73 74 20 75 73 65 20 61 20 66 6f 72 6d   must use a form
2470: 20 73 75 63 68 20 61 73 3a 0d 0a 3c 62 3e 57 48   such as:..<b>WH
2480: 45 52 45 20 3c 69 3e 6b 6e 6e 2d 63 6f 6c 75 6d  ERE <i>knn-colum
2490: 6e 3c 2f 69 3e 20 3d 20 3c 69 3e 76 61 6c 75 65  n</i> = <i>value
24a0: 3c 2f 69 3e 20 41 4e 44 20 3c 69 3e 6b 6e 6e 2d  </i> AND <i>knn-
24b0: 63 6f 6c 75 6d 6e 3c 2f 69 3e 20 3d 20 3c 69 3e  column</i> = <i>
24c0: 76 61 6c 75 65 3c 2f 69 3e 20 2e 2e 2e 3c 2f 62  value</i> ...</b
24d0: 3e 3c 2f 6c 69 3e 0d 0a 3c 6c 69 3e 74 68 65 20  ></li>..<li>the 
24e0: 69 6e 70 75 74 20 63 6f 6c 75 6d 6e 73 20 28 74  input columns (t
24f0: 68 65 20 6f 6e 65 73 20 79 6f 75 20 63 61 6e 20  he ones you can 
2500: 72 65 66 65 72 65 6e 63 65 20 69 6e 74 6f 20 61  reference into a
2510: 20 3c 62 3e 57 48 45 52 45 3c 2f 62 3e 20 63 6c   <b>WHERE</b> cl
2520: 61 75 73 65 29 20 61 72 65 3a 0d 0a 3c 75 6c 3e  ause) are:..<ul>
2530: 0d 0a 3c 6c 69 3e 3c 62 3e 66 5f 74 61 62 6c 65  ..<li><b>f_table
2540: 5f 6e 61 6d 65 3c 2f 62 3e 20 28 3c 69 3e 6d 61  _name</b> (<i>ma
2550: 6e 64 61 74 6f 72 79 3c 2f 69 3e 29 3c 62 72 3e  ndatory</i>)<br>
2560: 0d 0a 6e 61 6d 65 20 6f 66 20 74 68 65 20 47 65  ..name of the Ge
2570: 6f 54 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69 6e  oTable containin
2580: 67 20 74 68 65 20 47 65 6f 6d 65 74 72 69 65 73  g the Geometries
2590: 20 74 6f 20 62 65 20 73 65 61 72 63 68 65 64 2e   to be searched.
25a0: 3c 2f 6c 69 3e 0d 0a 3c 6c 69 3e 3c 62 3e 66 5f  </li>..<li><b>f_
25b0: 67 65 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e 3c  geometry_column<
25c0: 2f 62 3e 20 28 3c 69 3e 6f 70 74 69 6f 6e 61 6c  /b> (<i>optional
25d0: 3c 2f 69 3e 29 3c 62 72 3e 0d 0a 6e 61 6d 65 20  </i>)<br>..name 
25e0: 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6f 66  of the column of
25f0: 20 74 68 65 20 61 62 6f 76 65 20 74 61 62 6c 65   the above table
2600: 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
2610: 47 65 6f 6d 65 74 72 69 65 73 20 74 6f 20 62 65  Geometries to be
2620: 20 73 65 61 72 63 68 65 64 2e 0d 0a 3c 6f 6c 3e   searched...<ol>
2630: 0d 0a 3c 6c 69 3e 49 66 20 74 68 65 20 74 61 62  ..<li>If the tab
2640: 6c 65 20 69 64 65 6e 74 69 66 69 65 64 20 62 79  le identified by
2650: 20 3c 62 3e 66 5f 74 61 62 6c 65 5f 6e 61 6d 65   <b>f_table_name
2660: 3c 2f 62 3e 20 6a 75 73 74 20 63 6f 6e 74 61 69  </b> just contai
2670: 6e 73 20 61 20 73 69 6e 67 6c 65 20 47 65 6f 6d  ns a single Geom
2680: 65 74 72 79 20 63 6f 6c 75 6d 6e 20 79 6f 75 20  etry column you 
2690: 63 61 6e 20 73 61 66 65 6c 79 20 6f 6d 69 74 20  can safely omit 
26a0: 74 68 65 20 3c 62 3e 66 5f 67 65 6f 6d 65 74 72  the <b>f_geometr
26b0: 79 5f 63 6f 6c 75 6d 6e 3c 2f 62 3e 20 61 72 67  y_column</b> arg
26c0: 75 6d 65 6e 74 20 28 69 74 20 77 69 6c 6c 20 62  ument (it will b
26d0: 65 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20  e automatically 
26e0: 73 65 74 29 2e 3c 2f 6c 69 3e 0d 0a 3c 6c 69 3e  set).</li>..<li>
26f0: 49 66 2c 20 68 6f 77 65 76 65 72 2c 20 74 68 65  If, however, the
2700: 20 74 61 62 6c 65 20 69 64 65 6e 74 69 66 69 65   table identifie
2710: 64 20 62 79 20 3c 62 3e 66 5f 74 61 62 6c 65 5f  d by <b>f_table_
2720: 6e 61 6d 65 3c 2f 62 3e 20 63 6f 6e 74 61 69 6e  name</b> contain
2730: 73 20 74 77 6f 20 28 6f 72 20 6d 6f 72 65 29 20  s two (or more) 
2740: 47 65 6f 6d 65 74 72 79 20 63 6f 6c 75 6d 6e 73  Geometry columns
2750: 20 65 78 70 6c 69 63 69 74 6c 79 20 73 70 65 63   explicitly spec
2760: 69 66 79 69 6e 67 20 74 68 65 20 3c 62 3e 66 5f  ifying the <b>f_
2770: 67 65 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e 3c  geometry_column<
2780: 2f 62 3e 20 61 72 67 75 6d 65 6e 74 20 69 73 20  /b> argument is 
2790: 73 74 72 69 63 74 6c 79 20 72 65 71 75 69 72 65  strictly require
27a0: 64 2c 20 74 6f 20 61 76 6f 69 64 20 61 6e 20 61  d, to avoid an a
27b0: 6d 62 69 67 75 6f 75 73 20 64 65 66 69 6e 69 74  mbiguous definit
27c0: 69 6f 6e 20 6f 66 20 74 68 65 20 73 65 61 72 63  ion of the searc
27d0: 68 20 63 6f 6e 74 65 78 74 2e 3c 2f 6c 69 3e 0d  h context.</li>.
27e0: 0a 3c 6c 69 3e 49 6e 20 62 6f 74 68 20 63 61 73  .<li>In both cas
27f0: 65 73 20 3c 62 3e 66 5f 74 61 62 6c 65 5f 6e 61  es <b>f_table_na
2800: 6d 65 3c 2f 62 3e 20 61 6e 64 20 3c 62 3e 66 5f  me</b> and <b>f_
2810: 67 65 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e 3c  geometry_column<
2820: 2f 62 3e 20 6d 75 73 74 20 65 78 61 63 74 6c 79  /b> must exactly
2830: 20 6d 61 74 63 68 20 61 20 70 72 6f 70 65 72 6c   match a properl
2840: 79 20 64 65 66 69 6e 65 64 20 47 65 6f 6d 65 74  y defined Geomet
2850: 72 79 20 63 6f 6c 75 6d 6e 20 73 75 70 70 6f 72  ry column suppor
2860: 74 65 64 20 62 79 20 61 20 63 6f 72 72 65 73 70  ted by a corresp
2870: 6f 6e 64 69 6e 67 20 53 70 61 74 69 61 6c 20 49  onding Spatial I
2880: 6e 64 65 78 2e 3c 2f 6c 69 3e 0d 0a 3c 2f 6f 6c  ndex.</li>..</ol
2890: 3e 3c 2f 6c 69 3e 0d 0a 3c 6c 69 3e 3c 62 3e 72  ></li>..<li><b>r
28a0: 65 66 5f 67 65 6f 6d 65 74 72 79 3c 2f 62 3e 20  ef_geometry</b> 
28b0: 28 3c 69 3e 6d 61 6e 64 61 74 6f 72 79 3c 2f 69  (<i>mandatory</i
28c0: 3e 29 3c 62 72 3e 0d 0a 61 6e 79 20 61 72 62 69  >)<br>..any arbi
28d0: 74 72 61 72 79 20 47 65 6f 6d 65 74 72 79 20 28  trary Geometry (
28e0: 3c 62 3e 50 4f 49 4e 54 3c 2f 62 3e 2c 20 3c 62  <b>POINT</b>, <b
28f0: 3e 4c 49 4e 45 53 54 52 49 4e 47 3c 2f 62 3e 2c  >LINESTRING</b>,
2900: 20 3c 62 3e 50 4f 4c 59 47 4f 4e 3c 2f 62 3e 20   <b>POLYGON</b> 
2910: 6f 72 20 77 68 61 74 65 76 65 72 20 65 6c 73 65  or whatever else
2920: 29 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 72 65  ) intended to re
2930: 70 72 65 73 65 6e 74 20 74 68 65 20 6f 72 69 67  present the orig
2940: 69 6e 20 6f 66 20 74 68 65 20 4b 4e 4e 20 73 65  in of the KNN se
2950: 61 72 63 68 2e 3c 62 72 3e 0d 0a 54 68 69 73 20  arch.<br>..This 
2960: 47 65 6f 6d 65 74 72 79 20 6d 75 73 74 20 63 6f  Geometry must co
2970: 6e 74 61 69 6e 20 74 68 65 20 73 61 6d 65 20 53  ntain the same S
2980: 52 49 44 20 6f 66 20 74 68 65 20 74 61 72 67 65  RID of the targe
2990: 74 20 47 65 6f 6d 65 74 72 69 65 73 20 74 6f 20  t Geometries to 
29a0: 62 65 20 73 65 61 72 63 68 65 64 2e 3c 2f 6c 69  be searched.</li
29b0: 3e 0d 0a 3c 6c 69 3e 3c 62 3e 6d 61 78 5f 69 74  >..<li><b>max_it
29c0: 65 6d 73 3c 2f 62 3e 20 28 3c 69 3e 6f 70 74 69  ems</b> (<i>opti
29d0: 6f 6e 61 6c 3c 2f 69 3e 29 3c 62 72 3e 0d 0a 6d  onal</i>)<br>..m
29e0: 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66  aximum number of
29f0: 20 72 6f 77 73 20 74 6f 20 62 65 20 72 65 74 75   rows to be retu
2a00: 72 6e 65 64 20 69 6e 74 6f 20 74 68 65 20 72 65  rned into the re
2a10: 73 75 6c 74 73 65 74 2e 3c 62 72 3e 0d 0a 54 68  sultset.<br>..Th
2a20: 65 20 76 61 6c 69 64 20 72 61 6e 67 65 20 69 73  e valid range is
2a30: 20 66 72 6f 6d 20 3c 62 3e 31 3c 2f 62 3e 20 74   from <b>1</b> t
2a40: 6f 20 3c 62 3e 31 30 32 34 3c 2f 62 3e 20 28 68  o <b>1024</b> (h
2a50: 69 67 68 65 72 20 76 61 6c 75 65 73 20 77 69 6c  igher values wil
2a60: 6c 20 72 65 71 75 69 72 65 20 61 20 6c 6f 6e 67  l require a long
2a70: 65 72 20 74 69 6d 65 20 74 6f 20 62 65 20 70 72  er time to be pr
2a80: 6f 63 65 73 73 65 64 29 2e 3c 62 72 3e 0d 0a 42  ocessed).<br>..B
2a90: 79 20 64 65 66 61 75 6c 74 20 6f 6e 6c 79 20 74  y default only t
2aa0: 68 65 20 66 69 72 73 74 20 3c 62 3e 33 3c 2f 62  he first <b>3</b
2ab0: 3e 20 6e 65 61 72 65 73 74 20 47 65 6f 6d 65 74  > nearest Geomet
2ac0: 72 69 65 73 20 77 69 6c 6c 20 62 65 20 69 64 65  ries will be ide
2ad0: 6e 74 69 66 69 65 64 2e 3c 2f 6c 69 3e 0d 0a 3c  ntified.</li>..<
2ae0: 2f 75 6c 3e 3c 2f 6c 69 3e 0d 0a 3c 6c 69 3e 74  /ul></li>..<li>t
2af0: 68 65 20 6f 75 74 70 75 74 20 63 6f 6c 75 6d 6e  he output column
2b00: 73 20 28 74 68 65 20 6f 6e 65 73 20 63 6f 6e 74  s (the ones cont
2b10: 61 69 6e 69 6e 67 20 76 61 6c 75 65 73 20 72 65  aining values re
2b20: 74 72 69 65 76 65 64 20 62 79 20 74 68 65 20 4b  trieved by the K
2b30: 4e 4e 20 73 65 61 72 63 68 29 20 61 72 65 3a 0d  NN search) are:.
2b40: 0a 3c 75 6c 3e 0d 0a 3c 6c 69 3e 3c 62 3e 70 6f  .<ul>..<li><b>po
2b50: 73 3c 2f 62 3e 20 28 3c 69 3e 49 4e 54 45 47 45  s</b> (<i>INTEGE
2b60: 52 3c 2f 69 3e 29 3c 62 72 3e 0d 0a 72 65 6c 61  R</i>)<br>..rela
2b70: 74 69 76 65 20 72 61 6e 6b 20 28 73 6f 72 74 65  tive rank (sorte
2b80: 64 20 62 79 20 64 69 73 74 61 6e 63 65 29 3a 20  d by distance): 
2b90: 74 68 65 20 63 6c 6f 73 65 73 74 20 69 74 65 6d  the closest item
2ba0: 20 77 69 6c 6c 20 62 65 20 3c 62 3e 23 31 3c 2f   will be <b>#1</
2bb0: 62 3e 2c 20 74 68 65 20 73 65 63 6f 6e 64 20 63  b>, the second c
2bc0: 6c 6f 73 65 73 74 20 69 74 65 6d 20 77 69 6c 6c  losest item will
2bd0: 20 62 65 20 3c 62 3e 23 32 3c 2f 62 3e 20 61 6e   be <b>#2</b> an
2be0: 64 20 73 6f 20 6f 6e 2e 3c 2f 6c 69 3e 0d 0a 3c  d so on.</li>..<
2bf0: 6c 69 3e 3c 62 3e 66 69 64 3c 2f 62 3e 20 28 3c  li><b>fid</b> (<
2c00: 69 3e 49 4e 54 45 47 45 52 3c 2f 69 3e 29 3c 62  i>INTEGER</i>)<b
2c10: 72 3e 0d 0a 74 68 65 20 75 6e 69 71 75 65 20 3c  r>..the unique <
2c20: 62 3e 52 4f 57 49 44 3c 2f 62 3e 20 76 61 6c 75  b>ROWID</b> valu
2c30: 65 20 6f 66 20 74 68 65 20 63 6f 72 72 65 73 70  e of the corresp
2c40: 6f 6e 64 69 6e 67 20 66 65 61 74 75 72 65 20 69  onding feature i
2c50: 6e 74 6f 20 74 68 65 20 73 65 61 72 63 68 65 64  nto the searched
2c60: 20 47 65 6f 54 61 62 6c 65 2e 3c 2f 6c 69 3e 0d   GeoTable.</li>.
2c70: 0a 3c 6c 69 3e 3c 62 3e 64 69 73 74 61 6e 63 65  .<li><b>distance
2c80: 3c 2f 62 3e 20 28 3c 69 3e 44 4f 55 42 4c 45 3c  </b> (<i>DOUBLE<
2c90: 2f 69 3e 29 3c 62 72 3e 0d 0a 74 68 65 20 6d 69  /i>)<br>..the mi
2ca0: 6e 69 6d 75 6d 20 64 69 73 74 61 6e 63 65 20 62  nimum distance b
2cb0: 65 74 77 65 65 6e 20 74 68 65 20 63 6f 72 72 65  etween the corre
2cc0: 73 70 6f 6e 64 69 6e 67 20 66 65 61 74 75 72 65  sponding feature
2cd0: 20 69 6e 74 6f 20 74 68 65 20 73 65 61 72 63 68   into the search
2ce0: 65 64 20 47 65 6f 54 61 62 6c 65 20 61 6e 64 20  ed GeoTable and 
2cf0: 74 68 65 20 6f 72 69 67 69 6e 20 64 65 66 69 6e  the origin defin
2d00: 65 64 20 62 79 20 3c 62 3e 72 65 66 5f 67 65 6f  ed by <b>ref_geo
2d10: 6d 65 74 72 79 3c 2f 62 3e 2e 0d 0a 3c 6f 6c 3e  metry</b>...<ol>
2d20: 0d 0a 3c 6c 69 3e 57 68 65 6e 20 74 68 65 20 64  ..<li>When the d
2d30: 65 66 69 6e 65 64 20 53 52 49 44 20 75 73 65 73  efined SRID uses
2d40: 20 61 20 3c 62 3e 70 6c 61 6e 61 72 3c 2f 62 3e   a <b>planar</b>
2d50: 20 28 61 6b 61 20 3c 69 3e 70 72 6f 6a 65 63 74   (aka <i>project
2d60: 65 64 3c 2f 69 3e 29 20 72 65 66 65 72 65 6e 63  ed</i>) referenc
2d70: 65 20 73 79 73 74 65 6d 2c 20 61 6c 6c 20 64 69  e system, all di
2d80: 73 74 61 6e 63 65 73 20 77 69 6c 6c 20 62 65 20  stances will be 
2d90: 72 65 74 75 72 6e 65 64 20 69 6e 20 74 68 65 20  returned in the 
2da0: 75 6e 69 74 73 20 64 65 66 69 6e 65 64 20 62 79  units defined by
2db0: 20 74 68 65 20 70 72 6f 6a 65 63 74 69 6f 6e 20   the projection 
2dc0: 28 6d 65 74 65 72 73 2c 20 66 65 65 74 2c 20 63  (meters, feet, c
2dd0: 68 61 69 6e 73 20 65 74 63 2e 29 2e 3c 2f 6c 69  hains etc.).</li
2de0: 3e 0d 0a 3c 6c 69 3e 57 68 65 6e 20 75 73 69 6e  >..<li>When usin
2df0: 67 20 61 20 3c 62 3e 67 65 6f 67 72 61 70 68 69  g a <b>geographi
2e00: 63 3c 2f 62 3e 20 74 79 70 65 20 28 3c 69 3e 6c  c</b> type (<i>l
2e10: 6f 6e 67 69 74 75 64 65 3c 2f 69 3e 20 61 6e 64  ongitude</i> and
2e20: 20 3c 69 3e 6c 61 74 69 74 75 64 65 3c 2f 69 3e   <i>latitude</i>
2e30: 20 3c 62 3e 64 65 67 72 65 65 73 3c 2f 62 3e 29   <b>degrees</b>)
2e40: 2c 20 61 6c 6c 20 64 69 73 74 61 6e 63 65 73 20  , all distances 
2e50: 77 69 6c 6c 20 61 6c 77 61 79 73 20 62 65 20 6d  will always be m
2e60: 65 61 73 75 72 65 64 20 69 6e 20 3c 62 3e 6d 65  easured in <b>me
2e70: 74 65 72 73 3c 2f 62 3e 2c 20 77 69 74 68 20 74  ters</b>, with t
2e80: 68 65 20 6d 6f 73 74 20 70 72 65 63 69 73 65 20  he most precise 
2e90: 67 65 6f 64 65 74 69 63 20 66 6f 72 6d 75 6c 61  geodetic formula
2ea0: 73 20 62 65 69 6e 67 20 61 75 74 6f 6d 61 74 69  s being automati
2eb0: 63 61 6c 6c 79 20 61 70 70 6c 69 65 64 2e 3c 2f  cally applied.</
2ec0: 6c 69 3e 0d 0a 3c 2f 6f 6c 3e 3c 2f 6c 69 3e 0d  li>..</ol></li>.
2ed0: 0a 3c 2f 75 6c 3e 3c 2f 6c 69 3e 0d 0a 3c 2f 75  .</ul></li>..</u
2ee0: 6c 3e 3c 62 72 3e 0d 0a 3c 68 72 3e 0d 0a 3c 68  l><br>..<hr>..<h
2ef0: 33 3e 41 20 73 65 63 6f 6e 64 20 73 61 6d 70 6c  3>A second sampl
2f00: 65 20 6f 66 20 61 20 6d 6f 72 65 20 73 6f 70 68  e of a more soph
2f10: 69 73 74 69 63 61 74 65 64 20 4b 4e 4e 20 71 75  isticated KNN qu
2f20: 65 72 79 3c 2f 68 33 3e 0d 0a 3c 76 65 72 62 61  ery</h3>..<verba
2f30: 74 69 6d 3e 0d 0a 53 45 4c 45 43 54 20 61 2e 70  tim>..SELECT a.p
2f40: 6f 73 2c 20 62 2e 6e 61 6d 65 2c 20 62 2e 63 6f  os, b.name, b.co
2f50: 75 6e 74 72 79 2c 20 61 2e 64 69 73 74 61 6e 63  untry, a.distanc
2f60: 65 20 2f 20 31 30 30 30 2e 30 20 41 53 20 64 69  e / 1000.0 AS di
2f70: 73 74 5f 6b 6d 0d 0a 46 52 4f 4d 20 6b 6e 6e 20  st_km..FROM knn 
2f80: 41 53 20 61 0d 0a 4a 4f 49 4e 20 61 69 72 70 6f  AS a..JOIN airpo
2f90: 72 74 73 20 41 53 20 62 20 4f 4e 20 28 62 2e 67  rts AS b ON (b.g
2fa0: 65 6f 6e 61 6d 65 69 64 20 3d 20 61 2e 66 69 64  eonameid = a.fid
2fb0: 29 0d 0a 57 48 45 52 45 20 66 5f 74 61 62 6c 65  )..WHERE f_table
2fc0: 5f 6e 61 6d 65 20 3d 20 27 61 69 72 70 6f 72 74  _name = 'airport
2fd0: 73 27 20 41 4e 44 20 72 65 66 5f 67 65 6f 6d 65  s' AND ref_geome
2fe0: 74 72 79 20 3d 20 4d 61 6b 65 50 6f 69 6e 74 28  try = MakePoint(
2ff0: 31 30 2c 20 34 33 29 20 41 4e 44 20 6d 61 78 5f  10, 43) AND max_
3000: 69 74 65 6d 73 20 3d 20 35 3b 0d 0a 3c 2f 76 65  items = 5;..</ve
3010: 72 62 61 74 69 6d 3e 0d 0a 3c 74 61 62 6c 65 20  rbatim>..<table 
3020: 62 67 63 6f 6c 6f 72 3d 22 23 64 30 66 66 62 30  bgcolor="#d0ffb0
3030: 22 20 63 65 6c 6c 73 70 61 63 69 6e 67 3d 22 34  " cellspacing="4
3040: 22 20 63 65 6c 6c 70 61 64 64 69 6e 67 3d 22 34  " cellpadding="4
3050: 22 20 62 6f 72 64 65 72 3d 22 31 22 3e 0d 0a 3c  " border="1">..<
3060: 74 72 3e 3c 74 64 20 62 67 63 6f 6c 6f 72 3d 22  tr><td bgcolor="
3070: 23 63 30 64 64 61 6f 22 3e 3c 62 3e 72 61 6e 6b  #c0ddao"><b>rank
3080: 3c 2f 62 3e 3c 2f 74 64 3e 3c 74 64 20 62 67 63  </b></td><td bgc
3090: 6f 6c 6f 72 3d 22 23 63 30 64 64 61 6f 22 3e 3c  olor="#c0ddao"><
30a0: 62 3e 67 65 6f 6e 61 6d 65 69 64 3c 2f 62 3e 3c  b>geonameid</b><
30b0: 2f 74 64 3e 3c 74 64 20 62 67 63 6f 6c 6f 72 3d  /td><td bgcolor=
30c0: 22 23 63 30 64 64 61 6f 22 3e 3c 62 3e 6e 61 6d  "#c0ddao"><b>nam
30d0: 65 3c 2f 62 3e 3c 2f 74 64 3e 3c 74 64 20 62 67  e</b></td><td bg
30e0: 63 6f 6c 6f 72 3d 22 23 63 30 64 64 61 6f 22 3e  color="#c0ddao">
30f0: 3c 62 3e 63 6f 75 6e 74 72 79 3c 2f 62 3e 3c 2f  <b>country</b></
3100: 74 64 3e 3c 74 64 20 62 67 63 6f 6c 6f 72 3d 22  td><td bgcolor="
3110: 23 63 30 64 64 61 6f 22 3e 3c 62 3e 64 69 73 74  #c0ddao"><b>dist
3120: 5f 6b 6d 3c 2f 62 3e 3c 2f 74 64 3e 3c 2f 74 72  _km</b></td></tr
3130: 3e 0d 0a 3c 74 72 3e 3c 74 64 20 61 6c 69 67 6e  >..<tr><td align
3140: 3d 22 72 69 67 68 74 22 3e 31 3c 2f 74 64 3e 3c  ="right">1</td><
3150: 74 64 20 61 6c 69 67 6e 3d 22 72 69 67 68 74 22  td align="right"
3160: 3e 36 32 39 39 36 32 33 3c 2f 74 64 3e 3c 74 64  >6299623</td><td
3170: 3e 4d 61 72 69 6e 61 20 44 69 20 43 61 6d 70 6f  >Marina Di Campo
3180: 3c 2f 74 64 3e 3c 74 64 3e 49 54 3c 2f 74 64 3e  </td><td>IT</td>
3190: 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69 67 68 74  <td align="right
31a0: 22 3e 33 33 2e 30 34 33 33 32 30 3c 2f 74 64 3e  ">33.043320</td>
31b0: 3c 2f 74 72 3e 0d 0a 3c 74 72 3e 3c 74 64 20 61  </tr>..<tr><td a
31c0: 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e 32 3c 2f  lign="right">2</
31d0: 74 64 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69  td><td align="ri
31e0: 67 68 74 22 3e 36 32 39 39 33 39 32 3c 2f 74 64  ght">6299392</td
31f0: 3e 3c 74 64 3e 42 61 73 74 69 61 2d 50 6f 72 65  ><td>Bastia-Pore
3200: 74 74 61 3c 2f 74 64 3e 3c 74 64 3e 46 52 3c 2f  tta</td><td>FR</
3210: 74 64 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69  td><td align="ri
3220: 67 68 74 22 3e 36 35 2e 32 32 36 35 37 33 3c 2f  ght">65.226573</
3230: 74 64 3e 3c 2f 74 72 3e 0d 0a 3c 74 72 3e 3c 74  td></tr>..<tr><t
3240: 64 20 61 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e  d align="right">
3250: 33 3c 2f 74 64 3e 3c 74 64 20 61 6c 69 67 6e 3d  3</td><td align=
3260: 22 72 69 67 68 74 22 3e 36 32 39 39 36 32 38 3c  "right">6299628<
3270: 2f 74 64 3e 3c 74 64 3e 50 69 73 61 20 2f 20 53  /td><td>Pisa / S
3280: 2e 20 47 69 75 73 74 6f 3c 2f 74 64 3e 3c 74 64  . Giusto</td><td
3290: 3e 49 54 3c 2f 74 64 3e 3c 74 64 20 61 6c 69 67  >IT</td><td alig
32a0: 6e 3d 22 72 69 67 68 74 22 3e 38 32 2e 33 38 37  n="right">82.387
32b0: 30 31 34 3c 2f 74 64 3e 3c 2f 74 72 3e 0d 0a 3c  014</td></tr>..<
32c0: 74 72 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69  tr><td align="ri
32d0: 67 68 74 22 3e 34 3c 2f 74 64 3e 3c 74 64 20 61  ght">4</td><td a
32e0: 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e 36 32 39  lign="right">629
32f0: 39 36 33 30 3c 2f 74 64 3e 3c 74 64 3e 47 72 6f  9630</td><td>Gro
3300: 73 73 65 74 6f 20 41 69 72 70 6f 72 74 3c 2f 74  sseto Airport</t
3310: 64 3e 3c 74 64 3e 49 54 3c 2f 74 64 3e 3c 74 64  d><td>IT</td><td
3320: 20 61 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e 39   align="right">9
3330: 31 2e 35 34 39 37 37 33 3c 2f 74 64 3e 3c 2f 74  1.549773</td></t
3340: 72 3e 0d 0a 3c 74 72 3e 3c 74 64 20 61 6c 69 67  r>..<tr><td alig
3350: 6e 3d 22 72 69 67 68 74 22 3e 35 3c 2f 74 64 3e  n="right">5</td>
3360: 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69 67 68 74  <td align="right
3370: 22 3e 36 36 39 34 34 39 35 3c 2f 74 64 3e 3c 74  ">6694495</td><t
3380: 64 3e 43 6f 72 74 65 3c 2f 74 64 3e 3c 74 64 3e  d>Corte</td><td>
3390: 46 52 3c 2f 74 64 3e 3c 74 64 20 61 6c 69 67 6e  FR</td><td align
33a0: 3d 22 72 69 67 68 74 22 3e 31 30 32 2e 38 31 39  ="right">102.819
33b0: 37 37 38 3c 2f 74 64 3e 3c 2f 74 72 3e 0d 0a 3c  778</td></tr>..<
33c0: 2f 74 61 62 6c 65 3e 3c 62 72 3e 0d 0a 54 68 69  /table><br>..Thi
33d0: 73 20 73 65 63 6f 6e 64 20 4b 4e 4e 20 71 75 65  s second KNN que
33e0: 72 79 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 74  ry will return t
33f0: 68 65 20 73 61 6d 65 20 69 64 65 6e 74 69 63 61  he same identica
3400: 6c 20 72 65 73 75 6c 74 73 20 77 65 20 61 6c 72  l results we alr
3410: 65 61 64 79 20 72 65 63 65 69 76 65 64 20 62 65  eady received be
3420: 66 6f 72 65 20 62 79 20 75 73 69 6e 67 20 74 68  fore by using th
3430: 65 20 73 69 6d 70 6c 65 20 61 70 70 72 6f 61 63  e simple approac
3440: 68 2e 3c 62 72 3e 0d 0a 54 68 65 20 73 74 72 69  h.<br>..The stri
3450: 6b 69 6e 67 20 64 69 66 66 65 72 65 6e 63 65 20  king difference 
3460: 69 73 20 74 68 61 74 20 74 68 69 73 20 4b 4e 4e  is that this KNN
3470: 20 71 75 65 72 79 20 73 75 63 63 65 73 73 66 75   query successfu
3480: 6c 6c 79 20 63 6f 6d 70 6c 65 74 65 73 20 69 6e  lly completes in
3490: 20 6a 75 73 74 20 61 20 66 72 61 63 74 69 6f 6e   just a fraction
34a0: 20 6f 66 20 61 20 73 65 63 6f 6e 64 2c 20 77 68   of a second, wh
34b0: 69 6c 65 20 65 78 65 63 75 74 69 6e 67 20 74 68  ile executing th
34c0: 65 20 66 69 72 73 74 20 69 6e 65 66 66 69 63 69  e first ineffici
34d0: 65 6e 74 20 71 75 65 72 69 65 73 20 72 65 71 75  ent queries requ
34e0: 69 72 65 64 20 6d 61 6e 79 20 73 65 63 6f 6e 64  ired many second
34f0: 73 20 28 61 6e 64 20 74 68 61 74 20 77 69 74 68  s (and that with
3500: 20 61 20 72 65 6c 61 74 69 76 65 6c 79 20 73 6d   a relatively sm
3510: 61 6c 6c 20 64 61 74 61 73 65 74 29 2e 3c 62 72  all dataset).<br
3520: 3e 3c 62 72 3e 0d 0a 3c 62 3e 53 75 6d 6d 61 72  ><br>..<b>Summar
3530: 79 3c 2f 62 3e 3a 20 4b 4e 4e 20 71 75 65 72 69  y</b>: KNN queri
3540: 65 73 20 61 72 65 20 72 65 61 6c 6c 79 20 65 66  es are really ef
3550: 66 69 63 69 65 6e 74 20 61 6e 64 20 66 61 73 74  ficient and fast
3560: 2c 20 62 65 63 61 75 73 65 20 74 68 65 79 20 64  , because they d
3570: 69 72 65 63 74 6c 79 20 69 6e 74 65 72 61 63 74  irectly interact
3580: 20 77 69 74 68 20 74 68 65 20 6c 6f 77 65 72 6d   with the lowerm
3590: 6f 73 74 20 6c 65 76 65 6c 73 20 6f 66 20 74 68  ost levels of th
35a0: 65 20 52 2a 54 72 65 65 20 53 70 61 74 69 61 6c  e R*Tree Spatial
35b0: 20 49 6e 64 65 78 20 69 6d 70 6c 65 6d 65 6e 74   Index implement
35c0: 61 74 69 6f 6e 2e 3c 62 72 3e 3c 62 72 3e 0d 0a  ation.<br><br>..
35d0: 3c 68 72 3e 0d 0a 3c 68 33 3e 41 20 74 68 69 72  <hr>..<h3>A thir
35e0: 64 20 73 61 6d 70 6c 65 20 6f 66 20 4b 4e 4e 20  d sample of KNN 
35f0: 71 75 65 72 79 3c 2f 68 33 3e 0d 0a 3c 76 65 72  query</h3>..<ver
3600: 62 61 74 69 6d 3e 0d 0a 53 45 4c 45 43 54 20 61  batim>..SELECT a
3610: 2e 70 6f 73 20 41 53 20 72 61 6e 6b 2c 20 62 2e  .pos AS rank, b.
3620: 67 65 6f 6e 61 6d 65 69 64 2c 20 62 2e 6e 61 6d  geonameid, b.nam
3630: 65 2c 20 62 2e 63 6f 75 6e 74 72 79 2c 20 61 2e  e, b.country, a.
3640: 64 69 73 74 61 6e 63 65 20 2f 20 31 30 30 30 2e  distance / 1000.
3650: 30 20 41 53 20 64 69 73 74 5f 6b 6d 0d 0a 46 52  0 AS dist_km..FR
3660: 4f 4d 20 6b 6e 6e 20 41 53 20 61 0d 0a 4a 4f 49  OM knn AS a..JOI
3670: 4e 20 61 69 72 70 6f 72 74 73 20 41 53 20 62 20  N airports AS b 
3680: 4f 4e 20 28 62 2e 67 65 6f 6e 61 6d 65 69 64 20  ON (b.geonameid 
3690: 3d 20 61 2e 66 69 64 29 0d 0a 57 48 45 52 45 20  = a.fid)..WHERE 
36a0: 66 5f 74 61 62 6c 65 5f 6e 61 6d 65 20 3d 20 27  f_table_name = '
36b0: 61 69 72 70 6f 72 74 73 27 20 41 4e 44 20 72 65  airports' AND re
36c0: 66 5f 67 65 6f 6d 65 74 72 79 20 3d 20 4d 61 6b  f_geometry = Mak
36d0: 65 50 6f 69 6e 74 28 2d 31 37 2e 33 2c 20 2d 34  ePoint(-17.3, -4
36e0: 34 29 20 41 4e 44 20 6d 61 78 5f 69 74 65 6d 73  4) AND max_items
36f0: 20 3d 20 35 3b 0d 0a 3c 2f 76 65 72 62 61 74 69   = 5;..</verbati
3700: 6d 3e 0d 0a 3c 74 61 62 6c 65 20 62 67 63 6f 6c  m>..<table bgcol
3710: 6f 72 3d 22 23 64 30 66 66 62 30 22 20 63 65 6c  or="#d0ffb0" cel
3720: 6c 73 70 61 63 69 6e 67 3d 22 34 22 20 63 65 6c  lspacing="4" cel
3730: 6c 70 61 64 64 69 6e 67 3d 22 34 22 20 62 6f 72  lpadding="4" bor
3740: 64 65 72 3d 22 31 22 3e 0d 0a 3c 74 72 3e 3c 74  der="1">..<tr><t
3750: 64 20 62 67 63 6f 6c 6f 72 3d 22 23 63 30 64 64  d bgcolor="#c0dd
3760: 61 6f 22 3e 3c 62 3e 72 61 6e 6b 3c 2f 62 3e 3c  ao"><b>rank</b><
3770: 2f 74 64 3e 3c 74 64 20 62 67 63 6f 6c 6f 72 3d  /td><td bgcolor=
3780: 22 23 63 30 64 64 61 6f 22 3e 3c 62 3e 67 65 6f  "#c0ddao"><b>geo
3790: 6e 61 6d 65 69 64 3c 2f 62 3e 3c 2f 74 64 3e 3c  nameid</b></td><
37a0: 74 64 20 62 67 63 6f 6c 6f 72 3d 22 23 63 30 64  td bgcolor="#c0d
37b0: 64 61 6f 22 3e 3c 62 3e 6e 61 6d 65 3c 2f 62 3e  dao"><b>name</b>
37c0: 3c 2f 74 64 3e 3c 74 64 20 62 67 63 6f 6c 6f 72  </td><td bgcolor
37d0: 3d 22 23 63 30 64 64 61 6f 22 3e 3c 62 3e 63 6f  ="#c0ddao"><b>co
37e0: 75 6e 74 72 79 3c 2f 62 3e 3c 2f 74 64 3e 3c 74  untry</b></td><t
37f0: 64 20 62 67 63 6f 6c 6f 72 3d 22 23 63 30 64 64  d bgcolor="#c0dd
3800: 61 6f 22 3e 3c 62 3e 64 69 73 74 5f 6b 6d 3c 2f  ao"><b>dist_km</
3810: 62 3e 3c 2f 74 64 3e 3c 2f 74 72 3e 0d 0a 3c 74  b></td></tr>..<t
3820: 72 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69 67  r><td align="rig
3830: 68 74 22 3e 31 3c 2f 74 64 3e 3c 74 64 20 61 6c  ht">1</td><td al
3840: 69 67 6e 3d 22 72 69 67 68 74 22 3e 37 36 36 38  ign="right">7668
3850: 34 33 33 3c 2f 74 64 3e 3c 74 64 3e 43 61 62 6f  433</td><td>Cabo
3860: 20 46 72 69 6f 20 41 69 72 70 6f 72 74 3c 2f 74   Frio Airport</t
3870: 64 3e 3c 74 64 3e 42 52 3c 2f 74 64 3e 3c 74 64  d><td>BR</td><td
3880: 20 61 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e 33   align="right">3
3890: 32 35 36 2e 35 38 39 39 36 34 3c 2f 74 64 3e 3c  256.589964</td><
38a0: 2f 74 72 3e 0d 0a 3c 74 72 3e 3c 74 64 20 61 6c  /tr>..<tr><td al
38b0: 69 67 6e 3d 22 72 69 67 68 74 22 3e 32 3c 2f 74  ign="right">2</t
38c0: 64 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69 67  d><td align="rig
38d0: 68 74 22 3e 33 33 35 39 30 30 31 3c 2f 74 64 3e  ht">3359001</td>
38e0: 3c 74 64 3e 59 6f 75 6e 67 73 66 69 65 6c 64 3c  <td>Youngsfield<
38f0: 2f 74 64 3e 3c 74 64 3e 5a 41 3c 2f 74 64 3e 3c  /td><td>ZA</td><
3900: 74 64 20 61 6c 69 67 6e 3d 22 72 69 67 68 74 22  td align="right"
3910: 3e 33 32 36 32 2e 32 37 31 30 32 37 3c 2f 74 64  >3262.271027</td
3920: 3e 3c 2f 74 72 3e 0d 0a 3c 74 72 3e 3c 74 64 20  ></tr>..<tr><td 
3930: 61 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e 33 3c  align="right">3<
3940: 2f 74 64 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72  /td><td align="r
3950: 69 67 68 74 22 3e 37 37 33 30 31 35 33 3c 2f 74  ight">7730153</t
3960: 64 3e 3c 74 64 3e 55 6d 62 65 72 74 6f 20 4d 6f  d><td>Umberto Mo
3970: 64 69 61 6e 6f 20 41 69 72 70 6f 72 74 3c 2f 74  diano Airport</t
3980: 64 3e 3c 74 64 3e 42 52 3c 2f 74 64 3e 3c 74 64  d><td>BR</td><td
3990: 20 61 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e 33   align="right">3
39a0: 32 36 32 2e 36 39 33 30 33 32 3c 2f 74 64 3e 3c  262.693032</td><
39b0: 2f 74 72 3e 0d 0a 3c 74 72 3e 3c 74 64 20 61 6c  /tr>..<tr><td al
39c0: 69 67 6e 3d 22 72 69 67 68 74 22 3e 34 3c 2f 74  ign="right">4</t
39d0: 64 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69 67  d><td align="rig
39e0: 68 74 22 3e 33 33 36 32 33 35 35 3c 2f 74 64 3e  ht">3362355</td>
39f0: 3c 74 64 3e 52 6f 62 62 65 6e 20 49 73 6c 61 6e  <td>Robben Islan
3a00: 64 3c 2f 74 64 3e 3c 74 64 3e 5a 41 3c 2f 74 64  d</td><td>ZA</td
3a10: 3e 3c 74 64 20 61 6c 69 67 6e 3d 22 72 69 67 68  ><td align="righ
3a20: 74 22 3e 33 32 36 35 2e 33 30 32 35 36 38 3c 2f  t">3265.302568</
3a30: 74 64 3e 3c 2f 74 72 3e 0d 0a 3c 74 72 3e 3c 74  td></tr>..<tr><t
3a40: 64 20 61 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e  d align="right">
3a50: 35 3c 2f 74 64 3e 3c 74 64 20 61 6c 69 67 6e 3d  5</td><td align=
3a60: 22 72 69 67 68 74 22 3e 36 33 30 30 36 32 32 3c  "right">6300622<
3a70: 2f 74 64 3e 3c 74 64 3e 53 2e 20 50 2e 20 41 6c  /td><td>S. P. Al
3a80: 64 65 69 61 20 41 65 72 6f 64 72 6f 6d 65 3c 2f  deia Aerodrome</
3a90: 74 64 3e 3c 74 64 3e 42 52 3c 2f 74 64 3e 3c 74  td><td>BR</td><t
3aa0: 64 20 61 6c 69 67 6e 3d 22 72 69 67 68 74 22 3e  d align="right">
3ab0: 33 32 36 37 2e 32 33 34 31 34 32 3c 2f 74 64 3e  3267.234142</td>
3ac0: 3c 2f 74 72 3e 0d 0a 3c 2f 74 61 62 6c 65 3e 3c  </tr>..</table><
3ad0: 62 72 3e 0d 0a 54 68 69 73 20 6c 61 73 74 20 4b  br>..This last K
3ae0: 4e 4e 20 71 75 65 72 79 20 69 73 20 76 65 72 79  NN query is very
3af0: 20 73 69 6d 69 6c 61 72 20 74 6f 20 74 68 65 20   similar to the 
3b00: 70 72 65 76 69 6f 75 73 20 6f 6e 65 2e 3c 62 72  previous one.<br
3b10: 3e 0d 0a 54 68 69 73 20 74 69 6d 65 20 77 65 27  >..This time we'
3b20: 76 65 20 70 6c 61 63 65 64 20 74 68 65 20 6f 72  ve placed the or
3b30: 69 67 69 6e 20 6c 6f 63 61 74 69 6f 6e 20 73 6f  igin location so
3b40: 6d 65 77 68 65 72 65 20 69 6e 20 74 68 65 20 62  mewhere in the b
3b50: 6c 75 65 20 64 65 65 70 20 77 61 74 65 72 73 20  lue deep waters 
3b60: 6f 66 20 74 68 65 20 53 6f 75 74 68 20 41 74 6c  of the South Atl
3b70: 61 6e 74 69 63 20 28 6d 6f 72 65 20 6f 72 20 6c  antic (more or l
3b80: 65 73 73 20 6d 69 64 77 61 79 20 62 65 74 77 65  ess midway betwe
3b90: 65 6e 20 42 72 61 7a 69 6c 20 61 6e 64 20 53 6f  en Brazil and So
3ba0: 75 74 68 20 41 66 72 69 63 61 29 20 61 6e 64 20  uth Africa) and 
3bb0: 63 6f 6e 73 65 71 75 65 6e 74 6c 79 20 74 68 65  consequently the
3bc0: 20 6e 65 61 72 65 73 74 20 61 69 72 70 6f 72 74   nearest airport
3bd0: 73 20 61 72 65 20 6e 6f 74 20 72 65 61 6c 6c 79  s are not really
3be0: 20 73 6f 20 6e 65 61 72 2c 20 62 65 63 61 75 73   so near, becaus
3bf0: 65 20 74 68 65 79 20 61 72 65 20 6c 6f 63 61 74  e they are locat
3c00: 65 64 20 6d 61 6e 79 20 74 68 6f 75 73 61 6e 64  ed many thousand
3c10: 73 20 4b 6d 20 61 77 61 79 2e 0d 0a 41 73 20 79  s Km away...As y
3c20: 6f 75 20 63 61 6e 20 65 61 73 69 6c 79 20 63 68  ou can easily ch
3c30: 65 63 6b 20 62 79 20 79 6f 75 72 73 65 6c 66 20  eck by yourself 
3c40: 61 20 4b 4e 4e 20 71 75 65 72 79 20 77 69 6c 6c  a KNN query will
3c50: 20 65 66 66 69 63 69 65 6e 74 6c 79 20 70 65 72   efficiently per
3c60: 66 6f 72 6d 20 65 76 65 6e 20 75 6e 64 65 72 20  form even under 
3c70: 73 75 63 68 20 75 6e 75 73 75 61 6c 20 63 6f 6e  such unusual con
3c80: 64 69 74 69 6f 6e 73 2e 3c 62 72 3e 3c 62 72 3e  ditions.<br><br>
3c90: 0d 0a 3c 69 6d 67 20 73 72 63 3d 22 68 74 74 70  ..<img src="http
3ca0: 73 3a 2f 2f 77 77 77 2e 67 61 69 61 2d 67 69 73  s://www.gaia-gis
3cb0: 2e 69 74 2f 67 61 69 61 2d 73 69 6e 73 2f 6b 6e  .it/gaia-sins/kn
3cc0: 6e 2f 73 6f 75 74 68 2d 61 74 6c 61 6e 74 69 63  n/south-atlantic
3cd0: 2e 70 6e 67 22 20 61 6c 74 3d 22 53 6f 75 74 68  .png" alt="South
3ce0: 20 41 74 6c 61 6e 74 69 63 22 3e 3c 62 72 3e 3c   Atlantic"><br><
3cf0: 62 72 3e 0d 0a 3c 62 3e 53 75 6d 6d 61 72 79 3c  br>..<b>Summary<
3d00: 2f 62 3e 3a 20 4b 4e 4e 20 71 75 65 72 69 65 73  /b>: KNN queries
3d10: 20 6e 65 76 65 72 20 61 73 73 75 6d 65 20 61 6e   never assume an
3d20: 79 20 70 72 65 64 65 66 69 6e 65 64 20 64 69 73  y predefined dis
3d30: 74 72 69 62 75 74 69 6f 6e 20 6f 66 20 74 68 65  tribution of the
3d40: 20 73 65 61 72 63 68 65 64 20 69 74 65 6d 73 2c   searched items,
3d50: 20 61 6e 64 20 77 69 6c 6c 20 65 78 74 72 61 63   and will extrac
3d60: 74 20 74 68 65 20 63 6f 72 72 65 63 74 20 72 65  t the correct re
3d70: 73 75 6c 74 73 20 28 73 6f 72 74 65 64 20 62 79  sults (sorted by
3d80: 20 64 69 73 74 61 6e 63 65 29 2c 20 64 65 73 70   distance), desp
3d90: 69 74 65 20 61 20 76 65 72 79 20 69 72 72 65 67  ite a very irreg
3da0: 75 6c 61 72 20 73 61 6d 70 6c 65 20 64 69 73 74  ular sample dist
3db0: 72 69 62 75 74 69 6f 6e 2e 3c 62 72 3e 3c 62 72  ribution.<br><br
3dc0: 3e 0d 0a 3c 68 72 3e 0d 0a 3c 68 32 3e 41 64 76  >..<hr>..<h2>Adv
3dd0: 61 6e 63 65 64 20 74 75 74 6f 72 69 61 6c 3c 2f  anced tutorial</
3de0: 68 32 3e 0d 0a 59 6f 75 20 63 61 6e 20 3c 61 20  h2>..You can <a 
3df0: 68 72 65 66 3d 22 68 74 74 70 73 3a 2f 2f 77 77  href="https://ww
3e00: 77 2e 67 61 69 61 2d 67 69 73 2e 69 74 2f 67 61  w.gaia-gis.it/ga
3e10: 69 61 2d 73 69 6e 73 2f 6b 6e 6e 2f 74 75 73 63  ia-sins/knn/tusc
3e20: 61 6e 79 5f 68 6f 75 73 65 6e 75 6d 62 65 72 73  any_housenumbers
3e30: 2e 37 7a 22 3e 64 6f 77 6e 6c 6f 61 64 3c 2f 61  .7z">download</a
3e40: 3e 20 74 68 65 20 73 61 6d 70 6c 65 20 64 62 2d  > the sample db-
3e50: 66 69 6c 65 20 75 73 65 64 20 69 6e 20 74 68 65  file used in the
3e60: 20 73 61 6d 70 6c 65 73 20 6f 66 20 74 68 69 73   samples of this
3e70: 20 61 64 76 61 6e 63 65 64 20 74 75 74 6f 72 69   advanced tutori
3e80: 61 6c 2e 3c 62 72 3e 3c 62 72 3e 0d 0a 54 68 69  al.<br><br>..Thi
3e90: 73 20 69 73 20 61 20 6d 75 63 68 20 6d 6f 72 65  s is a much more
3ea0: 20 64 65 6d 61 6e 64 69 6e 67 20 64 61 74 61 73   demanding datas
3eb0: 65 74 20 77 69 74 68 20 61 20 63 6f 6c 6c 65 63  et with a collec
3ec0: 74 69 6f 6e 20 6f 66 20 61 62 6f 75 74 20 3c 62  tion of about <b
3ed0: 3e 32 2e 34 20 6d 69 6c 6c 69 6f 6e 20 68 6f 75  >2.4 million hou
3ee0: 73 65 20 6e 75 6d 62 65 72 73 3c 2f 62 3e 2e 3c  se numbers</b>.<
3ef0: 62 72 3e 0d 0a 54 68 65 20 6f 72 69 67 69 6e 61  br>..The origina
3f00: 6c 20 69 6e 70 75 74 20 73 68 61 70 65 66 69 6c  l input shapefil
3f10: 65 20 77 61 73 20 64 6f 77 6e 6c 6f 61 64 65 64  e was downloaded
3f20: 20 66 72 6f 6d 20 3c 61 20 68 72 65 66 3d 22 68   from <a href="h
3f30: 74 74 70 3a 2f 2f 77 77 77 35 30 32 2e 72 65 67  ttp://www502.reg
3f40: 69 6f 6e 65 2e 74 6f 73 63 61 6e 61 2e 69 74 2f  ione.toscana.it/
3f50: 67 65 6f 73 63 6f 70 69 6f 2f 63 61 72 74 6f 74  geoscopio/cartot
3f60: 65 63 61 2e 68 74 6d 6c 22 3e 54 75 73 63 61 6e  eca.html">Tuscan
3f70: 79 20 52 65 67 69 6f 6e 3c 2f 61 3e 20 28 3c 62  y Region</a> (<b
3f80: 3e 3c 69 3e 47 72 61 66 6f 20 53 74 72 61 64 61  ><i>Grafo Strada
3f90: 6c 65 20 63 69 76 69 63 69 2e 73 68 70 3c 2f 69  le civici.shp</i
3fa0: 3e 3c 2f 62 3e 29 20 61 6e 64 20 77 61 73 20 73  ></b>) and was s
3fb0: 75 62 73 65 71 75 65 6e 74 6c 79 20 72 65 61 72  ubsequently rear
3fc0: 72 61 6e 67 65 64 20 69 6e 74 6f 20 61 20 6d 6f  ranged into a mo
3fd0: 72 65 20 63 6f 6e 76 65 6e 69 65 6e 74 20 66 6f  re convenient fo
3fe0: 72 6d 2e 3c 62 72 3e 0d 0a 54 68 65 20 64 61 74  rm.<br>..The dat
3ff0: 61 73 65 74 20 69 73 20 72 65 6c 65 61 73 65 64  aset is released
4000: 20 75 6e 64 65 72 20 74 68 65 20 3c 62 3e 43 43   under the <b>CC
4010: 2d 42 59 2d 53 41 20 34 2e 30 3c 2f 62 3e 20 6c  -BY-SA 4.0</b> l
4020: 69 63 65 6e 73 65 20 74 65 72 6d 73 2e 3c 62 72  icense terms.<br
4030: 3e 0d 0a 54 68 65 20 75 73 65 64 20 72 65 66 65  >..The used refe
4040: 72 65 6e 63 65 20 73 79 73 74 65 6d 20 69 73 20  rence system is 
4050: 3c 62 3e 33 30 30 33 20 4d 6f 6e 74 65 20 4d 61  <b>3003 Monte Ma
4060: 72 69 6f 20 2f 20 49 74 61 6c 79 20 7a 6f 6e 65  rio / Italy zone
4070: 20 31 3c 2f 62 3e 3b 20 74 68 69 73 20 69 73 20   1</b>; this is 
4080: 61 20 3c 62 3e 70 6c 61 6e 61 72 3c 2f 62 3e 20  a <b>planar</b> 
4090: 28 3c 69 3e 70 72 6f 6a 65 63 74 65 64 3c 2f 69  (<i>projected</i
40a0: 3e 29 20 72 65 66 65 72 65 6e 63 65 20 73 79 73  >) reference sys
40b0: 74 65 6d 20 6d 65 61 73 75 72 65 64 20 69 6e 20  tem measured in 
40c0: 3c 62 3e 6d 65 74 65 72 73 3c 2f 62 3e 2e 0d 0a  <b>meters</b>...
40d0: 3c 62 72 3e 0d 0a 3c 76 65 72 62 61 74 69 6d 3e  <br>..<verbatim>
40e0: 0d 0a 53 45 4c 45 43 54 20 61 2e 70 6f 73 20 41  ..SELECT a.pos A
40f0: 53 20 72 61 6e 6b 2c 20 61 2e 66 69 64 20 41 53  S rank, a.fid AS
4100: 20 66 69 64 2c 20 61 2e 64 69 73 74 61 6e 63 65   fid, a.distance
4110: 20 41 53 20 64 69 73 74 5f 6d 2c 20 64 2e 70 72   AS dist_m, d.pr
4120: 6f 76 20 41 53 20 70 72 6f 76 69 6e 63 65 2c 0d  ov AS province,.
4130: 0a 20 20 20 20 20 20 20 64 2e 6e 6f 6d 65 20 41  .       d.nome A
4140: 53 20 6d 75 6e 69 63 69 70 61 6c 69 74 79 2c 20  S municipality, 
4150: 63 2e 74 6f 70 6f 6e 69 6d 6f 20 41 53 20 73 74  c.toponimo AS st
4160: 72 65 65 74 5f 6e 61 6d 65 2c 20 62 2e 63 69 76  reet_name, b.civ
4170: 69 63 6f 20 41 53 20 68 6f 75 73 65 5f 6e 75 6d  ico AS house_num
4180: 2c 0d 0a 20 20 20 20 20 20 20 62 2e 67 65 6f 6d  ,..       b.geom
4190: 20 41 53 20 67 65 6f 6d 0d 0a 46 52 4f 4d 20 6b   AS geom..FROM k
41a0: 6e 6e 20 41 53 20 61 0d 0a 4a 4f 49 4e 20 63 69  nn AS a..JOIN ci
41b0: 76 69 63 69 20 41 53 20 62 20 4f 4e 20 28 62 2e  vici AS b ON (b.
41c0: 72 6f 77 69 64 20 3d 20 61 2e 66 69 64 29 0d 0a  rowid = a.fid)..
41d0: 4a 4f 49 4e 20 74 6f 70 6f 6e 69 6d 69 20 41 53  JOIN toponimi AS
41e0: 20 63 20 4f 4e 20 28 63 2e 66 69 64 20 3d 20 62   c ON (c.fid = b
41f0: 2e 69 64 5f 74 6f 70 6f 6e 69 6d 6f 29 0d 0a 4a  .id_toponimo)..J
4200: 4f 49 4e 20 63 6f 6d 75 6e 69 20 41 53 20 64 20  OIN comuni AS d 
4210: 4f 4e 20 28 64 2e 66 69 64 20 3d 20 63 2e 69 64  ON (d.fid = c.id
4220: 5f 63 6f 6d 75 6e 65 29 0d 0a 57 48 45 52 45 20  _comune)..WHERE 
4230: 61 2e 66 5f 74 61 62 6c 65 5f 6e 61 6d 65 20 3d  a.f_table_name =
4240: 20 27 63 69 76 69 63 69 27 20 41 4e 44 20 61 2e   'civici' AND a.
4250: 72 65 66 5f 67 65 6f 6d 65 74 72 79 20 3d 20 4d  ref_geometry = M
4260: 61 6b 65 50 6f 69 6e 74 28 31 37 33 33 30 30 33  akePoint(1733003
4270: 2c 20 34 38 31 36 33 33 32 2c 20 33 30 30 33 29  , 4816332, 3003)
4280: 20 41 4e 44 20 61 2e 6d 61 78 5f 69 74 65 6d 73   AND a.max_items
4290: 20 3d 20 31 30 3b 0d 0a 0d 0a 53 45 4c 45 43 54   = 10;....SELECT
42a0: 20 61 2e 70 6f 73 20 41 53 20 72 61 6e 6b 2c 20   a.pos AS rank, 
42b0: 61 2e 66 69 64 20 41 53 20 66 69 64 2c 20 61 2e  a.fid AS fid, a.
42c0: 64 69 73 74 61 6e 63 65 20 41 53 20 64 69 73 74  distance AS dist
42d0: 5f 6d 2c 20 62 2e 70 72 6f 76 20 41 53 20 70 72  _m, b.prov AS pr
42e0: 6f 76 69 6e 63 65 2c 0d 0a 20 20 20 20 20 20 20  ovince,..       
42f0: 62 2e 63 6f 6d 75 6e 65 20 41 53 20 6d 75 6e 69  b.comune AS muni
4300: 63 69 70 61 6c 69 74 79 2c 20 62 2e 74 6f 70 6f  cipality, b.topo
4310: 6e 69 6d 6f 20 41 53 20 73 74 72 65 65 74 5f 6e  nimo AS street_n
4320: 61 6d 65 2c 20 62 2e 63 69 76 69 63 6f 20 41 53  ame, b.civico AS
4330: 20 68 6f 75 73 65 5f 6e 75 6d 2c 20 0d 0a 20 20   house_num, ..  
4340: 20 20 20 20 20 62 2e 67 65 6f 6d 20 41 53 20 67       b.geom AS g
4350: 65 6f 6d 0d 0a 46 52 4f 4d 20 6b 6e 6e 20 41 53  eom..FROM knn AS
4360: 20 61 0d 0a 4a 4f 49 4e 20 76 77 5f 63 69 76 69   a..JOIN vw_civi
4370: 63 69 20 41 53 20 62 20 4f 4e 20 28 62 2e 72 6f  ci AS b ON (b.ro
4380: 77 69 64 20 3d 20 61 2e 66 69 64 29 0d 0a 57 48  wid = a.fid)..WH
4390: 45 52 45 20 61 2e 66 5f 74 61 62 6c 65 5f 6e 61  ERE a.f_table_na
43a0: 6d 65 20 3d 20 27 76 77 5f 63 69 76 69 63 69 27  me = 'vw_civici'
43b0: 20 41 4e 44 20 61 2e 72 65 66 5f 67 65 6f 6d 65   AND a.ref_geome
43c0: 74 72 79 20 3d 20 4d 61 6b 65 50 6f 69 6e 74 28  try = MakePoint(
43d0: 31 37 33 33 30 30 33 2c 20 34 38 31 36 33 33 32  1733003, 4816332
43e0: 2c 20 33 30 30 33 29 20 41 4e 44 20 61 2e 6d 61  , 3003) AND a.ma
43f0: 78 5f 69 74 65 6d 73 20 3d 20 31 30 3b 0d 0a 3c  x_items = 10;..<
4400: 2f 76 65 72 62 61 74 69 6d 3e 0d 0a 54 68 65 20  /verbatim>..The 
4410: 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 70 20 63 6f  following map co
4420: 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 74 68 65  rresponds to the
4430: 20 61 62 6f 76 65 20 71 75 65 72 69 65 73 3a 20   above queries: 
4440: 74 68 65 20 6f 72 69 67 69 6e 20 70 6f 69 6e 74  the origin point
4450: 20 69 73 20 70 6f 73 69 74 69 6f 6e 65 64 20 6f   is positioned o
4460: 6e 20 61 20 72 6f 61 64 20 6a 75 6e 63 74 69 6f  n a road junctio
4470: 6e 20 69 6e 20 74 68 65 20 63 65 6e 74 72 61 6c  n in the central
4480: 20 61 72 65 61 20 6f 66 20 61 20 64 65 6e 73 65   area of a dense
4490: 6c 79 20 70 6f 70 75 6c 61 74 65 64 20 74 6f 77  ly populated tow
44a0: 6e 20 28 41 72 65 7a 7a 6f 29 2e 3c 62 72 3e 0d  n (Arezzo).<br>.
44b0: 0a 4e 6f 74 20 73 75 72 70 72 69 73 69 6e 67 6c  .Not surprisingl
44c0: 79 20 74 68 65 20 74 65 6e 20 6e 65 61 72 65 73  y the ten neares
44d0: 74 20 68 6f 75 73 65 20 6e 75 6d 62 65 72 73 20  t house numbers 
44e0: 68 61 76 65 20 62 65 65 6e 20 6c 6f 63 61 74 65  have been locate
44f0: 64 20 77 69 74 68 69 6e 20 61 20 72 61 64 69 75  d within a radiu
4500: 73 20 6f 66 20 61 62 6f 75 74 20 31 30 6d 2e 3c  s of about 10m.<
4510: 62 72 3e 3c 62 72 3e 0d 0a 3c 69 6d 67 20 73 72  br><br>..<img sr
4520: 63 3d 22 68 74 74 70 73 3a 2f 2f 77 77 77 2e 67  c="https://www.g
4530: 61 69 61 2d 67 69 73 2e 69 74 2f 67 61 69 61 2d  aia-gis.it/gaia-
4540: 73 69 6e 73 2f 6b 6e 6e 2f 68 6f 75 73 65 6e 75  sins/knn/housenu
4550: 6d 31 2e 6a 70 67 22 20 61 6c 74 3d 22 48 6f 75  m1.jpg" alt="Hou
4560: 73 65 20 4e 75 6d 62 65 72 73 20 23 31 22 3e 0d  se Numbers #1">.
4570: 0a 3c 62 72 3e 3c 62 72 3e 0d 0a 42 6f 74 68 20  .<br><br>..Both 
4580: 71 75 65 72 69 65 73 20 77 69 6c 6c 20 72 65 74  queries will ret
4590: 75 72 6e 20 69 64 65 6e 74 69 63 61 6c 20 72 65  urn identical re
45a0: 73 75 6c 74 73 2e 3c 62 72 3e 0d 0a 41 20 63 6c  sults.<br>..A cl
45b0: 6f 73 65 72 20 6c 6f 6f 6b 20 77 69 6c 6c 20 73  oser look will s
45c0: 68 6f 77 20 79 6f 75 20 74 68 61 74 20 74 68 65  how you that the
45d0: 20 73 65 63 6f 6e 64 20 71 75 65 72 79 20 69 73   second query is
45e0: 20 62 61 73 65 64 20 6f 6e 20 61 20 3c 62 3e 53   based on a <b>S
45f0: 70 61 74 69 61 6c 20 56 69 65 77 3c 2f 62 3e 3c  patial View</b><
4600: 62 72 3e 3c 62 72 3e 0d 0a 3c 62 3e 53 75 6d 6d  br><br>..<b>Summ
4610: 61 72 79 3c 2f 62 3e 3a 20 61 20 56 69 72 74 75  ary</b>: a Virtu
4620: 61 6c 4b 4e 4e 20 71 75 65 72 79 20 63 61 6e 20  alKNN query can 
4630: 69 6e 64 69 66 66 65 72 65 6e 74 6c 79 20 74 61  indifferently ta
4640: 72 67 65 74 20 65 69 74 68 65 72 20 61 20 47 65  rget either a Ge
4650: 6f 54 61 62 6c 65 20 6f 72 20 61 20 70 72 6f 70  oTable or a prop
4660: 65 72 6c 79 20 72 65 67 69 73 74 65 72 65 64 20  erly registered 
4670: 53 70 61 74 69 61 6c 20 56 69 65 77 2e 3c 62 72  Spatial View.<br
4680: 3e 3c 62 72 3e 0d 0a 3c 76 65 72 62 61 74 69 6d  ><br>..<verbatim
4690: 3e 0d 0a 53 45 4c 45 43 54 20 61 2e 70 6f 73 20  >..SELECT a.pos 
46a0: 41 53 20 72 61 6e 6b 2c 20 61 2e 66 69 64 20 41  AS rank, a.fid A
46b0: 53 20 66 69 64 2c 20 61 2e 64 69 73 74 61 6e 63  S fid, a.distanc
46c0: 65 20 41 53 20 64 69 73 74 5f 6d 2c 20 62 2e 70  e AS dist_m, b.p
46d0: 72 6f 76 20 41 53 20 70 72 6f 76 69 6e 63 65 2c  rov AS province,
46e0: 0d 0a 20 20 20 20 20 20 20 62 2e 63 6f 6d 75 6e  ..       b.comun
46f0: 65 20 41 53 20 6d 75 6e 69 63 69 70 61 6c 69 74  e AS municipalit
4700: 79 2c 20 62 2e 74 6f 70 6f 6e 69 6d 6f 20 41 53  y, b.toponimo AS
4710: 20 73 74 72 65 65 74 5f 6e 61 6d 65 2c 20 62 2e   street_name, b.
4720: 63 69 76 69 63 6f 20 41 53 20 68 6f 75 73 65 5f  civico AS house_
4730: 6e 75 6d 2c 20 0d 0a 20 20 20 20 20 20 20 62 2e  num, ..       b.
4740: 67 65 6f 6d 20 41 53 20 67 65 6f 6d 0d 0a 46 52  geom AS geom..FR
4750: 4f 4d 20 6b 6e 6e 20 41 53 20 61 0d 0a 4a 4f 49  OM knn AS a..JOI
4760: 4e 20 76 77 5f 63 69 76 69 63 69 20 41 53 20 62  N vw_civici AS b
4770: 20 4f 4e 20 28 62 2e 72 6f 77 69 64 20 3d 20 61   ON (b.rowid = a
4780: 2e 66 69 64 29 20 0d 0a 57 48 45 52 45 20 61 2e  .fid) ..WHERE a.
4790: 66 5f 74 61 62 6c 65 5f 6e 61 6d 65 20 3d 20 27  f_table_name = '
47a0: 63 69 76 69 63 69 27 20 41 4e 44 20 61 2e 72 65  civici' AND a.re
47b0: 66 5f 67 65 6f 6d 65 74 72 79 20 3d 20 4d 61 6b  f_geometry = Mak
47c0: 65 50 6f 69 6e 74 28 31 35 39 35 36 32 35 2c 20  ePoint(1595625, 
47d0: 34 37 36 37 34 32 30 2c 20 33 30 30 33 29 20 41  4767420, 3003) A
47e0: 4e 44 20 6d 61 78 5f 69 74 65 6d 73 20 3d 20 32  ND max_items = 2
47f0: 30 3b 0d 0a 3c 2f 76 65 72 62 61 74 69 6d 3e 0d  0;..</verbatim>.
4800: 0a 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d  .The following m
4810: 61 70 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74  ap corresponds t
4820: 6f 20 74 68 65 20 61 62 6f 76 65 20 71 75 65 72  o the above quer
4830: 79 2e 20 4e 6f 77 20 74 68 65 20 6f 72 69 67 69  y. Now the origi
4840: 6e 20 70 6f 69 6e 74 20 69 73 20 70 6f 73 69 74  n point is posit
4850: 69 6f 6e 65 64 20 69 6e 20 74 68 65 20 54 79 72  ioned in the Tyr
4860: 72 68 65 6e 69 61 6e 20 53 65 61 2c 20 61 62 6f  rhenian Sea, abo
4870: 75 74 20 3c 62 3e 32 36 20 6b 6d 3c 2f 62 3e 20  ut <b>26 km</b> 
4880: 66 72 6f 6d 20 74 68 65 20 54 75 73 63 61 6e 79  from the Tuscany
4890: 20 6d 61 69 6e 6c 61 6e 64 20 61 6e 64 20 74 68   mainland and th
48a0: 65 20 49 73 6c 61 6e 64 73 20 6f 66 20 45 6c 62  e Islands of Elb
48b0: 61 20 61 6e 64 20 43 61 70 72 61 69 61 2e 3c 62  a and Capraia.<b
48c0: 72 3e 0d 0a 48 65 72 65 20 74 6f 6f 20 56 69 72  r>..Here too Vir
48d0: 74 75 61 6c 4b 4e 4e 20 69 73 20 63 61 70 61 62  tualKNN is capab
48e0: 6c 65 20 6f 66 20 69 64 65 6e 74 69 66 79 69 6e  le of identifyin
48f0: 67 20 74 68 65 20 6e 65 61 72 65 73 74 20 68 6f  g the nearest ho
4900: 75 73 65 20 6e 75 6d 62 65 72 73 20 69 6e 20 6a  use numbers in j
4910: 75 73 74 20 61 20 66 72 61 63 74 69 6f 6e 20 6f  ust a fraction o
4920: 66 20 61 20 73 65 63 6f 6e 64 2e 3c 62 72 3e 3c  f a second.<br><
4930: 62 72 3e 0d 0a 3c 69 6d 67 20 73 72 63 3d 22 68  br>..<img src="h
4940: 74 74 70 73 3a 2f 2f 77 77 77 2e 67 61 69 61 2d  ttps://www.gaia-
4950: 67 69 73 2e 69 74 2f 67 61 69 61 2d 73 69 6e 73  gis.it/gaia-sins
4960: 2f 6b 6e 6e 2f 68 6f 75 73 65 6e 75 6d 32 2e 70  /knn/housenum2.p
4970: 6e 67 22 20 61 6c 74 3d 22 48 6f 75 73 65 20 4e  ng" alt="House N
4980: 75 6d 62 65 72 73 20 23 32 22 3e 3c 62 72 3e 3c  umbers #2"><br><
4990: 62 72 3e 0d 0a 3c 62 3e 53 75 6d 6d 61 72 79 3c  br>..<b>Summary<
49a0: 2f 62 3e 3a 20 20 56 69 72 74 75 61 6c 4b 4e 4e  /b>:  VirtualKNN
49b0: 20 69 73 20 68 69 67 68 6c 79 20 65 66 66 69 63   is highly effic
49c0: 69 65 6e 74 20 61 6e 64 20 76 65 72 79 20 66 61  ient and very fa
49d0: 73 74 2e 20 45 76 65 6e 20 77 68 65 6e 20 71 75  st. Even when qu
49e0: 65 72 79 69 6e 67 20 73 75 63 68 20 61 20 68 75  erying such a hu
49f0: 67 65 20 64 61 74 61 73 65 74 2c 20 77 68 69 6c  ge dataset, whil
4a00: 65 20 65 78 70 6c 6f 72 69 6e 67 20 74 68 65 20  e exploring the 
4a10: 6d 6f 73 74 20 63 6f 6e 74 72 61 73 74 69 6e 67  most contrasting
4a20: 20 28 76 65 72 79 20 68 69 67 68 20 2f 20 6c 6f   (very high / lo
4a30: 77 20 64 65 6e 73 69 74 79 29 20 64 61 74 61 20  w density) data 
4a40: 64 69 73 74 72 69 62 75 74 69 6f 6e 73 20 2e 3c  distributions .<
4a50: 62 72 3e 3c 62 72 3e 0d 0a 3c 74 61 62 6c 65 20  br><br>..<table 
4a60: 77 69 64 74 68 3d 22 31 30 30 25 22 3e 3c 74 72  width="100%"><tr
4a70: 3e 0d 0a 3c 74 64 20 77 69 64 74 68 3d 22 33 33  >..<td width="33
4a80: 25 22 20 61 6c 69 67 6e 3d 22 6c 65 66 74 22 3e  %" align="left">
4a90: 3c 2f 74 64 3e 0d 0a 3c 74 64 20 61 6c 69 67 6e  </td>..<td align
4aa0: 3d 22 63 65 6e 74 65 72 22 3e 3c 61 20 68 72 65  ="center"><a hre
4ab0: 66 3d 22 68 74 74 70 73 3a 2f 2f 77 77 77 2e 67  f="https://www.g
4ac0: 61 69 61 2d 67 69 73 2e 69 74 2f 66 6f 73 73 69  aia-gis.it/fossi
4ad0: 6c 2f 6c 69 62 73 70 61 74 69 61 6c 69 74 65 2f  l/libspatialite/
4ae0: 77 69 6b 69 3f 6e 61 6d 65 3d 6d 69 73 63 2d 64  wiki?name=misc-d
4af0: 6f 63 73 22 3e 62 61 63 6b 20 74 6f 20 69 6e 64  ocs">back to ind
4b00: 65 78 3c 2f 61 3e 3c 2f 74 64 3e 0d 0a 3c 74 64  ex</a></td>..<td
4b10: 20 77 69 64 74 68 3d 22 33 33 25 22 20 61 6c 69   width="33%" ali
4b20: 67 6e 3d 22 72 69 67 68 74 22 3e 3c 2f 74 64 3e  gn="right"></td>
4b30: 0d 0a 3c 2f 74 72 3e 3c 2f 74 61 62 6c 65 3e 0a  ..</tr></table>.
4b40: 5a 20 36 39 36 65 31 30 32 30 63 35 33 32 34 63  Z 696e1020c5324c
4b50: 65 33 32 31 37 61 65 37 31 62 33 63 65 62 32 64  e3217ae71b3ceb2d
4b60: 39 33 0a                                         93.