Thanks for the explanation both Paul and Regina :) thanks :)<br><br><div><span class="gmail_quote">On 11/5/07, <b class="gmail_sendername">Paul Ramsey</b> &lt;<a href="mailto:pramsey@refractions.net">pramsey@refractions.net
</a>&gt; wrote:</span><blockquote class="gmail_quote" style="margin-top: 0; margin-right: 0; margin-bottom: 0; margin-left: 0; margin-left: 0.80ex; border-left-color: #cccccc; border-left-width: 1px; border-left-style: solid; padding-left: 1ex">
The &quot;maximum minimum distance&quot; has nothing to do with PostGIS, it&#39;s<br>just an idea: is there a distance you expect all your minimum<br>distances to be smaller than?&nbsp;&nbsp;If so, you can winnow out pairings of<br>
cities that will *not* yield a minimum distance.&nbsp;&nbsp;This is what the<br>ST_Expand does, it turns a point into a box, with a distance of ?mmd?<br>from each side to the center.&nbsp;&nbsp;And the &amp;&amp; tests containment in that<br>
box, using the spatial index operator.<br><br>P<br><br>On 4-Nov-07, at 4:21 PM, Matthew Pulis wrote:<br><br>&gt; Can you please explain further why u used the EXPAND? Didn&#39;t much<br>&gt; get what is its use? And is mmd a thing which has to do with PostGis ?
<br>&gt;<br>&gt; On 11/4/07, Paul Ramsey &lt;<a href="mailto:pramsey@refractions.net">pramsey@refractions.net</a>&gt; wrote:<br>&gt; Well, you have to build the cartesian product of every city<br>&gt; combination and then measure every distance in that virtual table, so
<br>&gt; it&#39;s not going to scale well at all as the input table gets bigger.<br>&gt;<br>&gt; However, if you know the &quot;maximum minimum distance&quot; (?mmd?) you can<br>&gt; add a spatial constraint that should at least keep the calculations
<br>&gt; in the O(n*log(n)) range... (you&#39;ll need a spatial index on the table<br>&gt; for best effect as the table gets larger)<br>&gt;<br>&gt; SELECT DISTINCT ON (c1)<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c1.city_name AS &quot;c1&quot;,<br>
&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c2.city_name AS &quot;c2&quot;,<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;distance(c1.the_geom, c2.the_geom),<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;makeline(c1.the_geom, c2.the_geom)<br>&gt; FROM<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;city c1<br>&gt; JOIN<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;city c2<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ON (
<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c1.city_name &lt;&gt; c2.city_name AND<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c1.the_geom &amp;&amp; ST_Expand(c2.the_geom, ?mmd?)<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)<br>&gt; ORDER BY c1, distance ASC<br>&gt; ;<br>&gt;<br>&gt; Paul<br>&gt;<br>&gt; PS - Nice query, BTW.
<br>&gt;<br>&gt; On 4-Nov-07, at 9:15 AM, Yancho wrote:<br>&gt;<br>&gt; &gt;<br>&gt; &gt; Just wanted to say that I managed to write this Query :<br>&gt; &gt;<br>&gt; &gt; SELECT DISTINCT ON (c1)<br>&gt; &gt;&nbsp;&nbsp;&nbsp;&nbsp; c1.city_name
 AS &quot;c1&quot;,<br>&gt; &gt;&nbsp;&nbsp;&nbsp;&nbsp; c2.city_name AS &quot;c2&quot;,<br>&gt; &gt;&nbsp;&nbsp;&nbsp;&nbsp; distance( c1.the_geom, c2.the_geom),<br>&gt; &gt;&nbsp;&nbsp;&nbsp;&nbsp; makeline(c1.the_geom, c2.the_geom)<br>&gt; &gt; FROM<br>&gt; &gt;&nbsp;&nbsp;&nbsp;&nbsp; city c1<br>
&gt; &gt; JOIN<br>&gt; &gt;&nbsp;&nbsp;&nbsp;&nbsp; city c2<br>&gt; &gt;&nbsp;&nbsp;&nbsp;&nbsp; ON (<br>&gt; &gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; c1.city_name &lt;&gt; c2.city_name<br>&gt; &gt;&nbsp;&nbsp;&nbsp;&nbsp; )<br>&gt; &gt; ORDER BY c1, distance ASC<br>&gt; &gt; ;<br>&gt; &gt;<br>&gt; &gt; It works perfectly, however how much do you think it can scale ? On
<br>&gt; &gt; 16 rows<br>&gt; &gt; it didnt take long, however or 28,000 rows? Will it use the O(n^2)<br>&gt; &gt; scalability?<br>&gt; &gt;<br>&gt; &gt; Thanks<br>&gt; &gt;<br>&gt; &gt;<br>&gt; &gt; Yancho wrote:<br>&gt; &gt;&gt;
<br>&gt; &gt;&gt; Hi,<br>&gt; &gt;&gt;<br>&gt; &gt;&gt; I am trying to make a query so it parses through all the 16 cities<br>&gt; &gt;&gt; i have<br>&gt; &gt;&gt; in<br>&gt; &gt;&gt; a table called city, and for each city, picks the nearest city,
<br>&gt; &gt;&gt; and gives<br>&gt; &gt;&gt; me<br>&gt; &gt;&gt; the distance between both cities.<br>&gt; &gt;&gt;<br>&gt; &gt;&gt; This is the query I made :<br>&gt; &gt;&gt;<br>&gt; &gt;&gt; select<br>&gt; &gt;&gt; c.city_name
, astext(c.the_geom), distance(c.the_geom,<br>&gt; d.the_geom) AS<br>&gt; &gt;&gt; Distance, d.city_name, astext(d.the_geom)<br>&gt; &gt;&gt; from city c, city d<br>&gt; &gt;&gt; where<br>&gt; &gt;&gt; c.city_name = (<br>
&gt; &gt;&gt; select c.city_name order by c.city_name ASC<br>&gt; &gt;&gt; )<br>&gt; &gt;&gt; and<br>&gt; &gt;&gt; d.city_name = (<br>&gt; &gt;&gt; select d.city_name order by d.city_name DESC<br>&gt; &gt;&gt; )<br>&gt; &gt;&gt; group by 
c.city_name<br>&gt; &gt;&gt; order by Distance DESC<br>&gt; &gt;&gt; LIMIT 1;<br>&gt; &gt;&gt;<br>&gt; &gt;&gt; But I am getting this error : ERROR: column &quot;c.the_geom&quot; must<br>&gt; &gt;&gt; appear in<br>&gt; &gt;&gt; the
<br>&gt; &gt;&gt; GROUP BY clause or be used in an aggregate function<br>&gt; &gt;&gt;<br>&gt; &gt;&gt; I am seeing no reason why I should add c.the_geom, anyone can<br>&gt; &gt;&gt; enlighten me<br>&gt; &gt;&gt; more on why I should group by the_geom and after all if it does
<br>&gt; make<br>&gt; &gt;&gt; sense?<br>&gt; &gt;&gt;<br>&gt; &gt;&gt; Thanks<br>&gt; &gt;&gt;<br>&gt; &gt;&gt; --<br>&gt; &gt;&gt; Matthew Pulis<br>&gt; &gt;&gt; <a href="http://www.solutions-lab.net">www.solutions-lab.net
</a> // <a href="http://www.mepa-clan.info">www.mepa-clan.info</a><br>&gt; &gt;&gt;<br>&gt; &gt;&gt; _______________________________________________<br>&gt; &gt;&gt; postgis-users mailing list<br>&gt; &gt;&gt; <a href="mailto:postgis-users@postgis.refractions.net">
postgis-users@postgis.refractions.net</a><br>&gt; &gt;&gt; <a href="http://postgis.refractions.net/mailman/listinfo/postgis-users">http://postgis.refractions.net/mailman/listinfo/postgis-users</a><br>&gt; &gt;&gt;<br>&gt; &gt;&gt;
<br>&gt; &gt;<br>&gt; &gt; --<br>&gt; &gt; View this message in context: <a href="http://www.nabble.com/Shortest-">http://www.nabble.com/Shortest-</a><br>&gt; &gt; Distance-from-Every-Point-tf4743229.html#a13575499<br>&gt; &gt; Sent from the PostGIS - User mailing list archive at 
<a href="http://Nabble.com">Nabble.com</a>.<br>&gt; &gt;<br>&gt; &gt; _______________________________________________<br>&gt; &gt; postgis-users mailing list<br>&gt; &gt; <a href="mailto:postgis-users@postgis.refractions.net">
postgis-users@postgis.refractions.net</a><br>&gt; &gt; <a href="http://postgis.refractions.net/mailman/listinfo/postgis-users">http://postgis.refractions.net/mailman/listinfo/postgis-users</a><br>&gt;<br>&gt; _______________________________________________
<br>&gt; postgis-users mailing list<br>&gt; <a href="mailto:postgis-users@postgis.refractions.net">postgis-users@postgis.refractions.net</a><br>&gt; <a href="http://postgis.refractions.net/mailman/listinfo/postgis-users">
http://postgis.refractions.net/mailman/listinfo/postgis-users</a><br>&gt;<br>&gt;<br>&gt;<br>&gt; --<br>&gt; Matthew Pulis<br>&gt; <a href="http://www.solutions-lab.net">www.solutions-lab.net</a> // <a href="http://www.mepa-clan.info">
www.mepa-clan.info</a><br>&gt; _______________________________________________<br>&gt; postgis-users mailing list<br>&gt; <a href="mailto:postgis-users@postgis.refractions.net">postgis-users@postgis.refractions.net</a><br>
&gt; <a href="http://postgis.refractions.net/mailman/listinfo/postgis-users">http://postgis.refractions.net/mailman/listinfo/postgis-users</a><br><br>_______________________________________________<br>postgis-users mailing list
<br><a href="mailto:postgis-users@postgis.refractions.net">postgis-users@postgis.refractions.net</a><br><a href="http://postgis.refractions.net/mailman/listinfo/postgis-users">http://postgis.refractions.net/mailman/listinfo/postgis-users
</a><br></blockquote></div><br><br clear="all"><br>-- <br>Matthew Pulis<br><a href="http://www.solutions-lab.net">www.solutions-lab.net</a> // <a href="http://www.mepa-clan.info">www.mepa-clan.info</a>