<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:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:Tahoma;
        panose-1:2 11 6 4 3 5 4 4 2 4;}
 /* Style Definitions */
 p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        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;}
p
        {mso-style-priority:99;
        mso-margin-top-alt:auto;
        margin-right:0cm;
        mso-margin-bottom-alt:auto;
        margin-left:0cm;
        font-size:12.0pt;
        font-family:"Times New Roman","serif";}
span.EmailStyle17
        {mso-style-type:personal;
        font-family:"Calibri","sans-serif";
        color:windowtext;}
span.EmailStyle20
        {mso-style-type:personal-reply;
        font-family:"Calibri","sans-serif";
        color:#1F497D;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-size:10.0pt;}
@page Section1
        {size:612.0pt 792.0pt;
        margin:70.85pt 3.0cm 70.85pt 3.0cm;}
div.Section1
        {page:Section1;}
-->
</style>
<!--[if gte mso 9]><xml>
 <o:shapedefaults v:ext="edit" spidmax="1026" />
</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=PT link=blue vlink=purple>

<div class=Section1>

<p class=MsoNormal><span style='color:#1F497D'>Regina,<o:p></o:p></span></p>

<p class=MsoNormal><span style='color:#1F497D'><o:p> </o:p></span></p>

<p class=MsoNormal><span lang=EN-US style='color:#1F497D'>You’re simply an
angel! Thank you!<o:p></o:p></span></p>

<p class=MsoNormal><span lang=EN-US style='color:#1F497D'><o:p> </o:p></span></p>

<p class=MsoNormal><span lang=EN-US style='color:#1F497D'>Just one question:<o:p></o:p></span></p>

<p class=MsoNormal><span lang=EN-US style='color:#1F497D'>The expand(rc.agg_geometry,
0.5) uses *<b>degrees</b>*, right?<o:p></o:p></span></p>

<p class=MsoNormal><span lang=EN-US style='color:#1F497D'><o:p> </o:p></span></p>

<p class=MsoNormal><span lang=EN-US style='color:#1F497D'>All the best,<o:p></o:p></span></p>

<p class=MsoNormal><span lang=EN-US style='color:#1F497D'>Pedro.<o:p></o:p></span></p>

<p class=MsoNormal><span lang=EN-US style='color:#1F497D'><o:p> </o:p></span></p>

<div>

<div style='border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0cm 0cm 0cm'>

<p class=MsoNormal><b><span lang=EN-US style='font-size:10.0pt;font-family:
"Tahoma","sans-serif"'>From:</span></b><span lang=EN-US style='font-size:10.0pt;
font-family:"Tahoma","sans-serif"'>
postgis-users-bounces@postgis.refractions.net
[mailto:postgis-users-bounces@postgis.refractions.net] <b>On Behalf Of </b>Obe,
Regina<br>
<b>Sent:</b> segunda-feira, 2 de Abril de 2007 13:43<br>
<b>To:</b> PostGIS Users Discussion<br>
<b>Subject:</b> RE: [postgis-users] 10 closest units<o:p></o:p></span></p>

</div>

</div>

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

<p class=MsoNormal><span style='font-size:10.0pt;font-family:"Arial","sans-serif";
color:blue'>I'm not sure how slow this would be depending on how many line
segments you have by name, but it seems the most speedy to write. 
Basically I think you want to collect all your roads of N17 into a single
geometry so that you can then apply a single distance check call.</span><span
style='font-size:12.0pt;font-family:"Times New Roman","serif"'><o:p></o:p></span></p>

<p class=MsoNormal><span style='font-size:12.0pt;font-family:"Times New Roman","serif"'> <o:p></o:p></span></p>

<p class=MsoNormal><span style='font-size:10.0pt;font-family:"Arial","sans-serif";
color:blue'>So something like</span><span style='font-size:12.0pt;font-family:
"Times New Roman","serif"'><o:p></o:p></span></p>

<p class=MsoNormal><span style='font-size:12.0pt;font-family:"Times New Roman","serif"'> <o:p></o:p></span></p>

<p class=MsoNormal><span lang=EN-US style='color:blue'>select u.friendly_name,
distance(transform(u.curr_location, 32628), <o:p></o:p></span></p>

<p class=MsoNormal><span lang=EN-US style='color:blue'>transform(rc.agg_geometry,32628))
as thedistance <o:p></o:p></span></p>

<p class=MsoNormal><span lang=EN-US style='color:blue'>from (SELECT
collect(r.geometry) as agg_geometry FROM pt_mainland_roads r WHERE r.name =
'N17') rc, units as u <o:p></o:p></span></p>

<p class=MsoNormal><span lang=EN-US style='color:blue'>where
rc.agg_geometry && u.curr_location order by thedistance LIMIT 10;</span><o:p></o:p></p>

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

<p class=MsoNormal><span lang=EN-US style='color:blue'>If you have some units
that are not in the bounding box of a road that would be in the top 10, then
you may want to change your where clause to</span><span lang=EN-US><o:p></o:p></span></p>

<p class=MsoNormal> <span lang=EN-US><o:p></o:p></span></p>

<p class=MsoNormal><span lang=EN-US style='color:blue'>expand(rc.agg_geometry,
<somevalue>)&& u.curr_location </span><span lang=EN-US><o:p></o:p></span></p>

<p class=MsoNormal> <span lang=EN-US><o:p></o:p></span></p>

<p class=MsoNormal><span lang=EN-US style='color:blue'>Hope that helps,</span><span
lang=EN-US><o:p></o:p></span></p>

<p class=MsoNormal><span lang=EN-US style='color:blue'>Regina</span><span
lang=EN-US><o:p></o:p></span></p>

<p class=MsoNormal><span style='font-size:12.0pt;font-family:"Times New Roman","serif"'><o:p> </o:p></span></p>

<div class=MsoNormal align=center style='text-align:center'><span lang=EN-US
style='font-size:12.0pt;font-family:"Times New Roman","serif"'>

<hr size=2 width="100%" align=center>

</span></div>

<p class=MsoNormal style='margin-bottom:12.0pt'><b><span lang=EN-US
style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'>From:</span></b><span
lang=EN-US style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'>
postgis-users-bounces@postgis.refractions.net [mailto:postgis-users-bounces@postgis.refractions.net]
<b>On Behalf Of </b>Pedro Doria Meunier<br>
<b>Sent:</b> Sunday, April 01, 2007 9:50 PM<br>
<b>To:</b> 'PostGIS Users Discussion'<br>
<b>Subject:</b> [postgis-users] 10 closest units<br>
<b>Importance:</b> High</span><span lang=EN-US style='font-size:12.0pt;
font-family:"Times New Roman","serif"'><o:p></o:p></span></p>

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

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

<p class=MsoNormal><span lang=EN-US>@Regina:<o:p></o:p></span></p>

<p class=MsoNormal><span lang=EN-US>Remember you helped me with the 10 closest
units to a given point? This was it:<o:p></o:p></span></p>

<p class=MsoNormal><span lang=EN-US>(original layers srid==4326 – I want *<b>meters</b>*
so the transform at play)<o:p></o:p></span></p>

<p class=MsoNormal><span lang=EN-US>SELECT u.id, u.friendly_name,
u.curr_location, t.oid, distance(transform(u.curr_location,32628), transform(t.geometry,
32628)) AS thedistance, u.mobile FROM units AS u, (SELECT roads.oid,
roads.geometry FROM roads WHERE name=’N17’ LIMIT 1) AS t <o:p></o:p></span></p>

<p class=MsoNormal><span lang=EN-US>ORDER BY thedistance LIMIT 10;<o:p></o:p></span></p>

<p class=MsoNormal><span lang=EN-US><o:p> </o:p></span></p>

<p class=MsoNormal><span lang=EN-US>(this query returns the 1<sup>st</sup>
found line segment labelled ‘N17’ which is 68Kms away when the unit is actually
1.8 meters away from the closest line segment of the same label)<o:p></o:p></span></p>

<p class=MsoNormal><span lang=EN-US><o:p> </o:p></span></p>

<p class=MsoNormal><span lang=EN-US>@All<o:p></o:p></span></p>

<p class=MsoNormal><span lang=EN-US>This actually returns the FIRST occurrence
of ‘N17’ (the sample).<o:p></o:p></span></p>

<p class=MsoNormal><span lang=EN-US>This is *<b>NOT</b>* necessarily the
closest road to the unit’s current location.<o:p></o:p></span></p>

<p class=MsoNormal><span lang=EN-US><o:p> </o:p></span></p>

<p class=MsoNormal><span lang=EN-US>I’ve melted half-a-dozen neurons (mainly
due to tiredness at the time of this writing :] )trying to figure out how can I
implement the distance bit in the sub-query…<o:p></o:p></span></p>

