PostGIS connect points to 1 closing/bounding line
I have this points represented as geom:
And when I apply:
ST_MakeLine(points.geom)I got this:
When I need something like closing/bounding line (and even better the whole area as 1 geom):
This is how I get the points for example:
SELECT DISTINCT (ST_DumpPoints(buildings.geom)).geom --AS geom FROM buildings WHERE ST_DWithin(buildings.geom, ST_GeomFromText('POINT(lon lat)', 4326), radius)
ST_ConcaveHull(ST_Collect(vertices.geom), 0.99, false)resulted almost the solution:
The reason is as documented here: "he basic approach is that it first creates a convexhull of the geometry and then uses the ST_ClosestPoint function introduced in PostGIS 1.5 to cave in the hull to transform it into a concave hull. That's the basic idea". We can see that the points not connected properly are those where the issue with the closest point is Misleading…
What you want to do isn't really possible in, a general sense, without giving the database more information. It might be clear to a human what the correct solution is, but the machine isn't quite there yet.
I tried ST_ConcaveHull with 0.99, and got these two results:
The difference is a small perturbation of the inside point on the right (I exaggerated it for effect.)
vinayan's suggestion, that you order the points, is, I think, the right one. You could do that by assigning an ID to each one and ordering them on it.
Another thing you could try is a Delaunay triangulation. Build the triangulation, then remove edges greater than a certain length from the boundary until the desired shape emerges. This would still require some intervention on your part. (I believe this is similar to the idea behind alpha shapes.)