[postgis-users] Center of a path.
Luís Mota
luis.mota at iscte.pt
Sun Jan 12 12:32:25 PST 2003
Hi again.
Here is the function.
It does a bit more than what you asked: it computes points in any place in
the linestring, measured in a percentage of the line, and it can also
translate the point some meters to the left or right. The first argument is
the linestring, the second a percentage that will determine the distance
from the startpoint, the third the number of meters you want to translate
the resulting point from the line, and finally the fourth is if the
translation should be to the left (it is miningless if the third argument is
0).
If you simply want to find the point in the middle of the line, then please
use:
pointInLine(<lineString>,0.5,0,true)
Best regards, Luís Mota
CREATE OR REPLACE FUNCTION pointInLine (GEOMETRY,DOUBLE PRECISION,DOUBLE
PRECISION,BOOLEAN)
RETURNS GEOMETRY AS
'DECLARE
lineString ALIAS FOR $1;
percentage ALIAS FOR $2;
distance ALIAS FOR $3;
toLeft ALIAS FOR $4;
distanceFromStart DOUBLE PRECISION;
elapsedDistance DOUBLE PRECISION;
npoints INTEGER;
tempLineString GEOMETRY;
percToAdd DOUBLE PRECISION;
dx DOUBLE PRECISION;
dy DOUBLE PRECISION;
pointInLine GEOMETRY;
BEGIN
IF geometrytype(lineString) != ''LINESTRING'' OR
percentage < 0 OR percentage > 1 THEN
RAISE EXCEPTION ''Wrong arguments (LINESTRING,[0,1])'';
END IF;
--RAISE NOTICE ''percentagem %, geom:%'',percentage,lineString;
distanceFromStart := length(lineString)*percentage;
elapsedDistance = 0;
SELECT numpoints(lineString) INTO npoints;
-- RAISE NOTICE ''npoints %'',npoints;
FOR i IN 0..npoints-2 LOOP
-- RAISE NOTICE ''Iteracao'';
tempLineString :=
geomfromtext(''LINESTRING(''||
x(pointn(lineString,i))||'' ''||
y(pointn(lineString,i))||'',''||
x(pointn(lineString,i+1))||'' ''||
y(pointn(lineString,i+1))||'')'',
srid(lineString));
IF elapsedDistance+length(tempLineString)
< distanceFromStart THEN
elapsedDistance := elapsedDistance
+length(tempLineString);
ELSE
percToAdd :=
(distanceFromStart-elapsedDistance)
/ length(tempLineString);
dx := x(endpoint(tempLineString))
- x(startpoint(tempLineString));
dy := y(endpoint(tempLineString))
- y(startpoint(tempLineString));
pointInLine :=
translate(startpoint(tempLineString),
dx*percToAdd,dy*percToAdd,0);
IF distance > 0 THEN
RETURN
translatePointFromLine(tempLineString,
pointInLine,distance,toLeft);
ELSE
RETURN pointInLine;
END IF;
END IF;
END LOOP;
-- if there was some problem...
RETURN startpoint(lineString);
END'
LANGUAGE 'plpgsql';
----- Original Message -----
From: "cuicui" <meuh.meuh at noos.fr>
To: "PostGIS Users Discussion" <postgis-users at postgis.refractions.net>
Sent: Saturday, January 11, 2003 10:32 PM
Subject: Re: [postgis-users] Center of a path.
> Luís Mota wrote:
> > ----- Original Message ----- From: "cuicui" <meuh.meuh at noos.fr>
> >
> >> The problem is that this "centre" is not necessary on the path. I
> >> would like to know if a function exist and can find the centre of a
> >> path with this definition : "A point ON the path, which is half way
> >> from each end of the path".
> >
> > Hi.
> >
> > I had a similar need and developed a small plpgsql to compute the
> > center of a linestring. If you need this, I can send the code.
> >
> > Best regards, Luís Mota
>
> Yes, it would be very helpful and gentle ;o)
>
> Thank you very much in advance,
>
> Cheers,
>
> Nicolas
>
>
> _______________________________________________
> postgis-users mailing list
> postgis-users at postgis.refractions.net
> http://postgis.refractions.net/mailman/listinfo/postgis-users
More information about the postgis-users
mailing list