Chau Loi
Chau Loi

Reputation: 1225

How to convert list of points to GEOM in postgreSQL?

Well I have a list of points (lat and lon):

Like this:

[107.8188205,16.0573256],
[107.8188192,16.0576399],
[107.8188139,16.0580677],
[107.8187816,16.0585772],
[107.8188316,16.0589462],
[107.818921,16.059341],
[107.8191381,16.0600537],
[107.8194213,16.0606733]

I want convert these point into a geom (area inside these points) of PostgreSQL.

Then I can use function st_within to check if a point inside an area.

Upvotes: 2

Views: 527

Answers (1)

Alan Millirud
Alan Millirud

Reputation: 1192

there is a function ST_MakePolygon, which makes polygon from points

example

SELECT ST_MakePolygon( ST_GeomFromText('LINESTRING(75 29,77 29,77 29, 75 29)'));

https://postgis.net/docs/ST_MakePolygon.html

you can generate such string LINESTRING(75 29,77 29,77 29, 75 29) from your data and pass it into sql query

or another example

SELECT ST_MakePolygon(ST_GeomFromGeoJSON(
CONCAT('{"type":"LineString","coordinates":[', 
   '[107.8188205,16.0573256],
    [107.8188192,16.0576399],
    [107.8188139,16.0580677],
    [107.8187816,16.0585772],
    [107.8188316,16.0589462],
    [107.818921,16.059341],
    [107.8191381,16.0600537],
    [107.8194213,16.0606733],
    [107.8188205,16.0573256],
    [107.8188205,16.0573256]
', ']}')
))

last point must be same as first point

Upvotes: 3

Related Questions