ST_MakePolygon — Creates a Polygon or CurvePolygon from a shell and optional list of holes.
geometry ST_MakePolygon(geometry linestring);
geometry ST_MakePolygon(geometry outerlinestring, geometry[] interiorlinestrings);
Creates a Polygon or CurvePolygon formed by the given shell and optional array of holes. Input geometries must be closed LineStrings, CircularStrings, CompoundCurves, or NURBSCurves (rings). If any input ring is curved, the result is a CurvePolygon.
Variant 1: Accepts one shell ring.
Variant 2: Accepts a shell ring and an array of inner (hole) rings. A geometry array can be constructed using the PostgreSQL array_agg(), ARRAY[] or ARRAY() constructs.
|
|
|
此函数不接受多行字符串。 使用 ST_LineMerge生成线串。它还使用ST_Dump 来提取线串。 |
该函数支持 3d 并且不会丢失 z-index。
Enhanced: 3.7.0 - Support for curved input rings was introduced.
从 2D 线串生成多边形。
SELECT ST_MakePolygon(ST_GeomFromText('LINESTRING(75 29,77 29,77 29, 75 29)'));
使用 ST_StartPoint 和 ST_AddPoint闭合开放线串以生成面。
SELECT ST_MakePolygon(ST_AddPoint(foo.open_line, ST_StartPoint(foo.open_line)) )
FROM (
SELECT ST_GeomFromText('LINESTRING(75 29,77 29,77 29, 75 29)') As open_line) As foo;
从 3D 线串生成多边形
SELECT ST_AsEWKT(ST_MakePolygon('LINESTRING(75.15 29.53 1,77 29 1,77.6 29.5 1, 75.15 29.53 1)'));
POLYGON((75.15 29.53 1,77 29 1,77.6 29.5 1,75.15 29.53 1))
从具有 M 值的线串生成多边形
SELECT ST_AsEWKT(ST_MakePolygon('LINESTRINGM(75.15 29.53 1,77 29 1,77.6 29.5 2, 75.15 29.53 2)'));
POLYGONM((75.15 29.53 1,77 29 1,77.6 29.5 2,75.15 29.53 2))
Create a CurvePolygon from a CircularString shell.
SELECT ST_AsText(ST_MakePolygon('CIRCULARSTRING(0 0,1 1,2 0,1 -1,0 0)'));
CURVEPOLYGON(CIRCULARSTRING(0 0,1 1,2 0,1 -1,0 0))
创建一个带有额外孔的环形多边形
SELECT ST_MakePolygon(
ST_ExteriorRing(ST_Buffer(ring.line,10)),
ARRAY[ ST_Translate(ring.line, 1, 1),
ST_ExteriorRing(ST_Buffer(ST_Point(20,20),1)) ]
)
FROM (SELECT ST_ExteriorRing(ST_Buffer(ST_Point(10,10),10,10)) AS line ) AS ring;
创建一组省份边界,其中的孔代表湖泊。输入是省份多边形/多边形表和水域线串表。 形成湖泊的线是通过使用ST_IsClosed确定的。 使用ST_Boundary 提取省份线条。 根据ST_MakePolygon 的要求,通过使用 ST_LineMerge将边界强制为单个 LineString。 (但是,请注意,如果一个省份拥有多个区域或拥有岛屿,这将生成无效的多边形。)使用 LEFT JOIN 可确保包括所有省份,即使它们没有湖泊。
|
|
|
使用 CASE 构造是因为将空数组传递到 ST_MakePolygon 会导致 NULL 返回值。 |
SELECT p.gid, p.province_name,
CASE WHEN array_agg(w.geom) IS NULL
THEN p.geom
ELSE ST_MakePolygon(
ST_LineMerge(ST_Boundary(p.geom)),
array_agg(w.geom)) END
FROM
provinces p LEFT JOIN waterlines w
ON (ST_Within(w.geom, p.geom) AND ST_IsClosed(w.geom))
GROUP BY p.gid, p.province_name, p.geom;
另一种技术是利用相关子查询和 ARRAY() 构造函数将行集转换为数组。
SELECT p.gid, p.province_name,
CASE WHEN EXISTS( SELECT w.geom
FROM waterlines w
WHERE ST_Within(w.geom, p.geom)
AND ST_IsClosed(w.geom))
THEN ST_MakePolygon(
ST_LineMerge(ST_Boundary(p.geom)),
ARRAY( SELECT w.geom
FROM waterlines w
WHERE ST_Within(w.geom, p.geom)
AND ST_IsClosed(w.geom)))
ELSE p.geom
END AS geom
FROM provinces p;