I have a function to return a reduced size version of the geometries in our database.
CREATE OR REPLACE FUNCTION fog.small_kml(
shape geometry,
max_length integer)
RETURNS text
LANGUAGE 'plpgsql'
AS $BODY$
DECLARE kml text;
DECLARE tolerance float;
BEGIN
tolerance:=0.001;
SELECT ST_ASKML(shape) INTO kml;
WHILE LENGTH(kml) > max_length LOOP
SELECT ST_ASKML(ST_SIMPLIFY(shape, tolerance)) INTO kml;
tolerance = tolerance + 0.001;
END LOOP;
return kml;
END
$BODY$;
when I run this on certain geometries I get get the result
ERROR: parse error - invalid geometry
HINT: "<M" <-- parse error at position 2 within geometry
SQL state: XX000
occurring due to the statement SELECT ST_ASKML(ST_SIMPLIFY(shape, tolerance)) INTO kml;
. However, ST_ISVALID
says that the geometries are all valid.
Is there anything I can do to force the geometries into a state where I can simplify them? The length of the KML is around 12 million characters for the largest, which is why I want to simplify (and why I can't paste the whole thing here).
Each one starts <MultiGeometry><Polygon><outerBoundaryIs><LinearRing><coordinates>
and I've had no issues with them before other than being slow. It seems to be ST_SIMPLIFY
only that has an issue.
ST_Simplify
?SELECT ST_ASKML(ST_SIMPLIFY(ST_MAKEVALID(shape), tolerance)) INTO kml;
fixes it. I'll leave a while to let him answer. If he doesn't I'll self answer.