<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)">
<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;}
 /* 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;}
span.EmailStyle17
        {mso-style-type:personal-compose;
        font-family:"Calibri","sans-serif";
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;}
@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>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>

</body>

</html>