2013-09-27 45 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 || '))'