<p class=MsoNormal><span lang=EN-US><o:p> </o:p></span></p>

<p class=MsoNormal><span lang=EN-US>Given example for ONE unit:<o:p></o:p></span></p>

<p class=MsoNormal><span lang=EN-US>select u.friendly_name,
distance(transform(u.curr_location, 32628), <o:p></o:p></span></p>

<p class=MsoNormal><span lang=EN-US>transform(geometry,32628)) as thedistance <o:p></o:p></span></p>

<p class=MsoNormal><span lang=EN-US>from pt_mainland_roads as r, units as u <o:p></o:p></span></p>

<p class=MsoNormal><span lang=EN-US>where name='N17' AND u.curr_location
&& r.geometry order by thedistance LIMIT 1;<o:p></o:p></span></p>

<p class=MsoNormal><span lang=EN-US><o:p> </o:p></span></p>

<p class=MsoNormal><span lang=EN-US>(this returns the correct result)<o:p></o:p></span></p>

<p class=MsoNormal><span lang=EN-US><o:p> </o:p></span></p>

<p class=MsoNormal><span lang=EN-US>So to summarize things:<o:p></o:p></span></p>

<p class=MsoNormal><span lang=EN-US>I need help with a query that returns the *<b>FIRST
TEN</b>* units to the *<b>closest given</b>* road. <o:p></o:p></span></p>

