Skip to main content
fixed logic
Source Link
Jakub Kania
  • 2.9k
  • 18
  • 22

You haven't stated what output you want so an array of geometry should do:

CREATE OR REPLACE FUNCTION simplify_npoints(inGeom geometry, maxPoints integer) RETURNS geometry[] AS $$ DECLARE outGeom geometry[]=ARRAY[]::geometry[]; points geometry[]=ARRAY[]::geometry[]; counter integer:=0; BEGIN IF maxPoints=1 THEN RAISE EXCEPTION 'maxPoints must be >1. Split not possible'; END IF; FOR i IN 1..ST_NPoints(inGeom) LOOP counter:=counter+1; points := array_append(points, ST_PointN(inGeom,i)); IF counter=maxPoints THEN outgeom:=array_append(outGeom,ST_MakeLine(points)); points := ARRAY[ST_PointN(inGeom,i)]; counter:=1; END IF; END LOOP; IF counter>1 THEN outgeom:=array_append(outGeom,ST_MakeLine(points)); END IF; RETURN outGeom; END; $$ LANGUAGE plpgsql IMMUTABLE; 

You haven't stated what output you want so an array of geometry should do:

CREATE OR REPLACE FUNCTION simplify_npoints(inGeom geometry, maxPoints integer) RETURNS geometry[] AS $$ DECLARE outGeom geometry[]=ARRAY[]::geometry[]; points geometry[]=ARRAY[]::geometry[]; counter integer:=0; BEGIN IF maxPoints=1 THEN RAISE EXCEPTION 'maxPoints must be >1. Split not possible'; END IF; FOR i IN 1..ST_NPoints(inGeom) LOOP counter:=counter+1; points := array_append(points, ST_PointN(inGeom,i)); IF counter=maxPoints THEN outgeom:=array_append(outGeom,ST_MakeLine(points)); points := ARRAY[ST_PointN(inGeom,i)]; counter:=1; END IF; END LOOP; RETURN outGeom; END; $$ LANGUAGE plpgsql IMMUTABLE; 

You haven't stated what output you want so an array of geometry should do:

CREATE OR REPLACE FUNCTION simplify_npoints(inGeom geometry, maxPoints integer) RETURNS geometry[] AS $$ DECLARE outGeom geometry[]=ARRAY[]::geometry[]; points geometry[]=ARRAY[]::geometry[]; counter integer:=0; BEGIN IF maxPoints=1 THEN RAISE EXCEPTION 'maxPoints must be >1. Split not possible'; END IF; FOR i IN 1..ST_NPoints(inGeom) LOOP counter:=counter+1; points := array_append(points, ST_PointN(inGeom,i)); IF counter=maxPoints THEN outgeom:=array_append(outGeom,ST_MakeLine(points)); points := ARRAY[ST_PointN(inGeom,i)]; counter:=1; END IF; END LOOP; IF counter>1 THEN outgeom:=array_append(outGeom,ST_MakeLine(points)); END IF; RETURN outGeom; END; $$ LANGUAGE plpgsql IMMUTABLE; 
Source Link
Jakub Kania
  • 2.9k
  • 18
  • 22

You haven't stated what output you want so an array of geometry should do:

CREATE OR REPLACE FUNCTION simplify_npoints(inGeom geometry, maxPoints integer) RETURNS geometry[] AS $$ DECLARE outGeom geometry[]=ARRAY[]::geometry[]; points geometry[]=ARRAY[]::geometry[]; counter integer:=0; BEGIN IF maxPoints=1 THEN RAISE EXCEPTION 'maxPoints must be >1. Split not possible'; END IF; FOR i IN 1..ST_NPoints(inGeom) LOOP counter:=counter+1; points := array_append(points, ST_PointN(inGeom,i)); IF counter=maxPoints THEN outgeom:=array_append(outGeom,ST_MakeLine(points)); points := ARRAY[ST_PointN(inGeom,i)]; counter:=1; END IF; END LOOP; RETURN outGeom; END; $$ LANGUAGE plpgsql IMMUTABLE;