我想在Postgis中拉伸多边形以创建伪3D效果。为此,我编写了一个粗略的函数来实现它。这是非常多的测试代码,它为多边形上的每个点创建一个新的Y顶点,然后通过返回原始点将其关闭:-
CREATE OR REPLACE FUNCTION public.extrude_polygon(wkb_geometry_param geometry, height integer, simplify boolean DEFAULT false)
RETURNS geometry AS
$BODY$
DECLARE
f int;
ret_geom geometry;
wkb_geometry geometry;
BEGIN
--convert polygon to linestring
IF ST_GeometryType(wkb_geometry_param) != 'ST_Polygon' THEN
RETURN NULL;
END IF;
IF simplify THEN
wkb_geometry = ST_Simplify(ST_Transform(ST_Exteriorring(wkb_geometry_param), 27700), 0.5);
ELSE
wkb_geometry = ST_Transform(ST_Exteriorring(wkb_geometry_param), 27700);
END IF;
--initialise output geometry
ret_geom =ST_MakeLine(ST_PointN(wkb_geometry,1),ST_PointN(wkb_geometry,1));
--Move first point to up
SELECT ST_AddPoint(ret_geom,
ST_MakePoint(ST_X(ST_PointN(wkb_geometry, 1)),
ST_Y(ST_PointN(wkb_geometry, 1)) + height)
) into ret_geom;
FOR f IN 1..ST_NPoints(wkb_geometry) LOOP
IF f < ST_NPoints(wkb_geometry) THEN
--across to next high point
SELECT ST_AddPoint(ret_geom,
ST_MakePoint(ST_X(ST_PointN(wkb_geometry, f + 1)),
ST_Y(ST_PointN(wkb_geometry, f + 1)) + height)
) into ret_geom;
--down to next point
SELECT ST_AddPoint(ret_geom, ST_PointN(wkb_geometry,f + 1)) into ret_geom;
--back to last point
SELECT ST_AddPoint(ret_geom, ST_PointN(wkb_geometry,f)) into ret_geom;
--back then up again
SELECT ST_AddPoint(ret_geom, ST_PointN(wkb_geometry,f + 1)) into ret_geom;
SELECT ST_AddPoint(ret_geom,
ST_MakePoint(ST_X(ST_PointN(wkb_geometry, f + 1)),
ST_Y(ST_PointN(wkb_geometry, f + 1)) + height)
) into ret_geom;
ELSE
--across to first high point
SELECT ST_AddPoint(ret_geom,
ST_MakePoint(ST_X(ST_PointN(wkb_geometry, 1)),
ST_Y(ST_PointN(wkb_geometry, 1)) + height)
) into ret_geom;
SELECT ST_AddPoint(ret_geom, ST_PointN(wkb_geometry,1)) into ret_geom;
END IF;
END LOOP;
RETURN ST_Buffer(ST_Buffer(ST_MakePolygon(ret_geom),10), -10);
END;
$BODY$
LANGUAGE plpgsql
它适用于简单的多边形,但内部环有问题,但主要问题是它确实很慢。我需要将结果形状输出为可以在mapserver中着色和渲染的多边形。因此,最后进行缓冲区操作,这是我知道的将形状缩小为轮廓的唯一方法。
最终结果将是代表原始多边形的拉伸形状。然后,我可以将原始多边形偏移相同的拉伸距离,然后将其放在顶部以制作屋顶。
我考虑过在postgis-2.1.1中使用ST_Extrude函数,但会创建ST_PolyhedralSurface类型,但无法在mapserver中渲染它。据我所知,由于ST_Buffer不适用于ST_polyhedralsurfaces,因此无法创建此轮廓。
所以,我的问题是,我的功能可以改善吗?还是有更好的方法。输出需要按照我通过将偏移多边形放置到我的拉伸形状上所创建的图的外观进行查看。
好问题!也许您可以将数据输出为KML,以便通过挤出获得更大的便利性和灵活性?这里有一些出发点:postgis.net/docs/ST_AsKML.html,code.google.com/p/postexperiments/wiki/...,gdal.org/drv_libkml.html
—
布伦特爱德华兹