<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:st1="urn:schemas-microsoft-com:office:smarttags" xmlns="http://www.w3.org/TR/REC-html40">

<head>
<meta http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<meta name=Generator content="Microsoft Word 11 (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]--><o:SmartTagType
 namespaceuri="urn:schemas-microsoft-com:office:smarttags" name="place"/>
<o:SmartTagType namespaceuri="urn:schemas-microsoft-com:office:smarttags"
 name="PlaceType"/>
<o:SmartTagType namespaceuri="urn:schemas-microsoft-com:office:smarttags"
 name="PlaceName"/>
<!--[if !mso]>
<style>
st1\:*{behavior:url(#default#ieooui) }
</style>
<![endif]-->
<style>
<!--
 /* Font Definitions */
 @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:0pt;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman";}
a:link, span.MsoHyperlink
        {color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {color:blue;
        text-decoration:underline;}
p
        {mso-margin-top-alt:auto;
        margin-right:0pt;
        mso-margin-bottom-alt:auto;
        margin-left:0pt;
        font-size:12.0pt;
        font-family:"Times New Roman";}
span.EmailStyle19
        {mso-style-type:personal-reply;
        font-family:Arial;
        color:navy;}
@page Section1
        {size:612.0pt 792.0pt;
        margin:72.0pt 90.0pt 72.0pt 90.0pt;}
div.Section1
        {page:Section1;}
-->
</style>

</head>

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

<div class=Section1>

<p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size:
10.0pt;font-family:Arial;color:navy'>Hi Rodrigo, <o:p></o:p></span></font></p>

<p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size:
10.0pt;font-family:Arial;color:navy'><o:p> </o:p></span></font></p>

<p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size:
10.0pt;font-family:Arial;color:navy'>That worked perfectly.  Now I’ve
tried to take it a step further and I’m stuck.  The ultimate reason I’m
trying to get the intersection is to select a segment of road using
line_substring given three road names.  For example, a user specifies Twp 480
from Rge 20 to Rge 30.  I want to extract that segment of road as a geometry.<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size:
10.0pt;font-family:Arial;color:navy'><o:p> </o:p></span></font></p>

<p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size:
10.0pt;font-family:Arial;color:navy'>Once you showed me how to select the
intersection, it seemed like it should be easy to add that to a line_locate_point
query and save another step, but I’m stuck.<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size:
10.0pt;font-family:Arial;color:navy'><o:p> </o:p></span></font></p>

<p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size:
10.0pt;font-family:Arial;color:navy'>This is the query I tried:<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size:
10.0pt;font-family:Arial;color:navy'><o:p> </o:p></span></font></p>

<p class=MsoNormal style='margin-left:36.0pt'><i><font size=2 color=navy
face=Arial><span style='font-size:10.0pt;font-family:Arial;color:navy;
font-style:italic'>SELECT line_locate_point(road_geom,IntersectionPoint) AS
start FROM (SELECT road1.geom FROM (SELECT * FROM roads WHERE name = 'Twp 480')
AS road1 JOIN (SELECT * FROM roads WHERE name = 'Rge 20') AS road2 ON
road1.geom && road2.geom AND Intersects(road1.geom,road2.geom)) AS
road_geom, (SELECT Centroid(Intersection(road1.geom,road2.geom)) FROM (SELECT *
FROM roads WHERE name = 'Twp 480') AS road1 JOIN (SELECT * FROM roads WHERE
name = 'Rge 20') AS road2 ON road1.geom && road2.geom AND Intersects(road1.geom,road2.geom))
AS IntersectionPoint;<o:p></o:p></span></font></i></p>

<p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size:
10.0pt;font-family:Arial;color:navy'><o:p> </o:p></span></font></p>

<p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size:
10.0pt;font-family:Arial;color:navy'>The error message I get is:<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size:
10.0pt;font-family:Arial;color:navy'><o:p> </o:p></span></font></p>

<p class=MsoNormal style='margin-left:36.0pt'><i><font size=2 color=navy
face=Arial><span style='font-size:10.0pt;font-family:Arial;color:navy;
font-style:italic'>ERROR: function line_locate_point(record, record) does not
exist<o:p></o:p></span></font></i></p>

<p class=MsoNormal style='margin-left:36.0pt'><i><font size=2 color=navy
face=Arial><span style='font-size:10.0pt;font-family:Arial;color:navy;
font-style:italic'>SQL state: 42883<o:p></o:p></span></font></i></p>

<p class=MsoNormal style='margin-left:36.0pt'><i><font size=2 color=navy
face=Arial><span style='font-size:10.0pt;font-family:Arial;color:navy;
font-style:italic'>Hint: No function matches the given name and argument types.
You may need to add explicit type casts.<o:p></o:p></span></font></i></p>

<p class=MsoNormal style='margin-left:36.0pt'><i><font size=2 color=navy
face=Arial><span style='font-size:10.0pt;font-family:Arial;color:navy;
font-style:italic'>Character: 8<o:p></o:p></span></font></i></p>

<p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size:
10.0pt;font-family:Arial;color:navy'><o:p> </o:p></span></font></p>

<p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size:
10.0pt;font-family:Arial;color:navy'>I’m assuming the error is related to
how I am selected the road geometry for the line_locate_point function.  If I
break it into three stages, and select the road geometry, and then select the
point using the sql you showed me, and then select the line_locate_point
function, it works fine.<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size:
10.0pt;font-family:Arial;color:navy'><o:p> </o:p></span></font></p>

<p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size:
10.0pt;font-family:Arial;color:navy'>If I can get this working, I’m
hoping to go another step further and wind up with a sql statement something
like:<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size:
10.0pt;font-family:Arial;color:navy'><o:p> </o:p></span></font></p>

<p class=MsoNormal style='margin-left:36.0pt'><font size=2 color=navy
face=Arial><span style='font-size:10.0pt;font-family:Arial;color:navy'>SELECT
line_substring(road_geom, start_measure, end_measure) FROM ( … sql to
select road geometry …, sql to select line_locate_point for start point as
above, sql to select line_locate_point for end point as above).<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size:
10.0pt;font-family:Arial;color:navy'><o:p> </o:p></span></font></p>

<p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size:
10.0pt;font-family:Arial;color:navy'>My other question is, is this the best way
to go about this?<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size:
10.0pt;font-family:Arial;color:navy'><o:p> </o:p></span></font></p>

<p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size:
10.0pt;font-family:Arial;color:navy'>Thanks in advance,<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size:
10.0pt;font-family:Arial;color:navy'>Rob<o:p></o:p></span></font></p>

<div>

<div class=MsoNormal align=center style='text-align:center'><font size=3
face="Times New Roman"><span style='font-size:12.0pt'>

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

</span></font></div>

<p class=MsoNormal><b><font size=2 face=Tahoma><span style='font-size:10.0pt;
font-family:Tahoma;font-weight:bold'>From:</span></font></b><font size=2
face=Tahoma><span style='font-size:10.0pt;font-family:Tahoma'>
postgis-users-bounces@postgis.refractions.net
[mailto:postgis-users-bounces@postgis.refractions.net] <b><span
style='font-weight:bold'>On Behalf Of </span></b>Rodrigo Martín LÓPEZ GREGORIO<br>
<b><span style='font-weight:bold'>Sent:</span></b> Wednesday, August 08, 2007
1:00 PM<br>
<b><span style='font-weight:bold'>To:</span></b> PostGIS Users Discussion<br>
<b><span style='font-weight:bold'>Subject:</span></b> Re: [postgis-users] SQL
Help selecting intersections</span></font><o:p></o:p></p>

</div>

<p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size:
12.0pt'><o:p> </o:p></span></font></p>

<p class=MsoNormal style='margin-bottom:12.0pt'><font size=3
face="Times New Roman"><span style='font-size:12.0pt'>Hi Rob.<br>
<br>
You can make something like this:<o:p></o:p></span></font></p>

<div style='margin-left:30.0pt'>

<p class=MsoNormal><i><font size=3 face="Times New Roman"><span
style='font-size:12.0pt;font-style:italic'>SELECT *,
Centroid(Intersection(road1.the_geom,road2.the_geom)) AS intersectionPoint FROM
<br>
(SELECT * FROM roads WHERE name LIKE 'Township Road 524') AS road1<br>
JOIN <br>
(SELECT * FROM roads WHERE name LIKE 'Range Road 20') AS road2<br>
ON</span></font></i> <i><span style='font-style:italic'><br>
road1.the_geom && road2.the_geom AND
Intersects(road1.the_geom,road2.the_geom)</span></i><o:p></o:p></p>

</div>

<p class=MsoNormal style='margin-bottom:12.0pt'><font size=3
face="Times New Roman"><span style='font-size:12.0pt'><br>
Obviously you can select just the columns you want o get extra info like X and
Y coordinates of the intersectionPoint doing something like: <o:p></o:p></span></font></p>

<div style='margin-left:30.0pt'>

<p class=MsoNormal><i><font size=3 face="Times New Roman"><span
style='font-size:12.0pt;font-style:italic'>SELECT *,
X(Centroid(Intersection(road1.the_geom,road2.the_geom))) AS xcoord,
Y(Centroid(Intersection(road1.the_geom,road2.the_geom))) AS Ycoord FROM <br>
(SELECT * FROM roads WHERE name LIKE 'Township Road 524') AS road1<br>
JOIN <br>
(SELECT * FROM roads WHERE name LIKE 'Range Road 20') AS road2<br>
ON</span></font></i> <i><span style='font-style:italic'><br>
road1.the_geom && road2.the_geom AND
Intersects(road1.the_geom,road2.the_geom)</span></i><o:p></o:p></p>

</div>

<p class=MsoNormal style='margin-bottom:12.0pt'><font size=3
face="Times New Roman"><span style='font-size:12.0pt'><br>
You can also modify the WHERE condition in the search of each individual road
like using ILIKE instead of LIKE to make the search case insensitive, use % in
the LIKE condition to search with just part of the road name, etc. <br>
<br>
(This is just a simple solution, you can get better results doing more complex
things like having a separated table with road names and road id, making the
search in this table and getting the ids of the matched roads and then, having
a index on your road table by road_id, accessing just the roads that have
matched the previous search, etc.) <br>
<br>
If you have any question, ask me again.<br>
<br>
Rodrigo.<o:p></o:p></span></font></p>

<div>

<p class=MsoNormal><span class=gmailquote><font size=3 face="Times New Roman"><span
style='font-size:12.0pt'>On 8/8/07, <b><span style='font-weight:bold'>Rob
McCulley</span></b> <<a href="mailto:RMcCulley@county24.com">RMcCulley@county24.com
</a>> wrote:</span></font></span><o:p></o:p></p>

<div link=blue vlink=purple>

<div>

<p><font size=2 face=Arial><span style='font-size:10.0pt;font-family:Arial'>Hi
there, </span></font><o:p></o:p></p>

<p><font size=2 face=Arial><span style='font-size:10.0pt;font-family:Arial'> </span></font><o:p></o:p></p>

<p><font size=2 face=Arial><span style='font-size:10.0pt;font-family:Arial'>I'm
looking for a little help with some SQL.  I've got a table of roads with
names.  I need to be able to get the intersection of two roads given the
two names of the roads.  The problem I have is, most of my roads have
multiple segments with the same name.</span></font><o:p></o:p></p>

<p><font size=2 face=Arial><span style='font-size:10.0pt;font-family:Arial'> </span></font><o:p></o:p></p>

<p><font size=2 face=Arial><span style='font-size:10.0pt;font-family:Arial'>Currently
I'm doing this in essentially three steps.  For example, lets say I'm
looking for the intersection of Township Road 524 and Range Road 20.  I
just took a look at my database, and there are two segments for Twp Rd 524 and
there are 5 segments for Rge Rd 20.  My current solution is:</span></font><o:p></o:p></p>

<p><font size=2 face=Arial><span style='font-size:10.0pt;font-family:Arial'> </span></font><o:p></o:p></p>

<p><font size=2 face=Arial><span style='font-size:10.0pt;font-family:Arial'>Step
1:  Select all segments of Twp Rd 524.</span></font><o:p></o:p></p>

<p><font size=2 face=Arial><span style='font-size:10.0pt;font-family:Arial'>Step
2: Iterate through each segment of Twp Rd 524 and select any segment of Rge Rd
20 that intersects it.  After I'm finished the iteration, I have two
geometries that intersect, one from Twp Rd 524 and one from Rge Rd 20.</span></font><o:p></o:p></p>

<p><font size=2 face=Arial><span style='font-size:10.0pt;font-family:Arial'>Step
3:  Select the intersection of the two geometries to get a point geometry.</span></font><o:p></o:p></p>

<p><font size=2 face=Arial><span style='font-size:10.0pt;font-family:Arial'> </span></font><o:p></o:p></p>

<p><font size=2 face=Arial><span style='font-size:10.0pt;font-family:Arial'>This
seems like a bit of a brute force method of doing things.  In the example
case, I end up making four separate queries to the database.  In some
cases it could be quite a few more.  I'm sure there is a more elegant
solution, but I can't quite get my head around how to do it.  Is it
possible to get the intersection of the roads given the names in a single SQL
statement?</span></font><o:p></o:p></p>

<p><font size=2 face=Arial><span style='font-size:10.0pt;font-family:Arial'> </span></font><o:p></o:p></p>

<p><font size=2 face=Arial><span style='font-size:10.0pt;font-family:Arial'>Thank
You,</span></font><o:p></o:p></p>

<p><font size=2 face=Arial><span style='font-size:10.0pt;font-family:Arial'>Rob
McCulley</span></font><o:p></o:p></p>

<p><font size=2 face=Arial><span style='font-size:10.0pt;font-family:Arial'>County
of <st1:place w:st="on"><st1:PlaceName w:st="on">Vermilion</st1:PlaceName> <st1:PlaceType
 w:st="on">River</st1:PlaceType></st1:place></span></font><o:p></o:p></p>

</div>

</div>

<p class=MsoNormal style='margin-bottom:12.0pt'><font size=3
face="Times New Roman"><span style='font-size:12.0pt'><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"
target="_blank">http://postgis.refractions.net/mailman/listinfo/postgis-users</a><o:p></o:p></span></font></p>

</div>

<p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size:
12.0pt'><o:p> </o:p></span></font></p>

</div>

</body>

</html>