<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">

<head>
<meta http-equiv=Content-Type content="text/html; charset=us-ascii">
<meta name=Generator content="Microsoft Word 12 (filtered medium)">
<!--[if !mso]>
<style>
v\:* {behavior:url(#default#VML);}
o\:* {behavior:url(#default#VML);}
w\:* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}
</style>
<![endif]-->
<style>
<!--
 /* Font Definitions */
 @font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
 /* Style Definitions */
 p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri","sans-serif";}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
pre
        {mso-style-priority:99;
        mso-style-link:"HTML Preformatted Char";
        margin:0in;
        margin-bottom:.0001pt;
        font-size:10.0pt;
        font-family:"Courier New";}
span.EmailStyle17
        {mso-style-type:personal-compose;
        font-family:"Calibri","sans-serif";
        color:windowtext;}
span.HTMLPreformattedChar
        {mso-style-name:"HTML Preformatted Char";
        mso-style-priority:99;
        mso-style-link:"HTML Preformatted";
        font-family:"Courier New";}
.MsoChpDefault
        {mso-style-type:export-only;}
@page Section1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.Section1
        {page:Section1;}
-->
</style>
<!--[if gte mso 9]><xml>
 <o:shapedefaults v:ext="edit" spidmax="2050" />
</xml><![endif]--><!--[if gte mso 9]><xml>
 <o:shapelayout v:ext="edit">
  <o:idmap v:ext="edit" data="1" />
 </o:shapelayout></xml><![endif]-->
</head>

<body lang=EN-US link=blue vlink=purple>

<div class=Section1>

<p class=MsoNormal>Hi All,<o:p></o:p></p>

<p class=MsoNormal>                Pretty
excited by the new operators <-> and <#>, but a bit confused as to
how to use them in a query.  The two examples from P. Ramsey back in late
2011 ( <a
href="http://blog.opengeo.org/2011/09/28/indexed-nearest-neighbour-search-in-postgis/">http://blog.opengeo.org/2011/09/28/indexed-nearest-neighbour-search-in-postgis/</a>
) included doing a KNN on a single point to a cloud of points, i.e. <o:p></o:p></p>

<p class=MsoNormal><o:p> </o:p></p>

<p class=MsoNormal><span style='font-size:10.0pt;font-family:"Courier New"'>SELECT
name, gid<o:p></o:p></span></p>

<p class=MsoNormal><span style='font-size:10.0pt;font-family:"Courier New"'>FROM
geonames<o:p></o:p></span></p>

<p class=MsoNormal><span style='font-size:10.0pt;font-family:"Courier New"'>ORDER
BY geom <-> st_setsrid(st_makepoint(-90,40),4326)<o:p></o:p></span></p>

<p class=MsoNormal><span style='font-size:10.0pt;font-family:"Courier New"'>LIMIT
10;<o:p></o:p></span></p>

<p class=MsoNormal><o:p> </o:p></p>

<p class=MsoNormal>or doing KNN on non-point different geometries, where the
first neighbor by <-> or <#> might not be truly the first i.e.<o:p></o:p></p>

<p class=MsoNormal><o:p> </o:p></p>

<pre>with index_query as (<o:p></o:p></pre><pre>  select<o:p></o:p></pre><pre>    st_distance(geom, 'SRID=3005;POINT(1011102 450541)') as distance,<o:p></o:p></pre><pre>    parcel_id, address<o:p></o:p></pre><pre>  from parcels<o:p></o:p></pre><pre>  order by geom <#> 'SRID=3005;POINT(1011102 450541)' limit 100<o:p></o:p></pre><pre>)<o:p></o:p></pre><pre>select * from index_query order by distance limit 10;<o:p></o:p></pre>

<p class=MsoNormal><o:p> </o:p></p>

<p class=MsoNormal>So, how would one grab the first nearest neighbor for all
points in a dataset?  This is how I used to do it:<o:p></o:p></p>

<p class=MsoNormal><o:p> </o:p></p>

<p class=MsoNormal><span style='font-family:"Courier New"'>CREATE TABLE
n2180_560_height AS <o:p></o:p></span></p>

<p class=MsoNormal><span style='font-family:"Courier New"'>SELECT x, y, height
FROM <o:p></o:p></span></p>

<p class=MsoNormal><span style='font-family:"Courier New"'>(SELECT DISTINCT
ON(veg.gid) veg.gid as gid, ground.gid as gid_ground, veg.x as x, veg.y as y,
ground.z as z, veg.z - ground.z as height, veg.the_geom as geometry, veg.class
as class<o:p></o:p></span></p>

<p class=MsoNormal><span style='font-family:"Courier New"'>FROM (SELECT * FROM
n2180_560 WHERE class = 5) As veg, (SELECT * FROM n2180_560 WHERE class = 2) As
ground<o:p></o:p></span></p>

<p class=MsoNormal><span style='font-family:"Courier New"'>WHERE veg.class = 5
AND veg.gid <> ground.gid AND ST_DWithin(veg.the_geom, ground.the_geom,
10)<o:p></o:p></span></p>

<p class=MsoNormal><span style='font-family:"Courier New"'>ORDER BY veg.gid,
ST_Distance(veg.the_geom,ground.the_geom)) AS vegpoints;<o:p></o:p></span></p>

<p class=MsoNormal><o:p> </o:p></p>

<p class=MsoNormal>ST_DWithin prevents a full cross join, but is a sloppy way
to do this, as it requires a priori knowledge of the end cases.  I’m
hoping there’s a subquery or some such magic that would allow me to use
the distance operator to a similar end… .<o:p></o:p></p>

<p class=MsoNormal><o:p> </o:p></p>

<p class=MsoNormal>Thanks,<o:p></o:p></p>

<p class=MsoNormal>Best,<o:p></o:p></p>

<p class=MsoNormal>Steve<o:p></o:p></p>

<p class=MsoNormal><o:p> </o:p></p>

<p class=MsoNormal><!--[if gte vml 1]><v:shapetype id="_x0000_t75" coordsize="21600,21600" 
 o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f" 
 stroked="f">
 <v:stroke joinstyle="miter" />
 <v:formulas>
  <v:f eqn="if lineDrawn pixelLineWidth 0" />
  <v:f eqn="sum @0 1 0" />
  <v:f eqn="sum 0 0 @1" />
  <v:f eqn="prod @2 1 2" />
  <v:f eqn="prod @3 21600 pixelWidth" />
  <v:f eqn="prod @3 21600 pixelHeight" />
  <v:f eqn="sum @0 0 1" />
  <v:f eqn="prod @6 1 2" />
  <v:f eqn="prod @7 21600 pixelWidth" />
  <v:f eqn="sum @8 21600 0" />
  <v:f eqn="prod @7 21600 pixelHeight" />
  <v:f eqn="sum @10 21600 0" />
 </v:formulas>
 <v:path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect" />
 <o:lock v:ext="edit" aspectratio="t" />
</v:shapetype><v:shape id="cmp-ms-90x122.png" o:spid="_x0000_s1026" type="#_x0000_t75" 
 alt="http://www.clemetparks.com/images/esig/cmp-ms-90x122.png" style='position:absolute;
 margin-left:1.5pt;margin-top:0;width:67.5pt;height:91.5pt;z-index:1;
 visibility:visible;mso-wrap-style:square;mso-wrap-distance-left:9pt;
 mso-wrap-distance-top:0;mso-wrap-distance-right:9pt;
 mso-wrap-distance-bottom:0;mso-position-horizontal:absolute;
 mso-position-horizontal-relative:text;mso-position-vertical:absolute;
 mso-position-vertical-relative:text'>
 <v:imagedata src="cid:image001.png@01CD3297.76FAA280" o:href="http://www.clemetparks.com/images/esig/cmp-ms-90x122.png" />
 <w:wrap type="square"/>
</v:shape><![endif]--><![if !vml]><img width=90 height=122
src="cid:image001.png@01CD3297.76FAA280" align=left hspace=12
alt="http://www.clemetparks.com/images/esig/cmp-ms-90x122.png" v:shapes="cmp-ms-90x122.png"><![endif]><span
style='font-size:14.0pt;font-family:"Arial","sans-serif";color:#006C56'>Stephen
Mather<br>
</span><span style='font-family:"Arial","sans-serif";color:#006C56'>Geographic
Information Systems (GIS) Manager<br>
</span><span style='font-size:9.0pt;font-family:"Arial","sans-serif";
color:#006C56'>(216) 635-3243<o:p></o:p></span></p>

<p class=MsoNormal><span style='font-size:9.0pt;font-family:"Arial","sans-serif";
color:#006C56'>svm@clevelandmetroparks.com<br>
</span><a href="http://www.clemetparks.com/"><span style='font-size:9.0pt;
font-family:"Arial","sans-serif"'>clevelandmetroparks.com</span></a><o:p></o:p></p>

<p class=MsoNormal><o:p> </o:p></p>

<p class=MsoNormal><o:p> </o:p></p>

<p class=MsoNormal><o:p> </o:p></p>

<p class=MsoNormal><o:p> </o:p></p>

</div>

</body>

</html>