<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<div class="moz-cite-prefix">This should work:<br>
<br>
create or replace function flow(integer) RETURNS TABLE (gid int,
geom geometry, geojson text) AS<br>
$$ <br>
<br>
WITH RECURSIVE flow(gid, geom) AS (<br>
<br>
SELECT e.gid, e.geom FROM electric_line e, fuses f WHERE
ST_DWithin(ST_AsText(e.geom)::geometry,
ST_AsText(f.geom)::geometry, 0.01) AND f.gid=$1<br>
<br>
UNION ALL<br>
<br>
SELECT n.gid, n.geom<br>
<br>
FROM electric_line n, flow f<br>
<br>
WHERE
ST_DWithin(ST_EndPoint(f.geom),ST_StartPoint(n.geom),0.01)<br>
<br>
)<br>
<br>
SELECT *, ST_ASGeoJSON(ST_Transform(geom,4326),6) AS geojson<br>
<br>
FROM flow;<br>
<br>
$$<br>
LANGUAGE SQL;<br>
<br>
<br>
SELECT * FROM flow(1);<br>
<br>
<br>
On 12/09/2013 03:57 PM, Jonatan Malaver wrote:<br>
</div>
<blockquote
cite="mid:58E13A9FA1D4994EB29384A8025A97B216BE81DC@mbx027-e1-nj-6.exch027.domain.local"
type="cite">
<meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1">
<meta name="Generator" content="Microsoft Word 15 (filtered
medium)">
<style><!--
/* Font Definitions */
@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:0in;
margin-bottom:.0001pt;
font-size:11.0pt;
font-family:"Calibri","sans-serif";}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:#0563C1;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:#954F72;
text-decoration:underline;}
span.EmailStyle17
{mso-style-type:personal-compose;
font-family:"Calibri","sans-serif";
color:windowtext;}
.MsoChpDefault
{mso-style-type:export-only;
font-family:"Calibri","sans-serif";}
@page WordSection1
{size:8.5in 11.0in;
margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
{page:WordSection1;}
--></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]-->
<div class="WordSection1">
<p class="MsoNormal">Hello,<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"> I’m new to postgis/postgresql. I was
wondering if anyone could tell me how to make a function out
of this sql query:<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">WITH RECURSIVE flow(gid, geom) AS (<o:p></o:p></p>
<p class="MsoNormal"> SELECT e.gid, e.geom FROM electric_line
e, fuses f WHERE ST_DWithin(ST_AsText(e.geom)::geometry,
ST_AsText(f.geom)::geometry, 0.01) AND f.gid=' . $_GET['fuse']
. '<o:p></o:p></p>
<p class="MsoNormal"> UNION ALL<o:p></o:p></p>
<p class="MsoNormal"> SELECT n.gid, n.geom<o:p></o:p></p>
<p class="MsoNormal"> FROM electric_line n, flow f<o:p></o:p></p>
<p class="MsoNormal"> WHERE
ST_DWithin(ST_EndPoint(f.geom),ST_StartPoint(n.geom),0.01)<o:p></o:p></p>
<p class="MsoNormal"> )<o:p></o:p></p>
<p class="MsoNormal">SELECT *,
ST_ASGeoJSON(ST_Transform(geom,4326),6) AS geojson<o:p></o:p></p>
<p class="MsoNormal">FROM flow;<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"> I’d like to make a function flow(gid)<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Thanks,<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Jon<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<br>
<fieldset class="mimeAttachmentHeader"></fieldset>
<br>
<pre wrap="">_______________________________________________
postgis-users mailing list
<a class="moz-txt-link-abbreviated" href="mailto:postgis-users@lists.osgeo.org">postgis-users@lists.osgeo.org</a>
<a class="moz-txt-link-freetext" href="http://lists.osgeo.org/cgi-bin/mailman/listinfo/postgis-users">http://lists.osgeo.org/cgi-bin/mailman/listinfo/postgis-users</a></pre>
</blockquote>
<br>
</body>
</html>