gpt4 book ai didi

postgresql - Postgis - 如何简化多边形的起点

转载 作者:行者123 更新时间:2023-11-29 11:43:20 24 4
gpt4 key购买 nike

我正在使用 ST_Simplify 来简化一些多边形。它适用于其中的大多数,但我有一些带有不必要起点的多边形,我想将其删除。

这是一个可以简化的多边形示例:

SELECT ST_AsText(ST_Simplify(ST_GeomFromText(
'POLYGON((0 0, 5 0, 10 0, 10 10, 0 10, 0 0))'
), 1))

这是一个无法简化的等效多边形(相同点,不同起点)的示例:

SELECT ST_AsText(ST_Simplify(ST_GeomFromText(
'POLYGON((5 0, 10 0, 10 10, 0 10, 0 0, 5 0))'
), 1))

我不希望 Douglas-Peucker 算法 (ST_Simplify) 改变多边形的起点或终点。我应该怎么做才能简化第二个多边形?

编辑我可能有一些凹多边形:

SELECT ST_AsText(ST_Simplify(ST_GeomFromText(
'POLYGON((5 0, 10 0, 8 8, 10 10, 0 10, 0 0, 5 0))'
), 1))

最佳答案

按照 John Barça 的示例,添加距离测试以简化关闭点,并使其适用于所有几何形状,我得到了这个解决方案:

CREATE OR REPLACE FUNCTION simplify_geometry(geom geometry, tolerance float)
RETURNS geometry AS
$BODY$

DECLARE geomType text;
DECLARE simplified geometry;
DECLARE testSegment geometry;
DECLARE simplifiedElements geometry[];

BEGIN

geomType := GeometryType(geom);

IF ST_IsEmpty(geom) THEN
RETURN geom;
ELSIF geomType ~ '(MULTI|COLLECTION)' THEN
simplifiedElements := array(SELECT simplify_geometry((ST_Dump(geom)).geom, tolerance));
RETURN ST_Collect(simplifiedElements);
ELSIF geomType = 'LINESTRING' THEN
simplified := ST_Simplify(geom, tolerance);

IF ST_IsClosed(simplified) THEN
testSegment := ST_MakeLine(ST_PointN(simplified, 2), ST_PointN(simplified, ST_NPoints(simplified)-1));
IF ST_Distance(ST_StartPoint(simplified), testSegment) < tolerance THEN
simplified := ST_RemovePoint(simplified, 0);
simplified := ST_RemovePoint(simplified, ST_NPoints(simplified)-1);
simplified := ST_AddPoint(simplified, ST_StartPoint(simplified));
END IF;
END IF;

RETURN simplified;
ELSIF geomType = 'POLYGON' THEN
simplifiedElements := array(SELECT simplify_geometry(ST_ExteriorRing((ST_DumpRings(geom)).geom), tolerance));
RETURN ST_MakePolygon(simplifiedElements[1], simplifiedElements[2:array_length(simplifiedElements,1)]);
ELSE
RETURN ST_Simplify(geom, tolerance);
END IF;

END
$BODY$ LANGUAGE plpgsql IMMUTABLE

关于postgresql - Postgis - 如何简化多边形的起点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24998203/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com