<div dir="ltr">Hi James<div><br></div><div>Good you pointed to ST_Line_Interpolate. The use case I have in mind is that I have e.g. roads and want to smooth ithem before they are being rendered. </div><div>The input is a linestring and the output is another linestring with about same amount of coordinates (or less) - but smoothed.</div>
<div>Something like this <a href="http://www.codeproject.com/Articles/31859/Draw-a-Smooth-Curve-through-a-Set-of-2D-Points-wit">http://www.codeproject.com/Articles/31859/Draw-a-Smooth-Curve-through-a-Set-of-2D-Points-wit</a></div>
<div><br></div><div>Yours, Stefan </div></div><div class="gmail_extra"><br><br><div class="gmail_quote">2013/9/18 James David Smith <span dir="ltr"><<a href="mailto:james.david.smith@gmail.com" target="_blank">james.david.smith@gmail.com</a>></span><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I'm sure that it's because I don't quite understand what you are<br>
trying to do, but why not just use the native PostGIS function -<br>
<a href="http://postgis.net/docs/manual-2.0/ST_Line_Interpolate_Point.html" target="_blank">http://postgis.net/docs/manual-2.0/ST_Line_Interpolate_Point.html</a>  ?<br>
<div><div class="h5"><br>
On 18 September 2013 15:06,  <<a href="mailto:Michael.Scholz@dlr.de">Michael.Scholz@dlr.de</a>> wrote:<br>
> Hey!<br>
><br>
> First of all I can recommend two tutorials about how to use PL/R in PostGIS<br>
> [1][2]. One possibility to interpolate splines with R is to use R's xspline<br>
> package [3]. Some theoretical background 'bout those x-splines you'll find<br>
> in the related paper [4]. I tried some other spline-methods in R but with<br>
> dubious results. xspline works fine for me. A wrapping PL/R-function could<br>
> look like this:<br>
><br>
><br>
><br>
> /**<br>
><br>
> * Interpolates a LineString using R's X-spline-method<br>
><br>
> *<br>
><br>
>  * Parameters:<br>
><br>
> *   1st - A *single* PostGIS-geometry (POINT, LINESTRING or POLYGON) *as<br>
> text*: Use ST_AsEWKT() to pass a geometry as text.<br>
><br>
> *   2nd - Shape parameter used in R's X-spline-method [-1.0,...,1.0]. A<br>
> shape of 0.0 does not interpolate anything. Default: 0.5.<br>
><br>
> *<br>
><br>
>  * Returns:<br>
><br>
> *   A PostGIS-LINESTRING-geometry covering the interpolated points.<br>
><br>
> *<br>
><br>
>  * Usage:<br>
><br>
> *   > SELECT interpolateXSpline(ST_AsEWKT(geom), 1.0) FROM your_table<br>
><br>
> */<br>
><br>
> create or replace function interpolateXSpline(text, float DEFAULT 0.5)<br>
> returns geometry as $$<br>
><br>
>   # Select the points (also from line or polygon-boundary) into a data frame<br>
><br>
>   points <- pg.spi.exec(<br>
><br>
>     sprintf("WITH pts AS (SELECT (ST_DumpPoints(ST_GeomFromEWKT(%1$s))).geom<br>
> AS geom)<br>
><br>
>       SELECT ST_X(geom) AS x, ST_Y(geom) AS y FROM pts;",<br>
><br>
>         pg.quoteliteral(arg1)<br>
><br>
>     )<br>
><br>
>   )<br>
><br>
><br>
><br>
>   # Interpolate spline<br>
><br>
>   plot(points$x, points$y, type="n") # Dummy-call of plot() for xspline to<br>
> work<br>
><br>
>   spline_pts <- xspline(points$x, points$y, shape=arg2, draw=FALSE)<br>
><br>
>   # Here you could simply output spline_pts and return.<br>
><br>
>   # Otherwise proceed to build a WKT linestring as shown below.<br>
><br>
><br>
><br>
>   # LineString-creation: Build beginning of WKT string for output to PostGIS<br>
><br>
>   out_line = "LINESTRING("<br>
><br>
>     for (i in 1:(length(spline_pts$x)-1)) {<br>
><br>
>       out_line = sprintf(<br>
><br>
>         "%1$s %2$f %3$f,",<br>
><br>
>         out_line, spline_pts$x[i], spline_pts$y[i]<br>
><br>
>       )<br>
><br>
>     }<br>
><br>
>   # LineString-creation: Append middle part<br>
><br>
>   out_line =<br>
><br>
>     sprintf("%1$s %2$f %3$f)",<br>
><br>
>       out_line,<br>
><br>
>       spline_pts$x[length(spline_pts$x)],<br>
><br>
>       spline_pts$y[length(spline_pts$y)]<br>
><br>
>     )<br>
><br>
>   # LineString-creation: Finalise (close)<br>
><br>
>   oline <- pg.spi.exec(<br>
><br>
>     sprintf("SELECT ST_GeomFromText('%1$s', ST_SRID(ST_GeomFromEWKT(%2$s)))<br>
> AS ln;",<br>
><br>
>       out_line,<br>
><br>
>       pg.quoteliteral(arg1)<br>
><br>
>     )<br>
><br>
>  )<br>
><br>
>   return(oline$ln[1])<br>
><br>
> $$ language 'plr' IMMUTABLE;<br>
><br>
><br>
><br>
> I tried to get it to work with the 1st parameter as bytea to be able to pass<br>
> binary geometry but got unserialize-errors in R. So for now you have to<br>
> convert your input geometry to EWKT first and pass it as text.<br>
><br>
><br>
><br>
> Have fun! Michi<br>
><br>
><br>
><br>
> [1] <a href="http://www.bostongis.com/?content_name=postgresql_plr_tut01#87" target="_blank">http://www.bostongis.com/?content_name=postgresql_plr_tut01#87</a><br>
><br>
> [2] <a href="http://www.bostongis.com/?content_name=postgresql_plr_tut02#98" target="_blank">http://www.bostongis.com/?content_name=postgresql_plr_tut02#98</a><br>
><br>
> [3] <a href="http://stat.ethz.ch/R-manual/R-devel/library/graphics/html/xspline.html" target="_blank">http://stat.ethz.ch/R-manual/R-devel/library/graphics/html/xspline.html</a><br>
><br>
> [4]<br>
> <a href="http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.44.5770&rep=rep1&type=pdf" target="_blank">http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.44.5770&rep=rep1&type=pdf</a><br>

><br>
><br>
</div></div>> _______________________________________________<br>
> postgis-users mailing list<br>
> <a href="mailto:postgis-users@lists.osgeo.org">postgis-users@lists.osgeo.org</a><br>
> <a href="http://lists.osgeo.org/cgi-bin/mailman/listinfo/postgis-users" target="_blank">http://lists.osgeo.org/cgi-bin/mailman/listinfo/postgis-users</a><br>
_______________________________________________<br>
postgis-users mailing list<br>
<a href="mailto:postgis-users@lists.osgeo.org">postgis-users@lists.osgeo.org</a><br>
<a href="http://lists.osgeo.org/cgi-bin/mailman/listinfo/postgis-users" target="_blank">http://lists.osgeo.org/cgi-bin/mailman/listinfo/postgis-users</a><br>
</blockquote></div><br></div>