2013-09-27 110 views
4

我想使用PostGIS创建一个多边形表。表'点'中的每一行都有三个点ID。 表'point_location'具有点的位置信息。我GOOGLE了这个问题,但没有找到答案。下面的代码有什么问题?将点转换为多边形使用PostGIS

select ST_GeomFromText('POLYGON((' || b.x || ' ' || b.y || ',' || c.x || ' ' || c.y || ',' || d.x || ' ' || d.y || ',' || b.x || ' ' || b.y'))',4326) as polygon 
from point a, point_location b, point_location c, point_location d 
where a.p1=b.point_id and a.p2=c.point_id and a.p3=d.point_id 
+1

你应该问这个问题在gis.stackexchange.com –

回答

8

从点构造多边形的更好方法是使用PostGIS' geometry constructors。通过这种方式,您可以避免需要转换二进制文本→二进制文件(WKB → WKT → WKB),该文件速度较慢,有损耗,并且易于发生文本格式分散,如缺少||所证明的那样。例如,尝试:

SELECT ST_MakePolygon(ST_MakeLine(ARRAY[b, c, d, b])) 
FROM point a, point_location b, point_location c, point_location d 
WHERE a.p1=b.point_id and a.p2=c.point_id and a.p3=d.point_id 
+0

好!这个功能比较好。谢谢。 – Ben

0

b.y'))'应该改成b.y || '))'