<p class=MsoNormal><span lang=EN-US>(Keep in mind that multiple line segments
with the same name exist – I need the closest)<o:p></o:p></span></p>

<p class=MsoNormal><span lang=EN-US><o:p> </o:p></span></p>

<p class=MsoNormal><span lang=EN-US>Will try again tomorrow with a clear head
but in the meantime:<o:p></o:p></span></p>

<p class=MsoNormal><span lang=EN-US>Any help would be most appreciated!<o:p></o:p></span></p>

<p class=MsoNormal><span lang=EN-US><o:p> </o:p></span></p>

<p class=MsoNormal><span lang=EN-US>Best regards,<o:p></o:p></span></p>

<p class=MsoNormal><span lang=EN-US>Pedro Doria Meunier.<o:p></o:p></span></p>

<div class=MsoNormal align=center style='text-align:center'><span
style='font-size:12.0pt;font-family:"Times New Roman","serif"'>

<hr size=1 width="100%" align=center>

</span></div>

<p class=MsoNormal><span style='font-size:12.0pt;font-family:"Times New Roman","serif"'><o:p> </o:p></span></p>

<p><b><br>
<strong>The substance of this message, including any attachments, may be</strong><br>
<strong>confidential, legally privileged and/or exempt from disclosure</strong><br>
<strong>pursuant to Massachusetts law. It is intended solely for the</strong><br>
<strong>addressee. If you received this in error, please contact the sender</strong><br>
<strong>and delete the material from any computer.</strong></b><o:p></o:p></p>

</div>

</body>

</html>