{"id":102,"date":"2012-03-31T12:41:05","date_gmt":"2012-03-31T19:41:05","guid":{"rendered":"http:\/\/blog.light42.com\/wordpress\/?p=102"},"modified":"2012-11-10T20:01:41","modified_gmt":"2012-11-11T04:01:41","slug":"knn-gist-index-in-postgresql","status":"publish","type":"post","link":"http:\/\/blog.light42.com\/wordpress\/?p=102","title":{"rendered":"KNN GIST Index in Postgresql"},"content":{"rendered":"<p>With version 9.1 of PostgreSQL and above, the <a title=\"KNN GIST\" href=\"http:\/\/wiki.postgresql.org\/wiki\/What%27s_new_in_PostgreSQL_9.1#K-Nearest-Neighbor_Indexing\" target=\"_blank\">KNN GIST Index<\/a> is available.<\/p>\n<p>There are two new operators defined:\u00a0 &lt;-&gt; and &lt;#&gt;\u00a0 <code><br \/>\n<\/code><\/p>\n<p>With version 2.0 of <a title=\"PostGIS\" href=\"http:\/\/www.postgis.org\" target=\"_blank\">PostGIS<\/a> and above, the KNN GIST Index functionality is exposed for PostGIS geometry types.<\/p>\n<p><code><strong>&lt;-&gt;<\/strong> - Returns the distance between two points. For point \/ point checks it uses floating point accuracy (as opposed to the double precision accuracy of the underlying point geometry). For other geometry types the distance between the floating point bounding box centroids is returned. Useful for doing distance ordering and nearest neighbor limits using KNN gist functionality.<\/code><\/p>\n<p><code><strong>&lt;#&gt;<\/strong> - Returns the distance between bounding box of 2 geometries. For point \/ point checks it's almost the same as distance (though may be different since the bounding box is at floating point accuracy and geometries are double precision). Useful for doing distance ordering and nearest neighbor limits using KNN gist functionality.<\/code><\/p>\n<p>Let&#8217;s look at how they operators affect search results in a real-world example \u2013 proximity of parcels from a point on the street. Note that one of the geometries in this example is a very thin polygon just inside of a larger one against the street.<br \/>\nThis first example shows bounding box\u00a0ordered distance <strong>&lt;#&gt;<\/strong><br \/>\n<script type=\"text\/javascript\">jQuery(document).ready(function($) { $(\"#gallery a\").lightBox({fixedNavigation:true});});<\/script><\/p>\n<div id='gallery'>\n<a href=\"wp-content\/uploads\/2012\/03\/knn_rboxF-1024x536.png\"><img decoding=\"async\" src=\"wp-content\/uploads\/2012\/03\/knn_rboxF-300x157.png\" alt=\"&lt;#&gt;\" align=\"left\" \/><\/a><\/p>\n<p><a href=\"wp-content\/uploads\/2012\/03\/knn_rbox_ansF-1024x542.png\"><img decoding=\"async\" src=\"wp-content\/uploads\/2012\/03\/knn_rbox_ansF-300x158.png\" alt=\"&lt;#&gt;\" align=\"middle\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>Next example shows centroids ordered distance <strong>&lt;-&gt;<\/strong><\/p>\n<p><a href=\"wp-content\/uploads\/2012\/03\/knn_cboxF-1024x541.png\"><img decoding=\"async\" src=\"wp-content\/uploads\/2012\/03\/knn_cboxF-300x158.png\" alt=\"&lt;-&gt;\" align=\"left\" \/><\/a><\/p>\n<p><a href=\"wp-content\/uploads\/2012\/03\/knn_cbox_ansF-1024x544.png\"><img decoding=\"async\" src=\"wp-content\/uploads\/2012\/03\/knn_cbox_ansF-300x159.png\" alt=\"&lt;-&gt;\" align=\"middle\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>Lastly ordered by good old <strong>ST_Distance()<\/strong><\/p>\n<p><a href=\"wp-content\/uploads\/2012\/03\/knn_distF-1024x541.png\"><img decoding=\"async\" src=\"wp-content\/uploads\/2012\/03\/knn_distF-300x158.png\" alt=\"ST_Distance()\" align=\"left\" \/><\/a>\n<\/div>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>With version 9.1 of PostgreSQL and above, the KNN GIST Index is available. There are two new operators defined:\u00a0 &lt;-&gt; and &lt;#&gt;\u00a0 With version 2.0 of PostGIS and above, the KNN GIST Index functionality is exposed for PostGIS geometry types. &lt;-&gt; &#8211; Returns the distance between two points. For point \/ point checks it uses [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"_links":{"self":[{"href":"http:\/\/blog.light42.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/102"}],"collection":[{"href":"http:\/\/blog.light42.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.light42.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.light42.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.light42.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=102"}],"version-history":[{"count":64,"href":"http:\/\/blog.light42.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/102\/revisions"}],"predecessor-version":[{"id":266,"href":"http:\/\/blog.light42.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/102\/revisions\/266"}],"wp:attachment":[{"href":"http:\/\/blog.light42.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.light42.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=102"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.light42.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}