Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a polygon using fields in PostgreSQL?

I have 8 real values in a table that I'd like to combine into a polygon. I haven't been able to figure out how to create a polygon using these values though. I keep trying variations of

SELECT polygon(lat1,lon1,lat2,lon2,lat3,lon3,lat4,lon4) FROM table;

but keep getting errors about the polygon function not existing or an invalid input syntax for type polygon. Has anyone done this before?

like image 656
Dan Goldstein Avatar asked Jul 09 '09 18:07

Dan Goldstein


2 Answers

The syntax for a regular postgres polygon is more like:

insert into geo_table values (1, '((2,2),(3,4),(3,6),(1,1))');

Where 1 is some id and the quoted entry is the polygon. I would expect the query to be similar, you probably need parentheses etc for the coordinates. Typically for geospatial data you want (Lon Lat) coordinates. Postgis also takes WKT statements like:

GeomFromText('POLYGON((long1 lat1, long2 lat2, long3 lat3))')

like image 176
unmounted Avatar answered Oct 21 '22 14:10

unmounted


As mentioned by bvmou - GeomFromText will work fine. I'll just add a small syntax update:

GeomFromText('POLYGON((long1 lat1, long2 lat2, long3 lat3))')
like image 22
user569142 Avatar answered Oct 21 '22 13:10

user569142