I'm triying to calculate the average elevation of some multilinestring points.
Is there a fastest way than this pgsql function?
CREATE OR REPLACE FUNCTION topstation.sig_moyenne_z(ingeom geometry)
RETURNS double precision AS
$BODY$
DECLARE
geom2 geometry;
sum double precision;
max double precision;
BEGIN
sum:=0;
geom2=ST_LineMerge(inGeom);
max:=ST_NPoints(geom2);
IF max > 1 THEN
FOR i IN 1..max
LOOP
sum := sum + ST_Z(ST_PointN(geom2,i));
END LOOP;
RETURN sum / max;
ELSE
return 9999;
END IF;
END;
$BODY$
LANGUAGE plpgsql IMMUTABLE
COST 100;