How can I create a polygon using fields in PostgreSQL?

13,981

Solution 1

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))')

Solution 2

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

GeomFromText('POLYGON((long1 lat1, long2 lat2, long3 lat3))')
Share:
13,981
Dan Goldstein
Author by

Dan Goldstein

Hi, I'm Dan Goldstein. I've been a software developer both professionally and in my spare time since 2002. I've used a large number of languages and frameworks and have a lot of public code available under the Github and Bitbucket profiles thasmin. https://github.com/thasmin https://bitbucket.org/thasmin/ I created an open source Android podcast player called Podax. It was published and popular in the open source community around 2014 but time constraints prevented me from maintaining it. I do continue to work on it, and the newest branch contains the latest Android technologies, such as Kotlin and Architecture Components. At Teralogics, we created a video distribution system that took video from drones in Iraq and Afghanistan and brought it back to the US for analysis. I created the first version alone, then grew with the project to be the software lead of the team of 10 people, then the operations lead as the project matured. Under the strength of this project, the company was acquired for $39M in 2015. My software and business experience make me a strong contributor to any project. I look forward to going into more detail in an interview.

Updated on June 03, 2022

Comments

  • Dan Goldstein
    Dan Goldstein almost 2 years

    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?

  • Dan Goldstein
    Dan Goldstein almost 15 years
    That WKT statement looks like what I want, but the database was created by Drupal (a CMS) so the PostGIS template wasn't used when creating it. Can I convert text to polygon some other way?
  • Dan Goldstein
    Dan Goldstein almost 15 years
    I found the instructions on adding PostGIS to a database and got this to work.