Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Points to Polygon using PostGIS

I want to create a polygon table using PostGIS. Each row in table 'point' has three-point ID. Table 'point_location' has the location information of points. I googled this question but did not find the answer. What's wrong with the following code?

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
like image 803
Ben Avatar asked Sep 27 '13 08:09

Ben


People also ask

How do you convert Linestring to polygons?

geometry. Polygon to simply convert to line string to a polygon. It will connect the first and last coordinates. Try Polygon([(0, 0), (1, 1), (1, 2), (0, 1)]) or Polygon(s1) to produce POLYGON ((0 0, 1 1, 1 2, 0 1, 0 0)).

What is wkt in PostGIS?

Well-Known Text (WKT)

What is the difference between polygon and Multipolygon?

A MULTIPOLYGON is a collection of Polygons. MultiPolygons are useful for gathering a group of Polygons into one geometry. For example, you may want to gather the Polygons denoting a group of properties in a particular municipality.

What is Srid in PostGIS?

An SRID indicates coordinates of data. For example: Supported SRIDs can be queried from PostGIS, such as query coordinates related to Beijing and China. postgres=# select * from spatial_ref_sys where srtext ~* 'beijing'; postgres=# select * from spatial_ref_sys where srtext ~* 'china';


1 Answers

A better way to construct a polygon from points is to use PostGIS' geometry constructors. This way, you avoid the need to convert binary → text → binary (WKB → WKT → WKB), which is slower, lossy, and is prone to text formatting distractions as evidenced by the missing ||. For example, try:

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
like image 69
Mike T Avatar answered Sep 23 '22 09:09

Mike T